Skip to content

Commit 56944c1

Browse files
committed
Add hover documentation for 'case' keyword
1 parent 64a03e9 commit 56944c1

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

lib/ruby_lsp/listeners/hover.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Hover
88

99
ALLOWED_TARGETS = [
1010
Prism::CallNode,
11+
Prism::CaseNode,
1112
Prism::ConstantReadNode,
1213
Prism::ConstantWriteNode,
1314
Prism::ConstantPathNode,
@@ -54,6 +55,7 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so
5455

5556
dispatcher.register(
5657
self,
58+
:on_case_node_enter,
5759
:on_constant_read_node_enter,
5860
:on_constant_write_node_enter,
5961
:on_constant_path_node_enter,
@@ -97,6 +99,11 @@ def on_string_node_enter(node)
9799
generate_heredoc_hover(node)
98100
end
99101

102+
#: (Prism::CaseNode node) -> void
103+
def on_case_node_enter(node)
104+
handle_keyword_documentation(node.case_keyword)
105+
end
106+
100107
#: (Prism::InterpolatedStringNode node) -> void
101108
def on_interpolated_string_node_enter(node)
102109
generate_heredoc_hover(node)

lib/ruby_lsp/static_docs.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module RubyLsp
1414

1515
# A map of keyword => short documentation to be displayed on hover or completion
1616
KEYWORD_DOCS = {
17+
"case" => "Starts a case expression for pattern matching or multiple condition checking",
1718
"yield" => "Invokes the passed block with the given arguments",
1819
}.freeze #: Hash[String, String]
1920
end

static_docs/case.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Case Statement
2+
3+
In Ruby, the `case` statement provides a clean way to express conditional logic when you need to compare a value against multiple conditions. It's similar to if/else chains but often more readable and concise.
4+
5+
```ruby
6+
# Basic case statement comparing a value against multiple conditions
7+
grade = "A"
8+
9+
case grade
10+
when "A"
11+
puts "Excellent!"
12+
when "B"
13+
puts "Good job!"
14+
when "C"
15+
puts "Fair"
16+
else
17+
puts "Need improvement"
18+
end
19+
```
20+
21+
The `case` statement can also work with ranges, multiple values, and even custom matching using the `===` operator.
22+
23+
```ruby
24+
# Case statement with ranges and multiple conditions
25+
score = 85
26+
27+
case score
28+
when 90..100
29+
puts "A grade"
30+
when 80..89
31+
puts "B grade"
32+
when 70..79
33+
puts "C grade"
34+
else
35+
puts "Need improvement"
36+
end
37+
38+
# Case with multiple values in a single when clause
39+
day = "Saturday"
40+
41+
case day
42+
when "Saturday", "Sunday"
43+
puts "Weekend!"
44+
else
45+
puts "Weekday"
46+
end
47+
```
48+
49+
## Pattern Matching (Ruby 2.7+)
50+
51+
Starting from Ruby 2.7, `case` statements support pattern matching, which provides powerful ways to match and destructure data.
52+
53+
```ruby
54+
# Pattern matching with arrays
55+
data = [1, 2, 3]
56+
57+
case data
58+
when [1, 2, 3]
59+
puts "Exact match!"
60+
when [1, *rest]
61+
puts "Starts with 1, followed by #{rest}"
62+
when Array
63+
puts "Any array"
64+
else
65+
puts "Not an array"
66+
end
67+
68+
# Pattern matching with hashes (Ruby 3.0+)
69+
user = { name: "Alice", age: 30 }
70+
71+
case user
72+
in { name: "Alice", age: }
73+
puts "Alice is #{age} years old"
74+
in { name:, age: 20.. }
75+
puts "#{name} is at least 20"
76+
else
77+
puts "No match"
78+
end
79+
```
80+
81+
## Case without an argument
82+
83+
Ruby also allows `case` statements without an explicit argument, which acts like a series of if/elsif conditions.
84+
85+
```ruby
86+
# Case statement without an argument
87+
# rubocop:disable Style/EmptyCaseCondition
88+
case
89+
when Time.now.saturday?
90+
puts "It's Saturday!"
91+
when Time.now.sunday?
92+
puts "It's Sunday!"
93+
else
94+
puts "It's a weekday"
95+
end
96+
# rubocop:enable Style/EmptyCaseCondition
97+
```
98+
99+
The case statement is particularly useful when you have multiple conditions to check against a single value, or when you want to use pattern matching to destructure complex data structures.

0 commit comments

Comments
 (0)