Skip to content

Commit

Permalink
Extract ActiveSupport test name extraction into a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Sep 6, 2023
1 parent b7dd122 commit f7d24a2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 47 deletions.
29 changes: 6 additions & 23 deletions lib/ruby_lsp/ruby_lsp_rails/code_lens.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# typed: strict
# frozen_string_literal: true

require_relative "support/active_support_test_helper"

module RubyLsp
module Rails
# ![CodeLens demo](../../code_lens.gif)
Expand Down Expand Up @@ -36,6 +38,8 @@ class CodeLens < ::RubyLsp::Listener
extend T::Sig
extend T::Generic

include ActiveSupportTestHelper

ResponseType = type_member { { fixed: T::Array[::RubyLsp::Interface::CodeLens] } }
BASE_COMMAND = "bin/rails test"

Expand All @@ -53,30 +57,9 @@ def initialize(uri, emitter, message_queue)

sig { params(node: SyntaxTree::Command).void }
def on_command(node)
message_value = node.message.value
return unless message_value == "test" && node.arguments.parts.any?

first_argument = node.arguments.parts.first

parts = case first_argument
when SyntaxTree::StringConcat
# We only support two lines of concatenation on test names
if first_argument.left.is_a?(SyntaxTree::StringLiteral) &&
first_argument.right.is_a?(SyntaxTree::StringLiteral)
[*first_argument.left.parts, *first_argument.right.parts]
end
when SyntaxTree::StringLiteral
first_argument.parts
end

# The test name may be a blank string while the code is being typed
return if parts.nil? || parts.empty?

# We can't handle interpolation yet
return unless parts.all? { |part| part.is_a?(SyntaxTree::TStringContent) }
test_name = active_support_test_name(node)

test_name = parts.map(&:value).join
return if test_name.empty?
return unless test_name

line_number = node.location.start_line
command = "#{BASE_COMMAND} #{@path}:#{line_number}"
Expand Down
31 changes: 7 additions & 24 deletions lib/ruby_lsp/ruby_lsp_rails/document_symbol.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# typed: strict
# frozen_string_literal: true

require_relative "support/active_support_test_helper"

module RubyLsp
module Rails
class DocumentSymbol < ::RubyLsp::Listener
extend T::Sig
extend T::Generic

include ActiveSupportTestHelper

ResponseType = type_member { { fixed: T::Array[::RubyLsp::Interface::DocumentSymbol] } }
SymbolHierarchyRoot = RubyLsp::Requests::DocumentSymbol::SymbolHierarchyRoot

Expand Down Expand Up @@ -43,30 +47,9 @@ def after_class(node)

sig { params(node: SyntaxTree::Command).void }
def on_command(node)
message_value = node.message.value
return unless message_value == "test" && node.arguments.parts.any?

first_argument = node.arguments.parts.first

parts = case first_argument
when SyntaxTree::StringConcat
# We only support two lines of concatenation on test names
if first_argument.left.is_a?(SyntaxTree::StringLiteral) &&
first_argument.right.is_a?(SyntaxTree::StringLiteral)
[*first_argument.left.parts, *first_argument.right.parts]
end
when SyntaxTree::StringLiteral
first_argument.parts
end

# The test name may be a blank string while the code is being typed
return if parts.nil? || parts.empty?

# We can't handle interpolation yet
return unless parts.all? { |part| part.is_a?(SyntaxTree::TStringContent) }

test_name = parts.map(&:value).join
return if test_name.empty?
test_name = active_support_test_name(node)

return unless test_name

create_document_symbol(
name: test_name,
Expand Down
38 changes: 38 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/support/active_support_test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# typed: strict
# frozen_string_literal: true

module RubyLsp
module Rails
module ActiveSupportTestHelper
extend T::Sig

sig { params(node: SyntaxTree::Command).returns(T.nilable(String)) }
def active_support_test_name(node)
message_value = node.message.value
return unless message_value == "test" && node.arguments.parts.any?

first_argument = node.arguments.parts.first

parts = case first_argument
when SyntaxTree::StringConcat
# We only support two lines of concatenation on test names
if first_argument.left.is_a?(SyntaxTree::StringLiteral) &&
first_argument.right.is_a?(SyntaxTree::StringLiteral)
[*first_argument.left.parts, *first_argument.right.parts]
end
when SyntaxTree::StringLiteral
first_argument.parts
end

# The test name may be a blank string while the code is being typed
return if parts.nil? || parts.empty?

# We can't handle interpolation yet
return unless parts.all? { |part| part.is_a?(SyntaxTree::TStringContent) }

test_name = parts.map(&:value).join
test_name unless test_name.empty?
end
end
end
end

0 comments on commit f7d24a2

Please sign in to comment.