Class: RubyLsp::Requests::DocumentLink
- Extended by:
- T::Generic, T::Sig
- Defined in:
- lib/ruby_lsp/requests/document_link.rb
Overview

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
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(uri, emitter, message_queue) ⇒ DocumentLink
constructor
A new instance of DocumentLink.
- #on_comment(node) ⇒ Object
Methods inherited from Listener
Methods included from Support::Common
#create_code_lens, #full_constant_name, #range_from_syntax_tree_node, #visible?
Constructor Details
#initialize(uri, emitter, message_queue) ⇒ DocumentLink
Returns a new instance of DocumentLink.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ruby_lsp/requests/document_link.rb', line 79 def initialize(uri, emitter, ) super(emitter, ) # Match the version based on the version in the RBI file name. Notice that the `@` symbol is sanitized to `%40` # in the URI version_match = /(?<=%40)[\d.]+(?=\.rbi$)/.match(uri) @gem_version = T.let(version_match && version_match[0], T.nilable(String)) @response = T.let([], T::Array[Interface::DocumentLink]) emitter.register(self, :on_comment) end |
Instance Attribute Details
#response ⇒ Object (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_paths ⇒ Object
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_comment(node) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ruby_lsp/requests/document_link.rb', line 92 def on_comment(node) match = node.value.match(%r{source://.*#\d+$}) return unless match uri = T.cast(URI(T.must(match[0])), URI::Source) gem_version = T.must(resolve_version(uri)) file_path = self.class.gem_paths.dig(uri.gem_name, gem_version, uri.path) return if file_path.nil? @response << Interface::DocumentLink.new( range: range_from_syntax_tree_node(node), target: "file://#{file_path}##{uri.line_number}", tooltip: "Jump to #{file_path}##{uri.line_number}", ) end |