Elffuss BraidHub

kikocis/continue public

Merge pull request #6711 from fbricon/empty-selection-selects-line

feat: allow Cmd+L / Cmd+I on empty selection to select the entire file
Changeset91ef819325d3097187615c6b40f3e68191a755168edaa344f54a94e6599cbfb6
AuthorDallin Romney <dallinromney@gmail.com> · 2025-09-10 18:49 -07:00
CommitterGitHub <noreply@github.com> · 2025-09-10 18:49 -07:00
Parents8e0cf6e8890e, c16c1eb07935

Browse the files at this changeset

3 files changed

modified extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts +66 −5
@@ -4,12 +4,12 @@
44 InputBox,
55 Key,
66 TextEditor,
7+ until,
78 VSBrowser,
89 WebDriver,
910 WebElement,
1011 WebView,
1112 Workbench,
12- until,
1313 } from "vscode-extension-tester";
1414
1515 import { GlobalActions } from "../actions/Global.actions";
@@ -47,11 +47,31 @@
4747
4848 afterEach(async function () {
4949 this.timeout(DEFAULT_TIMEOUT.XL * 1000);
50+ await cleanupChat();
5051 await view.switchBack();
5152 await editor.clearText();
5253 await new EditorView().closeAllEditors();
5354 });
5455
56+ async function cleanupChat(chatInput?: WebElement) {
57+ try {
58+ if (!chatInput) {
59+ chatInput = await TestUtils.waitForSuccess(async () => {
60+ return GUISelectors.getMessageInputFieldAtIndex(view, 0);
61+ });
62+ }
63+ if (
64+ chatInput &&
65+ (await chatInput.isDisplayed()) &&
66+ (await chatInput.isEnabled())
67+ ) {
68+ await chatInput.clear();
69+ }
70+ } catch (e) {
71+ console.error(`Failed to clear chat: ${e}`);
72+ }
73+ }
74+
5575 it("Should correctly undo and redo using keyboard shortcuts when writing a chat message", async () => {
5676 await GUIActions.executeFocusContinueInputShortcut(driver);
5777 ({ view } = await GUIActions.switchToReactIframe());
@@ -105,10 +125,8 @@
105125 );
106126 }).timeout(DEFAULT_TIMEOUT.XL);
107127
108- it("Should not create a code block when Cmd+L is pressed without text highlighted", async () => {
109- const text = "Hello, world!";
110-
111- await editor.setText(text);
128+ it("Should not create a code block when Cmd+L is pressed on an empty document", async () => {
129+ expect((await editor.getText()).trim()).to.equal("");
112130
113131 await GUIActions.executeFocusContinueInputShortcut(driver);
114132
@@ -174,6 +192,9 @@
174192
175193 await driver.wait(until.elementIsNotVisible(textInput), DEFAULT_TIMEOUT.XS);
176194 expect(await textInput.isDisplayed()).to.equal(false);
195+
196+ // Make sure the view is visible again, so it can be cleared in afterEach()
197+ await GUIActions.executeFocusContinueInputShortcut(driver);
177198 }).timeout(DEFAULT_TIMEOUT.XL);
178199
179200 it("Should create a code block when Cmd+L is pressed with text highlighted", async () => {
@@ -197,4 +218,44 @@
197218
198219 await GUIActions.executeFocusContinueInputShortcut(driver);
199220 }).timeout(DEFAULT_TIMEOUT.XL);
221+
222+ it("Should create a code block with the whole file when Cmd+L is pressed on an empty line", async () => {
223+ const text = "Hello,\n\n\nworld!";
224+
225+ await editor.setText(text);
226+ await editor.moveCursor(2, 1); //Move cursor to an empty line
227+
228+ await GUIActions.executeFocusContinueInputShortcut(driver);
229+
230+ ({ view } = await GUIActions.switchToReactIframe());
231+
232+ const codeBlock = await TestUtils.waitForSuccess(() =>
233+ GUISelectors.getInputBoxCodeBlockAtIndex(view, 0),
234+ );
235+ const codeblockContent = await codeBlock.getAttribute(
236+ "data-codeblockcontent",
237+ );
238+
239+ expect(codeblockContent).to.equal(text);
240+ });
241+
242+ it("Should create a code block when Cmd+L is pressed on a non-empty line", async () => {
243+ const text = "Hello, world!";
244+
245+ await editor.setText(text);
246+ await editor.moveCursor(1, 7); //Move cursor to the 1st space
247+
248+ await GUIActions.executeFocusContinueInputShortcut(driver);
249+
250+ ({ view } = await GUIActions.switchToReactIframe());
251+
252+ const codeBlock = await TestUtils.waitForSuccess(() =>
253+ GUISelectors.getInputBoxCodeBlockAtIndex(view, 0),
254+ );
255+ const codeblockContent = await codeBlock.getAttribute(
256+ "data-codeblockcontent",
257+ );
258+
259+ expect(codeblockContent).to.equal(text);
260+ }).timeout(DEFAULT_TIMEOUT.XL);
200261 });
modified extensions/vscode/package.json +2 −3
@@ -524,13 +524,12 @@
524524 "continue.continueSubMenu": [
525525 {
526526 "command": "continue.focusContinueInputWithoutClear",
527- "group": "Continue",
528- "when": "editorHasSelection"
527+ "group": "Continue"
529528 },
530529 {
531530 "command": "continue.focusEdit",
532531 "group": "Continue",
533- "when": "editorHasSelection && !editorReadonly"
532+ "when": "!editorReadonly"
534533 }
535534 ],
536535 "explorer/context": [
modified extensions/vscode/src/util/addCode.ts +38 −18
@@ -36,22 +36,38 @@
3636 };
3737 }
3838
39- if (selection.isEmpty && !allowEmpty) {
39+ if ((selection.isEmpty && !allowEmpty) || isEmptyFile(editor.document)) {
4040 return null;
4141 }
4242
43- let selectionRange = new vscode.Range(selection.start, selection.end);
44- const document = editor.document;
45- // Select the context from the beginning of the selection start line to the selection start position
46- const beginningOfSelectionStartLine = selection.start.with(undefined, 0);
47- const textBeforeSelectionStart = document.getText(
48- new vscode.Range(beginningOfSelectionStartLine, selection.start),
49- );
50- // If there are only whitespace before the start of the selection, include the indentation
51- if (textBeforeSelectionStart.trim().length === 0) {
52- selectionRange = selectionRange.with({
53- start: beginningOfSelectionStartLine,
54- });
43+ let selectionRange: vscode.Range | undefined;
44+ // if the selection is empty and document is not empty, select the whole document
45+ if (selection.isEmpty) {
46+ selectionRange = new vscode.Range(
47+ new vscode.Position(0, 0),
48+ new vscode.Position(
49+ editor.document.lineCount - 1,
50+ editor.document.lineAt(
51+ editor.document.lineCount - 1,
52+ ).range.end.character,
53+ ),
54+ );
55+ }
56+
57+ if (!selectionRange) {
58+ selectionRange = new vscode.Range(selection.start, selection.end);
59+ const document = editor.document;
60+ // Select the context from the beginning of the selection start line to the selection start position
61+ const beginningOfSelectionStartLine = selection.start.with(undefined, 0);
62+ const textBeforeSelectionStart = document.getText(
63+ new vscode.Range(beginningOfSelectionStartLine, selection.start),
64+ );
65+ // If there are only whitespace before the start of the selection, include the indentation
66+ if (textBeforeSelectionStart.trim().length === 0) {
67+ selectionRange = selectionRange.with({
68+ start: beginningOfSelectionStartLine,
69+ });
70+ }
5571 }
5672
5773 const contents = editor.document.getText(selectionRange);
@@ -61,12 +77,12 @@
6177 contents,
6278 range: {
6379 start: {
64- line: selection.start.line,
65- character: selection.start.character,
80+ line: selectionRange.start.line,
81+ character: selectionRange.start.character,
6682 },
6783 end: {
68- line: selection.end.line,
69- character: selection.end.character,
84+ line: selectionRange.end.line,
85+ character: selectionRange.end.character,
7086 },
7187 },
7288 };
@@ -78,7 +94,7 @@
7894 export async function addHighlightedCodeToContext(
7995 webviewProtocol: VsCodeWebviewProtocol | undefined,
8096 ) {
81- const rangeInFileWithContents = getRangeInFileWithContents();
97+ const rangeInFileWithContents = getRangeInFileWithContents(true);
8298 if (rangeInFileWithContents) {
8399 webviewProtocol?.request("highlightedCode", {
84100 rangeInFileWithContents,
@@ -129,6 +145,10 @@
129145 });
130146 }
131147
148+export function isEmptyFile(document: vscode.TextDocument) {
149+ return document.lineCount === 1 && document.lineAt(0).range.isEmpty;
150+}
151+
132152 export function addCodeToContextFromRange(
133153 range: vscode.Range,
134154 webviewProtocol: VsCodeWebviewProtocol,