mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 07:22:43 +01:00
The short answer for why we need to do this is so we can consistently do `#include "nix/..."`. Without this change, there are ways to still make that work, but they are hacky, and they have downsides such as making it harder to make sure headers from the wrong Nix library (e..g. `libnixexpr` headers in `libnixutil`) aren't being used. The C API alraedy used `nix_api_*`, so its headers are *not* put in subdirectories accordingly. Progress on #7876 We resisted doing this for a while because it would be annoying to not have the header source file pairs close by / easy to change file path/name from one to the other. But I am ameliorating that with symlinks in the next commit.
37 lines
1 KiB
C++
37 lines
1 KiB
C++
#include "nix/pos-table.hh"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace nix {
|
|
|
|
/* Position table. */
|
|
|
|
Pos PosTable::operator[](PosIdx p) const
|
|
{
|
|
auto origin = resolve(p);
|
|
if (!origin)
|
|
return {};
|
|
|
|
const auto offset = origin->offsetOf(p);
|
|
|
|
Pos result{0, 0, origin->origin};
|
|
auto lines = this->lines.lock();
|
|
auto linesForInput = (*lines)[origin->offset];
|
|
|
|
if (linesForInput.empty()) {
|
|
auto source = result.getSource().value_or("");
|
|
const char * begin = source.data();
|
|
for (Pos::LinesIterator it(source), end; it != end; it++)
|
|
linesForInput.push_back(it->data() - begin);
|
|
if (linesForInput.empty())
|
|
linesForInput.push_back(0);
|
|
}
|
|
// as above: the first line starts at byte 0 and is always present
|
|
auto lineStartOffset = std::prev(std::upper_bound(linesForInput.begin(), linesForInput.end(), offset));
|
|
|
|
result.line = 1 + (lineStartOffset - linesForInput.begin());
|
|
result.column = 1 + (offset - *lineStartOffset);
|
|
return result;
|
|
}
|
|
|
|
}
|