Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delimiter selector updated #347

Merged
merged 9 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions data/playground/Java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Java {
private Java(String name) {
String value = "hello";
System.out.println(value);
}
}
1 change: 1 addition & 0 deletions data/playground/c-plus-plus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
string name = "hello";
1 change: 1 addition & 0 deletions data/playground/csharp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
String name = "hello";
File renamed without changes.
9 changes: 9 additions & 0 deletions data/playground/html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>

<body>
<script>
<h1>Title</h1>
</script>
</body>

</html>
1 change: 1 addition & 0 deletions data/playground/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const name = "value";
31 changes: 31 additions & 0 deletions data/playground/json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"measurementName": "Primärvård",
"periods": [
"2020"
],
"columns": [
"measurementName",
"measurementYear",
"measurementMonth",
"questionnaireName",
"questionnaireLanguage",
"orgHsaId"
],
"questions": [
"Hur upplevde du besöket som helhet?",
"Upplever du att distanskontakt är lika bra som att träffa personalen på riktigt?",
"Diskuterade behandlaren eller någon annan ur personalen någon av följande levnadsvanor med dig? Motionsvanor"
],
"dimensions": [
"Helhetsintryck",
"Emotionellt stöd",
"Delaktighet och involvering",
"Respekt och bemötande",
"Kontinuitet och koordinering",
"Information och kunskap",
"Tillgänglighet"
],
"excludeNonInvited": true,
"excludeNonAnswered": false,
"limit": 10
}
11 changes: 11 additions & 0 deletions data/playground/markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Some document

This some text

## Some sub header

Some more text

```js
const value = "hello there";
```
8 changes: 8 additions & 0 deletions data/playground/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
hello = 5


def my_funk(value: str) -> str:
print(value)


my_dict = {"key": "value"}
1 change: 1 addition & 0 deletions data/playground/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const value = "value";
10 changes: 10 additions & 0 deletions data/playground/typescriptreacts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const MyComponent = ({ children: any }) => {
return (
<div>
<h1>Some title</h1>
{children}
</div>
);
};

export default MyComponent;
3 changes: 3 additions & 0 deletions data/playground/xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<root>
<name id="some id">Some text</name>
</root>
15 changes: 9 additions & 6 deletions src/languages/cpp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
createPatternMatchers,
argumentMatcher,
prefixedMatcher,
leadingMatcher,
} from "../util/nodeMatchers";
import { NodeMatcherAlternative, ScopeType } from "../typings/Types";

Expand Down Expand Up @@ -95,11 +95,14 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
"function_definition[declarator][declarator]", // void funcName() {}
"declaration.function_declarator![declarator]", // void funcName();
],
value: prefixedMatcher(
"*[declarator][value]",
"*[value]",
"assignment_expression[right]",
"optional_parameter_declaration[default_value]"
value: leadingMatcher(
[
"*[declarator][value]",
"*[value]",
"assignment_expression[right]",
"optional_parameter_declaration[default_value]",
],
["=", ":"]
),
collectionItem: argumentMatcher("initializer_list"),
argumentOrParameter: argumentMatcher("parameter_list", "argument_list"),
Expand Down
44 changes: 15 additions & 29 deletions src/languages/csharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import {
matcher,
typeMatcher,
} from "../util/nodeMatchers";
import { NodeMatcher, ScopeType } from "../typings/Types";
import { getNameNode } from "../util/treeSitterUtils";
import { NodeMatcherAlternative, ScopeType } from "../typings/Types";
import { nodeFinder, typedNodeFinder } from "../util/nodeFinders";
import {
delimitedSelector,
selectWithLeadingDelimiter,
} from "../util/nodeSelectors";
import { delimitedSelector } from "../util/nodeSelectors";

// Generated by the following command:
// > curl https://github.com/raw/tree-sitter/tree-sitter-c-sharp/master/src/node-types.json \
Expand Down Expand Up @@ -146,10 +142,6 @@ const OBJECT_TYPES_WITH_INITIALIZERS_AS_CHILDREN = [
"implicit_object_creation_expression",
];

const findTypeNode = (node: SyntaxNode) => {
return node.childForFieldName("type") ?? null;
};

// Every array or map initializer contains its contents in a initializer_expression.
// There appears to be no distinction between dictionaries and arrays as far as the syntax tree goes.
// that means some of the commands for maps may work on arrays, and some of the commands for arrays may work on maps.
Expand Down Expand Up @@ -213,33 +205,27 @@ const getMapMatchers = {
string: typeMatcher("string_literal"),
};

const nodeMatchers: Partial<Record<ScopeType, NodeMatcher>> = {
const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
...getMapMatchers,
ifStatement: typeMatcher("if_statement"),
class: typeMatcher("class_declaration"),
className: composedMatcher([
typedNodeFinder("class_declaration"),
getNameNode,
]),
statement: typeMatcher(...STATEMENT_TYPES),
anonymousFunction: typeMatcher("lambda_expression"),
functionCall: typeMatcher("invocation_expression"),
ifStatement: "if_statement",
class: "class_declaration",
className: "class_declaration[name]",
statement: STATEMENT_TYPES,
anonymousFunction: "lambda_expression",
functionCall: "invocation_expression",
argumentOrParameter: matcher(
nodeFinder(
(node) =>
node.parent?.type === "argument_list" || node.type === "parameter"
),
makeDelimitedSelector("(", ")")
),
namedFunction: typeMatcher(...NAMED_FUNCTION_TYPES),
functionName: composedMatcher([
typedNodeFinder(...NAMED_FUNCTION_TYPES),
getNameNode,
]),
comment: matcher(typedNodeFinder("comment")),
regularExpression: matcher(typedNodeFinder("regex")),
type: matcher(findTypeNode, selectWithLeadingDelimiter),
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
name: matcher(getNameNode),
namedFunction: NAMED_FUNCTION_TYPES,
functionName: NAMED_FUNCTION_TYPES.map((t) => t + "[name]"),
comment: "comment",
regularExpression: "regex",
type: "*[type]",
name: ["*[name]", "identifier"],
};

export default createPatternMatchers(nodeMatchers);
4 changes: 2 additions & 2 deletions src/languages/java.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
createPatternMatchers,
argumentMatcher,
prefixedMatcher,
leadingMatcher,
conditionMatcher,
} from "../util/nodeMatchers";
import { NodeMatcherAlternative, ScopeType } from "../typings/Types";
Expand Down Expand Up @@ -63,7 +63,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
"method_declaration.identifier!",
"constructor_declaration.identifier!",
],
value: prefixedMatcher("*[declarator][value]", "*[value]"),
value: leadingMatcher(["*[declarator][value]", "*[value]"], ["="]),
condition: conditionMatcher("*[condition]"),
collectionItem: argumentMatcher("array_initializer"),
argumentOrParameter: argumentMatcher("formal_parameters", "argument_list"),
Expand Down
8 changes: 4 additions & 4 deletions src/languages/json.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
createPatternMatchers,
argumentMatcher,
prefixedMatcher,
suffixedMatcher,
leadingMatcher,
trailingMatcher,
} from "../util/nodeMatchers";
import { ScopeType, NodeMatcherAlternative } from "../typings/Types";

Expand All @@ -11,8 +11,8 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
list: "array",
string: "string",
comment: "comment",
collectionKey: suffixedMatcher("pair[key]"),
value: prefixedMatcher("*[value]"),
collectionKey: trailingMatcher(["pair[key]"], [":"]),
value: leadingMatcher(["*[value]"], [":"]),
collectionItem: argumentMatcher("object", "array"),
};

Expand Down
10 changes: 5 additions & 5 deletions src/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { SyntaxNode } from "web-tree-sitter";
import {
createPatternMatchers,
argumentMatcher,
prefixedMatcher,
suffixedMatcher,
leadingMatcher,
trailingMatcher,
cascadingMatcher,
patternMatcher,
conditionMatcher,
Expand Down Expand Up @@ -50,7 +50,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
list: listTypes,
statement: STATEMENT_TYPES,
string: "string",
collectionKey: suffixedMatcher("pair[key]"),
collectionKey: trailingMatcher(["pair[key]"], [":"]),
ifStatement: "if_statement",
anonymousFunction: "lambda?.lambda",
functionCall: "call",
Expand All @@ -60,7 +60,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
namedFunction: "decorated_definition?.function_definition",
functionName: "function_definition[name]",
condition: conditionMatcher("*[condition]"),
type: prefixedMatcher("function_definition[return_type]", "*[type]"),
type: leadingMatcher(["function_definition[return_type]", "*[type]"], [":", "->"]),
name: [
"assignment[left]",
"typed_parameter.identifier!",
Expand All @@ -69,7 +69,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
],
collectionItem: argumentMatcher(...dictionaryTypes, ...listTypes),
value: cascadingMatcher(
prefixedMatcher("assignment[right]", "~subscript[value]"),
leadingMatcher(["assignment[right]", "~subscript[value]"], ["=", ":"]),
patternMatcher("return_statement.~return!")
),
argumentOrParameter: argumentMatcher("parameters", "argument_list"),
Expand Down
39 changes: 7 additions & 32 deletions src/languages/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@ import {
patternMatcher,
createPatternMatchers,
argumentMatcher,
suffixedMatcher,
trailingMatcher,
conditionMatcher,
} from "../util/nodeMatchers";
import {
NodeMatcherAlternative,
ScopeType,
SelectionWithContext,
SelectionWithEditor,
} from "../typings/Types";
import {
makeRangeFromPositions,
selectWithLeadingDelimiter,
simpleSelectionExtractor,
} from "../util/nodeSelectors";
import { selectWithLeadingDelimiter } from "../util/nodeSelectors";
import { patternFinder } from "../util/nodeFinders";
import { TextEditor } from "vscode";

// Generated by the following command:
// > curl https://github.com/raw/tree-sitter/tree-sitter-typescript/4c20b54771e4b390ee058af2930feb2cd55f2bf8/typescript/src/node-types.json \
Expand Down Expand Up @@ -83,39 +77,20 @@ function valueMatcher() {
return matcher(
(node: SyntaxNode) =>
node.type === "jsx_attribute" ? node.lastChild : pFinder(node),
selectWithLeadingDelimiter
selectWithLeadingDelimiter("=", ":")
);
}

export function typeSelectionExtractor(
editor: TextEditor,
node: SyntaxNode
): SelectionWithContext {
const previousNode = node.previousSibling;

const leadingDelimiterRange =
previousNode != null && previousNode.type === ":"
? makeRangeFromPositions(previousNode.startPosition, node.startPosition)
: null;

return {
...simpleSelectionExtractor(editor, node),
context: {
leadingDelimiterRange,
},
};
}

const mapTypes = ["object", "object_pattern"];
const listTypes = ["array", "array_pattern"];

const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
map: mapTypes,
list: listTypes,
string: ["string", "template_string"],
collectionKey: suffixedMatcher(
"pair[key]",
"jsx_attribute.property_identifier!"
collectionKey: trailingMatcher(
["pair[key]", "jsx_attribute.property_identifier!"],
[":"]
),
collectionItem: argumentMatcher(...mapTypes, ...listTypes),
value: cascadingMatcher(
Expand Down Expand Up @@ -181,7 +156,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
],
type: cascadingMatcher(
// Typed parameters, properties, and functions
matcher(findTypeNode, typeSelectionExtractor),
matcher(findTypeNode, selectWithLeadingDelimiter(":")),
// Type alias/interface declarations
patternMatcher(
"export_statement?.type_alias_declaration",
Expand Down
27 changes: 27 additions & 0 deletions src/test/suite/fixtures/recorded/languages/cpp/chuckTypeAir.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
spokenForm: chuck type air
languageId: cpp
command:
actionName: remove
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: type, includeSiblings: false}
mark: {type: decoratedSymbol, symbolColor: default, character: a}
extraArgs: []
initialState:
documentContents: string name = "hello";
selections:
- anchor: {line: 0, character: 22}
active: {line: 0, character: 22}
marks:
default.a:
start: {line: 0, character: 7}
end: {line: 0, character: 11}
finalState:
documentContents: name = "hello";
selections:
- anchor: {line: 0, character: 15}
active: {line: 0, character: 15}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: a}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: containingScope, scopeType: type, includeSiblings: false}}]
Loading