split_long_lines Function

public function split_long_lines(src, line_length, indent, nsplit, changed) result(out)

src with each code line longer than line_length split once. The new continuation lines are indented by indent more than the line they come from if it is the first line of a statement, and like it if it is a continuation line, which the layout pass will usually leave in place. nsplit returns the number of splits.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: src

the source text

integer, intent(in) :: line_length

maximum line width, in characters

integer, intent(in) :: indent

indentation step, in blanks

integer, intent(out) :: nsplit

number of lines split

integer, intent(out), optional, allocatable :: changed(:,:)

where the statements that were split are in out: bytes changed(1, j):changed(2, j). Not allocated if one of them cannot be laid out again on its own (see stands_alone)

Return Value character(len=:), allocatable


Calls

proc~~split_long_lines~~CallsGraph proc~split_long_lines split_long_lines proc~break_point break_point proc~split_long_lines->proc~break_point proc~builder_append string_builder_t%builder_append proc~split_long_lines->proc~builder_append proc~builder_get string_builder_t%builder_get proc~split_long_lines->proc~builder_get proc~builder_length string_builder_t%builder_length proc~split_long_lines->proc~builder_length proc~parse_source parse_source proc~split_long_lines->proc~parse_source proc~stands_alone stands_alone proc~split_long_lines->proc~stands_alone proc~token_text token_text proc~split_long_lines->proc~token_text proc~utf8_width utf8_width proc~split_long_lines->proc~utf8_width proc~break_point->proc~token_text proc~break_point->proc~utf8_width proc~priority priority proc~break_point->proc~priority proc~token_lower token_lower proc~break_point->proc~token_lower proc~protected_items protected_items proc~parse_source->proc~protected_items proc~split_statements split_statements proc~parse_source->proc~split_statements proc~tokenize tokenize proc~parse_source->proc~tokenize proc~directive directive proc~protected_items->proc~directive proc~ends_with_continuation ends_with_continuation proc~split_statements->proc~ends_with_continuation proc~find_semicolon find_semicolon proc~split_statements->proc~find_semicolon proc~line_end_token line_end_token proc~split_statements->proc~line_end_token proc~line_kind line_kind proc~split_statements->proc~line_kind proc~push statement_list_t%push proc~split_statements->proc~push proc~make_lowercase make_lowercase proc~token_lower->proc~make_lowercase proc~add add proc~tokenize->proc~add proc~comment_kind comment_kind proc~tokenize->proc~comment_kind proc~dotop_at dotop_at proc~tokenize->proc~dotop_at proc~dotop_end dotop_end proc~tokenize->proc~dotop_end proc~fypp_inline_end fypp_inline_end proc~tokenize->proc~fypp_inline_end proc~is_blank is_blank proc~tokenize->proc~is_blank proc~is_digit is_digit proc~tokenize->proc~is_digit proc~is_preproc_start is_preproc_start proc~tokenize->proc~is_preproc_start proc~is_quote is_quote proc~tokenize->proc~is_quote proc~lex_number lex_number proc~tokenize->proc~lex_number proc~line_end line_end proc~tokenize->proc~line_end proc~name_end name_end proc~tokenize->proc~name_end proc~operator_end operator_end proc~tokenize->proc~operator_end proc~peek peek proc~tokenize->proc~peek proc~preproc_end preproc_end proc~tokenize->proc~preproc_end proc~string_end string_end proc~tokenize->proc~string_end proc~add->proc~is_blank proc~add->proc~peek proc~is_code_char is_code_char proc~add->proc~is_code_char proc~push~2 token_list_t%push proc~add->proc~push~2 proc~comment_kind->proc~peek proc~is_letter is_letter proc~comment_kind->proc~is_letter proc~lowercase lowercase proc~comment_kind->proc~lowercase proc~directive->proc~token_text proc~dotop_at->proc~peek proc~dotop_at->proc~is_letter proc~dotop_end->proc~name_end proc~dotop_end->proc~peek proc~is_name_char is_name_char proc~dotop_end->proc~is_name_char proc~dotop_end->proc~lowercase proc~fypp_inline_end->proc~peek proc~is_preproc_start->proc~peek proc~lex_number->proc~dotop_at proc~lex_number->proc~is_digit proc~lex_number->proc~name_end proc~lex_number->proc~peek proc~digits_end digits_end proc~lex_number->proc~digits_end proc~lex_number->proc~is_name_char proc~lower_char lower_char proc~lex_number->proc~lower_char proc~name_end->proc~peek proc~name_end->proc~is_name_char proc~preproc_end->proc~line_end proc~preproc_end->proc~peek proc~last_nonblank last_nonblank proc~preproc_end->proc~last_nonblank proc~string_end->proc~is_blank proc~string_end->proc~line_end proc~string_end->proc~peek proc~only_blanks_to_eol only_blanks_to_eol proc~string_end->proc~only_blanks_to_eol proc~digits_end->proc~is_digit proc~digits_end->proc~peek proc~is_code_char->proc~is_blank proc~is_name_char->proc~is_digit proc~is_name_char->proc~is_letter proc~last_nonblank->proc~is_blank proc~lowercase->proc~lower_char proc~only_blanks_to_eol->proc~line_end proc~only_blanks_to_eol->proc~last_nonblank

Called by

proc~~split_long_lines~~CalledByGraph proc~split_long_lines split_long_lines 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

function split_long_lines(src, line_length, indent, nsplit, changed) result(out)
    !! `src` with each code line longer than `line_length` split once. The new continuation lines are indented
    !! by `indent` more than the line they come from if it is the first line of a statement, and like
    !! it if it is a continuation line, which the layout pass will usually leave in place. `nsplit`
    !! returns the number of splits.
    character(len=*), intent(in) :: src           !! the source text
    integer, intent(in) :: line_length            !! maximum line width, in characters
    integer, intent(in) :: indent                 !! indentation step, in blanks
    integer, intent(out) :: nsplit                !! number of lines split
    integer, allocatable, intent(out), optional :: changed(:, :)   !! where the statements that were split
                                                  !! are in `out`: bytes `changed(1, j):changed(2, j)`.
                                                  !! Not allocated if one of them cannot be laid out
                                                  !! again on its own (see [[stands_alone]])
    character(len=:), allocatable :: out

    type(source_t) :: source
    type(string_builder_t) :: buf
    character(len=:), allocatable :: nl
    integer :: k, j, lend, a, p, q, base, start_col, offset, ncont, before, start, nranges
    integer, allocatable :: ranges(:, :)
    logical :: alone

    nsplit = 0
    call parse_source(src, source)
    nl = achar(10)
    do j = 1, source%tokens%n
        if (source%tokens%tok(j)%kind == TK_NEWLINE) then
            nl = token_text(src, source%tokens%tok(j))
            exit
        end if
    end do

    allocate (ranges(2, 16))
    nranges = 0
    alone = .true.
    do k = 1, source%stmts%n
        before = nsplit
        start = buf%length() + 1
        if (source%keep(k) .or. source%stmts%s(k)%kind /= SK_CODE) then
            call copy(source%stmts%s(k)%first, source%stmts%s(k)%last)
            cycle
        end if
        associate (s => source%stmts%s(k))
            ncont = count(source%tokens%tok(s%first:s%last)%kind == TK_CONT .and. &
                          source%tokens%tok(s%first:s%last)%sub == CT_TRAILING)
        end associate
        j = source%stmts%s(k)%first
        do while (j <= source%stmts%s(k)%last)
            lend = j
            do while (lend < source%stmts%s(k)%last)
                if (source%tokens%tok(lend)%kind == TK_NEWLINE) exit
                lend = lend + 1
            end do
            ! split the line once; the rest is aligned by the next layout pass and split again if needed
            a = j
            start_col = column(j)
            base = indentation(j)
            offset = merge(indent, 0, j == source%stmts%s(k)%first)
            p = 0
            if (ncont < MAX_CONTINUATIONS) p = break_point(source%tokens, src, a, lend, start_col, line_length)
            if (p > 0) then
                ncont = ncont + 1
                call copy(a, p)
                call buf%append(' &'//nl//repeat(' ', base + offset))
                nsplit = nsplit + 1
                q = p + 1
                do while (source%tokens%tok(q)%kind == TK_SPACE)
                    q = q + 1
                end do
                a = q
            end if
            call copy(a, lend)
            j = lend + 1
        end do
        if (nsplit > before) then
            if (nranges == size(ranges, 2)) ranges = reshape([ranges, ranges], [2, 2*nranges])
            nranges = nranges + 1
            ranges(:, nranges) = [start, buf%length()]
            alone = alone .and. stands_alone(source, k)
        end if
    end do
    out = buf%get()
    if (present(changed) .and. alone) changed = ranges(:, 1:nranges)

contains

    subroutine copy(first, last)
        !! Append the text of tokens `first:last` to the output.
        integer, intent(in) :: first, last   !! token range
        integer :: t
        do t = first, last
            call buf%append(token_text(src, source%tokens%tok(t)))
        end do
    end subroutine copy

    integer function column(t)
        !! Column of token `t` in `src`.
        integer, intent(in) :: t   !! index into the token list
        integer :: i
        i = scan(src(1:source%tokens%tok(t)%first - 1), achar(10)//achar(13), back=.true.)
        column = utf8_width(src(i + 1:source%tokens%tok(t)%first - 1))
    end function column

    integer function indentation(t)
        !! Indentation of the line containing token `t`.
        integer, intent(in) :: t   !! index into the token list
        integer :: i, m
        i = scan(src(1:source%tokens%tok(t)%first - 1), achar(10)//achar(13), back=.true.) + 1
        m = verify(src(i:), ' '//achar(9))
        if (m == 0) then
            indentation = 0
        else
            indentation = m - 1
        end if
    end function indentation

end function split_long_lines