Skip to content

Commit d9fa044

Browse files
AndreasArvidssonpokeyAndreas Arvidsson
authored
Added text based item scope (#709)
* Added text based item scope * Cleaned up imports * Use text based item as a fallback * Updated tests * Don't look for items inside strings * More work on item stage * updated test * Replaced take test with clear test * updated item stage * Typescript now uses text based item * Use text based item for more languages * Use text based item for more languages * Cleanup * Added more tests * Added test * Added support for escaped strings * Added additional test * Added more tests * Light clean up and add test * cleanup * Include trailing delimiter in arguments * Cleanup * Cleanup * Don't expand for weak item target * Include pairs inside strings * Cleanup * p * Added item support to python argument list * Use new requireStrongContainment * Cleanup * Cleanup * Added complex surrounding pair scope type * Tweaks to #709 (#823) * Tweaks to text-based item PR * More tweaks and tests * More doc string Co-authored-by: Andreas Arvidsson <andreas.arvidsson87@gmail.com> * Revert "Added complex surrounding pair scope type" This reverts commit 95acb52. * Cleanup * Create single union of items * Excluded separator from domain * Cleanup * Updated tests * Update src/processTargets/modifiers/ItemStage/tokenizeRange.ts Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com> * Update src/processTargets/modifiers/ItemStage/tokenizeRange.ts Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com> * Update src/languages/python.ts Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com> * Cleanup * Tweaks Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com> Co-authored-by: Andreas Arvidsson <andreas.arvidsson@redpill-linpro.com>
1 parent b43367c commit d9fa044

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1710
-159
lines changed

src/languages/cpp.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ const nodeMatchers: Partial<
113113
],
114114
[":", "=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>="]
115115
),
116-
collectionItem: argumentMatcher("initializer_list"),
117116
argumentOrParameter: argumentMatcher("parameter_list", "argument_list"),
118117
attribute: "attribute",
119118
};

src/languages/csharp.ts

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,6 @@ import { nodeFinder, typedNodeFinder } from "../util/nodeFinders";
1616
import { delimitedSelector, childRangeSelector } from "../util/nodeSelectors";
1717
import { patternFinder } from "../util/nodeFinders";
1818

19-
// Generated by the following command:
20-
// > curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-c-sharp/master/src/node-types.json \
21-
// | jq '.[] | select(.type == "_expression") | [.subtypes[].type]'
22-
const EXPRESSION_TYPES = [
23-
"anonymous_method_expression",
24-
"anonymous_object_creation_expression",
25-
"array_creation_expression",
26-
"as_expression",
27-
"assignment_expression",
28-
"await_expression",
29-
"base_expression",
30-
"binary_expression",
31-
"boolean_literal",
32-
"cast_expression",
33-
"character_literal",
34-
"checked_expression",
35-
"conditional_access_expression",
36-
"conditional_expression",
37-
"default_expression",
38-
"element_access_expression",
39-
"element_binding_expression",
40-
"generic_name",
41-
"global",
42-
"identifier",
43-
"implicit_array_creation_expression",
44-
"implicit_object_creation_expression",
45-
"implicit_stack_alloc_array_creation_expression",
46-
"initializer_expression",
47-
"integer_literal",
48-
"interpolated_string_expression",
49-
"invocation_expression",
50-
"is_expression",
51-
"is_pattern_expression",
52-
"lambda_expression",
53-
"make_ref_expression",
54-
"member_access_expression",
55-
"null_literal",
56-
"object_creation_expression",
57-
"parenthesized_expression",
58-
"postfix_unary_expression",
59-
"prefix_unary_expression",
60-
"query_expression",
61-
"range_expression",
62-
"real_literal",
63-
"ref_expression",
64-
"ref_type_expression",
65-
"ref_value_expression",
66-
"size_of_expression",
67-
"stack_alloc_array_creation_expression",
68-
"string_literal",
69-
"switch_expression",
70-
"this_expression",
71-
"throw_expression",
72-
"tuple_expression",
73-
"type_of_expression",
74-
"verbatim_string_literal",
75-
"with_expression",
76-
];
77-
78-
function isExpression(node: SyntaxNode) {
79-
return EXPRESSION_TYPES.includes(node.type);
80-
}
81-
8219
// Generated by the following command:
8320
// > curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-c-sharp/master/src/node-types.json \
8421
// | jq '.[] | select(.type == "_statement" or .type == "_declaration") | [.subtypes[].type]'
@@ -197,23 +134,6 @@ const getMapMatchers = {
197134
(node: SyntaxNode) => node.childForFieldName("initializer"),
198135
])
199136
),
200-
collectionItem: matcher(
201-
nodeFinder(
202-
(node) =>
203-
(node.parent?.type === "initializer_expression" &&
204-
isExpression(node)) ||
205-
node.type === "assignment_expression"
206-
),
207-
delimitedSelector(
208-
(node) =>
209-
node.type === "," ||
210-
node.type === "[" ||
211-
node.type === "]" ||
212-
node.type === "}" ||
213-
node.type === "{",
214-
", "
215-
)
216-
),
217137
string: typeMatcher("string_literal"),
218138
};
219139

src/languages/go.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ const nodeMatchers: Partial<
6464
patternMatcher("parameter_declaration"),
6565
patternMatcher("argument_declaration")
6666
),
67-
collectionItem: ["keyed_element", "element"],
6867
collectionKey: "keyed_element[0]",
6968
value: cascadingMatcher(
7069
patternMatcher("keyed_element[1]"),

src/languages/java.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ const nodeMatchers: Partial<
110110
["=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>="]
111111
),
112112
condition: conditionMatcher("*[condition]"),
113-
collectionItem: argumentMatcher("array_initializer"),
114113
argumentOrParameter: argumentMatcher("formal_parameters", "argument_list"),
115114
};
116115

src/languages/json.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import { SyntaxNode } from "web-tree-sitter";
2+
import { SimpleScopeTypeType } from "../typings/targetDescriptor.types";
3+
import { NodeMatcherAlternative, SelectionWithEditor } from "../typings/Types";
14
import {
25
createPatternMatchers,
3-
argumentMatcher,
46
leadingMatcher,
57
trailingMatcher,
68
} from "../util/nodeMatchers";
7-
import { NodeMatcherAlternative, SelectionWithEditor } from "../typings/Types";
8-
import { SimpleScopeTypeType } from "../typings/targetDescriptor.types";
9-
import { SyntaxNode } from "web-tree-sitter";
109
import { getNodeRange } from "../util/nodeSelectors";
1110

1211
const nodeMatchers: Partial<
@@ -17,7 +16,6 @@ const nodeMatchers: Partial<
1716
string: "string",
1817
collectionKey: trailingMatcher(["pair[key]"], [":"]),
1918
value: leadingMatcher(["*[value]"], [":"]),
20-
collectionItem: argumentMatcher("object", "array"),
2119
};
2220

2321
export const patternMatchers = createPatternMatchers(nodeMatchers);

src/languages/php.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ const nodeMatchers: Partial<
145145
),
146146

147147
collectionKey: trailingMatcher(["array_element_initializer[0]"], ["=>"]),
148-
collectionItem: argumentMatcher("array_creation_expression"),
149148

150149
argumentOrParameter: argumentMatcher("arguments", "formal_parameters"),
151150
};

src/languages/python.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
import { Selection } from "vscode";
12
import { SyntaxNode } from "web-tree-sitter";
3+
import { SimpleScopeTypeType } from "../typings/targetDescriptor.types";
4+
import { NodeFinder, NodeMatcherAlternative } from "../typings/Types";
5+
import { argumentNodeFinder, patternFinder } from "../util/nodeFinders";
26
import {
3-
createPatternMatchers,
47
argumentMatcher,
5-
leadingMatcher,
6-
trailingMatcher,
78
cascadingMatcher,
8-
patternMatcher,
99
conditionMatcher,
10+
createPatternMatchers,
11+
leadingMatcher,
1012
matcher,
13+
patternMatcher,
14+
trailingMatcher,
1115
} from "../util/nodeMatchers";
12-
import { patternFinder } from "../util/nodeFinders";
13-
import { NodeMatcherAlternative } from "../typings/Types";
14-
import { SimpleScopeTypeType } from "../typings/targetDescriptor.types";
15-
import { childRangeSelector } from "../util/nodeSelectors";
16+
import {
17+
argumentSelectionExtractor,
18+
childRangeSelector,
19+
} from "../util/nodeSelectors";
1620

1721
// Generated by the following command:
1822
// > curl https://raw.githubusercontent.com/tree-sitter/tree-sitter-python/d6210ceab11e8d812d4ab59c07c81458ec6e5184/src/node-types.json \
@@ -49,13 +53,29 @@ export const getTypeNode = (node: SyntaxNode) =>
4953
const dictionaryTypes = ["dictionary", "dictionary_comprehension"];
5054
const listTypes = ["list", "list_comprehension", "set"];
5155

56+
function importNodeFinder(): NodeFinder {
57+
const finder = argumentNodeFinder("import_from_statement");
58+
return (node: SyntaxNode, selection?: Selection) => {
59+
const childNode = finder(node, selection);
60+
if (
61+
childNode?.type === "dotted_name" &&
62+
childNode.id !== childNode.parent?.firstNamedChild?.id
63+
) {
64+
return childNode;
65+
}
66+
67+
return null;
68+
};
69+
}
70+
5271
const nodeMatchers: Partial<
5372
Record<SimpleScopeTypeType, NodeMatcherAlternative>
5473
> = {
5574
map: dictionaryTypes,
5675
list: listTypes,
5776
statement: STATEMENT_TYPES,
5877
string: "string",
78+
collectionItem: matcher(importNodeFinder(), argumentSelectionExtractor()),
5979
collectionKey: trailingMatcher(["pair[key]"], [":"]),
6080
ifStatement: "if_statement",
6181
anonymousFunction: "lambda?.lambda",
@@ -78,7 +98,6 @@ const nodeMatchers: Partial<
7898
"parameters.identifier!",
7999
"*[name]",
80100
],
81-
collectionItem: argumentMatcher(...dictionaryTypes, ...listTypes),
82101
value: cascadingMatcher(
83102
leadingMatcher(
84103
["assignment[right]", "augmented_assignment[right]", "~subscript[value]"],

src/languages/typescript.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ const nodeMatchers: Partial<
173173
],
174174
[":"]
175175
),
176-
collectionItem: argumentMatcher(...mapTypes, ...listTypes),
177176
value: cascadingMatcher(
178177
valueMatcher(),
179178
patternMatcher("return_statement.~return!"),

src/processTargets/getModifierStage.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ExcludeInteriorStage,
1111
InteriorOnlyStage,
1212
} from "./modifiers/InteriorStage";
13+
import ItemStage from "./modifiers/ItemStage";
1314
import { LeadingStage, TrailingStage } from "./modifiers/LeadingTrailingStages";
1415
import ModifyIfWeakStage from "./modifiers/ModifyIfWeakStage";
1516
import OrdinalRangeSubTokenStage, {
@@ -98,6 +99,8 @@ const getContainingScopeStage = (
9899
);
99100
case "url":
100101
return new UrlStage(modifier as UrlModifier);
102+
case "collectionItem":
103+
return new ItemStage(modifier);
101104
case "surroundingPair":
102105
return new SurroundingPairStage(
103106
modifier as ContainingSurroundingPairModifier

0 commit comments

Comments
 (0)