From a1bb6e48d195c22d445e8d8ad2b6671991cc97c4 Mon Sep 17 00:00:00 2001 From: Pokey Rule <755842+pokey@users.noreply.github.com> Date: Thu, 25 Apr 2024 19:09:19 +0100 Subject: [PATCH] Remove vscode version checks (#2305) Fixes #2274 ## Checklist - [-] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [-] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [-] I have not broken the cheatsheet --- packages/cursorless-vscode/package.json | 28 +------- .../src/ide/vscode/VscodeFocusEditor.ts | 28 ++------ .../src/ide/vscode/VscodeTextEditorImpl.ts | 2 +- .../src/ide/vscode/notebook/notebook.ts | 28 ++++---- .../src/ide/vscode/notebook/notebookLegacy.ts | 66 ------------------- .../src/getCursorlessVscodeFields.ts | 15 +---- 6 files changed, 24 insertions(+), 143 deletions(-) delete mode 100644 packages/cursorless-vscode/src/ide/vscode/notebook/notebookLegacy.ts diff --git a/packages/cursorless-vscode/package.json b/packages/cursorless-vscode/package.json index 8d04213065..1170d0823d 100644 --- a/packages/cursorless-vscode/package.json +++ b/packages/cursorless-vscode/package.json @@ -45,33 +45,7 @@ "pokey.parse-tree" ], "activationEvents": [ - "onLanguage", - "onView:cursorless.scopes", - "onCommand:cursorless.command", - "onCommand:cursorless.internal.updateCheatsheetDefaults", - "onCommand:cursorless.private.logQuickActions", - "onCommand:cursorless.keyboard.escape", - "onCommand:cursorless.keyboard.modal.modeOff", - "onCommand:cursorless.keyboard.modal.modeOn", - "onCommand:cursorless.keyboard.modal.modeToggle", - "onCommand:cursorless.keyboard.targeted.clearTarget", - "onCommand:cursorless.keyboard.targeted.runActionOnTarget", - "onCommand:cursorless.keyboard.targeted.targetHat", - "onCommand:cursorless.keyboard.targeted.targetScope", - "onCommand:cursorless.keyboard.targeted.targetSelection", - "onCommand:cursorless.pauseRecording", - "onCommand:cursorless.recomputeDecorationStyles", - "onCommand:cursorless.recordTestCase", - "onCommand:cursorless.recordOneTestCaseThenPause", - "onCommand:cursorless.resumeRecording", - "onCommand:cursorless.showCheatsheet", - "onCommand:cursorless.showDocumentation", - "onCommand:cursorless.showQuickPick", - "onCommand:cursorless.takeSnapshot", - "onCommand:cursorless.toggleDecorations", - "onCommand:cursorless.showScopeVisualizer", - "onCommand:cursorless.hideScopeVisualizer", - "onCommand:cursorless.analyzeCommandHistory" + "onLanguage" ], "main": "./extension.cjs", "capabilities": { diff --git a/packages/cursorless-vscode/src/ide/vscode/VscodeFocusEditor.ts b/packages/cursorless-vscode/src/ide/vscode/VscodeFocusEditor.ts index aaec544f92..4d159fc2d1 100644 --- a/packages/cursorless-vscode/src/ide/vscode/VscodeFocusEditor.ts +++ b/packages/cursorless-vscode/src/ide/vscode/VscodeFocusEditor.ts @@ -1,17 +1,12 @@ -import * as semver from "semver"; +import { getCellIndex } from "@cursorless/vscode-common"; import { commands, NotebookDocument, TextEditor, - version, ViewColumn, window, } from "vscode"; -import { getCellIndex } from "@cursorless/vscode-common"; import { getNotebookFromCellDocument } from "./notebook/notebook"; -import { focusNotebookCellLegacy } from "./notebook/notebookLegacy"; -import { isVscodeLegacyNotebookVersion } from "./notebook/notebook"; -import type { VscodeIDE } from "./VscodeIDE"; import { VscodeTextEditorImpl } from "./VscodeTextEditorImpl"; const columnFocusCommands = { @@ -28,10 +23,7 @@ const columnFocusCommands = { [ViewColumn.Beside]: "", }; -export default async function vscodeFocusEditor( - ide: VscodeIDE, - editor: VscodeTextEditorImpl, -) { +export default async function vscodeFocusEditor(editor: VscodeTextEditorImpl) { // Focusing the search editor brings focus back to the input field. // FIXME: This is a hack. There is no way to focus the search editor. If we // could figure out if the editor was not focused, we could issue @@ -48,10 +40,6 @@ export default async function vscodeFocusEditor( // If the view column is null we see if it's a notebook and try to see if we // can just move around in the notebook to focus the correct editor - if (isVscodeLegacyNotebookVersion()) { - return await focusNotebookCellLegacy(ide, editor); - } - await focusNotebookCell(editor); } } @@ -60,14 +48,12 @@ function getViewColumn(editor: TextEditor): ViewColumn | undefined { if (editor.viewColumn != null) { return editor.viewColumn; } - // FIXME: tabGroups is not available on older versions of vscode we still support. - // Remove any cast as soon as version is updated. - if (semver.lt(version, "1.67.0")) { - return undefined; - } const uri = editor.document.uri.toString(); - const tabGroup = (window as any)?.tabGroups?.all?.find((tabGroup: any) => - tabGroup?.tabs.find((tab: any) => tab?.input?.modified?.toString() === uri), + const tabGroup = window.tabGroups.all.find((tabGroup) => + tabGroup.tabs.find( + (tab: any) => + (tab?.input?.uri ?? tab?.input?.modified)?.toString() === uri, + ), ); return tabGroup?.viewColumn; } diff --git a/packages/cursorless-vscode/src/ide/vscode/VscodeTextEditorImpl.ts b/packages/cursorless-vscode/src/ide/vscode/VscodeTextEditorImpl.ts index ebd1158b80..464f5b3a53 100644 --- a/packages/cursorless-vscode/src/ide/vscode/VscodeTextEditorImpl.ts +++ b/packages/cursorless-vscode/src/ide/vscode/VscodeTextEditorImpl.ts @@ -89,7 +89,7 @@ export class VscodeTextEditorImpl implements EditableTextEditor { } public focus(): Promise { - return vscodeFocusEditor(this.ide, this); + return vscodeFocusEditor(this); } public editNewNotebookCellAbove(): Promise< diff --git a/packages/cursorless-vscode/src/ide/vscode/notebook/notebook.ts b/packages/cursorless-vscode/src/ide/vscode/notebook/notebook.ts index 8ebbb01664..107eafdcf0 100644 --- a/packages/cursorless-vscode/src/ide/vscode/notebook/notebook.ts +++ b/packages/cursorless-vscode/src/ide/vscode/notebook/notebook.ts @@ -1,12 +1,4 @@ -import * as semver from "semver"; -import { version } from "vscode"; -import { TextDocument } from "vscode"; -import { getNotebookFromCellDocumentLegacy } from "./notebookLegacy"; -import { getNotebookFromCellDocumentCurrent } from "./notebookCurrent"; - -export function isVscodeLegacyNotebookVersion() { - return semver.lt(version, "1.68.0"); -} +import { TextDocument, window } from "vscode"; /** * Given a document corresponding to a single cell, retrieve the notebook @@ -16,9 +8,17 @@ export function isVscodeLegacyNotebookVersion() { * given cell */ export function getNotebookFromCellDocument(document: TextDocument) { - if (isVscodeLegacyNotebookVersion()) { - return getNotebookFromCellDocumentLegacy(document); - } else { - return getNotebookFromCellDocumentCurrent(document); - } + const { notebookEditor } = + window.visibleNotebookEditors + .flatMap((notebookEditor) => + notebookEditor.notebook.getCells().map((cell) => ({ + notebookEditor, + cell, + })), + ) + .find( + ({ cell }) => cell.document.uri.toString() === document.uri.toString(), + ) ?? {}; + + return notebookEditor; } diff --git a/packages/cursorless-vscode/src/ide/vscode/notebook/notebookLegacy.ts b/packages/cursorless-vscode/src/ide/vscode/notebook/notebookLegacy.ts deleted file mode 100644 index a33115a911..0000000000 --- a/packages/cursorless-vscode/src/ide/vscode/notebook/notebookLegacy.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { getCellIndex } from "@cursorless/vscode-common"; -import { range } from "lodash"; -import { commands, NotebookDocument, TextDocument } from "vscode"; -import { toVscodeEditor } from "../toVscodeEditor"; -import type { VscodeIDE } from "../VscodeIDE"; -import type { VscodeTextEditorImpl } from "../VscodeTextEditorImpl"; - -export async function focusNotebookCellLegacy( - ide: VscodeIDE, - editor: VscodeTextEditorImpl, -) { - const { activeTextEditor } = ide; - - if (activeTextEditor == null) { - return; - } - - const vscodeActiveEditor = toVscodeEditor(activeTextEditor); - - const editorNotebook = getNotebookFromCellDocumentLegacy( - editor.vscodeEditor.document, - ); - const activeEditorNotebook = getNotebookFromCellDocumentLegacy( - vscodeActiveEditor.document, - ); - - if ( - editorNotebook == null || - activeEditorNotebook == null || - editorNotebook !== activeEditorNotebook - ) { - return; - } - - const editorIndex = getCellIndex( - editorNotebook, - editor.vscodeEditor.document, - ); - const activeEditorIndex = getCellIndex( - editorNotebook, - vscodeActiveEditor.document, - ); - - if (editorIndex === -1 || activeEditorIndex === -1) { - throw new Error( - "Couldn't find editor corresponding to given cell in the expected notebook", - ); - } - - const cellOffset = editorIndex - activeEditorIndex; - - const command = - cellOffset < 0 - ? "notebook.focusPreviousEditor" - : "notebook.focusNextEditor"; - - // This is a hack. We just repeatedly issued the command to move upwards or - // downwards a cell to get to the right cell - for (const _ of range(Math.abs(cellOffset))) { - await commands.executeCommand(command); - } -} - -export function getNotebookFromCellDocumentLegacy(document: TextDocument) { - return (document as any).notebook as NotebookDocument | undefined; -} diff --git a/packages/meta-updater/src/getCursorlessVscodeFields.ts b/packages/meta-updater/src/getCursorlessVscodeFields.ts index d6a16d6f0a..4407991a42 100644 --- a/packages/meta-updater/src/getCursorlessVscodeFields.ts +++ b/packages/meta-updater/src/getCursorlessVscodeFields.ts @@ -1,8 +1,4 @@ -import { - CURSORLESS_SCOPE_TREE_VIEW_ID, - cursorlessCommandDescriptions, - cursorlessCommandIds, -} from "@cursorless/common"; +import { cursorlessCommandDescriptions } from "@cursorless/common"; import { PackageJson } from "type-fest"; export function getCursorlessVscodeFields(input: PackageJson) { @@ -21,15 +17,6 @@ export function getCursorlessVscodeFields(input: PackageJson) { activationEvents: [ // Causes extension to activate whenever any text editor is opened "onLanguage", - - // Causes extension to activate when the Cursorless scope support side bar - // is opened - `onView:${CURSORLESS_SCOPE_TREE_VIEW_ID}`, - - // Causes extension to activate when any Cursorless command is run. - // Technically we don't need to do this since VSCode 1.74.0, but we support - // older versions - ...cursorlessCommandIds.map((id) => `onCommand:${id}`), ], }; }