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

Fix anonymous functions #232

Merged
merged 2 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/canonicalizeTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import update from "immutability-helper";
import { transformPrimitiveTargets } from "./util/targetUtils";

const scopeTypeAliasToCanonicalName: Record<string, ScopeType> = {
anonymousFunction: "arrowFunction",
arrowFunction: "anonymousFunction",
dictionary: "map",
regex: "regularExpression",
};
Expand Down
43 changes: 31 additions & 12 deletions src/languages/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const STATEMENT_TYPES = [
"switch_statement",
"throw_statement",
"try_statement",
"while_statement"
"while_statement",
];

// > curl https://github.com/raw/tree-sitter/tree-sitter-cpp/master/src/node-types.json | jq '[.[] | select(.type == "_type_specifier") | .subtypes[].type]'
Expand All @@ -57,34 +57,53 @@ const TYPE_TYPES = [
"struct_specifier",
"template_type",
"type_identifier",
"union_specifier"
"union_specifier",
];

const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
statement: STATEMENT_TYPES,
class: ["class_specifier", "struct_specifier", "enum_specifier", "union_specifier"],
class: [
"class_specifier",
"struct_specifier",
"enum_specifier",
"union_specifier",
],
className: [
"class_specifier[name]", "struct_specifier[name]", "enum_specifier[name]", "union_specifier[name]",
"function_definition[declarator][declarator][namespace]" // void ClassName::method() {}
"class_specifier[name]",
"struct_specifier[name]",
"enum_specifier[name]",
"union_specifier[name]",
"function_definition[declarator][declarator][namespace]", // void ClassName::method() {}
],
ifStatement: "if_statement",
string: "string_literal",
comment: "comment",
arrowFunction: "lambda_expression",
anonymousFunction: "lambda_expression",
list: "initializer_list",
functionCall: "call_expression",
name: ["*[declarator][declarator][name]", "*[declarator][name]", "*[declarator][declarator]", "*[declarator]", "*[name]"],
name: [
"*[declarator][declarator][name]",
"*[declarator][name]",
"*[declarator][declarator]",
"*[declarator]",
"*[name]",
],
namedFunction: ["function_definition", "declaration.function_declarator"],
type: TYPE_TYPES.concat([ "*[type]" ]),
type: TYPE_TYPES.concat(["*[type]"]),
functionName: [
"function_definition[declarator][declarator][name]", // void C::funcName() {}
"function_definition[declarator][declarator]", // void funcName() {}
"declaration.function_declarator![declarator]", // void funcName();
"function_definition[declarator][declarator]", // void funcName() {}
"declaration.function_declarator![declarator]", // void funcName();
],
value: valueMatcher("*[declarator][value]", "*[value]", "assignment_expression[right]", "optional_parameter_declaration[default_value]"),
value: valueMatcher(
"*[declarator][value]",
"*[value]",
"assignment_expression[right]",
"optional_parameter_declaration[default_value]"
),
collectionItem: argumentMatcher("initializer_list"),
argumentOrParameter: argumentMatcher("parameter_list", "argument_list"),
attribute: "attribute"
attribute: "attribute",
};

export default createPatternMatchers(nodeMatchers);
2 changes: 1 addition & 1 deletion src/languages/csharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcher>> = {
getNameNode,
]),
statement: typeMatcher(...STATEMENT_TYPES),
arrowFunction: typeMatcher("lambda_expression"),
anonymousFunction: typeMatcher("lambda_expression"),
functionCall: typeMatcher("invocation_expression"),
argumentOrParameter: matcher(
nodeFinder(
Expand Down
2 changes: 1 addition & 1 deletion src/languages/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
ifStatement: "if_statement",
string: "string_literal",
comment: "comment",
arrowFunction: "lambda_expression",
anonymousFunction: "lambda_expression",
list: "array_initializer",
functionCall: "method_invocation",
map: "block",
Expand Down
2 changes: 1 addition & 1 deletion src/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
string: "string",
collectionKey: "pair[key]",
ifStatement: "if_statement",
arrowFunction: "lambda",
anonymousFunction: "lambda",
functionCall: "call",
comment: "comment",
class: "decorated_definition?.class_definition",
Expand Down
4 changes: 3 additions & 1 deletion src/languages/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
collectionItem: argumentMatcher(...mapTypes, ...listTypes),
value: valueMatcher(),
ifStatement: "if_statement",
arrowFunction: "arrow_function",
anonymousFunction: ["arrow_function", "function"],
name: [
"*[name]",
"optional_parameter.identifier!",
Expand Down Expand Up @@ -127,6 +127,8 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
// [export] function
"export_statement?.function_declaration",
// export default function
// NB: We require export statement because otherwise it is an anonymous
// function
"export_statement.function",
// export default arrow
"export_statement.arrow_function",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
spokenForm: clear funk
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: namedFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |
function bar() {
foo(function () {

});
}
selections:
- anchor: {line: 2, character: 6}
active: {line: 2, character: 6}
finalState:
documentContents: |+

selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: namedFunction, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
spokenForm: clear funk
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: namedFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |
function bar() {
foo(() => {

});
}
selections:
- anchor: {line: 2, character: 6}
active: {line: 2, character: 6}
finalState:
documentContents: |+

selections:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 0}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: namedFunction, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: clear lambda
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
foo(() => {

})
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
finalState:
documentContents: foo()
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
thatMark:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: clear lambda
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
foo(function () {

})
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
finalState:
documentContents: foo()
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
thatMark:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: clear lambda
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
foo(async function () {

})
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
finalState:
documentContents: foo()
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
thatMark:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spokenForm: clear lambda
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
foo(async () => {

})
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 0}
finalState:
documentContents: foo()
selections:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
thatMark:
- anchor: {line: 0, character: 4}
active: {line: 0, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: anonymousFunction, includeSiblings: false}}]
2 changes: 1 addition & 1 deletion src/typings/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type Delimiter =

export type ScopeType =
| "argumentOrParameter"
| "arrowFunction"
| "anonymousFunction"
| "attribute"
| "class"
| "className"
Expand Down