Skip to content

Commit

Permalink
nixd/Server: construct names of nested attr set from tree-sitter
Browse files Browse the repository at this point in the history
Fixes: #247
  • Loading branch information
inclyc committed Aug 20, 2023
1 parent d1704af commit dac20e0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 50 deletions.
2 changes: 1 addition & 1 deletion nixd/include/nixd/Server/IPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Diagnostics : WorkerMessage {
};

struct AttrPathParams {
std::string Path;
std::vector<std::string> Path;
};

struct EvalParams {
Expand Down
46 changes: 0 additions & 46 deletions nixd/lib/Server/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<lspserver::Location>(
"nixd/ipc/option/textDocument/declaration", APParams,
{OptionWorkers, OptionWorkerLock, 2e4});
Expand Down Expand Up @@ -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<lspserver::CompletionList>(
"nixd/ipc/textDocument/completion/options", APParams,
{OptionWorkers, OptionWorkerLock, 1e5});
Expand Down
12 changes: 9 additions & 3 deletions nixd/lib/Server/OptionWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "nixd/Support/Diagnostic.h"
#include "nixd/Support/ReplyRAII.h"

#include <boost/algorithm/string.hpp>

#include <mutex>

namespace nixd {
Expand All @@ -29,6 +31,7 @@ void OptionWorker::onDeclaration(
const ipc::AttrPathParams &Params,
lspserver::Callback<lspserver::Location> Reply) {
using namespace lspserver;
using boost::algorithm::join;
ReplyRAII<Location> RR(std::move(Reply));
if (!OptionAttrSet)
return;
Expand All @@ -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;
}
Expand Down Expand Up @@ -106,6 +110,7 @@ void OptionWorker::onEvalOptionSet(
void OptionWorker::onCompletion(const ipc::AttrPathParams &Params,
lspserver::Callback<llvm::json::Value> Reply) {
using namespace lspserver;
using boost::algorithm::join;
ReplyRAII<CompletionList> RR(std::move(Reply));

if (!OptionAttrSet)
Expand All @@ -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;
}

Expand Down

0 comments on commit dac20e0

Please sign in to comment.