fix: add code range for quick actions/fixes (#1687)

* fix: add code range for quick actions/fixes

* Update test.js
This commit is contained in:
Patrick Erichsen 2024-07-08 17:01:10 -05:00 • committed by GitHub
parent 88073bba87
commit 9f2e9bc2df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 104 additions and 139 deletions

View file

@ -41,7 +41,14 @@ export type ToWebviewFromIdeProtocol = ToWebviewFromIdeOrCoreProtocol & {
focusContinueInput: [undefined, void];
focusContinueInputWithoutClear: [undefined, void];
focusContinueInputWithNewSession: [undefined, void];
highlightedCode: [{ rangeInFileWithContents: RangeInFileWithContents }, void];
highlightedCode: [
{
rangeInFileWithContents: RangeInFileWithContents;
prompt?: string;
shouldRun?: boolean;
},
void,
];
addModel: [undefined, void];
openSettings: [undefined, void];
viewHistory: [undefined, void];

View file

@ -172,22 +172,7 @@ class ViewLogsAction : AnAction() {
}
}
class ToggleAuxiliaryBarAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val toolWindowManager = ToolWindowManager.getInstance(project)
val toolWindow = toolWindowManager.getToolWindow("Continue")
if (toolWindow != null) {
if (toolWindow.isVisible) {
toolWindow.component.transferFocus()
toolWindow.hide(null)
} else {
toolWindow.activate(null)
}
}
}
}
class FocusContinueInputWithoutClearAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {

View file

@ -89,12 +89,7 @@
description="View Continue Server Logs">
<!-- No shortcut defined -->
</action>
<action id="continue.toggleAuxiliaryBar"
class="com.github.continuedev.continueintellijextension.actions.ToggleAuxiliaryBarAction"
text="Toggle Right Sidebar" description="Toggle Right Sidebar">
<keyboard-shortcut keymap="$default" first-keystroke="alt ctrl J"/>
<keyboard-shortcut keymap="Mac OS X" first-keystroke="alt meta J"/>
</action>
<action id="continue.focusContinueInputWithoutClear"
class="com.github.continuedev.continueintellijextension.actions.FocusContinueInputWithoutClearAction"
text="Add selected code to context"

View file

@ -143,12 +143,6 @@
"title": "Generate Code",
"group": "Continue"
},
{
"command": "continue.toggleAuxiliaryBar",
"category": "Continue",
"title": "Toggle Right Sidebar",
"group": "Continue"
},
{
"command": "continue.focusContinueInput",
"category": "Continue",
@ -303,11 +297,6 @@
"mac": "cmd+i",
"key": "ctrl+i"
},
{
"command": "continue.toggleAuxiliaryBar",
"mac": "alt+cmd+l",
"key": "alt+ctrl+l"
},
{
"command": "continue.debugTerminal",
"mac": "cmd+shift+r",
@ -337,9 +326,6 @@
{
"command": "continue.quickEdit"
},
{
"command": "continue.toggleAuxiliaryBar"
},
{
"command": "continue.focusContinueInput"
},

View file

@ -47,6 +47,40 @@ function captureCommandTelemetry(
Telemetry.capture(commandName, { isCommandEvent: true, ...properties });
}
function addCodeToContextFromRange(
range: vscode.Range,
webviewProtocol: VsCodeWebviewProtocol,
prompt?: string,
) {
const document = vscode.window.activeTextEditor?.document;
if (!document) {
return;
}
const rangeInFileWithContents = {
filepath: document.uri.fsPath,
contents: document.getText(range),
range: {
start: {
line: range.start.line,
character: range.start.character,
},
end: {
line: range.end.line,
character: range.end.character,
},
},
};
webviewProtocol?.request("highlightedCode", {
rangeInFileWithContents,
prompt,
// Assume `true` since range selection is currently only used for quick actions/fixes
shouldRun: true,
});
}
async function addHighlightedCodeToContext(
edit: boolean,
webviewProtocol: VsCodeWebviewProtocol | undefined,
@ -55,44 +89,9 @@ async function addHighlightedCodeToContext(
if (editor) {
const selection = editor.selection;
if (selection.isEmpty) {
// Capture highlighted terminal text
// const activeTerminal = vscode.window.activeTerminal;
// if (activeTerminal) {
// // Copy selected text
// const tempCopyBuffer = await vscode.env.clipboard.readText();
// await vscode.commands.executeCommand(
// "workbench.action.terminal.copySelection",
// );
// await vscode.commands.executeCommand(
// "workbench.action.terminal.clearSelection",
// );
// const contents = (await vscode.env.clipboard.readText()).trim();
// await vscode.env.clipboard.writeText(tempCopyBuffer);
// // Add to context
// const rangeInFileWithContents = {
// filepath: activeTerminal.name,
// contents,
// range: {
// start: {
// line: 0,
// character: 0,
// },
// end: {
// line: contents.split("\n").length,
// character: 0,
// },
// },
// };
// if (contents.trim() !== "") {
// webviewProtocol?.request("highlightedCode", {
// rangeInFileWithContents,
// });
// }
// }
return;
}
// adjust starting position to include indentation
const start = new vscode.Position(selection.start.line, 0);
const range = new vscode.Range(start, selection.end);
@ -260,23 +259,16 @@ const commandsMap: (
verticalDiffManager.acceptRejectVerticalDiffBlock(false, filepath, index);
},
"continue.quickFix": async (
message: string,
code: string,
edit: boolean,
range: vscode.Range,
diagnosticMessage: string,
) => {
captureCommandTelemetry("rejectVerticalDiffBlock", {
edit,
});
captureCommandTelemetry("quickFix");
sidebar.webviewProtocol?.request("newSessionWithPrompt", {
prompt: `${
edit ? "/edit " : ""
}${code}\n\nHow do I fix this problem in the above code?: ${message}`,
});
const prompt = `How do I fix the following problem in the above code?: ${diagnosticMessage}`;
if (!edit) {
vscode.commands.executeCommand("continue.continueGUIView.focus");
}
addCodeToContextFromRange(range, sidebar.webviewProtocol, prompt);
vscode.commands.executeCommand("continue.continueGUIView.focus");
},
"continue.defaultQuickActionDocstring": async (range: vscode.Range) => {
captureCommandTelemetry("defaultQuickActionDocstring");
@ -288,28 +280,26 @@ const commandsMap: (
range,
);
},
"continue.defaultQuickActionExplain": async (code: string) => {
"continue.defaultQuickActionExplain": async (range: vscode.Range) => {
captureCommandTelemetry("defaultQuickActionExplain");
vscode.commands.executeCommand("continue.continueGUIView.focus");
const prompt =
`Explain the above code in a few sentences without ` +
`going into detail on specific methods.`;
sidebar.webviewProtocol?.request("userInput", {
input:
`Explain the following code in a few sentences without ` +
`going into detail on specific methods:\n\n ${code}`,
});
addCodeToContextFromRange(range, sidebar.webviewProtocol, prompt);
vscode.commands.executeCommand("continue.continueGUIView.focus");
},
"continue.customQuickActionSendToChat": async (
prompt: string,
code: string,
range: vscode.Range,
) => {
captureCommandTelemetry("customQuickActionSendToChat");
vscode.commands.executeCommand("continue.continueGUIView.focus");
addCodeToContextFromRange(range, sidebar.webviewProtocol, prompt);
sidebar.webviewProtocol?.request("userInput", {
input: `${prompt}:\n\n ${code}`,
});
vscode.commands.executeCommand("continue.continueGUIView.focus");
},
"continue.customQuickActionStreamInlineEdit": async (
prompt: string,
@ -341,9 +331,6 @@ const commandsMap: (
);
await addHighlightedCodeToContext(true, sidebar.webviewProtocol);
},
"continue.toggleAuxiliaryBar": () => {
vscode.commands.executeCommand("workbench.action.toggleAuxiliaryBar");
},
"continue.quickEdit": (injectedPrompt?: string) => {
captureCommandTelemetry("quickEdit");
quickEdit.run(injectedPrompt);
@ -598,8 +585,8 @@ const commandsMap: (
currentStatus === StatusBarStatus.Paused
? StatusBarStatus.Enabled
: currentStatus === StatusBarStatus.Disabled
? StatusBarStatus.Paused
: StatusBarStatus.Disabled;
? StatusBarStatus.Paused
: StatusBarStatus.Disabled;
} else {
// Toggle between Disabled and Enabled
targetStatus =

View file

@ -15,34 +15,29 @@ class ContinueQuickFixProvider implements vscode.CodeActionProvider {
return [];
}
const createQuickFix = (edit: boolean) => {
const diagnostic = context.diagnostics[0];
const quickFix = new vscode.CodeAction(
edit ? "Fix with Continue" : "Ask Continue",
vscode.CodeActionKind.QuickFix,
);
quickFix.isPreferred = false;
const surroundingRange = new vscode.Range(
Math.max(0, range.start.line - 3),
0,
Math.min(document.lineCount, range.end.line + 3),
0,
);
quickFix.command = {
command: "continue.quickFix",
title: "Continue Quick Fix",
arguments: [
diagnostic.message,
document.getText(surroundingRange),
edit,
],
};
return quickFix;
const diagnostic = context.diagnostics[0];
const quickFix = new vscode.CodeAction(
"Ask Continue",
vscode.CodeActionKind.QuickFix,
);
quickFix.isPreferred = false;
const surroundingRange = new vscode.Range(
Math.max(0, range.start.line - 3),
0,
Math.min(document.lineCount, range.end.line + 3),
0,
);
quickFix.command = {
command: "continue.quickFix",
title: "Continue Quick Fix",
arguments: [surroundingRange, diagnostic.message],
};
return [
// createQuickFix(true),
createQuickFix(false),
];
return [quickFix];
}
}

View file

@ -65,7 +65,6 @@ export class QuickActionsCodeLensProvider implements vscode.CodeLensProvider {
}
getCustomCommands(
code: string,
range: vscode.Range,
quickActionConfigs: QuickActionConfig[],
): vscode.Command[] {
@ -74,7 +73,7 @@ export class QuickActionsCodeLensProvider implements vscode.CodeLensProvider {
? {
title,
command: "continue.customQuickActionSendToChat",
arguments: [prompt, code],
arguments: [prompt, range],
}
: {
title,
@ -84,11 +83,11 @@ export class QuickActionsCodeLensProvider implements vscode.CodeLensProvider {
});
}
getDefaultCommands(code: string, range: vscode.Range): vscode.Command[] {
getDefaultCommands(range: vscode.Range): vscode.Command[] {
const explain: vscode.Command = {
command: "continue.defaultQuickActionExplain",
title: "Explain",
arguments: [code],
arguments: [range],
};
const comment: vscode.Command = {
@ -124,11 +123,9 @@ export class QuickActionsCodeLensProvider implements vscode.CodeLensProvider {
);
return filteredSmybols.flatMap(({ range }) => {
const code = editor.document.getText(range);
const commands: vscode.Command[] = !!this.customQuickActionsConfig
? this.getCustomCommands(code, range, this.customQuickActionsConfig)
: this.getDefaultCommands(code, range);
? this.getCustomCommands(range, this.customQuickActionsConfig)
: this.getDefaultCommands(range);
return commands.map((command) => new vscode.CodeLens(range, command));
});

View file

@ -597,7 +597,9 @@ function TipTapEditor(props: TipTapEditorProps) {
rif.filepath,
await ideMessenger.ide.getWorkspaceDirs(),
);
const rangeStr = `(${rif.range.start.line + 1}-${rif.range.end.line + 1})`;
const rangeStr = `(${rif.range.start.line + 1}-${
rif.range.end.line + 1
})`;
const item: ContextItemWithId = {
content: rif.contents,
name: `${basename} ${rangeStr}`,
@ -626,6 +628,16 @@ function TipTapEditor(props: TipTapEditorProps) {
},
})
.run();
if (data.prompt) {
editor.commands.focus("end");
editor.commands.insertContent(data.prompt);
}
if (data.shouldRun) {
onEnterRef.current({ useCodebase: false, noContext: true });
}
setTimeout(() => {
editor.commands.blur();
editor.commands.focus("end");
@ -639,6 +651,7 @@ function TipTapEditor(props: TipTapEditorProps) {
historyLength,
ignoreHighlightedCode,
props.isMainInput,
onEnterRef.current,
],
);