default to current file selection
This commit is contained in:
parent
577af64909
commit
bb2bae3e10
7 changed files with 255 additions and 225 deletions
|
|
@ -51,6 +51,7 @@ export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & {
|
|||
},
|
||||
string | undefined,
|
||||
];
|
||||
"edit/addCurrentSelection": [undefined, void];
|
||||
"edit/clearDecorations": [{ shouldFocusEditor: boolean }, void];
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,4 @@
|
|||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import * as os from "node:os";
|
||||
|
||||
import {
|
||||
ContextMenuConfig,
|
||||
ILLM,
|
||||
ModelInstaller,
|
||||
RangeInFileWithContents,
|
||||
} from "core";
|
||||
import { ContextMenuConfig, ILLM, ModelInstaller } from "core";
|
||||
import { CompletionProvider } from "core/autocomplete/CompletionProvider";
|
||||
import { ConfigHandler } from "core/config/ConfigHandler";
|
||||
import { ContinueServerClient } from "core/continueServer/stubs/client";
|
||||
|
|
@ -40,7 +32,11 @@ import { Battery } from "./util/battery";
|
|||
import { getMetaKeyLabel } from "./util/util";
|
||||
import { VsCodeIde } from "./VsCodeIde";
|
||||
|
||||
import type { VsCodeWebviewProtocol } from "./webviewProtocol";
|
||||
import {
|
||||
addCodeToContextFromRange,
|
||||
addEntireFileToContext,
|
||||
addHighlightedCodeToContext,
|
||||
} from "./util/addCode";
|
||||
|
||||
let fullScreenPanel: vscode.WebviewPanel | undefined;
|
||||
|
||||
|
|
@ -63,160 +59,6 @@ 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.toString(),
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
function getRangeInFileWithContents(
|
||||
allowEmpty?: boolean,
|
||||
range?: vscode.Range,
|
||||
): RangeInFileWithContents | null {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
|
||||
if (editor) {
|
||||
const selection = editor.selection;
|
||||
const filepath = editor.document.uri.toString();
|
||||
|
||||
if (range) {
|
||||
const contents = editor.document.getText(range);
|
||||
|
||||
return {
|
||||
range: {
|
||||
start: {
|
||||
line: range.start.line,
|
||||
character: range.start.character,
|
||||
},
|
||||
end: {
|
||||
line: range.end.line,
|
||||
character: range.end.character,
|
||||
},
|
||||
},
|
||||
filepath,
|
||||
contents,
|
||||
};
|
||||
}
|
||||
|
||||
if (selection.isEmpty && !allowEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let selectionRange = new vscode.Range(selection.start, selection.end);
|
||||
const document = editor.document;
|
||||
// Select the context from the beginning of the selection start line to the selection start position
|
||||
const beginningOfSelectionStartLine = selection.start.with(undefined, 0);
|
||||
const textBeforeSelectionStart = document.getText(
|
||||
new vscode.Range(beginningOfSelectionStartLine, selection.start),
|
||||
);
|
||||
// If there are only whitespace before the start of the selection, include the indentation
|
||||
if (textBeforeSelectionStart.trim().length === 0) {
|
||||
selectionRange = selectionRange.with({
|
||||
start: beginningOfSelectionStartLine,
|
||||
});
|
||||
}
|
||||
|
||||
const contents = editor.document.getText(selectionRange);
|
||||
|
||||
return {
|
||||
filepath,
|
||||
contents,
|
||||
range: {
|
||||
start: {
|
||||
line: selection.start.line,
|
||||
character: selection.start.character,
|
||||
},
|
||||
end: {
|
||||
line: selection.end.line,
|
||||
character: selection.end.character,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function addHighlightedCodeToContext(
|
||||
webviewProtocol: VsCodeWebviewProtocol | undefined,
|
||||
) {
|
||||
const rangeInFileWithContents = getRangeInFileWithContents();
|
||||
if (rangeInFileWithContents) {
|
||||
webviewProtocol?.request("highlightedCode", {
|
||||
rangeInFileWithContents,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function addEntireFileToContext(
|
||||
uri: vscode.Uri,
|
||||
webviewProtocol: VsCodeWebviewProtocol | undefined,
|
||||
) {
|
||||
// If a directory, add all files in the directory
|
||||
const stat = await vscode.workspace.fs.stat(uri);
|
||||
if (stat.type === vscode.FileType.Directory) {
|
||||
const files = await vscode.workspace.fs.readDirectory(uri);
|
||||
for (const [filename, type] of files) {
|
||||
if (type === vscode.FileType.File) {
|
||||
addEntireFileToContext(
|
||||
vscode.Uri.joinPath(uri, filename),
|
||||
webviewProtocol,
|
||||
);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the contents of the file
|
||||
const contents = (await vscode.workspace.fs.readFile(uri)).toString();
|
||||
const rangeInFileWithContents = {
|
||||
filepath: uri.toString(),
|
||||
contents: contents,
|
||||
range: {
|
||||
start: {
|
||||
line: 0,
|
||||
character: 0,
|
||||
},
|
||||
end: {
|
||||
line: contents.split(os.EOL).length - 1,
|
||||
character: 0,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
webviewProtocol?.request("highlightedCode", {
|
||||
rangeInFileWithContents,
|
||||
});
|
||||
}
|
||||
|
||||
function focusGUI() {
|
||||
const fullScreenTab = getFullScreenTab();
|
||||
if (fullScreenTab) {
|
||||
|
|
@ -536,61 +378,6 @@ const getCommandsMap: (
|
|||
focusGUI();
|
||||
|
||||
sidebar.webviewProtocol?.request("focusEdit", undefined);
|
||||
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
|
||||
const existingDiff = verticalDiffManager.getHandlerForFile(
|
||||
editor.document.fileName,
|
||||
);
|
||||
|
||||
// If there's a diff currently being applied, then we just toggle focus back to the input
|
||||
if (existingDiff) {
|
||||
sidebar.webviewProtocol?.request("focusContinueInput", undefined);
|
||||
return;
|
||||
}
|
||||
|
||||
const startFromCharZero = editor.selection.start.with(undefined, 0);
|
||||
const document = editor.document;
|
||||
let lastLine, lastChar;
|
||||
// If the user selected onto a trailing line but didn't actually include any characters in it
|
||||
// they don't want to include that line, so trim it off.
|
||||
if (editor.selection.end.character === 0) {
|
||||
// This is to prevent the rare case that the previous line gets selected when user
|
||||
// is selecting nothing and the cursor is at the beginning of the line
|
||||
if (editor.selection.end.line === editor.selection.start.line) {
|
||||
lastLine = editor.selection.start.line;
|
||||
} else {
|
||||
lastLine = editor.selection.end.line - 1;
|
||||
}
|
||||
} else {
|
||||
lastLine = editor.selection.end.line;
|
||||
}
|
||||
lastChar = document.lineAt(lastLine).range.end.character;
|
||||
const endAtCharLast = new vscode.Position(lastLine, lastChar);
|
||||
const range =
|
||||
args?.range ?? new vscode.Range(startFromCharZero, endAtCharLast);
|
||||
|
||||
editDecorationManager.clear();
|
||||
editDecorationManager.addDecorations(editor, [range]);
|
||||
|
||||
const rangeInFileWithContents = getRangeInFileWithContents(true, range);
|
||||
|
||||
if (rangeInFileWithContents) {
|
||||
sidebar.webviewProtocol?.request(
|
||||
"setCodeToEdit",
|
||||
rangeInFileWithContents,
|
||||
);
|
||||
|
||||
// Un-select the current selection
|
||||
editor.selection = new vscode.Selection(
|
||||
editor.selection.anchor,
|
||||
editor.selection.anchor,
|
||||
);
|
||||
}
|
||||
},
|
||||
"continue.exitEditMode": async () => {
|
||||
captureCommandTelemetry("exitEditMode");
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import * as vscode from "vscode";
|
|||
import { EDIT_MODE_STREAM_ID } from "core/edit/constants";
|
||||
import { ApplyManager } from "../apply";
|
||||
import { VerticalDiffManager } from "../diff/vertical/manager";
|
||||
import { addCurrentSelectionToEdit } from "../quickEdit/AddCurrentSelection";
|
||||
import EditDecorationManager from "../quickEdit/EditDecorationManager";
|
||||
import {
|
||||
getControlPlaneSessionInfo,
|
||||
|
|
@ -189,6 +190,15 @@ export class VsCodeMessenger {
|
|||
);
|
||||
});
|
||||
});
|
||||
this.onWebview("edit/addCurrentSelection", async (msg) => {
|
||||
const verticalDiffManager = await this.verticalDiffManagerPromise;
|
||||
await addCurrentSelectionToEdit({
|
||||
args: undefined,
|
||||
editDecorationManager,
|
||||
webviewProtocol: this.webviewProtocol,
|
||||
verticalDiffManager,
|
||||
});
|
||||
});
|
||||
this.onWebview("edit/sendPrompt", async (msg) => {
|
||||
const prompt = msg.data.prompt;
|
||||
const { start, end } = msg.data.range.range;
|
||||
|
|
|
|||
70
extensions/vscode/src/quickEdit/AddCurrentSelection.ts
Normal file
70
extensions/vscode/src/quickEdit/AddCurrentSelection.ts
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import * as vscode from "vscode";
|
||||
import { VerticalDiffManager } from "../diff/vertical/manager";
|
||||
import { getRangeInFileWithContents } from "../util/addCode";
|
||||
import { VsCodeWebviewProtocol } from "../webviewProtocol";
|
||||
import EditDecorationManager from "./EditDecorationManager";
|
||||
import { QuickEditShowParams } from "./QuickEditQuickPick";
|
||||
|
||||
export async function addCurrentSelectionToEdit({
|
||||
webviewProtocol,
|
||||
verticalDiffManager,
|
||||
args,
|
||||
editDecorationManager,
|
||||
}: {
|
||||
webviewProtocol: VsCodeWebviewProtocol;
|
||||
verticalDiffManager: VerticalDiffManager;
|
||||
args: QuickEditShowParams | undefined;
|
||||
editDecorationManager: EditDecorationManager;
|
||||
}) {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
|
||||
const existingDiff = verticalDiffManager.getHandlerForFile(
|
||||
editor.document.fileName,
|
||||
);
|
||||
|
||||
// If there's a diff currently being applied, then we just toggle focus back to the input
|
||||
if (existingDiff) {
|
||||
webviewProtocol?.request("focusContinueInput", undefined);
|
||||
return;
|
||||
}
|
||||
|
||||
const startFromCharZero = editor.selection.start.with(undefined, 0);
|
||||
const document = editor.document;
|
||||
let lastLine, lastChar;
|
||||
// If the user selected onto a trailing line but didn't actually include any characters in it
|
||||
// they don't want to include that line, so trim it off.
|
||||
if (editor.selection.end.character === 0) {
|
||||
// This is to prevent the rare case that the previous line gets selected when user
|
||||
// is selecting nothing and the cursor is at the beginning of the line
|
||||
if (editor.selection.end.line === editor.selection.start.line) {
|
||||
lastLine = editor.selection.start.line;
|
||||
} else {
|
||||
lastLine = editor.selection.end.line - 1;
|
||||
}
|
||||
} else {
|
||||
lastLine = editor.selection.end.line;
|
||||
}
|
||||
lastChar = document.lineAt(lastLine).range.end.character;
|
||||
const endAtCharLast = new vscode.Position(lastLine, lastChar);
|
||||
const range =
|
||||
args?.range ?? new vscode.Range(startFromCharZero, endAtCharLast);
|
||||
|
||||
editDecorationManager.clear();
|
||||
editDecorationManager.addDecorations(editor, [range]);
|
||||
|
||||
const rangeInFileWithContents = getRangeInFileWithContents(true, range);
|
||||
|
||||
if (rangeInFileWithContents) {
|
||||
webviewProtocol?.request("setCodeToEdit", rangeInFileWithContents);
|
||||
|
||||
// Un-select the current selection
|
||||
editor.selection = new vscode.Selection(
|
||||
editor.selection.anchor,
|
||||
editor.selection.anchor,
|
||||
);
|
||||
}
|
||||
}
|
||||
158
extensions/vscode/src/util/addCode.ts
Normal file
158
extensions/vscode/src/util/addCode.ts
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
import { RangeInFileWithContents } from "core";
|
||||
import * as os from "node:os";
|
||||
import * as vscode from "vscode";
|
||||
import type { VsCodeWebviewProtocol } from "../webviewProtocol";
|
||||
|
||||
export function getRangeInFileWithContents(
|
||||
allowEmpty?: boolean,
|
||||
range?: vscode.Range,
|
||||
): RangeInFileWithContents | null {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
|
||||
if (editor) {
|
||||
const selection = editor.selection;
|
||||
const filepath = editor.document.uri.toString();
|
||||
|
||||
if (range) {
|
||||
const contents = editor.document.getText(range);
|
||||
|
||||
return {
|
||||
range: {
|
||||
start: {
|
||||
line: range.start.line,
|
||||
character: range.start.character,
|
||||
},
|
||||
end: {
|
||||
line: range.end.line,
|
||||
character: range.end.character,
|
||||
},
|
||||
},
|
||||
filepath,
|
||||
contents,
|
||||
};
|
||||
}
|
||||
|
||||
if (selection.isEmpty && !allowEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let selectionRange = new vscode.Range(selection.start, selection.end);
|
||||
const document = editor.document;
|
||||
// Select the context from the beginning of the selection start line to the selection start position
|
||||
const beginningOfSelectionStartLine = selection.start.with(undefined, 0);
|
||||
const textBeforeSelectionStart = document.getText(
|
||||
new vscode.Range(beginningOfSelectionStartLine, selection.start),
|
||||
);
|
||||
// If there are only whitespace before the start of the selection, include the indentation
|
||||
if (textBeforeSelectionStart.trim().length === 0) {
|
||||
selectionRange = selectionRange.with({
|
||||
start: beginningOfSelectionStartLine,
|
||||
});
|
||||
}
|
||||
|
||||
const contents = editor.document.getText(selectionRange);
|
||||
|
||||
return {
|
||||
filepath,
|
||||
contents,
|
||||
range: {
|
||||
start: {
|
||||
line: selection.start.line,
|
||||
character: selection.start.character,
|
||||
},
|
||||
end: {
|
||||
line: selection.end.line,
|
||||
character: selection.end.character,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function addHighlightedCodeToContext(
|
||||
webviewProtocol: VsCodeWebviewProtocol | undefined,
|
||||
) {
|
||||
const rangeInFileWithContents = getRangeInFileWithContents();
|
||||
if (rangeInFileWithContents) {
|
||||
webviewProtocol?.request("highlightedCode", {
|
||||
rangeInFileWithContents,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function addEntireFileToContext(
|
||||
uri: vscode.Uri,
|
||||
webviewProtocol: VsCodeWebviewProtocol | undefined,
|
||||
) {
|
||||
// If a directory, add all files in the directory
|
||||
const stat = await vscode.workspace.fs.stat(uri);
|
||||
if (stat.type === vscode.FileType.Directory) {
|
||||
const files = await vscode.workspace.fs.readDirectory(uri);
|
||||
for (const [filename, type] of files) {
|
||||
if (type === vscode.FileType.File) {
|
||||
addEntireFileToContext(
|
||||
vscode.Uri.joinPath(uri, filename),
|
||||
webviewProtocol,
|
||||
);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the contents of the file
|
||||
const contents = (await vscode.workspace.fs.readFile(uri)).toString();
|
||||
const rangeInFileWithContents = {
|
||||
filepath: uri.toString(),
|
||||
contents: contents,
|
||||
range: {
|
||||
start: {
|
||||
line: 0,
|
||||
character: 0,
|
||||
},
|
||||
end: {
|
||||
line: contents.split(os.EOL).length - 1,
|
||||
character: 0,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
webviewProtocol?.request("highlightedCode", {
|
||||
rangeInFileWithContents,
|
||||
});
|
||||
}
|
||||
|
||||
export function addCodeToContextFromRange(
|
||||
range: vscode.Range,
|
||||
webviewProtocol: VsCodeWebviewProtocol,
|
||||
prompt?: string,
|
||||
) {
|
||||
const document = vscode.window.activeTextEditor?.document;
|
||||
|
||||
if (!document) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rangeInFileWithContents = {
|
||||
filepath: document.uri.toString(),
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
|
@ -68,24 +68,23 @@ function ModeSelect() {
|
|||
? ["chat", "agent"]
|
||||
: ["chat", "agent", "edit"];
|
||||
const currentIndex = modes.indexOf(mode);
|
||||
const oneModeAhead = modes[(currentIndex + 1) % modes.length];
|
||||
// const twoModesAhead = modes[(currentIndex + 2) % modes.length];
|
||||
const nextMode = modes[(currentIndex + 1) % modes.length];
|
||||
if (mode === "edit") {
|
||||
await dispatch(
|
||||
exitEditMode({
|
||||
goToMode: oneModeAhead,
|
||||
goToMode: nextMode,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
if (oneModeAhead === "edit") {
|
||||
if (nextMode === "edit") {
|
||||
await dispatch(
|
||||
enterEditMode({
|
||||
returnToMode: mode, // twoModesAhead
|
||||
returnToMode: mode,
|
||||
returnCursorToEditor: false,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
dispatch(setMode(oneModeAhead));
|
||||
dispatch(setMode(nextMode));
|
||||
}
|
||||
}
|
||||
}, [jetbrains, mode]);
|
||||
|
|
|
|||
|
|
@ -84,6 +84,7 @@ export const exitEditMode = createAsyncThunk<
|
|||
});
|
||||
|
||||
dispatch(clearCodeToEdit());
|
||||
dispatch(updateEditStateApplyState(INITIAL_EDIT_APPLY_STATE));
|
||||
|
||||
if (openNewSession || state.editModeState.lastNonEditSessionWasEmpty) {
|
||||
dispatch(newSession());
|
||||
|
|
@ -127,5 +128,9 @@ export const enterEditMode = createAsyncThunk<
|
|||
);
|
||||
dispatch(updateEditStateApplyState(INITIAL_EDIT_APPLY_STATE));
|
||||
dispatch(setMode("edit"));
|
||||
|
||||
if (!state.editModeState.codeToEdit[0]) {
|
||||
extra.ideMessenger.post("edit/addCurrentSelection", undefined);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue