Class: Solargraph::LanguageServer::Host::Sources

Inherits:
Object
  • Object
show all
Includes:
Observable, UriHelpers
Defined in:
lib/solargraph/language_server/host/sources.rb

Overview

A Host class for managing sources.

Instance Method Summary collapse

Methods included from UriHelpers

file_to_uri, uri_to_file

Constructor Details

#initializeSources


12
13
14
# File 'lib/solargraph/language_server/host/sources.rb', line 12

def initialize
  @stopped = true
end

Instance Method Details

#async_update(uri, updater) ⇒ void


84
85
86
87
88
89
90
# File 'lib/solargraph/language_server/host/sources.rb', line 84

def async_update uri, updater
  src = find(uri)
  mutex.synchronize { open_source_hash[uri] = src.start_synchronize(updater) }
  mutex.synchronize { queue.push uri }
  changed
  notify_observers uri
end

#clearvoid


118
119
120
# File 'lib/solargraph/language_server/host/sources.rb', line 118

def clear
  open_source_hash.clear
end

#close(uri) ⇒ void

This method returns an undefined value.

Close the source with the given URI.


106
107
108
# File 'lib/solargraph/language_server/host/sources.rb', line 106

def close uri
  open_source_hash.delete uri
end

#find(uri) ⇒ Source

Find the source with the given URI.

Raises:


98
99
100
# File 'lib/solargraph/language_server/host/sources.rb', line 98

def find uri
  open_source_hash[uri] || raise(Solargraph::FileNotFoundError, "Host could not find #{uri}")
end

#include?(uri) ⇒ Boolean

True if a source with given URI is currently open.


113
114
115
# File 'lib/solargraph/language_server/host/sources.rb', line 113

def include? uri
  open_source_hash.key? uri
end

#open(uri, text, version) ⇒ Source

Open a source.


56
57
58
59
60
# File 'lib/solargraph/language_server/host/sources.rb', line 56

def open uri, text, version
  filename = uri_to_file(uri)
  source = Solargraph::Source.new(text, filename, version)
  open_source_hash[uri] = source
end

#open_from_disk(uri) ⇒ Object


62
63
64
65
# File 'lib/solargraph/language_server/host/sources.rb', line 62

def open_from_disk uri
  source = Solargraph::Source.load(UriHelpers.uri_to_file(uri))
  open_source_hash[uri] = source
end

#startvoid


21
22
23
24
25
26
27
28
29
30
# File 'lib/solargraph/language_server/host/sources.rb', line 21

def start
  return unless @stopped
  @stopped = false
  Thread.new do
    until stopped?
      tick
      sleep 0.25 if queue.empty?
    end
  end
end

#stopvoid


46
47
48
# File 'lib/solargraph/language_server/host/sources.rb', line 46

def stop
  @stopped = true
end

#stopped?Boolean


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

def stopped?
  @stopped
end

#tickvoid


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/solargraph/language_server/host/sources.rb', line 33

def tick
  return if queue.empty?
  uri = mutex.synchronize { queue.shift }
  return if queue.include?(uri)
  mutex.synchronize do
    nxt = open_source_hash[uri].finish_synchronize
    open_source_hash[uri] = nxt
    changed
    notify_observers uri
  end
end

#update(uri, updater) ⇒ Source

Update an existing source.

Raises:


74
75
76
77
78
79
# File 'lib/solargraph/language_server/host/sources.rb', line 74

def update uri, updater
  src = find(uri)
  mutex.synchronize { open_source_hash[uri] = src.synchronize(updater) }
  changed
  notify_observers uri
end