Merge pull request #573 from devchat-ai/refactor_quick_fix
feat: Add new IDE service endpoints and refactor quick fix command
This commit is contained in:
commit
0b2fed27f9
@ -352,15 +352,17 @@ export function registerFixCommand(context: vscode.ExtensionContext) {
|
|||||||
export async function registerQuickFixCommand(context: vscode.ExtensionContext) {
|
export async function registerQuickFixCommand(context: vscode.ExtensionContext) {
|
||||||
let disposable = vscode.commands.registerCommand(
|
let disposable = vscode.commands.registerCommand(
|
||||||
"DevChat.quickFix",
|
"DevChat.quickFix",
|
||||||
async (diagnosticMessage: string, code: string, surroundingCode: string) => {
|
async (document: vscode.TextDocument, range: vscode.Range | vscode.Selection, diagnostic: vscode.Diagnostic) => {
|
||||||
ensureChatPanel(context);
|
ensureChatPanel(context);
|
||||||
if (!ExtensionContextHolder.provider?.view()) {
|
if (!ExtensionContextHolder.provider?.view()) {
|
||||||
await waitForPanelActivation();
|
await waitForPanelActivation();
|
||||||
}
|
}
|
||||||
|
|
||||||
const language = DevChatConfig.getInstance().get('language');
|
// select the code
|
||||||
const prompt = await generatePrompt(code, surroundingCode, diagnosticMessage, language);
|
const editor = vscode.window.activeTextEditor;
|
||||||
chatWithDevChat(ExtensionContextHolder.provider?.view()!, prompt);
|
editor!.selection = new vscode.Selection(range.start, range.end);
|
||||||
|
|
||||||
|
chatWithDevChat(ExtensionContextHolder.provider?.view()!, "/fix_issue ");
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -24,22 +24,13 @@ class DevChatQuickFixProvider implements vscode.CodeActionProvider {
|
|||||||
quickFix.isPreferred = false;
|
quickFix.isPreferred = false;
|
||||||
|
|
||||||
return new Promise(async (resolve) => {
|
return new Promise(async (resolve) => {
|
||||||
const code = await collapseFileExculdeSelectRange(document.uri.fsPath, document.getText(), range.start.line, range.end.line);
|
|
||||||
|
|
||||||
const surroundingRange = new vscode.Range(
|
|
||||||
Math.max(0, range.start.line - 3),
|
|
||||||
0,
|
|
||||||
Math.min(document.lineCount, range.end.line + 3),
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
|
|
||||||
quickFix.command = {
|
quickFix.command = {
|
||||||
command: "DevChat.quickFix",
|
command: "DevChat.quickFix",
|
||||||
title: "DevChat Quick Fix",
|
title: "DevChat Quick Fix",
|
||||||
arguments: [
|
arguments: [
|
||||||
diagnostic.message,
|
document,
|
||||||
code,
|
range,
|
||||||
document.getText(surroundingRange)
|
diagnostic,
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
12
src/ide_services/endpoints/documentRangeDiagnostics.ts
Normal file
12
src/ide_services/endpoints/documentRangeDiagnostics.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
export async function getDiagnosticsInRange(fileName: string, startLine: number, endLine: number): Promise<string[]> {
|
||||||
|
const document = await vscode.workspace.openTextDocument(fileName);
|
||||||
|
const startPosition = new vscode.Position(startLine, 0);
|
||||||
|
const endPosition = new vscode.Position(endLine, Number.MAX_VALUE);
|
||||||
|
const range = new vscode.Range(startPosition, endPosition);
|
||||||
|
const diagnosticsAll = vscode.languages.getDiagnostics(document.uri);
|
||||||
|
|
||||||
|
const diagnostics = diagnosticsAll.filter(diag => range.contains(diag.range));
|
||||||
|
return diagnostics.map(diag => { return `${diag.message} <<${diag.source??""}:${diag.code??""}>>`; });
|
||||||
|
}
|
11
src/ide_services/endpoints/getCollapsedCode.ts
Normal file
11
src/ide_services/endpoints/getCollapsedCode.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { collapseFileExculdeSelectRange } from '../../contributes/codecomplete/ast/collapseBlock';
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
export async function getCollapsedCode(fileName: string, startLine: number, endLine: number): Promise<string> {
|
||||||
|
const document = await vscode.workspace.openTextDocument(fileName);
|
||||||
|
const startPosition = new vscode.Position(startLine, 0);
|
||||||
|
const endPosition = new vscode.Position(endLine, Number.MAX_VALUE);
|
||||||
|
const range = new vscode.Range(startPosition, endPosition);
|
||||||
|
const code = await collapseFileExculdeSelectRange(document.uri.fsPath, document.getText(), range.start.line, range.end.line);
|
||||||
|
return code;
|
||||||
|
}
|
6
src/ide_services/endpoints/getToolsPath.ts
Normal file
6
src/ide_services/endpoints/getToolsPath.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { UiUtilWrapper } from "../../util/uiUtil";
|
||||||
|
|
||||||
|
|
||||||
|
export async function getExtensionToolsPath(): Promise<string> {
|
||||||
|
return await UiUtilWrapper.extensionPath() + "/tools/";
|
||||||
|
}
|
@ -14,6 +14,9 @@ import { getDocumentSymbols } from "./endpoints/getDocumentSymbols";
|
|||||||
import { findTypeDefinitionLocations } from "./endpoints/findTypeDefs";
|
import { findTypeDefinitionLocations } from "./endpoints/findTypeDefs";
|
||||||
import { findDefinitionLocations } from "./endpoints/findDefs";
|
import { findDefinitionLocations } from "./endpoints/findDefs";
|
||||||
import { getCurrentFileInfo } from "./endpoints/getCurrentFileInfo";
|
import { getCurrentFileInfo } from "./endpoints/getCurrentFileInfo";
|
||||||
|
import { getDiagnosticsInRange } from "./endpoints/documentRangeDiagnostics";
|
||||||
|
import { getExtensionToolsPath } from "./endpoints/getToolsPath";
|
||||||
|
import { getCollapsedCode } from "./endpoints/getCollapsedCode";
|
||||||
|
|
||||||
const functionRegistry: any = {
|
const functionRegistry: any = {
|
||||||
/**
|
/**
|
||||||
@ -89,6 +92,18 @@ const functionRegistry: any = {
|
|||||||
"/current_file_info": {
|
"/current_file_info": {
|
||||||
keys: [],
|
keys: [],
|
||||||
handler: getCurrentFileInfo,
|
handler: getCurrentFileInfo,
|
||||||
|
},
|
||||||
|
"/get_diagnostics_in_range": {
|
||||||
|
keys: ["fileName", "startLine", "endLine"],
|
||||||
|
handler: getDiagnosticsInRange,
|
||||||
|
},
|
||||||
|
"/get_extension_tools_path": {
|
||||||
|
keys: [],
|
||||||
|
handler: getExtensionToolsPath,
|
||||||
|
},
|
||||||
|
"/get_collapsed_code": {
|
||||||
|
keys: ["fileName", "startLine", "endLine"],
|
||||||
|
handler: getCollapsedCode,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user