Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from talon import Context, Module, actions

from .cursorless_everywhere_types import EditorEdit, EditorState, SelectionOffsets
from .cursorless_everywhere_types import (
EditorChange,
EditorEdit,
EditorState,
SelectionOffsets,
)

mod = Module()

Expand Down Expand Up @@ -43,8 +48,9 @@ def cursorless_everywhere_edit_text(
edit: EditorEdit, # pyright: ignore [reportGeneralTypeIssues]
):
command = {
"id": "setText",
"id": "editText",
"text": edit["text"],
"changes": get_serializable_editor_changes(edit["changes"]),
}
res = rpc_get(command)
if use_fallback(res):
Expand All @@ -71,3 +77,17 @@ def get_serializable_selections(selections: list[SelectionOffsets]):
}
)
return result


def get_serializable_editor_changes(changes: list[EditorChange]):
result: list[EditorChange] = []
for i in range(changes.length): # pyright: ignore [reportAttributeAccessIssue]
change = changes[i]
result.append(
{
"text": change["text"],
"rangeOffset": change["rangeOffset"],
"rangeLength": change["rangeLength"],
}
)
return result
Comment on lines +84 to +93
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use a list comprehension here because changes it is not a true Python list, it's a QuickJS array which doesn't support that. We also don't have the ability to convert these JavaScript objects to Python with a one shot utility because certain reflection data is missing per Andreas.

But what we could do is make simple wrapping utilities that will convert one level of the tree:

  • JavaScript array to Python list (just does a iteration like this without looking at the objects in the array)
  • JavaScript dictionary to Python dictionary (you would have to pass exactly the keys you expect)

With this approach, we can contain the weirdness of the JavaScript objects to where we get them, requiring a couple of utility calls, and use proper Python code thereafter.

Loading