From 66c3649e699e1f42b31434eb6c1bd28beecfb7aa Mon Sep 17 00:00:00 2001 From: Yingchi Long Date: Sat, 19 Aug 2023 13:41:18 +0800 Subject: [PATCH] nixd/Server: use std::vector for attrpaths in IPC Fixes: #247 --- nixd/include/nixd/Server/IPC.h | 2 +- nixd/lib/Server/Controller.cpp | 46 -------------------------------- nixd/lib/Server/OptionWorker.cpp | 12 ++++++--- 3 files changed, 10 insertions(+), 50 deletions(-) diff --git a/nixd/include/nixd/Server/IPC.h b/nixd/include/nixd/Server/IPC.h index 8a37aaad0..be197dee2 100644 --- a/nixd/include/nixd/Server/IPC.h +++ b/nixd/include/nixd/Server/IPC.h @@ -20,7 +20,7 @@ struct Diagnostics : WorkerMessage { }; struct AttrPathParams { - std::string Path; + std::vector Path; }; struct EvalParams { diff --git a/nixd/lib/Server/Controller.cpp b/nixd/lib/Server/Controller.cpp index 852a235dd..9c487d46d 100644 --- a/nixd/lib/Server/Controller.cpp +++ b/nixd/lib/Server/Controller.cpp @@ -406,37 +406,6 @@ void Controller::onDecalration( ipc::AttrPathParams APParams; - // Try to get current attribute path, expand the position - - auto Code = llvm::StringRef( - *DraftMgr.getDraft(Params.textDocument.uri.file())->Contents); - auto ExpectedOffset = positionToOffset(Code, Params.position); - - if (!ExpectedOffset) - RR.Response = ExpectedOffset.takeError(); - - auto Offset = ExpectedOffset.get(); - auto From = Offset; - auto To = Offset; - - std::string Punc = "\r\n\t ;"; - - auto IsPunc = [&Punc](char C) { - for (const auto Char : Punc) { - if (Char == C) - return true; - } - return false; - }; - - for (; From >= 0 && !IsPunc(Code[From]);) - From--; - for (; To < Code.size() && !IsPunc(Code[To]);) - To++; - - APParams.Path = Code.substr(From, To - From).trim(Punc); - lspserver::log("requesting path: {0}", APParams.Path); - auto Responses = askWC( "nixd/ipc/option/textDocument/declaration", APParams, {OptionWorkers, OptionWorkerLock, 2e4}); @@ -572,21 +541,6 @@ void Controller::onCompletion(const lspserver::CompletionParams &Params, } if (OptionsEnabled) { ipc::AttrPathParams APParams; - - if (Params.context.triggerCharacter == ".") { - // Get nixpkgs options - // TODO: split this in AST, use AST-based attrpath construction. - auto Code = llvm::StringRef( - *DraftMgr.getDraft(Params.textDocument.uri.file())->Contents); - auto ExpectedPosition = positionToOffset(Code, Params.position); - - // get the attr path - auto TruncateBackCode = Code.substr(0, ExpectedPosition.get()); - - auto [_, AttrPath] = TruncateBackCode.rsplit(" "); - APParams.Path = AttrPath.str(); - } - auto Resp = askWC( "nixd/ipc/textDocument/completion/options", APParams, {OptionWorkers, OptionWorkerLock, 1e5}); diff --git a/nixd/lib/Server/OptionWorker.cpp b/nixd/lib/Server/OptionWorker.cpp index 9c04a86fe..f63e84e3f 100644 --- a/nixd/lib/Server/OptionWorker.cpp +++ b/nixd/lib/Server/OptionWorker.cpp @@ -7,6 +7,8 @@ #include "nixd/Support/Diagnostic.h" #include "nixd/Support/ReplyRAII.h" +#include + #include namespace nixd { @@ -29,6 +31,7 @@ void OptionWorker::onDeclaration( const ipc::AttrPathParams &Params, lspserver::Callback Reply) { using namespace lspserver; + using boost::algorithm::join; ReplyRAII RR(std::move(Reply)); if (!OptionAttrSet) return; @@ -38,8 +41,9 @@ void OptionWorker::onDeclaration( try { nix::Value *V = OptionAttrSet; if (!Params.Path.empty()) { + auto AttrPath = join(Params.Path, "."); auto &Bindings(*OptionIES->getState()->allocBindings(0)); - V = nix::findAlongAttrPath(*OptionIES->getState(), Params.Path, Bindings, + V = nix::findAlongAttrPath(*OptionIES->getState(), AttrPath, Bindings, *OptionAttrSet) .first; } @@ -106,6 +110,7 @@ void OptionWorker::onEvalOptionSet( void OptionWorker::onCompletion(const ipc::AttrPathParams &Params, lspserver::Callback Reply) { using namespace lspserver; + using boost::algorithm::join; ReplyRAII RR(std::move(Reply)); if (!OptionAttrSet) @@ -121,9 +126,10 @@ void OptionWorker::onCompletion(const ipc::AttrPathParams &Params, if (OptionAttrSet->type() == nix::ValueType::nAttrs) { nix::Value *V = OptionAttrSet; if (!Params.Path.empty()) { + auto AttrPath = join(Params.Path, "."); auto &Bindings(*OptionIES->getState()->allocBindings(0)); - V = nix::findAlongAttrPath(*OptionIES->getState(), Params.Path, - Bindings, *OptionAttrSet) + V = nix::findAlongAttrPath(*OptionIES->getState(), AttrPath, Bindings, + *OptionAttrSet) .first; }