Skip to content

Commit 0b2fed2

Browse files
authored
Merge pull request #573 from devchat-ai/refactor_quick_fix
feat: Add new IDE service endpoints and refactor quick fix command
2 parents a308d09 + 2831337 commit 0b2fed2

File tree

6 files changed

+53
-16
lines changed

6 files changed

+53
-16
lines changed

src/contributes/commands.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,17 @@ export function registerFixCommand(context: vscode.ExtensionContext) {
352352
export async function registerQuickFixCommand(context: vscode.ExtensionContext) {
353353
let disposable = vscode.commands.registerCommand(
354354
"DevChat.quickFix",
355-
async (diagnosticMessage: string, code: string, surroundingCode: string) => {
355+
async (document: vscode.TextDocument, range: vscode.Range | vscode.Selection, diagnostic: vscode.Diagnostic) => {
356356
ensureChatPanel(context);
357357
if (!ExtensionContextHolder.provider?.view()) {
358358
await waitForPanelActivation();
359359
}
360360

361-
const language = DevChatConfig.getInstance().get('language');
362-
const prompt = await generatePrompt(code, surroundingCode, diagnosticMessage, language);
363-
chatWithDevChat(ExtensionContextHolder.provider?.view()!, prompt);
361+
// select the code
362+
const editor = vscode.window.activeTextEditor;
363+
editor!.selection = new vscode.Selection(range.start, range.end);
364+
365+
chatWithDevChat(ExtensionContextHolder.provider?.view()!, "/fix_issue ");
364366
}
365367
);
366368

src/contributes/quickFixProvider.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,13 @@ class DevChatQuickFixProvider implements vscode.CodeActionProvider {
2424
quickFix.isPreferred = false;
2525

2626
return new Promise(async (resolve) => {
27-
const code = await collapseFileExculdeSelectRange(document.uri.fsPath, document.getText(), range.start.line, range.end.line);
28-
29-
const surroundingRange = new vscode.Range(
30-
Math.max(0, range.start.line - 3),
31-
0,
32-
Math.min(document.lineCount, range.end.line + 3),
33-
0,
34-
);
35-
3627
quickFix.command = {
3728
command: "DevChat.quickFix",
3829
title: "DevChat Quick Fix",
3930
arguments: [
40-
diagnostic.message,
41-
code,
42-
document.getText(surroundingRange)
31+
document,
32+
range,
33+
diagnostic,
4334
],
4435
};
4536

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as vscode from 'vscode';
2+
3+
export async function getDiagnosticsInRange(fileName: string, startLine: number, endLine: number): Promise<string[]> {
4+
const document = await vscode.workspace.openTextDocument(fileName);
5+
const startPosition = new vscode.Position(startLine, 0);
6+
const endPosition = new vscode.Position(endLine, Number.MAX_VALUE);
7+
const range = new vscode.Range(startPosition, endPosition);
8+
const diagnosticsAll = vscode.languages.getDiagnostics(document.uri);
9+
10+
const diagnostics = diagnosticsAll.filter(diag => range.contains(diag.range));
11+
return diagnostics.map(diag => { return `${diag.message} <<${diag.source??""}:${diag.code??""}>>`; });
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { collapseFileExculdeSelectRange } from '../../contributes/codecomplete/ast/collapseBlock';
2+
import * as vscode from 'vscode';
3+
4+
export async function getCollapsedCode(fileName: string, startLine: number, endLine: number): Promise<string> {
5+
const document = await vscode.workspace.openTextDocument(fileName);
6+
const startPosition = new vscode.Position(startLine, 0);
7+
const endPosition = new vscode.Position(endLine, Number.MAX_VALUE);
8+
const range = new vscode.Range(startPosition, endPosition);
9+
const code = await collapseFileExculdeSelectRange(document.uri.fsPath, document.getText(), range.start.line, range.end.line);
10+
return code;
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { UiUtilWrapper } from "../../util/uiUtil";
2+
3+
4+
export async function getExtensionToolsPath(): Promise<string> {
5+
return await UiUtilWrapper.extensionPath() + "/tools/";
6+
}

src/ide_services/services.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { getDocumentSymbols } from "./endpoints/getDocumentSymbols";
1414
import { findTypeDefinitionLocations } from "./endpoints/findTypeDefs";
1515
import { findDefinitionLocations } from "./endpoints/findDefs";
1616
import { getCurrentFileInfo } from "./endpoints/getCurrentFileInfo";
17+
import { getDiagnosticsInRange } from "./endpoints/documentRangeDiagnostics";
18+
import { getExtensionToolsPath } from "./endpoints/getToolsPath";
19+
import { getCollapsedCode } from "./endpoints/getCollapsedCode";
1720

1821
const functionRegistry: any = {
1922
/**
@@ -89,6 +92,18 @@ const functionRegistry: any = {
8992
"/current_file_info": {
9093
keys: [],
9194
handler: getCurrentFileInfo,
95+
},
96+
"/get_diagnostics_in_range": {
97+
keys: ["fileName", "startLine", "endLine"],
98+
handler: getDiagnosticsInRange,
99+
},
100+
"/get_extension_tools_path": {
101+
keys: [],
102+
handler: getExtensionToolsPath,
103+
},
104+
"/get_collapsed_code": {
105+
keys: ["fileName", "startLine", "endLine"],
106+
handler: getCollapsedCode,
92107
}
93108
};
94109

0 commit comments

Comments
 (0)