refactor(server): extract Indexer and Compiler from MasterServer (#403)

## Summary

- **Extract `Indexer` class** — owns all index state (ProjectIndex,
MergedIndex shards, OpenFileIndex) and query methods (definition,
references, call/type hierarchy, workspace symbol search)
- **Extract `Compiler` class** — owns document state, PCH/PCM cache,
compile argument resolution, header context, `ensure_compiled`, and
worker forwarding
- **MasterServer is now a pure LSP handler registration layer** (~700
lines, down from ~3200)
- **`MergedIndexShard`** wraps `index::MergedIndex` with a lazily-cached
PositionMapper; `OpenFileIndex` gains matching
`find_occurrence()`/`find_relations()` APIs — callers get pre-converted
LSP ranges directly
- **Indexer returns typed values** (`vector<Location>`,
`vector<CallHierarchyIncomingCall>`, etc.) instead of pre-serialized
JSON, fixing the references handler from JSON string surgery to simple
vector concatenation
- **Fix**: duplicate `workspace/symbol` loop in the original code

## Test plan

- [x] 465 unit tests pass
- [x] 113 integration tests pass
- [x] 2/2 smoke tests pass
- [x] `clang-format` applied

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Server-side C++ compilation orchestration (module & precompiled header
builds) with LSP-integrated document handling.

* **Improvements**
* Deterministic, persistent, dependency-aware caching to avoid redundant
rebuilds and speed up incremental work.
* Better cross-file indexing and navigation, improved diagnostics and
more reliable include/import-aware completions.

* **Tests**
  * Unit tests updated to the unified worker query/build request shapes.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
ykiko
2026-04-07 13:30:12 +08:00
committed by GitHub
parent ada202e489
commit bb0b160a28
14 changed files with 3359 additions and 3612 deletions

View File

@@ -39,7 +39,8 @@ TEST_CASE(BuildPCMThenCompileWithImport) {
bool phase1_done = false;
sl.run([&]() -> et::task<> {
worker::BuildPCMParams params;
worker::BuildParams params;
params.kind = worker::BuildKind::BuildPCM;
params.file = iface;
params.directory = "/tmp";
params.arguments = {"clang++",
@@ -54,7 +55,7 @@ TEST_CASE(BuildPCMThenCompileWithImport) {
auto result = co_await sl.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value());
CO_ASSERT_TRUE(result.value().success);
pcm_path = result.value().pcm_path;
pcm_path = result.value().output_path;
EXPECT_FALSE(pcm_path.empty());
phase1_done = true;
@@ -125,7 +126,8 @@ TEST_CASE(BuildPCMChainThenCompile) {
sl.run([&]() -> et::task<> {
// Build PCM for A first.
{
worker::BuildPCMParams params;
worker::BuildParams params;
params.kind = worker::BuildKind::BuildPCM;
params.file = mod_a;
params.directory = "/tmp";
params.arguments = {"clang++",
@@ -139,12 +141,13 @@ TEST_CASE(BuildPCMChainThenCompile) {
auto result = co_await sl.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value() && result.value().success);
pcm_a = result.value().pcm_path;
pcm_a = result.value().output_path;
}
// Build PCM for B, passing A's PCM (transitive dep).
{
worker::BuildPCMParams params;
worker::BuildParams params;
params.kind = worker::BuildKind::BuildPCM;
params.file = mod_b;
params.directory = "/tmp";
params.arguments = {"clang++",
@@ -161,7 +164,7 @@ TEST_CASE(BuildPCMChainThenCompile) {
auto result = co_await sl.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value() && result.value().success);
pcm_b = result.value().pcm_path;
pcm_b = result.value().output_path;
}
pcm_done = true;
@@ -225,7 +228,8 @@ TEST_CASE(ModuleImplementationUnitWithWorker) {
bool pcm_done = false;
sl.run([&]() -> et::task<> {
worker::BuildPCMParams params;
worker::BuildParams params;
params.kind = worker::BuildKind::BuildPCM;
params.file = iface;
params.directory = "/tmp";
params.arguments = {"clang++",
@@ -239,7 +243,7 @@ TEST_CASE(ModuleImplementationUnitWithWorker) {
auto result = co_await sl.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value() && result.value().success);
pcm_path = result.value().pcm_path;
pcm_path = result.value().output_path;
pcm_done = true;
sl.peer->close_output();

View File

@@ -40,7 +40,8 @@ TEST_CASE(BuildPCHThenCompile) {
bool phase1_done = false;
sl.run([&]() -> et::task<> {
worker::BuildPCHParams params;
worker::BuildParams params;
params.kind = worker::BuildKind::BuildPCH;
params.file = main_file;
params.directory = dir;
params.arguments = {"clang++",
@@ -51,13 +52,13 @@ TEST_CASE(BuildPCHThenCompile) {
"-I",
dir,
main_file};
params.content = main_text;
params.text = main_text;
params.output_path = tmp.path("preamble.pch");
auto result = co_await sl.peer->send_request(params);
CO_ASSERT_TRUE(result.has_value());
CO_ASSERT_TRUE(result.value().success);
pch_path = result.value().pch_path;
pch_path = result.value().output_path;
EXPECT_FALSE(pch_path.empty());
phase1_done = true;

View File

@@ -61,7 +61,8 @@ TEST_CASE(HoverWithoutCompile) {
w.run([&]() -> et::task<> {
// Hover on a file that hasn't been compiled should return null.
worker::HoverParams params;
worker::QueryParams params;
params.kind = worker::QueryKind::Hover;
params.path = "/tmp/nonexistent.cpp";
params.offset = 0;
@@ -101,7 +102,8 @@ TEST_CASE(CompileThenHover) {
// After successful compilation, hover should return info.
// "int foo() { return 42; }\n" is 25 chars, then char 22 on line 1 = offset 47
worker::HoverParams hp;
worker::QueryParams hp;
hp.kind = worker::QueryKind::Hover;
hp.path = src;
hp.offset = 47; // position of 'foo' in 'return foo();'
@@ -147,7 +149,8 @@ TEST_CASE(DocumentUpdate) {
w.peer->send_notification(up);
// After update, hover still returns stale AST results (not null).
worker::HoverParams hp;
worker::QueryParams hp;
hp.kind = worker::QueryKind::Hover;
hp.path = src;
hp.offset = 4;
@@ -168,7 +171,8 @@ TEST_CASE(CodeActionReturnsEmpty) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::CodeActionParams params;
worker::QueryParams params;
params.kind = worker::QueryKind::CodeAction;
params.path = "/tmp/test.cpp";
auto result = co_await w.peer->send_request(params);
@@ -189,7 +193,8 @@ TEST_CASE(GoToDefinitionReturnsEmpty) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::GoToDefinitionParams params;
worker::QueryParams params;
params.kind = worker::QueryKind::GoToDefinition;
params.path = "/tmp/test.cpp";
params.offset = 0;
@@ -211,7 +216,8 @@ TEST_CASE(SemanticTokensWithoutCompile) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::SemanticTokensParams params;
worker::QueryParams params;
params.kind = worker::QueryKind::SemanticTokens;
params.path = "/tmp/nonexistent.cpp";
auto result = co_await w.peer->send_request(params);
@@ -231,7 +237,8 @@ TEST_CASE(FoldingRangeWithoutCompile) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::FoldingRangeParams params;
worker::QueryParams params;
params.kind = worker::QueryKind::FoldingRange;
params.path = "/tmp/nonexistent.cpp";
auto result = co_await w.peer->send_request(params);
@@ -251,7 +258,8 @@ TEST_CASE(DocumentSymbolWithoutCompile) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::DocumentSymbolParams params;
worker::QueryParams params;
params.kind = worker::QueryKind::DocumentSymbol;
params.path = "/tmp/nonexistent.cpp";
auto result = co_await w.peer->send_request(params);
@@ -271,7 +279,8 @@ TEST_CASE(DocumentLinkWithoutCompile) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::DocumentLinkParams params;
worker::QueryParams params;
params.kind = worker::QueryKind::DocumentLink;
params.path = "/tmp/nonexistent.cpp";
auto result = co_await w.peer->send_request(params);
@@ -291,7 +300,8 @@ TEST_CASE(InlayHintsWithoutCompile) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::InlayHintsParams params;
worker::QueryParams params;
params.kind = worker::QueryKind::InlayHints;
params.path = "/tmp/nonexistent.cpp";
auto result = co_await w.peer->send_request(params);
@@ -333,13 +343,15 @@ TEST_CASE(MultipleSequentialRequests) {
CO_ASSERT_TRUE(cr.has_value());
// Now send multiple different feature requests sequentially.
worker::HoverParams hp;
worker::QueryParams hp;
hp.kind = worker::QueryKind::Hover;
hp.path = src;
hp.offset = 4; // 'foo' on line 0
auto r1 = co_await w.peer->send_request(hp);
EXPECT_TRUE(r1.has_value());
worker::CodeActionParams cap;
worker::QueryParams cap;
cap.kind = worker::QueryKind::CodeAction;
cap.path = src;
auto r2 = co_await w.peer->send_request(cap);
EXPECT_TRUE(r2.has_value());
@@ -347,18 +359,21 @@ TEST_CASE(MultipleSequentialRequests) {
// 'foo' in 'return foo(0);' at line 4, char 11
// lines: "int foo(int x) {\n"=17, " return x + 1;\n"=18, "}\n"=2, "int main() {\n"=14
// offset = 17+18+2+14+11 = 62
worker::GoToDefinitionParams gdp;
worker::QueryParams gdp;
gdp.kind = worker::QueryKind::GoToDefinition;
gdp.path = src;
gdp.offset = 62;
auto r3 = co_await w.peer->send_request(gdp);
EXPECT_TRUE(r3.has_value());
worker::SemanticTokensParams stp;
worker::QueryParams stp;
stp.kind = worker::QueryKind::SemanticTokens;
stp.path = src;
auto r4 = co_await w.peer->send_request(stp);
EXPECT_TRUE(r4.has_value());
worker::FoldingRangeParams frp;
worker::QueryParams frp;
frp.kind = worker::QueryKind::FoldingRange;
frp.path = src;
auto r5 = co_await w.peer->send_request(frp);
EXPECT_TRUE(r5.has_value());
@@ -403,7 +418,8 @@ TEST_CASE(MultipleDocuments) {
// Hover on each document after compilation.
for(int i = 0; i < 3; i++) {
worker::HoverParams hp;
worker::QueryParams hp;
hp.kind = worker::QueryKind::Hover;
hp.path = paths[i];
hp.offset = 4; // 'var_N'
@@ -431,7 +447,8 @@ TEST_CASE(EvictNotification) {
w.peer->send_notification(ep);
// Hover on the evicted document should return null (document doesn't exist).
worker::HoverParams hp;
worker::QueryParams hp;
hp.kind = worker::QueryKind::Hover;
hp.path = "/tmp/evict_test.cpp";
hp.offset = 0;
@@ -470,7 +487,8 @@ TEST_CASE(SpawnWithMemoryLimit) {
EXPECT_TRUE(cr.has_value());
// Feature request should work after compilation.
worker::HoverParams hp;
worker::QueryParams hp;
hp.kind = worker::QueryKind::Hover;
hp.path = src;
hp.offset = 4; // 'memlimit_var'

View File

@@ -93,12 +93,13 @@ TEST_CASE(BuildPCHRequest) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::BuildPCHParams params;
worker::BuildParams params;
params.kind = worker::BuildKind::BuildPCH;
params.file = hdr;
params.directory = "/tmp";
params.arguments =
{"clang++", "-resource-dir", std::string(resource_dir()), "-x", "c++-header", hdr};
params.content = "#pragma once\nint pch_global = 42;\n";
params.text = "#pragma once\nint pch_global = 42;\n";
params.output_path = tmp.path("test_pch.pch");
auto result = co_await w.peer->send_request(params);
@@ -108,7 +109,7 @@ TEST_CASE(BuildPCHRequest) {
co_return;
}
EXPECT_TRUE(result.value().success);
EXPECT_FALSE(result.value().pch_path.empty());
EXPECT_FALSE(result.value().output_path.empty());
test_done = true;
w.peer->close_output();
});
@@ -127,7 +128,8 @@ TEST_CASE(IndexRequest) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::IndexParams params;
worker::BuildParams params;
params.kind = worker::BuildKind::Index;
params.file = src;
params.directory = "/tmp";
params.arguments = make_args(src);
@@ -161,7 +163,8 @@ TEST_CASE(BuildPCMRequest) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::BuildPCMParams params;
worker::BuildParams params;
params.kind = worker::BuildKind::BuildPCM;
params.file = src;
params.directory = "/tmp";
params.arguments = {"clang++",
@@ -194,8 +197,9 @@ TEST_CASE(CompletionRequest) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::CompletionParams params;
params.path = src;
worker::BuildParams params;
params.kind = worker::BuildKind::Completion;
params.file = src;
params.version = 1;
params.text = text;
params.directory = "/tmp";
@@ -223,8 +227,9 @@ TEST_CASE(SignatureHelpRequest) {
bool test_done = false;
w.run([&]() -> et::task<> {
worker::SignatureHelpParams params;
params.path = src;
worker::BuildParams params;
params.kind = worker::BuildKind::SignatureHelp;
params.file = src;
params.version = 1;
params.text = text;
params.directory = "/tmp";
@@ -259,7 +264,8 @@ TEST_CASE(MultipleStatelessRequests) {
w.run([&]() -> et::task<> {
// Send multiple index requests to test stateless worker handles them sequentially.
for(int i = 0; i < 3; i++) {
worker::IndexParams params;
worker::BuildParams params;
params.kind = worker::BuildKind::Index;
params.file = paths[i];
params.directory = "/tmp";
params.arguments = make_args(paths[i]);