-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathindexer.h
More file actions
288 lines (230 loc) · 10.8 KB
/
Copy pathindexer.h
File metadata and controls
288 lines (230 loc) · 10.8 KB
1
2
3
4
5
6
7
8
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
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
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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#pragma once
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "semantic/relation_kind.h"
#include "semantic/symbol_kind.h"
#include "server/workspace/workspace.h"
#include "kota/async/async.h"
#include "kota/ipc/codec/json.h"
#include "kota/ipc/lsp/position.h"
#include "kota/ipc/lsp/progress.h"
#include "kota/ipc/lsp/protocol.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
namespace clice {
namespace protocol = kota::ipc::protocol;
namespace lsp = kota::ipc::lsp;
struct Session;
class Compiler;
class WorkerPool;
/// Information about a symbol at a given position.
struct SymbolInfo {
index::SymbolHash hash = 0;
std::string name;
SymbolKind kind;
std::string uri;
protocol::Range range;
};
/// Index query layer and background indexing scheduler.
///
/// Indexer holds no index data of its own. All persistent data lives in
/// Workspace (disk-derived ProjectIndex + MergedIndex shards) and per-file
/// data lives in Session (OpenFileIndex from unsaved buffers).
///
/// Responsibilities:
/// - Cross-file navigation queries (definition, references, hierarchy)
/// - Symbol search (workspace/symbol)
/// - Background indexing scheduling (enqueue → idle timer → worker dispatch)
/// - Merging TUIndex results into Workspace's ProjectIndex
///
/// NOT responsible for:
/// - Compilation — handled by Compiler
/// - Document lifecycle — handled by MasterServer
class Indexer {
public:
Indexer(kota::event_loop& loop,
Workspace& workspace,
llvm::DenseMap<std::uint32_t, Session>& sessions,
WorkerPool& pool,
Compiler& compiler,
std::function<bool(std::uint32_t)> is_file_open = {}) :
loop(loop), bg_tasks(loop), workspace(workspace), sessions(sessions), pool(pool),
compiler(compiler), is_file_open(std::move(is_file_open)) {}
/// Set the LSP peer for progress reporting. Must be called before
/// schedule() if progress notifications are desired.
void set_peer(kota::ipc::JsonPeer* p) {
peer = p;
}
/// Temporarily pause background indexing to give priority to user
/// requests. Indexing tasks already dispatched to workers continue,
/// but no new tasks will be sent until resume_indexing() is called.
void pause_indexing();
/// Resume background indexing after a pause.
void resume_indexing();
/// RAII guard that pauses indexing for its lifetime.
struct [[nodiscard]] ScopedPause {
Indexer& indexer;
explicit ScopedPause(Indexer& idx) : indexer(idx) {
indexer.pause_indexing();
}
~ScopedPause() {
indexer.resume_indexing();
}
ScopedPause(const ScopedPause&) = delete;
ScopedPause& operator=(const ScopedPause&) = delete;
};
ScopedPause scoped_pause() {
return ScopedPause{*this};
}
/// Set the maximum number of concurrent index tasks.
/// Also sets the baseline that dynamic adjustment will restore to.
void set_max_concurrency(std::size_t n) {
max_concurrent = std::max<std::size_t>(n, 1);
baseline_concurrent = max_concurrent;
}
/// Add a file to the background indexing queue.
void enqueue(std::uint32_t server_path_id);
/// Schedule background indexing (respects idle timeout and dedup).
void schedule();
/// Merge a TUIndex result into Workspace's ProjectIndex and MergedIndex shards.
void merge(const void* tu_index_data, std::size_t size);
/// Save Workspace's ProjectIndex and MergedIndex shards to disk.
void save(llvm::StringRef index_dir);
/// Load Workspace's ProjectIndex and MergedIndex shards from disk.
void load(llvm::StringRef index_dir);
/// Check whether a file needs re-indexing (stale or missing shard).
bool need_update(llvm::StringRef file_path);
/// Query relations (Definition, Reference, etc.) for a symbol at cursor.
/// @param session Active Session for this file, or nullptr to use MergedIndex only.
std::vector<protocol::Location> query_relations(llvm::StringRef path,
const protocol::Position& position,
RelationKind kind,
Session* session);
/// Look up symbol info (hash, name, kind, range) at a cursor position.
/// @param session Active Session for this file, or nullptr.
std::optional<SymbolInfo> lookup_symbol(const std::string& uri,
llvm::StringRef path,
const protocol::Position& position,
Session* session);
/// Find the definition location of a symbol by hash.
std::optional<protocol::Location> find_definition_location(index::SymbolHash hash);
/// Find a symbol's name and kind by hash.
bool find_symbol_info(index::SymbolHash hash, std::string& name, SymbolKind& kind) const;
/// Resolve a hierarchy item (from stored data or by position lookup).
/// @param session Active Session for this file, or nullptr.
std::optional<SymbolInfo> resolve_hierarchy_item(const std::string& uri,
llvm::StringRef path,
const protocol::Range& range,
const std::optional<protocol::LSPAny>& data,
Session* session);
/// Find incoming calls to a function.
std::vector<protocol::CallHierarchyIncomingCall> find_incoming_calls(index::SymbolHash hash);
/// Find outgoing calls from a function.
std::vector<protocol::CallHierarchyOutgoingCall> find_outgoing_calls(index::SymbolHash hash);
/// Find supertypes (base classes) of a type.
std::vector<protocol::TypeHierarchyItem> find_supertypes(index::SymbolHash hash);
/// Find subtypes (derived classes) of a type.
std::vector<protocol::TypeHierarchyItem> find_subtypes(index::SymbolHash hash);
/// Search symbols by name substring.
std::vector<protocol::SymbolInformation> search_symbols(llvm::StringRef query,
std::size_t max_results = 100);
struct DefinitionText {
std::string file;
int start_line;
int end_line;
std::string text;
};
/// Get full definition text for a symbol, using stored index ranges and content.
std::optional<DefinitionText> get_definition_text(index::SymbolHash hash);
struct ReferenceWithContext {
std::string file;
int line;
std::string context;
};
/// Collect references (or definitions) with context lines from stored content.
std::vector<ReferenceWithContext> collect_references(index::SymbolHash hash, RelationKind kind);
/// Cancel background indexing and wait for all tasks to settle.
kota::task<> stop();
/// Whether background indexing is currently idle (no active or queued work).
bool is_idle() const {
return !indexing_active && index_queue_pos >= index_queue.size();
}
/// Number of files remaining in the indexing queue.
std::size_t pending_files() const {
return index_queue_pos < index_queue.size() ? index_queue.size() - index_queue_pos : 0;
}
/// Total files that were enqueued in the current (or last) indexing round.
std::size_t total_queued() const {
return index_queue.size();
}
/// Convert internal SymbolKind to LSP SymbolKind.
static protocol::SymbolKind to_lsp_symbol_kind(SymbolKind kind);
/// Build hierarchy items from SymbolInfo.
static protocol::CallHierarchyItem build_call_hierarchy_item(const SymbolInfo& info);
static protocol::TypeHierarchyItem build_type_hierarchy_item(const SymbolInfo& info);
private:
/// Result of resolving a symbol at a cursor position.
struct CursorHit {
index::SymbolHash hash = 0;
protocol::Range range{};
};
/// Resolve the symbol at (position), checking Session's file_index first
/// then falling back to Workspace's MergedIndex.
CursorHit resolve_cursor(llvm::StringRef path,
const protocol::Position& position,
Session* session);
/// Resolve an include directive argument at (position), if any.
std::optional<protocol::Location> find_include_definition(llvm::StringRef path,
const protocol::Position& position,
Session* session);
/// Collect relations grouped by target symbol, across all index sources.
void collect_grouped_relations(
index::SymbolHash hash,
RelationKind kind,
llvm::DenseMap<index::SymbolHash, std::vector<protocol::Range>>& target_ranges);
/// Collect unique target symbol hashes for a relation kind.
void collect_unique_targets(index::SymbolHash hash,
RelationKind kind,
llvm::SmallVectorImpl<index::SymbolHash>& targets);
/// Resolve a symbol hash into a SymbolInfo with definition location.
std::optional<SymbolInfo> resolve_symbol(index::SymbolHash hash);
/// Check whether a project-level path_id has an active Session.
bool is_proj_path_open(std::uint32_t proj_path_id) const {
return is_file_open && is_file_open(proj_path_id);
}
private:
kota::event_loop& loop;
kota::task_group<> bg_tasks;
Workspace& workspace;
llvm::DenseMap<std::uint32_t, Session>& sessions;
WorkerPool& pool;
Compiler& compiler;
/// Callback that checks if a *project-level* path_id has an active
/// Session. Set by the owner (e.g. MasterServer) to bridge the
/// server-path-id-keyed sessions map to project-level path_ids.
std::function<bool(std::uint32_t)> is_file_open;
/// LSP peer for progress reporting (optional, not owned).
kota::ipc::JsonPeer* peer = nullptr;
/// Background indexing queue and scheduling state.
std::vector<std::uint32_t> index_queue;
std::size_t index_queue_pos = 0;
bool indexing_active = false;
bool indexing_scheduled = false;
std::shared_ptr<kota::timer> index_idle_timer;
/// Concurrency control for background indexing.
std::size_t max_concurrent = 2;
std::size_t baseline_concurrent = 2;
/// Pause/resume: when paused, new index tasks wait on this event.
/// Uses a counter so nested pause/resume pairs work correctly.
std::size_t pause_depth = 0;
kota::event resume_event{true};
kota::task<> run_background_indexing();
kota::task<> index_one(std::uint32_t server_path_id);
kota::task<> monitor_resources();
};
} // namespace clice