|
| 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 = { |
| 24 | + "id": "getEditorState", |
| 25 | + } |
| 26 | + res = rpc_get(command) |
| 27 | + if use_fallback(res): |
| 28 | + return actions.next() |
| 29 | + return res |
| 30 | + |
| 31 | + def cursorless_everywhere_set_selections( |
| 32 | + selections: list[SelectionOffsets], # pyright: ignore [reportGeneralTypeIssues] |
| 33 | + ): |
| 34 | + command = { |
| 35 | + "id": "setSelections", |
| 36 | + "selections": get_serializable_selections(selections), |
| 37 | + } |
| 38 | + res = rpc_get(command) |
| 39 | + if use_fallback(res): |
| 40 | + actions.next(selections) |
| 41 | + |
| 42 | + def cursorless_everywhere_edit_text( |
| 43 | + edit: EditorEdit, # pyright: ignore [reportGeneralTypeIssues] |
| 44 | + ): |
| 45 | + command = { |
| 46 | + "id": "setText", |
| 47 | + "text": edit["text"], |
| 48 | + } |
| 49 | + res = rpc_get(command) |
| 50 | + if use_fallback(res): |
| 51 | + actions.next(edit) |
| 52 | + |
| 53 | + |
| 54 | +def rpc_get(command: dict): |
| 55 | + return actions.user.run_rpc_command_get(RPC_COMMAND, command) |
| 56 | + |
| 57 | + |
| 58 | +def use_fallback(result: dict) -> bool: |
| 59 | + return result.get("fallback", False) |
| 60 | + |
| 61 | + |
| 62 | +# What is passed from cursorless everywhere js is a javascript object, which is not serializable for python. |
| 63 | +def get_serializable_selections(selections: list[SelectionOffsets]): |
| 64 | + result: list[SelectionOffsets] = [] |
| 65 | + for i in range(selections.length): # pyright: ignore [reportAttributeAccessIssue] |
| 66 | + selection = selections[i] |
| 67 | + result.append( |
| 68 | + { |
| 69 | + "anchor": selection["anchor"], |
| 70 | + "active": selection["active"], |
| 71 | + } |
| 72 | + ) |
| 73 | + return result |
0 commit comments