Skip to content

Commit 0b037a4

Browse files
Update go if scope (#3168)
Fixed incorrect removal range and condition domain for if scope in go
1 parent 7e7c17a commit 0b037a4

4 files changed

Lines changed: 27 additions & 39 deletions

File tree

data/fixtures/recorded/languages/go/chuckBranch.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ initialState:
1717
active: {line: 0, character: 0}
1818
marks: {}
1919
finalState:
20-
documentContents: "else if y {\n\ty++\n} else {\n\tx--\n}"
20+
documentContents: "if y {\n\ty++\n} else {\n\tx--\n}"
2121
selections:
2222
- anchor: {line: 0, character: 0}
2323
active: {line: 0, character: 0}

data/fixtures/scopes/go/branch/branch.if.elif.else.scope

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ if true {
1111
1| } else if false {
1212
-<
1313

14-
[#1 Removal] = 0:0-1:2
14+
[#1 Removal] = 0:0-1:7
1515
>---------
1616
0| if true {
1717
1| } else if false {
18-
--<
18+
-------<
1919

2020
[#1 Trailing delimiter] = 1:1-1:2
2121
>-<

data/fixtures/scopes/go/condition/condition.if.scope

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ if true {
4747
>-<
4848
1| } else if false {
4949

50-
[#2 Domain] = 1:7-3:1
51-
>----------
50+
[#2 Domain] = 1:2-2:1
51+
>---------------
5252
1| } else if false {
5353
2| } else {
54-
3| }
5554
-<
5655

5756
[#2 Insertion delimiter] = " "

queries/go.scm

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -251,37 +251,43 @@
251251
"}" @interior.end.startOf @branch.iteration.end.startOf @condition.iteration.end.startOf
252252
) @value.domain
253253

254-
;; The outermost if statement
254+
;;!! if () {} else {}
255255
(
256256
(if_statement) @ifStatement @statement @branch.iteration
257257
(#not-parent-type? @ifStatement if_statement)
258258
)
259259

260-
;; first if in an if-else chain
260+
;;!! if () {}
261261
(
262262
(if_statement
263-
consequence: (block) @branch.end.endOf
264-
) @branch.start.startOf
265-
(#not-parent-type? @branch.start.startOf if_statement)
266-
(#insertion-delimiter! @branch.start.startOf " ")
263+
"if" @branch.start @branch.removal.start
264+
condition: (_) @condition
265+
consequence: (_) @branch.end @branch.removal.end
266+
"else"? @branch.removal.end.startOf
267+
alternative: (if_statement)? @branch.removal.end.startOf
268+
) @condition.domain
269+
(#not-parent-type? @condition.domain if_statement)
270+
(#insertion-delimiter! @branch.start " ")
271+
(#shrink-to-match! @condition "^\\(?(?<keep>[^)]*)\\)?$")
267272
)
268273

269-
;; internal if in an if-else chain
274+
;;!! else if () {}
270275
(if_statement
271-
"else" @branch.start
276+
"else" @branch.start @condition.domain.start
272277
alternative: (if_statement
273-
consequence: (block) @branch.end
278+
condition: (_) @condition
279+
consequence: (_) @branch.end @condition.domain.end
274280
)
275281
(#insertion-delimiter! @branch.start " ")
282+
(#shrink-to-match! @condition "^\\(?(?<keep>[^)]*)\\)?$")
276283
)
277284

278-
;; final else branch in an if-else chain
279-
(
280-
(if_statement
281-
"else" @branch.start.startOf
282-
alternative: (block)
283-
) @branch.end.endOf
284-
(#insertion-delimiter! @branch.start.startOf " ")
285+
;;!! else {}
286+
287+
(if_statement
288+
"else" @branch.start
289+
alternative: (block) @branch.end
290+
(#insertion-delimiter! @branch.start " ")
285291
)
286292

287293
[
@@ -356,23 +362,6 @@
356362
result: (_) @type
357363
) @_.domain
358364

359-
;;!! if true {}
360-
(
361-
(_
362-
condition: (_) @condition
363-
) @_.domain
364-
(#not-type? @condition parenthesized_expression)
365-
(#not-type? @_.domain for_clause)
366-
)
367-
368-
;;!! if (true) {}
369-
(
370-
(_
371-
condition: (parenthesized_expression) @condition
372-
) @_.domain
373-
(#child-range! @condition 0 -1 true true)
374-
)
375-
376365
;;!! for i := 0; i < size; i++ {}
377366
;;! ^^^^^^^^
378367
(for_statement

0 commit comments

Comments
 (0)