Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixd/Sema: filter item prefix for completion builder #242

Merged
merged 2 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions nixd/include/nixd/Sema/CompletionBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "lspserver/Protocol.h"

#include <utility>
#include <vector>

namespace nixd {
Expand All @@ -14,6 +15,9 @@ using CompletionResult = lspserver::CompletionList;
class CompletionBuilder {
CompletionResult Result;
size_t Limit = 1000;
std::string Prefix;

void addItem(lspserver::CompletionItem Item);

public:
/// { a = 1; b = 2; }.|
Expand Down Expand Up @@ -44,6 +48,8 @@ class CompletionBuilder {
CompletionResult &getResult() { return Result; }

void setLimit(size_t Limit) { this->Limit = Limit; }

void setPrefix(std::string Prefix) { this->Prefix = std::move(Prefix); }
};

} // namespace nixd
50 changes: 25 additions & 25 deletions nixd/lib/Sema/CompletionBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ namespace nixd {
using lspserver::CompletionItem;
using lspserver::CompletionItemKind;

void CompletionBuilder::addItem(lspserver::CompletionItem Item) {
if (!Item.label.starts_with(Prefix))
return;

if (Result.items.size() >= Limit) {
Result.isIncomplete = true;
return;
}

Result.items.emplace_back(std::move(Item));
}

void CompletionBuilder::addAttrFields(const EvalAST &AST,
const lspserver::Position &Pos,
nix::EvalState &State) {
Expand All @@ -25,30 +37,23 @@ void CompletionBuilder::addAttrFields(const EvalAST &AST,
return;

for (const auto &Attr : *OpV->attrs) {

CompletionItem R;
R.label = State.symbols[Attr.name];
R.kind = CompletionItemKind::Field;
Result.items.emplace_back(std::move(R));

if (Result.items.size() > Limit) {
Result.isIncomplete = true;
break;
}
addItem(std::move(R));
}
}

void CompletionBuilder::addSymbols(const ParseAST &AST, const nix::Expr *Node) {
std::vector<nix::Symbol> Symbols;
AST.collectSymbols(Node, Symbols);
// Insert symbols to our completion list.
std::transform(Symbols.begin(), Symbols.end(),
std::back_inserter(Result.items), [&](const nix::Symbol &V) {
lspserver::CompletionItem R;
R.kind = CompletionItemKind::Interface;
R.label = AST.symbols()[V];
return R;
});
for (const auto &Symbol : Symbols) {
lspserver::CompletionItem R;
R.kind = CompletionItemKind::Interface;
R.label = AST.symbols()[Symbol];
addItem(std::move(R));
}
}

void CompletionBuilder::addLambdaFormals(const EvalAST &AST,
Expand Down Expand Up @@ -81,7 +86,7 @@ void CompletionBuilder::addLambdaFormals(const EvalAST &AST,
CompletionItem R;
R.label = State.symbols[Formal.name];
R.kind = CompletionItemKind::Constructor;
Result.items.emplace_back(std::move(R));
addItem(std::move(R));
}
}

Expand All @@ -95,16 +100,10 @@ void CompletionBuilder::addEnv(nix::EvalState &State, nix::Env &NixEnv) {
}
if (NixEnv.type == nix::Env::HasWithAttrs) {
for (const auto &SomeAttr : *NixEnv.values[0]->attrs) {
std::string Name = State.symbols[SomeAttr.name];
lspserver::CompletionItem R;
R.label = Name;
R.label = State.symbols[SomeAttr.name];
R.kind = lspserver::CompletionItemKind::Variable;
Result.items.emplace_back(std::move(R));

if (Result.items.size() > Limit) {
Result.isIncomplete = true;
break;
}
addItem(std::move(R));
}
}
if (NixEnv.up)
Expand All @@ -123,13 +122,14 @@ void CompletionBuilder::addStaticEnv(const nix::SymbolTable &STable,
const nix::StaticEnv &SEnv) {
for (auto [Symbol, Displ] : SEnv.vars) {
std::string Name = STable[Symbol];
inclyc marked this conversation as resolved.
Show resolved Hide resolved

if (Name.starts_with("__"))
continue;

CompletionItem R;
R.label = Name;
R.label = std::move(Name);
R.kind = CompletionItemKind::Constant;
Result.items.emplace_back(std::move(R));
addItem(std::move(R));
}

if (SEnv.up)
Expand Down
7 changes: 7 additions & 0 deletions nixd/lib/Server/EvalWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <llvm/ADT/StringRef.h>

#include <nix/nixexpr.hh>

namespace nixd {

EvalWorker::EvalWorker(std::unique_ptr<lspserver::InboundPort> In,
Expand Down Expand Up @@ -171,6 +173,11 @@ void EvalWorker::onCompletion(const lspserver::CompletionParams &Params,
Builder.addAttrFields(*AST, Params.position, *State);
} else {
const auto *Node = AST->lookupContainMin(Params.position);

if (const auto *EVar = dynamic_cast<const nix::ExprVar *>(Node)) {
Builder.setPrefix(State->symbols[EVar->name]);
}

Builder.addSymbols(*AST, Node);
Builder.addLambdaFormals(*AST, *State, Node);
Builder.addEnv(*AST, *State, Node);
Expand Down