Skip to content

Commit

Permalink
Support multiple -init=
Browse files Browse the repository at this point in the history
Initialization options are applied (deserialized to the same object) in the following order:

* "initializationOptions" from client
* first -init=
* second -init=
* ...

Scalar options will be overridden but arrays will get concatenated, e.g.

ccls -log-file=/dev/stderr -index . -init='{"clang":{"extraArgs":["-DA"]}}' -init='{"clang":{"extraArgs":["-DB"]}}'

results in clang.extraArgs: ["-DA", "-DB"]
  • Loading branch information
MaskRay committed Oct 24, 2019
1 parent 0b53720 commit 39009b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
40 changes: 21 additions & 19 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace llvm;
using namespace llvm::cl;

namespace ccls {
std::string g_init_options;
std::vector<std::string> g_init_options;
}

namespace {
Expand All @@ -44,8 +44,8 @@ opt<std::string> opt_test_index("test-index", ValueOptional, init("!"),
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"),
cat(C));
list<std::string> opt_init("init", desc("extra initialization options in JSON"),
cat(C));
opt<std::string> opt_log_file("log-file", desc("log"), value_desc("filename"),
cat(C));
opt<std::string> opt_log_file_append("log-file-append", desc("log"),
Expand Down Expand Up @@ -106,23 +106,25 @@ int main(int argc, char **argv) {
if (!opt_init.empty()) {
// We check syntax error here but override client-side
// initializationOptions in messages/initialize.cc
g_init_options = opt_init.getValue();
g_init_options = opt_init;
rapidjson::Document reader;
rapidjson::ParseResult ok = reader.Parse(g_init_options.c_str());
if (!ok) {
fprintf(stderr, "Failed to parse --init as JSON: %s (%zd)\n",
rapidjson::GetParseError_En(ok.Code()), ok.Offset());
return 1;
}
JsonReader json_reader{&reader};
try {
Config config;
Reflect(json_reader, config);
} catch (std::invalid_argument &e) {
fprintf(stderr, "Failed to parse --init %s, expected %s\n",
static_cast<JsonReader &>(json_reader).GetPath().c_str(),
e.what());
return 1;
for (const std::string &str : g_init_options) {
rapidjson::ParseResult ok = reader.Parse(str.c_str());
if (!ok) {
fprintf(stderr, "Failed to parse --init as JSON: %s (%zd)\n",
rapidjson::GetParseError_En(ok.Code()), ok.Offset());
return 1;
}
JsonReader json_reader{&reader};
try {
Config config;
Reflect(json_reader, config);
} catch (std::invalid_argument &e) {
fprintf(stderr, "Failed to parse --init %s, expected %s\n",
static_cast<JsonReader &>(json_reader).GetPath().c_str(),
e.what());
return 1;
}
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/messages/initialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
namespace ccls {
using namespace llvm;

// TODO Cleanup global variables
extern std::string g_init_options;
extern std::vector<std::string> g_init_options;

namespace {
enum class TextDocumentSyncKind { None = 0, Full = 1, Incremental = 2 };
Expand Down Expand Up @@ -243,14 +242,16 @@ void Initialize(MessageHandler *m, InitializeParam &param, ReplyOnce &reply) {
{
g_config = new Config(param.initializationOptions);
rapidjson::Document reader;
reader.Parse(g_init_options.c_str());
if (!reader.HasParseError()) {
JsonReader json_reader{&reader};
try {
Reflect(json_reader, *g_config);
} catch (std::invalid_argument &) {
// This will not trigger because parse error is handled in
// MessageRegistry::Parse in lsp.cc
for (const std::string &str : g_init_options) {
reader.Parse(str.c_str());
if (!reader.HasParseError()) {
JsonReader json_reader{&reader};
try {
Reflect(json_reader, *g_config);
} catch (std::invalid_argument &) {
// This will not trigger because parse error is handled in
// MessageRegistry::Parse in lsp.cc
}
}
}

Expand Down

0 comments on commit 39009b7

Please sign in to comment.