Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 66 additions & 5 deletions extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
InputBox,
Key,
TextEditor,
until,
VSBrowser,
WebDriver,
WebElement,
WebView,
Workbench,
until,
} from "vscode-extension-tester";

import { GlobalActions } from "../actions/Global.actions";
Expand Down Expand Up @@ -47,11 +47,31 @@ describe("Keyboard Shortcuts", () => {

afterEach(async function () {
this.timeout(DEFAULT_TIMEOUT.XL * 1000);
await cleanupChat();
await view.switchBack();
await editor.clearText();
await new EditorView().closeAllEditors();
});

async function cleanupChat(chatInput?: WebElement) {
try {
if (!chatInput) {
chatInput = await TestUtils.waitForSuccess(async () => {
return GUISelectors.getMessageInputFieldAtIndex(view, 0);
});
}
if (
chatInput &&
(await chatInput.isDisplayed()) &&
(await chatInput.isEnabled())
) {
await chatInput.clear();
}
} catch (e) {
console.error(`Failed to clear chat: ${e}`);
}
}

it("Should correctly undo and redo using keyboard shortcuts when writing a chat message", async () => {
await GUIActions.executeFocusContinueInputShortcut(driver);
({ view } = await GUIActions.switchToReactIframe());
Expand Down Expand Up @@ -105,10 +125,8 @@ 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!";

await editor.setText(text);
it("Should not create a code block when Cmd+L is pressed on an empty document", async () => {
expect((await editor.getText()).trim()).to.equal("");

await GUIActions.executeFocusContinueInputShortcut(driver);

Expand Down Expand Up @@ -174,6 +192,9 @@ describe("Keyboard Shortcuts", () => {

await driver.wait(until.elementIsNotVisible(textInput), DEFAULT_TIMEOUT.XS);
expect(await textInput.isDisplayed()).to.equal(false);

// Make sure the view is visible again, so it can be cleared in afterEach()
await GUIActions.executeFocusContinueInputShortcut(driver);
}).timeout(DEFAULT_TIMEOUT.XL);

it("Should create a code block when Cmd+L is pressed with text highlighted", async () => {
Expand All @@ -197,4 +218,44 @@ describe("Keyboard Shortcuts", () => {

await GUIActions.executeFocusContinueInputShortcut(driver);
}).timeout(DEFAULT_TIMEOUT.XL);

it("Should create a code block with the whole file when Cmd+L is pressed on an empty line", async () => {
const text = "Hello,\n\n\nworld!";

await editor.setText(text);
await editor.moveCursor(2, 1); //Move cursor to an empty line

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);
});

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);
}).timeout(DEFAULT_TIMEOUT.XL);
});
5 changes: 2 additions & 3 deletions extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,12 @@
"continue.continueSubMenu": [
{
"command": "continue.focusContinueInputWithoutClear",
"group": "Continue",
"when": "editorHasSelection"
"group": "Continue"
},
{
"command": "continue.focusEdit",
"group": "Continue",
"when": "editorHasSelection && !editorReadonly"
"when": "!editorReadonly"
}
],
"explorer/context": [
Expand Down
56 changes: 38 additions & 18 deletions extensions/vscode/src/util/addCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,38 @@ export function getRangeInFileWithContents(
};
}

if (selection.isEmpty && !allowEmpty) {
if ((selection.isEmpty && !allowEmpty) || isEmptyFile(editor.document)) {
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 document is not empty, select the whole document
if (selection.isEmpty) {
selectionRange = new vscode.Range(
new vscode.Position(0, 0),
new vscode.Position(
editor.document.lineCount - 1,
editor.document.lineAt(
editor.document.lineCount - 1,
).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);
Expand All @@ -61,12 +77,12 @@ export function getRangeInFileWithContents(
contents,
range: {
start: {
line: selection.start.line,
character: selection.start.character,
line: selectionRange.start.line,
character: selectionRange.start.character,
},
end: {
line: selection.end.line,
character: selection.end.character,
line: selectionRange.end.line,
character: selectionRange.end.character,
},
},
};
Expand All @@ -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,
Expand Down Expand Up @@ -129,6 +145,10 @@ export async function addEntireFileToContext(
});
}

export function isEmptyFile(document: vscode.TextDocument) {
return document.lineCount === 1 && document.lineAt(0).range.isEmpty;
}

export function addCodeToContextFromRange(
range: vscode.Range,
webviewProtocol: VsCodeWebviewProtocol,
Expand Down
Loading