Skip to content

Commit ee55a20

Browse files
authored
Merge pull request #595 from devchat-ai/shortcut_code_completion
Update packages, add features, and improve logging
2 parents 4f17211 + d76a15f commit ee55a20

File tree

11 files changed

+845
-712
lines changed

11 files changed

+845
-712
lines changed

package-lock.json

Lines changed: 517 additions & 444 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "devchat",
33
"displayName": "${ASSISTANT_NAME_ZH}",
44
"description": "Write prompts, not code",
5-
"version": "0.1.74",
5+
"version": "0.1.84",
66
"icon": "assets/devchat.png",
77
"publisher": "${PUBLISHER}",
88
"engines": {
@@ -91,6 +91,14 @@
9191
]
9292
},
9393
"commands": [
94+
{
95+
"command": "devchat.triggerCodeComplete",
96+
"title": "Trigger Code Completion"
97+
},
98+
{
99+
"command": "devchat.triggerCodeCompleteChinese",
100+
"title": "触发代码补全"
101+
},
94102
{
95103
"command": "devchat.applyDiffResult",
96104
"title": "Apply Diff",
@@ -189,6 +197,12 @@
189197
"command": "devchat.openChatPanel",
190198
"key": "ctrl+shift+/",
191199
"mac": "cmd+shift+/"
200+
},
201+
{
202+
"command": "devchat.triggerCodeComplete",
203+
"key": "ctrl+shift+'",
204+
"mac": "cmd+shift+'",
205+
"when": "editorTextFocus"
192206
}
193207
],
194208
"menus": {
@@ -220,6 +234,14 @@
220234
"command": "devchat.addConext_chinese",
221235
"when": "false"
222236
},
237+
{
238+
"command": "devchat.triggerCodeComplete",
239+
"when": "false"
240+
},
241+
{
242+
"command": "devchat.triggerCodeCompleteChinese",
243+
"when": "false"
244+
},
223245
{
224246
"command": "devchat.askForCode_chinese",
225247
"when": "false"
@@ -290,6 +312,16 @@
290312
}
291313
],
292314
"editor/context": [
315+
{
316+
"command": "devchat.triggerCodeComplete",
317+
"when": "!isChineseLocale && editorTextFocus",
318+
"group": "navigation"
319+
},
320+
{
321+
"command": "devchat.triggerCodeCompleteChinese",
322+
"when": "isChineseLocale && editorTextFocus",
323+
"group": "navigation"
324+
},
293325
{
294326
"command": "devchat.askForCode_chinese",
295327
"when": "isChineseLocale && editorTextFocus && editorHasSelection",

src/contributes/codecomplete/codecomplete.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function registerCodeCompleteCallbackCommand(context: vscode.ExtensionCon
1616
let disposable = vscode.commands.registerCommand(
1717
"DevChat.codecomplete_callback",
1818
async (callback: any) => {
19+
logger.channel()?.trace(`Trigger codecomplete callback command`);
1920
callback();
2021
}
2122
);
@@ -63,6 +64,7 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv
6364
private previousCodeComplete: CodeCompleteResult | undefined;
6465
private previousPrefix: string | undefined;
6566
private preCompletionItem: vscode.InlineCompletionItem | undefined;
67+
private isManualTrigger: boolean = false;
6668

6769
constructor() {
6870
// TODO
@@ -126,7 +128,17 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv
126128
// return true;
127129
// }
128130

131+
async triggerCodeComplete(document: vscode.TextDocument, position: vscode.Position) {
132+
this.isManualTrigger = true;
133+
await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger');
134+
// 重置标记,以便下次正常检查配置
135+
setTimeout(() => {
136+
this.isManualTrigger = false;
137+
}, 100);
138+
}
139+
129140
async codeComplete(document: vscode.TextDocument, position: vscode.Position, context: vscode.InlineCompletionContext, token: vscode.CancellationToken): Promise<CodeCompleteResultWithMeta | undefined> {
141+
logger.channel()?.debug("codeComplete called");
130142
const startTime = process.hrtime();
131143
GitDiffWatcher.getInstance().tryRun();
132144

@@ -139,6 +151,7 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv
139151

140152
const prompt = await createPrompt(fsPath, fileContent, position.line, position.character, posOffset, this.recentEditors.getEdits());
141153
if (!prompt) {
154+
logger.channel()?.debug("prompt is empty");
142155
return undefined;
143156
}
144157
logger.channel()?.trace("prompt:", prompt);
@@ -203,7 +216,7 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv
203216
// if (context.selectedCompletionInfo) {
204217
// return [];
205218
// }
206-
if (this.devchatConfig.get("complete_enable") !== true) {
219+
if (!this.isManualTrigger && this.devchatConfig.get("complete_enable") !== true) {
207220
return [];
208221
}
209222

@@ -221,7 +234,9 @@ export class InlineCompletionProvider implements vscode.InlineCompletionItemProv
221234
let response: CodeCompleteResultWithMeta | undefined = undefined;
222235

223236
// 获取当前光标前三行代码
224-
const linePrefix = document.getText(new vscode.Range(position.line - 4, 0, position.line, position.character));
237+
let preLinesNum = 4;
238+
const startLine = Math.max(0, position.line - preLinesNum);
239+
const linePrefix = document.getText(new vscode.Range(startLine, 0, position.line, position.character));
225240

226241
// 如果this.previousPrefix + this.previousCodeComplete包含“当前行光标之前内容”,且index为0,那么不需要执行代码补全
227242
if (this.previousPrefix && this.previousCodeComplete && this.previousCodeComplete.code.length > 0) {

src/contributes/codecomplete/llm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
通过LLM模型生成代码补全
44
*/
55
import axios from 'axios';
6+
import fetch from 'node-fetch';
67

78
import { logger } from "../../util/logger";
89
import { Chunk } from 'webpack';

src/contributes/commands.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export function registerInstallCommandsCommand(
191191
let disposable = vscode.commands.registerCommand(
192192
"DevChat.InstallCommands",
193193
async () => {
194+
logger.channel()?.debug("InstallCommands command triggered.");
194195
const homePath = process.env.HOME || process.env.USERPROFILE || "";
195196
const sysDirPath = path.join(homePath, ".chat", "scripts");
196197
const sysMericoDirPath = path.join(homePath, ".chat", "scripts", "merico");
@@ -202,17 +203,20 @@ export function registerInstallCommandsCommand(
202203
const dcClient = new DevChatClient();
203204

204205
if (!fs.existsSync(sysMericoDirPath)) {
206+
logger.channel()?.debug("Creating directory: " + sysMericoDirPath);
205207
await copyDirectory(pluginDirPath, sysDirPath);
206208
}
207209

208210
// Check if ~/.chat/scripts directory exists
209-
if (!fs.existsSync(sysDirPath)) {
211+
if (!fs.existsSync(sysMericoDirPath)) {
210212
// Directory does not exist, wait for updateWorkflows to finish
213+
logger.channel()?.debug("Update workflows...");
211214
await dcClient.updateWorkflows();
212215
await dcClient.updateCustomWorkflows();
213216
sendCommandListByDevChatRun();
214217
} else {
215218
// Directory exists, execute sendCommandListByDevChatRun immediately
219+
logger.channel()?.debug("Sending and updating workflows...");
216220
await sendCommandListByDevChatRun();
217221

218222
// Then asynchronously execute updateWorkflows

src/extension.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ async function activate(context: vscode.ExtensionContext) {
146146
context.subscriptions.push(vscode.languages.registerInlineCompletionItemProvider(selector, provider));
147147
registerCodeCompleteCallbackCommand(context);
148148

149+
function handleCodeComplete() {
150+
const editor = vscode.window.activeTextEditor;
151+
if (editor) {
152+
const position = editor.selection.active;
153+
provider.triggerCodeComplete(editor.document, position);
154+
}
155+
}
156+
157+
// command for code completion
158+
context.subscriptions.push(
159+
// 注册英文命令
160+
vscode.commands.registerCommand('devchat.triggerCodeComplete', handleCodeComplete),
161+
// 注册中文命令
162+
vscode.commands.registerCommand('devchat.triggerCodeCompleteChinese', handleCodeComplete)
163+
);
149164

150165
registerOpenChatPanelCommand(context);
151166
registerAddContextCommand(context);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { logger } from '../../util/logger';
12
import * as vscode from 'vscode';
23

34

45
export async function updateSlashCommands() {
6+
logger.channel()?.debug('Updating slash commands...');
57
vscode.commands.executeCommand('DevChat.InstallCommands');
68
return true;
79
}

src/panel/statusBarView.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as vscode from 'vscode';
33
import { dependencyCheck } from './statusBarViewBase';
44
import { ProgressBar } from '../util/progressBar';
55
import { ASSISTANT_NAME_EN } from '../util/constants';
6+
import { logger } from '../util/logger';
67

78

89
export function createStatusBarItem(context: vscode.ExtensionContext): vscode.StatusBarItem {
@@ -50,7 +51,9 @@ export function createStatusBarItem(context: vscode.ExtensionContext): vscode.St
5051
// install devchat workflow commands
5152
if (!hasInstallCommands) {
5253
hasInstallCommands = true;
54+
logger.channel()?.debug("Starting local service...");
5355
await vscode.commands.executeCommand('DevChat.StartLocalService');
56+
logger.channel()?.debug("Installing commands...");
5457
await vscode.commands.executeCommand('DevChat.InstallCommands');
5558
// vscode.commands.executeCommand('DevChat.InstallCommandPython');
5659
}

tools

Submodule tools updated 171 files

0 commit comments

Comments
 (0)