aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpennae <github@quasiparticle.net>2022-03-04 20:54:50 +0100
committerpennae <github@quasiparticle.net>2022-04-21 21:25:18 +0200
commit39df15fb8e766c0a4fa2fda83784fb8a478a766c (patch)
tree8f60f47059cf33b6b9c62b92bbbd6783868b1d47 /src
parent38de79fcf7e00187107e638036c010911d1b675b (diff)
don't use full Pos for findPackageFilename/editorFor
only file and line of the returned position were ever used, it wasn't actually used a position. as such we may as well use a path+int pair for only those two values and remove a use of Pos that would not work well with a position table.
Diffstat (limited to 'src')
-rw-r--r--src/libcmd/command.cc8
-rw-r--r--src/libcmd/command.hh2
-rw-r--r--src/libexpr/attr-path.cc6
-rw-r--r--src/libexpr/attr-path.hh2
-rw-r--r--src/nix/edit.cc16
-rw-r--r--src/nix/repl.cc26
6 files changed, 29 insertions, 31 deletions
diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc
index a53b029b7..f28cfe5de 100644
--- a/src/libcmd/command.cc
+++ b/src/libcmd/command.cc
@@ -197,17 +197,17 @@ void StorePathCommand::run(ref<Store> store, std::vector<StorePath> && storePath
run(store, *storePaths.begin());
}
-Strings editorFor(const Pos & pos)
+Strings editorFor(const Path & file, uint32_t line)
{
auto editor = getEnv("EDITOR").value_or("cat");
auto args = tokenizeString<Strings>(editor);
- if (pos.line > 0 && (
+ if (line > 0 && (
editor.find("emacs") != std::string::npos ||
editor.find("nano") != std::string::npos ||
editor.find("vim") != std::string::npos ||
editor.find("kak") != std::string::npos))
- args.push_back(fmt("+%d", pos.line));
- args.push_back(pos.file);
+ args.push_back(fmt("+%d", line));
+ args.push_back(file);
return args;
}
diff --git a/src/libcmd/command.hh b/src/libcmd/command.hh
index 84bbb5292..078e2a2ce 100644
--- a/src/libcmd/command.hh
+++ b/src/libcmd/command.hh
@@ -219,7 +219,7 @@ static RegisterCommand registerCommand2(std::vector<std::string> && name)
/* Helper function to generate args that invoke $EDITOR on
filename:lineno. */
-Strings editorFor(const Pos & pos);
+Strings editorFor(const Path & file, uint32_t line);
struct MixProfile : virtual StoreCommand
{
diff --git a/src/libexpr/attr-path.cc b/src/libexpr/attr-path.cc
index 32deecfae..c6e3a9c92 100644
--- a/src/libexpr/attr-path.cc
+++ b/src/libexpr/attr-path.cc
@@ -106,7 +106,7 @@ std::pair<Value *, Pos> findAlongAttrPath(EvalState & state, const std::string &
}
-Pos findPackageFilename(EvalState & state, Value & v, std::string what)
+std::pair<std::string, uint32_t> findPackageFilename(EvalState & state, Value & v, std::string what)
{
Value * v2;
try {
@@ -132,9 +132,7 @@ Pos findPackageFilename(EvalState & state, Value & v, std::string what)
throw ParseError("cannot parse line number '%s'", pos);
}
- Symbol file = state.symbols.create(filename);
-
- return { foFile, file, lineno, 0 };
+ return { std::move(filename), lineno };
}
diff --git a/src/libexpr/attr-path.hh b/src/libexpr/attr-path.hh
index ff1135a06..f06d28f7f 100644
--- a/src/libexpr/attr-path.hh
+++ b/src/libexpr/attr-path.hh
@@ -17,7 +17,7 @@ std::pair<Value *, Pos> findAlongAttrPath(
Value & vIn);
/* Heuristic to find the filename and lineno or a nix value. */
-Pos findPackageFilename(EvalState & state, Value & v, std::string what);
+std::pair<std::string, uint32_t> findPackageFilename(EvalState & state, Value & v, std::string what);
std::vector<Symbol> parseAttrPath(EvalState & state, std::string_view s);
diff --git a/src/nix/edit.cc b/src/nix/edit.cc
index fc48db0d7..ffe79af89 100644
--- a/src/nix/edit.cc
+++ b/src/nix/edit.cc
@@ -30,17 +30,17 @@ struct CmdEdit : InstallableCommand
auto [v, pos] = installable->toValue(*state);
- try {
- pos = findPackageFilename(*state, *v, installable->what());
- } catch (NoPositionInfo &) {
- }
-
- if (pos == noPos)
- throw Error("cannot find position information for '%s", installable->what());
+ const auto [file, line] = [&] {
+ try {
+ return findPackageFilename(*state, *v, installable->what());
+ } catch (NoPositionInfo &) {
+ throw Error("cannot find position information for '%s", installable->what());
+ }
+ }();
stopProgressBar();
- auto args = editorFor(pos);
+ auto args = editorFor(file, line);
restoreProcessContext();
diff --git a/src/nix/repl.cc b/src/nix/repl.cc
index 0eb037858..391255ce9 100644
--- a/src/nix/repl.cc
+++ b/src/nix/repl.cc
@@ -461,21 +461,21 @@ bool NixRepl::processLine(std::string line)
Value v;
evalString(arg, v);
- Pos pos;
-
- if (v.type() == nPath || v.type() == nString) {
- PathSet context;
- auto filename = state->coerceToString(noPos, v, context);
- pos.file = state->symbols.create(*filename);
- } else if (v.isLambda()) {
- pos = v.lambda.fun->pos;
- } else {
- // assume it's a derivation
- pos = findPackageFilename(*state, v, arg);
- }
+ const auto [file, line] = [&] () -> std::pair<std::string, uint32_t> {
+ if (v.type() == nPath || v.type() == nString) {
+ PathSet context;
+ auto filename = state->coerceToString(noPos, v, context);
+ return {state->symbols.create(*filename), 0};
+ } else if (v.isLambda()) {
+ return {v.lambda.fun->pos.file, v.lambda.fun->pos.line};
+ } else {
+ // assume it's a derivation
+ return findPackageFilename(*state, v, arg);
+ }
+ }();
// Open in EDITOR
- auto args = editorFor(pos);
+ auto args = editorFor(file, line);
auto editor = args.front();
args.pop_front();