Skip to content

Commit

Permalink
fix: range create error
Browse files Browse the repository at this point in the history
If the import completion is triggered by deleting a quote on an existing
import and readding it then, the position is the start of the import but
we delete the entire length of the import line which creates a negative
position triggering the bug.

Instead, we short circuit. If the import is already the suggestion we
don't suggest it again.

Fixes #150.
  • Loading branch information
kanej committed Jul 9, 2024
1 parent 476477d commit 232974a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
27 changes: 14 additions & 13 deletions server/src/frameworks/Hardhat/getImportCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ export function getImportCompletions(
? _findNodeModulesContractFilesInIndex(ctx, currentImport)
: _findNodeModulePackagesInIndex(ctx);

return contractFilePaths.map((pathFromNodeModules): CompletionItem => {
const normalizedPath = normalizeSlashes(pathFromNodeModules);

const completionItem: CompletionItem = {
label: normalizedPath,
textEdit: replaceFor(normalizedPath, position, currentImport),

kind: CompletionItemKind.Module,
documentation: "Imports the package",
};

return completionItem;
});
return contractFilePaths
.map((pathFromNodeModules) => normalizeSlashes(pathFromNodeModules))
.filter((p) => p !== currentImport) // Don't suggest the current import
.map((normalizedPath): CompletionItem => {
const completionItem: CompletionItem = {
label: normalizedPath,
textEdit: replaceFor(normalizedPath, position, currentImport),

kind: CompletionItemKind.Module,
documentation: "Imports the package",
};

return completionItem;
});
}

function _findNodeModulesContractFilesInIndex(
Expand Down
5 changes: 5 additions & 0 deletions server/src/services/completion/getImportPathCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ function convertFileToCompletion(
const insertText = `${prefix}${file}`;

if (fileStat.isFile() && file.slice(-4) === ".sol") {
// Don't suggest the current import
if (partial === insertText) {
return null;
}

if (partial === "") {
return {
label,
Expand Down

0 comments on commit 232974a

Please sign in to comment.