if (Server)
return Reply(make_error<LSPError>("server already initialized",
ErrorCode::InvalidRequest));
- Server.emplace(CDB.getCDB(), FSProvider,
+ Optional<Path> CompileCommandsDir;
+ if (Params.initializationOptions)
+ CompileCommandsDir = Params.initializationOptions->compilationDatabasePath;
+ CDB.emplace(UseInMemoryCDB
+ ? CompilationDB::makeInMemory()
+ : CompilationDB::makeDirectoryBased(CompileCommandsDir));
+ Server.emplace(CDB->getCDB(), FSProvider,
static_cast<DiagnosticsConsumer &>(*this), ClangdServerOpts);
- if (Params.initializationOptions) {
- const ClangdInitializationOptions &Opts = *Params.initializationOptions;
-
- // Explicit compilation database path.
- if (Opts.compilationDatabasePath.hasValue()) {
- CDB.setCompileCommandsDir(Opts.compilationDatabasePath.getValue());
- }
-
- applyConfiguration(Opts.ParamsChange);
- }
+ if (Params.initializationOptions)
+ applyConfiguration(Params.initializationOptions->ParamsChange);
CCOpts.EnableSnippets = Params.capabilities.CompletionSnippets;
DiagOpts.EmbedFixesInDiagnostics = Params.capabilities.DiagnosticFixes;
const DidOpenTextDocumentParams &Params) {
PathRef File = Params.textDocument.uri.file();
if (Params.metadata && !Params.metadata->extraFlags.empty())
- CDB.setExtraFlagsForFile(File, std::move(Params.metadata->extraFlags));
+ CDB->setExtraFlagsForFile(File, std::move(Params.metadata->extraFlags));
const std::string &Contents = Params.textDocument.text;
// fail rather than giving wrong results.
DraftMgr.removeDraft(File);
Server->removeDocument(File);
- CDB.invalidate(File);
+ CDB->invalidate(File);
elog("Failed to update {0}: {1}", File, Contents.takeError());
return;
}
PathRef File = Params.textDocument.uri.file();
DraftMgr.removeDraft(File);
Server->removeDocument(File);
- CDB.invalidate(File);
+ CDB->invalidate(File);
}
void ClangdLSPServer::onDocumentOnTypeFormatting(
/// The opened files need to be reparsed only when some existing
/// entries are changed.
PathRef File = Entry.first;
- if (!CDB.setCompilationCommandForFile(
+ if (!CDB->setCompilationCommandForFile(
File, tooling::CompileCommand(
std::move(Entry.second.workingDirectory), File,
std::move(Entry.second.compilationCommand),
Optional<Path> CompileCommandsDir,
bool ShouldUseInMemoryCDB,
const ClangdServer::Options &Opts)
- : Transp(Transp), MsgHandler(new MessageHandler(*this)),
- CDB(ShouldUseInMemoryCDB ? CompilationDB::makeInMemory()
- : CompilationDB::makeDirectoryBased(
- std::move(CompileCommandsDir))),
- CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()),
+ : Transp(Transp), MsgHandler(new MessageHandler(*this)), CCOpts(CCOpts),
+ SupportedSymbolKinds(defaultSymbolKinds()),
SupportedCompletionItemKinds(defaultCompletionItemKinds()),
- ClangdServerOpts(Opts) {
+ UseInMemoryCDB(ShouldUseInMemoryCDB), ClangdServerOpts(Opts) {
// clang-format off
MsgHandler->bind("initialize", &ClangdLSPServer::onInitialize);
MsgHandler->bind("shutdown", &ClangdLSPServer::onShutdown);
->setExtraFlagsForFile(File, std::move(ExtraFlags));
}
-void ClangdLSPServer::CompilationDB::setCompileCommandsDir(Path P) {
- if (!IsDirectoryBased) {
- elog("Trying to set compile commands dir while using in-memory compilation "
- "database");
- return;
- }
- static_cast<DirectoryBasedGlobalCompilationDatabase *>(CDB.get())
- ->setCompileCommandsDir(P);
-}
-
} // namespace clangd
} // namespace clang
void setExtraFlagsForFile(PathRef File,
std::vector<std::string> ExtraFlags);
- /// Set the compile commands directory to \p P.
- /// Only valid for directory-based CDB, no-op and error log on InMemoryCDB;
- void setCompileCommandsDir(Path P);
-
/// Returns a CDB that should be used to get compile commands for the
/// current instance of ClangdLSPServer.
GlobalCompilationDatabase &getCDB() { return *CDB; }
void notify(StringRef Method, llvm::json::Value Params);
void reply(llvm::json::Value ID, llvm::Expected<llvm::json::Value> Result);
- // Various ClangdServer parameters go here. It's important they're created
- // before ClangdServer.
- CompilationDB CDB;
-
RealFileSystemProvider FSProvider;
/// Options used for code completion
clangd::CodeCompleteOptions CCOpts;
// Store of the current versions of the open documents.
DraftStore DraftMgr;
+ // The CDB is created by the "initialize" LSP method.
+ bool UseInMemoryCDB; // FIXME: make this a capability.
+ llvm::Optional<CompilationDB> CDB;
// The ClangdServer is created by the "initialize" LSP method.
// It is destroyed before run() returns, to ensure worker threads exit.
ClangdServer::Options ClangdServerOpts;