Skip to content

Commit

Permalink
Support plain heredocs in on type formatting (#2620)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Sep 25, 2024
1 parent b66d02a commit d9420a2
Show file tree
Hide file tree
Showing 2 changed files with 71 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 @@ -63,7 +63,7 @@ def perform
if (comment_match = @previous_line.match(/^#(\s*)/))
handle_comment_line(T.must(comment_match[1]))
elsif @document.syntax_error?
match = /(?<=<<(-|~))(?<quote>['"`]?)(?<delimiter>\w+)\k<quote>/.match(@previous_line)
match = /(<<((-|~)?))(?<quote>['"`]?)(?<delimiter>\w+)\k<quote>/.match(@previous_line)
heredoc_delimiter = match && match.named_captures["delimiter"]

if heredoc_delimiter
Expand Down
70 changes: 70 additions & 0 deletions test/requests/on_type_formatting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,76 @@ def test_adding_heredoc_delimiter
assert_equal(expected_edits.to_json, T.must(edits).to_json)
end

def test_plain_heredoc_completion
document = RubyLsp::RubyDocument.new(source: +"", version: 1, uri: URI("file:///fake.rb"))

document.push_edits(
[{
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
text: "str = <<STR",
}],
version: 2,
)
document.parse!

edits = RubyLsp::Requests::OnTypeFormatting.new(
document,
{ line: 1, character: 2 },
"\n",
"Visual Studio Code",
).perform
expected_edits = [
{
range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } },
newText: "\n",
},
{
range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } },
newText: "STR",
},
{
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_quoted_heredoc_completion
document = RubyLsp::RubyDocument.new(source: +"", version: 1, uri: URI("file:///fake.rb"))

document.push_edits(
[{
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } },
text: "str = <<-'STR'",
}],
version: 2,
)
document.parse!

edits = RubyLsp::Requests::OnTypeFormatting.new(
document,
{ line: 1, character: 2 },
"\n",
"Visual Studio Code",
).perform
expected_edits = [
{
range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } },
newText: "\n",
},
{
range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } },
newText: "STR",
},
{
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_parameters
document = RubyLsp::RubyDocument.new(source: +"foo(proc do\n)", version: 1, uri: URI("file:///fake.rb"))

Expand Down

0 comments on commit d9420a2

Please sign in to comment.