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:
@@ -0,0 +1,107 @@
|
||||
package pool
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFindSubstring_Basic(t *testing.T) {
|
||||
text := "hello world hello"
|
||||
got := FindSubstring(text, "hello")
|
||||
want := [][2]int{{0, 5}, {12, 17}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindSubstring_CaseInsensitive(t *testing.T) {
|
||||
got := FindSubstring("Hello HELLo hellO", "hEllO")
|
||||
want := [][2]int{{0, 5}, {6, 11}, {12, 17}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindSubstring_NonASCIIFold(t *testing.T) {
|
||||
// Only the ASCII-foldable "STRASSE" matches "strasse" (lower-cased to
|
||||
// "strasse"); "Straße" lower-cases to "straße", which does not contain it.
|
||||
// Byte offset 8: "ß" is two UTF-8 bytes, so "Straße" is 7 bytes.
|
||||
got := FindSubstring("Straße STRASSE", "strasse")
|
||||
want := [][2]int{{8, 15}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
// And a query containing a multibyte rune still folds its ASCII parts.
|
||||
got = FindSubstring("Straße", "STRA")
|
||||
want = [][2]int{{0, 4}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindSubstring_NoMatch(t *testing.T) {
|
||||
if got := FindSubstring("abc", "xyz"); got != nil {
|
||||
t.Fatalf("got %v, want nil", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindSubstring_EmptyQuery(t *testing.T) {
|
||||
if got := FindSubstring("abc", ""); got != nil {
|
||||
t.Fatalf("got %v, want nil for empty query", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindSubstring_OverlappingConsumed(t *testing.T) {
|
||||
// "aa" in "aaa": one match; the match consumes its range.
|
||||
got := FindSubstring("aaa", "aa")
|
||||
want := [][2]int{{0, 2}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
// "...abaaba": matches at 3 and 6, not at 4.
|
||||
got = FindSubstring("abaaba", "aba")
|
||||
want = [][2]int{{0, 3}, {3, 6}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindSubstring_Multibyte(t *testing.T) {
|
||||
// Offsets are BYTE offsets. "é" is 2 bytes.
|
||||
text := "éxéx" // bytes: 0-1 é, 2 x, 3-4 é, 5 x
|
||||
got := FindSubstring(text, "xé")
|
||||
want := [][2]int{{2, 5}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindSubstring_WholeText(t *testing.T) {
|
||||
got := FindSubstring("abc", "ABC")
|
||||
want := [][2]int{{0, 3}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchTask_Execute(t *testing.T) {
|
||||
task := NewSearchTask("one two One TWO", "two", 7)
|
||||
res := task.Execute()
|
||||
if !res.Success {
|
||||
t.Fatalf("unexpected error: %v", res.Error)
|
||||
}
|
||||
if res.TaskType != TypeSearch {
|
||||
t.Fatalf("type %v", res.TaskType)
|
||||
}
|
||||
sd, ok := res.Data.(SearchData)
|
||||
if !ok {
|
||||
t.Fatalf("data type %T", res.Data)
|
||||
}
|
||||
if sd.Gen != 7 {
|
||||
t.Fatalf("gen %d", sd.Gen)
|
||||
}
|
||||
want := [][2]int{{4, 7}, {12, 15}}
|
||||
if !reflect.DeepEqual(sd.Matches, want) {
|
||||
t.Fatalf("matches %v, want %v", sd.Matches, want)
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
@@ -71,6 +72,8 @@ const (
|
||||
// State persistence types
|
||||
TypeSaveState
|
||||
TypeSaveUndo
|
||||
// In-file search (scans an in-memory content snapshot on a worker)
|
||||
TypeSearch
|
||||
)
|
||||
|
||||
func (t TaskType) String() string {
|
||||
@@ -105,6 +108,8 @@ func (t TaskType) String() string {
|
||||
return "save_state"
|
||||
case TypeSaveUndo:
|
||||
return "save_undo"
|
||||
case TypeSearch:
|
||||
return "search"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
@@ -502,6 +507,94 @@ func (t *ReadFileTask) Context() context.Context { return t.ctx }
|
||||
func (t *ReadFileTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
func (t *ReadFileTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
|
||||
// SearchData is the payload of a completed SearchTask: the byte ranges
|
||||
// [start, end) of every match of Query in the scanned Text, ascending and
|
||||
// non-overlapping. Gen is the query generation the scan was dispatched for,
|
||||
// so the caller can drop stale results.
|
||||
type SearchData struct {
|
||||
Gen uint64
|
||||
Matches [][2]int
|
||||
}
|
||||
|
||||
// SearchTask scans an in-memory content snapshot for a plain, case-
|
||||
// insensitive substring. The caller snapshots the content (the editor's
|
||||
// in-range files are fully resident) so the scan never races edits or chunk
|
||||
// state; a superseding query gets a higher Gen and its result is dropped by
|
||||
// the caller. Matching is plain substring (no regex), case-insensitive via
|
||||
// unicode lower-casing of both sides, matching the browser's name filter.
|
||||
type SearchTask struct {
|
||||
taskID string
|
||||
Text string
|
||||
Query string
|
||||
Gen uint64
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
// NewSearchTask creates a new SearchTask.
|
||||
func NewSearchTask(text, query string, gen uint64) *SearchTask {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
return &SearchTask{
|
||||
taskID: fmt.Sprintf("search-%d", taskIDCounter.Add(1)),
|
||||
Text: text,
|
||||
Query: query,
|
||||
Gen: gen,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *SearchTask) Execute() Result {
|
||||
matches := FindSubstring(t.Text, t.Query)
|
||||
return Result{TaskID: t.taskID, TaskType: TypeSearch, Success: true, Data: SearchData{Gen: t.Gen, Matches: matches}}
|
||||
}
|
||||
|
||||
// FindSubstring returns the byte ranges [start, end) of every occurrence of
|
||||
// query in text, case-insensitively. Occurrences are ascending and non-
|
||||
// overlapping (a match consumes its range, so "aa" finds one match in
|
||||
// "aaa"). An empty query matches nothing.
|
||||
func FindSubstring(text, query string) [][2]int {
|
||||
if query == "" {
|
||||
return nil
|
||||
}
|
||||
q := toLowerFold(query)
|
||||
t := toLowerFold(text)
|
||||
var matches [][2]int
|
||||
from := 0
|
||||
for {
|
||||
i := indexOf(t[from:], q)
|
||||
if i < 0 {
|
||||
return matches
|
||||
}
|
||||
abs := from + i
|
||||
matches = append(matches, [2]int{abs, abs + len(q)})
|
||||
from = abs + len(q)
|
||||
}
|
||||
}
|
||||
|
||||
// toLowerFold lower-cases s without allocating when s is already
|
||||
// lower-case ASCII (the common case for both query and content).
|
||||
func toLowerFold(s string) string {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if b := s[i]; b >= 'A' && b <= 'Z' {
|
||||
return strings.ToLower(s)
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// indexOf is strings.Index but kept local so the scan above reads as one
|
||||
// unit; it is the standard library's two-way search.
|
||||
func indexOf(s, sub string) int { return strings.Index(s, sub) }
|
||||
|
||||
func (t *SearchTask) Priority() Priority { return MediumPriority }
|
||||
func (t *SearchTask) TaskType() TaskType { return TypeSearch }
|
||||
func (t *SearchTask) TaskID() string { return t.taskID }
|
||||
func (t *SearchTask) DirPath() string { return "" }
|
||||
func (t *SearchTask) Context() context.Context { return t.ctx }
|
||||
func (t *SearchTask) Timeout() time.Duration { return 10 * time.Second }
|
||||
func (t *SearchTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
|
||||
// WriteFileTask writes content to a file.
|
||||
type WriteFileTask struct {
|
||||
taskID string
|
||||
|
||||
Reference in New Issue
Block a user