Skip to main content

Module organise

Module organise 

Source
Expand description

#2100 slice 5: the source.organizeImports code action — organise a unit header’s uses and consumes clauses.

The action sorts the uses clauses by target, then the consumes clauses by target (the order bynk-fmt prints them in: every uses before every consumes), drops exact duplicates, sorts and deduplicates the names in a braced consumes U { … } selection, and drops what the unit never uses:

  • a uses U or whole-unit consumes U (or consumes U as A) whose target no reference in the unit resolves into;
  • a name in a braced selection that no reference in the unit names, and the whole braced clause once every name in it is gone.

“Unused” is read from the committed binding index (ADR 0053), over every file of the unit (a uses is unit-wide: the driver merges the clauses of all of a unit’s files, and a suite resolves through its target’s), not just the one being organised. A reference inside a uses/consumes clause is not a use — the braced form records one for every name it selects. A target that is not a project unit (the first-party bynk surface, a bynk.list-style commons) has no index symbols to test against, so a whole-unit clause naming one is never dropped; a braced selection from one is pruned against the index’s first-party capability references, matched on their source text.

The edit is one replacement of the header’s clause run. Each clause moves with its own comments (the comment lines directly above it and a trailing comment on its line). A clause carrying a comment is never dropped: an unused or duplicate one is kept while the rest are still organised. The action declines (offers nothing) when the file doesn’t parse cleanly, when the clauses don’t form one run (something other than whitespace between two of them), when a clause shares its line with other code, or when the file is bynk-fmt-canonical but the organised text would not be. It is also never an empty edit: an already-organised header offers nothing.

The pure computation lives here; the code_action handler adds DECISION B’s safety gate (re-analyse the edited project, offer the edit only if it introduces no diagnostic), falling back from Mode::Full to Mode::SortOnly when a drop would break something.

Structs§

Project
The project facts the unused test reads: the committed binding index, the round’s snapshots (project-relative path → text), and each unit’s source files.

Enums§

Mode
What the organise pass may change.

Functions§

action
The versioned source.organizeImports action for one organise edit.
apply
The text with one organise edit applied.
organise
The organised header for text (the snapshot of rel), as one edit: (span, replacement). None when there is nothing to change or the action declines (see the module docs).