Module: Solargraph::LanguageServer::UriHelpers

Overview

Methods to handle conversions between file URIs and paths.

Class Method Summary collapse

Class Method Details

.decode(text) ⇒ String

Decode text from a URI path component in LSP.

Parameters:

  • text (String)

Returns:

  • (String)


44
45
46
# File 'lib/solargraph/language_server/uri_helpers.rb', line 44

def decode text
  CGI.unescape(text)
end

.encode(text) ⇒ String

Encode text to be used as a URI path component in LSP.

Parameters:

  • text (String)

Returns:

  • (String)


32
33
34
35
36
37
38
# File 'lib/solargraph/language_server/uri_helpers.rb', line 32

def encode text
  CGI.escape(text)
     .gsub('%3A', ':')
     .gsub('%5C', '\\')
     .gsub('%2F', '/')
     .gsub('+', '%20')
end

.file_to_uri(file) ⇒ String

Convert a file path to a URI.

Parameters:

  • file (String)

Returns:

  • (String)


24
25
26
# File 'lib/solargraph/language_server/uri_helpers.rb', line 24

def file_to_uri file
  "file://#{encode(file.gsub(/^([a-z]\:)/i, '/\1'))}"
end

.uri_to_file(uri) ⇒ String

Convert a file URI to a path.

Parameters:

  • uri (String)

Returns:

  • (String)


16
17
18
# File 'lib/solargraph/language_server/uri_helpers.rb', line 16

def uri_to_file uri
  decode(uri).sub(/^file\:(?:\/\/)?/, '').sub(/^\/([a-z]\:)/i, '\1')
end