Fix nix shebang interaction with #8131 overhaul completions

This commit is contained in:
Robert Hensing 2023-10-23 18:38:54 +02:00 committed by tomberek
parent e91fd837ee
commit ffd414eb75
4 changed files with 25 additions and 29 deletions

View file

@ -294,7 +294,9 @@ void completeFlakeRefWithFragment(
prefixRoot = ".";
}
auto flakeRefS = std::string(prefix.substr(0, hash));
auto flakeRef = parseFlakeRef(expandTilde(flakeRefS), absPath(getCommandBaseDir()));
// TODO: ideally this would use the command base directory instead of assuming ".".
auto flakeRef = parseFlakeRef(expandTilde(flakeRefS), absPath("."));
auto evalCache = openEvalCache(*evalState,
std::make_shared<flake::LockedFlake>(lockFlake(*evalState, flakeRef, lockFlags)));

View file

@ -80,12 +80,6 @@ std::optional<std::string> RootArgs::needsCompletion(std::string_view s)
return {};
}
void RootArgs::parseCmdline(const Strings & _cmdline)
{
// Default via 5.1.2.2.1 in C standard
Args::parseCmdline(_cmdline, false);
}
/**
* Basically this is `typedef std::optional<Parser> Parser(std::string_view s, Strings & r);`
*
@ -227,7 +221,7 @@ static Strings parseShebangContent(std::string_view s) {
return result;
}
void Args::parseCmdline(const Strings & _cmdline, bool allowShebang)
void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang)
{
Strings pendingArgs;
bool dashDash = false;
@ -339,10 +333,13 @@ void Args::parseCmdline(const Strings & _cmdline, bool allowShebang)
Path Args::getCommandBaseDir() const
{
if (parent)
return parent->getCommandBaseDir();
else
return commandBaseDir;
assert(parent);
return parent->getCommandBaseDir();
}
Path RootArgs::getCommandBaseDir() const
{
return commandBaseDir;
}
bool Args::processFlag(Strings::iterator & pos, Strings::iterator end)

View file

@ -24,24 +24,9 @@ class AddCompletions;
class Args
{
/**
* @brief The command's "working directory", but only set when top level.
*
* Use getCommandBaseDir() to get the directory regardless of whether this
* is a top-level command or subcommand.
*
* @see getCommandBaseDir()
*/
Path commandBaseDir = ".";
public:
/**
* Parse the command line with argv0, throwing a UsageError if something
goes wrong.
*/
void parseCmdline(const Strings & _cmdline, bool allowShebang);
/**
* Return a short one-line description of the command.
*/
@ -62,7 +47,7 @@ public:
*
* This only returns the correct value after parseCmdline() has run.
*/
Path getCommandBaseDir() const;
virtual Path getCommandBaseDir() const;
protected:

View file

@ -29,14 +29,26 @@ struct Completions final : AddCompletions
*/
class RootArgs : virtual public Args
{
/**
* @brief The command's "working directory", but only set when top level.
*
* Use getCommandBaseDir() to get the directory regardless of whether this
* is a top-level command or subcommand.
*
* @see getCommandBaseDir()
*/
Path commandBaseDir = ".";
public:
/** Parse the command line, throwing a UsageError if something goes
* wrong.
*/
void parseCmdline(const Strings & cmdline);
void parseCmdline(const Strings & cmdline, bool allowShebang = false);
std::shared_ptr<Completions> completions;
Path getCommandBaseDir() const override;
protected:
friend class Args;