Skip to content

Commit 7341d0f

Browse files
AndreasArvidssonpre-commit-ci-lite[bot]pokey
authored
Public get text action (#2069)
Fixes #452 ## Checklist - [x] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [x] 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) - [x] I have not broken the cheatsheet --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: Pokey Rule <[email protected]>
1 parent 297ec43 commit 7341d0f

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

cursorless-talon-dev/src/cursorless_test.talon

+10
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@ test api command <user.cursorless_target>:
77
user.cursorless_command("setSelection", cursorless_target)
88
test api command bring <user.cursorless_target>:
99
user.cursorless_command("replaceWithTarget", cursorless_target)
10+
test api get text <user.cursorless_target>:
11+
user.cursorless_get_text(cursorless_target)
12+
test api get text list on <user.cursorless_target>:
13+
user.cursorless_get_text_list(cursorless_target)
14+
test api get text hide decorations <user.cursorless_target>:
15+
user.cursorless_get_text(cursorless_target, true)
16+
test api get text hide decorations list on <user.cursorless_target>:
17+
user.cursorless_get_text_list(cursorless_target, true)
18+
1019
test api insert <user.word> <user.cursorless_destination>:
1120
user.cursorless_insert(cursorless_destination, word)
1221
test api insert <user.word> and <user.word> <user.cursorless_destination>:
1322
user.cursorless_insert(cursorless_destination, word_list)
23+
1424
test api insert snippet:
1525
user.cursorless_insert_snippet("Hello, $foo! My name is $bar!")
1626
test api insert snippet <user.cursorless_destination> :

cursorless-talon/src/actions/get_text.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
11
from typing import Optional
22

3-
from talon import actions
3+
from talon import Module, actions
44

55
from ..targets.target_types import CursorlessTarget
66

7+
mod = Module()
8+
9+
10+
@mod.action_class
11+
class Actions:
12+
def cursorless_get_text(
13+
target: CursorlessTarget,
14+
hide_decorations: bool = False,
15+
) -> str:
16+
"""Get target text. If hide_decorations is True, don't show decorations"""
17+
return cursorless_get_text_action(
18+
target,
19+
show_decorations=not hide_decorations,
20+
ensure_single_target=True,
21+
)[0]
22+
23+
def cursorless_get_text_list(
24+
target: CursorlessTarget,
25+
hide_decorations: bool = False,
26+
) -> list[str]:
27+
"""Get texts for multiple targets. If hide_decorations is True, don't show decorations"""
28+
return cursorless_get_text_action(
29+
target,
30+
show_decorations=not hide_decorations,
31+
ensure_single_target=False,
32+
)
33+
734

835
def cursorless_get_text_action(
936
target: CursorlessTarget,

docs/user/customization.md

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ Cursorless exposes a couple talon actions and captures that you can use to defin
117117
- `user.cursorless_ide_command(command_id: str, target: cursorless_target)`:
118118
Performs a built-in IDE command on the given target
119119
eg: `user.cursorless_ide_command("editor.action.addCommentLine", cursorless_target)`
120+
- `user.cursorless_get_text(target: CursorlessTarget, hide_decorations: bool = False) -> str`
121+
Get text from target. If `hide_decorations` is `true`, will not show decorations.
122+
- `user.cursorless_get_text_list(target: CursorlessTarget, hide_decorations: bool = False) -> list[str]`
123+
Get texts from multiple targets. If `hide_decorations` is `true`, will not show decorations.
120124
- `user.cursorless_insert(destination: CursorlessDestination, text: Union[str, List[str]])`:
121125
Insert text at destination.
122126
eg: `user.cursorless_insert(cursorless_destination, "hello")`

packages/cursorless-engine/src/test/fixtures/spokenFormTest.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ export interface SpokenFormTest {
2424
export function spokenFormTest(
2525
spokenForm: string,
2626
action: ActionDescriptor,
27+
mockedGetValue?: unknown,
2728
): SpokenFormTest {
2829
return {
2930
spokenForm,
30-
mockedGetValue: undefined,
31+
mockedGetValue,
3132
commands: [command(spokenForm, action)],
3233
};
3334
}

packages/cursorless-engine/src/test/fixtures/talonApi.fixture.ts

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
ActionDescriptor,
3+
GetTextActionOptions,
34
PartialPrimitiveTargetDescriptor,
45
} from "@cursorless/common";
56
import { spokenFormTest } from "./spokenFormTest";
@@ -135,6 +136,14 @@ const alternateHighlightNothingAction: ActionDescriptor = {
135136
highlightId: "highlight1",
136137
};
137138

139+
function getTextAction(options: GetTextActionOptions): ActionDescriptor {
140+
return {
141+
name: "getText",
142+
options,
143+
target: decoratedPrimitiveTarget("a"),
144+
};
145+
}
146+
138147
/**
139148
* These test our Talon api using dummy spoken forms defined in
140149
* cursorless-talon-dev/src/cursorless_test.talon
@@ -158,6 +167,26 @@ export const talonApiFixture = [
158167
"test api wrap with snippet by name this",
159168
wrapWithSnippetByNameAction,
160169
),
170+
spokenFormTest(
171+
"test api get text air",
172+
getTextAction({ showDecorations: true, ensureSingleTarget: true }),
173+
["apple"],
174+
),
175+
spokenFormTest(
176+
"test api get text list on air",
177+
getTextAction({ showDecorations: true, ensureSingleTarget: false }),
178+
["apple"],
179+
),
180+
spokenFormTest(
181+
"test api get text hide decorations air",
182+
getTextAction({ showDecorations: false, ensureSingleTarget: true }),
183+
["apple"],
184+
),
185+
spokenFormTest(
186+
"test api get text hide decorations list on air",
187+
getTextAction({ showDecorations: false, ensureSingleTarget: false }),
188+
["apple"],
189+
),
161190
spokenFormTest(
162191
"test api extract decorated marks air past bat",
163192
alternateHighlightAirAndBatAction,

0 commit comments

Comments
 (0)