fitting_column Function

private 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).

Arguments

Type IntentOptional 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 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

Return Value integer


Calls

proc~~fitting_column~~CallsGraph proc~fitting_column fitting_column 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~line_pieces line_pieces proc~first_piece_width->proc~line_pieces proc~utf8_width utf8_width proc~first_piece_width->proc~utf8_width proc~line_width->proc~line_pieces proc~line_width->proc~utf8_width

Called by

proc~~fitting_column~~CalledByGraph proc~fitting_column fitting_column proc~comment_column comment_column proc~comment_column->proc~fitting_column proc~emit_continuation emit_continuation proc~emit_continuation->proc~fitting_column proc~emit_statement emit_statement proc~emit_statement->proc~fitting_column proc~emit_statement->proc~comment_column proc~emit_statement->proc~emit_continuation proc~emit_comment_line emit_comment_line proc~emit_comment_line->proc~comment_column proc~layout layout proc~layout->proc~emit_statement proc~layout->proc~emit_comment_line proc~format_pass format_pass proc~format_pass->proc~layout proc~format_text format_text proc~format_text->proc~format_pass proc~relayout relayout proc~format_text->proc~relayout proc~relayout->proc~format_pass 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

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