From f7d24a22efa55cbcdeece582e26919a4bf9e234a Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 6 Sep 2023 12:13:21 +0100 Subject: [PATCH] Extract ActiveSupport test name extraction into a helper --- lib/ruby_lsp/ruby_lsp_rails/code_lens.rb | 29 +++----------- .../ruby_lsp_rails/document_symbol.rb | 31 ++++----------- .../support/active_support_test_helper.rb | 38 +++++++++++++++++++ 3 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 lib/ruby_lsp/ruby_lsp_rails/support/active_support_test_helper.rb diff --git a/lib/ruby_lsp/ruby_lsp_rails/code_lens.rb b/lib/ruby_lsp/ruby_lsp_rails/code_lens.rb index 0995353a..2e308915 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/code_lens.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/code_lens.rb @@ -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) @@ -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" @@ -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}" diff --git a/lib/ruby_lsp/ruby_lsp_rails/document_symbol.rb b/lib/ruby_lsp/ruby_lsp_rails/document_symbol.rb index 2dd5a006..b471f4fd 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/document_symbol.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/document_symbol.rb @@ -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 @@ -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, diff --git a/lib/ruby_lsp/ruby_lsp_rails/support/active_support_test_helper.rb b/lib/ruby_lsp/ruby_lsp_rails/support/active_support_test_helper.rb new file mode 100644 index 00000000..c772e746 --- /dev/null +++ b/lib/ruby_lsp/ruby_lsp_rails/support/active_support_test_helper.rb @@ -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