Skip to content

Commit

Permalink
nixd/Server: rewrite attrpath construction to "nested" + "code"
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Aug 26, 2023
1 parent 4636092 commit 3959db7
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions nixd/lib/Server/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,25 +560,33 @@ void Controller::onCompletion(const lspserver::CompletionParams &Params,
auto TriggerCharacter = Params.context.triggerCharacter;
std::vector<std::string> AttrPath;

if (TriggerCharacter == ".") {
// Workaround for trigger character "."
// Our parser generates incorrect attrpath for the following example
// {
// foo.|
// boot.isContainer = 1;
// ^~~~~~~~~~~~~~~~ -> "foo.boot.isContainer"
// }
auto Code = llvm::StringRef(*Draft.Contents);
auto AttrPathStr = getAttrPathFromCode(Code, Position);
llvm::SmallVector<llvm::StringRef> AttrPathVec;
AttrPathStr.split(AttrPathVec, ".");
AttrPath = AST.getAttrPath(AST.lookupContainMin(Position));
for (const auto &Attr : AttrPathVec)
AttrPath.emplace_back(Attr.str());
} else {
AttrLocator Locator(AST);
AttrPath = AST.getAttrPath(AST.lookupContainMin(Position));
}
// Nested level, e.g.
// {
// foo = {
// bar = { | };
// }; ^ <- [ "foo", "bar" ]
// }
auto NestedAttrPath = AST.getAttrPath(AST.lookupContainMin(Position));

// "Code"AttrPath, extracted by string manipulation (not parsing)
// e.g. : { foo.bar.b| }
// ^~~~~~~~~~ <- [ "foo", "bar", "b" ]
auto Code = llvm::StringRef(*Draft.Contents);
auto AttrPathStr = getAttrPathFromCode(Code, Position);
llvm::SmallVector<llvm::StringRef> CodeAttrPath;
AttrPathStr.split(CodeAttrPath, ".");

// Pops the last item, it is incomplete
// {
// boot.isContain
// } ^~~~~~~~~ pops this
if (!CodeAttrPath.empty() && TriggerCharacter != ".")
CodeAttrPath.pop_back();

// Merge "nested" and "code" attrpath, for completion
AttrPath = std::move(NestedAttrPath);
for (const auto &Attr : CodeAttrPath)
AttrPath.emplace_back(Attr.str());

ipc::AttrPathParams APParams;
APParams.Path = std::move(AttrPath);
Expand Down

0 comments on commit 3959db7

Please sign in to comment.