break_point Function

private function break_point(tokens, src, a, lend, start_col, line_length) result(best)

The token after which to split tokens a:lend of a line that starts at token a in column start_col, or 0 if the code fits or cannot be split.

Arguments

Type IntentOptional Attributes Name
type(token_list_t), intent(in) :: tokens

the tokens of src

character(len=*), intent(in) :: src

the source text

integer, intent(in) :: a

first token of the line

integer, intent(in) :: lend

last token of the line, its line break if it has one

integer, intent(in) :: start_col

column of token a

integer, intent(in) :: line_length

maximum line width, in characters

Return Value integer


Calls

proc~~break_point~~CallsGraph proc~break_point break_point proc~priority priority proc~break_point->proc~priority proc~token_lower token_lower proc~break_point->proc~token_lower proc~token_text token_text proc~break_point->proc~token_text proc~utf8_width utf8_width proc~break_point->proc~utf8_width proc~make_lowercase make_lowercase proc~token_lower->proc~make_lowercase

Called by

proc~~break_point~~CalledByGraph proc~break_point break_point proc~split_long_lines split_long_lines proc~split_long_lines->proc~break_point proc~format_text format_text proc~format_text->proc~split_long_lines 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 break_point(tokens, src, a, lend, start_col, line_length) result(best)
    !! The token after which to split tokens `a:lend` of a line that starts at token `a` in column
    !! `start_col`, or 0 if the code fits or cannot be split.
    type(token_list_t), intent(in) :: tokens      !! the tokens of `src`
    character(len=*), intent(in) :: src           !! the source text
    integer, intent(in) :: a                      !! first token of the line
    integer, intent(in) :: lend                   !! last token of the line, its line break if it has one
    integer, intent(in) :: start_col              !! column of token `a`
    integer, intent(in) :: line_length            !! maximum line width, in characters
    integer :: t, cend, p, q, prio, best_prio, end_col

    best = 0
    cend = 0
    do t = a, lend
        if (scan(token_text(src, tokens%tok(t)), achar(10)//achar(13)) > 0 .and. tokens%tok(t)%kind /= TK_NEWLINE) &
            return                                      ! a token continued on the next line
        if (tokens%tok(t)%kind == TK_COMMENT) exit
        if (is_code_kind(tokens%tok(t)%kind) .or. tokens%tok(t)%kind == TK_CONT) cend = t
    end do
    if (cend == 0) return
    if (end_column(cend) <= line_length) return

    best_prio = huge(1)
    do p = a, cend - 1
        if (.not. is_code_kind(tokens%tok(p)%kind)) cycle
        if (p == a .and. tokens%tok(p)%kind == TK_INT) cycle    ! a statement label
        q = p + 1
        do while (q <= cend)
            if (tokens%tok(q)%kind /= TK_SPACE) exit
            q = q + 1
        end do
        if (q > cend) cycle
        if (.not. is_code_kind(tokens%tok(q)%kind) .or. tokens%tok(q)%glued) cycle
        end_col = end_column(p)
        if (end_col + 2 > line_length) exit                     ! ` &` would not fit
        prio = priority(token_lower(src, tokens%tok(p)), tokens%tok(p)%kind)
        if (prio <= best_prio) then
            best_prio = prio
            best = p
        end if
    end do

contains

    integer function end_column(t)
        !! Column just after token `t`, for a line starting at token `a` in column `start_col`.
        integer, intent(in) :: t   !! index into the token list
        end_column = start_col + utf8_width(src(tokens%tok(a)%first:tokens%tok(t)%last))
    end function end_column

end function break_point