Skip to content

Commit

Permalink
nixd: basic work-done progress support (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc authored Apr 15, 2024
1 parent 2d71629 commit ddd1e56
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 18 deletions.
40 changes: 40 additions & 0 deletions nixd/include/nixd/Controller/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,48 @@ class Controller : public lspserver::LSPServer {

lspserver::DraftStore Store;

lspserver::ClientCapabilities ClientCaps;

llvm::unique_function<void(const lspserver::PublishDiagnosticsParams &)>
PublishDiagnostic;
llvm::unique_function<void(const lspserver::WorkDoneProgressCreateParams &,
lspserver::Callback<std::nullptr_t>)>
CreateWorkDoneProgress;

void
createWorkDoneProgress(const lspserver::WorkDoneProgressCreateParams &Params);

llvm::unique_function<void(
const lspserver::ProgressParams<lspserver::WorkDoneProgressBegin> &)>
BeginWorkDoneProgress;

void beginWorkDoneProgress(
const lspserver::ProgressParams<lspserver::WorkDoneProgressBegin>
&Params) {
if (ClientCaps.WorkDoneProgress)
BeginWorkDoneProgress(Params);
}

llvm::unique_function<void(
const lspserver::ProgressParams<lspserver::WorkDoneProgressReport> &)>
ReportWorkDoneProgress;

void reportWorkDoneProgress(
const lspserver::ProgressParams<lspserver::WorkDoneProgressReport>
&Params) {
if (ClientCaps.WorkDoneProgress)
ReportWorkDoneProgress(Params);
}

llvm::unique_function<void(
lspserver::ProgressParams<lspserver::WorkDoneProgressEnd>)>
EndWorkDoneProgress;

void endWorkDoneProgress(
const lspserver::ProgressParams<lspserver::WorkDoneProgressEnd> &Params) {
if (ClientCaps.WorkDoneProgress)
EndWorkDoneProgress(Params);
}

std::mutex TUsLock;
llvm::StringMap<std::shared_ptr<NixTU>> TUs;
Expand Down
43 changes: 25 additions & 18 deletions nixd/lib/Controller/LifeTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@ opt<std::string> NixpkgsWorkerStderr{
desc("Writable file path for nixpkgs worker stderr (debugging)"),
cat(NixdCategory), init("/dev/null")};

void notifyNixpkgsEval(AttrSetClient &NixpkgsProvider) {
lspserver::log("launched nixd attrs eval for nixpkgs");
auto Action = [](llvm::Expected<EvalExprResponse> Resp) {
if (!Resp) {
lspserver::elog("error on nixpkgs attrs worker eval: {0}",
Resp.takeError());
return;
}
lspserver::log("evaluated nixpkgs entries");
};
// Tell nixpkgs worker to eval
NixpkgsProvider.evalExpr(DefaultNixpkgsExpr, std::move(Action));
}

void startNixpkgs(std::unique_ptr<AttrSetClientProc> &NixpkgsEval) {
NixpkgsEval = std::make_unique<AttrSetClientProc>([]() {
freopen(NixpkgsWorkerStderr.c_str(), "w", stderr);
Expand Down Expand Up @@ -129,10 +115,31 @@ void Controller::

Reply(std::move(Result));

PublishDiagnostic = mkOutNotifiction<PublishDiagnosticsParams>(
"textDocument/publishDiagnostics");
ClientCaps = Params.capabilities;

startNixpkgs(NixpkgsEval);
if (auto *Provider = nixpkgsEval().client())
notifyNixpkgsEval(*Provider);
if (nixpkgsClient()) {
auto Token = rand();
auto Action = [Token, this](llvm::Expected<EvalExprResponse> Resp) {
endWorkDoneProgress({
.token = Token,
.value = WorkDoneProgressEnd{.message = "evaluated nixpkgs entries"},
});
if (!Resp) {
lspserver::elog("error on nixpkgs attrs worker eval: {0}",
Resp.takeError());
return;
}
lspserver::log("evaluated nixpkgs entries");
};
createWorkDoneProgress({Token});
beginWorkDoneProgress({.token = Token,
.value = WorkDoneProgressBegin{
.title = "evaluating nixipkgs",
.cancellable = false,
.percentage = false,
}});
// Tell nixpkgs worker to eval
nixpkgsClient()->evalExpr(DefaultNixpkgsExpr, std::move(Action));
}
}
21 changes: 21 additions & 0 deletions nixd/lib/Controller/Support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ void Controller::actOnDocumentAdd(PathRef File,
Action();
}

void Controller::createWorkDoneProgress(
const lspserver::WorkDoneProgressCreateParams &Params) {
if (ClientCaps.WorkDoneProgress)
CreateWorkDoneProgress(Params, [](llvm::Expected<std::nullptr_t> Reply) {
if (!Reply)
elog("create workdone progress error: {0}", Reply.takeError());
});
}

Controller::Controller(std::unique_ptr<lspserver::InboundPort> In,
std::unique_ptr<lspserver::OutboundPort> Out)
: LSPServer(std::move(In), std::move(Out)), LitTest(false) {
Expand Down Expand Up @@ -135,6 +144,18 @@ Controller::Controller(std::unique_ptr<lspserver::InboundPort> In,
Registry.addMethod("textDocument/rename", this, &Controller::onRename);
Registry.addMethod("textDocument/prepareRename", this,
&Controller::onPrepareRename);

PublishDiagnostic = mkOutNotifiction<PublishDiagnosticsParams>(
"textDocument/publishDiagnostics");
CreateWorkDoneProgress =
mkOutMethod<WorkDoneProgressCreateParams, std::nullptr_t>(
"window/workDoneProgress/create");
BeginWorkDoneProgress =
mkOutNotifiction<ProgressParams<WorkDoneProgressBegin>>("$/progress");
ReportWorkDoneProgress =
mkOutNotifiction<ProgressParams<WorkDoneProgressReport>>("$/progress");
EndWorkDoneProgress =
mkOutNotifiction<ProgressParams<WorkDoneProgressEnd>>("$/progress");
}

} // namespace nixd

0 comments on commit ddd1e56

Please sign in to comment.