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

Check ancestors for every #227

Merged
merged 3 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 29 additions & 10 deletions src/languages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,38 @@ function matcherIncludeSiblings(matcher: NodeMatcher): NodeMatcher {
selection: SelectionWithEditor,
node: SyntaxNode
): NodeMatcherValue[] | null => {
const matches = matcher(selection, node);
let matches = matcher(selection, node);
if (matches == null) {
return null;
}
return matches
.flatMap((match) =>
match.node.parent!.namedChildren.flatMap((sibling) =>
matcher(
selectionWithEditorFromRange(selection, match.selection.selection),
sibling
)
)
matches = matches.flatMap((match) =>
findNearestContainingAncestorNode(
match.node,
selectionWithEditorFromRange(selection, match.selection.selection),
matcher
)
.filter((match) => match != null) as NodeMatcherValue[];
) as NodeMatcherValue[];
if (matches.length > 0) {
return matches;
}
return null;
};
}

function findNearestContainingAncestorNode(
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
node: SyntaxNode,
selection: SelectionWithEditor,
nodeMatcher: NodeMatcher
) {
let parent: SyntaxNode | null = node.parent;
while (parent != null) {
const matches = parent!.namedChildren
.flatMap((sibling) => nodeMatcher(selection, sibling))
.filter((match) => match != null) as NodeMatcherValue[];
if (matches.length > 0) {
return matches;
}
parent = parent.parent;
}
return [];
}
9 changes: 4 additions & 5 deletions src/processTargets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,11 @@ function transformSelection(
modifier.scopeType,
modifier.includeSiblings ?? false
);
let node: SyntaxNode | null = context.getNodeAtLocation(
const node: SyntaxNode | null = context.getNodeAtLocation(
new Location(selection.editor.document.uri, selection.selection)
);

let result = findNearestContainingAncestorNode(
const result = findNearestContainingAncestorNode(
node,
nodeMatcher,
selection
Expand Down Expand Up @@ -418,18 +418,17 @@ function transformSelection(

case "surroundingPair":
{
let node: SyntaxNode | null = context.getNodeAtLocation(
const node: SyntaxNode | null = context.getNodeAtLocation(
new vscode.Location(
selection.editor.document.uri,
selection.selection
)
);

const nodeMatcher = createSurroundingPairMatcher(
modifier.delimiter,
modifier.delimitersOnly
);
let result = findNearestContainingAncestorNode(
const result = findNearestContainingAncestorNode(
node,
nodeMatcher,
selection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
spokenForm: take every item
languageId: typescript
command:
actionName: setSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: collectionItem, includeSiblings: true}
extraArgs: []
marks: {}
initialState:
documentContents: |-
const value = {
key1: "hello",
key2: "there",
};
selections:
- anchor: {line: 1, character: 10}
active: {line: 1, character: 10}
- anchor: {line: 2, character: 10}
active: {line: 2, character: 17}
finalState:
documentContents: |-
const value = {
key1: "hello",
key2: "there",
};
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 17}
- anchor: {line: 2, character: 4}
active: {line: 2, character: 17}
- anchor: {line: 1, character: 4}
active: {line: 1, character: 17}
- anchor: {line: 2, character: 4}
active: {line: 2, character: 17}
Comment on lines +28 to +35
Copy link
Member

Choose a reason for hiding this comment

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

Is this really what we want? Appears selections are duplicated

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, but has actually nothing to do with this pr. transformSelection/getNodeMatcher gets called for every selection. But might be a good idea to actually filter out duplicate selections at the end of the pipeline. You should probably put that on the task list for the new grammar.

thatMark:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 17}
- anchor: {line: 2, character: 4}
active: {line: 2, character: 17}
- anchor: {line: 1, character: 4}
active: {line: 1, character: 17}
- anchor: {line: 2, character: 4}
active: {line: 2, character: 17}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, modifier: {type: containingScope, scopeType: collectionItem, includeSiblings: true}, insideOutsideType: inside}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
spokenForm: take every key
languageId: typescript
command:
actionName: setSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: collectionKey, includeSiblings: true}
extraArgs: []
marks: {}
initialState:
documentContents: |-
const value = {
key1: "hello",
key2: "there",
};
selections:
- anchor: {line: 1, character: 10}
active: {line: 1, character: 10}
finalState:
documentContents: |-
const value = {
key1: "hello",
key2: "there",
};
selections:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 8}
- anchor: {line: 2, character: 4}
active: {line: 2, character: 8}
thatMark:
- anchor: {line: 1, character: 4}
active: {line: 1, character: 8}
- anchor: {line: 2, character: 4}
active: {line: 2, character: 8}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, modifier: {type: containingScope, scopeType: collectionKey, includeSiblings: true}, insideOutsideType: inside}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
spokenForm: take every value
languageId: typescript
command:
actionName: setSelection
partialTargets:
- type: primitive
modifier: {type: containingScope, scopeType: value, includeSiblings: true}
extraArgs: []
marks: {}
initialState:
documentContents: |-
const value = {
key1: "hello",
key2: "there",
};
selections:
- anchor: {line: 1, character: 10}
active: {line: 1, character: 10}
finalState:
documentContents: |-
const value = {
key1: "hello",
key2: "there",
};
selections:
- anchor: {line: 1, character: 10}
active: {line: 1, character: 17}
- anchor: {line: 2, character: 10}
active: {line: 2, character: 17}
thatMark:
- anchor: {line: 1, character: 10}
active: {line: 1, character: 17}
- anchor: {line: 2, character: 10}
active: {line: 2, character: 17}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, modifier: {type: containingScope, scopeType: value, includeSiblings: true}, insideOutsideType: inside}]