Skip to content

Commit

Permalink
nixd/Server/configuration: use nixd.json for `workspace/configurati…
Browse files Browse the repository at this point in the history
…on` defaults

Add jjw-log for options.enable

Actually use BaseConstructor

Fix some bugs

Fix move bug

Remove jjw-log

Format changed files
  • Loading branch information
jonathanjameswatson committed Oct 6, 2023
1 parent e8f144c commit ae7f63f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lspserver/include/lspserver/LSPBinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace lspserver {
template <typename T>
llvm::Expected<T> parseParam(const llvm::json::Value &Raw,
llvm::StringRef PayloadName,
llvm::StringRef PayloadKind) {
T Result;
llvm::StringRef PayloadKind, T Base = T()) {
T Result = Base;
llvm::json::Path::Root Root;
if (!fromJSON(Raw, Result, Root)) {
elog("Failed to decode {0} {1}: {2}", PayloadName, PayloadKind,
Expand Down
7 changes: 4 additions & 3 deletions lspserver/include/lspserver/LSPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class LSPServer : public MessageHandler {

template <class ParamTy, class ResponseTy>
llvm::unique_function<void(const ParamTy &, Callback<ResponseTy>)>
mkOutMethod(llvm::StringRef Method, OutboundPort *O = nullptr) {
mkOutMethod(llvm::StringRef Method, OutboundPort *O = nullptr,
ResponseTy Base = ResponseTy()) {
if (!O)
O = Out.get();
return [=, this](const ParamTy &Params, Callback<ResponseTy> Reply) {
Expand All @@ -76,8 +77,8 @@ class LSPServer : public MessageHandler {
llvm::Expected<llvm::json::Value> Response) mutable {
if (!Response)
return Reply(Response.takeError());
Reply(
parseParam<ResponseTy>(std::move(*Response), Method, "reply"));
Reply(parseParam<ResponseTy>(std::move(*Response), Method, "reply",
Base));
},
O);
};
Expand Down
1 change: 1 addition & 0 deletions nixd/include/nixd/Server/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class Controller : public lspserver::LSPServer {

std::mutex ConfigLock;

configuration::TopLevel JSONConfig;
configuration::TopLevel Config; // GUARDED_BY(ConfigLock)

std::shared_ptr<const std::string> getDraft(lspserver::PathRef File) const;
Expand Down
19 changes: 11 additions & 8 deletions nixd/lib/Server/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ void Controller::readJSONConfig(lspserver::PathRef File) noexcept {
std::ifstream In(File.str(), std::ios::in);
SS << In.rdbuf();

if (auto NewConfig = parseConfig(SS.str()))
updateConfig(std::move(NewConfig.get()));
else {
if (auto NewConfig = parseConfig(SS.str())) {
JSONConfig = std::move(NewConfig.get());
} else {
throw nix::Error("configuration cannot be parsed");
}
} catch (std::exception &E) {
Expand All @@ -257,6 +257,14 @@ Controller::Controller(std::unique_ptr<lspserver::InboundPort> In,
: LSPServer(std::move(In), std::move(Out)), WaitWorker(WaitWorker),
ASTMgr(Pool) {

// JSON Config, run before initialize
readJSONConfig();
configuration::TopLevel JSONConfigCopy = JSONConfig;
updateConfig(std::move(JSONConfigCopy));
WorkspaceConfiguration =
mkOutMethod<lspserver::ConfigurationParams, configuration::TopLevel>(
"workspace/configuration", nullptr, JSONConfig);

// Life Cycle
Registry.addMethod("initialize", this, &Controller::onInitialize);
Registry.addNotification("initialized", this, &Controller::onInitialized);
Expand Down Expand Up @@ -293,17 +301,12 @@ Controller::Controller(std::unique_ptr<lspserver::InboundPort> In,
// Workspace
Registry.addNotification("workspace/didChangeConfiguration", this,
&Controller::onWorkspaceDidChangeConfiguration);
WorkspaceConfiguration =
mkOutMethod<lspserver::ConfigurationParams, configuration::TopLevel>(
"workspace/configuration");

/// IPC
Registry.addNotification("nixd/ipc/diagnostic", this,
&Controller::onEvalDiagnostic);

Registry.addNotification("nixd/ipc/finished", this, &Controller::onFinished);

readJSONConfig();
}

//-----------------------------------------------------------------------------/
Expand Down

0 comments on commit ae7f63f

Please sign in to comment.