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