Skip to content

Commit

Permalink
✨ Add select command
Browse files Browse the repository at this point in the history
command for selecting the current node
  • Loading branch information
sansarip committed Aug 20, 2024
1 parent 840a500 commit 8f2b476
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added docs/assets/examples/tsx-select-20240819-00.mp4
Binary file not shown.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
38 changes: 28 additions & 10 deletions src/ts/extension/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ExtensionContext,
window,
workspace,
TextEditor,
} from "vscode";
import { EditCtx, OwlbearFunction } from "./types";
import {
Expand All @@ -13,6 +14,7 @@ import {
copyRangeToClipboard,
cutRangeToClipboard,
moveCursor,
selectRange,
} from "./utilities";
import { docUriToTreeIdMap, setNewTreeIdForDocUri } from "./tree";
import {
Expand Down Expand Up @@ -46,9 +48,10 @@ type OwlbearOperation =
| "Splice"
| "UpwardMove";

enum ClipboardOp {
enum SelectionOp {
copy = "Copy",
cut = "Cut",
select = "Select",
}

const getOwlbearFunction = (
Expand Down Expand Up @@ -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> | 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;
};

Expand Down Expand Up @@ -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;
Expand All @@ -210,6 +223,10 @@ const cut: Handler = async () => {
return ctx;
};

const select: Handler = async () => {
return doSelectionOp(SelectionOp.select);
}

const raise: Handler = () => {
return doEditOp("Raise");
};
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit 8f2b476

Please sign in to comment.