mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-27 00:06:16 +02:00
libexpr: add findDerivationFilename
extract the derivation to filename:lineno heuristic
This commit is contained in:
parent
207a537343
commit
59c7249769
4 changed files with 36 additions and 43 deletions
|
@ -93,4 +93,32 @@ Value * findAlongAttrPath(EvalState & state, const string & attrPath,
|
|||
}
|
||||
|
||||
|
||||
std::tuple<std::string, int> findDerivationFilename(EvalState & state, Value & v, std::string what)
|
||||
{
|
||||
Value * v2;
|
||||
try {
|
||||
auto dummyArgs = state.allocBindings(0);
|
||||
v2 = findAlongAttrPath(state, "meta.position", *dummyArgs, v);
|
||||
} catch (Error &) {
|
||||
throw Error("package '%s' has no source location information", what);
|
||||
}
|
||||
|
||||
auto pos = state.forceString(*v2);
|
||||
|
||||
auto colon = pos.rfind(':');
|
||||
if (colon == std::string::npos)
|
||||
throw Error("cannot parse meta.position attribute '%s'", pos);
|
||||
|
||||
std::string filename(pos, 0, colon);
|
||||
int lineno;
|
||||
try {
|
||||
lineno = std::stoi(std::string(pos, colon + 1));
|
||||
} catch (std::invalid_argument & e) {
|
||||
throw Error("cannot parse line number '%s'", pos);
|
||||
}
|
||||
|
||||
return std::make_tuple(filename, lineno);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -4,10 +4,15 @@
|
|||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
|
||||
namespace nix {
|
||||
|
||||
Value * findAlongAttrPath(EvalState & state, const string & attrPath,
|
||||
Bindings & autoArgs, Value & vIn);
|
||||
|
||||
/* Heuristic to find the filename and lineno or a derivation. */
|
||||
std::tuple<std::string, int> findDerivationFilename(EvalState & state,
|
||||
Value & v, std::string what);
|
||||
|
||||
}
|
||||
|
|
|
@ -36,28 +36,9 @@ struct CmdEdit : InstallableCommand
|
|||
|
||||
auto v = installable->toValue(*state);
|
||||
|
||||
Value * v2;
|
||||
try {
|
||||
auto dummyArgs = state->allocBindings(0);
|
||||
v2 = findAlongAttrPath(*state, "meta.position", *dummyArgs, *v);
|
||||
} catch (Error &) {
|
||||
throw Error("package '%s' has no source location information", installable->what());
|
||||
}
|
||||
|
||||
auto pos = state->forceString(*v2);
|
||||
debug("position is %s", pos);
|
||||
|
||||
auto colon = pos.rfind(':');
|
||||
if (colon == std::string::npos)
|
||||
throw Error("cannot parse meta.position attribute '%s'", pos);
|
||||
|
||||
std::string filename(pos, 0, colon);
|
||||
std::string filename;
|
||||
int lineno;
|
||||
try {
|
||||
lineno = std::stoi(std::string(pos, colon + 1));
|
||||
} catch (std::invalid_argument & e) {
|
||||
throw Error("cannot parse line number '%s'", pos);
|
||||
}
|
||||
std::tie(filename, lineno) = findDerivationFilename(*state, *v, installable->what());
|
||||
|
||||
stopProgressBar();
|
||||
|
||||
|
|
|
@ -481,28 +481,7 @@ bool NixRepl::processLine(string line)
|
|||
lineno = 0;
|
||||
} else {
|
||||
// assume it's a derivation
|
||||
Value * v2;
|
||||
try {
|
||||
auto dummyArgs = state.allocBindings(0);
|
||||
v2 = findAlongAttrPath(state, "meta.position", *dummyArgs, v);
|
||||
} catch (Error &) {
|
||||
throw Error("package '%s' has no source location information", arg);
|
||||
}
|
||||
|
||||
auto pos = state.forceString(*v2);
|
||||
debug("position is %s", pos);
|
||||
|
||||
auto colon = pos.rfind(':');
|
||||
if (colon == std::string::npos)
|
||||
throw Error("cannot parse meta.position attribute '%s'", pos);
|
||||
|
||||
filename = std::string(pos, 0, colon);
|
||||
try {
|
||||
lineno = std::stoi(std::string(pos, colon + 1));
|
||||
} catch (std::invalid_argument & e) {
|
||||
throw Error("cannot parse line number '%s'", pos);
|
||||
}
|
||||
|
||||
std::tie(filename, lineno) = findDerivationFilename(state, v, arg);
|
||||
}
|
||||
|
||||
// Open in EDITOR
|
||||
|
|
Loading…
Reference in a new issue