Skip to content

Commit 02c656e

Browse files
committed
add #trim-end predicate
The same thing can be accomplished with #shrink-to-match!, but this is considerably simpler to use, and meets a common need. Suggested by Pokey during review of #1854.
1 parent 6ec84ad commit 02c656e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/cursorless-engine/src/languages/TreeSitterQuery/queryPredicateOperators.ts

+20
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ class ShrinkToMatch extends QueryPredicateOperator<ShrinkToMatch> {
150150
}
151151
}
152152

153+
/**
154+
* A predicate operator that modifies the range of the match by trimming trailing whitespace,
155+
* similar to the javascript trimEnd function.
156+
*/
157+
class TrimEnd extends QueryPredicateOperator<TrimEnd> {
158+
name = "trim-end!" as const;
159+
schema = z.tuple([q.node]);
160+
161+
run(nodeInfo: MutableQueryCapture) {
162+
const { document, range } = nodeInfo;
163+
const text = document.getText(range);
164+
const trimmed = text.trimEnd();
165+
const endOffset =
166+
document.offsetAt(range.end) + trimmed.length - text.length;
167+
nodeInfo.range = new Range(range.start, document.positionAt(endOffset));
168+
return true;
169+
}
170+
}
171+
153172
class AllowMultiple extends QueryPredicateOperator<AllowMultiple> {
154173
name = "allow-multiple!" as const;
155174
schema = z.tuple([q.node]);
@@ -179,6 +198,7 @@ class InsertionDelimiter extends QueryPredicateOperator<InsertionDelimiter> {
179198

180199
export const queryPredicateOperators = [
181200
new NotType(),
201+
new TrimEnd(),
182202
new NotEmpty(),
183203
new NotParentType(),
184204
new IsNthChild(),

queries/talon.scm

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@
113113
) @_.domain
114114
(#not-empty? @condition)
115115
(#not-empty? @_.trailing)
116-
(#shrink-to-match! @condition "^(?<keep>.*)(\s|\n|\r)+-$")
117-
(#shrink-to-match! @_.trailing "^.*(?<keep>(\s|\n|\r)+-)$")
116+
(#trim-end! @condition)
117+
(#trim-end! @_.trailing)
118118
)
119119

120120
;;!! slap: key(enter)

0 commit comments

Comments
 (0)