Skip to content

Commit 7811c41

Browse files
Merge pull request #956 from pulsar-edit/tree-sitter-ruby-folds
Fixed folds for Ruby
2 parents 1481d39 + ab2df0e commit 7811c41

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
## [Unreleased]
88

9+
- Fixed some folds in Ruby like `unless`, some blocks, multiline comments, function calls, and different array syntaxes for strings and keywords.
10+
911
## 1.114.0
1012

1113
- Prevented an exception raised in the command palette in certain unusual filtering scenarios.
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
[
22
(method)
3+
(comment)
34
(singleton_method)
45
(class)
56
(module)
67
(case)
78
(do_block)
9+
(block)
810
(singleton_class)
911
(lambda)
1012
(hash)
13+
(argument_list)
1114
(array)
12-
(string_array)
1315
(symbol_array)
16+
(string_array)
1417
] @fold
1518

1619

1720
; Fold from `if` to the next `elsif` or `else` in the chain.
1821
((if
1922
alternative: [(elsif) (else)]) @fold
20-
(#set! fold.endAt firstNamedChild.nextNamedSibling.nextNamedSibling.startPosition)
21-
(#set! fold.adjustToEndOfPreviousLine true))
23+
(#set! fold.endAt lastNamedChild.startPosition)
24+
(#set! fold.adjustToEndOfPreviousRow true))
25+
26+
((unless
27+
alternative: (else)) @fold
28+
(#set! fold.endAt lastNamedChild.startPosition)
29+
(#set! fold.adjustToEndOfPreviousRow true))
2230

2331
; Fold from `elsif` to the next `elsif` or `else` in the chain.
2432
((elsif
2533
consequence: [(then) (elsif)]) @fold
26-
(#set! fold.endAt firstNamedChild.nextNamedSibling.nextNamedSibling.startPosition)
27-
(#set! fold.adjustToEndOfPreviousLine true))
34+
(#set! fold.endAt lastNamedChild.startPosition)
35+
(#set! fold.adjustToEndOfPreviousRow true))
2836

2937
; Fold from `else` to `end`.
3038
((else) @fold
3139
(#set! fold.endAt endPosition))
3240

3341
; A bare `if` without an `else` or `elsif`.
3442
(if) @fold
43+
(unless) @fold

packages/language-ruby/spec/fixtures/folds.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
class Car < Vehicle
22
# <- fold_begin.class
33
# ^ fold_new_position.class
4+
5+
class << self
6+
# <- fold_begin.singleton_class
7+
# ^ fold_new_position.singleton_class
8+
end
9+
# <- fold_end.singleton_class
10+
11+
def self.something
12+
# <- fold_begin.singleton
13+
# ^ fold_new_position.singleton
14+
end
15+
# <- fold_end.singleton
16+
417
def init(id)
518
# <- fold_begin.method
619
# ^ fold_new_position.method
@@ -21,3 +34,72 @@ def init(id)
2134
# <- fold_end.method
2235
end
2336
# <- fold_end.class
37+
38+
if something
39+
# <- fold_begin.if
40+
# ^ fold_new_position.if
41+
do_other_thing(
42+
)
43+
end
44+
# <- fold_end.if
45+
46+
# if something
47+
# # ^ fold_begin.if_else
48+
# do_other_thing()
49+
# # <- fold_new_position.if_else
50+
# else
51+
# # <- fold_end.if_else
52+
# # # <- fold_begin.else
53+
# # # ^ fold_new_position.else
54+
# do_another()
55+
# end
56+
# # # <- fold_end.else
57+
58+
unless something
59+
# ^ fold_begin.unless
60+
# ^ fold_new_position.unless
61+
do_other_thing()
62+
end
63+
# <- fold_end.unless
64+
65+
# unless something
66+
# # ^ fold_begin.unless_with_else
67+
# # ^ fold_new_position.unless_with_else
68+
# do_other_thing()
69+
# else
70+
# # <- fold_end.unless_with_else
71+
# # <- fold_begin.unless_else
72+
# # ^ fold_new_position.unless_else
73+
# we_should_never_do_this()
74+
# end
75+
# # <- fold_end.unless_else
76+
77+
call_something do
78+
# ^ fold_begin.do_block
79+
# ^ fold_new_position.do_block
80+
a
81+
end
82+
# <- fold_end.do_block
83+
84+
call_something {
85+
# ^ fold_begin.inline_block
86+
# ^ fold_new_position.inline_block
87+
}
88+
# <- fold_end.inline_block
89+
90+
multiline_call(
91+
# ^ fold_begin.call
92+
# ^ fold_new_position.call
93+
10,
94+
20
95+
)
96+
# <- fold_end.call
97+
98+
=begin
99+
# <- fold_begin.multi_comment
100+
# ^ fold_new_position.multi_comment
101+
a
102+
b
103+
c
104+
=end
105+
# <- fold_end.multi_comment

0 commit comments

Comments
 (0)