Skip to content

Commit d8287e6

Browse files
Added utilities to convert javascript objects to python ones (#2718)
Fixes #2716 ## Checklist - [/] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [/] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [/] I have not broken the cheatsheet
1 parent 96dc78b commit d8287e6

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

cursorless-everywhere-talon/cursorless_everywhere_talon_browser.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from talon import Context, Module, actions
22

33
from .cursorless_everywhere_types import (
4-
EditorChange,
54
EditorEdit,
65
EditorState,
76
SelectionOffsets,
@@ -38,7 +37,10 @@ def cursorless_everywhere_set_selections(
3837
):
3938
command = {
4039
"id": "setSelections",
41-
"selections": get_serializable_selections(selections),
40+
"selections": [
41+
js_object_to_python_dict(s, ["anchor", "active"])
42+
for s in js_array_to_python_list(selections)
43+
],
4244
}
4345
res = rpc_get(command)
4446
if use_fallback(res):
@@ -50,7 +52,10 @@ def cursorless_everywhere_edit_text(
5052
command = {
5153
"id": "editText",
5254
"text": edit["text"],
53-
"changes": get_serializable_editor_changes(edit["changes"]),
55+
"changes": [
56+
js_object_to_python_dict(c, ["text", "rangeOffset", "rangeLength"])
57+
for c in js_array_to_python_list(edit["changes"])
58+
],
5459
}
5560
res = rpc_get(command)
5661
if use_fallback(res):
@@ -65,29 +70,15 @@ def use_fallback(result: dict) -> bool:
6570
return result.get("fallback", False)
6671

6772

68-
# What is passed from cursorless everywhere js is a javascript object, which is not serializable for python.
69-
def get_serializable_selections(selections: list[SelectionOffsets]):
70-
result: list[SelectionOffsets] = []
71-
for i in range(selections.length): # pyright: ignore [reportAttributeAccessIssue]
72-
selection = selections[i]
73-
result.append(
74-
{
75-
"anchor": selection["anchor"],
76-
"active": selection["active"],
77-
}
78-
)
73+
def js_array_to_python_list(array) -> list:
74+
result = []
75+
for i in range(array.length):
76+
result.append(array[i])
7977
return result
8078

8179

82-
def get_serializable_editor_changes(changes: list[EditorChange]):
83-
result: list[EditorChange] = []
84-
for i in range(changes.length): # pyright: ignore [reportAttributeAccessIssue]
85-
change = changes[i]
86-
result.append(
87-
{
88-
"text": change["text"],
89-
"rangeOffset": change["rangeOffset"],
90-
"rangeLength": change["rangeLength"],
91-
}
92-
)
80+
def js_object_to_python_dict(object, keys: list[str]) -> dict:
81+
result = {}
82+
for key in keys:
83+
result[key] = object[key]
9384
return result

0 commit comments

Comments
 (0)