Add in-file search (find bar)
- pool: SearchTask + FindSubstring (case-insensitive substring scan with query generation for stale-result dropping) - editor: FindState on EditorState; find bar handlers (ToggleFind/FindNext/ FindPrev/FindClose); worker-pool scan of the in-memory content; edit-aware offset remapping (shift after, drop overlapping); next/prev with wrap-around, selection + scroll to match; main-owned find_bar input forwarded via FindQueryChan (browser search_bar precedent) - ui: find-bar layout below the margin-free top bar; search icon on the top bar right; new search/close/chevron_up/chevron_down icons - logic: findQueryChan, TypeSearch result handling, findReset on open - frame: FindQuery field (main syncs the widget text like Query) - tests: pool scan tests, find-state unit tests (remap, nav, gen gate, focus), e2e find bar + navigation + edit-survival on real files - docs: spec/architecture/development_plan updated (search implemented, channel/task tables)
This commit is contained in:
@@ -28,6 +28,11 @@ type Frame struct {
|
||||
FontScale float32 // user font-size setting the logic bookkeeping used
|
||||
FocusedElementID string
|
||||
Query string
|
||||
// FindQuery: the in-file search query the logic goroutine has processed
|
||||
// (mirrors EditorState.Find.Query). Main compares it against the
|
||||
// "find_bar" widget's text and forwards changes via FindQueryChan — the
|
||||
// same contract as Query/searchQueryChan.
|
||||
FindQuery string
|
||||
// WindowStartByte / WindowStartLine / EditSeq: the editor window this
|
||||
// frame's elements describe. The main goroutine forwards them with the
|
||||
// shaped glyph layout (LayoutFeedback) so the logic goroutine can apply
|
||||
@@ -48,6 +53,7 @@ func (l *Logic) frameOf(elems []ui.Element) Frame {
|
||||
FontScale: l.state.fontScale,
|
||||
FocusedElementID: l.state.FocusedElementID,
|
||||
Query: l.state.Browser.Query,
|
||||
FindQuery: l.state.Editor.Find.Query,
|
||||
WindowStartByte: l.state.Editor.IMEWindowStartByte,
|
||||
WindowStartLine: l.state.WindowStartLine,
|
||||
WindowText: l.state.Editor.IMEWindowText,
|
||||
|
||||
@@ -69,6 +69,7 @@ type Logic struct {
|
||||
layoutChan chan ui.LayoutFeedback
|
||||
resultChan chan ResultEvent
|
||||
searchQueryChan chan string
|
||||
findQueryChan chan string // in-file search text (main-owned "find_bar")
|
||||
openFileChan chan string
|
||||
retryChan chan string // auto-save retries
|
||||
autosaveChan chan struct{} // auto-save debounce ticks (timer -> owner)
|
||||
@@ -127,6 +128,7 @@ func NewLogic(mfs pool.FileSystem, path string, openfunc func(string)) *Logic {
|
||||
layoutChan: make(chan ui.LayoutFeedback),
|
||||
resultChan: make(chan ResultEvent),
|
||||
searchQueryChan: make(chan string),
|
||||
findQueryChan: make(chan string),
|
||||
openFileChan: make(chan string),
|
||||
retryChan: make(chan string, 1), // Buffered channel
|
||||
autosaveChan: make(chan struct{}),
|
||||
@@ -173,6 +175,13 @@ func (l *Logic) SearchQueryChan() chan<- string {
|
||||
return l.searchQueryChan
|
||||
}
|
||||
|
||||
// FindQueryChan returns the in-file search query channel. The main goroutine
|
||||
// forwards the "find_bar" widget's text here when it changes (see
|
||||
// search.go).
|
||||
func (l *Logic) FindQueryChan() chan<- string {
|
||||
return l.findQueryChan
|
||||
}
|
||||
|
||||
// ClipboardSetChan returns the channel the logic goroutine uses to request a
|
||||
// system clipboard write; the main goroutine executes the Gio clipboard op.
|
||||
func (l *Logic) ClipboardSetChan() <-chan string {
|
||||
@@ -247,7 +256,13 @@ func (l *Logic) Run() {
|
||||
}
|
||||
}
|
||||
l.emitFrame()
|
||||
case q := <-l.findQueryChan:
|
||||
l.state.Editor.findSetQuery(q)
|
||||
l.emitFrame()
|
||||
case path := <-l.openFileChan:
|
||||
// Discard find results for the previous file; the query is kept
|
||||
// (see EditorState.findReset) and re-scanned against the new file.
|
||||
TheState.Editor.findReset()
|
||||
// Create chunked buffer for virtual scrolling
|
||||
chunkSize := DefaultChunkSize
|
||||
cb := NewChunkedBuffer(path, chunkSize, l.mockFS, "")
|
||||
@@ -579,11 +594,14 @@ func (l *Logic) handleWorkerResult(res pool.Result) {
|
||||
}
|
||||
})
|
||||
}
|
||||
} else if res.TaskType == pool.TypeSearch {
|
||||
if res.Success {
|
||||
l.state.Editor.applySearchResult(res)
|
||||
}
|
||||
}
|
||||
l.emitFrame()
|
||||
}
|
||||
|
||||
|
||||
// State returns the current state.
|
||||
func (l *Logic) State() *State {
|
||||
return l.state
|
||||
|
||||
@@ -0,0 +1,288 @@
|
||||
package editor
|
||||
|
||||
// In-file search (find bar).
|
||||
//
|
||||
// Model:
|
||||
// - FindState is logic-owned (EditorState.Find); only the logic goroutine
|
||||
// mutates it.
|
||||
// - The find bar's text field is a MAIN-owned widget.Editor registered as
|
||||
// "find_bar" (the browser search bar "search_bar" is the precedent,
|
||||
// architecture.md §1): its text reaches the logic only through
|
||||
// Logic.findQueryChan. The find icon, counter, and prev/next/close
|
||||
// buttons are plain elements whose taps run the handlers below on the
|
||||
// logic goroutine.
|
||||
// - A query is scanned by a pool SearchTask over a snapshot of the
|
||||
// FULL in-memory content (in-range files are fully loaded, so the
|
||||
// snapshot is exact and unsaved edits are searchable). Results carry a
|
||||
// generation; only the latest generation is applied, so typing fast
|
||||
// simply supersedes older scans.
|
||||
// - Edits do NOT invalidate search: matches outside the edited range are
|
||||
// shifted by the length delta, matches overlapping it are dropped
|
||||
// (findEdit). The count is therefore "as of the last scan, adjusted for
|
||||
// edits" — re-typing the query re-scans.
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"pad/internal/io/pool"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// FindState holds the in-file search state for the open file.
|
||||
type FindState struct {
|
||||
Visible bool // the find bar is shown
|
||||
Query string // the query the logic last processed
|
||||
Matches [][2]int // ascending, non-overlapping byte ranges
|
||||
Cur int // index into Matches; -1 when none is selected
|
||||
Gen uint64 // generation of the newest dispatched scan
|
||||
Scanning bool // a scan for Gen is in flight
|
||||
}
|
||||
|
||||
// ToggleFind opens the find bar (or closes it when open). It is the tap
|
||||
// handler of the find icon in the editor top bar.
|
||||
func ToggleFind(data any) {
|
||||
e := &TheState.Editor
|
||||
if e.Find.Visible {
|
||||
e.findClose()
|
||||
} else {
|
||||
e.findShow()
|
||||
}
|
||||
}
|
||||
|
||||
// FindNext / FindPrev move to the next/previous match, wrap around, select
|
||||
// it, and scroll it into view. They are the tap handlers of the find bar's
|
||||
// chevron buttons.
|
||||
func FindNext(data any) { findStep(1) }
|
||||
|
||||
func FindPrev(data any) { findStep(-1) }
|
||||
|
||||
// FindClose is the tap handler of the find bar's close button.
|
||||
func FindClose(data any) { TheState.Editor.findClose() }
|
||||
|
||||
// findShow opens the find bar. The text input is main-owned, so logic takes
|
||||
// key focus off the editor (FocusedElementID) and, if a previous query has
|
||||
// no live matches (they are cleared on close and on file open), re-scans it.
|
||||
func (e *EditorState) findShow() {
|
||||
e.Find.Visible = true
|
||||
if e.Filename != "" {
|
||||
TheState.FocusedElementID = "find_bar"
|
||||
}
|
||||
if e.Find.Query != "" && len(e.Find.Matches) == 0 && !e.Find.Scanning {
|
||||
e.findDispatchScan()
|
||||
}
|
||||
}
|
||||
|
||||
// findClose hides the find bar. The query is kept so re-opening shows it
|
||||
// (the main-owned widget keeps its text); matches are dropped — the next
|
||||
// show re-scans, so they are never stale.
|
||||
func (e *EditorState) findClose() {
|
||||
e.Find.Visible = false
|
||||
e.Find.Matches = nil
|
||||
e.Find.Cur = -1
|
||||
e.Find.Scanning = false
|
||||
if TheState.FocusedElementID == "find_bar" {
|
||||
TheState.FocusedElementID = "editor_text"
|
||||
}
|
||||
}
|
||||
|
||||
// findReset discards all find state for the open file (file open, page
|
||||
// change). The query is kept: the main-owned widget still shows it, and
|
||||
// keeping it lets the next show() re-scan the NEW file with the old query
|
||||
// (and a frame with a changed FindQuery re-sends it, re-scanning live).
|
||||
func (e *EditorState) findReset() {
|
||||
// Monotonic generation bump (do not zero the counter): a stale
|
||||
// in-flight scan carrying a HIGH generation must never equal a fresh
|
||||
// one, or its result would pass the gen gate in applySearchResult.
|
||||
e.Find = FindState{Query: e.Find.Query, Gen: e.Find.Gen + 1}
|
||||
}
|
||||
|
||||
// findSetQuery processes a query forwarded from the main-owned "find_bar"
|
||||
// widget. An empty query clears the results; otherwise a fresh scan of the
|
||||
// full in-memory content is dispatched on the worker pool.
|
||||
func (e *EditorState) findSetQuery(q string) {
|
||||
f := &e.Find
|
||||
f.Query = q
|
||||
f.Matches = nil
|
||||
f.Cur = -1
|
||||
if q == "" || e.TooLarge {
|
||||
f.Scanning = false
|
||||
return
|
||||
}
|
||||
e.findDispatchScan()
|
||||
}
|
||||
|
||||
// findDispatchScan snapshots the full content and dispatches a SearchTask.
|
||||
// Must be called on the logic goroutine.
|
||||
func (e *EditorState) findDispatchScan() {
|
||||
f := &e.Find
|
||||
f.Gen++
|
||||
f.Scanning = true
|
||||
if TheLogic == nil || TheLogic.workerPool == nil { // unit tests
|
||||
return
|
||||
}
|
||||
text := e.Buffer
|
||||
if cb := e.ChunkedBuffer; cb != nil {
|
||||
if full, err := cb.FullContent(); err == nil {
|
||||
text = full
|
||||
}
|
||||
}
|
||||
TheLogic.workerPool.Dispatch(pool.NewSearchTask(text, f.Query, f.Gen))
|
||||
}
|
||||
|
||||
// applySearchResult applies a completed SearchTask result. Stale generations
|
||||
// (a newer query superseded this scan) are dropped. Must be called on the
|
||||
// logic goroutine.
|
||||
func (e *EditorState) applySearchResult(res pool.Result) {
|
||||
f := &e.Find
|
||||
var sd pool.SearchData
|
||||
switch v := res.Data.(type) {
|
||||
case pool.SearchData:
|
||||
sd = v
|
||||
case *pool.SearchData:
|
||||
sd = *v
|
||||
default:
|
||||
return
|
||||
}
|
||||
if sd.Gen != f.Gen || !f.Visible {
|
||||
return
|
||||
}
|
||||
f.Scanning = false
|
||||
wasEmpty := len(f.Matches) == 0
|
||||
prevStart := -1
|
||||
if f.Cur >= 0 && f.Cur < len(f.Matches) {
|
||||
prevStart = f.Matches[f.Cur][0]
|
||||
}
|
||||
f.Matches = sd.Matches
|
||||
f.Cur = -1
|
||||
if len(f.Matches) > 0 {
|
||||
// Keep the previously current match when it survived the
|
||||
// re-scan; otherwise select the first one.
|
||||
if prevStart >= 0 {
|
||||
if i := sort.Search(len(f.Matches), func(i int) bool {
|
||||
return f.Matches[i][0] >= prevStart
|
||||
}); i < len(f.Matches) && f.Matches[i][0] == prevStart {
|
||||
f.Cur = i
|
||||
}
|
||||
}
|
||||
if f.Cur < 0 {
|
||||
f.Cur = 0
|
||||
}
|
||||
// The first discovery of matches (matches went 0 -> N) selects and
|
||||
// scrolls the view to the current match; while the user keeps
|
||||
// typing, results update in place and the view moves only on
|
||||
// explicit next/prev.
|
||||
if wasEmpty {
|
||||
SetSelection(f.Matches[f.Cur][0], f.Matches[f.Cur][1])
|
||||
scrollToFindMatch(f.Matches[f.Cur][0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// findStep selects the next (dir>0) or previous (dir<0) match, wrapping
|
||||
// around. With no current match yet, "next" goes to the first match at or
|
||||
// after the current selection start/caret (wrapping to the first) and
|
||||
// "prev" to the last match ending before the selection end (wrapping to the
|
||||
// last).
|
||||
func findStep(dir int) {
|
||||
e := &TheState.Editor
|
||||
f := &e.Find
|
||||
n := len(f.Matches)
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
idx := -1
|
||||
if f.Cur >= 0 {
|
||||
idx = (f.Cur + dir + n) % n
|
||||
} else {
|
||||
ref := e.CursorPosition
|
||||
if selActive() {
|
||||
if dir > 0 {
|
||||
ref = e.SelectionStart
|
||||
} else {
|
||||
ref = e.SelectionEnd
|
||||
}
|
||||
}
|
||||
if dir > 0 {
|
||||
for i, m := range f.Matches {
|
||||
if m[0] >= ref {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if idx < 0 {
|
||||
idx = 0 // wrap to the first
|
||||
}
|
||||
} else {
|
||||
for i := n - 1; i >= 0; i-- {
|
||||
if f.Matches[i][1] < ref {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if idx < 0 {
|
||||
idx = n - 1 // wrap to the last
|
||||
}
|
||||
}
|
||||
}
|
||||
m := f.Matches[idx]
|
||||
f.Cur = idx
|
||||
SetSelection(m[0], m[1])
|
||||
scrollToFindMatch(m[0])
|
||||
}
|
||||
|
||||
// scrollToFindMatch scrolls so the visual line containing absByte sits at
|
||||
// the top of the editor viewport (clamped to [0, MaxScroll]).
|
||||
func scrollToFindMatch(absByte int) {
|
||||
cb := TheState.Editor.ChunkedBuffer
|
||||
if cb == nil || cb.LineIndex == nil {
|
||||
return
|
||||
}
|
||||
li := cb.LineIndex.FindLogicalLineForByteOffset(absByte)
|
||||
vl := int64(li)
|
||||
if w := cb.WrapIndex; w != nil {
|
||||
vl += int64(w.VisualsBefore(li))
|
||||
}
|
||||
lh := float64(EffectiveLineHeight())
|
||||
target := ui.Dp(float64(vl) * lh)
|
||||
if target < 0 {
|
||||
target = 0
|
||||
}
|
||||
if target > TheState.MaxScroll {
|
||||
target = TheState.MaxScroll
|
||||
}
|
||||
TheState.ScrollOffset = target
|
||||
}
|
||||
|
||||
// findEdit reports a length-changing edit to the find state: the byte range
|
||||
// [start, end) of the pre-edit content was replaced by newLen bytes. Matches
|
||||
// entirely before it are unchanged, matches entirely after it shift by
|
||||
// newLen-(end-start), and matches overlapping it are dropped (their text no
|
||||
// longer matches the query — finding text an edit creates requires a
|
||||
// re-scan, which happens when the query is re-typed).
|
||||
func (e *EditorState) findEdit(start, end, newLen int) {
|
||||
f := &e.Find
|
||||
if len(f.Matches) == 0 {
|
||||
return
|
||||
}
|
||||
delta := newLen - (end - start)
|
||||
out := make([][2]int, 0, len(f.Matches))
|
||||
cur := -1
|
||||
for i, m := range f.Matches {
|
||||
s, mEnd := m[0], m[1]
|
||||
var kept [2]int
|
||||
switch {
|
||||
case mEnd <= start:
|
||||
kept = m
|
||||
case s >= end:
|
||||
kept = [2]int{s + delta, mEnd + delta}
|
||||
default:
|
||||
continue // overlaps the edited range: drop
|
||||
}
|
||||
if i == f.Cur {
|
||||
cur = len(out)
|
||||
}
|
||||
out = append(out, kept)
|
||||
}
|
||||
f.Matches = out
|
||||
f.Cur = cur
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
package editor
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"pad/internal/io/pool"
|
||||
)
|
||||
|
||||
// findTestState builds a small state: 10 lines of "aa\n" (30 bytes) with a
|
||||
// find query "a" whose matches were scanned before the test's edits.
|
||||
func findTestState() {
|
||||
// NewLogic (used by other tests in this package) leaves the global
|
||||
// TheLogic pointing at a STOPPED logic; the find code must not dispatch
|
||||
// through it (findDispatchScan guards on nil).
|
||||
TheLogic = nil
|
||||
TheState = NewState()
|
||||
TheState.Editor.Filename = "t.txt"
|
||||
content := "aa\naa\naa\naa\naa\naa\naa\naa\naa\naa\n"
|
||||
TheState.Editor.Buffer = content
|
||||
TheState.Editor.ChunkedBuffer = nil
|
||||
e := &TheState.Editor
|
||||
e.Find = FindState{Visible: true, Query: "a", Cur: -1}
|
||||
// Every byte except newlines matches "a".
|
||||
for i := 0; i < len(content); i++ {
|
||||
if content[i] != '\n' {
|
||||
e.Find.Matches = append(e.Find.Matches, [2]int{i, i + 1})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindEdit_ShiftAfter(t *testing.T) {
|
||||
findTestState()
|
||||
e := &TheState.Editor
|
||||
// Insert 4 bytes at offset 20: matches at/after 20 shift by 4; the
|
||||
// current match (pick one after the edit) keeps its identity.
|
||||
before := e.Find.Matches[len(e.Find.Matches)-1]
|
||||
e.Find.Cur = len(e.Find.Matches) - 1
|
||||
e.findEdit(20, 20, 4)
|
||||
if got := e.Find.Matches[len(e.Find.Matches)-1]; got != [2]int{before[0] + 4, before[1] + 4} {
|
||||
t.Fatalf("last match %v, want %v shifted by 4", got, [2]int{before[0] + 4, before[1] + 4})
|
||||
}
|
||||
if e.Find.Cur != len(e.Find.Matches)-1 {
|
||||
t.Fatalf("cur %d", e.Find.Cur)
|
||||
}
|
||||
// A match before the edit is untouched.
|
||||
if e.Find.Matches[0] != [2]int{0, 1} {
|
||||
t.Fatalf("first match %v", e.Find.Matches[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindEdit_DropOverlapping(t *testing.T) {
|
||||
findTestState()
|
||||
e := &TheState.Editor
|
||||
e.Find.Cur = 2 // byte 2
|
||||
// Replace [2,4) with nothing: matches 2 and 3 (bytes 2,3) overlap and
|
||||
// are dropped; later matches shift left by 2; the current match is
|
||||
// dropped, so Cur resets to -1.
|
||||
e.findEdit(2, 4, 0)
|
||||
for _, m := range e.Find.Matches {
|
||||
if m[0] == 2 && m[1] == 4 {
|
||||
t.Fatalf("stale match %v survived an edit inside it", m)
|
||||
}
|
||||
}
|
||||
if e.Find.Cur != -1 {
|
||||
t.Fatalf("cur %d, want -1 (current match was edited away)", e.Find.Cur)
|
||||
}
|
||||
// The match that was at byte 4 is now at byte 2.
|
||||
if e.Find.Matches[0][0] != 0 || e.Find.Matches[1][0] != 1 || e.Find.Matches[2][0] != 2 {
|
||||
t.Fatalf("matches after delete: %v", e.Find.Matches[:5])
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindStep_NextPrevWrap(t *testing.T) {
|
||||
findTestState()
|
||||
e := &TheState.Editor
|
||||
// No current match yet: next from caret 0 -> the match AT 0 (index 0).
|
||||
e.CursorPosition = 0
|
||||
findStep(1)
|
||||
if e.Find.Cur != 0 {
|
||||
t.Fatalf("cur %d, want 0", e.Find.Cur)
|
||||
}
|
||||
if e.SelectionStart != 0 || e.SelectionEnd != 1 {
|
||||
t.Fatalf("selection [%d,%d), want [0,1)", e.SelectionStart, e.SelectionEnd)
|
||||
}
|
||||
// next -> 1, prev -> 0.
|
||||
findStep(1)
|
||||
findStep(-1)
|
||||
if e.Find.Cur != 0 {
|
||||
t.Fatalf("cur %d, want 0", e.Find.Cur)
|
||||
}
|
||||
// prev from 0 wraps to the LAST match.
|
||||
findStep(-1)
|
||||
if e.Find.Cur != len(e.Find.Matches)-1 {
|
||||
t.Fatalf("cur %d, want last (wrapped)", e.Find.Cur)
|
||||
}
|
||||
// next from last wraps to the FIRST.
|
||||
findStep(1)
|
||||
if e.Find.Cur != 0 {
|
||||
t.Fatalf("cur %d, want 0 (wrapped)", e.Find.Cur)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindStep_NoMatches(t *testing.T) {
|
||||
findTestState()
|
||||
e := &TheState.Editor
|
||||
e.Find.Matches = nil
|
||||
e.CursorPosition = 5
|
||||
findStep(1)
|
||||
if e.Find.Cur != -1 {
|
||||
t.Fatalf("cur %d, want -1 with no matches", e.Find.Cur)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplySearchResult_GenGate(t *testing.T) {
|
||||
findTestState()
|
||||
e := &TheState.Editor
|
||||
e.Find.Gen = 3
|
||||
// Stale generation: dropped, state untouched.
|
||||
stale := e.Find.Matches
|
||||
e.applySearchResult(pool.Result{TaskType: pool.TypeSearch, Success: true, Data: pool.SearchData{Gen: 2, Matches: [][2]int{}}})
|
||||
if len(e.Find.Matches) != len(stale) {
|
||||
t.Fatalf("stale result was applied: %d matches", len(e.Find.Matches))
|
||||
}
|
||||
// Current generation: applied; the surviving previous current match
|
||||
// keeps its position.
|
||||
e.Find.Cur = 1
|
||||
e.applySearchResult(pool.Result{TaskType: pool.TypeSearch, Success: true, Data: pool.SearchData{Gen: 3, Matches: stale}})
|
||||
if e.Find.Cur != 1 {
|
||||
t.Fatalf("cur %d, want 1 (kept)", e.Find.Cur)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplySearchResult_FirstDiscoveryScrolls(t *testing.T) {
|
||||
findTestState()
|
||||
e := &TheState.Editor
|
||||
// No ChunkedBuffer/LineIndex in this unit state: scrollToFindMatch is a
|
||||
// no-op, so this only pins that first discovery selects match 0.
|
||||
e.Find.Matches = nil
|
||||
e.Find.Cur = -1
|
||||
e.Find.Scanning = true
|
||||
e.Find.Gen = 1
|
||||
e.applySearchResult(pool.Result{TaskType: pool.TypeSearch, Success: true, Data: pool.SearchData{
|
||||
Gen: 1, Matches: [][2]int{{10, 11}, {20, 21}},
|
||||
}})
|
||||
if e.Find.Scanning {
|
||||
t.Fatal("scanning flag not cleared")
|
||||
}
|
||||
if e.Find.Cur != 0 {
|
||||
t.Fatalf("cur %d, want 0 (first discovery)", e.Find.Cur)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindShowClose_FocusTransitions(t *testing.T) {
|
||||
findTestState()
|
||||
TheState.FocusedElementID = "editor_text"
|
||||
TheState.Editor.Find.Visible = false
|
||||
TheState.Editor.findShow()
|
||||
if !TheState.Editor.Find.Visible {
|
||||
t.Fatal("find bar not shown")
|
||||
}
|
||||
if TheState.FocusedElementID != "find_bar" {
|
||||
t.Fatalf("focus %q, want find_bar", TheState.FocusedElementID)
|
||||
}
|
||||
TheState.Editor.findClose()
|
||||
if TheState.Editor.Find.Visible {
|
||||
t.Fatal("find bar still visible")
|
||||
}
|
||||
if TheState.FocusedElementID != "editor_text" {
|
||||
t.Fatalf("focus %q, want editor_text", TheState.FocusedElementID)
|
||||
}
|
||||
// The query survives close/reopen.
|
||||
q := TheState.Editor.Find.Query
|
||||
TheState.Editor.findShow()
|
||||
if TheState.Editor.Find.Query != q {
|
||||
t.Fatal("query lost across close/reopen")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindReset_SupersedesInflight(t *testing.T) {
|
||||
findTestState()
|
||||
e := &TheState.Editor
|
||||
e.Find.Gen = 5
|
||||
e.findReset()
|
||||
if e.Find.Visible || len(e.Find.Matches) != 0 {
|
||||
t.Fatal("findReset did not clear state")
|
||||
}
|
||||
if e.Find.Gen != 6 {
|
||||
t.Fatalf("gen %d, want 6 (in-flight scan must be superseded)", e.Find.Gen)
|
||||
}
|
||||
// The in-flight (gen 5) result must now be dropped.
|
||||
e.applySearchResult(pool.Result{TaskType: pool.TypeSearch, Success: true, Data: pool.SearchData{Gen: 5, Matches: [][2]int{{0, 1}}}})
|
||||
if len(e.Find.Matches) != 0 {
|
||||
t.Fatal("superseded result was applied")
|
||||
}
|
||||
}
|
||||
@@ -125,7 +125,9 @@ type EditorState struct {
|
||||
SelDragPressY float64
|
||||
SelDragAnchorX float64
|
||||
SelDragAnchorY float64
|
||||
Filename string
|
||||
// Find is the in-file search state (find bar); see search.go.
|
||||
Find FindState
|
||||
Filename string
|
||||
// TooLarge is set when an opened file exceeds MaxEditableFileSize. The
|
||||
// editor shows a "too large to edit" notice instead of content (the
|
||||
// browser can still list the file).
|
||||
@@ -376,6 +378,7 @@ func GoToBrowser(data any) {
|
||||
if TheLogic != nil {
|
||||
TheLogic.FlushAll()
|
||||
}
|
||||
TheState.Editor.findClose()
|
||||
TheState.page = BrowserPage
|
||||
}
|
||||
|
||||
@@ -638,6 +641,7 @@ func deleteRange(start, end int) {
|
||||
str := TheState.Editor.Buffer
|
||||
TheState.Editor.Buffer = str[:start] + str[end:]
|
||||
}
|
||||
TheState.Editor.findEdit(start, end, 0)
|
||||
}
|
||||
|
||||
// --- Touch selection (v1) ---------------------------------------------------
|
||||
@@ -1630,6 +1634,7 @@ func HandleDelete() {
|
||||
if w := utf8AdvanceWidth(seg); w > 0 {
|
||||
buf.Delete(pos, w)
|
||||
buf.UpdateLineIndexAfterDelete(pos, pos+w)
|
||||
e.findEdit(pos, pos+w, 0)
|
||||
markDirty()
|
||||
}
|
||||
return
|
||||
@@ -1643,7 +1648,9 @@ func HandleDelete() {
|
||||
if pos+4 < end {
|
||||
end = pos + 4
|
||||
}
|
||||
TheState.Editor.Buffer = str[:pos] + str[pos+utf8AdvanceWidth(str[pos:end]):]
|
||||
w := utf8AdvanceWidth(str[pos:end])
|
||||
TheState.Editor.Buffer = str[:pos] + str[pos+w:]
|
||||
e.findEdit(pos, pos+w, 0)
|
||||
markDirty()
|
||||
}
|
||||
|
||||
@@ -1668,6 +1675,7 @@ func HandleInsert(s string) {
|
||||
str := e.Buffer
|
||||
e.Buffer = str[:pos] + s + str[pos:]
|
||||
}
|
||||
e.findEdit(pos, pos, len(s))
|
||||
e.CursorPosition = pos + len(s)
|
||||
markDirty()
|
||||
}
|
||||
@@ -1697,6 +1705,7 @@ func HandleBackspace() {
|
||||
buf.Delete(pos-w, w)
|
||||
buf.UpdateLineIndexAfterDelete(pos-w, pos)
|
||||
TheState.Editor.CursorPosition = pos - w
|
||||
e.findEdit(pos-w, pos, 0)
|
||||
markDirty()
|
||||
return
|
||||
}
|
||||
@@ -1709,6 +1718,7 @@ func HandleBackspace() {
|
||||
w := utf8BackspaceWidth(str[segStart:pos])
|
||||
TheState.Editor.Buffer = str[:pos-w] + str[pos:]
|
||||
TheState.Editor.CursorPosition = pos - w
|
||||
e.findEdit(pos-w, pos, 0)
|
||||
markDirty()
|
||||
}
|
||||
|
||||
@@ -1807,6 +1817,7 @@ func HandleReplaceRange(startRune, endRune int, text string) {
|
||||
TheState.Editor.Buffer = s[:absStart] + text + s[absStart:]
|
||||
newCursor = absStart + len(text)
|
||||
}
|
||||
TheState.Editor.findEdit(absStart, absEnd, len(text))
|
||||
TheState.Editor.CursorPosition = newCursor
|
||||
// A commit consumed any selection it overlapped (see the union above).
|
||||
ClearSelection()
|
||||
@@ -1846,6 +1857,54 @@ func markDirty() {
|
||||
}
|
||||
|
||||
// EditorLayout computes the element tree for the editor page.
|
||||
// buildFindBar lays out the in-file search bar: [input][counter][prev][next]
|
||||
// [close], full screen width, directly below the margin-free top bar (the
|
||||
// bars merge, so this one does too; inner content keeps the margin). The
|
||||
// input is a MAIN-owned GioEditor registered as "find_bar" (the browser
|
||||
// "search_bar" precedent, architecture.md §1); the counter and buttons are
|
||||
// logic-owned elements whose taps run the handlers in search.go.
|
||||
func buildFindBar(screenWidth ui.Dp) ui.Element {
|
||||
margin := ui.Dp(10)
|
||||
gap := ui.Dp(8)
|
||||
h := ui.Dp(40)
|
||||
topY := ui.Dp(32) // top bar height (see EditorLayout)
|
||||
|
||||
// Right-hand button column: prev, next, close.
|
||||
iconW := ui.IconSize
|
||||
closeX := screenWidth - margin - iconW
|
||||
nextX := closeX - gap - iconW
|
||||
prevX := nextX - gap - iconW
|
||||
counterW := ui.Dp(96)
|
||||
inputX := margin
|
||||
inputW := prevX - gap - counterW - margin
|
||||
|
||||
f := TheState.Editor.Find
|
||||
counter := "0"
|
||||
switch {
|
||||
case f.Scanning:
|
||||
counter = "…"
|
||||
case f.Query != "" && len(f.Matches) == 0:
|
||||
counter = "No matches"
|
||||
case f.Cur >= 0:
|
||||
counter = fmt.Sprintf("%d / %d", f.Cur+1, len(f.Matches))
|
||||
}
|
||||
|
||||
return ui.NewContainer(
|
||||
ui.Region{X: 0, Y: topY, W: screenWidth, H: h},
|
||||
ui.Color{R: 230, G: 230, B: 230, A: 255},
|
||||
[]ui.Element{
|
||||
ui.NewGioEditor("find_bar", ui.Region{X: inputX, Y: ui.Dp(6), W: inputW, H: ui.Dp(28)}),
|
||||
ui.NewLabel(counter, 12, ui.Region{X: inputX + inputW + gap, Y: ui.Dp(8), W: counterW, H: ui.Dp(20)}, ui.AlignEnd, "", nil),
|
||||
ui.NewIcon("chevron_up", ui.Region{X: prevX, Y: ui.Dp(8), W: iconW, H: iconW}, 0,
|
||||
[]ui.Interaction{{Gesture: ui.Tap, Handler: FindPrev}}),
|
||||
ui.NewIcon("chevron_down", ui.Region{X: nextX, Y: ui.Dp(8), W: iconW, H: iconW}, 0,
|
||||
[]ui.Interaction{{Gesture: ui.Tap, Handler: FindNext}}),
|
||||
ui.NewIcon("close", ui.Region{X: closeX, Y: ui.Dp(8), W: iconW, H: iconW}, 0,
|
||||
[]ui.Interaction{{Gesture: ui.Tap, Handler: FindClose}}),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
margin := ui.Dp(10)
|
||||
|
||||
@@ -1870,7 +1929,9 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
[]ui.Element{
|
||||
ui.NewIcon("back", ui.Region{X: margin, Y: ui.Dp(4), W: ui.IconSize, H: ui.IconSize}, 0,
|
||||
[]ui.Interaction{{Gesture: ui.Tap, Handler: GoToBrowser}}),
|
||||
ui.NewLabel(filename, 14, ui.Region{X: ui.Dp(32) + margin, Y: ui.Dp(6), W: statusBarW - ui.Dp(32) - margin, H: ui.Dp(20)}, ui.AlignStart, "", nil),
|
||||
ui.NewLabel(filename, 14, ui.Region{X: ui.Dp(32) + margin, Y: ui.Dp(6), W: statusBarW - ui.Dp(32) - margin - ui.IconSize - ui.Dp(8), H: ui.Dp(20)}, ui.AlignStart, "", nil),
|
||||
ui.NewIcon("search", ui.Region{X: screenWidth - margin - ui.IconSize, Y: ui.Dp(4), W: ui.IconSize, H: ui.IconSize}, 0,
|
||||
[]ui.Interaction{{Gesture: ui.Tap, Handler: ToggleFind}}),
|
||||
},
|
||||
)
|
||||
|
||||
@@ -1919,6 +1980,13 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
},
|
||||
)
|
||||
|
||||
// --- Find bar (in-file search): full width, directly below the top bar,
|
||||
// while open (see search.go / buildFindBar). ---
|
||||
var findBar ui.Element
|
||||
if TheState.Editor.Find.Visible {
|
||||
findBar = buildFindBar(screenWidth)
|
||||
}
|
||||
|
||||
// --- Editor text area ---
|
||||
editorY := statusBarRegion.Y + statusBarRegion.H
|
||||
editorH := bottomBarRegion.Y - editorY
|
||||
@@ -2137,6 +2205,15 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
{Gesture: ui.SelDrag, Handler: HandleSelDragEvt},
|
||||
},
|
||||
)
|
||||
// While the find bar is open, key focus belongs to the main-owned
|
||||
// "find_bar" widget.Editor: the editor stops its per-frame IME sync, and
|
||||
// regaining "editor_text" focus re-issues key.FocusCmd on close (see
|
||||
// TextField.Draw's focus dedup).
|
||||
if TheState.Editor.Find.Visible {
|
||||
TheState.FocusedElementID = "find_bar"
|
||||
} else if TheState.FocusedElementID == "find_bar" {
|
||||
TheState.FocusedElementID = "editor_text"
|
||||
}
|
||||
// Set Focused so TextField.Draw() issues key.FocusCmd, which is required
|
||||
// for Gio to deliver key events to this element. ShowIMESeq carries the
|
||||
// keyboard-raise pulse (see TextField.ShowIMESeq).
|
||||
@@ -2146,6 +2223,9 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
editorElem.CaretDrag = TheState.Editor.CaretDrag
|
||||
|
||||
elems := []ui.Element{statusBar, editorElem, bottomBar}
|
||||
if findBar != nil {
|
||||
elems = append(elems, findBar)
|
||||
}
|
||||
// The selection menu is added last so it draws on top of the editor.
|
||||
// Re-anchor it to the selection's live screen position every frame so it
|
||||
// follows the text while scrolling (it used to stay where it was first
|
||||
|
||||
Reference in New Issue
Block a user