Skip to content

Commit bcf5023

Browse files
Added text insertion action to Cursorless public api (#1875)
Fixes #1754 ## 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) - [/] I have not broken the cheatsheet --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent e4c0c5b commit bcf5023

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/actions/actions.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
from typing import Union
2+
13
from talon import Module, actions
24

3-
from ..targets.target_types import CursorlessTarget, ImplicitDestination
5+
from ..targets.target_types import (
6+
CursorlessDestination,
7+
CursorlessTarget,
8+
ImplicitDestination,
9+
)
410
from .bring_move import BringMoveTargets
511
from .call import cursorless_call_action
612
from .execute_command import cursorless_execute_command_action
713
from .homophones import cursorless_homophones_action
14+
from .replace import cursorless_replace_action
815

916
mod = Module()
1017

@@ -111,6 +118,14 @@ def cursorless_ide_command(command_id: str, target: CursorlessTarget):
111118
"""Perform ide command on cursorless target"""
112119
return cursorless_execute_command_action(command_id, target)
113120

121+
def cursorless_insert(
122+
destination: CursorlessDestination, text: Union[str, list[str]]
123+
):
124+
"""Perform text insertion on Cursorless destination"""
125+
if isinstance(text, str):
126+
text = [text]
127+
cursorless_replace_action(destination, text)
128+
114129
def private_cursorless_action_or_ide_command(
115130
instruction: dict, target: CursorlessTarget
116131
):

src/actions/homophones.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from talon import actions, app
22

3+
from ..targets.target_types import PrimitiveDestination
34
from .get_text import cursorless_get_text_action
45
from .replace import cursorless_replace_action
56

@@ -12,7 +13,8 @@ def cursorless_homophones_action(target: dict):
1213
except LookupError as e:
1314
app.notify(str(e))
1415
return
15-
cursorless_replace_action(target, updated_texts)
16+
destination = PrimitiveDestination("to", target)
17+
cursorless_replace_action(destination, updated_texts)
1618

1719

1820
def get_next_homophone(word: str):

src/actions/reformat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from talon import Module, actions
22

3-
from ..targets.target_types import CursorlessTarget
3+
from ..targets.target_types import CursorlessTarget, PrimitiveDestination
44
from .get_text import cursorless_get_text_action
55
from .replace import cursorless_replace_action
66

@@ -15,4 +15,5 @@ def private_cursorless_reformat(target: CursorlessTarget, formatters: str):
1515
"""Execute Cursorless reformat action. Reformat target with formatter"""
1616
texts = cursorless_get_text_action(target, show_decorations=False)
1717
updated_texts = [actions.user.reformat_text(text, formatters) for text in texts]
18-
cursorless_replace_action(target, updated_texts)
18+
destination = PrimitiveDestination("to", target)
19+
cursorless_replace_action(destination, updated_texts)

src/actions/replace.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
from talon import actions
22

3-
from ..targets.target_types import CursorlessTarget, PrimitiveDestination
3+
from ..targets.target_types import CursorlessDestination
44

55

6-
def cursorless_replace_action(target: CursorlessTarget, replace_with: list[str]):
6+
def cursorless_replace_action(
7+
destination: CursorlessDestination, replace_with: list[str]
8+
):
79
"""Execute Cursorless replace action. Replace targets with texts"""
810
actions.user.private_cursorless_command_and_wait(
911
{
1012
"name": "replace",
1113
"replaceWith": replace_with,
12-
"destination": PrimitiveDestination("to", target),
14+
"destination": destination,
1315
}
1416
)

0 commit comments

Comments
 (0)