Skip to content

Commit ccc49a2

Browse files
AndreasArvidssonpokeypre-commit-ci-lite[bot]
authored
Terser leading/trailing syntax in scm (#2113)
Fixes #2108 ## Checklist - [-] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [-] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [-] I have not broken the cheatsheet --------- Co-authored-by: Pokey Rule <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 40a6fee commit ccc49a2

File tree

13 files changed

+115
-117
lines changed

13 files changed

+115
-117
lines changed

packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/TreeSitterScopeHandler/TreeSitterScopeHandler.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Range, SimpleScopeType, TextEditor } from "@cursorless/common";
1+
import { SimpleScopeType, TextEditor } from "@cursorless/common";
22
import { TreeSitterQuery } from "../../../../languages/TreeSitterQuery";
33
import { QueryMatch } from "../../../../languages/TreeSitterQuery/QueryCapture";
44
import { ScopeTypeTarget } from "../../../targets/ScopeTypeTarget";
@@ -55,31 +55,33 @@ export class TreeSitterScopeHandler extends BaseTreeSitterScopeHandler {
5555

5656
const removalRange = getRelatedRange(match, scopeTypeType, "removal", true);
5757

58-
const leadingDelimiterRange = dropEmptyRange(
59-
getRelatedRange(match, scopeTypeType, "leading", true),
60-
);
61-
62-
const trailingDelimiterRange = dropEmptyRange(
63-
getRelatedRange(match, scopeTypeType, "trailing", true),
64-
);
65-
6658
const interiorRange = getRelatedRange(
6759
match,
6860
scopeTypeType,
6961
"interior",
7062
true,
7163
);
7264

73-
const rawPrefixRange = getRelatedRange(
65+
const prefixRange = getRelatedRange(
7466
match,
7567
scopeTypeType,
7668
"prefix",
7769
true,
78-
);
79-
const prefixRange =
80-
rawPrefixRange != null
81-
? new Range(rawPrefixRange.start, contentRange.start)
82-
: undefined;
70+
)?.with(undefined, contentRange.start);
71+
72+
const leadingDelimiterRange = getRelatedRange(
73+
match,
74+
scopeTypeType,
75+
"leading",
76+
true,
77+
)?.with(undefined, prefixRange?.start ?? contentRange.start);
78+
79+
const trailingDelimiterRange = getRelatedRange(
80+
match,
81+
scopeTypeType,
82+
"trailing",
83+
true,
84+
)?.with(contentRange.end);
8385

8486
return {
8587
editor,
@@ -102,7 +104,3 @@ export class TreeSitterScopeHandler extends BaseTreeSitterScopeHandler {
102104
};
103105
}
104106
}
105-
106-
function dropEmptyRange(range?: Range) {
107-
return range != null && !range.isEmpty ? range : undefined;
108-
}

queries/html.scm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@
1919
;;!! <aaa id="me">
2020
;;! ^^
2121
(attribute
22-
(attribute_name) @collectionKey @collectionKey.trailing.start.endOf
22+
(attribute_name) @collectionKey
2323
[
2424
(quoted_attribute_value)
2525
(attribute_value)
26-
] ? @collectionKey.trailing.end.startOf
26+
] ? @collectionKey.trailing.startOf
2727
) @_.domain
2828

2929
;;!! <aaa value=2>
3030
;;! ^
3131
;;!! <aaa id="me">
3232
;;! ^^^^
3333
(attribute
34-
(attribute_name) @value.leading.start.endOf
34+
(attribute_name) @value.leading.endOf
3535
[
3636
(quoted_attribute_value)
3737
(attribute_value)
38-
] @value @value.leading.end.startOf
38+
] @value
3939
) @value.domain
4040

4141
;;!! <aaa id="me">

queries/java.scm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,14 @@
288288
;;! --------------
289289
(local_variable_declaration
290290
(variable_declarator
291-
name: (_) @name @name.trailing.start.endOf @value.leading.start.endOf
292-
value: (_)? @value @name.trailing.end.startOf @value.leading.end.startOf
291+
name: (_) @name @value.leading.endOf
292+
value: (_)? @value @name.trailing.startOf
293293
)
294294
) @_.domain
295295
(field_declaration
296296
(variable_declarator
297-
name: (_) @name @name.trailing.start.endOf @value.leading.start.endOf
298-
value: (_)? @value @name.trailing.end.startOf @value.leading.end.startOf
297+
name: (_) @name @value.leading.endOf
298+
value: (_)? @value @name.trailing.startOf
299299
)
300300
) @_.domain
301301

@@ -305,8 +305,8 @@
305305
;;! ----------
306306
(_
307307
(assignment_expression
308-
left: (_) @name @name.trailing.start.endOf @value.leading.start.endOf
309-
right: (_) @value @name.trailing.end.startOf @value.leading.end.startOf
308+
left: (_) @name @value.leading.endOf
309+
right: (_) @value @name.trailing.startOf
310310
) @_.domain.start
311311
";"? @_.domain.end
312312
)

queries/javascript.core.scm

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@
9999
(variable_declarator)
100100
.
101101
(variable_declarator
102-
name: (_) @name @name.trailing.start.endOf
103-
value: (_)? @name.trailing.end.startOf
102+
name: (_) @name
103+
value: (_)? @name.trailing.startOf
104104
)
105105
)
106106

@@ -120,8 +120,8 @@
120120
(variable_declarator)
121121
.
122122
(variable_declarator
123-
name: (_) @name @name.trailing.start.endOf
124-
value: (_)? @name.trailing.end.startOf
123+
name: (_) @name
124+
value: (_)? @name.trailing.startOf
125125
)
126126
)
127127
] @_.domain
@@ -147,8 +147,8 @@
147147
;;! xxxx
148148
;;! -----------------------------------
149149
(variable_declarator
150-
name: (_) @name @name.trailing.start.endOf
151-
value: (_)? @name.trailing.end.startOf
150+
name: (_) @name
151+
value: (_)? @name.trailing.startOf
152152
)
153153
)
154154
) @_.domain
@@ -187,8 +187,8 @@
187187
(variable_declarator)
188188
.
189189
(variable_declarator
190-
name: (_) @name @name.trailing.start.endOf
191-
value: (_)? @name.trailing.end.startOf
190+
name: (_) @name
191+
value: (_)? @name.trailing.startOf
192192
) @_.domain
193193
)
194194

@@ -203,9 +203,9 @@
203203
;;! ---------------------------
204204
(lexical_declaration
205205
(variable_declarator
206-
(_) @value.leading.start.endOf
206+
(_) @value.leading.endOf
207207
.
208-
value: (_)? @value @value.leading.end.startOf
208+
value: (_)? @value
209209
)
210210
)
211211

@@ -217,9 +217,9 @@
217217
;; of https://github.com/tree-sitter/tree-sitter/issues/1442#issuecomment-1584628651
218218
(variable_declaration
219219
(variable_declarator
220-
(_) @value.leading.start.endOf
220+
(_) @value.leading.endOf
221221
.
222-
value: (_)? @value @value.leading.end.startOf
222+
value: (_)? @value
223223
)
224224
)
225225
] @_.domain
@@ -239,9 +239,9 @@
239239
;;! xxxx
240240
;;! ----------------------------------------
241241
(variable_declarator
242-
(_) @value.leading.start.endOf
242+
(_) @value.leading.endOf
243243
.
244-
value: (_)? @value @value.leading.end.startOf
244+
value: (_)? @value
245245
)
246246
)
247247
) @_.domain
@@ -262,9 +262,9 @@
262262
(
263263
(_
264264
(variable_declarator
265-
(_) @value.leading.start.endOf
265+
(_) @value.leading.endOf
266266
.
267-
value: (_)? @value @value.leading.end.startOf
267+
value: (_)? @value
268268
) @_.domain
269269
) @dummy
270270
(#has-multiple-children-of-type? @dummy variable_declarator)
@@ -283,8 +283,8 @@
283283
;;! xxxx
284284
;;! --------
285285
(assignment_expression
286-
left: (_) @name @value.leading.start.endOf @name.trailing.start.endOf
287-
right: (_) @value @name.trailing.end.startOf @value.leading.end.startOf
286+
left: (_) @name @value.leading.endOf
287+
right: (_) @value @name.trailing.startOf
288288
)
289289

290290
;; name:
@@ -298,8 +298,8 @@
298298
;;! xxxxx
299299
;;! ---------
300300
(augmented_assignment_expression
301-
left: (_) @name @value.leading.start.endOf @name.trailing.start.endOf
302-
right: (_) @value @name.trailing.end.startOf @value.leading.end.startOf
301+
left: (_) @name @value.leading.endOf
302+
right: (_) @value @name.trailing.startOf
303303
)
304304
]
305305
) @_.domain
@@ -323,8 +323,8 @@
323323
;;!2 xxxx
324324
;;!2 -------
325325
(assignment_expression
326-
left: (_) @name @value.leading.start.endOf @name.trailing.start.endOf
327-
right: (_) @value @name.trailing.end.startOf @value.leading.end.startOf
326+
left: (_) @name @value.leading.endOf
327+
right: (_) @value @name.trailing.startOf
328328
)
329329

330330
;; name:
@@ -344,8 +344,8 @@
344344
;;!2 xxxxx
345345
;;!2 --------
346346
(augmented_assignment_expression
347-
left: (_) @name @value.leading.start.endOf @name.trailing.start.endOf
348-
right: (_) @value @name.trailing.end.startOf @value.leading.end.startOf
347+
left: (_) @name @value.leading.endOf
348+
right: (_) @value @name.trailing.startOf
349349
)
350350
] @_.domain
351351

@@ -420,8 +420,8 @@
420420
;;! ^^^
421421
;;! --------
422422
(pair_pattern
423-
key: (_) @collectionKey @collectionKey.trailing.start.endOf @value.leading.start.endOf
424-
value: (_) @value @collectionKey.trailing.end.startOf @value.leading.end.startOf
423+
key: (_) @collectionKey @value.leading.endOf
424+
value: (_) @value @collectionKey.trailing.startOf
425425
) @_.domain
426426

427427
;;!! "string"
@@ -640,8 +640,8 @@
640640
;;! xxx
641641
;;! --------
642642
(pair
643-
key: (_) @collectionKey @collectionKey.trailing.start.endOf @value.leading.start.endOf
644-
value: (_) @value @collectionKey.trailing.end.startOf @value.leading.end.startOf
643+
key: (_) @collectionKey @value.leading.endOf
644+
value: (_) @value @collectionKey.trailing.startOf
645645
) @_.domain
646646

647647
;; Statements that are not a child of an export statement
@@ -685,11 +685,11 @@
685685
;;! ^^^^^^^^^^^^
686686
(
687687
(formal_parameters
688-
(_)? @_.leading.start.endOf
688+
(_)? @_.leading.endOf
689689
.
690-
(_) @argumentOrParameter @_.leading.end.startOf @_.trailing.start.endOf
690+
(_) @argumentOrParameter
691691
.
692-
(_)? @_.trailing.end.startOf
692+
(_)? @_.trailing.startOf
693693
) @dummy
694694
(#not-type? @argumentOrParameter "comment")
695695
(#single-or-multi-line-delimiter! @argumentOrParameter @dummy ", " ",\n")
@@ -699,11 +699,11 @@
699699
;;! ^^^^^
700700
(
701701
(arguments
702-
(_)? @_.leading.start.endOf
702+
(_)? @_.leading.endOf
703703
.
704-
(_) @argumentOrParameter @_.leading.end.startOf @_.trailing.start.endOf
704+
(_) @argumentOrParameter
705705
.
706-
(_)? @_.trailing.end.startOf
706+
(_)? @_.trailing.startOf
707707
) @dummy
708708
(#not-type? @argumentOrParameter "comment")
709709
(#single-or-multi-line-delimiter! @argumentOrParameter @dummy ", " ",\n")

queries/javascript.jsx.scm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@
9797
;;! xxxxxx
9898
;;! ---------
9999
(jsx_attribute
100-
(_) @value.leading.start.endOf
101-
(_) @value @value.leading.end.startOf
100+
(_) @_.leading.endOf
101+
(_) @value
102102
) @_.domain
103103

104104
;;!! <aaa />

queries/json.scm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
;;! ^^^^^^^ ^
1919
;;! ----------
2020
(pair
21-
key: (_) @collectionKey @collectionKey.trailing.start.endOf @value.leading.start.endOf
22-
value: (_) @value @collectionKey.trailing.end.startOf @value.leading.end.startOf
21+
key: (_) @collectionKey @value.leading.endOf
22+
value: (_) @value @collectionKey.trailing.startOf
2323
) @_.domain
2424

2525
;;!! {"bbb": 0, "ccc": 0}

queries/markdown.scm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@
2424
(list
2525
(list_item
2626
(paragraph
27-
(inline) @_.leading.start.endOf
27+
(inline) @_.leading.endOf
2828
)
2929
)?
3030
.
3131
(list_item
3232
(_) @_.prefix
3333
(paragraph
34-
(inline) @collectionItem @_.trailing.start.endOf
34+
(inline) @collectionItem
3535
)
36-
) @_.domain @_.leading.end.startOf
36+
) @_.domain
3737
.
38-
(list_item)? @_.trailing.end.startOf
38+
(list_item)? @_.trailing.startOf
3939
(#trim-end! @_.domain)
4040
(#insertion-delimiter! @collectionItem "\n")
4141
)

0 commit comments

Comments
 (0)