Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to sort individual blocks of imports separately #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ You can override these default settings according to your needs.
"namespaceResolver.sortOnSave": false, // Auto sort when a file is saved
"namespaceResolver.sortAlphabetically": false, // Sort imports in alphabetical order instead of line length
"namespaceResolver.sortNatural": false, // Sort imports using a 'natural order' algorithm
"namespaceResolver.sortBlockWise": false, // Sort imports in individual blocks separated by blank lines
"namespaceResolver.leadingSeparator": true, // Expand class with leading namespace separator
"namespaceResolver.highlightOnSave": false, // Auto highlight not imported and not used when a file is saved
"namespaceResolver.highlightOnOpen": false // Auto highlight not imported and not used when a file is opened
Expand Down
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
47 changes: 31 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,21 @@ 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);
return [useStatements.flat(), useStatements.map(block => block.slice().sort(sortFunction)).flat()];
}

const useStatements = this.getDeclarations();
return [useStatements, useStatements.slice().sort(sortFunction)];
})();

this.activeEditor().edit(textEdit => {
for (let i = 0; i < sorted.length; i++) {
Expand All @@ -507,21 +515,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