Class: Rfmt::LSP::Workspace

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

Instance Method Summary collapse

Constructor Details

#initializeWorkspace

Returns a new instance of Workspace.



10
11
12
# File 'lib/rfmt/lsp/workspace.rb', line 10

def initialize
  @roots = []
end

Instance Method Details

#configure(params) ⇒ Object



14
15
16
17
18
19
# File 'lib/rfmt/lsp/workspace.rb', line 14

def configure(params)
  @roots = workspace_folder_roots(params)
  root_uri = params['rootUri']
  @roots << URI.file_uri_to_path(root_uri) if root_uri
  @roots = @roots.compact.map { |root| File.expand_path(root) }.uniq
end

#root_for(uri) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rfmt/lsp/workspace.rb', line 21

def root_for(uri)
  path = URI.file_uri_to_path(uri)
  return existing_root(@roots.first) unless path

  matching_root = @roots
                  .select { |root| path_inside?(path, root) }
                  .max_by(&:length)
  return existing_root(matching_root) if matching_root

  existing_root(File.dirname(path))
end

#with_root_for(uri, &block) ⇒ Object



33
34
35
36
37
38
# File 'lib/rfmt/lsp/workspace.rb', line 33

def with_root_for(uri, &block)
  root = root_for(uri)
  return block.call unless root

  Dir.chdir(root, &block)
end