Module: MilkTea::LSP::Workspace::WorkspaceCaches
- Included in:
- MilkTea::LSP::Workspace
- Defined in:
- lib/milk_tea/lsp/workspace/caches.rb
Instance Method Summary collapse
- #all_documents ⇒ Object
- #doc_comment_data_for_definition(uri, token) ⇒ Object
- #doc_comment_for_definition(uri, token) ⇒ Object
-
#find_all_references(name) ⇒ Object
Return all identifier token locations matching
nameacross all known documents. - #find_all_references_in(name, uris) ⇒ Object
- #get_ast(uri) ⇒ Object
- #get_content(uri) ⇒ Object
- #get_facts(uri, allow_last_good_fallback: true) ⇒ Object
- #get_symbols(uri) ⇒ Object
- #get_tokens(uri) ⇒ Object
- #get_tooling_snapshot(uri, allow_last_good_fallback: true) ⇒ Object
- #index_identifier_tokens(uri, tokens) ⇒ Object
- #module_name_for_uri(uri) ⇒ Object
-
#peek_facts(uri) ⇒ Object
Return the most recent facts already computed for
uriwithout ever running analysis or blocking on the analysis mutex. - #position_to_offset(uri, line, char) ⇒ Object
- #remove_identifier_index_entries(uri) ⇒ Object
Instance Method Details
#all_documents ⇒ Object
150 151 152 153 154 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 150 def all_documents @document_state_mutex.synchronize do (@indexed_documents.keys + @open_documents.keys).uniq end end |
#doc_comment_data_for_definition(uri, token) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 138 def doc_comment_data_for_definition(uri, token) return nil unless token docs_by_location = @doc_comments_cache[uri] ||= extract_doc_comments_for_definitions(uri) docs_by_location[doc_comment_key(token.line, token.column)] end |
#doc_comment_for_definition(uri, token) ⇒ Object
132 133 134 135 136 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 132 def doc_comment_for_definition(uri, token) return nil unless token doc_comment_data_for_definition(uri, token)&.fetch(:raw_markdown, nil) end |
#find_all_references(name) ⇒ Object
Return all identifier token locations matching name across all known documents.
Uses the identifier index for known documents, falling back to scanning.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 162 def find_all_references(name) refs = [] indexed_entries = @identifier_index_mutex.synchronize { @identifier_index[name]&.dup } if indexed_entries indexed_entries.each do |entry| refs << { uri: entry[:uri], range: { start: { line: entry[:line] - 1, character: entry[:col] - 1 }, end: { line: entry[:line] - 1, character: entry[:col] - 1 + name.length }, }, } end end unindexed = all_documents.reject { |uri| @indexed_uris.include?(uri) } unless unindexed.empty? refs.concat(scan_for_references(name, unindexed)) end refs end |
#find_all_references_in(name, uris) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 184 def find_all_references_in(name, uris) refs = [] uriset = uris.to_set indexed_entries = @identifier_index_mutex.synchronize { @identifier_index[name]&.dup } if indexed_entries indexed_entries.each do |entry| next unless uriset.include?(entry[:uri]) refs << { uri: entry[:uri], range: { start: { line: entry[:line] - 1, character: entry[:col] - 1 }, end: { line: entry[:line] - 1, character: entry[:col] - 1 + name.length }, }, } end end unindexed = uris.reject { |uri| @indexed_uris.include?(uri) } unless unindexed.empty? refs.concat(scan_for_references(name, unindexed)) end refs end |
#get_ast(uri) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 25 def get_ast(uri) @ast_cache[uri] ||= begin # Prefer the facts' own AST so consumers that iterate the AST and # resolve bindings via facts.binding_resolution operate on the same # node instances (binding IDs are keyed by AST node object_id). peek_facts(uri)&.ast || parse_document(uri) end end |
#get_content(uri) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 7 def get_content(uri) @document_state_mutex.synchronize do @open_documents[uri] || @indexed_documents[uri] || begin path = uri_to_path(uri) if path && File.file?(path) @indexed_documents[uri] = File.read(path) end rescue StandardError nil end || '' end end |
#get_facts(uri, allow_last_good_fallback: true) ⇒ Object
34 35 36 37 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 34 def get_facts(uri, allow_last_good_fallback: true) snapshot = get_tooling_snapshot(uri, allow_last_good_fallback:) snapshot&.facts end |
#get_symbols(uri) ⇒ Object
128 129 130 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 128 def get_symbols(uri) @symbols_cache[uri] ||= extract_symbols_from_tokens(uri) end |
#get_tokens(uri) ⇒ Object
21 22 23 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 21 def get_tokens(uri) @tokens_cache[uri] ||= lex_document(uri) end |
#get_tooling_snapshot(uri, allow_last_good_fallback: true) ⇒ Object
50 51 52 53 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 50 def get_tooling_snapshot(uri, allow_last_good_fallback: true) total_start = perf_logging? ? monotonic_time : nil cache_state = 'miss' lock_wait_ms = 0.0 analyze_ms = 0.0 snapshot = nil last_good_snapshot = nil generation = nil @facts_cache_mutex.synchronize do generation = @facts_generation[uri] cached = @tooling_snapshot_cache[uri] last_good_snapshot = @last_good_tooling_snapshot_cache[uri] if cached cache_state = 'hit' snapshot = cached end end return snapshot if snapshot compute_snapshot = lambda do @facts_cache_mutex.synchronize do generation = @facts_generation[uri] cached = @tooling_snapshot_cache[uri] if cached cache_state = 'hit' snapshot = cached end end unless snapshot analyze_start = total_start ? monotonic_time : nil snapshot = analyze_document(uri) analyze_ms = elapsed_ms(analyze_start) if analyze_start @facts_cache_mutex.synchronize do if @facts_generation[uri] == generation @tooling_snapshot_cache[uri] = snapshot if snapshot if snapshot&.facts @facts_cache[uri] = snapshot.facts @last_good_facts_cache[uri] = snapshot.facts @last_good_tooling_snapshot_cache[uri] = snapshot end update_dependency_index(uri, snapshot&.facts) if snapshot else cache_state = 'stale' snapshot = @tooling_snapshot_cache[uri] || @last_good_tooling_snapshot_cache[uri] end end end end lock_wait_start = total_start ? monotonic_time : nil if @facts_state_mutex.try_lock begin compute_snapshot.call ensure @facts_state_mutex.unlock end elsif allow_last_good_fallback && last_good_snapshot cache_state = 'last_good' snapshot = last_good_snapshot else @facts_state_mutex.synchronize do lock_wait_ms = elapsed_ms(lock_wait_start) if lock_wait_start compute_snapshot.call end end snapshot ensure if total_start result_state = snapshot&.facts.nil? ? 'nil' : 'ok' log_perf_breakdown( 'workspace/get_tooling_snapshot', elapsed_ms(total_start), "uri=#{uri} cache=#{cache_state} result=#{result_state} stages_ms=lock_wait:#{lock_wait_ms},analyze:#{analyze_ms}", ) end end |
#index_identifier_tokens(uri, tokens) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 209 def index_identifier_tokens(uri, tokens) return unless tokens @identifier_index_mutex.synchronize do @indexed_uris << uri tokens.each do |tok| next unless tok.type == :identifier (@identifier_index[tok.lexeme] ||= []) << { uri: uri, line: tok.line, col: tok.column } end end end |
#module_name_for_uri(uri) ⇒ Object
156 157 158 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 156 def module_name_for_uri(uri) @document_module_names[uri] end |
#peek_facts(uri) ⇒ Object
Return the most recent facts already computed for uri without ever
running analysis or blocking on the analysis mutex. Used by hot request
paths (semantic tokens) that must not stall the request thread on sema;
they serve the latest cached/last-good facts and are told to re-fetch
once fresh facts land in the background.
44 45 46 47 48 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 44 def peek_facts(uri) @facts_cache_mutex.synchronize do @facts_cache[uri] || @last_good_facts_cache[uri] end end |
#position_to_offset(uri, line, char) ⇒ Object
145 146 147 148 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 145 def position_to_offset(uri, line, char) content = get_content(uri) line_char_to_offset(content, line, char) end |
#remove_identifier_index_entries(uri) ⇒ Object
222 223 224 225 226 227 228 229 230 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 222 def remove_identifier_index_entries(uri) @identifier_index_mutex.synchronize do @indexed_uris.delete(uri) @identifier_index.each_value do |entries| entries.reject! { |entry| entry[:uri] == uri } end @identifier_index.delete_if { |_name, entries| entries.empty? } end end |