line_width Function

private function line_width(w, src, tokens, first, lend) result(width)

Width of tokens first:lend as they will be written on one line, without the line break and without a trailing comment (compilers only limit the length of the code). A comment on a line of its own counts.

Arguments

Type IntentOptional Attributes Name
type(writer_t), intent(in) :: 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) :: first

first token of the part of the line to measure

integer, intent(in) :: lend

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

Return Value integer


Calls

proc~~line_width~~CallsGraph proc~line_width line_width proc~line_pieces line_pieces proc~line_width->proc~line_pieces proc~utf8_width utf8_width proc~line_width->proc~utf8_width

Called by

proc~~line_width~~CalledByGraph proc~line_width line_width proc~fitting_column fitting_column proc~fitting_column->proc~line_width 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 line_width(w, src, tokens, first, lend) result(width)
    !! Width of tokens `first:lend` as they will be written on one line, without the line break and
    !! without a trailing comment (compilers only limit the length of the code). A comment on a line
    !! of its own counts.
    type(writer_t), intent(in) :: 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) :: first               !! first token of the part of the line to measure
    integer, intent(in) :: lend                !! last token of the line, its line break if it has one
    type(piece_t), allocatable :: pieces(:)
    integer :: k, j, before_blank

    call line_pieces(w, src, tokens, first, lend, pieces)
    width = 0
    before_blank = 0
    do k = 1, size(pieces)
        if (pieces(k)%tok > 0 .and. k > 1) then
            if (tokens%tok(pieces(k)%tok)%kind == TK_COMMENT) then
                width = before_blank            ! a trailing comment, and the blanks before it
                return
            end if
        end if
        if (pieces(k)%blanks > 0) then
            width = width + pieces(k)%blanks
            cycle
        end if
        associate (text => src(pieces(k)%first:pieces(k)%last))
            if (pieces(k)%tok > 0) before_blank = width + utf8_width(text)
            j = scan(text, achar(10)//achar(13))
            if (j > 0) then                       ! a line break, or a token continued on the next line
                width = width + utf8_width(text(1:j - 1))
                return
            end if
            width = width + utf8_width(text)
        end associate
    end do
end function line_width