-
-
Notifications
You must be signed in to change notification settings - Fork 84
Add tree-sitter related node support #1430
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8e2a6a4
Add support for domain, leading, trailing, interior
pokey 7e51aa3
Iinitial working iteration scopes
pokey 0435e9b
change shorthand
pokey 2e45793
Cleanup
pokey b125dbf
tweak iteration scope
pokey 62f80c6
More ruby queries
pokey d106ab4
Cleanup
pokey 5b49bcb
Empty commit
pokey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
packages/cursorless-engine/src/processTargets/modifiers/getContainingScopeTarget.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { Direction, Position, TextEditor } from "@cursorless/common"; | ||
import type { Target } from "../../typings/target.types"; | ||
import { constructScopeRangeTarget } from "./constructScopeRangeTarget"; | ||
import { getContainingScope } from "./getContainingScope"; | ||
import { TargetScope } from "./scopeHandlers/scope.types"; | ||
import { ScopeHandler } from "./scopeHandlers/scopeHandler.types"; | ||
|
||
/** | ||
* Finds the containing scope of the target for the given scope handler | ||
* | ||
* @param target The target to find the containing scope of | ||
* @param scopeHandler The scope handler for the scope type to find containing scope of | ||
* @param ancestorIndex How many ancestors to go up. 0 means the immediate containing scope | ||
* @returns A target representing the containing scope, or undefined if no containing scope found | ||
*/ | ||
export function getContainingScopeTarget( | ||
target: Target, | ||
scopeHandler: ScopeHandler, | ||
ancestorIndex: number = 0, | ||
): Target | undefined { | ||
const { | ||
isReversed, | ||
editor, | ||
contentRange: { start, end }, | ||
} = target; | ||
|
||
if (end.isEqual(start)) { | ||
// Input target is empty; return the preferred scope touching target | ||
let scope = getPreferredScopeTouchingPosition(scopeHandler, editor, start); | ||
|
||
if (scope == null) { | ||
return undefined; | ||
} | ||
|
||
if (ancestorIndex > 0) { | ||
scope = expandFromPosition( | ||
scopeHandler, | ||
editor, | ||
scope.domain.end, | ||
"forward", | ||
ancestorIndex - 1, | ||
); | ||
} | ||
|
||
if (scope == null) { | ||
return undefined; | ||
} | ||
|
||
return scope.getTarget(isReversed); | ||
} | ||
|
||
const startScope = expandFromPosition( | ||
scopeHandler, | ||
editor, | ||
start, | ||
"forward", | ||
ancestorIndex, | ||
); | ||
|
||
if (startScope == null) { | ||
return undefined; | ||
} | ||
|
||
if (startScope.domain.contains(end)) { | ||
return startScope.getTarget(isReversed); | ||
} | ||
|
||
const endScope = expandFromPosition( | ||
scopeHandler, | ||
editor, | ||
end, | ||
"backward", | ||
ancestorIndex, | ||
); | ||
|
||
if (endScope == null) { | ||
return undefined; | ||
} | ||
|
||
return constructScopeRangeTarget(isReversed, startScope, endScope); | ||
} | ||
|
||
function expandFromPosition( | ||
scopeHandler: ScopeHandler, | ||
editor: TextEditor, | ||
position: Position, | ||
direction: Direction, | ||
ancestorIndex: number, | ||
): TargetScope | undefined { | ||
let nextAncestorIndex = 0; | ||
for (const scope of scopeHandler.generateScopes(editor, position, direction, { | ||
containment: "required", | ||
})) { | ||
if (nextAncestorIndex === ancestorIndex) { | ||
return scope; | ||
} | ||
|
||
// Because containment is required, and we are moving in a consistent | ||
// direction (ie forward or backward), each scope will be progressively | ||
// larger | ||
nextAncestorIndex += 1; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
function getPreferredScopeTouchingPosition( | ||
scopeHandler: ScopeHandler, | ||
editor: TextEditor, | ||
position: Position, | ||
): TargetScope | undefined { | ||
const forwardScope = getContainingScope( | ||
scopeHandler, | ||
editor, | ||
position, | ||
"forward", | ||
); | ||
|
||
if (forwardScope == null) { | ||
return getContainingScope(scopeHandler, editor, position, "backward"); | ||
} | ||
|
||
if ( | ||
scopeHandler.isPreferredOver == null || | ||
forwardScope.domain.start.isBefore(position) | ||
) { | ||
return forwardScope; | ||
} | ||
|
||
const backwardScope = getContainingScope( | ||
scopeHandler, | ||
editor, | ||
position, | ||
"backward", | ||
); | ||
|
||
// If there is no backward scope, or if the backward scope is an ancestor of | ||
// forward scope, return forward scope | ||
if ( | ||
backwardScope == null || | ||
backwardScope.domain.contains(forwardScope.domain) | ||
) { | ||
return forwardScope; | ||
} | ||
|
||
return scopeHandler.isPreferredOver(backwardScope, forwardScope) ?? false | ||
? backwardScope | ||
: forwardScope; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Factored out the core implementation of
ContainingScopeStage
so thatEveryScopeStage
could use it directly for expanding to containing iteration scope, because iteration scopes don't necessarily have a well-defined scope type, so it's easier if it can just pass in a scope handler rather than making a dummy scope type just for this purpose