filesearch

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package filesearch is Silk's find-in-files engine — the UI-agnostic core behind an IDE's "Find in Files" (Qt Creator's Search > Find in Files). It walks a directory tree, scans each eligible text file line by line, and reports every match; a companion Replace computes the rewritten line for a match without touching disk (a caller applies the edit).

Design notes:

  • Column is a 1-based BYTE offset. Match.Col is the byte position of the match start within the line (first byte = 1), not a rune or display column. For multibyte/UTF-8 text a caller that needs a character column can derive it from Match.Text[:Match.Col-1]. Byte offsets are what the scanner and regexp engine report natively, so they are exact and cheap.

  • Every occurrence is reported. A line containing the pattern N times yields N Matches that share Path/Line/Text and differ only in Col (left-to-right, non-overlapping), mirroring how Qt Creator lists each hit as its own result. Zero-width regex matches (e.g. "a*" against a line with no "a") are ignored.

  • Skipped by the walk: .git, vendor and node_modules directories, and any hidden directory (name starting with "."). Hidden *files* are still scanned. With Options.SkipBinary set, a file whose first chunk contains a NUL byte is treated as binary and skipped.

  • The only error Search returns (besides an unreadable root) is a regexp compile error for a malformed pattern. Individual files that cannot be opened or read are skipped, not fatal. An empty pattern matches nothing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Replace

func Replace(m Match, pattern, replacement string, opt Options) (newLine string)

Replace computes m's line with pattern replaced by replacement, using the same Regex/IgnoreCase semantics as the Search that produced m. It is pure: it reads only m.Text and never touches disk.

Regex mode expands replacement templates ($1, ${name}); literal mode inserts replacement verbatim (a "$" stays a "$"). Every occurrence on the line is replaced, so a caller should apply Replace once per line, not once per Match.

Types

type Match

type Match struct {
	Path string // file path as walked from root
	Line int    // 1-based line number
	Col  int    // 1-based BYTE offset of the match start within the line
	Text string // the full line, with any trailing CR/LF removed
}

Match is a single occurrence of the pattern.

func Search(root, pattern string, opt Options) ([]Match, error)

Search walks root and returns every match of pattern under the rules in opt. Matches are ordered by walk order, then Line, then Col. A malformed regex (when opt.Regex) returns a compile error and no matches; an empty pattern matches nothing.

type Options

type Options struct {
	// Regex interprets pattern as an RE2 regular expression. When false,
	// pattern is matched literally.
	Regex bool
	// IgnoreCase folds case for both literal and regex matching.
	IgnoreCase bool
	// Include restricts the scan to files whose extension is one of these,
	// e.g. []string{".go", ".txt"}. A leading dot is optional and the
	// comparison is case-insensitive. Empty means every text file.
	Include []string
	// SkipBinary skips a file whose first chunk contains a NUL byte.
	SkipBinary bool
}

Options configures a Search (and the matcher Replace reconstructs).

Jump to

Keyboard shortcuts

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