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.
| Type | Intent | Optional | 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 |
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