Replace text, the text of stmt, by the statement with its only: list sorted, unless the
list is sorted already or the statement cannot be rewritten safely. With width > 0, the
list is broken after commas so that its lines fit in width columns where possible.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | src |
source text |
||
| type(token_list_t), | intent(in) | :: | tokens |
tokens of |
||
| type(statement_t), | intent(in) | :: | stmt |
the |
||
| type(statement_view_t), | intent(in) | :: | v |
the view of |
||
| integer, | intent(in) | :: | width |
line length to fit the list into, or 0 |
||
| character(len=:), | intent(inout), | allocatable | :: | text |
text of |
subroutine sort_only_list(src, tokens, stmt, v, width, text) !! Replace `text`, the text of `stmt`, by the statement with its `only:` list sorted, unless the !! list is sorted already or the statement cannot be rewritten safely. With `width > 0`, the !! list is broken after commas so that its lines fit in `width` columns where possible. character(len=*), intent(in) :: src !! source text type(token_list_t), intent(in) :: tokens !! tokens of `src` type(statement_t), intent(in) :: stmt !! the `use` statement type(statement_view_t), intent(in) :: v !! the view of `stmt` integer, intent(in) :: width !! line length to fit the list into, or 0 character(len=:), allocatable, intent(inout) :: text !! text of `stmt`, from its first to its last token integer, allocatable :: starts(:), ends(:), order(:) type(string_t), allocatable :: keys(:) type(string_builder_t) :: buf character(len=:), allocatable :: sep, prefix, item, nl logical :: commented integer :: j, n, ns, column, used starts = only_items(v) ns = size(starts) if (ns < 2) return n = v%n ! comments are only allowed on the last line; no preprocessor lines or tokens split across lines commented = .false. do j = stmt%first, stmt%last select case (tokens%tok(j)%kind) case (TK_COMMENT) commented = .true. case (TK_NEWLINE) if (commented .and. j /= stmt%last) return case (TK_PREPROC) return end select end do if (any(tokens%tok(v%code(starts(1):n))%glued)) return ends = [starts(2:) - 2, n] allocate (keys(ns)) do j = 1, ns keys(j)%s = local_name(starts(j), ends(j)) end do order = sorted_order(keys) if (all(order == [(j, j=1, ns)])) return prefix = src(tokens%tok(stmt%first)%first:tokens%tok(v%code(starts(1) - 1))%last) if (separated(starts(1))) prefix = prefix//' ' call buf%append(prefix) sep = ',' if (separated(starts(2))) sep = ', ' nl = achar(10) if (tokens%tok(stmt%last)%kind == TK_NEWLINE) nl = token_text(src, tokens%tok(stmt%last)) ! the column of the first name, where the continuation lines start too column = len(prefix) - max(index(prefix, achar(10), back=.true.), index(prefix, achar(13), back=.true.)) used = column do j = 1, ns item = joined(starts(order(j)), ends(order(j))) if (j > 1) then ! room for the item and a `, &` after it if (width > 0 .and. used + len(sep) + len(item) + 3 > width) then call buf%append(', &'//nl//repeat(' ', column)) used = column else call buf%append(sep) used = used + len(sep) end if end if call buf%append(item) used = used + len(item) end do call buf%append(src(tokens%tok(v%code(n))%last + 1:tokens%tok(stmt%last)%last)) text = buf%get() contains logical function separated(p) !! True if anything (blanks, a line break) comes between code tokens `p - 1` and `p`. integer, intent(in) :: p !! index into `v%code(:)` separated = v%code(p) /= v%code(p - 1) + 1 end function separated function joined(first, last) result(s) !! The text of code tokens `first:last`, with a blank wherever they were separated. integer, intent(in) :: first, last !! range of indices into `v%code(:)` character(len=:), allocatable :: s integer :: p s = token_text(src, tokens%tok(v%code(first))) do p = first + 1, last if (separated(p)) s = s//' ' s = s//token_text(src, tokens%tok(v%code(p))) end do end function joined function local_name(first, last) result(s) !! Sort key of an item: its tokens before `=>`, in lowercase. integer, intent(in) :: first, last !! range of indices into `v%code(:)` of the item character(len=:), allocatable :: s integer :: p s = '' do p = first, last if (v%kinds(p) == TK_OP .and. v%is(p, '=>')) exit s = s//v%word(p) end do end function local_name end subroutine sort_only_list