From 12bbd46bd81a1c393f0746c9b86a2032f23ab23f Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Thu, 8 Aug 2024 07:46:41 -0400 Subject: [PATCH] Extract common behaviour --- lib/ruby_indexer/lib/ruby_indexer/entry.rb | 18 ++++++++++++++++++ lib/ruby_lsp/listeners/hover.rb | 7 +------ lib/ruby_lsp/requests/completion_resolve.rb | 8 +------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/entry.rb b/lib/ruby_indexer/lib/ruby_indexer/entry.rb index a2f9c7a92..5bbf9e2b0 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/entry.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/entry.rb @@ -342,6 +342,19 @@ def decorated_parameters "(#{first_signature.format})" end + + sig { returns(String) } + def formatted_signatures + overloads_count = signatures.size + case overloads_count + when 1 + "" + when 2 + "\n(+1 overload)" + else + "\n(+#{overloads_count - 1} overloads)" + end + end end class Accessor < Member @@ -543,6 +556,11 @@ def decorated_parameters @target.decorated_parameters end + sig { returns(String) } + def formatted_signatures + @target.formatted_signatures + end + sig { returns(T::Array[Signature]) } def signatures @target.signatures diff --git a/lib/ruby_lsp/listeners/hover.rb b/lib/ruby_lsp/listeners/hover.rb index 2e5e4b392..e8bafab85 100644 --- a/lib/ruby_lsp/listeners/hover.rb +++ b/lib/ruby_lsp/listeners/hover.rb @@ -177,12 +177,7 @@ def handle_method_hover(message, inherited_only: false) first_method = T.must(methods.first) title = "#{message}#{first_method.decorated_parameters}" - overloads_count = first_method.signatures.size - if overloads_count == 2 - title << "\n(+1 overload)" - elsif overloads_count > 2 - title << "\n(+#{overloads_count - 1} overloads)" - end + title << first_method.formatted_signatures if type.is_a?(TypeInferrer::GuessedType) title << "\n\nGuessed receiver: #{type.name}" diff --git a/lib/ruby_lsp/requests/completion_resolve.rb b/lib/ruby_lsp/requests/completion_resolve.rb index 7abe4ba2d..21d91d2a8 100644 --- a/lib/ruby_lsp/requests/completion_resolve.rb +++ b/lib/ruby_lsp/requests/completion_resolve.rb @@ -63,13 +63,7 @@ def perform if first_entry.is_a?(RubyIndexer::Entry::Member) label = +"#{label}#{first_entry.decorated_parameters}" - - overloads_count = first_entry.signatures.size - if overloads_count == 2 - label << "\n(+1 overload)" - elsif overloads_count > 2 - label << "\n(+#{overloads_count - 1} overloads)" - end + label << first_entry.formatted_signatures end guessed_type = @item.dig(:data, :guessed_type)