From 898e0dfddac2c7af4e8565e57d1467db3a2b5815 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Thu, 25 Jul 2024 11:51:35 -0400 Subject: [PATCH] Prevent non keywords from accidentally matching on type formatting --- lib/ruby_lsp/requests/on_type_formatting.rb | 5 +- test/requests/on_type_formatting_test.rb | 58 +++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/lib/ruby_lsp/requests/on_type_formatting.rb b/lib/ruby_lsp/requests/on_type_formatting.rb index 0831388f8..0ae579320 100644 --- a/lib/ruby_lsp/requests/on_type_formatting.rb +++ b/lib/ruby_lsp/requests/on_type_formatting.rb @@ -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], ) diff --git a/test/requests/on_type_formatting_test.rb b/test/requests/on_type_formatting_test.rb index 137e3d06a..2c23e5c6a 100644 --- a/test/requests/on_type_formatting_test.rb +++ b/test/requests/on_type_formatting_test.rb @@ -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