-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nixd/Server: split worker in a dedicated class
TODO: sync the draft store
- Loading branch information
Showing
15 changed files
with
481 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <nix/eval-inline.hh> | ||
#include <nix/shared.hh> | ||
|
||
namespace nix { | ||
|
||
// Copy-paste from nix source code, do not know why it is inlined. | ||
|
||
inline void EvalState::evalAttrs(Env &env, Expr *e, Value &v, const PosIdx pos, | ||
std::string_view errorCtx) { | ||
try { | ||
e->eval(*this, env, v); | ||
if (v.type() != nAttrs) | ||
error("value is %1% while a set was expected", showType(v)) | ||
.withFrame(env, *e) | ||
.debugThrow<TypeError>(); | ||
} catch (Error &e) { | ||
e.addTrace(positions[pos], errorCtx); | ||
throw; | ||
} | ||
} | ||
|
||
} // namespace nix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <nix/eval.hh> | ||
#include <nix/globals.hh> | ||
#include <nix/shared.hh> | ||
|
||
namespace nixd { | ||
|
||
inline void initEval() { | ||
nix::initNix(); | ||
nix::initLibStore(); | ||
nix::initPlugins(); | ||
nix::initGC(); | ||
} | ||
|
||
} // namespace nixd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include "nixd/Server/EvalDraftStore.h" | ||
#include "nixd/Support/Diagnostic.h" | ||
#include "nixd/Support/JSONSerialization.h" | ||
#include "nixd/Support/ReplyRAII.h" | ||
|
||
#include "lspserver/LSPServer.h" | ||
|
||
namespace nixd { | ||
|
||
class EvalWorker : public lspserver::LSPServer { | ||
llvm::unique_function<void(const ipc::Diagnostics &)> EvalDiagnostic; | ||
|
||
std::unique_ptr<IValueEvalResult> IER; | ||
|
||
template <class ReplyTy> | ||
void withAST( | ||
const std::string &, ReplyRAII<ReplyTy>, | ||
llvm::unique_function<void(nix::ref<EvalAST>, ReplyRAII<ReplyTy> &&)>); | ||
|
||
public: | ||
EvalWorker(std::unique_ptr<lspserver::InboundPort> In, | ||
std::unique_ptr<lspserver::OutboundPort> Out); | ||
|
||
void onEvalDefinition(const lspserver::TextDocumentPositionParams &, | ||
lspserver::Callback<lspserver::Location>); | ||
|
||
void onEvalHover(const lspserver::TextDocumentPositionParams &, | ||
lspserver::Callback<llvm::json::Value>); | ||
|
||
void onEvalCompletion(const lspserver::CompletionParams &, | ||
lspserver::Callback<llvm::json::Value>); | ||
}; | ||
|
||
template <class ReplyTy> | ||
void EvalWorker::withAST( | ||
const std::string &RequestedFile, ReplyRAII<ReplyTy> RR, | ||
llvm::unique_function<void(nix::ref<EvalAST>, ReplyRAII<ReplyTy> &&)> | ||
Action) { | ||
try { | ||
auto AST = IER->Forest.at(RequestedFile); | ||
try { | ||
Action(AST, std::move(RR)); | ||
} catch (std::exception &E) { | ||
RR.Response = | ||
lspserver::error("something uncaught in the AST action, reason {0}", | ||
stripANSI(E.what())); | ||
} | ||
} catch (std::out_of_range &E) { | ||
RR.Response = lspserver::error("no AST available on requested file {0}", | ||
RequestedFile); | ||
} | ||
} | ||
|
||
} // namespace nixd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include "lspserver/Protocol.h" | ||
|
||
#include <nix/eval.hh> | ||
|
||
#include <vector> | ||
|
||
namespace nixd { | ||
|
||
struct CompletionHelper { | ||
using Items = std::vector<lspserver::CompletionItem>; | ||
static void fromEnv(nix::EvalState &State, nix::Env &NixEnv, Items &Items); | ||
static void fromStaticEnv(const nix::SymbolTable &STable, | ||
const nix::StaticEnv &SEnv, Items &Items); | ||
}; | ||
|
||
} // namespace nixd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#pragma once | ||
|
||
#include "lspserver/Protocol.h" | ||
|
||
#include <llvm/Support/JSON.h> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include "lspserver/Function.h" | ||
#include "lspserver/Logger.h" | ||
|
||
namespace nixd { | ||
|
||
template <class ReplyTy> struct ReplyRAII { | ||
lspserver::Callback<ReplyTy> R; | ||
llvm::Expected<ReplyTy> Response = lspserver::error("no response available"); | ||
ReplyRAII(decltype(R) R) : R(std::move(R)) {} | ||
~ReplyRAII() { | ||
if (R) | ||
R(std::move(Response)); | ||
}; | ||
ReplyRAII(ReplyRAII &&Old) noexcept { | ||
R = std::move(Old.R); | ||
Response = std::move(Old.Response); | ||
} | ||
}; | ||
|
||
} // namespace nixd |
Oops, something went wrong.