Skip to content

Commit

Permalink
nixd/Eval: refactor rpc method strings to header file
Browse files Browse the repository at this point in the history
Share these method definitions among the client and the provider.
  • Loading branch information
inclyc committed Jul 16, 2024
1 parent 3856001 commit b7b3a8e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
11 changes: 11 additions & 0 deletions nixd/include/nixd/Protocol/AttrSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@

namespace nixd {

namespace rpcMethod {

constexpr inline std::string_view EvalExpr = "attrset/evalExpr";
constexpr inline std::string_view AttrPathInfo = "attrset/attrpathInfo";
constexpr inline std::string_view AttrPathComplete = "attrset/attrpathComplete";
constexpr inline std::string_view OptionInfo = "attrset/optionInfo";
constexpr inline std::string_view OptionComplete = "attrset/optionComplete";
constexpr inline std::string_view Exit = "exit";

} // namespace rpcMethod

using EvalExprParams = std::string;
using EvalExprResponse = std::optional<std::string>;

Expand Down
14 changes: 7 additions & 7 deletions nixd/lib/Eval/AttrSetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ using namespace lspserver;
AttrSetClient::AttrSetClient(std::unique_ptr<lspserver::InboundPort> In,
std::unique_ptr<lspserver::OutboundPort> Out)
: LSPServer(std::move(In), std::move(Out)) {
EvalExpr = mkOutMethod<EvalExprParams, EvalExprResponse>("attrset/evalExpr");
EvalExpr = mkOutMethod<EvalExprParams, EvalExprResponse>(rpcMethod::EvalExpr);
AttrPathInfo = mkOutMethod<AttrPathInfoParams, AttrPathInfoResponse>(
"attrset/attrpathInfo");
rpcMethod::AttrPathInfo);
AttrPathComplete =
mkOutMethod<AttrPathCompleteParams, AttrPathCompleteResponse>(
"attrset/attrpathComplete");
OptionInfo =
mkOutMethod<AttrPathInfoParams, OptionInfoResponse>("attrset/optionInfo");
rpcMethod::AttrPathComplete);
OptionInfo = mkOutMethod<AttrPathInfoParams, OptionInfoResponse>(
rpcMethod::OptionInfo);
OptionComplete = mkOutMethod<AttrPathCompleteParams, OptionCompleteResponse>(
"attrset/optionComplete");
Exit = mkOutNotifiction<std::nullptr_t>("exit");
rpcMethod::OptionComplete);
Exit = mkOutNotifiction<std::nullptr_t>(rpcMethod::Exit);
}

const char *AttrSetClient::getExe() {
Expand Down
10 changes: 5 additions & 5 deletions nixd/lib/Eval/AttrSetProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ AttrSetProvider::AttrSetProvider(std::unique_ptr<InboundPort> In,
std::unique_ptr<OutboundPort> Out)
: LSPServer(std::move(In), std::move(Out)),
State(new nix::EvalState({}, nix::openStore())) {
Registry.addMethod("attrset/evalExpr", this, &AttrSetProvider::onEvalExpr);
Registry.addMethod("attrset/attrpathInfo", this,
Registry.addMethod(rpcMethod::EvalExpr, this, &AttrSetProvider::onEvalExpr);
Registry.addMethod(rpcMethod::AttrPathInfo, this,
&AttrSetProvider::onAttrPathInfo);
Registry.addMethod("attrset/attrpathComplete", this,
Registry.addMethod(rpcMethod::AttrPathComplete, this,
&AttrSetProvider::onAttrPathComplete);
Registry.addMethod("attrset/optionInfo", this,
Registry.addMethod(rpcMethod::OptionInfo, this,
&AttrSetProvider::onOptionInfo);
Registry.addMethod("attrset/optionComplete", this,
Registry.addMethod(rpcMethod::OptionComplete, this,
&AttrSetProvider::onOptionComplete);
}

Expand Down
2 changes: 1 addition & 1 deletion nixd/lspserver/include/lspserver/LSPBinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct HandlerRegistry {
/// Handler should be e.g. void peek(const PeekParams&, Callback<PeekResult>);
/// PeekParams must be JSON-parseable and PeekResult must be serializable.
template <typename Param, typename Result, typename ThisT>
void addMethod(llvm::StringLiteral Method, ThisT *This,
void addMethod(llvm::StringRef Method, ThisT *This,
void (ThisT::*Handler)(const Param &, Callback<Result>)) {
MethodHandlers[Method] = [Method, Handler, This](JSON RawParams,
Callback<JSON> Reply) {
Expand Down

0 comments on commit b7b3a8e

Please sign in to comment.