Module: MilkTea::LSP::Workspace::WorkspaceDependencyGraph

Included in:
MilkTea::LSP::Workspace
Defined in:
lib/milk_tea/lsp/workspace/dependency_graph.rb

Instance Method Summary collapse

Instance Method Details

#apply_watched_file_change(uri, change_type) ⇒ Object

Apply a workspace/didChangeWatchedFiles change to the indexed snapshot. Open documents are source-of-truth and are left untouched.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 9

def apply_watched_file_change(uri, change_type)
  return [] if @document_state_mutex.synchronize { @open_documents.key?(uri) }

  if change_type.to_i == 3 # Deleted
    @document_state_mutex.synchronize do
      @indexed_documents.delete(uri)
    end
    invalidate_cache(uri)
    return refresh_import_dependent_caches(changed_uri: uri)
  end

  path = uri_to_path(uri)
  return [] unless path && File.file?(path)

  @document_state_mutex.synchronize do
    @indexed_documents[uri] = File.read(path)
  end
  invalidate_cache(uri)
  enqueue_definition_warmup(uri)
  affected_uris = refresh_import_dependent_caches(changed_uri: uri)
  if affected_uris.empty? && change_type.to_i == 1 # Created
    affected_uris = refresh_import_dependent_caches
    affected_uris.delete(uri)
  end
  affected_uris
rescue StandardError => e
  warn "LSP watched-file update error #{uri}: #{e.message}"
  []
end

#clear_dependency_indexObject



103
104
105
106
107
108
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 103

def clear_dependency_index
  @dependency_module_name_by_uri.clear
  @dependency_imports_by_uri.clear
  @reverse_import_dependents.clear
  @full_reverse_index_built = false
end

#clear_shared_module_cacheObject

Drop all cached module analyses (and the per-uri snapshots derived from them) when an analysis input that was captured in those analyses is no longer authoritative — e.g. a closed document whose buffer differed from disk. Kept rare: closing a file is infrequent, so a wholesale clear is safer than reasoning about which dependents could have observed the closed buffer. Open documents keep their last-known-good snapshots so they keep serving rich features while re-analysis lands.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 117

def clear_shared_module_cache
  @facts_state_mutex.synchronize do
    @facts_cache_mutex.synchronize do
      all_open = @document_state_mutex.synchronize { @open_documents.keys }
      preserved_facts = all_open.each_with_object({}) do |open_uri, preserved|
        facts = @last_good_facts_cache[open_uri]
        preserved[open_uri] = facts if facts
      end
      preserved_snapshots = all_open.each_with_object({}) do |open_uri, preserved|
        snapshot = @last_good_tooling_snapshot_cache[open_uri]
        preserved[open_uri] = snapshot if snapshot
      end
      @shared_module_cache.clear
      @facts_cache.clear
      @tooling_snapshot_cache.clear
      @diagnostics_cache.clear
      @last_good_facts_cache.clear
      @last_good_tooling_snapshot_cache.clear
      preserved_snapshots.each { |open_uri, snapshot| @last_good_tooling_snapshot_cache[open_uri] = snapshot }
      preserved_facts.each { |open_uri, facts| @last_good_facts_cache[open_uri] = facts }
    end
  end
end

#delete_dependency_index(uri) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 183

def delete_dependency_index(uri)
  imported_module_names = @dependency_imports_by_uri.delete(uri) || Set.new
  imported_module_names.each do |module_name|
    dependents = @reverse_import_dependents[module_name]
    dependents.delete(uri)
    @reverse_import_dependents.delete(module_name) if dependents.empty?
  end
  @dependency_module_name_by_uri.delete(uri)
end

#dependency_index_imports_for(uri) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 163

def dependency_index_imports_for(uri)
  ast = @ast_cache[uri]
  unless ast
    content = get_content(uri)
    return nil if content.empty?

    path = uri_to_path(uri)
    ast = if path && File.file?(path)
            MilkTea::Parser.parse_collecting_errors(content, path: uri).ast
          else
            MilkTea::Parser.parse(content, path: uri)
          end
  end
  return nil unless ast

  ast.imports.map { |import| import.path.to_s }.to_set
rescue MilkTea::LexError, MilkTea::ParseError
  nil
end

#dependent_open_document_uris_for(changed_uri, open_document_uris) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 193

def dependent_open_document_uris_for(changed_uri, open_document_uris)
  return open_document_uris.reject { |open_uri| open_uri == changed_uri } unless changed_uri

  module_name = @dependency_module_name_by_uri[changed_uri] || infer_module_name_for_uri(changed_uri)
  return open_document_uris.reject { |open_uri| open_uri == changed_uri } unless module_name

  dependents = @reverse_import_dependents[module_name]
  open_document_uris.select { |open_uri| open_uri != changed_uri && dependents.include?(open_uri) }
end

#ensure_full_reverse_import_indexObject



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 283

def ensure_full_reverse_import_index
  return if @full_reverse_index_built

  all_uris = @document_state_mutex.synchronize do
    (@indexed_documents.keys + @open_documents.keys).uniq
  end

  all_uris.each do |uri|
    next if @dependency_imports_by_uri.key?(uri)

    imported = dependency_index_imports_for(uri)
    next unless imported

    @dependency_imports_by_uri[uri] = imported
    imported.each do |mod_name|
      @reverse_import_dependents[mod_name] << uri
    end
  end

  @full_reverse_index_built = true
end

#infer_module_name_for_uri(uri) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 203

def infer_module_name_for_uri(uri)
  path = uri_to_path(uri)
  return nil unless path

  resolution = DependencyResolution.resolve(path, mode: @dependency_resolution_mode)
  return nil if resolution.error_message

  loader = ModuleLoader.new(
    module_roots: module_roots_for_path(path, locked: resolution.locked),
    package_graph: package_graph_for_path(path, locked: resolution.locked),
    platform: effective_platform_for_path(path),
  )
  loader.send(:inferred_module_name_for_path, path)
rescue StandardError
  nil
end

#module_roots_for_path(path, locked: false) ⇒ Object



220
221
222
223
224
225
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 220

def module_roots_for_path(path, locked: false)
  roots = MilkTea::ModuleRoots.roots_for_path(path, locked: locked)
  return roots unless workspace_root_applicable_for?(path)

  [@workspace_root_path, *roots].uniq
end

#normalize_workspace_root_path(path) ⇒ Object



234
235
236
237
238
239
240
241
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 234

def normalize_workspace_root_path(path)
  return nil if path.nil? || path.to_s.strip.empty?

  expanded_path = File.expand_path(path.to_s)
  return nil unless File.directory?(expanded_path)

  expanded_path
end

#open_document_urisObject



46
47
48
49
50
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 46

def open_document_uris
  @document_state_mutex.synchronize do
    @open_documents.keys.dup
  end
end

#refresh_import_dependent_caches(changed_uri: nil) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 243

def refresh_import_dependent_caches(changed_uri: nil)
  all_open_document_uris = @document_state_mutex.synchronize do
    @open_documents.keys
  end
  open_document_uris = @facts_cache_mutex.synchronize do
    dependent_open_document_uris_for(changed_uri, all_open_document_uris)
  end

  @facts_state_mutex.synchronize do
    @facts_cache_mutex.synchronize do
      preserved_open_last_good_facts = all_open_document_uris.each_with_object({}) do |open_uri, preserved|
        facts = @last_good_facts_cache[open_uri]
        preserved[open_uri] = facts if facts
      end
      preserved_open_last_good_snapshots = all_open_document_uris.each_with_object({}) do |open_uri, preserved|
        snapshot = @last_good_tooling_snapshot_cache[open_uri]
        preserved[open_uri] = snapshot if snapshot
      end
      @shared_module_cache.clear
      @facts_cache.clear
      @tooling_snapshot_cache.clear
      @diagnostics_cache.clear
      @last_good_facts_cache.clear
      @last_good_tooling_snapshot_cache.clear
      preserved_open_last_good_snapshots.each do |open_uri, snapshot|
        @last_good_tooling_snapshot_cache[open_uri] = snapshot
      end
      preserved_open_last_good_facts.each do |open_uri, facts|
        @last_good_facts_cache[open_uri] = facts
      end
    end
  end
  open_document_uris
end

#refresh_open_document_dependency_caches(changed_uri) ⇒ Object



39
40
41
42
43
44
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 39

def refresh_open_document_dependency_caches(changed_uri)
  path = uri_to_path(changed_uri)
  return [] unless path

  refresh_import_dependent_caches(changed_uri: changed_uri)
end

Returns open documents connected to seed_uri through the import dependency graph (imports and reverse dependents).



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 54

def related_open_document_uris(seed_uri)
  open_uris = @document_state_mutex.synchronize do
    @open_documents.keys.dup
  end
  return [] if open_uris.empty?

  @facts_cache_mutex.synchronize do
    open_uris.each do |uri|
      facts = @facts_cache[uri]
      update_dependency_index(uri, facts)
    end

    open_set = open_uris.to_set
    return [seed_uri] unless open_set.include?(seed_uri)

    module_to_uris = Hash.new { |hash, key| hash[key] = Set.new }
    open_uris.each do |uri|
      module_name = @dependency_module_name_by_uri[uri]
      module_to_uris[module_name] << uri if module_name
    end

    visited = Set.new
    queue = [seed_uri]

    until queue.empty?
      current = queue.shift
      next if visited.include?(current)

      visited << current

      current_module = @dependency_module_name_by_uri[current]
      if current_module
        @reverse_import_dependents[current_module].each do |dependent_uri|
          queue << dependent_uri if open_set.include?(dependent_uri) && !visited.include?(dependent_uri)
        end
      end

      imported_modules = @dependency_imports_by_uri[current] || Set.new
      imported_modules.each do |imported_module_name|
        module_to_uris[imported_module_name].each do |imported_uri|
          queue << imported_uri if open_set.include?(imported_uri) && !visited.include?(imported_uri)
        end
      end
    end

    visited.to_a
  end
end

#reverse_import_dependents_for(module_name) ⇒ Object



278
279
280
281
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 278

def reverse_import_dependents_for(module_name)
  ensure_full_reverse_import_index
  @reverse_import_dependents[module_name]
end

#update_dependency_index(uri, facts) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 141

def update_dependency_index(uri, facts)
  imported_module_names = if facts
    facts.imports.each_value.filter_map(&:name).to_set
  else
    dependency_index_imports_for(uri)
  end

  if imported_module_names&.empty?
    imported_module_names = dependency_index_imports_for(uri) || Set.new
  end
  return if imported_module_names.nil? && facts.nil?

  delete_dependency_index(uri)

  module_name = facts&.module_name || infer_module_name_for_uri(uri)
  @dependency_module_name_by_uri[uri] = module_name if module_name
  @dependency_imports_by_uri[uri] = imported_module_names
  imported_module_names.each do |module_name|
    @reverse_import_dependents[module_name] << uri
  end
end

#workspace_root_applicable_for?(path) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 227

def workspace_root_applicable_for?(path)
  return false if @workspace_root_path.nil? || path.nil?

  expanded_path = File.expand_path(path)
  expanded_path == @workspace_root_path || expanded_path.start_with?(@workspace_root_path + File::SEPARATOR)
end