1
1
from talon import Context , Module , actions
2
2
3
3
from .cursorless_everywhere_types import (
4
- EditorChange ,
5
4
EditorEdit ,
6
5
EditorState ,
7
6
SelectionOffsets ,
@@ -38,7 +37,10 @@ def cursorless_everywhere_set_selections(
38
37
):
39
38
command = {
40
39
"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
+ ],
42
44
}
43
45
res = rpc_get (command )
44
46
if use_fallback (res ):
@@ -50,7 +52,10 @@ def cursorless_everywhere_edit_text(
50
52
command = {
51
53
"id" : "editText" ,
52
54
"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
+ ],
54
59
}
55
60
res = rpc_get (command )
56
61
if use_fallback (res ):
@@ -65,29 +70,15 @@ def use_fallback(result: dict) -> bool:
65
70
return result .get ("fallback" , False )
66
71
67
72
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 ])
79
77
return result
80
78
81
79
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 ]
93
84
return result
0 commit comments