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.
| Type | Intent | Optional | 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 |
|
||
| character(len=:), | intent(out), | allocatable | :: | errmsg |
error message; unallocated if |
|
| type(diagnostic_t), | intent(out), | optional, | allocatable | :: | diags(:) |
problems found while formatting |
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