src with the use statements of each group sorted by module name (if statements) and
the names of only: lists sorted (if only_lists).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | src |
source text |
||
| logical, | intent(in) | :: | statements |
sort the |
||
| logical, | intent(in) | :: | only_lists |
sort the names of |
||
| integer, | intent(in) | :: | width |
line length to fit a rewritten |
function sort_use(src, statements, only_lists, width) result(out) !! `src` with the `use` statements of each group sorted by module name (if `statements`) and !! the names of `only:` lists sorted (if `only_lists`). character(len=*), intent(in) :: src !! source text logical, intent(in) :: statements !! sort the `use` statements of each group logical, intent(in) :: only_lists !! sort the names of `only:` lists integer, intent(in) :: width !! line length to fit a rewritten `only:` list into, or 0 to write it on one line character(len=:), allocatable :: out type(source_t) :: source type(statement_view_t) :: v logical, allocatable :: movable(:) type(string_t), allocatable :: text(:), keys(:) integer, allocatable :: order(:) type(string_builder_t) :: buf integer :: k, m, j if (.not. (statements .or. only_lists)) then out = src return end if call parse_source(src, source) associate (tokens => source%tokens, stmts => source%stmts) allocate (text(stmts%n), keys(stmts%n), movable(stmts%n)) do k = 1, stmts%n text(k)%s = src(tokens%tok(stmts%s(k)%first)%first:tokens%tok(stmts%s(k)%last)%last) keys(k)%s = '' movable(k) = .false. if (stmts%s(k)%kind /= SK_CODE .or. source%keep(k)) cycle call set_view(src, tokens, stmts%s(k), v) if (.not. is_use_statement(v)) cycle if (only_lists) call sort_only_list(src, tokens, stmts%s(k), v, width, text(k)%s) keys(k)%s = module_key(v) movable(k) = statements .and. whole_lines(tokens, stmts%s(k)) end do end associate k = 1 do while (k <= size(movable)) if (.not. movable(k)) then call buf%append(text(k)%s) k = k + 1 cycle end if m = k do while (m < size(movable)) if (.not. movable(m + 1)) exit m = m + 1 end do order = sorted_order(keys(k:m)) do j = 1, size(order) call buf%append(text(k + order(j) - 1)%s) end do k = m + 1 end do out = buf%get() end function sort_use