Class: URI::Source

Inherits:
File
  • Object
show all
Defined in:
lib/ruby_lsp/requests/support/source_uri.rb

Overview

Must be kept in sync with the one in Tapioca

Constant Summary collapse

COMPONENT =
[
  :scheme,
  :gem_name,
  :gem_version,
  :path,
  :line_number,
].freeze
PARSER =

‘uri` for Ruby 3.4 switched the default parser from RFC2396 to RFC3986. The new parser emits a deprecation warning on a few methods and delegates them to RFC2396, namely `extract`/`make_regexp`/`escape`/`unescape`. On earlier versions of the uri gem, the RFC2396_PARSER constant doesn’t exist, so it needs some special handling to select a parser that doesn’t emit deprecations. While it was backported to Ruby 3.1, users may have the uri gem in their own bundle and thus not use a compatible version.

const_defined?(:RFC2396_PARSER) ? RFC2396_PARSER : DEFAULT_PARSER
@@schemes =

: Hash[String, untyped] # rubocop:disable Style/ClassVars

@@schemes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gem_versionObject (readonly)

: String?



30
31
32
# File 'lib/ruby_lsp/requests/support/source_uri.rb', line 30

def gem_version
  @gem_version
end

Class Method Details

.build(gem_name:, gem_version:, path:, line_number:) ⇒ Object

: (gem_name: String, gem_version: String?, path: String, line_number: String?) -> URI::Source



34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_lsp/requests/support/source_uri.rb', line 34

def build(gem_name:, gem_version:, path:, line_number:)
  super(
    {
      scheme: "source",
      host: gem_name,
      path: PARSER.escape("/#{gem_version}/#{path}"),
      fragment: line_number,
    }
  )
end

Instance Method Details

#check_host(v) ⇒ Object

: (String? v) -> bool



57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby_lsp/requests/support/source_uri.rb', line 57

def check_host(v)
  return true unless v

  if /[A-Za-z][A-Za-z0-9\-_]*/ !~ v
    raise InvalidComponentError,
      "bad component(expected gem name): #{v}"
  end

  true
end

#set_path(v) ⇒ Object

: (String? v) -> void



47
48
49
50
51
52
53
54
# File 'lib/ruby_lsp/requests/support/source_uri.rb', line 47

def set_path(v) # rubocop:disable Naming/AccessorMethodName
  return if v.nil?

  gem_version, path = v.delete_prefix("/").split("/", 2)

  @gem_version = gem_version #: String?
  @path = path #: String?
end

#to_sObject

: -> String



69
70
71
# File 'lib/ruby_lsp/requests/support/source_uri.rb', line 69

def to_s
  "source://#{gem_name}/#{gem_version}#{path}##{line_number}"
end