Module: MilkTea::LSP::Workspace::WorkspaceDependencyGraph
- Included in:
- MilkTea::LSP::Workspace
- Defined in:
- lib/milk_tea/lsp/workspace/dependency_graph.rb
Instance Method Summary collapse
-
#apply_watched_file_change(uri, change_type) ⇒ Object
Apply a workspace/didChangeWatchedFiles change to the indexed snapshot.
- #clear_dependency_index ⇒ Object
- #delete_dependency_index(uri) ⇒ Object
- #dependency_index_imports_for(uri) ⇒ Object
- #dependent_open_document_uris_for(changed_uri, open_document_uris) ⇒ Object
- #ensure_full_reverse_import_index ⇒ Object
- #infer_module_name_for_uri(uri) ⇒ Object
- #module_roots_for_path(path, locked: false) ⇒ Object
- #normalize_workspace_root_path(path) ⇒ Object
- #open_document_uris ⇒ Object
- #refresh_import_dependent_caches(changed_uri: nil) ⇒ Object
- #refresh_open_document_dependency_caches(changed_uri) ⇒ Object
-
#related_open_document_uris(seed_uri) ⇒ Object
Returns open documents connected to
seed_urithrough the import dependency graph (imports and reverse dependents). - #reverse_import_dependents_for(module_name) ⇒ Object
- #update_dependency_index(uri, facts) ⇒ Object
- #workspace_root_applicable_for?(path) ⇒ Boolean
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.}" [] end |
#clear_dependency_index ⇒ Object
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 |
#delete_dependency_index(uri) ⇒ Object
152 153 154 155 156 157 158 159 160 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 152 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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 132 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
162 163 164 165 166 167 168 169 170 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 162 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_index ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 252 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
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 172 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. 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
189 190 191 192 193 194 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 189 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
203 204 205 206 207 208 209 210 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 203 def normalize_workspace_root_path(path) return nil if path.nil? || path.to_s.strip.empty? = File.(path.to_s) return nil unless File.directory?() end |
#open_document_uris ⇒ Object
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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 212 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 |
#related_open_document_uris(seed_uri) ⇒ Object
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 (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
247 248 249 250 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 247 def reverse_import_dependents_for(module_name) ensure_full_reverse_import_index @reverse_import_dependents[module_name] end |
#update_dependency_index(uri, facts) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 110 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
196 197 198 199 200 201 |
# File 'lib/milk_tea/lsp/workspace/dependency_graph.rb', line 196 def workspace_root_applicable_for?(path) return false if @workspace_root_path.nil? || path.nil? = File.(path) == @workspace_root_path || .start_with?(@workspace_root_path + File::SEPARATOR) end |