Merge pull request #3463 from continuedev/tomasz/shortcut-fixes

fix: close sidebar when cmd+l pressed on focus
This commit is contained in:
Tomasz Stefaniak 2024-12-19 16:43:55 -08:00 • committed by GitHub
commit 2df7055aea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 109 additions and 39 deletions

View file

@ -37,8 +37,8 @@ export class TestUtils {
throw new Error(`Element not found after ${timeout}ms timeout`);
}
public static async expectNoElement(
locatorFn: () => Promise<WebElement>,
public static async expectNoElement<T>(
locatorFn: () => Promise<T>,
timeout: number = 1000,
interval: number = 200,
): Promise<void> {
@ -48,6 +48,7 @@ export class TestUtils {
while (Date.now() - startTime < timeout) {
try {
const element = await locatorFn();
console.log("ELEMENT", element);
if (element) {
elementFound = true;
break;
@ -71,6 +72,10 @@ export class TestUtils {
};
}
public static waitForTimeout(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
public static get isMacOS(): boolean {
return process.platform === "darwin";
}

View file

@ -1,8 +1,33 @@
import { Key, WebElement, WebView, Workbench } from "vscode-extension-tester";
import {
InputBox,
Key,
WebDriver,
WebElement,
WebView,
Workbench,
} from "vscode-extension-tester";
import { GUISelectors } from "../selectors/GUI.selectors";
import { TestUtils } from "../TestUtils";
import { DEFAULT_TIMEOUT } from "../constants";
export class GUIActions {
public static moveContinueToSidebar = async (driver: WebDriver) => {
await GUIActions.toggleGui();
await TestUtils.waitForSuccess(async () => {
await new Workbench().executeCommand("View: Move View");
await (await InputBox.create(DEFAULT_TIMEOUT.MD)).selectQuickPick(4);
await (await InputBox.create(DEFAULT_TIMEOUT.MD)).selectQuickPick(14);
});
// first call focuses the input
await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS);
await GUIActions.executeFocusContinueInputShortcut(driver);
// second call closes the gui
await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS);
await GUIActions.executeFocusContinueInputShortcut(driver);
};
public static switchToReactIframe = async () => {
const view = new WebView();
const driver = view.getDriver();
@ -19,7 +44,7 @@ export class GUIActions {
}
if (!continueIFrame) {
throw new Error("Could not find continue iframe");
throw new Error("Could not find Continue iframe");
}
await driver.switchTo().frame(continueIFrame);
@ -41,7 +66,7 @@ export class GUIActions {
};
};
public static openGui = async () => {
public static toggleGui = async () => {
return TestUtils.waitForSuccess(() =>
new Workbench().executeCommand("continue.focusContinueInput"),
);
@ -77,4 +102,13 @@ export class GUIActions {
await editor.sendKeys(message);
await editor.sendKeys(Key.ENTER);
}
public static async executeFocusContinueInputShortcut(driver: WebDriver) {
return driver
.actions()
.keyDown(TestUtils.osControlKey)
.sendKeys("l")
.keyUp(TestUtils.osControlKey)
.perform();
}
}

View file

@ -1,14 +1,15 @@
import { WebDriver } from "vscode-extension-tester";
import { TextEditor, WebDriver, WebView } from "vscode-extension-tester";
import { TestUtils } from "../TestUtils";
export class KeyboardShortcutsActions {
public static async executeFocusContinueInput(driver: WebDriver) {
return await driver
.actions()
.keyDown(TestUtils.osControlKey)
.sendKeys("l")
.keyUp(TestUtils.osControlKey)
.perform();
/**
* For some reason Selenium-simulated keyboard shortcuts don't perfectly
* mimic the behavior of real shortcuts unless some text is highlighted first.
*/
public static async HACK__typeWithSelect(editor: TextEditor, text: string) {
await editor.typeText(text);
await editor.selectText(text);
await editor.typeText(text);
}
}

View file

@ -18,7 +18,7 @@ describe("GUI Test", () => {
beforeEach(async function () {
this.timeout(DEFAULT_TIMEOUT.XL);
await GUIActions.openGui();
await GUIActions.toggleGui();
({ view, driver } = await GUIActions.switchToReactIframe());
await GUIActions.selectModelFromDropdown(view, "TEST LLM");

View file

@ -6,19 +6,24 @@ import {
TextEditor,
Workbench,
WebView,
WebElement,
} from "vscode-extension-tester";
import { DEFAULT_TIMEOUT } from "../constants";
import { GUISelectors } from "../selectors/GUI.selectors";
import { GUIActions } from "../actions/GUI.actions";
import { expect } from "chai";
import { TestUtils } from "../TestUtils";
import { KeyboardShortcutsActions } from "../actions/KeyboardShortcuts.actions";
describe("Cmd+L Shortcut Test", () => {
let driver: WebDriver;
let editor: TextEditor;
let view: WebView;
before(async function () {
this.timeout(DEFAULT_TIMEOUT.XL);
await GUIActions.moveContinueToSidebar(VSBrowser.instance.driver);
});
beforeEach(async function () {
this.timeout(DEFAULT_TIMEOUT.XL);
@ -38,11 +43,6 @@ describe("Cmd+L Shortcut Test", () => {
this.timeout(DEFAULT_TIMEOUT.XL * 1000);
await view.switchBack();
await editor.clearText();
await TestUtils.waitForSuccess(
async () => (await GUISelectors.getContinueExtensionBadge(view)).click(),
DEFAULT_TIMEOUT.XS,
);
await new EditorView().closeAllEditors();
});
@ -51,13 +51,30 @@ describe("Cmd+L Shortcut Test", () => {
await editor.setText(text);
await KeyboardShortcutsActions.executeFocusContinueInput(driver);
await GUIActions.executeFocusContinueInputShortcut(driver);
({ view } = await GUIActions.switchToReactIframe());
await TestUtils.expectNoElement(async () => {
return GUISelectors.getInputBoxCodeBlockAtIndex(view, 0);
}, DEFAULT_TIMEOUT.XS);
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());
const textInput = await TestUtils.waitForSuccess(() =>
GUISelectors.getMessageInputFieldAtIndex(view, 0),
);
const activeElement: WebElement = await driver.switchTo().activeElement();
const textInputHtml = await textInput.getAttribute("outerHTML");
const activeElementHtml = await activeElement.getAttribute("outerHTML");
expect(textInputHtml).to.equal(activeElementHtml);
expect(await textInput.isDisplayed()).to.equal(true);
await GUIActions.executeFocusContinueInputShortcut(driver);
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 () => {
@ -66,7 +83,7 @@ describe("Cmd+L Shortcut Test", () => {
await editor.setText(text);
await editor.selectText(text);
await KeyboardShortcutsActions.executeFocusContinueInput(driver);
await GUIActions.executeFocusContinueInputShortcut(driver);
({ view } = await GUIActions.switchToReactIframe());
@ -78,5 +95,7 @@ describe("Cmd+L Shortcut Test", () => {
);
expect(codeblockContent).to.equal(text);
await GUIActions.executeFocusContinueInputShortcut(driver);
}).timeout(DEFAULT_TIMEOUT.XL);
});

View file

@ -432,6 +432,12 @@ const getCommandsMap: (
core.invoke("context/indexDocs", { reIndex: true });
},
"continue.focusContinueInput": async () => {
const isContinueInputFocused = await sidebar.webviewProtocol.request(
"isContinueInputFocused",
undefined,
false,
);
// This is a temporary fix—sidebar.webviewProtocol.request is blocking
// when the GUI hasn't yet been setup and we should instead be
// immediately throwing an error, or returning a Result object
@ -448,11 +454,6 @@ const getCommandsMap: (
undefined,
false,
);
const isContinueInputFocused = await sidebar.webviewProtocol.request(
"isContinueInputFocused",
undefined,
false,
);
if (isContinueInputFocused) {
if (historyLength === 0) {
@ -475,19 +476,23 @@ const getCommandsMap: (
}
},
"continue.focusContinueInputWithoutClear": async () => {
// This is a temporary fix—sidebar.webviewProtocol.request is blocking
// when the GUI hasn't yet been setup and we should instead be
// immediately throwing an error, or returning a Result object
if (!sidebar.isReady) {
focusGUI();
return;
}
const isContinueInputFocused = await sidebar.webviewProtocol.request(
"isContinueInputFocused",
undefined,
false,
);
// This is a temporary fix—sidebar.webviewProtocol.request is blocking
// when the GUI hasn't yet been setup and we should instead be
// immediately throwing an error, or returning a Result object
focusGUI();
if (!sidebar.isReady) {
const isReady = await waitForSidebarReady(sidebar, 5000, 100);
if (!isReady) {
return;
}
}
if (isContinueInputFocused) {
hideGUI();
} else {
@ -721,7 +726,10 @@ const getCommandsMap: (
"continue.toggleFullScreen": async () => {
focusGUI();
const sessionId = await sidebar.webviewProtocol.request("getCurrentSessionId", undefined);
const sessionId = await sidebar.webviewProtocol.request(
"getCurrentSessionId",
undefined,
);
// Check if full screen is already open by checking open tabs
const fullScreenTab = getFullScreenTab();
@ -755,11 +763,14 @@ const getCommandsMap: (
undefined,
true,
);
panel.onDidChangeViewState(() => {
vscode.commands.executeCommand("continue.newSession");
if(sessionId){
vscode.commands.executeCommand("continue.focusContinueSessionId", sessionId);
if (sessionId) {
vscode.commands.executeCommand(
"continue.focusContinueSessionId",
sessionId,
);
}
});