From 73e9543eb796f06f3af4eae4304fe892a3fbea62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Szabo?= Date: Sat, 16 Mar 2024 02:15:06 -0300 Subject: [PATCH] Fixed folds for Ruby --- .../grammars/tree-sitter-ruby/folds.scm | 17 +++- packages/language-ruby/spec/fixtures/folds.rb | 82 +++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) diff --git a/packages/language-ruby/grammars/tree-sitter-ruby/folds.scm b/packages/language-ruby/grammars/tree-sitter-ruby/folds.scm index 99810d788..32044a611 100644 --- a/packages/language-ruby/grammars/tree-sitter-ruby/folds.scm +++ b/packages/language-ruby/grammars/tree-sitter-ruby/folds.scm @@ -1,13 +1,16 @@ [ (method) + (comment) (singleton_method) (class) (module) (case) (do_block) + (block) (singleton_class) (lambda) (hash) + (argument_list) (array) ] @fold @@ -15,14 +18,19 @@ ; Fold from `if` to the next `elsif` or `else` in the chain. ((if alternative: [(elsif) (else)]) @fold - (#set! fold.endAt firstNamedChild.nextNamedSibling.nextNamedSibling.startPosition) - (#set! fold.adjustToEndOfPreviousLine true)) + (#set! fold.endAt lastNamedChild.startPosition) + (#set! fold.adjustToEndOfPreviousRow true)) + +((unless + alternative: (else)) @fold + (#set! fold.endAt lastNamedChild.startPosition) + (#set! fold.adjustToEndOfPreviousRow true)) ; Fold from `elsif` to the next `elsif` or `else` in the chain. ((elsif consequence: [(then) (elsif)]) @fold - (#set! fold.endAt firstNamedChild.nextNamedSibling.nextNamedSibling.startPosition) - (#set! fold.adjustToEndOfPreviousLine true)) + (#set! fold.endAt lastNamedChild.startPosition) + (#set! fold.adjustToEndOfPreviousRow true)) ; Fold from `else` to `end`. ((else) @fold @@ -30,3 +38,4 @@ ; A bare `if` without an `else` or `elsif`. (if) @fold +(unless) @fold diff --git a/packages/language-ruby/spec/fixtures/folds.rb b/packages/language-ruby/spec/fixtures/folds.rb index 977407a65..c66bf47ab 100644 --- a/packages/language-ruby/spec/fixtures/folds.rb +++ b/packages/language-ruby/spec/fixtures/folds.rb @@ -1,6 +1,19 @@ class Car < Vehicle # <- fold_begin.class # ^ fold_new_position.class + + class << self + # <- fold_begin.singleton_class + # ^ fold_new_position.singleton_class + end + # <- fold_end.singleton_class + + def self.something + # <- fold_begin.singleton + # ^ fold_new_position.singleton + end + # <- fold_end.singleton + def init(id) # <- fold_begin.method # ^ fold_new_position.method @@ -21,3 +34,72 @@ class Car < Vehicle # <- fold_end.method end # <- fold_end.class + +if something + # <- fold_begin.if + # ^ fold_new_position.if + do_other_thing( + ) +end +# <- fold_end.if + +# if something +# # ^ fold_begin.if_else +# do_other_thing() +# # <- fold_new_position.if_else +# else +# # <- fold_end.if_else +# # # <- fold_begin.else +# # # ^ fold_new_position.else +# do_another() +# end +# # # <- fold_end.else + +unless something + # ^ fold_begin.unless + # ^ fold_new_position.unless + do_other_thing() +end +# <- fold_end.unless + +# unless something +# # ^ fold_begin.unless_with_else +# # ^ fold_new_position.unless_with_else +# do_other_thing() +# else +# # <- fold_end.unless_with_else +# # <- fold_begin.unless_else +# # ^ fold_new_position.unless_else +# we_should_never_do_this() +# end +# # <- fold_end.unless_else + +call_something do + # ^ fold_begin.do_block + # ^ fold_new_position.do_block + a +end +# <- fold_end.do_block + +call_something { + # ^ fold_begin.inline_block + # ^ fold_new_position.inline_block +} +# <- fold_end.inline_block + +multiline_call( + # ^ fold_begin.call + # ^ fold_new_position.call + 10, + 20 +) +# <- fold_end.call + +=begin +# <- fold_begin.multi_comment +# ^ fold_new_position.multi_comment + a + b + c +=end +# <- fold_end.multi_comment