-
-
Notifications
You must be signed in to change notification settings - Fork 91
Added support for browser rpc #2704
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eff6110
Added support for browser rpc
AndreasArvidsson 86ff8de
Enable Cursorless tag when Cursorless everywhere is enabled
AndreasArvidsson ea4f79a
Use fall back
AndreasArvidsson 1a64801
Clean up
AndreasArvidsson fc80408
Use get editor state command
AndreasArvidsson 6546bf4
Use id instead of type
AndreasArvidsson 10672ec
Add the type to module export
AndreasArvidsson 40a71a9
Remove debug prints
AndreasArvidsson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
cursorless-everywhere-talon/cursorless_everywhere_talon_browser.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.