Skip to content

Commit

Permalink
Fix corner cases with autocompletion (#277)
Browse files Browse the repository at this point in the history
* Add more error handling for autocompletion

* Misc

* Add 'var' declarations to autocompletion

* 0.16.3
  • Loading branch information
rvanasa authored Mar 25, 2024
1 parent 47d1d01 commit a34ba78
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 43 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-motoko",
"displayName": "Motoko",
"description": "Motoko language support",
"version": "0.16.2",
"version": "0.16.3",
"publisher": "dfinity-foundation",
"repository": "https://github.com/dfinity/vscode-motoko",
"engines": {
Expand Down
7 changes: 2 additions & 5 deletions src/server/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,8 @@ export default class ImportResolver {
* Finds all available module-level imports.
* @returns Array of `[name, path]` entries
*/
getNameEntries(uri: string): [string, string][] {
return [...this._moduleNameUriMap.entries()].map(([name, path]) => [
name,
getRelativeUri(uri, path),
]);
getNameEntries(): [string, string][] {
return [...this._moduleNameUriMap.entries()];
}

// /**
Expand Down
88 changes: 53 additions & 35 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import {
import {
formatMotoko,
getFileText,
getRelativeUri,
rangeContainsPosition,
resolveFilePath,
resolveVirtualPath,
Expand Down Expand Up @@ -1044,35 +1045,51 @@ connection.onCompletion((event) => {
?.slice(1) ?? ['', ''];

if (!dot) {
let hadError = false;
context.importResolver
.getNameEntries(uri)
.forEach(([name, path]) => {
.getNameEntries()
.forEach(([name, importPath]) => {
if (name.startsWith(identStart)) {
const status = context.astResolver.request(uri);
const existingImport = status?.program?.imports.find(
(i) =>
i.name === name ||
i.fields.some(([, alias]) => alias === name),
);
if (existingImport || !status?.program) {
// Skip alternatives with already imported name
return;
try {
const path = importPath.startsWith('mo:')
? importPath
: getRelativeUri(uri, importPath);

const status = context.astResolver.request(uri);
const existingImport =
status?.program?.imports.find(
(i) =>
i.name === name ||
i.fields.some(
([, alias]) => alias === name,
),
);
if (existingImport || !status?.program) {
// Skip alternatives with already imported name
return;
}
const edits: TextEdit[] = [
TextEdit.insert(
findNewImportPosition(uri, context, path),
`import ${name} "${path}";\n`,
),
];
list.items.push({
label: name,
detail: path,
insertText: name,
kind: path.startsWith('mo:')
? CompletionItemKind.Module
: CompletionItemKind.Class, // TODO: resolve actors, classes, etc.
additionalTextEdits: edits,
});
} catch (err) {
if (!hadError) {
hadError = true;
console.error('Error during autocompletion:');
console.error(err);
}
}
const edits: TextEdit[] = [
TextEdit.insert(
findNewImportPosition(uri, context, path),
`import ${name} "${path}";\n`,
),
];
list.items.push({
label: name,
detail: path,
insertText: name,
kind: path.startsWith('mo:')
? CompletionItemKind.Module
: CompletionItemKind.Class, // TODO: resolve actors, classes, etc.
additionalTextEdits: edits,
});
}
});

Expand All @@ -1092,14 +1109,15 @@ connection.onCompletion((event) => {
if (program) {
// TODO: only show relevant identifiers
const idents = new Set<string>();
findNodes(program.ast, (node) => node.name === 'VarP').forEach(
(node) => {
const ident = node.args?.[0];
if (typeof ident === 'string') {
idents.add(ident);
}
},
);
findNodes(
program.ast,
(node) => node.name === 'VarP' || node.name === 'VarD',
).forEach((node) => {
const ident = node.args?.[0]; // First arg for both `VarP` and `VarD`
if (typeof ident === 'string') {
idents.add(ident);
}
});
idents.forEach((ident) => {
list.items.push({
label: ident,
Expand Down Expand Up @@ -1298,7 +1316,7 @@ connection.onDefinition(
);
return definition ? locationFromDefinition(definition) : [];
} catch (err) {
console.error(`Error while finding definition:`);
console.error('Error while finding definition:');
console.error(err);
// throw err;
return [];
Expand Down

0 comments on commit a34ba78

Please sign in to comment.