Class: Yoda::Server::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/server/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspaces:) ⇒ Session

Returns a new instance of Session.

Parameters:



22
23
24
# File 'lib/yoda/server/session.rb', line 22

def initialize(workspaces:)
  @workspaces = workspaces
end

Instance Attribute Details

#workspacesArray<Workspace> (readonly)

Returns:



7
8
9
# File 'lib/yoda/server/session.rb', line 7

def workspaces
  @workspaces
end

Class Method Details

.from_root_uri(root_uri) ⇒ Object



9
10
11
12
13
# File 'lib/yoda/server/session.rb', line 9

def self.from_root_uri(root_uri)
  workspaces = [Workspace.new(name: 'root', root_uri: root_uri)]
  Logger.trace("Setting up session for #{root_uri}")
  new(workspaces: workspaces)
end

.from_workspace_folders(workspace_folders) ⇒ Object

Parameters:

  • workspace_folders (Array<LanguageServer::Protocol::Interface::WorkspaceFolder>)

    an uri expression of project root path



16
17
18
19
# File 'lib/yoda/server/session.rb', line 16

def self.from_workspace_folders(workspace_folders)
  workspaces = workspace_folders.map { |folder| Workspace.from_workspace_folder(folder) }
  new(workspaces: workspaces)
end

Instance Method Details

#add_workspace(new_workspace) ⇒ Object

Parameters:



37
38
39
40
41
# File 'lib/yoda/server/session.rb', line 37

def add_workspace(new_workspace)
  return if workspaces.find { |workspace| workspace.id == new_workspace.id }
  new_workspace.setup
  workspaces.push(new_workspace)
end

#projectStore::Project?

Returns:



49
50
51
# File 'lib/yoda/server/session.rb', line 49

def project
  workspaces.first&.project
end

#read_source(uri) ⇒ Object Also known as: reparse_doc



53
54
55
# File 'lib/yoda/server/session.rb', line 53

def read_source(uri)
  workspaces_for(uri).each { |workspace| workspace.read_source(uri) }
end

#registryStore::Registry

Returns:



27
28
29
# File 'lib/yoda/server/session.rb', line 27

def registry
  project.registry
end

#remove_source(uri:) ⇒ Object

Parameters:

  • uri (String)


65
66
67
# File 'lib/yoda/server/session.rb', line 65

def remove_source(uri:)
  workspaces_for(uri).each { |workspace| workspace.remove_source(uri: uri) }
end

#remove_workspace(id:) ⇒ Object

Parameters:

  • id (String)


44
45
46
# File 'lib/yoda/server/session.rb', line 44

def remove_workspace(id:)
  @workspaces = workspaces.reject { |workspace| workspace.id == id }
end

#setupArray<Exception>

Returns errors on setup.

Returns:

  • (Array<Exception>)

    errors on setup



32
33
34
# File 'lib/yoda/server/session.rb', line 32

def setup
  workspaces.flat_map(&:setup)
end

#store_source(uri:, source:) ⇒ Object

Parameters:

  • uri (String)
  • source (String)


60
61
62
# File 'lib/yoda/server/session.rb', line 60

def store_source(uri:, source:)
  workspaces_for(uri).each { |workspace| workspace.store_source(uri: uri, source: source) }
end

#temporal_workspace_for(uri) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/yoda/server/session.rb', line 80

def temporal_workspace_for(uri)
  temporal_workspaces[uri] ||= RootlessWorkspace.new(name: uri).tap do |workspace|
    Logger.trace "Setting up temporal workspace for #{uri}"
    workspace.setup
    # Store the file content to the temporally workspace
    workspace.read_source(uri)
  end
end

#workspace_for(uri) ⇒ Workspace?

Returns:



70
71
72
# File 'lib/yoda/server/session.rb', line 70

def workspace_for(uri)
  workspaces_for(uri).first
end

#workspaces_for(uri) ⇒ Array<Workspace>

Returns:



75
76
77
78
# File 'lib/yoda/server/session.rb', line 75

def workspaces_for(uri)
  matched_workspaces = workspaces.select { |workspace| workspace.suburi?(uri) }
  matched_workspaces.empty? ? [temporal_workspace_for(uri)] : matched_workspaces
end