From e1ebf5900b403be83c5316ba61bc234e687f805d Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 24 Sep 2026 17:30:52 +0000 Subject: [PATCH] TerminalRenderer: restart the span cursor on any backward lookup The restart test compared against the previous span's start, so a lookup that moved back into an earlier span returned the default kind. Not reachable today (drawing only moves forward); found in review. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB --- TerminalRenderer.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/TerminalRenderer.cc b/TerminalRenderer.cc index e535833..23f9f9f 100644 --- a/TerminalRenderer.cc +++ b/TerminalRenderer.cc @@ -170,11 +170,13 @@ TerminalRenderer::Draw(Editor &ed) // Called left to right while drawing: walk the spans with a // cursor (rescanning from the first span for every byte was // quadratic on long highlighted lines). - std::size_t span_i = 0; + std::size_t span_i = 0; + int last_token_si = -1; auto token_at = [&](std::size_t src_index) -> kte::TokenKind { const int si = static_cast(src_index); - if (span_i > 0 && span_i <= sane_spans.size() && si < sane_spans[span_i - 1].col_start) - span_i = 0; // moved backwards: restart + if (si < last_token_si) + span_i = 0; // asked about an earlier byte: restart + last_token_si = si; while (span_i < sane_spans.size() && sane_spans[span_i].col_end <= si) ++span_i; if (span_i < sane_spans.size() && sane_spans[span_i].col_start <= si)