|
1 | 1 | import { SimpleScopeTypeType } from "@cursorless/common";
|
2 |
| -import type { SyntaxNode } from "web-tree-sitter"; |
3 |
| -import { NodeMatcherAlternative, SelectionWithEditor } from "../typings/Types"; |
4 |
| -import { patternFinder } from "../util/nodeFinders"; |
5 |
| -import { |
6 |
| - argumentMatcher, |
7 |
| - cascadingMatcher, |
8 |
| - conditionMatcher, |
9 |
| - createPatternMatchers, |
10 |
| - matcher, |
11 |
| - patternMatcher, |
12 |
| - trailingMatcher, |
13 |
| -} from "../util/nodeMatchers"; |
14 |
| -import { |
15 |
| - childRangeSelector, |
16 |
| - extendForwardPastOptional, |
17 |
| - getNodeInternalRange, |
18 |
| - getNodeRange, |
19 |
| - unwrapSelectionExtractor, |
20 |
| -} from "../util/nodeSelectors"; |
21 |
| -import { branchMatcher } from "./branchMatcher"; |
22 |
| -import { elseExtractor, elseIfExtractor } from "./elseIfExtractor"; |
23 |
| -import { ternaryBranchMatcher } from "./ternaryBranchMatcher"; |
24 |
| - |
25 |
| -// Generated by the following command: |
26 |
| -// > curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-typescript/4c20b54771e4b390ee058af2930feb2cd55f2bf8/typescript/src/node-types.json \ |
27 |
| -// | jq '[.[] | select(.type == "statement" or .type == "declaration") | .subtypes[].type]' |
28 |
| -const STATEMENT_TYPES = [ |
29 |
| - "abstract_class_declaration", |
30 |
| - "ambient_declaration", |
31 |
| - "break_statement", |
32 |
| - "class_declaration", |
33 |
| - "continue_statement", |
34 |
| - "debugger_statement", |
35 |
| - "declaration", |
36 |
| - "do_statement", |
37 |
| - "empty_statement", |
38 |
| - "enum_declaration", |
39 |
| - "export_statement", |
40 |
| - "expression_statement", |
41 |
| - "for_in_statement", |
42 |
| - "for_statement", |
43 |
| - "function_declaration", |
44 |
| - "function_signature", |
45 |
| - "generator_function_declaration", |
46 |
| - "if_statement", |
47 |
| - "import_alias", |
48 |
| - "import_statement", |
49 |
| - "interface_declaration", |
50 |
| - "internal_module", |
51 |
| - "labeled_statement", |
52 |
| - "lexical_declaration", |
53 |
| - "module", |
54 |
| - "return_statement", |
55 |
| - // "statement_block", This is disabled since we want the whole statement and not just the block |
56 |
| - "switch_statement", |
57 |
| - "throw_statement", |
58 |
| - "try_statement", |
59 |
| - "type_alias_declaration", |
60 |
| - "variable_declaration", |
61 |
| - "while_statement", |
62 |
| - "with_statement", |
63 |
| -]; |
64 |
| - |
65 |
| -const mapTypes = ["object", "object_pattern"]; |
66 |
| -const listTypes = ["array", "array_pattern"]; |
| 2 | +import { NodeMatcherAlternative } from "../typings/Types"; |
| 3 | +import { argumentMatcher, createPatternMatchers } from "../util/nodeMatchers"; |
67 | 4 |
|
68 | 5 | const nodeMatchers: Partial<
|
69 | 6 | Record<SimpleScopeTypeType, NodeMatcherAlternative>
|
70 | 7 | > = {
|
71 |
| - map: mapTypes, |
72 |
| - list: listTypes, |
73 |
| - string: ["string", "template_string"], |
74 | 8 | collectionItem: "jsx_attribute",
|
75 |
| - collectionKey: trailingMatcher( |
76 |
| - [ |
77 |
| - "pair[key]", |
78 |
| - "jsx_attribute.property_identifier!", |
79 |
| - "object_type.property_signature[name]!", |
80 |
| - "shorthand_property_identifier", |
81 |
| - ], |
82 |
| - [":"], |
83 |
| - ), |
84 |
| - ifStatement: "if_statement", |
85 |
| - comment: "comment", |
86 |
| - regularExpression: "regex", |
87 |
| - className: ["class_declaration[name]", "class[name]"], |
88 |
| - functionCall: ["call_expression", "new_expression"], |
89 |
| - functionCallee: cascadingMatcher( |
90 |
| - patternMatcher("call_expression[function]"), |
91 |
| - matcher( |
92 |
| - patternFinder("new_expression"), |
93 |
| - childRangeSelector(["arguments"], []), |
94 |
| - ), |
95 |
| - ), |
96 |
| - statement: cascadingMatcher( |
97 |
| - matcher( |
98 |
| - patternFinder( |
99 |
| - "property_signature", |
100 |
| - "public_field_definition", |
101 |
| - "abstract_method_signature", |
102 |
| - ), |
103 |
| - extendForwardPastOptional(";"), |
104 |
| - ), |
105 |
| - patternMatcher( |
106 |
| - ...STATEMENT_TYPES.map((type) => `export_statement?.${type}`), |
107 |
| - "method_definition", |
108 |
| - ), |
109 |
| - ), |
110 |
| - condition: cascadingMatcher( |
111 |
| - patternMatcher("ternary_expression[condition]"), |
112 |
| - conditionMatcher( |
113 |
| - "if_statement[condition]", |
114 |
| - "for_statement[condition]", |
115 |
| - "while_statement[condition]", |
116 |
| - "do_statement[condition]", |
117 |
| - ), |
118 |
| - ), |
119 |
| - ["private.switchStatementSubject"]: matcher( |
120 |
| - patternFinder("switch_statement[value]"), |
121 |
| - unwrapSelectionExtractor, |
122 |
| - ), |
123 |
| - branch: cascadingMatcher( |
124 |
| - patternMatcher("switch_case"), |
125 |
| - matcher(patternFinder("else_clause"), elseExtractor("if_statement")), |
126 |
| - matcher(patternFinder("if_statement"), elseIfExtractor()), |
127 |
| - branchMatcher("try_statement", ["catch_clause", "finally_clause"]), |
128 |
| - ternaryBranchMatcher("ternary_expression", [1, 2]), |
129 |
| - ), |
130 |
| - class: [ |
131 |
| - "export_statement?.class_declaration", // export class | class |
132 |
| - "export_statement?.abstract_class_declaration", // export abstract class | abstract class |
133 |
| - "export_statement.class", // export default class |
134 |
| - ], |
135 | 9 | argumentOrParameter: argumentMatcher("formal_parameters", "arguments"),
|
136 |
| - // XML, JSX |
137 |
| - attribute: ["jsx_attribute"], |
138 | 10 | };
|
139 | 11 |
|
140 | 12 | export const patternMatchers = createPatternMatchers(nodeMatchers);
|
141 |
| - |
142 |
| -export function stringTextFragmentExtractor( |
143 |
| - node: SyntaxNode, |
144 |
| - _selection: SelectionWithEditor, |
145 |
| -) { |
146 |
| - if ( |
147 |
| - node.type === "string_fragment" || |
148 |
| - node.type === "regex_pattern" || |
149 |
| - node.type === "jsx_text" |
150 |
| - ) { |
151 |
| - return getNodeRange(node); |
152 |
| - } |
153 |
| - |
154 |
| - if (node.type === "template_string") { |
155 |
| - // Exclude starting and ending quotation marks |
156 |
| - return getNodeInternalRange(node); |
157 |
| - } |
158 |
| - |
159 |
| - return null; |
160 |
| -} |
0 commit comments