Skip to content

Editor search: mark the current match on any line (#1177) - #1182

Merged
buggins merged 1 commit into
masterfrom
current-match-1177
Nov 24, 2026
Merged

buggins merged 1 commit into
masterfrom
current-match-1177

Conversation

@buggins

@buggins buggins commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Fixes #1177.

The current search match (the one Find next or Replace takes) was marked only when it was on the caret line. Find next crosses lines and wraps around, so e.g. with the caret after the last match of its line, nothing was marked. The Find panel's highlight now marks the current match wherever it is: the selected match, else the first match after the caret (before it when searching back), wrapping around.

How it is found

  • EditBox.findCurrentMatch() searches outward from the caret: the selected match's line, the caret line, then the following lines in the search direction, wrapping around. It gives up after 1000 lines or 100k characters.
  • The result is cached per text revision, pattern, options, scope, selection, caret and direction, so a repaint (caret blink) doesn't search again.
  • When it gives up (no match near the caret), all matches are collected once the text has been unchanged for 300 ms. Caret moves then pick from them. Until then, and while the text keeps changing faster than every 300 ms, a match that far away isn't marked. That's intended: typing doesn't search the whole text.
  • Cost, measured with forEachLineMatch on 100k lines of code-like text: a full pass takes 11–63 ms in a release build (44–91 ms debug), depending on the pattern and case sensitivity. So the bounded search costs about 0.1–0.6 ms per caret move, and a full pass happens at most once per pause in typing, only when matches are sparse near the caret. Keeping the match list up to date on edits would avoid even that, but isn't done here.

Other highlights

  • The two-argument setTextToHighlight() (used by DlangIDE's search results and its editor highlight) keeps the old behavior: a current match only on the caret line, or the selected one. It now goes through the four-argument version again, so overrides see every call.

API changes (unreleased)

Testing

  • dub test (Linux and Windows) and dub test -c console: pass, no leak lines. D-Scanner: clean.
  • Unittests compare findCurrentMatch() with currentMatchIndex() over all matches, for every caret position, selection, direction and scope on several multi-line texts, also with a small search limit. Further tests cover giving up at the line and character limits, and hand-written cases for the caret-line-only mode. Each fails with a deliberately broken version (no wrap-around, no give-up, r.end >= caret).
  • dmledit console (tmux, temporary hooks not committed), each with a normal exit and no leak lines:
    • Caret at the end of line 2: line 9's id: is marked current (master: nothing).
    • Caret at the start of line 9, then Down: the mark moves from line 9 to line 14.
    • 3000 lines with matches only on lines 1487–1490, caret at line 1491: after the 300 ms collection, the mark appears on line 1487 (wrapped around).
  • Not checked in a Win32 GUI (drawing code is shared).

🤖 Generated with Claude Code

The current search match, the one Find next or Replace takes, was marked
only when it was on the caret line. Find next crosses lines and wraps
around, so with the caret after the last match of its line, no match was
marked. The Find panel's highlight now marks it wherever it is: the selected
match, else the first match after the caret (before it when searching back),
wrapping around.

It is found by searching outward from the caret
(EditBox.findCurrentMatch()), at most 1000 lines or 100k characters, and is
cached per text revision, pattern, options, scope, selection, caret and
direction, so a repaint doesn't search again. When no match is within
those limits, all matches are collected once the text has been unchanged
for 300 ms; caret moves then pick from them.

Highlights set with the two-argument setTextToHighlight() (DlangIDE's
search results, for example) keep the old behavior: a current match only on
the caret line, or the selected one.

currentMatchIndex() wraps around now, like Find next. The scoped
forEachLineMatch() overload takes the current match instead of the
selection, caret and direction. Both came with #1145 and are not released.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@buggins buggins self-assigned this Nov 24, 2026
@buggins
buggins marked this pull request as ready for review November 24, 2026 14:33
@buggins
buggins merged commit 9c4bb3a into master Nov 24, 2026
13 checks passed
@buggins
buggins deleted the current-match-1177 branch November 24, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Editor search: the current match is shown only on the caret line

1 participant