Add command line option -index=root to index without starting language server

This commit is contained in:
Fangrui Song 2018-10-22 22:01:10 -07:00
parent a56b6fb228
commit 66bf514b5f
4 changed files with 70 additions and 52 deletions

View File

@ -11,6 +11,7 @@
#include <llvm/Support/CommandLine.h> #include <llvm/Support/CommandLine.h>
#include <llvm/Support/CrashRecoveryContext.h> #include <llvm/Support/CrashRecoveryContext.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Process.h> #include <llvm/Support/Process.h>
#include <llvm/Support/Program.h> #include <llvm/Support/Program.h>
#include <llvm/Support/Signals.h> #include <llvm/Support/Signals.h>
@ -37,6 +38,9 @@ opt<int> opt_verbose("v", desc("verbosity"), init(0), cat(C));
opt<std::string> opt_test_index("test-index", ValueOptional, init("!"), opt<std::string> opt_test_index("test-index", ValueOptional, init("!"),
desc("run index tests"), cat(C)); desc("run index tests"), cat(C));
opt<std::string> opt_index("index",
desc("standalone mode: index a project and exit"),
value_desc("root"), cat(C));
opt<std::string> opt_init("init", desc("extra initialization options in JSON"), opt<std::string> opt_init("init", desc("extra initialization options in JSON"),
cat(C)); cat(C));
opt<std::string> opt_log_file("log-file", desc("log"), value_desc("filename"), opt<std::string> opt_log_file("log-file", desc("log"), value_desc("filename"),
@ -118,14 +122,20 @@ int main(int argc, char **argv) {
sys::ChangeStdinToBinary(); sys::ChangeStdinToBinary();
sys::ChangeStdoutToBinary(); sys::ChangeStdoutToBinary();
// The thread that reads from stdin and dispatchs commands to the main if (opt_index.size()) {
// thread. SmallString<256> Root(opt_index);
pipeline::LaunchStdin(); sys::fs::make_absolute(Root);
// The thread that writes responses from the main thread to stdout. pipeline::Standalone(Root.str());
pipeline::LaunchStdout(); } else {
// Main thread which also spawns indexer threads upon the "initialize" // The thread that reads from stdin and dispatchs commands to the main
// request. // thread.
pipeline::MainLoop(); pipeline::LaunchStdin();
// The thread that writes responses from the main thread to stdout.
pipeline::LaunchStdout();
// Main thread which also spawns indexer threads upon the "initialize"
// request.
pipeline::MainLoop();
}
} }
return 0; return 0;

View File

@ -310,25 +310,13 @@ struct lsClientCapabilities {
MAKE_REFLECT_STRUCT(lsClientCapabilities, workspace, textDocument); MAKE_REFLECT_STRUCT(lsClientCapabilities, workspace, textDocument);
struct lsInitializeParams { struct lsInitializeParams {
// The process Id of the parent process that started
// the server. Is null if the process has not been started by another process.
// If the parent process is not alive then the server should exit (see exit
// notification) its process.
std::optional<int> processId;
// The rootPath of the workspace. Is null
// if no folder is open.
//
// @deprecated in favour of rootUri.
std::optional<std::string> rootPath;
// The rootUri of the workspace. Is null if no // The rootUri of the workspace. Is null if no
// folder is open. If both `rootPath` and `rootUri` are set // folder is open. If both `rootPath` and `rootUri` are set
// `rootUri` wins. // `rootUri` wins.
std::optional<lsDocumentUri> rootUri; std::optional<lsDocumentUri> rootUri;
// User provided initialization options. // User provided initialization options.
std::optional<Config> initializationOptions; Config initializationOptions;
// The capabilities provided by the client (editor or tool) // The capabilities provided by the client (editor or tool)
lsClientCapabilities capabilities; lsClientCapabilities capabilities;
@ -360,33 +348,8 @@ void Reflect(Reader &reader, lsInitializeParams::lsTrace &value) {
value = lsInitializeParams::lsTrace::Verbose; value = lsInitializeParams::lsTrace::Verbose;
} }
#if 0 // unused MAKE_REFLECT_STRUCT(lsInitializeParams, rootUri, initializationOptions,
void Reflect(Writer& writer, lsInitializeParams::lsTrace& value) { capabilities, trace, workspaceFolders);
switch (value) {
case lsInitializeParams::lsTrace::Off:
writer.String("off");
break;
case lsInitializeParams::lsTrace::Messages:
writer.String("messages");
break;
case lsInitializeParams::lsTrace::Verbose:
writer.String("verbose");
break;
}
}
#endif
MAKE_REFLECT_STRUCT(lsInitializeParams, processId, rootPath, rootUri,
initializationOptions, capabilities, trace,
workspaceFolders);
struct lsInitializeError {
// Indicates whether the client should retry to send the
// initilize request after showing the message provided
// in the ResponseError.
bool retry;
};
MAKE_REFLECT_STRUCT(lsInitializeError, retry);
struct In_InitializeRequest : public RequestMessage { struct In_InitializeRequest : public RequestMessage {
MethodType GetMethodType() const override { return kMethodType; } MethodType GetMethodType() const override { return kMethodType; }
@ -426,10 +389,7 @@ struct Handler_Initialize : BaseMessageHandler<In_InitializeRequest> {
<< params.rootUri->raw_uri; << params.rootUri->raw_uri;
{ {
if (params.initializationOptions) g_config = new Config(params.initializationOptions);
g_config = new Config(*params.initializationOptions);
else
g_config = new Config;
rapidjson::Document reader; rapidjson::Document reader;
reader.Parse(g_init_options.c_str()); reader.Parse(g_init_options.c_str());
if (!reader.HasParseError()) { if (!reader.HasParseError()) {
@ -519,3 +479,17 @@ struct Handler_Initialize : BaseMessageHandler<In_InitializeRequest> {
}; };
REGISTER_MESSAGE_HANDLER(Handler_Initialize); REGISTER_MESSAGE_HANDLER(Handler_Initialize);
} // namespace } // namespace
void StandaloneInitialize(const std::string &root, Project &project,
WorkingFiles &wfiles, VFS &vfs,
IncludeComplete &complete) {
Handler_Initialize handler;
handler.project = &project;
handler.working_files = &wfiles;
handler.vfs = &vfs;
handler.include_complete = &complete;
In_InitializeRequest request;
request.params.rootUri = lsDocumentUri::FromPath(root);
handler.Run(&request);
}

View File

@ -18,6 +18,7 @@
#include <rapidjson/writer.h> #include <rapidjson/writer.h>
#include <llvm/Support/Process.h>
#include <llvm/Support/Threading.h> #include <llvm/Support/Threading.h>
#include <llvm/Support/Timer.h> #include <llvm/Support/Timer.h>
using namespace llvm; using namespace llvm;
@ -30,6 +31,9 @@ using namespace llvm;
#include <unistd.h> #include <unistd.h>
#endif #endif
void StandaloneInitialize(const std::string &, Project &, WorkingFiles &, VFS &,
IncludeComplete &);
void VFS::Clear() { void VFS::Clear() {
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
state.clear(); state.clear();
@ -523,6 +527,35 @@ void MainLoop() {
} }
} }
void Standalone(const std::string &root) {
Project project;
WorkingFiles wfiles;
VFS vfs;
IncludeComplete complete(&project);
StandaloneInitialize(root, project, wfiles, vfs, complete);
bool tty = sys::Process::StandardOutIsDisplayed();
if (tty) {
int entries = 0;
for (auto &[_, folder] : project.root2folder)
entries += folder.entries.size();
printf("entries: %5d\n", entries);
}
while (1) {
(void)on_indexed->DequeueAll();
int pending = pending_index_requests;
if (tty) {
printf("\rpending: %5d", pending);
fflush(stdout);
}
if (!pending)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (tty)
puts("");
}
void Index(const std::string &path, const std::vector<const char *> &args, void Index(const std::string &path, const std::vector<const char *> &args,
IndexMode mode, lsRequestId id) { IndexMode mode, lsRequestId id) {
pending_index_requests++; pending_index_requests++;

View File

@ -48,6 +48,7 @@ void LaunchStdout();
void Indexer_Main(CompletionManager *completion, VFS *vfs, Project *project, void Indexer_Main(CompletionManager *completion, VFS *vfs, Project *project,
WorkingFiles *wfiles); WorkingFiles *wfiles);
void MainLoop(); void MainLoop();
void Standalone(const std::string &root);
void Index(const std::string &path, const std::vector<const char *> &args, void Index(const std::string &path, const std::vector<const char *> &args,
IndexMode mode, lsRequestId id = {}); IndexMode mode, lsRequestId id = {});