diff --git a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts index e9b79472f0d..60dd595e0c5 100644 --- a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts +++ b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts @@ -4,12 +4,12 @@ import { InputBox, Key, TextEditor, + until, VSBrowser, WebDriver, WebElement, WebView, - Workbench, - until, + Workbench } from "vscode-extension-tester"; import { GUIActions } from "../actions/GUI.actions"; @@ -103,10 +103,12 @@ describe("Keyboard Shortcuts", () => { ); }).timeout(DEFAULT_TIMEOUT.XL); - it("Should not create a code block when Cmd+L is pressed without text highlighted", async () => { - const text = "Hello, world!"; + + it("Should not create a code block when Cmd+L is pressed on an empty line", async () => { + const text = "\n\nHello, world!"; await editor.setText(text); + await editor.moveCursor(1, 1);//Move cursor to the 1st line await GUIActions.executeFocusContinueInputShortcut(driver); @@ -115,9 +117,54 @@ describe("Keyboard Shortcuts", () => { await TestUtils.expectNoElement(async () => { return GUISelectors.getInputBoxCodeBlockAtIndex(view, 0); }, DEFAULT_TIMEOUT.XS); + + }); + + it("Should create a code block when Cmd+L is pressed with text highlighted", async () => { + const text = "Hello, world!"; + + await editor.setText(text); + await editor.selectText(text); + + await GUIActions.executeFocusContinueInputShortcut(driver); + + ({ view } = await GUIActions.switchToReactIframe()); + + const codeBlock = await TestUtils.waitForSuccess(() => + GUISelectors.getInputBoxCodeBlockAtIndex(view, 0), + ); + const codeblockContent = await codeBlock.getAttribute( + "data-codeblockcontent", + ); + + expect(codeblockContent).to.equal(text); + + await GUIActions.executeFocusContinueInputShortcut(driver); + }).timeout(DEFAULT_TIMEOUT.XL); + + it("Should create a code block when Cmd+L is pressed on a non-empty line", async () => { + const text = "Hello, world!"; + + await editor.setText(text); + await editor.moveCursor(1, 7);//Move cursor to the 1st space + + await GUIActions.executeFocusContinueInputShortcut(driver); + + ({ view } = await GUIActions.switchToReactIframe()); + + const codeBlock = await TestUtils.waitForSuccess(() => + GUISelectors.getInputBoxCodeBlockAtIndex(view, 0), + ); + const codeblockContent = await codeBlock.getAttribute( + "data-codeblockcontent", + ); + + expect(codeblockContent).to.equal(text); + await GUIActions.executeFocusContinueInputShortcut(driver); }).timeout(DEFAULT_TIMEOUT.XL); + it("Fresh VS Code window → sidebar closed → cmd+L with no code highlighted → opens sidebar and focuses input → cmd+L closes sidebar", async () => { await GUIActions.executeFocusContinueInputShortcut(driver); ({ view } = await GUIActions.switchToReactIframe()); @@ -174,25 +221,4 @@ describe("Keyboard Shortcuts", () => { expect(await textInput.isDisplayed()).to.equal(false); }).timeout(DEFAULT_TIMEOUT.XL); - it("Should create a code block when Cmd+L is pressed with text highlighted", async () => { - const text = "Hello, world!"; - - await editor.setText(text); - await editor.selectText(text); - - await GUIActions.executeFocusContinueInputShortcut(driver); - - ({ view } = await GUIActions.switchToReactIframe()); - - const codeBlock = await TestUtils.waitForSuccess(() => - GUISelectors.getInputBoxCodeBlockAtIndex(view, 0), - ); - const codeblockContent = await codeBlock.getAttribute( - "data-codeblockcontent", - ); - - expect(codeblockContent).to.equal(text); - - await GUIActions.executeFocusContinueInputShortcut(driver); - }).timeout(DEFAULT_TIMEOUT.XL); }); diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 4a321af0c9e..028a8bde7f2 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -525,13 +525,12 @@ "continue.continueSubMenu": [ { "command": "continue.focusContinueInputWithoutClear", - "group": "Continue", - "when": "editorHasSelection" + "group": "Continue" }, { "command": "continue.focusEdit", "group": "Continue", - "when": "editorHasSelection && !editorReadonly" + "when": "!editorReadonly" }, { "command": "continue.writeCommentsForCode", diff --git a/extensions/vscode/src/util/addCode.ts b/extensions/vscode/src/util/addCode.ts index 524d77e8817..2fe3ea2ec11 100644 --- a/extensions/vscode/src/util/addCode.ts +++ b/extensions/vscode/src/util/addCode.ts @@ -40,18 +40,34 @@ export function getRangeInFileWithContents( 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, - }); + let selectionRange: vscode.Range | undefined; + // if the selection is empty and the line is not, then use the entire line + if (selection.isEmpty) { + const startLine = editor.document.lineAt(selection.start.line); + if (startLine.isEmptyOrWhitespace) { + // Empty selection on an empty line, nothing else to do here + return null; + } + // Select the whole line + selectionRange = new vscode.Range( + selection.start.with(undefined, 0), + selection.end.with(undefined, startLine.range.end.character), + ); + } + if (!selectionRange) { + 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); @@ -78,7 +94,7 @@ export function getRangeInFileWithContents( export async function addHighlightedCodeToContext( webviewProtocol: VsCodeWebviewProtocol | undefined, ) { - const rangeInFileWithContents = getRangeInFileWithContents(); + const rangeInFileWithContents = getRangeInFileWithContents(true); if (rangeInFileWithContents) { webviewProtocol?.request("highlightedCode", { rangeInFileWithContents,