Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
2 changes: 2 additions & 0 deletions cursorless-everywhere-talon/cursorless_everywhere_talon.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
tag: user.cursorless_everywhere_talon
"""

ctx.tags = ["user.cursorless"]


@ctx.action_class("user")
class UserActions:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from talon import Context, Module, actions

from .cursorless_everywhere_types import EditorEdit, EditorState, SelectionOffsets

mod = Module()

mod.tag(
"cursorless_everywhere_talon_browser",
desc="Enable RPC to browser extension when using cursorless everywhere in Talon",
)

ctx = Context()
ctx.matches = r"""
tag: user.cursorless_everywhere_talon_browser
"""

RPC_COMMAND = "talonCommand"


@ctx.action_class("user")
class Actions:
def cursorless_everywhere_get_editor_state() -> EditorState:
command = {
"id": "getEditorState",
}
res = rpc_get(command)
if use_fallback(res):
return actions.next()
return res

def cursorless_everywhere_set_selections(
selections: list[SelectionOffsets], # pyright: ignore [reportGeneralTypeIssues]
):
command = {
"id": "setSelections",
"selections": get_serializable_selections(selections),
}
res = rpc_get(command)
if use_fallback(res):
actions.next(selections)

def cursorless_everywhere_edit_text(
edit: EditorEdit, # pyright: ignore [reportGeneralTypeIssues]
):
command = {
"id": "setText",
"text": edit["text"],
}
res = rpc_get(command)
if use_fallback(res):
actions.next(edit)


def rpc_get(command: dict):
return actions.user.run_rpc_command_get(RPC_COMMAND, command)


def use_fallback(result: dict) -> bool:
return result.get("fallback", False)


# What is passed from cursorless everywhere js is a javascript object, which is not serializable for python.
def get_serializable_selections(selections: list[SelectionOffsets]):
result: list[SelectionOffsets] = []
for i in range(selections.length): # pyright: ignore [reportAttributeAccessIssue]
selection = selections[i]
result.append(
{
"anchor": selection["anchor"],
"active": selection["active"],
}
)
return result
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Actions:
def cursorless_everywhere_get_editor_state() -> EditorState:
el = ui.focused_element()

print(el.patterns)
print(el)

if "Text2" not in el.patterns:
raise ValueError("Focused element is not a text element")

Expand Down
Loading