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

Added support for browser rpc #2704

Merged
merged 8 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Loading