Module: Solargraph::LanguageServer::Host::Dispatch

Included in:
Solargraph::LanguageServer::Host
Defined in:
lib/solargraph/language_server/host/dispatch.rb

Overview

Methods for associating sources with libraries via URIs.

Instance Method Summary collapse

Instance Method Details

#diagnoserHost::Diagnoser

This method is abstract.

Returns:

Raises:

  • (NotImplementedError)


11
12
13
# File 'lib/solargraph/language_server/host/dispatch.rb', line 11

def diagnoser
  raise NotImplementedError, 'Host::Dispatch requires a diagnoser method'
end

#explicit_library_for(uri) ⇒ Library?

Find an explicit library match for the given URI. An explicit match means the libary’s workspace includes the file.

If a matching library is found, the source corresponding to the URI gets attached to it.

Parameters:

  • uri (String)

Returns:

Raises:



65
66
67
68
69
70
71
72
73
74
# File 'lib/solargraph/language_server/host/dispatch.rb', line 65

def explicit_library_for uri
  filename = UriHelpers.uri_to_file(uri)
  libraries.each do |lib|
    if lib.contain?(filename)
      lib.attach sources.find(uri) if sources.include?(uri)
      return lib
    end
  end
  nil
end

#generic_libraryLibrary

Returns:



116
117
118
119
# File 'lib/solargraph/language_server/host/dispatch.rb', line 116

def generic_library
  @generic_library ||= Solargraph::Library.new(Solargraph::Workspace.new('', nil, options), nil)
                                          .tap { |lib| lib.add_observer self }
end

#generic_library_for(uri) ⇒ Library

Get a generic library for the given URI and attach the corresponding source.

Parameters:

  • uri (String)

Returns:

Raises:



110
111
112
113
# File 'lib/solargraph/language_server/host/dispatch.rb', line 110

def generic_library_for uri
  generic_library.attach sources.find(uri)
  generic_library
end

#implicit_library_for(uri) ⇒ Library?

Find an implicit library match for the given URI. An implicit match means the file is located inside the library’s workspace directory, regardless of whether the workspace is configured to include it.

If a matching library is found, the source corresponding to the URI gets attached to it.

Parameters:

  • uri (String)

Returns:

Raises:



87
88
89
90
91
92
93
94
95
96
# File 'lib/solargraph/language_server/host/dispatch.rb', line 87

def implicit_library_for uri
  filename = UriHelpers.uri_to_file(uri)
  libraries.each do |lib|
    if filename.start_with?(lib.workspace.directory)
      lib.attach sources.find(uri)
      return lib
    end
  end
  nil
end

#libraries::Array<Library>

Returns:



25
26
27
# File 'lib/solargraph/language_server/host/dispatch.rb', line 25

def libraries
  @libraries ||= []
end

#library_for(uri) ⇒ Library

Find the best libary match for the given URI.

Parameters:

  • uri (String)

Returns:



46
47
48
49
50
51
52
53
# File 'lib/solargraph/language_server/host/dispatch.rb', line 46

def library_for uri
  result = explicit_library_for(uri) ||
    implicit_library_for(uri) ||
    generic_library_for(uri)
  # previous library for already call attach. avoid call twice
  # result.attach sources.find(uri) if sources.include?(uri)
  result
end

#optionsHash{String => undefined}

Returns:

  • (Hash{String => undefined})


99
100
101
# File 'lib/solargraph/language_server/host/dispatch.rb', line 99

def options
  @options ||= {}.freeze
end

#sourcesSources

Returns:



16
17
18
19
20
21
22
# File 'lib/solargraph/language_server/host/dispatch.rb', line 16

def sources
  @sources ||= begin
    src = Sources.new
    src.add_observer self, :update_libraries
    src
  end
end

#update(progress) ⇒ void

This method returns an undefined value.

Parameters:



124
125
126
# File 'lib/solargraph/language_server/host/dispatch.rb', line 124

def update progress
  progress&.send(self)
end

#update_libraries(uri) ⇒ void

This method returns an undefined value.

The Sources observer callback that merges a source into the host’s libraries when it gets updated.

Parameters:

  • uri (String)


34
35
36
37
38
39
40
# File 'lib/solargraph/language_server/host/dispatch.rb', line 34

def update_libraries uri
  src = sources.find(uri)
  using = libraries.select { |lib| lib.contain?(src.filename) }
  using.push library_for(uri) if using.empty?
  using.each { |lib| lib.merge src }
  diagnoser.schedule uri
end