• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tstack / lnav / 17589970077-2502

09 Sep 2025 05:00PM UTC coverage: 65.196% (-5.0%) from 70.225%
17589970077-2502

push

github

tstack
[format] add fields for source file/line

Knowing the source file/line context in a log
message can help find log messages when using
log2src.

56 of 70 new or added lines in 2 files covered. (80.0%)

13954 existing lines in 210 files now uncovered.

45516 of 69814 relevant lines covered (65.2%)

404154.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.52
/src/plain_text_source.cc
1
/**
2
 * Copyright (c) 2022, Timothy Stack
3
 *
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 * * Neither the name of Timothy Stack nor the names of its contributors
15
 * may be used to endorse or promote products derived from this software
16
 * without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29

30
#include "plain_text_source.hh"
31

32
#include "base/itertools.hh"
33
#include "config.h"
34
#include "document.sections.hh"
35
#include "scn/scan.h"
36

37
static std::vector<plain_text_source::text_line>
UNCOV
38
to_text_line(const std::vector<attr_line_t>& lines)
×
39
{
UNCOV
40
    file_off_t off = 0;
×
41

42
    return lines | lnav::itertools::map([&off](const auto& elem) {
×
UNCOV
43
               auto retval = plain_text_source::text_line{
×
44
                   off,
45
                   elem,
46
               };
47

48
               off += elem.length() + 1;
×
49
               return retval;
×
UNCOV
50
           });
×
51
}
52

53
plain_text_source::plain_text_source(const string_fragment& text)
×
54
{
55
    size_t start = 0;
×
56

57
    for (const auto& line : text.split_lines()) {
×
58
        this->tds_lines.emplace_back(start, attr_line_t::from_ansi_frag(line));
×
59
        start += line.length();
×
60
    }
UNCOV
61
    this->tds_longest_line = this->compute_longest_line();
×
62
}
63

UNCOV
64
plain_text_source::plain_text_source(const std::vector<std::string>& text_lines)
×
65
{
UNCOV
66
    this->replace_with(text_lines);
×
67
}
68

69
plain_text_source::plain_text_source(const std::vector<attr_line_t>& text_lines)
×
UNCOV
70
    : tds_lines(to_text_line(text_lines))
×
71
{
UNCOV
72
    this->tds_longest_line = this->compute_longest_line();
×
73
}
74

75
plain_text_source&
76
plain_text_source::replace_with(const attr_line_t& text_lines)
42✔
77
{
78
    this->tds_lines.clear();
42✔
79
    this->tds_doc_sections = lnav::document::discover_metadata(text_lines);
42✔
80
    file_off_t off = 0;
42✔
81
    auto lines = text_lines.split_lines();
42✔
82
    while (!lines.empty() && lines.back().empty()) {
42✔
UNCOV
83
        lines.pop_back();
×
84
    }
85
    for (auto& line : lines) {
19,128✔
86
        auto line_len = line.al_string.size() + 1;
19,086✔
87
        this->tds_lines.emplace_back(off, std::move(line));
19,086✔
88
        off += line_len;
19,086✔
89
    }
90
    this->tds_longest_line = this->compute_longest_line();
42✔
91
    if (this->tss_view != nullptr) {
42✔
92
        this->tss_view->set_needs_update();
42✔
93
    }
94
    return *this;
42✔
95
}
42✔
96

97
plain_text_source&
98
plain_text_source::replace_with_mutable(attr_line_t& text_lines,
22✔
99
                                        text_format_t tf)
100
{
101
    this->tds_text_format = tf;
22✔
102
    this->tds_lines.clear();
22✔
103
    this->tds_doc_sections
104
        = lnav::document::discover(text_lines).with_text_format(tf).perform();
22✔
105
    file_off_t off = 0;
22✔
106
    auto lines = text_lines.split_lines();
22✔
107
    while (!lines.empty() && lines.back().empty()) {
22✔
UNCOV
108
        lines.pop_back();
×
109
    }
110
    for (auto& line : lines) {
4,129✔
111
        auto line_len = line.length() + 1;
4,107✔
112
        this->tds_lines.emplace_back(off, std::move(line));
4,107✔
113
        off += line_len;
4,107✔
114
    }
115
    this->tds_longest_line = this->compute_longest_line();
22✔
116
    if (this->tss_view != nullptr) {
22✔
117
        this->tss_view->set_needs_update();
4✔
118
    }
119
    return *this;
22✔
120
}
22✔
121

122
plain_text_source&
123
plain_text_source::replace_with(const std::vector<std::string>& text_lines)
×
124
{
UNCOV
125
    file_off_t off = 0;
×
UNCOV
126
    for (const auto& str : text_lines) {
×
UNCOV
127
        this->tds_lines.emplace_back(off, attr_line_t::from_ansi_str(str));
×
UNCOV
128
        off += str.length() + 1;
×
129
    }
UNCOV
130
    this->tds_longest_line = this->compute_longest_line();
×
131
    if (this->tss_view != nullptr) {
×
132
        this->tss_view->set_needs_update();
×
133
    }
134
    return *this;
×
135
}
136

137
plain_text_source&
138
plain_text_source::replace_with(const std::vector<attr_line_t>& text_lines)
×
139
{
140
    file_off_t off = 0;
×
UNCOV
141
    this->tds_lines.clear();
×
UNCOV
142
    for (const auto& al : text_lines) {
×
UNCOV
143
        this->tds_lines.emplace_back(off, al);
×
UNCOV
144
        off += al.length() + 1;
×
145
    }
UNCOV
146
    this->tds_longest_line = this->compute_longest_line();
×
UNCOV
147
    if (this->tss_view != nullptr) {
×
UNCOV
148
        this->tss_view->set_needs_update();
×
149
    }
UNCOV
150
    return *this;
×
151
}
152

153
void
154
plain_text_source::clear()
374✔
155
{
156
    this->tds_lines.clear();
374✔
157
    this->tds_longest_line = 0;
374✔
158
    this->tds_text_format = text_format_t::TF_UNKNOWN;
374✔
159
    if (this->tss_view != nullptr) {
374✔
160
        this->tss_view->set_needs_update();
345✔
161
    }
162
}
374✔
163

164
plain_text_source&
UNCOV
165
plain_text_source::truncate_to(size_t max_lines)
×
166
{
UNCOV
167
    while (this->tds_lines.size() > max_lines) {
×
UNCOV
168
        this->tds_lines.pop_back();
×
169
    }
UNCOV
170
    if (this->tss_view != nullptr) {
×
171
        this->tss_view->set_needs_update();
×
172
    }
173
    return *this;
×
174
}
175

176
size_t
177
plain_text_source::text_line_width(textview_curses& curses)
4,758✔
178
{
179
    return this->tds_longest_line;
4,758✔
180
}
181

182
line_info
183
plain_text_source::text_value_for_line(textview_curses& tc,
6,703✔
184
                                       int row,
185
                                       std::string& value_out,
186
                                       text_sub_source::line_flags_t flags)
187
{
188
    value_out = this->tds_lines[row].tl_value.get_string();
6,703✔
189
    this->tds_line_indent_size = 0;
6,703✔
190
    for (const auto& ch : value_out) {
20,192✔
191
        if (ch == ' ') {
19,065✔
192
            this->tds_line_indent_size += 1;
13,489✔
193
        } else if (ch == '\t') {
5,576✔
194
            do {
UNCOV
195
                this->tds_line_indent_size += 1;
×
UNCOV
196
            } while (this->tds_line_indent_size % 8);
×
197
        } else {
198
            break;
5,576✔
199
        }
200
    }
201

202
    return {};
6,703✔
203
}
204

205
void
206
plain_text_source::text_attrs_for_line(textview_curses& tc,
6,703✔
207
                                       int line,
208
                                       string_attrs_t& value_out)
209
{
210
    value_out = this->tds_lines[line].tl_value.get_attrs();
6,703✔
UNCOV
211
    if (this->tds_reverse_selection && tc.is_selectable()
×
212
        && tc.get_selection() == line)
6,703✔
213
    {
UNCOV
214
        value_out.emplace_back(line_range{0, -1},
×
UNCOV
215
                               VC_STYLE.value(text_attrs::with_reverse()));
×
216
    }
217
    for (const auto& indent : this->tds_doc_sections.m_indents) {
7,919✔
218
        if (indent < this->tds_line_indent_size) {
1,216✔
219
            auto guide_lr = line_range{
220
                (int) indent,
336✔
221
                (int) (indent + 1),
336✔
222
                line_range::unit::codepoint,
223
            };
336✔
224
            value_out.emplace_back(guide_lr,
336✔
225
                                   VC_BLOCK_ELEM.value(block_elem_t{
672✔
226
                                       L'\u258f', role_t::VCR_INDENT_GUIDE}));
227
        }
228
    }
229
}
6,703✔
230

231
size_t
UNCOV
232
plain_text_source::text_size_for_line(textview_curses& tc,
×
233
                                      int row,
234
                                      text_sub_source::line_flags_t flags)
235
{
236
    return this->tds_lines[row].tl_value.length();
×
237
}
238

239
text_format_t
240
plain_text_source::get_text_format() const
4,921✔
241
{
242
    return this->tds_text_format;
4,921✔
243
}
244

245
size_t
246
plain_text_source::compute_longest_line()
64✔
247
{
248
    size_t retval = 0;
64✔
249
    for (auto& iter : this->tds_lines) {
23,257✔
250
        retval = std::max(retval, (size_t) iter.tl_value.length());
23,193✔
251
    }
252
    return retval;
64✔
253
}
254

255
std::optional<vis_line_t>
256
plain_text_source::line_for_offset(file_off_t off) const
6✔
257
{
258
    struct cmper {
259
        bool operator()(const file_off_t& lhs, const text_line& rhs) const
260
        {
261
            return lhs < rhs.tl_offset;
262
        }
263

264
        bool operator()(const text_line& lhs, const file_off_t& rhs) const
57✔
265
        {
266
            return lhs.tl_offset < rhs;
57✔
267
        }
268
    };
269

270
    if (this->tds_lines.empty()) {
6✔
UNCOV
271
        return std::nullopt;
×
272
    }
273

274
    log_trace("line_for_offset(%d)", off);
6✔
275
    auto iter = std::lower_bound(
6✔
276
        this->tds_lines.begin(), this->tds_lines.end(), off, cmper{});
277
    if (iter == this->tds_lines.end()) {
6✔
UNCOV
278
        if (this->tds_lines.back().contains_offset(off)) {
×
UNCOV
279
            return std::make_optional(
×
UNCOV
280
                vis_line_t(std::distance(this->tds_lines.end() - 1, iter)));
×
281
        }
282
        return std::nullopt;
×
283
    }
284

285
    if (!iter->contains_offset(off) && iter != this->tds_lines.begin()) {
6✔
286
        log_trace("  lower_bound (%lld) does not contain offset",
2✔
287
                  iter->tl_offset);
288
        --iter;
2✔
289
    }
290

291
    auto retval = vis_line_t(std::distance(this->tds_lines.begin(), iter));
12✔
292
    log_trace("  retval=%d", retval);
6✔
293
    return retval;
6✔
294
}
295

296
void
297
plain_text_source::text_crumbs_for_line(int line,
2✔
298
                                        std::vector<breadcrumb::crumb>& crumbs)
299
{
300
    const auto initial_size = crumbs.size();
2✔
301
    const auto& tl = this->tds_lines[line];
2✔
302

303
    this->tds_doc_sections.m_sections_tree.visit_overlapping(
4✔
304
        tl.tl_offset,
2✔
305
        tl.tl_offset + tl.tl_value.length(),
2✔
306
        [&crumbs, initial_size, meta = &this->tds_doc_sections, this](
2✔
307
            const auto& iv) {
308
            auto path = crumbs | lnav::itertools::skip(initial_size)
12✔
309
                | lnav::itertools::map(&breadcrumb::crumb::c_key)
8✔
310
                | lnav::itertools::append(iv.value);
4✔
311
            crumbs.emplace_back(
8✔
312
                iv.value,
4✔
313
                [meta, path]() { return meta->possibility_provider(path); },
12✔
314
                [this, meta, path](const auto& key) {
8✔
UNCOV
315
                    auto curr_node = lnav::document::hier_node::lookup_path(
×
UNCOV
316
                        meta->m_sections_root.get(), path);
×
317
                    if (!curr_node) {
×
UNCOV
318
                        return;
×
319
                    }
UNCOV
320
                    auto* parent_node = curr_node.value()->hn_parent;
×
321

322
                    if (parent_node == nullptr) {
×
UNCOV
323
                        return;
×
324
                    }
UNCOV
325
                    key.match(
×
UNCOV
326
                        [this, parent_node](const std::string& str) {
×
327
                            auto sib_iter
UNCOV
328
                                = parent_node->hn_named_children.find(str);
×
UNCOV
329
                            if (sib_iter
×
330
                                == parent_node->hn_named_children.end()) {
×
UNCOV
331
                                return;
×
332
                            }
UNCOV
333
                            this->line_for_offset(sib_iter->second->hn_start) |
×
UNCOV
334
                                [this](const auto new_top) {
×
UNCOV
335
                                    this->tss_view->set_selection(new_top);
×
336
                                    if (this->tss_view->is_selectable()) {
×
UNCOV
337
                                        this->tss_view->set_top(new_top - 2_vl,
×
338
                                                                false);
339
                                    }
340
                                };
341
                        },
342
                        [this, parent_node](size_t index) {
×
343
                            if (index >= parent_node->hn_children.size()) {
×
UNCOV
344
                                return;
×
345
                            }
346
                            auto sib = parent_node->hn_children[index].get();
×
347
                            this->line_for_offset(sib->hn_start) |
×
348
                                [this](const auto new_top) {
×
349
                                    this->tss_view->set_selection(new_top);
×
350
                                    if (this->tss_view->is_selectable()) {
×
UNCOV
351
                                        this->tss_view->set_top(new_top - 2_vl,
×
352
                                                                false);
353
                                    }
354
                                };
355
                        });
356
                });
357
        });
4✔
358

359
    auto path = crumbs | lnav::itertools::skip(initial_size)
4✔
360
        | lnav::itertools::map(&breadcrumb::crumb::c_key);
4✔
361
    auto node = lnav::document::hier_node::lookup_path(
2✔
362
        this->tds_doc_sections.m_sections_root.get(), path);
2✔
363

364
    if (node && !node.value()->hn_children.empty()) {
2✔
365
        auto poss_provider = [curr_node = node.value()]() {
1✔
366
            std::vector<breadcrumb::possibility> retval;
1✔
367
            for (const auto& child : curr_node->hn_named_children) {
2✔
368
                retval.emplace_back(child.first);
1✔
369
            }
370
            return retval;
1✔
371
        };
372
        auto path_performer = [this, curr_node = node.value()](
2✔
373
                                  const breadcrumb::crumb::key_t& value) {
UNCOV
374
            value.match(
×
UNCOV
375
                [this, curr_node](const std::string& str) {
×
UNCOV
376
                    auto child_iter = curr_node->hn_named_children.find(str);
×
UNCOV
377
                    if (child_iter != curr_node->hn_named_children.end()) {
×
UNCOV
378
                        this->line_for_offset(child_iter->second->hn_start) |
×
UNCOV
379
                            [this](const auto new_top) {
×
UNCOV
380
                                this->tss_view->set_selection(new_top);
×
381
                            };
382
                    }
383
                },
×
384
                [this, curr_node](size_t index) {
×
385
                    auto* child = curr_node->hn_children[index].get();
×
386
                    this->line_for_offset(child->hn_start) |
×
387
                        [this](const auto new_top) {
×
UNCOV
388
                            this->tss_view->set_selection(new_top);
×
389
                        };
UNCOV
390
                });
×
391
        };
1✔
392
        crumbs.emplace_back(
1✔
393
            "", "\u22ef", std::move(poss_provider), std::move(path_performer));
1✔
394
        crumbs.back().c_expected_input = node.value()->hn_named_children.empty()
2✔
395
            ? breadcrumb::crumb::expected_input_t::index
1✔
396
            : breadcrumb::crumb::expected_input_t::index_or_exact;
397
    }
398
}
2✔
399

400
std::optional<vis_line_t>
401
plain_text_source::row_for_anchor(const std::string& id)
4✔
402
{
403
    std::optional<vis_line_t> retval;
4✔
404

405
    if (this->tds_doc_sections.m_sections_root == nullptr) {
4✔
UNCOV
406
        return retval;
×
407
    }
408

409
    const auto& meta = this->tds_doc_sections;
4✔
410

411
    auto is_ptr = startswith(id, "#/");
4✔
412
    if (is_ptr) {
4✔
413
        auto hier_sf = string_fragment::from_str(id).consume_n(2).value();
×
UNCOV
414
        std::vector<lnav::document::section_key_t> path;
×
415

416
        while (!hier_sf.empty()) {
×
UNCOV
417
            auto comp_pair = hier_sf.split_when(string_fragment::tag1{'/'});
×
418
            auto scan_res
419
                = scn::scan_value<int64_t>(comp_pair.first.to_string_view());
×
420
            if (scan_res && scan_res->range().empty()) {
×
UNCOV
421
                path.emplace_back(scan_res->value());
×
422
            } else {
UNCOV
423
                stack_buf allocator;
×
424
                path.emplace_back(
×
UNCOV
425
                    json_ptr::decode(comp_pair.first, allocator).to_string());
×
426
            }
427
            hier_sf = comp_pair.second;
×
428
        }
429

430
        auto lookup_res = lnav::document::hier_node::lookup_path(
×
UNCOV
431
            meta.m_sections_root.get(), path);
×
UNCOV
432
        if (lookup_res) {
×
433
            retval = this->line_for_offset(lookup_res.value()->hn_start);
×
434
        }
435

UNCOV
436
        return retval;
×
437
    }
438

439
    lnav::document::hier_node::depth_first(
4✔
440
        meta.m_sections_root.get(),
441
        [this, &id, &retval](const lnav::document::hier_node* node) {
59✔
442
            for (const auto& child_pair : node->hn_named_children) {
99✔
443
                const auto& child_anchor = to_anchor_string(child_pair.first);
44✔
444

445
                if (child_anchor != id) {
44✔
446
                    continue;
40✔
447
                }
448

449
                retval = this->line_for_offset(child_pair.second->hn_start);
4✔
450
                break;
4✔
451
            }
44✔
452
        });
59✔
453

454
    return retval;
4✔
455
}
456

457
std::unordered_set<std::string>
UNCOV
458
plain_text_source::get_anchors()
×
459
{
UNCOV
460
    std::unordered_set<std::string> retval;
×
461

462
    lnav::document::hier_node::depth_first(
×
463
        this->tds_doc_sections.m_sections_root.get(),
UNCOV
464
        [&retval](const lnav::document::hier_node* node) {
×
465
            for (const auto& child_pair : node->hn_named_children) {
×
UNCOV
466
                retval.emplace(to_anchor_string(child_pair.first));
×
467
            }
468
        });
×
469

UNCOV
470
    return retval;
×
UNCOV
471
}
×
472

473
std::optional<std::string>
474
plain_text_source::anchor_for_row(vis_line_t vl)
4✔
475
{
476
    std::optional<std::string> retval;
4✔
477

478
    if (vl > (ssize_t) this->tds_lines.size()
4✔
479
        || this->tds_doc_sections.m_sections_root == nullptr)
4✔
480
    {
UNCOV
481
        return retval;
×
482
    }
483

484
    const auto& tl = this->tds_lines[vl];
4✔
485
    auto& md = this->tds_doc_sections;
4✔
486
    auto path_for_line = md.path_for_range(
487
        tl.tl_offset, tl.tl_offset + tl.tl_value.al_string.length());
4✔
488

489
    if (path_for_line.empty()) {
4✔
490
        return std::nullopt;
1✔
491
    }
492

493
    if ((path_for_line.size() == 1
3✔
494
         || this->tds_text_format == text_format_t::TF_MARKDOWN)
2✔
495
        && path_for_line.back().is<std::string>())
5✔
496
    {
497
        return to_anchor_string(path_for_line.back().get<std::string>());
2✔
498
    }
499

500
    auto comps
501
        = path_for_line | lnav::itertools::map([](const auto& elem) {
2✔
502
              return elem.match(
503
                  [](const std::string& str) {
×
504
                      stack_buf allocator;
1✔
505
                      return json_ptr::encode(str, allocator).to_string();
2✔
506
                  },
1✔
507
                  [](size_t index) { return fmt::to_string(index); });
5✔
508
          });
1✔
509

510
    return fmt::format(FMT_STRING("#/{}"),
4✔
511
                       fmt::join(comps.begin(), comps.end(), "/"));
2✔
512
}
4✔
513

514
std::optional<vis_line_t>
515
plain_text_source::adjacent_anchor(vis_line_t vl, direction dir)
2✔
516
{
517
    if (vl > (ssize_t) this->tds_lines.size()
2✔
518
        || this->tds_doc_sections.m_sections_root == nullptr)
2✔
519
    {
UNCOV
520
        return std::nullopt;
×
521
    }
522

523
    const auto& tl = this->tds_lines[vl];
2✔
524
    auto path_for_line = this->tds_doc_sections.path_for_range(
525
        tl.tl_offset, tl.tl_offset + tl.tl_value.al_string.length());
2✔
526

527
    log_trace("adjacent_anchor: curr path = %s",
2✔
528
              fmt::format(FMT_STRING("{}"), path_for_line).c_str());
529
    const auto& md = this->tds_doc_sections;
2✔
530
    if (path_for_line.empty()) {
2✔
531
        auto neighbors_res = md.m_sections_root->line_neighbors(vl);
1✔
532
        if (!neighbors_res) {
1✔
533
            return std::nullopt;
×
534
        }
535

536
        switch (dir) {
1✔
537
            case direction::prev: {
×
UNCOV
538
                if (neighbors_res->cnr_previous) {
×
539
                    return this->line_for_offset(
×
540
                        neighbors_res->cnr_previous.value()->hn_start);
×
541
                }
542
                break;
×
543
            }
544
            case direction::next: {
1✔
545
                if (neighbors_res->cnr_next) {
1✔
UNCOV
546
                    return this->line_for_offset(
×
547
                        neighbors_res->cnr_next.value()->hn_start);
×
548
                }
549
                if (!md.m_sections_root->hn_children.empty()) {
1✔
550
                    return this->line_for_offset(
1✔
551
                        md.m_sections_root->hn_children[0]->hn_start);
2✔
552
                }
UNCOV
553
                break;
×
554
            }
555
        }
UNCOV
556
        return std::nullopt;
×
557
    }
558

559
    auto last_key = path_for_line.back();
1✔
560
    path_for_line.pop_back();
1✔
561

562
    auto parent_opt = lnav::document::hier_node::lookup_path(
1✔
563
        md.m_sections_root.get(), path_for_line);
1✔
564
    if (!parent_opt) {
1✔
UNCOV
565
        log_trace("  no parent");
×
566
        return std::nullopt;
×
567
    }
568
    const auto* parent = parent_opt.value();
1✔
569

570
    auto child_hn = parent->lookup_child(last_key);
1✔
571
    if (!child_hn) {
1✔
572
        // XXX "should not happen"
UNCOV
573
        return std::nullopt;
×
574
    }
575

576
    auto neighbors_res = parent->child_neighbors(
3✔
577
        child_hn.value(), tl.tl_offset + tl.tl_value.al_string.length() + 1);
1✔
578
    if (!neighbors_res) {
1✔
UNCOV
579
        log_trace("no neighbors");
×
UNCOV
580
        return std::nullopt;
×
581
    }
582

583
    if (neighbors_res->cnr_previous && last_key.is<std::string>()) {
1✔
584
        auto neighbor_sub
585
            = neighbors_res->cnr_previous.value()->lookup_child(last_key);
1✔
586
        if (neighbor_sub) {
1✔
587
            log_trace("  loading previous child");
×
UNCOV
588
            neighbors_res->cnr_previous = neighbor_sub;
×
589
        }
590
    }
591

592
    if (neighbors_res->cnr_next && last_key.is<std::string>()) {
1✔
593
        auto neighbor_sub
594
            = neighbors_res->cnr_next.value()->lookup_child(last_key);
1✔
595
        if (neighbor_sub) {
1✔
UNCOV
596
            log_trace("  loading next child");
×
597
            neighbors_res->cnr_next = neighbor_sub;
×
598
        }
599
    }
600

601
    switch (dir) {
1✔
UNCOV
602
        case direction::prev: {
×
UNCOV
603
            if (neighbors_res->cnr_previous) {
×
604
                return this->line_for_offset(
×
UNCOV
605
                    neighbors_res->cnr_previous.value()->hn_start);
×
606
            }
UNCOV
607
            break;
×
608
        }
609
        case direction::next: {
1✔
610
            if (neighbors_res->cnr_next) {
1✔
611
                log_trace("  next offset %d",
1✔
612
                          neighbors_res->cnr_next.value()->hn_start);
613
                return this->line_for_offset(
1✔
614
                    neighbors_res->cnr_next.value()->hn_start);
1✔
615
            }
UNCOV
616
            break;
×
617
        }
618
    }
619

UNCOV
620
    return std::nullopt;
×
621
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc