Skip to content

Commit 67f9e25

Browse files
committed
add option to sort individual blocks of imports separately
Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
1 parent fa57397 commit 67f9e25

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@
116116
"default": false,
117117
"description": "Sort imports using a 'natural order' algorithm"
118118
},
119+
"namespaceResolver.sortBlockWise": {
120+
"type": "boolean",
121+
"default": false,
122+
"description": "Sort imports in individual blocks separated by blank lines"
123+
},
119124
"namespaceResolver.leadingSeparator": {
120125
"type": "boolean",
121126
"default": true,

src/Resolver.js

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -448,13 +448,7 @@ class Resolver {
448448
return parsedNamespaces;
449449
}
450450

451-
sortImports() {
452-
let [useStatements,] = this.getDeclarations();
453-
454-
if (useStatements.length <= 1) {
455-
throw new Error('$(issue-opened) Nothing to sort.');
456-
}
457-
451+
getSortFunction() {
458452
let sortFunction = (a, b) => {
459453
if (this.config('sortAlphabetically')) {
460454
if (a.text.toLowerCase() < b.text.toLowerCase()) return -1;
@@ -481,7 +475,27 @@ class Resolver {
481475
};
482476
}
483477

484-
let sorted = useStatements.slice().sort(sortFunction);
478+
return sortFunction;
479+
}
480+
481+
sortImports() {
482+
const sortFunction = this.getSortFunction();
483+
484+
const [useStatements, sorted] = (() => {
485+
if (this.config('sortBlockWise')) {
486+
const useStatements = this.getUseStatementsArray(true);
487+
if (useStatements.length === 0 || useStatements[0].length <= 1) {
488+
throw new Error('$(issue-opened) Nothing to sort.');
489+
}
490+
return [useStatements.flat(), useStatements.map(block => block.slice().sort(sortFunction)).flat()];
491+
}
492+
493+
const useStatements = this.getDeclarations();
494+
if (useStatements.length <= 1) {
495+
throw new Error('$(issue-opened) Nothing to sort.');
496+
}
497+
return [useStatements, useStatements.slice().sort(sortFunction)];
498+
})();
485499

486500
this.activeEditor().edit(textEdit => {
487501
for (let i = 0; i < sorted.length; i++) {
@@ -507,21 +521,28 @@ class Resolver {
507521
return false;
508522
}
509523

510-
getUseStatementsArray() {
511-
let useStatements = [];
524+
getUseStatementsArray(blocked = false) {
525+
const useStatements = [];
526+
const matchRegex = new RegExp(/^(use\s[\w\\]+;)/, 'g');
512527

513528
for (let line = 0; line < this.activeEditor().document.lineCount; line++) {
514-
let text = this.activeEditor().document.lineAt(line).text;
515-
516-
if (text.startsWith('use ')) {
517-
useStatements.push(
518-
text.match(/(\w+?);/)[1]
519-
);
529+
const text = this.activeEditor().document.lineAt(line).text;
530+
const matchRes = text.match(matchRegex);
531+
532+
if (matchRes != null) {
533+
const prevLine = this.activeEditor().document.lineAt(Math.max(0, line - 1)).text;
534+
if (prevLine === '' || !prevLine.match(matchRegex) || useStatements.length === 0)
535+
useStatements.push([{ text: matchRes[0], line }]);
536+
else
537+
useStatements[useStatements.length - 1].push({ text: matchRes[0], line });
520538
} else if (/(class|trait|interface)\s+\w+/.test(text)) {
521539
break;
522540
}
523541
}
524542

543+
if (!blocked)
544+
return useStatements.flat();
545+
525546
return useStatements;
526547
}
527548

0 commit comments

Comments
 (0)