Class: RubyLsp::Requests::DocumentLink

Inherits:
Listener
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/requests/document_link.rb

Overview

![Document link demo](../../document_link.gif)

The [document link](microsoft.github.io/language-server-protocol/specification#textDocument_documentLink) makes ‘# source://PATH_TO_FILE#line` comments in a Ruby/RBI file clickable if the file exists. When the user clicks the link, it’ll open that location.

# Example

“‘ruby # source://syntax_tree/3.2.1/lib/syntax_tree.rb#51 <- it will be clickable and will take the user to that location def format(source, maxwidth = T.unsafe(nil)) end “`

Constant Summary collapse

ResponseType =
type_member { { fixed: T::Array[Interface::DocumentLink] } }
GEM_TO_VERSION_MAP =
T.let(
  [*::Gem::Specification.default_stubs, *::Gem::Specification.stubs].map! do |s|
    [s.name, s.version.to_s]
  end.to_h.freeze,
  T::Hash[String, String],
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Listener

#response

Methods included from Support::Common

#create_code_lens, #defined_in_gem?, #markdown_from_index_entries, #range_from_location, #range_from_node, #self_receiver?, #visible?

Constructor Details

#initialize(uri, comments, dispatcher, message_queue) ⇒ DocumentLink

Returns a new instance of DocumentLink.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ruby_lsp/requests/document_link.rb', line 86

def initialize(uri, comments, dispatcher, message_queue)
  super(dispatcher, message_queue)

  # Match the version based on the version in the RBI file name. Notice that the `@` symbol is sanitized to `%40`
  # in the URI
  path = uri.to_standardized_path
  version_match = path ? /(?<=40)[\d.]+(?=\.rbi$)/.match(path) : nil
  @gem_version = T.let(version_match && version_match[0], T.nilable(String))
  @_response = T.let([], T::Array[Interface::DocumentLink])
  @lines_to_comments = T.let(
    comments.to_h do |comment|
      [comment.location.end_line, comment]
    end,
    T::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,
  )
end

Instance Attribute Details

#_responseObject (readonly)

Returns the value of attribute _response.



76
77
78
# File 'lib/ruby_lsp/requests/document_link.rb', line 76

def _response
  @_response
end

Class Method Details

.gem_pathsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_lsp/requests/document_link.rb', line 38

def gem_paths
  @gem_paths ||= T.let(
    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}/").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,
    T.nilable(T::Hash[String, T::Hash[String, T::Hash[String, String]]]),
  )
end

Instance Method Details

#on_class_node_enter(node) ⇒ Object



118
119
120
# File 'lib/ruby_lsp/requests/document_link.rb', line 118

def on_class_node_enter(node)
  extract_document_link(node)
end

#on_constant_path_write_node_enter(node) ⇒ Object



133
134
135
# File 'lib/ruby_lsp/requests/document_link.rb', line 133

def on_constant_path_write_node_enter(node)
  extract_document_link(node)
end

#on_constant_write_node_enter(node) ⇒ Object



128
129
130
# File 'lib/ruby_lsp/requests/document_link.rb', line 128

def on_constant_write_node_enter(node)
  extract_document_link(node)
end

#on_def_node_enter(node) ⇒ Object



113
114
115
# File 'lib/ruby_lsp/requests/document_link.rb', line 113

def on_def_node_enter(node)
  extract_document_link(node)
end

#on_module_node_enter(node) ⇒ Object



123
124
125
# File 'lib/ruby_lsp/requests/document_link.rb', line 123

def on_module_node_enter(node)
  extract_document_link(node)
end