From 618dcf5525bbf2bf65369e7b1940d9d0f9517e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lek=C3=AB=20Mula?= Date: Thu, 3 Oct 2024 17:37:18 +0200 Subject: [PATCH] Include convention pins in document_symbols --- .../message/text_document/document_symbol.rb | 5 ++- lib/solargraph/source_map.rb | 17 +++++++- spec/source_map_spec.rb | 43 +++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/lib/solargraph/language_server/message/text_document/document_symbol.rb b/lib/solargraph/language_server/message/text_document/document_symbol.rb index 670b99469..19a64cf93 100644 --- a/lib/solargraph/language_server/message/text_document/document_symbol.rb +++ b/lib/solargraph/language_server/message/text_document/document_symbol.rb @@ -6,6 +6,8 @@ class Solargraph::LanguageServer::Message::TextDocument::DocumentSymbol < Solarg def process pins = host.document_symbols params['textDocument']['uri'] info = pins.map do |pin| + next nil unless pin.location&.filename + result = { name: pin.name, containerName: pin.namespace, @@ -17,7 +19,8 @@ def process deprecated: pin.deprecated? } result - end + end.compact + set_result info end end diff --git a/lib/solargraph/source_map.rb b/lib/solargraph/source_map.rb index 7c7ff9f6a..60cba47c1 100644 --- a/lib/solargraph/source_map.rb +++ b/lib/solargraph/source_map.rb @@ -30,6 +30,7 @@ def initialize source, pins, locals @pins = pins @locals = locals environ.merge Convention.for_local(self) unless filename.nil? + self.convention_pins = environ.pins @pin_class_hash = pins.to_set.classify(&:class).transform_values(&:to_a) @pin_select_cache = {} end @@ -65,11 +66,12 @@ def environ @environ ||= Environ.new end + # all pins except Solargraph::Pin::Reference::Reference # @return [Array] def document_symbols - @document_symbols ||= pins.select { |pin| + @document_symbols ||= (pins + convention_pins).select do |pin| pin.path && !pin.path.empty? - } + end end # @param query [String] @@ -159,6 +161,17 @@ def map source private + # @return [Array] + def convention_pins + @convention_pins || [] + end + + def convention_pins=(pins) + # unmemoizing the document_symbols in case it was called from any of convnetions + @document_symbols = nil + @convention_pins = pins + end + # @param line [Integer] # @param character [Integer] # @param klasses [Array] diff --git a/spec/source_map_spec.rb b/spec/source_map_spec.rb index 8e623296b..db9789c6b 100644 --- a/spec/source_map_spec.rb +++ b/spec/source_map_spec.rb @@ -20,6 +20,49 @@ def baz_qux; end expect(map.query_symbols("bazqux")).to eq(map.document_symbols.select{ |pin_namespace| pin_namespace.name == "baz_qux" }) end + it 'returns all pins, except for references as document symbols' do + map = Solargraph::SourceMap.load_string(%( + class FooBar + require 'foo' + include SomeModule + extend SomeOtherModule + + def baz_qux; end + end + ), 'test.rb') + + expect(map.document_symbols.map(&:path)).to eq(['FooBar', 'FooBar#baz_qux']) + expect(map.document_symbols.map(&:class)).not_to include(an_instance_of(Solargraph::Pin::Reference)) + end + + it 'includes convention pins in document symbols' do + dummy_convention = Class.new(Solargraph::Convention::Base) do + def local(source_map) + source_map.document_symbols # call memoized method + + Solargraph::Environ.new( + pins: [ + Solargraph::Pin::Method.new( + closure: Solargraph::Pin::Namespace.new(name: 'FooBar', type: :class), + name: 'baz_convention', + scope: :instance + ) + ] + ) + end + end + + Solargraph::Convention.register dummy_convention + + map = Solargraph::SourceMap.load_string(%( + class FooBar + def baz_qux; end + end + ), 'test.rb') + + expect(map.document_symbols.map(&:path)).to include('FooBar#baz_convention') + end + it "locates block pins" do map = Solargraph::SourceMap.load_string(%( class Foo