Skip to content

Commit

Permalink
Fix folding ranges when a line is surrounded by comments and contains…
Browse files Browse the repository at this point in the history
… code (#2954)

### Motivation
This would previously create one comment folding range from the first comment all the way to the bottom comment (5 lines in total)

Now the two comment blocks are considered by themselves.

Before             |  After
:-------------------------:|:-------------------------:
![image](https://github.com/user-attachments/assets/d2e64391-ba91-49a4-8908-3d1838b46160) ![image](https://github.com/user-attachments/assets/3033715c-5d83-43dd-83b4-e2004ed2a851)  |  ![image](https://github.com/user-attachments/assets/93b70cbb-0649-4ba5-a030-b8bd68b1f51b) ![image](https://github.com/user-attachments/assets/7afad117-710b-47ae-bed3-7bd01cba3b6c)

### Implementation

A Prism comment has a method saying wether it is preceeded by code or not. Never consider these trailing comments to be foldable.

### Automated Tests

Yes. Expectation diff:

```diff
diff --git a/test/expectations/folding_ranges/block_comments_surround.exp.json b/test/expectations/folding_ranges/block_comments_surround.exp.json
index b0264ed1..cafcbeca 100644
--- a/test/expectations/folding_ranges/block_comments_surround.exp.json
+++ b/test/expectations/folding_ranges/block_comments_surround.exp.json
@@ -12,11 +12,6 @@
     },
     {
       "startLine": 0,
-      "endLine": 1,
-      "kind": "comment"
-    },
-    {
-      "startLine": 3,
       "endLine": 4,
       "kind": "comment"
     }
```

### Manual Tests

Test code:

```rb
# Lorem
# Ipsum
foo do # Dolor
  # Sit
  # Amet
end
```
  • Loading branch information
Earlopain authored Dec 10, 2024
1 parent 7f641ba commit 896617a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/listeners/folding_ranges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def on_lambda_node_enter(node)
def push_comment_ranges
# Group comments that are on consecutive lines and then push ranges for each group that has at least 2 comments
@comments.chunk_while do |this, other|
this.location.end_line + 1 == other.location.start_line
this.location.end_line + 1 == other.location.start_line && !this.trailing? && !other.trailing?
end.each do |chunk|
next if chunk.length == 1

Expand Down
27 changes: 27 additions & 0 deletions test/expectations/folding_ranges/block_comments_surround.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"result": [
{
"startLine": 2,
"endLine": 4,
"kind": "region"
},
{
"startLine": 2,
"endLine": 4,
"kind": "region"
},
{
"startLine": 0,
"endLine": 1,
"kind": "comment"
},
{
"startLine": 3,
"endLine": 4,
"kind": "comment"
}
],
"params": [

]
}
6 changes: 6 additions & 0 deletions test/fixtures/block_comments_surround.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Lorem
# Ipsum
foo do # Dolor
# Sit
# Amet
end

0 comments on commit 896617a

Please sign in to comment.