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

feat(vscode): adding auto-import during accept inline completion item #3389

Merged
Merged
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
38 changes: 36 additions & 2 deletions clients/vscode/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
QuickPickItem,
ViewColumn,
Range,
CodeAction,
CodeActionKind,
} from "vscode";
import os from "os";
import path from "path";
Expand Down Expand Up @@ -222,8 +224,20 @@ export class Commands {
"inlineCompletion.trigger": () => {
commands.executeCommand("editor.action.inlineSuggest.trigger");
},
"inlineCompletion.accept": () => {
commands.executeCommand("editor.action.inlineSuggest.commit");
"inlineCompletion.accept": async () => {
const editor = window.activeTextEditor;
if (!editor) {
return;
}

const uri = editor.document.uri;
const range = this.inlineCompletionProvider.calcEditedRangeAfterAccept();

await commands.executeCommand("editor.action.inlineSuggest.commit");

if (range) {
applyQuickFixes(uri, range);
}
},
"inlineCompletion.acceptNextWord": () => {
this.inlineCompletionProvider.handleEvent("accept_word");
Expand Down Expand Up @@ -452,3 +466,23 @@ export class Commands {
},
};
}

async function applyQuickFixes(uri: Uri, range: Range): Promise<void> {
const codeActions = await commands.executeCommand<CodeAction[]>("vscode.executeCodeActionProvider", uri, range);
const quickFixActions = codeActions.filter(
(action) =>
action.kind && action.kind.contains(CodeActionKind.QuickFix) && action.title.toLowerCase().includes("import"),
);
quickFixActions.forEach(async (action) => {
try {
if (action.edit) {
await workspace.applyEdit(action.edit);
}
if (action.command) {
await commands.executeCommand(action.command.command, action.command.arguments);
}
} catch (error) {
// ignore errors
}
});
}
23 changes: 23 additions & 0 deletions clients/vscode/src/InlineCompletionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,27 @@ export class InlineCompletionProvider extends EventEmitter implements InlineComp
// await not required
this.client.telemetry.postEvent(params);
}

/**
* Calculate the edited range in the modified document as if the current completion item has been accepted.
* @return {Range | undefined} - The range with the current completion item
*/
public calcEditedRangeAfterAccept(): Range | undefined {
const item = this.displayedCompletion?.completions.items[this.displayedCompletion.index];
const range = item?.range;
if (!range) {
// FIXME: If the item has a null range, we can use current position and text length to calculate the result range
return undefined;
Sma1lboy marked this conversation as resolved.
Show resolved Hide resolved
}
if (!item) {
return undefined;
}
const length = (item.insertText as string).split("\n").length - 1; //remove current line count;
const completionRange = new Range(
new Position(range.start.line, range.start.character),
new Position(range.end.line + length + 1, 0),
);
this.logger.debug("Calculate edited range for displayed completion item:", completionRange);
return completionRange;
}
}