forked from lix-project/lix
6526d1676b
Pos objects are somewhat wasteful as they duplicate the origin file name and input type for each object. on files that produce more than one Pos when parsed this a sizeable waste of memory (one pointer per Pos). the same goes for ptr<Pos> on 64 bit machines: parsing enough source to require 8 bytes to locate a position would need at least 8GB of input and 64GB of expression memory. it's not likely that we'll hit that any time soon, so we can use a uint32_t index to locate positions instead.
56 lines
1.3 KiB
C++
56 lines
1.3 KiB
C++
#include "command.hh"
|
|
#include "shared.hh"
|
|
#include "eval.hh"
|
|
#include "attr-path.hh"
|
|
#include "progress-bar.hh"
|
|
|
|
#include <unistd.h>
|
|
|
|
using namespace nix;
|
|
|
|
struct CmdEdit : InstallableCommand
|
|
{
|
|
std::string description() override
|
|
{
|
|
return "open the Nix expression of a Nix package in $EDITOR";
|
|
}
|
|
|
|
std::string doc() override
|
|
{
|
|
return
|
|
#include "edit.md"
|
|
;
|
|
}
|
|
|
|
Category category() override { return catSecondary; }
|
|
|
|
void run(ref<Store> store) override
|
|
{
|
|
auto state = getEvalState();
|
|
|
|
const auto [file, line] = [&] {
|
|
auto [v, pos] = installable->toValue(*state);
|
|
|
|
try {
|
|
return findPackageFilename(*state, *v, installable->what());
|
|
} catch (NoPositionInfo &) {
|
|
throw Error("cannot find position information for '%s", installable->what());
|
|
}
|
|
}();
|
|
|
|
stopProgressBar();
|
|
|
|
auto args = editorFor(file, line);
|
|
|
|
restoreProcessContext();
|
|
|
|
execvp(args.front().c_str(), stringsToCharPtrs(args).data());
|
|
|
|
std::string command;
|
|
for (const auto &arg : args) command += " '" + arg + "'";
|
|
throw SysError("cannot run command%s", command);
|
|
}
|
|
};
|
|
|
|
static auto rCmdEdit = registerCommand<CmdEdit>("edit");
|