-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathlatex.ts
More file actions
150 lines (138 loc) · 3.42 KB
/
Copy pathlatex.ts
File metadata and controls
150 lines (138 loc) · 3.42 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import type { SimpleScopeTypeType, TextEditor } from "@cursorless/common";
import { Selection } from "@cursorless/common";
import type { Node } from "web-tree-sitter";
import type {
NodeMatcherAlternative,
SelectionWithContext,
} from "../typings/Types";
import { patternFinder } from "../util/nodeFinders";
import {
ancestorChainNodeMatcher,
cascadingMatcher,
createPatternMatchers,
matcher,
patternMatcher,
} from "../util/nodeMatchers";
const COMMANDS = [
"command",
"displayed_equation",
"generic_command",
"inline_formula",
"math_set",
"block_comment",
"package_include",
"class_include",
"latex_include",
"biblatex_include",
"bibtex_include",
"graphics_include",
"svg_include",
"inkscape_include",
"verbatim_include",
"import_include",
"caption",
"citation",
"label_definition",
"label_reference",
"label_reference_range",
"label_number",
"new_command_definition",
"old_command_definition",
"let_command_definition",
"environment_definition",
"glossary_entry_definition",
"glossary_entry_reference",
"acronym_definition",
"acronym_reference",
"theorem_definition",
"color_definition",
"color_set_definition",
"color_reference",
"tikz_library_import",
];
const GROUPS = [
"curly_group",
"curly_group_text",
"curly_group_text_list",
"curly_group_path",
"curly_group_path_list",
"curly_group_command_name",
"curly_group_key_value",
"curly_group_glob_pattern",
"curly_group_impl",
"brack_group",
"brack_group_text",
"brack_group_argc",
"brack_group_key_value",
"mixed_group",
];
const SECTIONING = [
"subparagraph",
"paragraph",
"subsubsection",
"subsection",
"section",
"chapter",
"part",
];
const sectioningText = SECTIONING.map((s) => `${s}[text]`);
const sectioningCommand = SECTIONING.map((s) => `${s}[command]`);
function unwrapGroupParens(
editor: TextEditor,
node: Node,
): SelectionWithContext {
return {
selection: new Selection(
editor.document.positionAt(node.startIndex + 1),
editor.document.positionAt(node.endIndex - 1),
),
context: {
removalRange: new Selection(
editor.document.positionAt(node.startIndex),
editor.document.positionAt(node.endIndex),
),
},
};
}
function extendToNamedSiblingIfExists(
editor: TextEditor,
node: Node,
): SelectionWithContext {
const startIndex = node.startIndex;
let endIndex = node.endIndex;
const sibling = node.nextNamedSibling;
if (sibling != null && sibling.isNamed) {
endIndex = sibling.endIndex;
}
return {
selection: new Selection(
editor.document.positionAt(startIndex),
editor.document.positionAt(endIndex),
),
context: {},
};
}
const nodeMatchers: Partial<
Record<SimpleScopeTypeType, NodeMatcherAlternative>
> = {
argumentOrParameter: cascadingMatcher(
ancestorChainNodeMatcher(
[patternFinder(...COMMANDS), patternFinder(...GROUPS)],
1,
unwrapGroupParens,
),
matcher(
patternFinder("begin[name]", "end[name]", ...sectioningText),
unwrapGroupParens,
),
),
functionCall: cascadingMatcher(
matcher(patternFinder(...COMMANDS, "begin", "end")),
matcher(patternFinder(...sectioningCommand), extendToNamedSiblingIfExists),
),
name: cascadingMatcher(
matcher(patternFinder(...sectioningText), unwrapGroupParens),
patternMatcher("begin[name][text]", "end[name][text]"),
),
};
export default createPatternMatchers(nodeMatchers);