Skip to content

Commit

Permalink
Fix end completion inside parenthesis or brackets (#1231)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Dec 1, 2023
1 parent 4ebe9e0 commit 5d1ce4f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/on_type_formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def handle_statement_end
current_line = @lines[@position[:line]]
next_line = @lines[@position[:line] + 1]

if current_line.nil? || current_line.strip.empty?
if current_line.nil? || current_line.strip.empty? || current_line.include?(")") || current_line.include?("]")
add_edit_with_text("\n")
add_edit_with_text("#{indents}end")
move_cursor_to(@position[:line], @indentation + 2)
Expand Down
42 changes: 42 additions & 0 deletions test/requests/on_type_formatting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,46 @@ def test_adding_heredoc_delimiter
]
assert_equal(expected_edits.to_json, T.must(edits).to_json)
end

def test_completing_end_token_inside_parameters
document = RubyLsp::RubyDocument.new(source: +"foo(proc do\n)", version: 1, uri: URI("file:///fake.rb"))

edits = RubyLsp::Requests::OnTypeFormatting.new(document, { line: 1, character: 0 }, "\n").run
expected_edits = [
{
range: { start: { line: 1, character: 0 }, end: { line: 1, character: 0 } },
newText: "\n",
},
{
range: { start: { line: 1, character: 0 }, end: { line: 1, character: 0 } },
newText: "end",
},
{
range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } },
newText: "$0",
},
]
assert_equal(expected_edits.to_json, T.must(edits).to_json)
end

def test_completing_end_token_inside_brackets
document = RubyLsp::RubyDocument.new(source: +"foo[proc do\n]", version: 1, uri: URI("file:///fake.rb"))

edits = RubyLsp::Requests::OnTypeFormatting.new(document, { line: 1, character: 0 }, "\n").run
expected_edits = [
{
range: { start: { line: 1, character: 0 }, end: { line: 1, character: 0 } },
newText: "\n",
},
{
range: { start: { line: 1, character: 0 }, end: { line: 1, character: 0 } },
newText: "end",
},
{
range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } },
newText: "$0",
},
]
assert_equal(expected_edits.to_json, T.must(edits).to_json)
end
end

0 comments on commit 5d1ce4f

Please sign in to comment.