Highlight search matches in the text view
- ui: TextField gains MatchRanges (window-relative byte ranges) + CurrentMatch; drawWrappedText draws a per-glyph highlight pass (same tiling as the selection, so wrapped lines are covered): all matches in translucent yellow, the selection in blue as before, and the current match in stronger orange on top so it stands out. - editor: EditorLayout windows the absolute FindState matches into the visible content (binary search on the sorted ranges, clamp to the window) and stamps them on the editor TextField. - e2e: poll the captured frames until the highlight data lands; assert all matches are windowed, each highlights a "needle", and CurrentMatch follows FindNext.
This commit is contained in:
@@ -18,6 +18,33 @@ const findContent = "needle at start\n" +
|
||||
"another filler line\n" +
|
||||
"needle at the end"
|
||||
|
||||
// editorField returns the editor's TextField from the latest frame (the
|
||||
// frame element whose value is the file's visible content), if present.
|
||||
func editorField(h *e2e.Harness) (ui.TextField, bool) {
|
||||
for _, el := range e2e.GetLastFrame(h) {
|
||||
if tf, ok := el.(ui.TextField); ok && strings.Contains(tf.Value, "needle") {
|
||||
return tf, true
|
||||
}
|
||||
}
|
||||
return ui.TextField{}, false
|
||||
}
|
||||
|
||||
// waitForEditorField polls the captured frames (frame capture lags the
|
||||
// logic-owner state) until the editor TextField satisfies want, and returns
|
||||
// it.
|
||||
func waitForEditorField(t *testing.T, h *e2e.Harness, want func(ui.TextField) bool) ui.TextField {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if tf, ok := editorField(h); ok && want(tf) {
|
||||
return tf
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("timed out waiting for an editor frame satisfying the predicate")
|
||||
return ui.TextField{}
|
||||
}
|
||||
|
||||
// frameHasFindBar reports whether the latest frame carries the find bar
|
||||
// (the "find_bar" GioEditor inside the find-bar container).
|
||||
func frameHasFindBar(h *e2e.Harness) bool {
|
||||
@@ -102,6 +129,17 @@ func TestRealFile_FindBarAndNavigation(t *testing.T) {
|
||||
t.Fatalf("test content drift: %q", got)
|
||||
}
|
||||
|
||||
// The in-text highlight data rides the frame: all three matches are
|
||||
// windowed into the visible content, the first flagged as current.
|
||||
tf := waitForEditorField(t, h, func(tf ui.TextField) bool {
|
||||
return len(tf.MatchRanges) == 3 && tf.CurrentMatch == 0
|
||||
})
|
||||
for i, m := range tf.MatchRanges {
|
||||
if !strings.EqualFold(tf.Value[m[0]:m[1]], "needle") {
|
||||
t.Fatalf("match %d highlights %q, want a needle", i, tf.Value[m[0]:m[1]])
|
||||
}
|
||||
}
|
||||
|
||||
// Next -> the second match (byte 38); the selection text must be a
|
||||
// match, whatever the offset.
|
||||
h.SendInput([]ui.InputEvent{{Handler: editor.FindNext, Data: ui.Point{}}})
|
||||
@@ -111,6 +149,10 @@ func TestRealFile_FindBarAndNavigation(t *testing.T) {
|
||||
if want := strings.Index(findContent[6:], "needle") + 6; sel.([2]int) != [2]int{want, want + 6} {
|
||||
t.Fatalf("selection %v, want [%d,%d)", sel, want, want+6)
|
||||
}
|
||||
// The current-match highlight follows the navigation.
|
||||
waitForEditorField(t, h, func(tf ui.TextField) bool {
|
||||
return len(tf.MatchRanges) == 3 && tf.CurrentMatch == 1
|
||||
})
|
||||
|
||||
// Next -> the third; next again wraps to the FIRST.
|
||||
h.SendInput([]ui.InputEvent{{Handler: editor.FindNext, Data: ui.Point{}}})
|
||||
|
||||
Reference in New Issue
Block a user