diff --git a/CHANGELOG.md b/CHANGELOG.md index d09294c..ae2a4d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to the Owlbear extension will be documented in this file. +## [Unreleased] + +### Added + +- Add command for selecting the current node + ## [1.1.7] - 2024-08-19 ### Added diff --git a/docs/assets/examples/tsx-select-20240819-00.mp4 b/docs/assets/examples/tsx-select-20240819-00.mp4 new file mode 100644 index 0000000..a4367b9 Binary files /dev/null and b/docs/assets/examples/tsx-select-20240819-00.mp4 differ diff --git a/package.json b/package.json index 886c627..7453c44 100644 --- a/package.json +++ b/package.json @@ -106,6 +106,11 @@ "title": "Owlbear: Raise", "enablement": "editorLangId in owlbear:supportedLanguages" }, + { + "command": "owlbear.select", + "title": "Owlbear: Select Current Node", + "enablement": "editorLangId in owlbear:supportedLanguages" + }, { "command": "owlbear.splice", "title": "Owlbear: Splice", @@ -193,6 +198,11 @@ "key": "ctrl+alt+p ctrl+alt+r", "when": "!editorReadOnly && !editorHasMultipleSelections && editorTextFocus && editorLangId in owlbear:supportedLanguages" }, + { + "command": "owlbear.select", + "key": "ctrl+alt+c ctrl+s", + "when": "!editorReadOnly && !editorHasMultipleSelections && editorTextFocus && editorLangId in owlbear:supportedLanguages" + }, { "command": "owlbear.splice", "key": "ctrl+alt+s", diff --git a/src/ts/extension/commands.ts b/src/ts/extension/commands.ts index 4c45647..8b80cde 100644 --- a/src/ts/extension/commands.ts +++ b/src/ts/extension/commands.ts @@ -4,6 +4,7 @@ import { ExtensionContext, window, workspace, + TextEditor, } from "vscode"; import { EditCtx, OwlbearFunction } from "./types"; import { @@ -13,6 +14,7 @@ import { copyRangeToClipboard, cutRangeToClipboard, moveCursor, + selectRange, } from "./utilities"; import { docUriToTreeIdMap, setNewTreeIdForDocUri } from "./tree"; import { @@ -46,9 +48,10 @@ type OwlbearOperation = | "Splice" | "UpwardMove"; -enum ClipboardOp { +enum SelectionOp { copy = "Copy", cut = "Cut", + select = "Select", } const getOwlbearFunction = ( @@ -106,18 +109,28 @@ const doEditOp: Edit = (obOp: OwlbearOperation) => { return edit(editor, editCtx, shouldFormat); }; -const doClipboardOp = async (op: ClipboardOp) => { +const doSelectionOp = async (op: SelectionOp) => { const ctx = getEditCtx("Kill"); - const removedText = ctx?.removedText; + const text = ctx?.removedText; const editor = window.activeTextEditor; - if (!removedText || !editor) { + if (!text || !editor) { return; } const startIndex = ctx.offset; - const endIndex = startIndex + removedText.length; - const clipboardOpFn = - op === ClipboardOp.copy ? copyRangeToClipboard : cutRangeToClipboard; - await clipboardOpFn(editor, startIndex, endIndex); + const endIndex = startIndex + text.length; + let selectOpFn: (editor: TextEditor, startIndex: number, endIndex: number) => Promise | void; + switch (op) { + case SelectionOp.copy: + selectOpFn = copyRangeToClipboard; + break; + case SelectionOp.cut: + selectOpFn = cutRangeToClipboard; + break; + default: + selectOpFn = selectRange; + break; + } + await selectOpFn(editor, startIndex, endIndex); return ctx; }; @@ -196,11 +209,11 @@ const forwardMove: Handler = () => doEditOp("ForwardMove"); const kill: Handler = () => doEditOp("Kill"); const copy: Handler = async () => { - return doClipboardOp(ClipboardOp.copy); + return doSelectionOp(SelectionOp.copy); }; const cut: Handler = async () => { - const ctx = await doClipboardOp(ClipboardOp.cut); + const ctx = await doSelectionOp(SelectionOp.cut); const editor = window.activeTextEditor; if (!ctx?.removedText || !editor) { return; @@ -210,6 +223,10 @@ const cut: Handler = async () => { return ctx; }; +const select: Handler = async () => { + return doSelectionOp(SelectionOp.select); +} + const raise: Handler = () => { return doEditOp("Raise"); }; @@ -253,6 +270,7 @@ const commands: Command[] = [ { id: "owlbear.forwardSlurp", handler: forwardSlurp }, { id: "owlbear.kill", handler: kill }, { id: "owlbear.raise", handler: raise }, + { id: "owlbear.select", handler: select }, { id: "owlbear.splice", handler: splice }, { id: "owlbear.toggleAutoformat", handler: toggleAutoformat }, { id: "owlbear.toggleParedit", handler: toggleParedit },