From c5150468a2c70b630ba821e0e80b02d1a229ed18 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Tue, 2 Jul 2024 10:08:02 -0700 Subject: [PATCH] more keybindings w copilot tests (#2875) Signed-off-by: Jess Frazelle --- e2e/playwright/flow-tests.spec.ts | 100 ++++++++++++++++++++++++ src/editor/plugins/lsp/copilot/index.ts | 49 +++++++----- 2 files changed, 131 insertions(+), 18 deletions(-) diff --git a/e2e/playwright/flow-tests.spec.ts b/e2e/playwright/flow-tests.spec.ts index c13284124..cdcec7b2a 100644 --- a/e2e/playwright/flow-tests.spec.ts +++ b/e2e/playwright/flow-tests.spec.ts @@ -1527,6 +1527,106 @@ test.describe('Copilot ghost text', () => { ) }) + test('Ctrl+shift+z in code rejects the suggestion', async ({ page }) => { + const u = await getUtils(page) + // const PUR = 400 / 37.5 //pixeltoUnitRatio + await page.setViewportSize({ width: 1200, height: 500 }) + + await u.waitForAuthSkipAppStart() + const CtrlKey = process.platform === 'darwin' ? 'Meta' : 'Control' + + await u.codeLocator.click() + await expect(page.locator('.cm-content')).toHaveText(``) + + await expect(page.locator('.cm-ghostText')).not.toBeVisible() + await page.waitForTimeout(500) + await page.keyboard.press('Enter') + await expect(page.locator('.cm-ghostText').first()).toBeVisible() + await expect(page.locator('.cm-content')).toHaveText( + `fn cube = (pos, scale) => { const sg = startSketchOn('XY') |> startProfileAt(pos, %) |> line([0, scale], %) |> line([scale, 0], %) |> line([0, -scale], %) return sg}const part001 = cube([0,0], 20) |> close(%) |> extrude(20, %)` + ) + await expect(page.locator('.cm-ghostText').first()).toHaveText( + `fn cube = (pos, scale) => {` + ) + + // Going elsewhere in the code should hide the ghost text. + await page.keyboard.down(CtrlKey) + await page.keyboard.down('Shift') + await page.keyboard.press('KeyZ') + await page.keyboard.up(CtrlKey) + await page.keyboard.up('Shift') + await expect(page.locator('.cm-ghostText').first()).not.toBeVisible() + + await expect(page.locator('.cm-content')).toHaveText(``) + }) + + test('Ctrl+z in code rejects the suggestion and undos the last code', async ({ + page, + }) => { + const u = await getUtils(page) + // const PUR = 400 / 37.5 //pixeltoUnitRatio + await page.setViewportSize({ width: 1200, height: 500 }) + + await u.waitForAuthSkipAppStart() + + const CtrlKey = process.platform === 'darwin' ? 'Meta' : 'Control' + + await page.waitForTimeout(800) + await u.codeLocator.click() + await expect(page.locator('.cm-content')).toHaveText(``) + + await page.keyboard.type('{thing: "blah"}', { delay: 0 }) + + await expect(page.locator('.cm-content')).toHaveText(`{thing: "blah"}`) + + // We wanna make sure the code saves. + await page.waitForTimeout(800) + + // Ctrl+z + await page.keyboard.down(CtrlKey) + await page.keyboard.press('KeyZ') + await page.keyboard.up(CtrlKey) + + await expect(page.locator('.cm-content')).toHaveText(``) + + // Ctrl+shift+z + await page.keyboard.down(CtrlKey) + await page.keyboard.down('Shift') + await page.keyboard.press('KeyZ') + await page.keyboard.up(CtrlKey) + await page.keyboard.up('Shift') + + await expect(page.locator('.cm-content')).toHaveText(`{thing: "blah"}`) + + // We wanna make sure the code saves. + await page.waitForTimeout(800) + + await expect(page.locator('.cm-ghostText')).not.toBeVisible() + await page.waitForTimeout(500) + await page.keyboard.press('Enter') + await expect(page.locator('.cm-ghostText').first()).toBeVisible() + await expect(page.locator('.cm-content')).toHaveText( + `{thing: "blah"}fn cube = (pos, scale) => { const sg = startSketchOn('XY') |> startProfileAt(pos, %) |> line([0, scale], %) |> line([scale, 0], %) |> line([0, -scale], %) return sg}const part001 = cube([0,0], 20) |> close(%) |> extrude(20, %)` + ) + await expect(page.locator('.cm-ghostText').first()).toHaveText( + `fn cube = (pos, scale) => {` + ) + + // Once for the enter. + await page.keyboard.down(CtrlKey) + await page.keyboard.press('KeyZ') + await page.keyboard.up(CtrlKey) + + // Once for the text. + await page.keyboard.down(CtrlKey) + await page.keyboard.press('KeyZ') + await page.keyboard.up(CtrlKey) + + await expect(page.locator('.cm-ghostText').first()).not.toBeVisible() + + await expect(page.locator('.cm-content')).toHaveText(``) + }) + test('delete in code rejects the suggestion', async ({ page }) => { const u = await getUtils(page) // const PUR = 400 / 37.5 //pixeltoUnitRatio diff --git a/src/editor/plugins/lsp/copilot/index.ts b/src/editor/plugins/lsp/copilot/index.ts index efdcedcf8..444ed3a5f 100644 --- a/src/editor/plugins/lsp/copilot/index.ts +++ b/src/editor/plugins/lsp/copilot/index.ts @@ -624,6 +624,16 @@ export const copilotPlugin = (options: LanguageServerOptions): Extension => { }, }) + const rejectSuggestionCommand = (view: EditorView): boolean => { + if (view.plugin === null) return false + + // Get the current plugin from the map. + const p = view.plugin(completionPlugin) + if (p === null) return false + + return p.rejectSuggestionCommand() + } + const copilotAutocompleteKeymap: readonly KeyBinding[] = [ { key: 'Tab', @@ -639,27 +649,30 @@ export const copilotPlugin = (options: LanguageServerOptions): Extension => { }, { key: 'Backspace', - run: (view: EditorView): boolean => { - if (view.plugin === null) return false - - // Get the current plugin from the map. - const p = view.plugin(completionPlugin) - if (p === null) return false - - return p.rejectSuggestionCommand() - }, + run: rejectSuggestionCommand, }, { key: 'Delete', - run: (view: EditorView): boolean => { - if (view.plugin === null) return false - - // Get the current plugin from the map. - const p = view.plugin(completionPlugin) - if (p === null) return false - - return p.rejectSuggestionCommand() - }, + run: rejectSuggestionCommand, + }, + { key: 'Mod-z', run: rejectSuggestionCommand, preventDefault: true }, + { + key: 'Mod-y', + mac: 'Mod-Shift-z', + run: rejectSuggestionCommand, + preventDefault: true, + }, + { + linux: 'Ctrl-Shift-z', + run: rejectSuggestionCommand, + preventDefault: true, + }, + { key: 'Mod-u', run: rejectSuggestionCommand, preventDefault: true }, + { + key: 'Alt-u', + mac: 'Mod-Shift-u', + run: rejectSuggestionCommand, + preventDefault: true, }, ]