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
- #position_to_offset(uri, line, char) ⇒ Object
- #remove_identifier_index_entries(uri) ⇒ Object
Instance Method Details
#all_documents ⇒ Object
134 135 136 137 138 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 134 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
122 123 124 125 126 127 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 122 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
116 117 118 119 120 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 116 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.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 146 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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 168 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 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 25 def get_ast(uri) @ast_cache[uri] ||= parse_document(uri) 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
29 30 31 32 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 29 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
112 113 114 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 112 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
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 34 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
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 193 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
140 141 142 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 140 def module_name_for_uri(uri) @document_module_names[uri] end |
#position_to_offset(uri, line, char) ⇒ Object
129 130 131 132 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 129 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
206 207 208 209 210 211 212 213 214 |
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 206 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 |