comment_gaps Subroutine

private subroutine comment_gaps(source, spacing, gap)

Ask for spacing blanks before each trailing comment (one after code or a continuation &), unless a trailing comment on the line before or after starts in the same column: comments lined up in a column are left as they are.

Arguments

Type IntentOptional Attributes Name
type(source_t), intent(in) :: source

the source

integer, intent(in) :: spacing

blanks wanted before a trailing comment

integer, intent(inout) :: gap(:)

gap wanted before each token, set for the comments


Calls

proc~~comment_gaps~~CallsGraph proc~comment_gaps comment_gaps proc~utf8_width utf8_width proc~comment_gaps->proc~utf8_width

Called by

proc~~comment_gaps~~CalledByGraph proc~comment_gaps comment_gaps proc~compute_gaps compute_gaps proc~compute_gaps->proc~comment_gaps proc~format_pass format_pass proc~format_pass->proc~compute_gaps 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

subroutine comment_gaps(source, spacing, gap)
    !! Ask for `spacing` blanks before each trailing comment (one after code or a continuation `&`),
    !! unless a trailing comment on the line before or after starts in the same column: comments
    !! lined up in a column are left as they are.
    type(source_t), intent(in) :: source   !! the source
    integer, intent(in) :: spacing         !! blanks wanted before a trailing comment
    integer, intent(inout) :: gap(:)       !! gap wanted before each token, set for the comments
    integer, allocatable :: tok(:), line(:), col(:)
    integer :: t, k, n, p

    allocate (tok(source%tokens%n), line(source%tokens%n), col(source%tokens%n))
    n = 0
    do t = 2, source%tokens%n
        if (source%tokens%tok(t)%kind /= TK_COMMENT) cycle
        p = t - 1
        if (source%tokens%tok(p)%kind == TK_SPACE .and. p > 1) p = p - 1
        if (.not. (is_code_kind(source%tokens%tok(p)%kind) .or. source%tokens%tok(p)%kind == TK_CONT)) cycle
        if (source%tokens%tok(p)%kind == TK_SPACE) cycle
        n = n + 1
        tok(n) = t
        line(n) = source%tokens%tok(t)%line
        col(n) = column(source%tokens%tok(t)%first)
    end do
    do k = 1, n
        if (k > 1) then
            if (line(k - 1) == line(k) - 1 .and. col(k - 1) == col(k)) cycle
        end if
        if (k < n) then
            if (line(k + 1) == line(k) + 1 .and. col(k + 1) == col(k)) cycle
        end if
        gap(tok(k)) = spacing
    end do

contains

    integer function column(i)
        !! Input column of byte `i`: the characters before it on its line.
        integer, intent(in) :: i   !! index into the source text
        integer :: j
        j = scan(source%text(1:i - 1), achar(10)//achar(13), back=.true.)
        column = utf8_width(source%text(j + 1:i - 1))
    end function column

end subroutine comment_gaps