-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathedit_command_modifiers.py
100 lines (68 loc) · 2.73 KB
/
edit_command_modifiers.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from dataclasses import dataclass
from typing import Callable, Literal, Union
from talon import Module, actions
EditSimpleModifierType = Literal["containingTokenIfEmpty",]
@dataclass
class EditSimpleModifier:
type: EditSimpleModifierType
def __str__(self):
return self.type
@dataclass
class EditContainingScopeModifier:
type = "containingScope"
scope_type: str
def __str__(self):
return f"{self.type}({self.scope_type})"
@dataclass
class EditExtendThroughModifier:
type: Literal["extendThroughStartOf", "extendThroughEndOf"]
scope_type: str
def __str__(self):
return f"{self.type}({self.scope_type})"
EditModifier = Union[
EditSimpleModifier,
EditContainingScopeModifier,
EditExtendThroughModifier,
]
mod = Module()
mod.list("edit_scope_type", desc="Scope types for the edit command")
@mod.capture(rule="this")
def edit_modifier_this(m) -> EditSimpleModifier:
return EditSimpleModifier("containingTokenIfEmpty")
@mod.capture(rule="head | tail")
def edit_modifier_head_tail(m) -> EditExtendThroughModifier:
if m[0] == "head":
return EditExtendThroughModifier("extendThroughStartOf", "line")
return EditExtendThroughModifier("extendThroughEndOf", "line")
@mod.capture(rule="{user.edit_scope_type}")
def edit_modifier_containing_scope(m) -> EditContainingScopeModifier:
return EditContainingScopeModifier(m.edit_scope_type)
@mod.capture(
rule="<user.edit_modifier_this> | "
"<user.edit_modifier_containing_scope> | "
"<user.edit_modifier_head_tail>"
)
def edit_modifier(m) -> EditModifier:
return m[0]
def containing_token_if_empty():
if actions.edit.selected_text() == "":
actions.edit.select_word()
modifier_callbacks = {
"containingTokenIfEmpty": containing_token_if_empty,
"extendThroughStartOf(line)": actions.user.select_line_start,
"extendThroughEndOf(line)": actions.user.select_line_end,
"containingScope(token)": actions.edit.select_word,
"containingScope(line)": actions.edit.select_line,
"containingScope(sentence)": actions.edit.select_sentence,
"containingScope(paragraph)": actions.edit.select_paragraph,
"containingScope(document)": actions.edit.select_all,
"containingScope(surroundingPair)": actions.user.select_surrounding_pair,
"containingScope(surroundingPairInterior)": actions.user.select_surrounding_pair_interior,
}
def get_modifier_callbacks(modifiers: list[EditModifier]) -> list[Callable]:
return [get_modifier_callback(modifier) for modifier in modifiers]
def get_modifier_callback(modifier: EditModifier) -> Callable:
key = str(modifier)
if key in modifier_callbacks:
return modifier_callbacks[key]
raise ValueError(f"Unknown edit modifier: {modifier}")