Skip to content

Commit

Permalink
fixup! Improve ImportDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardSergeev committed Jun 21, 2024
1 parent cb0cf37 commit 0759c39
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
5 changes: 5 additions & 0 deletions input/after/ImportProvider.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Control.Arrow ((>>>))
import Data.Bits ((.&.))
import Data.Char ( isDigit )
import Data.List (foldl', sort, tails)
import Data.Maybe

Expand All @@ -18,3 +19,7 @@ escaped =
dot :: Int
dot =
42 .&. 1

existing :: Char -> Bool
existing =
isDigit
5 changes: 5 additions & 0 deletions input/before/ImportProvider.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Data.Char ( )

foo :: Ord a => [a] -> Maybe [a]
foo xs =
Expand All @@ -14,3 +15,7 @@ escaped =
dot :: Int
dot =
42 .&. 1

existing :: Char -> Bool
existing =
isDigit
39 changes: 25 additions & 14 deletions src/features/importProvider/importDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,38 @@ export default class ImportDeclaration {
};

public get importElements() {
const separators = this._importSeparators.concat([this._after]);
return [this._before].concat(this._importElements.flatMap((elem, i) => [elem, separators[i]])).join('');
const separators = this._importSeparators.concat('');
const list = this._importElements.flatMap((elem, i) => [elem, separators[i]]);
return [this._before, ...list, this._after].join('');
}

public set importElements(elementsString: string) {
const input = elementsString ?? '';
const empty = /^\s*$/g;
const before = /^\s*/g;
const elements = /[^\s,]+(\s*\([^,]+\))?/g;
const separators = /\S(\s*,\s*)\S/g;
const after = /\s*$/g;
elementsString ??= '';
this._before = elementsString.match(before)[0];
this._importElements = elementsString ? [...elementsString.matchAll(elements)].map(m => m[0]) : [];
this._importSeparators = elementsString ? [...elementsString.matchAll(separators)].map(m => m[1]) : [];
this._after = elementsString.match(after)[0];
const separators = /(?<=\S)\s*,\s*(?=\S)/g;
if (empty.test(input)) {
const middle = input.length / 2;
this._before = input.slice(0, middle);
this._after = input.slice(middle)
} else {
this._before = input.match(before)[0];
this._after = input.match(after)[0];
}
const matches = [...input.matchAll(separators)].map(m => [m.index, m[0]] as const);
this._importSeparators = matches.map(m => m[1]);
const indices = matches.map(m => [m[0], m[0] + m[1].length] as const);
const starts = [this._before.length].concat(indices.map(ixs => ixs[1]));
const ends = indices.map(ixs => ixs[0]).concat(input.length - this._after.length);
this._importElements = starts.map((ix, i) => input.slice(ix, ends[i])).filter(e => e !== '');
}

public addImportElement(newElem: string) {
let before = this.importElements;
if (this._importElements.length === 0) {
this.importList = " (I)";
before = "I";
let before = `(${this.importElements})`;
if (!this.importList) {
this.importList = " ()";
before = "()";
}

let index = this._importElements.findIndex(oldElem => this.compareImportElements(newElem, oldElem) < 0);
Expand All @@ -74,7 +85,7 @@ export default class ImportDeclaration {
}
this._importElements.splice(index, 0, newElem);

this.importList = this.importList.replace(before, this.importElements);
this.importList = this.importList.replace(before, `(${this.importElements})`);
}

public removeElement(elem: string) {
Expand Down
5 changes: 3 additions & 2 deletions src/test/providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ suite('', () => {
});

test('Add missing import', () => {
return runQuickfixTest('ImportProvider.hs', 6,
return runQuickfixTest('ImportProvider.hs', 7,
'Add: "import Control.Arrow ((>>>))"',
'Add: "import Data.Maybe"',
'Add: "import Data.List (tails)"',
'Add: "import Data.List (sort)"',
'Add: "import Data.List (foldl\')"',
'Add: "import Data.Bits ((.&.))"'
'Add: "import Data.Bits ((.&.))"',
'Add: "import Data.Char (isDigit)"'
);
});

Expand Down

0 comments on commit 0759c39

Please sign in to comment.