Skip to content

Commit 53aedd7

Browse files
Fix bug with go text fragment (#2472)
When using these multiline strings in go ``` `foo` ``` we get a node of type `raw_string_literal` which have no children so the `child-range` predicate ran into an undefined at runtime problem. Fixes #2458 ## Checklist - [x] 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
1 parent 8c246a9 commit 53aedd7

File tree

6 files changed

+63
-4
lines changed

6 files changed

+63
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// foo
2+
---
3+
4+
[Content] =
5+
[Removal] =
6+
[Domain] = 0:0-0:6
7+
>------<
8+
0| // foo
9+
10+
[Insertion delimiter] = " "
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
`
2+
foo
3+
`
4+
---
5+
6+
[Content] =
7+
[Removal] =
8+
[Domain] = 0:1-2:0
9+
>
10+
0| `
11+
1| foo
12+
2| `
13+
<
14+
15+
[Insertion delimiter] = " "
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"foo"
2+
---
3+
4+
[Content] =
5+
[Removal] =
6+
[Domain] = 0:1-0:4
7+
>---<
8+
0| "foo"
9+
10+
[Insertion delimiter] = " "

packages/common/src/scopeSupportFacets/go.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ const { supported, unsupported, notApplicable } = ScopeSupportFacetLevel;
88

99
export const goScopeSupport: LanguageScopeSupportFacetMap = {
1010
"comment.line": supported,
11+
12+
"textFragment.string.singleLine": supported,
13+
"textFragment.string.multiLine": supported,
14+
"textFragment.comment.line": supported,
1115
};

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ class ChildRange extends QueryPredicateOperator<ChildRange> {
9898
}
9999
}
100100

101+
class CharacterRange extends QueryPredicateOperator<CharacterRange> {
102+
name = "character-range!" as const;
103+
schema = z.union([
104+
z.tuple([q.node, q.integer]),
105+
z.tuple([q.node, q.integer, q.integer]),
106+
]);
107+
108+
run(nodeInfo: MutableQueryCapture, startOffset: number, endOffset?: number) {
109+
nodeInfo.range = new Range(
110+
nodeInfo.range.start.translate(undefined, startOffset),
111+
nodeInfo.range.end.translate(undefined, endOffset ?? 0),
112+
);
113+
114+
return true;
115+
}
116+
}
117+
101118
/**
102119
* A predicate operator that modifies the range of the match to shrink to regex
103120
* match. For example, `(#shrink-to-match! @foo "\\S+")` will modify the range
@@ -252,6 +269,7 @@ export const queryPredicateOperators = [
252269
new NotParentType(),
253270
new IsNthChild(),
254271
new ChildRange(),
272+
new CharacterRange(),
255273
new ShrinkToMatch(),
256274
new AllowMultiple(),
257275
new InsertionDelimiter(),

queries/go.scm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
] @statement
3131

3232
(
33-
[
34-
(interpreted_string_literal)
35-
(raw_string_literal)
36-
] @string @textFragment
33+
(interpreted_string_literal) @string @textFragment
3734
(#child-range! @textFragment 0 -1 true true)
3835
)
3936

37+
(
38+
(raw_string_literal) @string @textFragment
39+
(#character-range! @textFragment 1 -1)
40+
)
41+
4042
(comment) @comment @textFragment
4143

4244
;; What should map and list refer to in Go programs?

0 commit comments

Comments
 (0)