The first of the candidates columns at which the output line fits within the line length, or
the smallest if none fits. The last candidate is the line's original column: if only that one
fits, the line is recorded in long_lines. When lines are split (w%keep_original false), the
original column is not a candidate for code, since splitting will make the line fit; it is
still used if always_original is true (for comments, which are not split). Also when lines are
split, a candidate other than the last is accepted if the line can be split to fit there: if
its first piece up to a possible break fits.
The input line starts with token f. The output line is prefix characters (e.g. & for a
leading continuation marker) followed by tokens from:lend (default f:lend).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(writer_t), | intent(inout) | :: | w |
the output and the alignment state |
||
| character(len=*), | intent(in) | :: | src |
the source text |
||
| type(token_list_t), | intent(in) | :: | tokens |
its tokens |
||
| integer, | intent(in) | :: | f |
first non-blank token of the input line |
||
| integer, | intent(in) | :: | lend |
last token of the line, its line break if it has one |
||
| integer, | intent(in) | :: | candidates(:) |
columns to try, best first; negative ones are skipped |
||
| integer, | intent(in), | optional | :: | from |
first token written after the prefix; requires |
|
| integer, | intent(in), | optional | :: | prefix |
width of the text written before token |
|
| logical, | intent(in), | optional | :: | always_original |
try the original column even when lines are split |
integer function fitting_column(w, src, tokens, f, lend, candidates, from, prefix, always_original) result(col) !! The first of the `candidates` columns at which the output line fits within the line length, or !! the smallest if none fits. The last candidate is the line's original column: if only that one !! fits, the line is recorded in `long_lines`. When lines are split (`w%keep_original` false), the !! original column is not a candidate for code, since splitting will make the line fit; it is !! still used if `always_original` is true (for comments, which are not split). Also when lines are !! split, a candidate other than the last is accepted if the line can be split to fit there: if !! its first piece up to a possible break fits. !! !! The input line starts with token `f`. The output line is `prefix` characters (e.g. `& ` for a !! leading continuation marker) followed by tokens `from:lend` (default `f:lend`). type(writer_t), intent(inout) :: w !! the output and the alignment state character(len=*), intent(in) :: src !! the source text type(token_list_t), intent(in) :: tokens !! its tokens integer, intent(in) :: f !! first non-blank token of the input line integer, intent(in) :: lend !! last token of the line, its line break if it has one integer, intent(in) :: candidates(:) !! columns to try, best first; negative ones are skipped integer, intent(in), optional :: from !! first token written after the prefix; requires `prefix` integer, intent(in), optional :: prefix !! width of the text written before token `from` logical, intent(in), optional :: always_original !! try the original column even when lines are split integer :: k, width, ncand, first_piece logical :: original col = max(0, candidates(1)) if (w%line_length <= 0) return original = w%keep_original if (present(always_original)) original = original .or. always_original ncand = size(candidates) if (.not. original .and. ncand > 1) ncand = ncand - 1 if (present(from)) then width = prefix + line_width(w, src, tokens, from, lend) else width = line_width(w, src, tokens, f, lend) end if first_piece = -1 do k = 1, ncand if (candidates(k) < 0) cycle if (.not. original .and. k < ncand .and. candidates(k) + width > w%line_length) then if (first_piece < 0) then if (present(from)) then first_piece = prefix + first_piece_width(w, src, tokens, from, lend) else first_piece = first_piece_width(w, src, tokens, f, lend) end if end if if (candidates(k) + first_piece + 2 <= w%line_length) then ! ` &` after the first piece col = candidates(k) return end if end if if (candidates(k) + width <= w%line_length) then col = candidates(k) if (original .and. k == size(candidates) .and. k > 1 .and. col /= candidates(1)) & w%long_lines = [w%long_lines, tokens%tok(f)%line] return end if end do col = max(0, minval(candidates(1:ncand))) ! too long: it is split later, or was too long already end function fitting_column