Support multiple -init=

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"]
This commit is contained in:
Fangrui Song 2018-12-09 13:50:58 -08:00
parent 259b9fefb3
commit 6945a56fb8
2 changed files with 32 additions and 29 deletions

View File

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

View File

@ -36,8 +36,7 @@ limitations under the License.
namespace ccls { namespace ccls {
using namespace llvm; using namespace llvm;
// TODO Cleanup global variables extern std::vector<std::string> g_init_options;
extern std::string g_init_options;
namespace { namespace {
enum class TextDocumentSyncKind { None = 0, Full = 1, Incremental = 2 }; enum class TextDocumentSyncKind { None = 0, Full = 1, Incremental = 2 };
@ -255,14 +254,16 @@ void Initialize(MessageHandler *m, InitializeParam &param, ReplyOnce &reply) {
{ {
g_config = new Config(param.initializationOptions); g_config = new Config(param.initializationOptions);
rapidjson::Document reader; rapidjson::Document reader;
reader.Parse(g_init_options.c_str()); for (const std::string &str : g_init_options) {
if (!reader.HasParseError()) { reader.Parse(str.c_str());
JsonReader json_reader{&reader}; if (!reader.HasParseError()) {
try { JsonReader json_reader{&reader};
Reflect(json_reader, *g_config); try {
} catch (std::invalid_argument &) { Reflect(json_reader, *g_config);
// This will not trigger because parse error is handled in } catch (std::invalid_argument &) {
// MessageRegistry::Parse in lsp.cc // This will not trigger because parse error is handled in
// MessageRegistry::Parse in lsp.cc
}
} }
} }