Sort includes when formatting - #7519
Open
ewels wants to merge 1 commit into
Open
Conversation
When the formatter's sort-declarations option is enabled, sort include declarations alphabetically by source path within blank-line-separated groups. Groups are kept in place and their blank-line separators are preserved; only the includes within a group are reordered. Leading and trailing comments move with their include. A comment above the first include of a group is treated as a group header and kept at the top of the group when sorting changes which include comes first. Assisted-by: Claude Opus 4.8 (Claude Code) Signed-off-by: Phil Ewels <phil.ewels@seqera.io>
✅ Deploy Preview for nextflow-docs canceled.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds include sorting to the
nf-langformatter, resolving nextflow-io/language-server#54. When the sort-declarations option is enabled,include { ... } from '...'statements are sorted alphabetically by their source path. This is one of the formatter follow-ups deferred from #7346.Sorting happens within blank-line-separated groups: groups stay in place and keep their blank-line separators, and only the includes inside a group are reordered. This preserves the common convention of grouping includes (e.g. local modules vs nf-core modules) rather than collapsing everything into one alphabetised block.
The main subtlety is that, in the current comment architecture, blank lines are derived from each node's original source line, so naively reordering nodes misplaces the surrounding blank lines. The sorted path therefore emits group separators and leading comments explicitly (via small
Formatter/CommentAttacherhelpers) instead of relying on source positions. Comments travel with their include; a comment above the first include of a group is treated as a group header and pinned to the top of the group. That header heuristic is the one debatable behaviour here — it favours the common "section header above a group" case at the cost of a first-include-specific comment, and is easy to drop if reviewers prefer comments always stick to their include.This is the first of a short stack of formatter follow-up PRs (the others cover blank-line normalisation, line wrapping, and
fmt:directives).Assisted-by: Claude Opus 4.8 (Claude Code)
Example
With
-sort-declarationsenabled, each blank-line-separated block of includes is sorted alphabetically by source path, independently of the others — so the two blocks below are each ordered internally but never merged.Before:
include { MULTIQC } from './modules/local/multiqc.nf' include { FASTQC } from './modules/local/fastqc.nf' include { TRIMGALORE } from './modules/local/trimgalore.nf' // sorting happens within each blank-line-separated block, never across blocks include { SAMTOOLS_SORT } from './modules/nf-core/samtools/sort' include { BWA_MEM } from './modules/nf-core/bwa/mem' include { PICARD_MARKDUPLICATES } from './modules/nf-core/picard/markduplicates'After:
include { FASTQC } from './modules/local/fastqc.nf' include { MULTIQC } from './modules/local/multiqc.nf' include { TRIMGALORE } from './modules/local/trimgalore.nf' // sorting happens within each blank-line-separated block, never across blocks include { BWA_MEM } from './modules/nf-core/bwa/mem' include { PICARD_MARKDUPLICATES } from './modules/nf-core/picard/markduplicates' include { SAMTOOLS_SORT } from './modules/nf-core/samtools/sort'