Skip to content

Commit eff6110

Browse files
Added support for browser rpc
1 parent 25a838f commit eff6110

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from talon import Context, Module, actions
2+
3+
from .cursorless_everywhere_types import EditorEdit, EditorState, SelectionOffsets
4+
5+
mod = Module()
6+
7+
mod.tag(
8+
"cursorless_everywhere_talon_browser",
9+
desc="Enable RPC to browser extension when using cursorless everywhere in Talon",
10+
)
11+
12+
ctx = Context()
13+
ctx.matches = r"""
14+
tag: user.cursorless_everywhere_talon_browser
15+
"""
16+
17+
RPC_COMMAND = "talonCommand"
18+
19+
20+
@ctx.action_class("user")
21+
class Actions:
22+
def cursorless_everywhere_get_editor_state() -> EditorState:
23+
command = {"type": "getActiveEditor"}
24+
return actions.user.run_rpc_command_get(RPC_COMMAND, command)
25+
26+
def cursorless_everywhere_set_selections(
27+
selections: list[SelectionOffsets], # pyright: ignore [reportGeneralTypeIssues]
28+
):
29+
command = {
30+
"type": "setSelections",
31+
"selections": get_serializable_selections(selections),
32+
}
33+
actions.user.run_rpc_command_and_wait(RPC_COMMAND, command)
34+
35+
def cursorless_everywhere_edit_text(
36+
edit: EditorEdit, # pyright: ignore [reportGeneralTypeIssues]
37+
):
38+
command = {"type": "setText", "text": edit["text"]}
39+
actions.user.run_rpc_command_and_wait(RPC_COMMAND, command)
40+
41+
42+
# What is passed from cursorless everywhere js is a javascript object, which is not serializable for python.
43+
def get_serializable_selections(selections: list[SelectionOffsets]):
44+
result: list[SelectionOffsets] = []
45+
for i in range(selections.length): # pyright: ignore [reportAttributeAccessIssue]
46+
selection = selections[i]
47+
result.append(
48+
{
49+
"anchor": selection["anchor"],
50+
"active": selection["active"],
51+
}
52+
)
53+
return result

0 commit comments

Comments
 (0)