From 67f9e25fba312814528217315f4fa6724d8a66f2 Mon Sep 17 00:00:00 2001 From: Anupam Kumar Date: Sun, 16 Jul 2023 03:41:05 +0530 Subject: [PATCH] add option to sort individual blocks of imports separately Signed-off-by: Anupam Kumar --- package.json | 5 +++++ src/Resolver.js | 53 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 701cdc5..9cb2467 100644 --- a/package.json +++ b/package.json @@ -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, diff --git a/src/Resolver.js b/src/Resolver.js index cf28949..d0bcaf2 100644 --- a/src/Resolver.js +++ b/src/Resolver.js @@ -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; @@ -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++) { @@ -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; }