Skip to content

Commit

Permalink
add option to sort individual blocks of imports separately
Browse files Browse the repository at this point in the history
Signed-off-by: Anupam Kumar <[email protected]>
  • Loading branch information
kyteinsky committed Jul 15, 2023
1 parent fa57397 commit 67f9e25
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
"default": false,
"description": "Sort imports using a 'natural order' algorithm"
},
"namespaceResolver.sortBlockWise": {
"type": "boolean",
"default": false,
"description": "Sort imports in individual blocks separated by blank lines"
},
"namespaceResolver.leadingSeparator": {
"type": "boolean",
"default": true,
Expand Down
53 changes: 37 additions & 16 deletions src/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,7 @@ class Resolver {
return parsedNamespaces;
}

sortImports() {
let [useStatements,] = this.getDeclarations();

if (useStatements.length <= 1) {
throw new Error('$(issue-opened) Nothing to sort.');
}

getSortFunction() {
let sortFunction = (a, b) => {
if (this.config('sortAlphabetically')) {
if (a.text.toLowerCase() < b.text.toLowerCase()) return -1;
Expand All @@ -481,7 +475,27 @@ class Resolver {
};
}

let sorted = useStatements.slice().sort(sortFunction);
return sortFunction;
}

sortImports() {
const sortFunction = this.getSortFunction();

const [useStatements, sorted] = (() => {
if (this.config('sortBlockWise')) {
const useStatements = this.getUseStatementsArray(true);
if (useStatements.length === 0 || useStatements[0].length <= 1) {
throw new Error('$(issue-opened) Nothing to sort.');
}
return [useStatements.flat(), useStatements.map(block => block.slice().sort(sortFunction)).flat()];
}

const useStatements = this.getDeclarations();
if (useStatements.length <= 1) {
throw new Error('$(issue-opened) Nothing to sort.');
}
return [useStatements, useStatements.slice().sort(sortFunction)];
})();

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

getUseStatementsArray() {
let useStatements = [];
getUseStatementsArray(blocked = false) {
const useStatements = [];
const matchRegex = new RegExp(/^(use\s[\w\\]+;)/, 'g');

for (let line = 0; line < this.activeEditor().document.lineCount; line++) {
let text = this.activeEditor().document.lineAt(line).text;

if (text.startsWith('use ')) {
useStatements.push(
text.match(/(\w+?);/)[1]
);
const text = this.activeEditor().document.lineAt(line).text;
const matchRes = text.match(matchRegex);

if (matchRes != null) {
const prevLine = this.activeEditor().document.lineAt(Math.max(0, line - 1)).text;
if (prevLine === '' || !prevLine.match(matchRegex) || useStatements.length === 0)
useStatements.push([{ text: matchRes[0], line }]);
else
useStatements[useStatements.length - 1].push({ text: matchRes[0], line });
} else if (/(class|trait|interface)\s+\w+/.test(text)) {
break;
}
}

if (!blocked)
return useStatements.flat();

return useStatements;
}

Expand Down

0 comments on commit 67f9e25

Please sign in to comment.