Module: MilkTea::LSP::Workspace::WorkspaceAnalysis
- Included in:
- MilkTea::LSP::Workspace
- Defined in:
- lib/milk_tea/lsp/workspace/analysis.rb
Instance Method Summary collapse
- #analyze_document(uri) ⇒ Object
- #ast_for_content(uri, content) ⇒ Object
- #compute_facts_for_content(uri, content) ⇒ Object
- #effective_platform_for_path(path) ⇒ Object
- #elapsed_ms(start_time) ⇒ Object
- #ensure_root_platform_compatible!(path, effective_platform) ⇒ Object
-
#lex_document(uri) ⇒ Object
── Compilation helpers ─────────────────────────────────────────────────.
- #log_perf_breakdown(name, elapsed_ms_value, detail) ⇒ Object
- #monotonic_time ⇒ Object
- #package_graph_for_path(path, locked: false) ⇒ Object
- #parse_document(uri) ⇒ Object
- #perf_breakdown_logging?(elapsed_ms_value) ⇒ Boolean
- #perf_logging? ⇒ Boolean
- #perf_verbose? ⇒ Boolean
-
#warm_document_facts(uri, content, warm: true) ⇒ Object
Eagerly compute and cache facts for
uri. - #with_inferred_module_name(ast, loader:, path:) ⇒ Object
Instance Method Details
#analyze_document(uri) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 136 def analyze_document(uri) path = uri_to_path(uri) snapshot = if path && File.file?(path) parse_result = MilkTea::Parser.parse_collecting_errors(get_content(uri), path: uri) ast = parse_result.ast return @last_good_tooling_snapshot_cache[uri] if ast.nil? @ast_cache[uri] = ast resolution = DependencyResolution.resolve(path, mode: @dependency_resolution_mode) return nil unless resolution.ok? effective_platform = effective_platform_for_path(path) ensure_root_platform_compatible!(path, effective_platform) loader = MilkTea::ModuleLoader.new( module_roots: module_roots_for_path(path, locked: resolution.locked), package_graph: package_graph_for_path(path, locked: resolution.locked), shared_cache: @shared_module_cache, source_overrides: file_backed_source_overrides, platform: effective_platform, ) ast = with_inferred_module_name(ast, loader:, path:) import_resolution = loader.imported_modules_for_ast_collecting_errors(ast, importer_path: path) MilkTea::SemanticAnalyzer.tooling_snapshot( ast, imported_modules: import_resolution.modules, allow_missing_imports: true, path: path, ) else ast = get_ast(uri) return @last_good_tooling_snapshot_cache[uri] if ast.nil? result = MilkTea::SemanticAnalyzer.check_collecting_errors(ast, path:) facts = result[:analysis] MilkTea::SemanticAnalyzer::ToolingSnapshot.new(facts:, diagnostics: (Array(result[:errors]).map { |e| e.to_diagnostic(path:) } || []).freeze) end if snapshot&.facts @last_good_tooling_snapshot_cache[uri] = snapshot @last_good_facts_cache[uri] = snapshot.facts @document_module_names[uri] = snapshot.facts.module_name end snapshot rescue MilkTea::LexError, MilkTea::SemanticError, ModuleLoadError, PackageLockError @last_good_tooling_snapshot_cache[uri] rescue StandardError => e warn "LSP sema error #{uri}: #{e.}" @last_good_tooling_snapshot_cache[uri] end |
#ast_for_content(uri, content) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 57 def ast_for_content(uri, content) return get_ast(uri) if get_content(uri) == content MilkTea::Parser.parse(content, path: uri) rescue StandardError nil end |
#compute_facts_for_content(uri, content) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 104 def compute_facts_for_content(uri, content) path = uri_to_path(uri) if path && File.file?(path) parse_result = MilkTea::Parser.parse_collecting_errors(content, path: uri) ast = parse_result.ast return nil unless ast effective_platform = effective_platform_for_path(path) ensure_root_platform_compatible!(path, effective_platform) loader = MilkTea::ModuleLoader.new( module_roots: module_roots_for_path(path), package_graph: package_graph_for_path(path), shared_cache: @shared_module_cache, source_overrides: file_backed_source_overrides, platform: effective_platform, ) ast = with_inferred_module_name(ast, loader:, path:) import_resolution = loader.imported_modules_for_ast_collecting_errors(ast, importer_path: path) MilkTea::SemanticAnalyzer.tooling_snapshot( ast, imported_modules: import_resolution.modules, allow_missing_imports: true, path: path, ).facts else ast = MilkTea::Parser.parse(content, path: uri) MilkTea::SemanticAnalyzer.check(ast) end rescue MilkTea::LexError, MilkTea::SemanticError, ModuleLoadError nil end |
#effective_platform_for_path(path) ⇒ Object
206 207 208 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 206 def effective_platform_for_path(path) ModuleLoader.effective_platform_for_path(path, platform_override: @platform_override) end |
#elapsed_ms(start_time) ⇒ Object
47 48 49 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 47 def elapsed_ms(start_time) ((monotonic_time - start_time) * 1000).round(1) end |
#ensure_root_platform_compatible!(path, effective_platform) ⇒ Object
210 211 212 213 214 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 210 def ensure_root_platform_compatible!(path, effective_platform) return unless ModuleLoader.platform_suffix_for_path(path) ModuleLoader.resolve_source_path(path, platform: effective_platform, error_class: ModuleLoadError) end |
#lex_document(uri) ⇒ Object
── Compilation helpers ─────────────────────────────────────────────────
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 9 def lex_document(uri) content = get_content(uri) return nil if content.empty? recovery_errors = [] tokens = MilkTea::Lexer.lex(content, path: uri, recovery_errors:) @last_good_tokens_cache[uri] = tokens index_identifier_tokens(uri, tokens) if tokens tokens rescue StandardError @last_good_tokens_cache[uri] || @tokens_cache[uri] end |
#log_perf_breakdown(name, elapsed_ms_value, detail) ⇒ Object
51 52 53 54 55 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 51 def log_perf_breakdown(name, elapsed_ms_value, detail) return unless perf_breakdown_logging?(elapsed_ms_value) warn "[LSP perf] breakdown #{name} #{elapsed_ms_value}ms #{detail}" end |
#monotonic_time ⇒ Object
43 44 45 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 43 def monotonic_time Process.clock_gettime(Process::CLOCK_MONOTONIC) end |
#package_graph_for_path(path, locked: false) ⇒ Object
187 188 189 190 191 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 187 def package_graph_for_path(path, locked: false) PackageGraph.load(path, locked:) rescue PackageManifestError nil end |
#parse_document(uri) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 22 def parse_document(uri) tokens = get_tokens(uri) return nil if tokens.nil? MilkTea::Parser.parse_collecting_errors(tokens: tokens, path: uri).ast rescue StandardError nil end |
#perf_breakdown_logging?(elapsed_ms_value) ⇒ Boolean
39 40 41 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 39 def perf_breakdown_logging?(elapsed_ms_value) perf_logging? && (perf_verbose? || elapsed_ms_value > PERF_LOG_THRESHOLD_MS) end |
#perf_logging? ⇒ Boolean
31 32 33 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 31 def perf_logging? @perf_logging ||= !ENV.fetch('MILK_TEA_LSP_PERF', nil).to_s.empty? end |
#perf_verbose? ⇒ Boolean
35 36 37 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 35 def perf_verbose? @perf_verbose ||= ENV.fetch('MILK_TEA_LSP_PERF', nil).to_s == 'verbose' end |
#warm_document_facts(uri, content, warm: true) ⇒ Object
Eagerly compute and cache facts for uri. The keystroke path
(didChange) passes warm: false so the sema/import-resolution cost runs
on the debounced diagnostics worker instead of the request thread;
requests then serve last-good facts until the background pass lands.
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 102 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 69 def warm_document_facts(uri, content, warm: true) stats = { bytes: content.bytesize, lines: content.count("\n") + 1, eager_facts: false, facts_mode: nil, facts_ms: nil, skip_reason: nil, import_count: 0, shared_module_cache_size: @shared_module_cache.length, } if background_document?(uri) stats[:skip_reason] = :background_document return stats end unless warm stats[:skip_reason] = :deferred return stats end ast = ast_for_content(uri, content) stats[:import_count] = ast.respond_to?(:imports) ? ast.imports.length : 0 facts_start = Process.clock_gettime(Process::CLOCK_MONOTONIC) facts_path = uri_to_path(uri) get_facts(uri) stats[:eager_facts] = true stats[:facts_mode] = facts_path && File.file?(facts_path) ? :module_loader : :memory stats[:facts_ms] = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - facts_start) * 1000).round(1) stats[:shared_module_cache_size] = @shared_module_cache.length stats end |
#with_inferred_module_name(ast, loader:, path:) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/milk_tea/lsp/workspace/analysis.rb', line 193 def with_inferred_module_name(ast, loader:, path:) inferred_module_name = loader.send(:inferred_module_name_for_path, path) AST::SourceFile.new( module_name: AST::QualifiedName.new(inferred_module_name.split('.')), module_kind: ast.module_kind, imports: ast.imports, directives: ast.directives, declarations: ast.declarations, line: ast.line, node_ids: ast.node_ids, ) end |