File tree 2 files changed +22
-2
lines changed
packages/cursorless-engine/src/languages/TreeSitterQuery
2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,25 @@ class ShrinkToMatch extends QueryPredicateOperator<ShrinkToMatch> {
150
150
}
151
151
}
152
152
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
+
153
172
class AllowMultiple extends QueryPredicateOperator < AllowMultiple > {
154
173
name = "allow-multiple!" as const ;
155
174
schema = z . tuple ( [ q . node ] ) ;
@@ -179,6 +198,7 @@ class InsertionDelimiter extends QueryPredicateOperator<InsertionDelimiter> {
179
198
180
199
export const queryPredicateOperators = [
181
200
new NotType ( ) ,
201
+ new TrimEnd ( ) ,
182
202
new NotEmpty ( ) ,
183
203
new NotParentType ( ) ,
184
204
new IsNthChild ( ) ,
Original file line number Diff line number Diff line change 113
113
) @_.domain
114
114
(#not-empty? @condition)
115
115
(#not-empty? @_.trailing)
116
- (#shrink-to-match ! @condition " ^(?<keep>.*)( \s | \n | \r )+-$ " )
117
- (#shrink-to-match ! @_.trailing " ^.*(?<keep>( \s | \n | \r )+-)$ " )
116
+ (#t rim-end ! @condition)
117
+ (#t rim-end ! @_.trailing)
118
118
)
119
119
120
120
; ;!! slap: key(enter)
You can’t perform that action at this time.
0 commit comments