format_text Subroutine

public subroutine format_text(src, opts, out, stat, errmsg, diags)

Format src. On success out is the formatted text and stat is FMT_OK.

As a safety check, the code tokens of the result are compared with those of the input. If they differ, which would be a bug in the formatter, out is the unchanged input and stat is FMT_ERR_INTERNAL.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

source text to format

type(format_options_t), intent(in) :: opts

formatting options

character(len=:), intent(out), allocatable :: out

the formatted text

integer, intent(out) :: stat

FMT_OK or FMT_ERR_INTERNAL

character(len=:), intent(out), allocatable :: errmsg

error message; unallocated if stat is FMT_OK

type(diagnostic_t), intent(out), optional, allocatable :: diags(:)

problems found while formatting


Calls

proc~~format_text~~CallsGraph proc~format_text format_text proc~convert_relational convert_relational proc~format_text->proc~convert_relational proc~format_pass format_pass proc~format_text->proc~format_pass proc~limit_diagnostics limit_diagnostics proc~format_text->proc~limit_diagnostics proc~long_code_lines long_code_lines proc~format_text->proc~long_code_lines proc~relayout relayout proc~format_text->proc~relayout proc~sort_use sort_use proc~format_text->proc~sort_use proc~split_long_lines split_long_lines proc~format_text->proc~split_long_lines proc~to_s to_s proc~format_text->proc~to_s proc~tokenize tokenize proc~format_text->proc~tokenize proc~too_long too_long proc~format_text->proc~too_long proc~builder_append string_builder_t%builder_append proc~convert_relational->proc~builder_append proc~builder_get string_builder_t%builder_get proc~convert_relational->proc~builder_get proc~is_chevron is_chevron proc~convert_relational->proc~is_chevron proc~parse_source parse_source proc~convert_relational->proc~parse_source proc~token_lower token_lower proc~convert_relational->proc~token_lower proc~token_text token_text proc~convert_relational->proc~token_text proc~format_pass->proc~to_s proc~analyze_structure analyze_structure proc~format_pass->proc~analyze_structure proc~compute_case compute_case proc~format_pass->proc~compute_case proc~compute_gaps compute_gaps proc~format_pass->proc~compute_gaps proc~layout layout proc~format_pass->proc~layout proc~format_pass->proc~parse_source proc~set_views set_views proc~format_pass->proc~set_views proc~limit_diagnostics->proc~to_s proc~longest_line longest_line proc~limit_diagnostics->proc~longest_line proc~limit_diagnostics->proc~parse_source proc~long_code_lines->proc~parse_source proc~utf8_width utf8_width proc~long_code_lines->proc~utf8_width proc~relayout->proc~format_pass proc~relayout->proc~builder_append proc~relayout->proc~builder_get proc~sort_use->proc~builder_append proc~sort_use->proc~builder_get proc~is_use_statement is_use_statement proc~sort_use->proc~is_use_statement proc~module_key module_key proc~sort_use->proc~module_key proc~sort_use->proc~parse_source proc~set_view set_view proc~sort_use->proc~set_view proc~sort_only_list sort_only_list proc~sort_use->proc~sort_only_list proc~sorted_order sorted_order proc~sort_use->proc~sorted_order proc~whole_lines whole_lines proc~sort_use->proc~whole_lines proc~break_point break_point proc~split_long_lines->proc~break_point proc~split_long_lines->proc~builder_append proc~split_long_lines->proc~builder_get proc~builder_length string_builder_t%builder_length proc~split_long_lines->proc~builder_length proc~split_long_lines->proc~parse_source proc~stands_alone stands_alone proc~split_long_lines->proc~stands_alone proc~split_long_lines->proc~token_text proc~split_long_lines->proc~utf8_width proc~add add proc~tokenize->proc~add proc~comment_kind comment_kind proc~tokenize->proc~comment_kind proc~dotop_at dotop_at proc~tokenize->proc~dotop_at proc~dotop_end dotop_end proc~tokenize->proc~dotop_end proc~fypp_inline_end fypp_inline_end proc~tokenize->proc~fypp_inline_end proc~is_blank is_blank proc~tokenize->proc~is_blank proc~is_digit is_digit proc~tokenize->proc~is_digit proc~is_preproc_start is_preproc_start proc~tokenize->proc~is_preproc_start proc~is_quote is_quote proc~tokenize->proc~is_quote proc~lex_number lex_number proc~tokenize->proc~lex_number proc~line_end line_end proc~tokenize->proc~line_end proc~name_end name_end proc~tokenize->proc~name_end proc~operator_end operator_end proc~tokenize->proc~operator_end proc~peek peek proc~tokenize->proc~peek proc~preproc_end preproc_end proc~tokenize->proc~preproc_end proc~string_end string_end proc~tokenize->proc~string_end proc~longest_code longest_code proc~too_long->proc~longest_code proc~add->proc~is_blank proc~add->proc~peek proc~is_code_char is_code_char proc~add->proc~is_code_char proc~push~2 token_list_t%push proc~add->proc~push~2 proc~analyze_structure->proc~to_s proc~analyze_structure->proc~token_lower proc~classify_statement classify_statement proc~analyze_structure->proc~classify_statement proc~closes closes proc~analyze_structure->proc~closes proc~conditional_kind conditional_kind proc~analyze_structure->proc~conditional_kind proc~construct_label construct_label proc~analyze_structure->proc~construct_label proc~fypp_depths fypp_depths proc~analyze_structure->proc~fypp_depths proc~middle_fits middle_fits proc~analyze_structure->proc~middle_fits proc~stack_push scope_stack_t%stack_push proc~analyze_structure->proc~stack_push proc~stack_restore scope_stack_t%stack_restore proc~analyze_structure->proc~stack_restore proc~stack_snapshot scope_stack_t%stack_snapshot proc~analyze_structure->proc~stack_snapshot proc~break_point->proc~token_lower proc~break_point->proc~token_text proc~break_point->proc~utf8_width proc~priority priority proc~break_point->proc~priority proc~comment_kind->proc~peek proc~is_letter is_letter proc~comment_kind->proc~is_letter proc~lowercase lowercase proc~comment_kind->proc~lowercase proc~kind_part kind_part proc~compute_case->proc~kind_part proc~names_case names_case proc~compute_case->proc~names_case proc~statement_case statement_case proc~compute_case->proc~statement_case proc~comment_gaps comment_gaps proc~compute_gaps->proc~comment_gaps proc~statement_gaps statement_gaps proc~compute_gaps->proc~statement_gaps proc~dotop_at->proc~peek proc~dotop_at->proc~is_letter proc~dotop_end->proc~name_end proc~dotop_end->proc~peek proc~is_name_char is_name_char proc~dotop_end->proc~is_name_char proc~dotop_end->proc~lowercase proc~fypp_inline_end->proc~peek proc~is_preproc_start->proc~peek proc~layout->proc~builder_get proc~emit_blank emit_blank proc~layout->proc~emit_blank proc~emit_comment_line emit_comment_line proc~layout->proc~emit_comment_line proc~emit_fypp_line emit_fypp_line proc~layout->proc~emit_fypp_line proc~emit_range emit_range proc~layout->proc~emit_range proc~emit_statement emit_statement proc~layout->proc~emit_statement proc~lex_number->proc~dotop_at proc~lex_number->proc~is_digit proc~lex_number->proc~name_end proc~lex_number->proc~peek proc~digits_end digits_end proc~lex_number->proc~digits_end proc~lex_number->proc~is_name_char proc~lower_char lower_char proc~lex_number->proc~lower_char proc~view_word statement_view_t%view_word proc~module_key->proc~view_word proc~name_end->proc~peek proc~name_end->proc~is_name_char proc~parse_source->proc~tokenize proc~protected_items protected_items proc~parse_source->proc~protected_items proc~split_statements split_statements proc~parse_source->proc~split_statements proc~preproc_end->proc~line_end proc~preproc_end->proc~peek proc~last_nonblank last_nonblank proc~preproc_end->proc~last_nonblank proc~make_lowercase make_lowercase proc~set_view->proc~make_lowercase proc~set_views->proc~set_view proc~sort_only_list->proc~builder_append proc~sort_only_list->proc~builder_get proc~sort_only_list->proc~sorted_order proc~sort_only_list->proc~token_text proc~only_items only_items proc~sort_only_list->proc~only_items proc~sort_only_list->proc~view_word proc~string_end->proc~is_blank proc~string_end->proc~line_end proc~string_end->proc~peek proc~only_blanks_to_eol only_blanks_to_eol proc~string_end->proc~only_blanks_to_eol proc~token_lower->proc~make_lowercase proc~closed_construct closed_construct proc~classify_statement->proc~closed_construct proc~expand_keyword expand_keyword proc~classify_statement->proc~expand_keyword proc~read_label read_label proc~classify_statement->proc~read_label proc~set set proc~classify_statement->proc~set proc~view_closing statement_view_t%view_closing proc~classify_statement->proc~view_closing proc~view_kind_of statement_view_t%view_kind_of proc~classify_statement->proc~view_kind_of proc~comment_gaps->proc~utf8_width proc~digits_end->proc~is_digit proc~digits_end->proc~peek proc~emit_blank->proc~token_text proc~put put proc~emit_blank->proc~put proc~emit_comment_line->proc~token_text proc~emit_comment_line->proc~emit_range proc~comment_column comment_column proc~emit_comment_line->proc~comment_column proc~input_column input_column proc~emit_comment_line->proc~input_column proc~emit_comment_line->proc~put proc~trim_blanks trim_blanks proc~emit_comment_line->proc~trim_blanks proc~emit_fypp_line->proc~emit_range proc~first_non_blank first_non_blank proc~emit_fypp_line->proc~first_non_blank proc~emit_fypp_line->proc~put proc~emit_range->proc~token_text proc~emit_range->proc~put proc~emit_statement->proc~token_lower proc~emit_statement->proc~token_text proc~emit_statement->proc~emit_range proc~emit_statement->proc~comment_column proc~continuation_target continuation_target proc~emit_statement->proc~continuation_target proc~emit_body emit_body proc~emit_statement->proc~emit_body proc~emit_continuation emit_continuation proc~emit_statement->proc~emit_continuation proc~emit_statement->proc~first_non_blank proc~fitting_column fitting_column proc~emit_statement->proc~fitting_column proc~emit_statement->proc~input_column proc~emit_statement->proc~put proc~fypp_depths->proc~token_lower proc~fypp_kind fypp_kind proc~fypp_depths->proc~fypp_kind proc~is_code_char->proc~is_blank proc~is_name_char->proc~is_digit proc~is_name_char->proc~is_letter proc~last_nonblank->proc~is_blank proc~lowercase->proc~lower_char proc~names_case->proc~view_word proc~names_case->proc~view_kind_of proc~only_blanks_to_eol->proc~line_end proc~only_blanks_to_eol->proc~last_nonblank proc~directive directive proc~protected_items->proc~directive proc~ends_with_continuation ends_with_continuation proc~split_statements->proc~ends_with_continuation proc~find_semicolon find_semicolon proc~split_statements->proc~find_semicolon proc~line_end_token line_end_token proc~split_statements->proc~line_end_token proc~line_kind line_kind proc~split_statements->proc~line_kind proc~push statement_list_t%push proc~split_statements->proc~push proc~stack_restore->proc~stack_push proc~statement_case->proc~view_word proc~statement_case->proc~view_closing proc~statement_case->proc~view_kind_of proc~statement_gaps->proc~is_chevron proc~statement_gaps->proc~view_word proc~is_word is_word proc~statement_gaps->proc~is_word proc~statement_gaps->proc~view_kind_of proc~comment_column->proc~fitting_column proc~comment_column->proc~input_column proc~directive->proc~token_text proc~emit_body->proc~token_lower proc~emit_body->proc~kind_part proc~emit_body->proc~input_column proc~emit_body->proc~put proc~line_pieces line_pieces proc~emit_body->proc~line_pieces proc~to_case to_case proc~emit_body->proc~to_case proc~emit_continuation->proc~emit_body proc~emit_continuation->proc~first_non_blank proc~emit_continuation->proc~fitting_column proc~emit_continuation->proc~input_column proc~emit_continuation->proc~put proc~first_piece_width first_piece_width proc~fitting_column->proc~first_piece_width proc~line_width line_width proc~fitting_column->proc~line_width proc~input_column->proc~utf8_width proc~put->proc~builder_append proc~put->proc~utf8_width proc~first_piece_width->proc~utf8_width proc~first_piece_width->proc~line_pieces proc~line_width->proc~utf8_width proc~line_width->proc~line_pieces

Called by

proc~~format_text~~CalledByGraph proc~format_text format_text proc~process_text process_text proc~process_text->proc~format_text proc~process_file process_file proc~process_file->proc~process_text proc~process_stdin process_stdin proc~process_stdin->proc~process_text proc~run run proc~run->proc~process_file proc~run->proc~process_stdin

Source Code

subroutine format_text(src, opts, out, stat, errmsg, diags)
    !! Format `src`. On success `out` is the formatted text and `stat` is `FMT_OK`.
    !!
    !! As a safety check, the code tokens of the result are compared with those of the input. If they
    !! differ, which would be a bug in the formatter, `out` is the unchanged input and `stat` is
    !! `FMT_ERR_INTERNAL`.
    character(len=*), intent(in) :: src                    !! source text to format
    type(format_options_t), intent(in) :: opts             !! formatting options
    character(len=:), allocatable, intent(out) :: out      !! the formatted text
    integer, intent(out) :: stat                           !! `FMT_OK` or `FMT_ERR_INTERNAL`
    character(len=:), allocatable, intent(out) :: errmsg   !! error message; unallocated if `stat` is `FMT_OK`
    type(diagnostic_t), allocatable, intent(out), optional :: diags(:)   !! problems found while formatting

    ! each pass splits each long line once, and a statement gets at most MAX_CONTINUATIONS
    ! continuation lines, so this many passes split every line that can be split
    integer, parameter :: MAX_PASSES = MAX_CONTINUATIONS + 1
    type(diagnostic_t), allocatable :: d(:), unused(:)
    type(token_list_t) :: tokens
    integer, allocatable :: long(:), changed(:, :)
    character(len=:), allocatable :: prepared, split
    integer :: k, nsplit

    ! a sorted `only:` list is written on one line for splitting, or broken to fit if splitting is off
    prepared = convert_relational(sort_use(src, opts%sort_use, opts%sort_use_only, &
                                           merge(0, opts%line_length, opts%split_lines)), opts%relational)
    call format_pass(prepared, opts, out, d, tokens)
    ! no code longer than the limit, as most files: nothing to split, and no need to look for
    ! code lines that are too long
    if (opts%split_lines .and. opts%line_length > 0 .and. too_long(out, opts%line_length)) then
        ! split long lines and format again, until every line fits or nothing more can be split; only
        ! the statements that were split need laying out again, unless one of them depends on the
        ! lines around it
        do k = 1, MAX_PASSES
            split = split_long_lines(out, opts%line_length, opts%continuation_indent, nsplit, changed)
            if (nsplit == 0) exit
            if (allocated(changed) .and. opts%comment_spacing < 0) then
                out = relayout(split, changed, opts)
            else
                call format_pass(split, opts, out, unused)
            end if
            if (.not. too_long(out, opts%line_length)) exit
        end do
        if (too_long(out, opts%line_length)) then
            long = long_code_lines(out, opts%line_length)
            do k = 1, size(long)
                d = [d, diagnostic_t(long(k), 'line '//to_s(long(k))//' of the formatted file is longer than '// &
                                     to_s(opts%line_length)//' characters and cannot be split')]
            end do
        end if
    end if
    if (opts%comment_spacing >= 0) then
        ! comments are kept where they line up in the input; once the code is formatted, some of
        ! them may no longer line up, so decide again on the result (which is then stable)
        split = out
        call format_pass(split, opts, out, unused)
    end if
    d = [limit_diagnostics(src), d]
    if (present(diags)) diags = d

    stat = FMT_OK
    ! the tokens of the first pass are those of `src` if sorting and relational spelling changed nothing
    if (len(prepared) /= len(src) .or. prepared /= src) call tokenize(src, tokens)
    if (.not. same_code(src, out, opts%sort_use .or. opts%sort_use_only, tokens)) then
        stat = FMT_ERR_INTERNAL
        errmsg = 'internal error: formatting would change the code, so the file was left unchanged'
        out = src
    end if
end subroutine format_text