Class: URI::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_indexer/lib/ruby_indexer/uri.rb

Constant Summary collapse

PARSER =

NOTE: We also define this in the shim

const_defined?(:RFC2396_PARSER) ? RFC2396_PARSER : DEFAULT_PARSER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#require_pathObject

: String?



44
45
46
# File 'lib/ruby_indexer/lib/ruby_indexer/uri.rb', line 44

def require_path
  @require_path
end

Class Method Details

.from_path(path:, fragment: nil, scheme: "file", load_path_entry: nil) ⇒ Object

: (path: String, ?fragment: String?, ?scheme: String, ?load_path_entry: String?) -> URI::Generic



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_indexer/lib/ruby_indexer/uri.rb', line 17

def from_path(path:, fragment: nil, scheme: "file", load_path_entry: nil)
  # This unsafe regex is the same one used in the URI::RFC2396_REGEXP class with the exception of the fact that we
  # do not include colon as a safe character. VS Code URIs always escape colons and we need to ensure we do the
  # same to avoid inconsistencies in our URIs, which are used to identify resources
  unsafe_regex = %r{[^\-_.!~*'()a-zA-Z\d;/?@&=+$,\[\]]}

  # On Windows, if the path begins with the disk name, we need to add a leading slash to make it a valid URI
  escaped_path = if /^[A-Z]:/i.match?(path)
    PARSER.escape("/#{path}", unsafe_regex)
  elsif path.start_with?("//?/")
    # Some paths on Windows start with "//?/". This is a special prefix that allows for long file paths
    PARSER.escape(path.delete_prefix("//?"), unsafe_regex)
  else
    PARSER.escape(path, unsafe_regex)
  end

  uri = build(scheme: scheme, path: escaped_path, fragment: fragment)

  if load_path_entry
    uri.require_path = path.delete_prefix("#{load_path_entry}/").delete_suffix(".rb")
  end

  uri
end

Instance Method Details

#add_require_path_from_load_entry(load_path_entry) ⇒ Object

: (String load_path_entry) -> void



47
48
49
50
51
52
# File 'lib/ruby_indexer/lib/ruby_indexer/uri.rb', line 47

def add_require_path_from_load_entry(load_path_entry)
  path = to_standardized_path
  return unless path

  self.require_path = path.delete_prefix("#{load_path_entry}/").delete_suffix(".rb")
end

#to_standardized_pathObject Also known as: full_path

: -> String?



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_indexer/lib/ruby_indexer/uri.rb', line 55

def to_standardized_path
  parsed_path = path
  return unless parsed_path

  unescaped_path = PARSER.unescape(parsed_path)

  # On Windows, when we're getting the file system path back from the URI, we need to remove the leading forward
  # slash
  if %r{^/[A-Z]:}i.match?(unescaped_path)
    unescaped_path.delete_prefix("/")
  else
    unescaped_path
  end
end