Skip to content

Commit 368a154

Browse files
authored
Merge pull request #104 from Shopify/vs/support_multiline_strings_in_code_lens
Support escaped multiline strings in test names
2 parents 70f7cc2 + 673adb1 commit 368a154

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/ruby_lsp/ruby_lsp_rails/code_lens.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,26 @@ def on_command(node)
5959
return unless message_value == "test" && node.arguments.parts.any?
6060

6161
first_argument = node.arguments.parts.first
62-
return unless first_argument.is_a?(SyntaxTree::StringLiteral)
62+
63+
parts = case first_argument
64+
when SyntaxTree::StringConcat
65+
# We only support two lines of concatenation on test names
66+
if first_argument.left.is_a?(SyntaxTree::StringLiteral) &&
67+
first_argument.right.is_a?(SyntaxTree::StringLiteral)
68+
[*first_argument.left.parts, *first_argument.right.parts]
69+
end
70+
when SyntaxTree::StringLiteral
71+
first_argument.parts
72+
end
6373

6474
# The test name may be a blank string while the code is being typed
65-
return if first_argument.parts.empty?
75+
return if parts.nil? || parts.empty?
6676

6777
# We can't handle interpolation yet
68-
return unless first_argument.parts.all? { |part| part.is_a?(SyntaxTree::TStringContent) }
78+
return unless parts.all? { |part| part.is_a?(SyntaxTree::TStringContent) }
6979

70-
test_name = first_argument.parts.first.value
71-
return unless test_name
80+
test_name = parts.map(&:value).join
81+
return if test_name.empty?
7282

7383
line_number = node.location.start_line
7484
command = "#{BASE_COMMAND} #{@path}:#{line_number}"

test/ruby_lsp_rails/code_lens_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ class Test < ActiveSupport::TestCase
3838
assert_match("Debug", response[5].command.title)
3939
end
4040

41+
test "recognizes multiline escaped strings" do
42+
@store.set(uri: "file:///fake.rb", source: <<~RUBY, version: 1)
43+
class Test < ActiveSupport::TestCase
44+
test "an example" \
45+
"multiline" do
46+
# test body
47+
end
48+
end
49+
RUBY
50+
51+
response = RubyLsp::Executor.new(@store, @message_queue).execute({
52+
method: "textDocument/codeLens",
53+
params: { textDocument: { uri: "file:///fake.rb" }, position: { line: 0, character: 0 } },
54+
}).response
55+
56+
# The first 3 responses are for the test class.
57+
# The last 3 are for the test declaration.
58+
assert_equal(6, response.size)
59+
assert_match("Run", response[3].command.title)
60+
assert_equal("bin/rails test /fake.rb:2", response[3].command.arguments[2])
61+
assert_match("Run In Terminal", response[4].command.title)
62+
assert_match("Debug", response[5].command.title)
63+
end
64+
4165
test "ignores unnamed tests (empty string)" do
4266
@store.set(uri: "file:///fake.rb", source: <<~RUBY, version: 1)
4367
class Test < ActiveSupport::TestCase

0 commit comments

Comments
 (0)