Skip to content

Support #start-position! and #end-position! predicates #1502

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 2 commits into from
Jun 1, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Range } from "@cursorless/common";
import z from "zod";
import { HasSchema } from "./PredicateOperatorSchemaTypes";
import { MutableQueryCapture } from "./QueryCapture";
Expand Down Expand Up @@ -45,8 +46,44 @@ class IsNthChild extends QueryPredicateOperator<IsNthChild> {
}
}

/**
* A predicate operator that modifies the range of the match to be a zero-width
* range at the start of the node. For example, `(#start-position! @foo)` will
* modify the range of the `@foo` capture to be a zero-width range at the start
* of the `@foo` node.
*/
class StartPosition extends QueryPredicateOperator<StartPosition> {
name = "start-position!" as const;
schema = z.tuple([q.node]);

run(nodeInfo: MutableQueryCapture) {
nodeInfo.range = new Range(nodeInfo.range.start, nodeInfo.range.start);

return true;
}
}

/**
* A predicate operator that modifies the range of the match to be a zero-width
* range at the end of the node. For example, `(#end-position! @foo)` will
* modify the range of the `@foo` capture to be a zero-width range at the end of
* the `@foo` node.
*/
class EndPosition extends QueryPredicateOperator<EndPosition> {
name = "end-position!" as const;
schema = z.tuple([q.node]);

run(nodeInfo: MutableQueryCapture) {
nodeInfo.range = new Range(nodeInfo.range.end, nodeInfo.range.end);

return true;
}
}

export const queryPredicateOperators: QueryPredicateOperator<HasSchema>[] = [
new NotType(),
new NotParentType(),
new IsNthChild(),
new StartPosition(),
new EndPosition(),
];