ffmt_use_sort Module

Sorting use statements, and the names in their only: lists, alphabetically ignoring case.

A group of use statements is a run of them with nothing between them: a blank line, comment, preprocessor line or any other statement ends the group, so statements in different #if branches never mix. A statement moves with its continuation lines and trailing comment. Statements that share a line with another (;) or that are protected by formatter directives stay where they are and end the group. Statements of one group that use the same module keep their order.

The names of an only: list are sorted by their local name (c => x sorts as c). A list that is not sorted yet is rewritten: on one line, for line splitting to break again, or broken at commas to fit a given width, with its continuation lines starting below the first name. A statement with a comment or preprocessor line before its last line is left alone.


Uses

  • module~~ffmt_use_sort~~UsesGraph module~ffmt_use_sort ffmt_use_sort module~ffmt_source ffmt_source module~ffmt_use_sort->module~ffmt_source module~ffmt_statements ffmt_statements module~ffmt_use_sort->module~ffmt_statements module~ffmt_strings ffmt_strings module~ffmt_use_sort->module~ffmt_strings module~ffmt_tokens ffmt_tokens module~ffmt_use_sort->module~ffmt_tokens module~ffmt_source->module~ffmt_statements module~ffmt_source->module~ffmt_tokens module~ffmt_directives ffmt_directives module~ffmt_source->module~ffmt_directives module~ffmt_lexer ffmt_lexer module~ffmt_source->module~ffmt_lexer module~ffmt_statements->module~ffmt_strings module~ffmt_statements->module~ffmt_tokens module~ffmt_tokens->module~ffmt_strings module~ffmt_directives->module~ffmt_statements module~ffmt_directives->module~ffmt_tokens module~ffmt_lexer->module~ffmt_strings module~ffmt_lexer->module~ffmt_tokens

Used by

  • module~~ffmt_use_sort~~UsedByGraph module~ffmt_use_sort ffmt_use_sort module~ffmt_format ffmt_format module~ffmt_format->module~ffmt_use_sort module~ffmt_cli ffmt_cli module~ffmt_cli->module~ffmt_format

Functions

public pure function is_use_statement(v)

True if statement v is a use statement.

Arguments

Type IntentOptional Attributes Name
type(statement_view_t), intent(in) :: v

the statement

Return Value logical

private function module_key(v) result(key)

Sort key of a use statement: the module name in lowercase.

Arguments

Type IntentOptional Attributes Name
type(statement_view_t), intent(in) :: v

the use statement

Return Value character(len=:), allocatable

public function only_items(v) result(starts)

Positions in v%code where the items of a use statement's only: list start. Each item ends before the comma in front of the next one, the last at the end of the statement. Empty if there is no only: list, the list is empty, or it has an empty item.

Arguments

Type IntentOptional Attributes Name
type(statement_view_t), intent(in) :: v

the use statement

Return Value integer, allocatable, (:)

public 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).

Arguments

Type IntentOptional Attributes Name
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

Return Value character(len=:), allocatable

public function sorted_order(keys) result(order)

The order in which keys are sorted (a stable sort, comparing in ASCII order).

Arguments

Type IntentOptional Attributes Name
type(string_t), intent(in) :: keys(:)

the sort keys

Return Value integer, allocatable, (:)

private pure function whole_lines(tokens, stmt)

True if stmt takes up whole lines on its own, ending with a line break, so that it can be moved.

Arguments

Type IntentOptional Attributes Name
type(token_list_t), intent(in) :: tokens

tokens of the source

type(statement_t), intent(in) :: stmt

the statement, a range of tokens

Return Value logical


Subroutines

private 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.

Arguments

Type IntentOptional Attributes Name
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=:), intent(inout), allocatable :: text

text of stmt, from its first to its last token