add command to trigger code completion by shortcut
This commit is contained in:
parent
dc16a78beb
commit
5d345ea2f2
@ -16,6 +16,7 @@ export function registerCodeCompleteCallbackCommand(context: vscode.ExtensionCon
|
|||||||
let disposable = vscode.commands.registerCommand(
|
let disposable = vscode.commands.registerCommand(
|
||||||
"DevChat.codecomplete_callback",
|
"DevChat.codecomplete_callback",
|
||||||
async (callback: any) => {
|
async (callback: any) => {
|
||||||
|
logger.channel()?.trace(`Trigger codecomplete callback command`);
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -63,6 +64,7 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv
|
|||||||
private previousCodeComplete: CodeCompleteResult | undefined;
|
private previousCodeComplete: CodeCompleteResult | undefined;
|
||||||
private previousPrefix: string | undefined;
|
private previousPrefix: string | undefined;
|
||||||
private preCompletionItem: vscode.InlineCompletionItem | undefined;
|
private preCompletionItem: vscode.InlineCompletionItem | undefined;
|
||||||
|
private isManualTrigger: boolean = false;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
// TODO
|
// TODO
|
||||||
@ -126,7 +128,17 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv
|
|||||||
// return true;
|
// return true;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
async triggerCodeComplete(document: vscode.TextDocument, position: vscode.Position) {
|
||||||
|
this.isManualTrigger = true;
|
||||||
|
await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger');
|
||||||
|
// 重置标记,以便下次正常检查配置
|
||||||
|
setTimeout(() => {
|
||||||
|
this.isManualTrigger = false;
|
||||||
|
}, 100);
|
||||||
|
}
|
||||||
|
|
||||||
async codeComplete(document: vscode.TextDocument, position: vscode.Position, context: vscode.InlineCompletionContext, token: vscode.CancellationToken): Promise<CodeCompleteResultWithMeta | undefined> {
|
async codeComplete(document: vscode.TextDocument, position: vscode.Position, context: vscode.InlineCompletionContext, token: vscode.CancellationToken): Promise<CodeCompleteResultWithMeta | undefined> {
|
||||||
|
logger.channel()?.debug("codeComplete called");
|
||||||
const startTime = process.hrtime();
|
const startTime = process.hrtime();
|
||||||
GitDiffWatcher.getInstance().tryRun();
|
GitDiffWatcher.getInstance().tryRun();
|
||||||
|
|
||||||
@ -139,6 +151,7 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv
|
|||||||
|
|
||||||
const prompt = await createPrompt(fsPath, fileContent, position.line, position.character, posOffset, this.recentEditors.getEdits());
|
const prompt = await createPrompt(fsPath, fileContent, position.line, position.character, posOffset, this.recentEditors.getEdits());
|
||||||
if (!prompt) {
|
if (!prompt) {
|
||||||
|
logger.channel()?.debug("prompt is empty");
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
logger.channel()?.trace("prompt:", prompt);
|
logger.channel()?.trace("prompt:", prompt);
|
||||||
@ -203,7 +216,7 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv
|
|||||||
// if (context.selectedCompletionInfo) {
|
// if (context.selectedCompletionInfo) {
|
||||||
// return [];
|
// return [];
|
||||||
// }
|
// }
|
||||||
if (this.devchatConfig.get("complete_enable") !== true) {
|
if (!this.isManualTrigger && this.devchatConfig.get("complete_enable") !== true) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,7 +234,9 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv
|
|||||||
let response: CodeCompleteResultWithMeta | undefined = undefined;
|
let response: CodeCompleteResultWithMeta | undefined = undefined;
|
||||||
|
|
||||||
// 获取当前光标前三行代码
|
// 获取当前光标前三行代码
|
||||||
const linePrefix = document.getText(new vscode.Range(position.line - 4, 0, position.line, position.character));
|
let preLinesNum = 4;
|
||||||
|
const startLine = Math.max(0, position.line - preLinesNum);
|
||||||
|
const linePrefix = document.getText(new vscode.Range(startLine, 0, position.line, position.character));
|
||||||
|
|
||||||
// 如果this.previousPrefix + this.previousCodeComplete包含“当前行光标之前内容”,且index为0,那么不需要执行代码补全
|
// 如果this.previousPrefix + this.previousCodeComplete包含“当前行光标之前内容”,且index为0,那么不需要执行代码补全
|
||||||
if (this.previousPrefix && this.previousCodeComplete && this.previousCodeComplete.code.length > 0) {
|
if (this.previousPrefix && this.previousCodeComplete && this.previousCodeComplete.code.length > 0) {
|
||||||
|
@ -146,6 +146,21 @@ async function activate(context: vscode.ExtensionContext) {
|
|||||||
context.subscriptions.push(vscode.languages.registerInlineCompletionItemProvider(selector, provider));
|
context.subscriptions.push(vscode.languages.registerInlineCompletionItemProvider(selector, provider));
|
||||||
registerCodeCompleteCallbackCommand(context);
|
registerCodeCompleteCallbackCommand(context);
|
||||||
|
|
||||||
|
function handleCodeComplete() {
|
||||||
|
const editor = vscode.window.activeTextEditor;
|
||||||
|
if (editor) {
|
||||||
|
const position = editor.selection.active;
|
||||||
|
provider.triggerCodeComplete(editor.document, position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// command for code completion
|
||||||
|
context.subscriptions.push(
|
||||||
|
// 注册英文命令
|
||||||
|
vscode.commands.registerCommand('devchat.triggerCodeComplete', handleCodeComplete),
|
||||||
|
// 注册中文命令
|
||||||
|
vscode.commands.registerCommand('devchat.triggerCodeCompleteChinese', handleCodeComplete)
|
||||||
|
);
|
||||||
|
|
||||||
registerOpenChatPanelCommand(context);
|
registerOpenChatPanelCommand(context);
|
||||||
registerAddContextCommand(context);
|
registerAddContextCommand(context);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user