Class: Ibex::LSP::Workspace

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/lsp/workspace.rb

Overview

Converts file URIs to canonical paths constrained to initialized workspace roots.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_uris, loader) ⇒ Workspace

Returns a new instance of Workspace.

Raises:



10
11
12
13
14
15
# File 'lib/ibex/lsp/workspace.rb', line 10

def initialize(root_uris, loader)
  raise ProtocolError.new("initialize requires a workspace root", code: -32_602) if root_uris.empty?

  @loader = loader
  @roots = root_uris.map { |uri| canonical_root(uri) }.uniq.sort.freeze
end

Instance Attribute Details

#rootsObject (readonly)

: Array



7
8
9
# File 'lib/ibex/lsp/workspace.rb', line 7

def roots
  @roots
end

Instance Method Details

#path(uri, allow_missing: true) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ibex/lsp/workspace.rb', line 18

def path(uri, allow_missing: true)
  parsed = parse_file_uri(uri)
  decoded = decode_path(parsed.path)

  canonical = @loader.canonical_path(decoded, allow_missing: allow_missing)
  return canonical if root_for(canonical)

  raise ProtocolError.new("document is outside initialized workspace roots: #{uri}", code: -32_602)
rescue URI::InvalidURIError, ArgumentError => e
  raise ProtocolError.new("invalid file URI #{uri.inspect}: #{e.message}", code: -32_602)
rescue SystemCallError => e
  raise ProtocolError.new("cannot resolve file URI #{uri.inspect}: #{e.message}", code: -32_602)
end

#root_for(path) ⇒ Object



44
45
46
# File 'lib/ibex/lsp/workspace.rb', line 44

def root_for(path)
  roots.select { |root| inside?(path, root) }.max_by(&:length)
end

#uri(path) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/ibex/lsp/workspace.rb', line 33

def uri(path)
  canonical = @loader.canonical_path(path, allow_missing: true)
  parser = begin
    URI::RFC2396_PARSER
  rescue NameError
    URI::DEFAULT_PARSER
  end
  "file://#{parser.escape(canonical)}"
end