finder

package
v0.44.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package finder is nook's in-file find/replace overlay.

Two modes share state: ModeFind (Ctrl+F) and ModeReplace (Ctrl+H). The overlay renders as a one- or two-line bar at the bottom of the workspace. As the user types, the host re-runs Search and feeds the matches back via WithMatches. Navigation keys jump the cursor between matches; replace edits the buffer one match at a time, replace-all rewrites every line that contains at least one match.

The finder does not own the buffer. The host calls Search to compute matches and ApplyReplacement to produce the rewritten line text; the host applies the edit through editor.Pane.SetLine so the editor's dirty flag, LSP didChange, and version counters stay coherent.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyReplacement

func ApplyReplacement(line string, m Match, replacement string, re *regexp.Regexp) string

ApplyReplacement returns the new text for the row after substituting the match's range with replacement. The host calls this then writes the result back via editor.Pane.SetLine. If useRegex is true and the pattern compiles, regex expansion (`$1`, `${name}`) is applied to replacement; the caller passes the compiled regex via re so we don't recompile per call.

When re is nil the substitution is a plain byte-range splice.

func CompileRegex

func CompileRegex(pattern string, caseSensitive bool) (*regexp.Regexp, error)

CompileRegex compiles the pattern under the same flags Search applies. The host calls this once to obtain a *Regexp it can pass into ApplyReplacement for capture-group expansion.

Types

type Event

type Event int

Event is what the host needs to act on after Update. Most updates produce EventNone; specific keys produce typed events.

const (
	EventNone           Event = iota
	EventClose                // Esc
	EventPatternChanged       // re-run Search
	EventJumpNext             // jump editor cursor to next match
	EventJumpPrev             // jump editor cursor to prev match
	EventReplaceCurrent       // replace match at current
	EventReplaceAll           // replace every match
)

type Finder

type Finder struct {
	// contains filtered or unexported fields
}

Finder owns the input fields, toggles, match list, and cursor.

func New

func New(t theme.Theme) Finder

New constructs a closed finder.

func (Finder) CaseSensitive

func (f Finder) CaseSensitive() bool

CaseSensitive reports whether case-sensitive mode is on.

func (Finder) Close

func (f Finder) Close() Finder

Close hides the finder. Pattern and replacement are preserved so reopening recalls the last query.

func (Finder) CurrentIndex

func (f Finder) CurrentIndex() int

CurrentIndex returns the 0-based index of the active match, or -1.

func (Finder) CurrentMatch

func (f Finder) CurrentMatch() (Match, bool)

CurrentMatch returns the active match, or false if there is none.

func (Finder) Focus

func (f Finder) Focus() Focus

Focus returns which input is focused.

func (Finder) Height

func (f Finder) Height() int

Height reports how many rows the finder occupies. 1 for find, 2 for replace mode.

func (Finder) IsOpen

func (f Finder) IsOpen() bool

IsOpen reports whether the bar should render.

func (Finder) Matches

func (f Finder) Matches() []Match

Matches returns the current match list.

func (Finder) Mode

func (f Finder) Mode() Mode

Mode returns the current mode.

func (Finder) Next

func (f Finder) Next() Finder

Next advances the cursor to the next match (wrapping). No-op when empty.

func (Finder) Open

func (f Finder) Open(m Mode) Finder

Open shows the finder in the requested mode. If the mode is changed from ModeFind to ModeReplace (or vice versa) the existing pattern is preserved.

func (Finder) Pattern

func (f Finder) Pattern() string

Pattern returns the find pattern.

func (Finder) PatternErr

func (f Finder) PatternErr() error

PatternErr returns the most recent pattern-compile error (regex mode).

func (Finder) Prev

func (f Finder) Prev() Finder

Prev advances the cursor to the previous match (wrapping). No-op when empty.

func (Finder) Replacement

func (f Finder) Replacement() string

Replacement returns the replacement text.

func (Finder) SelectMatchAt

func (f Finder) SelectMatchAt(row, col int) Finder

SelectMatchAt picks the first match at-or-after (row, col). Called by the host after a JumpTo so the counter "n/N" stays aligned with the editor's cursor position.

func (Finder) SetPatternErr

func (f Finder) SetPatternErr(err error) Finder

SetPatternErr stashes a compile/search error so View can surface it.

func (Finder) SetTheme added in v0.38.0

func (f Finder) SetTheme(t theme.Theme) Finder

SetTheme swaps the palette used for the input box, mode chip, and the `n of N` counter. Next View() picks up the new colors.

func (Finder) ToggleCase

func (f Finder) ToggleCase() Finder

ToggleCase flips case-sensitivity.

func (Finder) ToggleRegex

func (f Finder) ToggleRegex() Finder

ToggleRegex flips regex mode and clears the cached pattern-compile error.

func (Finder) Update

func (f Finder) Update(km tea.KeyMsg) (Finder, Event)

Update routes a key event. Returns the new finder and an Event the host should react to (e.g. EventPatternChanged → re-run Search, EventJumpNext → move editor cursor).

func (Finder) UseRegex

func (f Finder) UseRegex() bool

UseRegex reports whether regex mode is on.

func (Finder) View

func (f Finder) View() string

View renders the bar.

func (Finder) WithMatches

func (f Finder) WithMatches(ms []Match, keepCurrent bool) Finder

WithMatches replaces the match list, computed by the host via Search. If keepCurrent is true the cursor stays on the same row/column as before; if false it resets to the first match. Always clamps to len(matches)-1.

func (Finder) WithSize

func (f Finder) WithSize(w int) Finder

WithSize sets the render width. Height is fixed (1 or 2 rows depending on mode).

type Focus

type Focus int

Focus is which input the keystroke routes to (find vs replace box).

const (
	FocusFind Focus = iota
	FocusReplace
)

type Match

type Match struct {
	Row      int // 0-based row
	StartCol int // 0-based byte column, inclusive
	EndCol   int // 0-based byte column, exclusive
}

Match is one hit inside the active buffer.

func Search(buf []string, pattern string, useRegex, caseSensitive bool) ([]Match, error)

Search scans buf and returns every match of pattern. When useRegex is false the pattern is treated as a literal substring (case-folded when caseSensitive is false). When useRegex is true the pattern compiles as Go's RE2; on compile error the returned matches are nil and the error is returned so the caller can stash it via SetPatternErr.

Each line is searched independently; multi-line regex patterns can use `.` to match within a line but cannot cross a newline.

type Mode

type Mode int

Mode is the finder's editing mode.

const (
	ModeFind Mode = iota
	ModeReplace
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL