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

completeFlakeRefWithFragment: handle names with dots #11377

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 25 additions & 5 deletions src/libcmd/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,28 @@ void completeFlakeRefWithFragment(

for (auto & attrPathPrefixS : attrPathPrefixes) {
auto attrPathPrefix = parseAttrPath(*evalState, attrPathPrefixS);
auto attrPathS = attrPathPrefixS + std::string(fragment);
auto attrPath = parseAttrPath(*evalState, attrPathS);
// we receive backslashes as typed; left alone, they get into attr names.
auto fragmentUnshell = replaceStrings(std::string(fragment), "\\\"", "\"");
auto attrPathS = attrPathPrefixS + fragmentUnshell;
auto incompleteQuote = false;

std::vector<Symbol> attrPath;
try {
attrPath = parseAttrPath(*evalState, attrPathS);
} catch (const ParseError&) {
attrPath = parseAttrPath(*evalState, attrPathS + "\"");
incompleteQuote = true;
}

std::string lastAttr;
if (!attrPath.empty() && !hasSuffix(attrPathS, ".")) {
while (!attrPath.empty() && !hasSuffix(attrPathS, ".")) {
lastAttr = evalState->symbols[attrPath.back()];
attrPath.pop_back();
if (incompleteQuote && lastAttr.empty()) {
incompleteQuote = false;
continue;
}
break;
}

auto attr = root->findAlongAttrPath(attrPath);
Expand All @@ -376,8 +391,13 @@ void completeFlakeRefWithFragment(
auto attrPath2 = (*attr)->getAttrPath(attr2);
/* Strip the attrpath prefix. */
attrPath2.erase(attrPath2.begin(), attrPath2.begin() + attrPathPrefix.size());
// FIXME: handle names with dots
completions.add(flakeRefS + "#" + prefixRoot + concatStringsSep(".", evalState->symbols.resolve(attrPath2)));
Strings escs;
for (auto sym : evalState->symbols.resolve(attrPath2)) {
std::stringstream ss;
ss << sym;
escs.push_back(replaceStrings(ss.str(), "\"", "\\\""));
}
completions.add(flakeRefS + "#" + prefixRoot + concatStringsSep(".", escs));
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/completions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ mkdir -p err
cat <<EOF > err/flake.nix
throw "error"
EOF
mkdir -p hasdots
cat <<EOF > hasdots/flake.nix
{
outputs = i: {
"sample.dotted.output" = {
subAttr = 1;
};
};
}
EOF

# Test the completion of a subcommand
[[ "$(NIX_GET_COMPLETIONS=1 nix buil)" == $'normal\nbuild\t' ]]
Expand Down Expand Up @@ -73,3 +83,5 @@ NIX_GET_COMPLETIONS=3 nix build --option allow-import-from | grep -- "allow-impo
[[ "$(NIX_GET_COMPLETIONS=4 nix eval --file ./foo/flake.nix outp)" == $'attrs\noutputs\t' ]]
[[ "$(NIX_GET_COMPLETIONS=4 nix eval --file ./err/flake.nix outp 2>&1)" == $'attrs' ]]
[[ "$(NIX_GET_COMPLETIONS=2 nix eval ./err\# 2>&1)" == $'attrs' ]]
[[ "$(NIX_GET_COMPLETIONS=2 nix eval './hasdots#s')" == $'attrs\n./hasdots#\\"sample.dotted.output\\"\t' ]]
[[ "$(NIX_GET_COMPLETIONS=2 nix eval './hasdots#\"sample.dotted.output\".s')" == $'attrs\n./hasdots#\\"sample.dotted.output\\".subAttr\t' ]]
Loading