Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public get text action #2069

Merged
merged 29 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4d1b83d
Made public get text action
AndreasArvidsson Nov 29, 2023
16e26f5
Added spoken form tests
AndreasArvidsson Dec 1, 2023
7436c88
clean up
AndreasArvidsson Dec 1, 2023
84baff0
Split get text action into two separate actions
AndreasArvidsson Dec 2, 2023
d166bd4
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] Dec 2, 2023
43e5f05
Remove unused import
AndreasArvidsson Dec 2, 2023
48c73a6
Merge branch 'get_text_action' of github.com:cursorless-dev/cursorles…
AndreasArvidsson Dec 2, 2023
8da2961
Update docs/user/customization.md
AndreasArvidsson Dec 4, 2023
24a4612
invert boolean argument
AndreasArvidsson Dec 5, 2023
46d2f48
Fixed
AndreasArvidsson Dec 5, 2023
ab8b436
Update
AndreasArvidsson Dec 5, 2023
a690090
Flip boolean
AndreasArvidsson Dec 5, 2023
b717b32
Update fixtures
AndreasArvidsson Dec 5, 2023
99d7964
Update phrasing
AndreasArvidsson Dec 5, 2023
a4bb31f
work work
AndreasArvidsson Dec 7, 2023
216b7f9
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] Dec 7, 2023
c518b9b
Merge branch 'main' into get_text_action
pokey Dec 18, 2023
4556f10
Tweaks
pokey Dec 18, 2023
5d3d7f2
fixes
pokey Dec 18, 2023
1953bb1
fix
pokey Dec 18, 2023
10252a4
Fix talon tests
pokey Dec 18, 2023
3887fbb
docs
pokey Dec 18, 2023
cf8f1b4
fixes
pokey Dec 19, 2023
e26c816
Update docs
pokey Dec 19, 2023
2f7985c
Update doc string#
pokey Dec 19, 2023
258570b
Fixes
pokey Dec 19, 2023
fcdf675
Merge branch 'main' into get_text_action
pokey Dec 19, 2023
4900e1a
Merge branch 'main' into get_text_action
pokey Dec 19, 2023
c7d20de
fix
pokey Dec 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cursorless-talon-dev/src/cursorless_test.talon
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ test api command <user.cursorless_target>:
user.cursorless_command("setSelection", cursorless_target)
test api command bring <user.cursorless_target>:
user.cursorless_command("replaceWithTarget", cursorless_target)
test api get text <user.cursorless_target>:
user.cursorless_get_text(cursorless_target)
test api get text list <user.cursorless_target>:
user.cursorless_get_text_list(cursorless_target)

test api insert <user.word> <user.cursorless_destination>:
user.cursorless_insert(cursorless_destination, word)
test api insert <user.word> and <user.word> <user.cursorless_destination>:
user.cursorless_insert(cursorless_destination, word_list)

test api insert snippet:
user.cursorless_insert_snippet("Hello, $foo! My name is $bar!")
test api insert snippet <user.cursorless_destination> :
Expand Down
46 changes: 35 additions & 11 deletions cursorless-talon/src/actions/get_text.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
from typing import Optional

from talon import actions
from talon import Module, actions

from ..targets.target_types import CursorlessTarget

mod = Module()


@mod.action_class
class Actions:
def cursorless_get_text(
target: CursorlessTarget,
hide_decorations: bool = False,
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
) -> str:
"""Get target text"""
return cursorless_get_text(
target,
hide_decorations=hide_decorations,
ensure_single_target=True,
)[0]

def cursorless_get_text_action(
def cursorless_get_text_list(
target: CursorlessTarget,
hide_decorations: bool = False,
) -> list[str]:
"""Get texts for multiple targets"""
return cursorless_get_text(
target,
hide_decorations=hide_decorations,
ensure_single_target=False,
)


def cursorless_get_text(
target: CursorlessTarget,
*,
show_decorations: Optional[bool] = None,
ensure_single_target: Optional[bool] = None,
hide_decorations: bool,
ensure_single_target: bool,
) -> list[str]:
"""Get target texts"""
options: dict[str, bool] = {}

if show_decorations is not None:
options["showDecorations"] = show_decorations

if ensure_single_target is not None:
options["ensureSingleTarget"] = ensure_single_target
if hide_decorations:
options["showDecorations"] = False
if ensure_single_target:
options["ensureSingleTarget"] = True

return actions.user.private_cursorless_command_get(
{
Expand Down
3 changes: 1 addition & 2 deletions cursorless-talon/src/actions/homophones.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
from talon import actions, app

from ..targets.target_types import CursorlessTarget, PrimitiveDestination
from .get_text import cursorless_get_text_action
from .replace import cursorless_replace_action


def cursorless_homophones_action(target: CursorlessTarget):
"""Replaced target with next homophone"""
texts = cursorless_get_text_action(target, show_decorations=False)
texts = actions.user.cursorless_get_text_list(target, True)
try:
updated_texts = list(map(get_next_homophone, texts))
except LookupError as e:
Expand Down
3 changes: 1 addition & 2 deletions cursorless-talon/src/actions/reformat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from talon import Module, actions

from ..targets.target_types import CursorlessTarget, PrimitiveDestination
from .get_text import cursorless_get_text_action
from .replace import cursorless_replace_action

mod = Module()
Expand All @@ -13,7 +12,7 @@
class Actions:
def private_cursorless_reformat(target: CursorlessTarget, formatters: str):
"""Execute Cursorless reformat action. Reformat target with formatter"""
texts = cursorless_get_text_action(target, show_decorations=False)
texts = actions.user.cursorless_get_text_list(target, True)
updated_texts = [actions.user.reformat_text(text, formatters) for text in texts]
destination = PrimitiveDestination("to", target)
cursorless_replace_action(destination, updated_texts)
4 changes: 1 addition & 3 deletions cursorless-talon/src/apps/cursorless_vscode.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from talon import Context, actions, app

from ..actions.get_text import cursorless_get_text_action
from ..targets.target_types import CursorlessTarget

ctx = Context()
Expand All @@ -16,8 +15,7 @@
class Actions:
def private_cursorless_find(target: CursorlessTarget):
"""Find text of target in editor"""
texts = cursorless_get_text_action(target, ensure_single_target=True)
search_text = texts[0]
search_text = actions.user.cursorless_get_text(target)
if len(search_text) > 200:
search_text = search_text[:200]
app.notify("Search text is longer than 200 characters; truncating")
Expand Down
4 changes: 4 additions & 0 deletions docs/user/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ Cursorless exposes a couple talon actions and captures that you can use to defin
- `user.cursorless_ide_command(command_id: str, target: cursorless_target)`:
Performs a built-in IDE command on the given target
eg: `user.cursorless_ide_command("editor.action.addCommentLine", cursorless_target)`
- `user.cursorless_get_text(target: CursorlessTarget, hide_decorations: bool = False) -> str`
Get text from target
- `user.cursorless_get_text_list(target: CursorlessTarget, hide_decorations: bool = False) -> list[str]`
Get texts from multiple targets
- `user.cursorless_insert(destination: CursorlessDestination, text: Union[str, List[str]])`:
Insert text at destination.
eg: `user.cursorless_insert(cursorless_destination, "hello")`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ const parseTreeAction: ActionDescriptor = {
name: "private.showParseTree",
target: decoratedPrimitiveTarget("a"),
};
const getTextAction: ActionDescriptor = {
name: "getText",
options: {
ensureSingleTarget: true,
},
target: decoratedPrimitiveTarget("a"),
};
const getTextListAction: ActionDescriptor = {
name: "getText",
options: {},
target: decoratedPrimitiveTarget("a"),
};

/**
* These test our Talon api using dummy spoken forms defined in
Expand All @@ -121,6 +133,8 @@ export const talonApiFixture = [
wrapWithSnippetByNameAction,
),
spokenFormTest("parse tree air", parseTreeAction),
spokenFormTest("test api get text air", getTextAction),
spokenFormTest("test api get text list air", getTextListAction),
];

function decoratedPrimitiveTarget(
Expand Down