Skip to content

Commit 44f3504

Browse files
committed
Add 'break' keyword
1 parent 56944c1 commit 44f3504

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

lib/ruby_lsp/listeners/hover.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Hover
77
include Requests::Support::Common
88

99
ALLOWED_TARGETS = [
10+
Prism::BreakNode,
1011
Prism::CallNode,
1112
Prism::CaseNode,
1213
Prism::ConstantReadNode,
@@ -55,6 +56,7 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so
5556

5657
dispatcher.register(
5758
self,
59+
:on_break_node_enter,
5860
:on_case_node_enter,
5961
:on_constant_read_node_enter,
6062
:on_constant_write_node_enter,
@@ -99,6 +101,11 @@ def on_string_node_enter(node)
99101
generate_heredoc_hover(node)
100102
end
101103

104+
#: (Prism::BreakNode node) -> void
105+
def on_break_node_enter(node)
106+
handle_keyword_documentation(node.keyword)
107+
end
108+
102109
#: (Prism::CaseNode node) -> void
103110
def on_case_node_enter(node)
104111
handle_keyword_documentation(node.case_keyword)

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+
"break" => "Terminates the execution of a block, loop, or method",
1718
"case" => "Starts a case expression for pattern matching or multiple condition checking",
1819
"yield" => "Invokes the passed block with the given arguments",
1920
}.freeze #: Hash[String, String]

static_docs/break.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Break
2+
3+
In Ruby, the `break` keyword is used to exit a loop or block prematurely. Unlike `next` which skips to the next iteration, `break` terminates the loop entirely and continues with the code after the loop.
4+
5+
```ruby
6+
# Basic break usage in a loop
7+
5.times do |i|
8+
break if i == 3
9+
10+
puts i
11+
end
12+
# Output:
13+
# 0
14+
# 1
15+
# 2
16+
```
17+
18+
The `break` statement can be used with any of Ruby's iteration methods or loops.
19+
20+
```ruby
21+
array = [1, 2, 3, 4, 5]
22+
23+
# Break in each iteration
24+
array.each do |num|
25+
break if num > 3
26+
27+
puts "Number: #{num}"
28+
end
29+
# Output:
30+
# Number: 1
31+
# Number: 2
32+
# Number: 3
33+
34+
# Break in an infinite loop
35+
count = 0
36+
loop do
37+
count += 1
38+
break if count >= 3
39+
40+
puts "Count: #{count}"
41+
end
42+
# Output:
43+
# Count: 1
44+
# Count: 2
45+
```
46+
47+
## Break with a Value
48+
49+
When used inside a block, `break` can return a value that becomes the result of the method call.
50+
51+
```ruby
52+
# Break with a return value in map
53+
result = [1, 2, 3, 4, 5].map do |num|
54+
break "Too large!" if num > 3
55+
56+
num * 2
57+
end
58+
puts result # Output: "Too large!"
59+
60+
# Break with a value in find
61+
number = (1..10).find do |n|
62+
break n if n > 5 && n.even?
63+
end
64+
puts number # Output: 6
65+
```
66+
67+
## Break in Nested Loops
68+
69+
When using `break` in nested loops, it only exits the innermost loop. To break from nested loops, you typically need to use a flag or return.
70+
71+
```ruby
72+
# Break in nested iteration
73+
(1..3).each do |i|
74+
puts "Outer: #{i}"
75+
76+
(1..3).each do |j|
77+
break if j == 2
78+
79+
puts " Inner: #{j}"
80+
end
81+
end
82+
# Output:
83+
# Outer: 1
84+
# Inner: 1
85+
# Outer: 2
86+
# Inner: 1
87+
# Outer: 3
88+
# Inner: 1
89+
90+
# Breaking from nested loops with a flag
91+
found = false
92+
(1..3).each do |i|
93+
(1..3).each do |j|
94+
if i * j == 4
95+
found = true
96+
break
97+
end
98+
end
99+
break if found
100+
end
101+
```
102+
103+
The `break` keyword is essential for controlling loop execution and implementing early exit conditions. It's particularly useful when you've found what you're looking for and don't need to continue iterating.

0 commit comments

Comments
 (0)