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

Add suffixedMatcher; use for keys #260

Merged
merged 4 commits into from
Sep 6, 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
4 changes: 2 additions & 2 deletions src/languages/cpp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
createPatternMatchers,
argumentMatcher,
valueMatcher,
prefixedMatcher,
} from "../util/nodeMatchers";
import { NodeMatcherAlternative, ScopeType } from "../typings/Types";

Expand Down Expand Up @@ -95,7 +95,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
"function_definition[declarator][declarator]", // void funcName() {}
"declaration.function_declarator![declarator]", // void funcName();
],
value: valueMatcher(
value: prefixedMatcher(
"*[declarator][value]",
"*[value]",
"assignment_expression[right]",
Expand Down
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,
valueMatcher,
prefixedMatcher,
} from "../util/nodeMatchers";
import { NodeMatcherAlternative, ScopeType } from "../typings/Types";

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

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

Expand Down
9 changes: 5 additions & 4 deletions src/languages/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { SyntaxNode } from "web-tree-sitter";
import {
createPatternMatchers,
argumentMatcher,
valueMatcher,
prefixedMatcher,
suffixedMatcher,
} from "../util/nodeMatchers";
import { NodeMatcherAlternative, ScopeType } from "../typings/Types";

Expand Down Expand Up @@ -46,7 +47,7 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
list: listTypes,
statement: STATEMENT_TYPES,
string: "string",
collectionKey: "pair[key]",
collectionKey: suffixedMatcher("pair[key]"),
ifStatement: "if_statement",
anonymousFunction: "lambda",
functionCall: "call",
Expand All @@ -55,15 +56,15 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
className: "class_definition[name]",
namedFunction: "decorated_definition?.function_definition",
functionName: "function_definition[name]",
type: valueMatcher("function_definition[return_type]", "*[type]"),
type: prefixedMatcher("function_definition[return_type]", "*[type]"),
name: [
"assignment[left]",
"typed_parameter.identifier!",
"parameters.identifier!",
"*[name]",
],
collectionItem: argumentMatcher(...dictionaryTypes, ...listTypes),
value: valueMatcher("assignment[right]", "~subscript[value]"),
value: prefixedMatcher("assignment[right]", "~subscript[value]"),
argumentOrParameter: argumentMatcher("parameters", "argument_list"),
};

Expand Down
6 changes: 5 additions & 1 deletion src/languages/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
patternMatcher,
createPatternMatchers,
argumentMatcher,
suffixedMatcher,
} from "../util/nodeMatchers";
import {
NodeMatcherAlternative,
Expand Down Expand Up @@ -86,7 +87,10 @@ const nodeMatchers: Partial<Record<ScopeType, NodeMatcherAlternative>> = {
map: mapTypes,
list: listTypes,
string: ["string", "template_string"],
collectionKey: ["pair[key]", "jsx_attribute.property_identifier!"],
collectionKey: suffixedMatcher(
"pair[key]",
"jsx_attribute.property_identifier!"
),
collectionItem: argumentMatcher(...mapTypes, ...listTypes),
value: valueMatcher(),
ifStatement: "if_statement",
Expand Down
31 changes: 31 additions & 0 deletions src/test/suite/fixtures/recorded/languages/python/chuckKey.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
spokenForm: chuck key
languageId: python
command:
actionName: remove
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: collectionKey, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
{
"foo": "bar",
"baz": "bongo",
}
selections:
- anchor: {line: 1, character: 5}
active: {line: 1, character: 5}
finalState:
documentContents: |-
{
"bar",
"baz": "bongo",
}
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
thatMark:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: containingScope, scopeType: collectionKey, includeSiblings: false}}]
31 changes: 31 additions & 0 deletions src/test/suite/fixtures/recorded/languages/python/chuckKey2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
spokenForm: chuck key
languageId: python
command:
actionName: remove
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: collectionKey, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
{
"foo": "bar",
"baz": "bongo",
}
selections:
- anchor: {line: 1, character: 12}
active: {line: 1, character: 12}
finalState:
documentContents: |-
{
"bar",
"baz": "bongo",
}
selections:
- anchor: {line: 1, character: 5}
active: {line: 1, character: 5}
thatMark:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: containingScope, scopeType: collectionKey, includeSiblings: false}}]
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ initialState:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
finalState:
documentContents: "foo: str "
documentContents: "foo: str"
selections:
- anchor: {line: 0, character: 2}
active: {line: 0, character: 2}
thatMark:
- anchor: {line: 0, character: 9}
active: {line: 0, character: 9}
- anchor: {line: 0, character: 8}
active: {line: 0, character: 8}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: containingScope, scopeType: value, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
spokenForm: chuck key
languageId: typescript
command:
actionName: remove
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: collectionKey, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
{
foo: "bar"
}
selections:
- anchor: {line: 1, character: 5}
active: {line: 1, character: 5}
finalState:
documentContents: |-
{
"bar"
}
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
thatMark:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: outside, modifier: {type: containingScope, scopeType: collectionKey, includeSiblings: false}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
spokenForm: clear key
languageId: typescript
command:
actionName: clearAndSetSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: collectionKey, includeSiblings: false}
extraArgs: []
marks: {}
initialState:
documentContents: |-
{
foo: "bar"
}
selections:
- anchor: {line: 1, character: 10}
active: {line: 1, character: 10}
finalState:
documentContents: |-
{
: "bar"
}
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
thatMark:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 4}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: containingScope, scopeType: collectionKey, includeSiblings: false}}]
19 changes: 18 additions & 1 deletion src/util/nodeMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
simpleSelectionExtractor,
argumentSelectionExtractor,
selectWithLeadingDelimiter,
selectWithTrailingDelimiter,
} from "./nodeSelectors";
import {
typedNodeFinder,
Expand Down Expand Up @@ -72,10 +73,26 @@ export function argumentMatcher(...parentTypes: string[]): NodeMatcher {
);
}

export function valueMatcher(...patterns: string[]): NodeMatcher {
/**
* Given `patterns`, creates a node matcher that will add leading delimiter to
* removal range.
* @param patterns Patterns for pattern finder
* @returns A node matcher
*/
export function prefixedMatcher(...patterns: string[]): NodeMatcher {
return matcher(patternFinder(...patterns), selectWithLeadingDelimiter);
}

/**
* Given `patterns`, creates a node matcher that will add trailing delimiter to
* removal range.
* @param patterns Patterns for pattern finder
* @returns A node matcher
*/
export function suffixedMatcher(...patterns: string[]): NodeMatcher {
return matcher(patternFinder(...patterns), selectWithTrailingDelimiter);
}

/**
* Create a new matcher that will try the given matchers in sequence until one
* returns non-null
Expand Down
25 changes: 22 additions & 3 deletions src/util/nodeSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export function selectWithLeadingDelimiter(
editor: TextEditor,
node: SyntaxNode
): SelectionWithContext {
const leadingDelimiterToken = node.previousSibling;
const leadingNonDelimiterToken = node.previousSibling?.previousSibling;
Copy link
Member Author

@pokey pokey Sep 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we now use the end of the token before the delimiter rather than the start of the delimiter itself. This way, for example, if you have foo = bar and say "chuck value", you won't get the trailing space after foo. See changed test case above (chuckValue)


const leadingDelimiterRange =
leadingDelimiterToken != null
? makeRange(leadingDelimiterToken.startPosition, node.startPosition)
leadingNonDelimiterToken != null
? makeRange(leadingNonDelimiterToken.endPosition, node.startPosition)
: null;

return {
Expand All @@ -55,6 +55,25 @@ export function selectWithLeadingDelimiter(
};
}

export function selectWithTrailingDelimiter(
editor: TextEditor,
node: SyntaxNode
): SelectionWithContext {
const trailingNonDelimiterToken = node.nextSibling?.nextSibling;

const trailingDelimiterRange =
trailingNonDelimiterToken != null
? makeRange(node.endPosition, trailingNonDelimiterToken.startPosition)
: null;

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

function getNextNonDelimiterNode(
startNode: SyntaxNode,
isDelimiterNode: (node: SyntaxNode) => boolean
Expand Down