Help us shape the future of Go! Take the 2026 Go Developer Survey

search

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package search finds a literal substring across parsed diffs and tracks the cursor over the hits, including the view position to restore when a search is canceled.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MatchLocation

type MatchLocation struct {
	FilePath  string
	MatchText string
	FileIdx   int
	HunkIdx   int
	LineIdx   int
	StartCol  int
	EndCol    int
}

MatchLocation is one hit. StartCol and EndCol are byte offsets into MatchText with EndCol exclusive, and HunkIdx and LineIdx are -1 on a hit in a file path rather than a diff line.

type NavigationState struct {
	SelectedFile   int
	SelectedHunk   int
	DiffViewOffset int
	FocusedPanel   int
}

NavigationState is the view position a search moves away from, held so canceling can put the cursor, scroll offset, and focused panel back where they were.

type State added in v0.1.2

type State struct {
	Query           string
	Matches         []MatchLocation
	CurrentIdx      int
	OriginalState   NavigationState
	IsActive        bool
	IsCaseSensitive bool
}

State carries a query and the hits it produced. Query and IsCaseSensitive are inputs, and Matches and CurrentIdx only reflect them once ExecuteSearch runs, so editing a field alone leaves the match list stale.

func NewState added in v0.1.2

func NewState() *State

NewState returns an inactive, case-insensitive state with CurrentIdx at -1, which is the "nothing selected" value every match accessor checks for.

func (*State) ExecuteSearch added in v0.1.2

func (s *State) ExecuteSearch(files []diff.FileChange)

ExecuteSearch rebuilds the match list by scanning every file path and diff line, then points CurrentIdx at the first hit (or -1 when there is none). Overlapping hits are all reported, because the scan resumes one byte past each one, and an empty Query clears the list.

func (*State) GetCurrentMatch added in v0.1.2

func (s *State) GetCurrentMatch() *MatchLocation

GetCurrentMatch returns the match CurrentIdx points at, or nil when the index is out of range, which covers both a fresh state and a query that found nothing.

func (*State) GetMatchesForLine added in v0.1.2

func (s *State) GetMatchesForLine(fileIdx, hunkIdx, lineIdx int) []MatchLocation

GetMatchesForLine returns every hit on one diff line in ascending column order, which is the order a renderer needs to highlight the spans in one pass. The result is nil when the line has no hit.

func (*State) IsCurrentMatch added in v0.1.2

func (s *State) IsCurrentMatch(fileIdx, hunkIdx, lineIdx int) bool

IsCurrentMatch reports whether the hit CurrentIdx points at falls on one diff line, which lets a renderer style the active hit apart from the rest. It is false whenever there is no current hit.

func (*State) IsLineMatch added in v0.1.2

func (s *State) IsLineMatch(fileIdx, hunkIdx, lineIdx int) bool

IsLineMatch reports whether any hit falls on one diff line. All three indices are 0-based, and a hunkIdx and lineIdx of -1 tests a file-path hit. It walks the whole match list, so cost grows with matches times rendered lines.

func (*State) MatchCount added in v0.1.2

func (s *State) MatchCount() int

MatchCount reports how many hits the last ExecuteSearch found, counting occurrences rather than lines, so one line with two hits counts twice.

func (*State) NextMatch added in v0.1.2

func (s *State) NextMatch() *MatchLocation

NextMatch advances one match and wraps past the last, returning nil when there are no matches. The pointer refers into the match slice, which the next ExecuteSearch replaces.

func (*State) PrevMatch added in v0.1.2

func (s *State) PrevMatch() *MatchLocation

PrevMatch steps back one match and wraps past the first, returning nil when there are no matches.

func (*State) RestoreOriginalState added in v0.1.2

func (s *State) RestoreOriginalState() NavigationState

RestoreOriginalState returns the position saved when the search opened, leaving it saved so a repeated cancel is harmless.

func (*State) SaveOriginalState added in v0.1.2

func (s *State) SaveOriginalState(nav NavigationState)

SaveOriginalState records where to return if the search is canceled, overwriting any earlier save. Call it when the search opens, because calling it after the cursor has jumped to a match saves the match instead of the origin.

Jump to

Keyboard shortcuts

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