Skip to content

Commit

Permalink
Prevent non keywords from accidentally matching on type formatting (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Jul 25, 2024
1 parent edfe065 commit a5e1775
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/ruby_lsp/requests/on_type_formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def provider

END_REGEXES = T.let(
[
/\b(if|unless|for|while|class|module|until|def|case)\b.*/,
/.*\s\bdo\b/,
/\b(if|unless|for|while|until)\b($|\s|\()/,
/\b(class|module|def|case)\b($|\s)/,
/.*\s\bdo\b($|\s)/,
],
T::Array[Regexp],
)
Expand Down
58 changes: 58 additions & 0 deletions test/requests/on_type_formatting_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,62 @@ def test_includes_snippets_on_vscode_insiders
]
assert_equal(expected_edits.to_json, T.must(edits).to_json)
end

def test_does_not_confuse_class_parameter_with_keyword
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: "link_to :something,\n class: 'foo',\n ",
}],
version: 2,
)
document.parse

edits = RubyLsp::Requests::OnTypeFormatting.new(
document,
{ line: 2, character: 4 },
"\n",
"Visual Studio Code - Insiders",
).perform

assert_empty(edits)
end

def test_allows_end_completion_when_parenthesis_are_present
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: "if(\n ",
}],
version: 2,
)
document.parse

edits = RubyLsp::Requests::OnTypeFormatting.new(
document,
{ line: 1, character: 2 },
"\n",
"Visual Studio Code - Insiders",
).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: "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 a5e1775

Please sign in to comment.