Module: MilkTea::LSP::Workspace::WorkspaceCaches

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

Instance Method Summary collapse

Instance Method Details

#all_documentsObject



177
178
179
180
181
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 177

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



165
166
167
168
169
170
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 165

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



159
160
161
162
163
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 159

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.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 189

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



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 211

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



155
156
157
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 155

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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
    # No analysis is in flight; computing here is fine (cold paths,
    # tests). It does not block on other work.
    begin
      compute_snapshot.call
    ensure
      @facts_state_mutex.unlock
    end
  elsif allow_last_good_fallback && last_good_snapshot
    # Facts exist from a prior pass; never block on the in-flight one.
    snapshot = last_good_snapshot
    cache_state = 'last_good'
  elsif allow_last_good_fallback
    # No facts yet and a background analysis is computing this document.
    # Wait briefly (bounded) for it so the request usually returns fresh
    # facts instead of a lexical fallback — without duplicating the work
    # (we never run analysis here) or blocking indefinitely. Beyond the
    # budget, serve nil and let the handler fall back.
    deadline = monotonic_time + (IN_FLIGHT_FACTS_WAIT_MS / 1000.0)
    acquired = false
    while monotonic_time < deadline
      if @facts_state_mutex.try_lock
        acquired = true
        break
      end
      sleep 0.02
    end
    if acquired
      begin
        compute_snapshot.call
      ensure
        @facts_state_mutex.unlock
      end
    else
      cache_state = 'nil'
    end
  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



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 236

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



183
184
185
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 183

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



172
173
174
175
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 172

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



249
250
251
252
253
254
255
256
257
# File 'lib/milk_tea/lsp/workspace/caches.rb', line 249

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