-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathcompound_targets.py
More file actions
85 lines (66 loc) · 2.36 KB
/
Copy pathcompound_targets.py
File metadata and controls
85 lines (66 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from talon import Module
from .connective import default_range_connective
from .primitive_target import BASE_TARGET
mod = Module()
mod.list(
"cursorless_range_connective",
desc="A range joiner that indicates whether to include or exclude anchor and active",
)
mod.list(
"cursorless_list_connective",
desc="A list joiner",
)
@mod.capture(
rule="[<user.cursorless_range_type>] {user.cursorless_range_connective} | <user.cursorless_range_type>"
)
def cursorless_range_connective_with_type(m) -> str:
return {
"connective": getattr(
m, "cursorless_range_connective", default_range_connective
),
"type": getattr(m, "cursorless_range_type", None),
}
@mod.capture(
rule=(
"<user.cursorless_primitive_target> | "
"<user.cursorless_range_connective_with_type> <user.cursorless_primitive_target> | "
"<user.cursorless_primitive_target> <user.cursorless_range_connective_with_type> <user.cursorless_primitive_target>"
)
)
def cursorless_range(m) -> str:
primitive_targets = m.cursorless_primitive_target_list
range_connective_with_type = getattr(
m, "cursorless_range_connective_with_type", None
)
if range_connective_with_type is None:
return primitive_targets[0]
if len(primitive_targets) == 1:
anchor = BASE_TARGET.copy()
else:
anchor = primitive_targets[0]
range_connective = range_connective_with_type["connective"]
range_type = range_connective_with_type["type"]
range = {
"type": "range",
"anchor": anchor,
"active": primitive_targets[-1],
"excludeAnchor": not is_anchor_included(range_connective),
"excludeActive": not is_active_included(range_connective),
}
if range_type:
range["rangeType"] = range_type
return range
def is_anchor_included(range_connective: str):
return range_connective not in ["rangeExclusive", "rangeExcludingStart"]
def is_active_included(range_connective: str):
return range_connective not in ["rangeExclusive", "rangeExcludingEnd"]
@mod.capture(
rule="<user.cursorless_range> ({user.cursorless_list_connective} <user.cursorless_range>)*"
)
def cursorless_target(m) -> dict:
if len(m.cursorless_range_list) == 1:
return m.cursorless_range
return {
"type": "list",
"elements": m.cursorless_range_list,
}