Skip to content

Commit

Permalink
feat(vscode): adding auto-import during accept inline completion item (
Browse files Browse the repository at this point in the history
…#3389)

* fix(vscode): fix QuickFix codeAction range calculation

* fix: Adjust range calculation in Commands.ts for inline completion acceptance
  • Loading branch information
Sma1lboy authored Nov 17, 2024
1 parent 41785e0 commit 5f0d9f0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
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 @@ -225,8 +227,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 @@ -455,3 +469,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;
}
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;
}
}

0 comments on commit 5f0d9f0

Please sign in to comment.