diff --git a/src/command_line.cc b/src/command_line.cc index 4cc0f35c..975fec18 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -20,6 +20,7 @@ #include "queue_manager.h" #include "semantic_highlight_symbol_cache.h" #include "serializer.h" +#include "serializers/json.h" #include "standard_includes.h" #include "test.h" #include "threaded_queue.h" @@ -29,14 +30,11 @@ #include "working_files.h" #include -#include -#include #include +#include #include -#include #include -#include #include #include #include @@ -52,6 +50,8 @@ // TODO: implement ThreadPool type which monitors CPU usage / number of work // items per second completed and scales up/down number of running threads. +std::string g_init_options; + namespace { std::vector kEmptyArgs; @@ -438,10 +438,22 @@ int main(int argc, char** argv) { return -1; } - if (HasOption(options, "--enable-comments")) { - // TODO Place this global variable into config - extern bool g_enable_comments; - g_enable_comments = true; + if (HasOption(options, "--init")) { + // We check syntax error here but override client-side initializationOptions + // in messages/initialize.cc + g_init_options = options["--init"]; + rapidjson::Document reader; + rapidjson::ParseResult ok = reader.Parse(g_init_options.c_str()); + if (!ok) { + std::cerr << "Failed to parse --init as JSON: " + << rapidjson::GetParseError_En(ok.Code()) << " (" << ok.Offset() + << ")\n"; + return 1; + } + if (!reader.IsObject()) { + std::cerr << "--init must be a JSON object\n"; + return 1; + } } if (HasOption(options, "--language-server")) { diff --git a/src/config.h b/src/config.h index 9b73c709..fe0297db 100644 --- a/src/config.h +++ b/src/config.h @@ -91,6 +91,8 @@ struct Config { // a function or method bool enableSnippetInsertion = true; + bool enableComments = false; + //// For debugging // Dump AST after parsing if some pattern matches the source filename. @@ -132,6 +134,8 @@ MAKE_REFLECT_STRUCT(Config, clientVersion, enableSnippetInsertion, + enableComments, + dumpAST ); diff --git a/src/messages/initialize.cc b/src/messages/initialize.cc index d6bc4fdb..ab865220 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -10,6 +10,10 @@ #include +// TODO Cleanup global variables +extern std::string g_init_options; +extern bool g_enable_comments; + namespace { struct Ipc_InitializeRequest : public IpcMessage { const static IpcId kIpcId = IpcId::Initialize; @@ -60,6 +64,15 @@ struct InitializeHandler : BaseMessageHandler { } *config = *request->params.initializationOptions; + { + rapidjson::Document reader; + reader.Parse(g_init_options.c_str()); + if (!reader.HasParseError()) { + JsonReader json_reader{&reader}; + Reflect(json_reader, *config); + } + } + g_enable_comments = config->enableComments; // Check client version. if (config->clientVersion.has_value() &&