Skip to content

Commit f8d31ee

Browse files
committed
Prevent adding end token on endless methods
1 parent be1c4f7 commit f8d31ee

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

lib/ruby_lsp/requests/on_type_formatting.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def handle_statement_end
120120

121121
return unless END_REGEXES.any? { |regex| regex.match?(@previous_line) }
122122

123+
# Endless method definitions (e.g., `def foo = 42`) are complete statements
124+
# and should not have `end` added
125+
return if @previous_line.match?(/\bdef\s+[\w.]+[!?=]?(\([^)]*\))?\s*=[^=~>]/)
126+
123127
indents = " " * @indentation
124128
current_line = @lines[@position[:line]]
125129
next_line = @lines[@position[:line] + 1]

test/requests/on_type_formatting_test.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,4 +1122,86 @@ def test_allows_end_completion_when_parenthesis_are_present
11221122
]
11231123
assert_equal(expected_edits.to_json, edits.to_json)
11241124
end
1125+
1126+
def test_does_not_add_end_for_endless_method
1127+
document = RubyLsp::RubyDocument.new(
1128+
source: +"",
1129+
version: 1,
1130+
uri: URI("file:///fake.rb"),
1131+
global_state: @global_state,
1132+
)
1133+
1134+
document.push_edits(
1135+
[{
1136+
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
1137+
text: "def foo = 42",
1138+
}],
1139+
version: 2,
1140+
)
1141+
document.parse!
1142+
1143+
edits = RubyLsp::Requests::OnTypeFormatting.new(
1144+
document,
1145+
{ line: 1, character: 0 },
1146+
"\n",
1147+
"Visual Studio Code",
1148+
).perform
1149+
1150+
assert_empty(edits)
1151+
end
1152+
1153+
def test_does_not_add_end_for_multiline_endless_method
1154+
document = RubyLsp::RubyDocument.new(
1155+
source: +"",
1156+
version: 1,
1157+
uri: URI("file:///fake.rb"),
1158+
global_state: @global_state,
1159+
)
1160+
1161+
document.push_edits(
1162+
[{
1163+
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
1164+
text: "def hello = puts(",
1165+
}],
1166+
version: 2,
1167+
)
1168+
document.parse!
1169+
1170+
edits = RubyLsp::Requests::OnTypeFormatting.new(
1171+
document,
1172+
{ line: 1, character: 0 },
1173+
"\n",
1174+
"Visual Studio Code",
1175+
).perform
1176+
1177+
assert_empty(edits)
1178+
end
1179+
1180+
def test_does_not_add_end_for_endless_method_with_equals_and_parameters
1181+
document = RubyLsp::RubyDocument.new(
1182+
source: +"",
1183+
version: 1,
1184+
uri: URI("file:///fake.rb"),
1185+
global_state: @global_state,
1186+
)
1187+
1188+
# This is actually invalid Ruby. It crashes with a syntax error. But we shouldn't add the `end` token anyway
1189+
document.push_edits(
1190+
[{
1191+
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
1192+
text: "def foo=(bar) = 42",
1193+
}],
1194+
version: 2,
1195+
)
1196+
document.parse!
1197+
1198+
edits = RubyLsp::Requests::OnTypeFormatting.new(
1199+
document,
1200+
{ line: 1, character: 0 },
1201+
"\n",
1202+
"Visual Studio Code",
1203+
).perform
1204+
1205+
assert_empty(edits)
1206+
end
11251207
end

0 commit comments

Comments
 (0)