Class: RubyLsp::Listeners::DocumentLink
- Inherits:
-
Object
- Object
- RubyLsp::Listeners::DocumentLink
- Includes:
- Requests::Support::Common
- Defined in:
- lib/ruby_lsp/listeners/document_link.rb
Constant Summary collapse
- GEM_TO_VERSION_MAP =
[*::Gem::Specification.default_stubs, *::Gem::Specification.stubs].map! do |s| [s.name, s.version.to_s] end.to_h.freeze
Class Method Summary collapse
-
.gem_paths ⇒ Object
: -> Hash[String, Hash[String, Hash[String, String]]].
Instance Method Summary collapse
-
#initialize(response_builder, uri, comments, dispatcher) ⇒ DocumentLink
constructor
: (ResponseBuilders::CollectionResponseBuilder response_builder, URI::Generic uri, Array comments, Prism::Dispatcher dispatcher) -> void.
-
#on_call_node_enter(node) ⇒ Object
: (Prism::CallNode node) -> void.
-
#on_class_node_enter(node) ⇒ Object
: (Prism::ClassNode node) -> void.
-
#on_constant_path_write_node_enter(node) ⇒ Object
: (Prism::ConstantPathWriteNode node) -> void.
-
#on_constant_write_node_enter(node) ⇒ Object
: (Prism::ConstantWriteNode node) -> void.
-
#on_def_node_enter(node) ⇒ Object
: (Prism::DefNode node) -> void.
-
#on_module_node_enter(node) ⇒ Object
: (Prism::ModuleNode node) -> void.
Methods included from Requests::Support::Common
#categorized_markdown_from_index_entries, #constant_name, #create_code_lens, #each_constant_path_part, #kind_for_entry, #markdown_from_index_entries, #namespace_constant_name, #not_in_dependencies?, #range_from_location, #range_from_node, #self_receiver?
Constructor Details
#initialize(response_builder, uri, comments, dispatcher) ⇒ DocumentLink
: (ResponseBuilders::CollectionResponseBuilder response_builder, URI::Generic uri, Array comments, Prism::Dispatcher dispatcher) -> void
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ruby_lsp/listeners/document_link.rb', line 53 def initialize(response_builder, uri, comments, dispatcher) # Match the version based on the version in the RBI file name. Notice that the `@` symbol is sanitized to `%40` # in the URI @response_builder = response_builder path = uri.to_standardized_path version_match = path ? /(?<=%40)[\d.]+(?=\.rbi$)/.match(path) : nil @gem_version = version_match && version_match[0] #: String? @lines_to_comments = comments.to_h do |comment| [comment.location.end_line, comment] end #: Hash[Integer, Prism::Comment] @sig_comments = {} #: Hash[Integer, Prism::Comment] dispatcher.register( self, :on_def_node_enter, :on_class_node_enter, :on_module_node_enter, :on_constant_write_node_enter, :on_constant_path_write_node_enter, :on_call_node_enter, ) end |
Class Method Details
.gem_paths ⇒ Object
: -> Hash[String, Hash[String, Hash[String, String]]]
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ruby_lsp/listeners/document_link.rb', line 18 def gem_paths @gem_paths ||= begin lookup = {} Gem::Specification.stubs.each do |stub| spec = stub.to_spec lookup[spec.name] = {} lookup[spec.name][spec.version.to_s] = {} Dir.glob("**/*.rb", base: "#{spec.full_gem_path.delete_prefix("//?/")}/").each do |path| lookup[spec.name][spec.version.to_s][path] = "#{spec.full_gem_path}/#{path}" end end Gem::Specification.default_stubs.each do |stub| spec = stub.to_spec lookup[spec.name] = {} lookup[spec.name][spec.version.to_s] = {} prefix_matchers = Regexp.union(spec.require_paths.map do |rp| Regexp.new("^#{rp}/") end) prefix_matcher = Regexp.union(prefix_matchers, //) spec.files.each do |file| path = file.sub(prefix_matcher, "") lookup[spec.name][spec.version.to_s][path] = "#{RbConfig::CONFIG["rubylibdir"]}/#{path}" end end lookup end #: Hash[String, Hash[String, Hash[String, String]]]? end |
Instance Method Details
#on_call_node_enter(node) ⇒ Object
: (Prism::CallNode node) -> void
77 78 79 80 81 82 83 84 |
# File 'lib/ruby_lsp/listeners/document_link.rb', line 77 def on_call_node_enter(node) return unless node.name == :sig comment = @lines_to_comments[node.location.start_line - 1] return unless comment @sig_comments[node.location.end_line] = comment end |
#on_class_node_enter(node) ⇒ Object
: (Prism::ClassNode node) -> void
92 93 94 |
# File 'lib/ruby_lsp/listeners/document_link.rb', line 92 def on_class_node_enter(node) extract_document_link(node) end |
#on_constant_path_write_node_enter(node) ⇒ Object
: (Prism::ConstantPathWriteNode node) -> void
107 108 109 |
# File 'lib/ruby_lsp/listeners/document_link.rb', line 107 def on_constant_path_write_node_enter(node) extract_document_link(node) end |
#on_constant_write_node_enter(node) ⇒ Object
: (Prism::ConstantWriteNode node) -> void
102 103 104 |
# File 'lib/ruby_lsp/listeners/document_link.rb', line 102 def on_constant_write_node_enter(node) extract_document_link(node) end |
#on_def_node_enter(node) ⇒ Object
: (Prism::DefNode node) -> void
87 88 89 |
# File 'lib/ruby_lsp/listeners/document_link.rb', line 87 def on_def_node_enter(node) extract_document_link(node) end |
#on_module_node_enter(node) ⇒ Object
: (Prism::ModuleNode node) -> void
97 98 99 |
# File 'lib/ruby_lsp/listeners/document_link.rb', line 97 def on_module_node_enter(node) extract_document_link(node) end |