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

decode, encode, file_to_uri, uri_to_file

Constructor Details

#initializeSources

Returns a new instance of Sources.



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

def initialize
  @mutex = Mutex.new
  @stopped = true
  @has_uri = ConditionVariable.new
end

Instance Method Details

#add_uri(uri) ⇒ void

This method returns an undefined value.



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

def add_uri(uri)
  queue.push(uri)
  @has_uri.signal
end

#async_update(uri, updater) ⇒ void

This method returns an undefined value.

Parameters:



97
98
99
100
101
102
103
104
105
# File 'lib/solargraph/language_server/host/sources.rb', line 97

def async_update uri, updater
  src = find(uri)
  mutex.synchronize do
    open_source_hash[uri] = src.start_synchronize(updater)
    add_uri(uri)
  end
  changed
  notify_observers uri
end

#clearvoid

This method returns an undefined value.



133
134
135
# File 'lib/solargraph/language_server/host/sources.rb', line 133

def clear
  open_source_hash.clear
end

#close(uri) ⇒ void

This method returns an undefined value.

Close the source with the given URI.

Parameters:

  • uri (String)


121
122
123
# File 'lib/solargraph/language_server/host/sources.rb', line 121

def close uri
  open_source_hash.delete uri
end

#find(uri) ⇒ Source

Find the source with the given URI.

Parameters:

  • uri (String)

Returns:

Raises:



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

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.

Parameters:

  • uri (String)

Returns:

  • (Boolean)


128
129
130
# File 'lib/solargraph/language_server/host/sources.rb', line 128

def include? uri
  open_source_hash.key? uri
end

#next_uriString

Returns:

  • (String)


53
54
55
56
# File 'lib/solargraph/language_server/host/sources.rb', line 53

def next_uri
  @has_uri.wait(mutex) if queue.empty?
  queue.shift
end

#open(uri, text, version) ⇒ Source

Open a source.

Parameters:

  • uri (String)
  • text (String)
  • version (Integer)

Returns:



69
70
71
72
73
# File 'lib/solargraph/language_server/host/sources.rb', line 69

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



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

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

#startvoid

This method returns an undefined value.



25
26
27
28
29
30
31
# File 'lib/solargraph/language_server/host/sources.rb', line 25

def start
  return unless @stopped
  @stopped = false
  Thread.new do
    tick until stopped?
  end
end

#stopvoid

This method returns an undefined value.



59
60
61
# File 'lib/solargraph/language_server/host/sources.rb', line 59

def stop
  @stopped = true
end

#stopped?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/solargraph/language_server/host/sources.rb', line 20

def stopped?
  @stopped
end

#tickvoid

This method returns an undefined value.



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

def tick
  uri = mutex.synchronize { next_uri }

  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.

Parameters:

Returns:

Raises:



87
88
89
90
91
92
# File 'lib/solargraph/language_server/host/sources.rb', line 87

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