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

search

package
v0.410.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTypes = map[string][]string{
	"go":        {"*.go"},
	"js":        {"*.js", "*.mjs", "*.cjs"},
	"ts":        {"*.ts", "*.tsx", "*.mts", "*.cts"},
	"py":        {"*.py", "*.pyi", "*.pyw"},
	"python":    {"*.py", "*.pyi", "*.pyw"},
	"rust":      {"*.rs"},
	"c":         {"*.c", "*.h"},
	"cpp":       {"*.cc", "*.cpp", "*.cxx", "*.hh", "*.hpp", "*.hxx"},
	"java":      {"*.java"},
	"kotlin":    {"*.kt", "*.kts"},
	"swift":     {"*.swift"},
	"cs":        {"*.cs"},
	"csharp":    {"*.cs"},
	"json":      {"*.json"},
	"yaml":      {"*.yml", "*.yaml"},
	"toml":      {"*.toml"},
	"xml":       {"*.xml", "*.xsd", "*.xsl"},
	"html":      {"*.html", "*.htm"},
	"css":       {"*.css", "*.scss", "*.sass", "*.less"},
	"sh":        {"*.sh", "*.bash", "*.zsh", "*.fish"},
	"bash":      {"*.sh", "*.bash"},
	"sql":       {"*.sql"},
	"proto":     {"*.proto"},
	"md":        {"*.md", "*.markdown", "*.mkd"},
	"markdown":  {"*.md", "*.markdown"},
	"rb":        {"*.rb"},
	"ruby":      {"*.rb"},
	"php":       {"*.php"},
	"lua":       {"*.lua"},
	"docker":    {"*Dockerfile*", "*.dockerfile"},
	"makefile":  {"Makefile", "GNUmakefile", "*.mk"},
	"cmake":     {"CMakeLists.txt", "*.cmake"},
	"tf":        {"*.tf", "*.tfvars"},
	"terraform": {"*.tf", "*.tfvars"},
	"gradle":    {"*.gradle", "*.gradle.kts"},
	"nim":       {"*.nim", "*.nims"},
	"zig":       {"*.zig"},
	"dart":      {"*.dart"},
	"elixir":    {"*.ex", "*.exs"},
	"clojure":   {"*.clj", "*.cljc", "*.cljs"},
	"scala":     {"*.scala", "*.sc"},
	"haskell":   {"*.hs", "*.lhs"},
	"r":         {"*.r", "*.R"},
}

DefaultTypes maps type names (as used in --type flag) to glob patterns. Ported from ripgrep's crates/ignore/src/default_types.rs

Functions

func CountMatches

func CountMatches(path string, pattern *regexp.Regexp) (int, error)

CountMatches returns the number of matching lines in a file.

func TypeToGlobs

func TypeToGlobs(typeName string) ([]string, bool)

TypeToGlobs returns the glob patterns for a given type name. Returns nil, false if the type is not found.

Types

type FileMatch

type FileMatch struct {
	Path    string
	ModTime time.Time
	Lines   []LineMatch // empty for file-only searches
	Count   int         // for OutputModeCount
}

FileMatch is a file that matched a search query.

func SearchFiles

func SearchFiles(ctx context.Context, opts WalkOptions) ([]FileMatch, bool, error)

SearchFiles walks rootPath and returns matching files, sorted by modification time (newest first).

type IgnoreMatcher

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

IgnoreMatcher holds compiled ignore patterns from one or more ignore files. Patterns are applied in order; last match wins (like git).

func LoadIgnoreFiles

func LoadIgnoreFiles(rootPath string) (*IgnoreMatcher, error)

LoadIgnoreFiles loads .gitignore and .pandoignore files starting from rootPath and walking up to the filesystem root. Returns an IgnoreMatcher with all patterns.

func (*IgnoreMatcher) Matches

func (m *IgnoreMatcher) Matches(absPath string, isDir bool) bool

Matches returns true if the given absolute path should be ignored. isDir should be true when the path is a directory.

type LineMatch

type LineMatch struct {
	LineNum   int
	Text      string
	IsContext bool // true = context line, false = actual match
}

LineMatch is a single matched or context line from a search.

func SearchFile

func SearchFile(ctx context.Context, path string, opts SearchOptions) ([]LineMatch, error)

SearchFile searches a single file and returns matched lines. Returns nil, nil if the file is binary or has no matches.

type OutputMode

type OutputMode string

OutputMode controls what the searcher returns.

const (
	OutputModeContent OutputMode = "content"            // matched lines + context
	OutputModeFiles   OutputMode = "files_with_matches" // just file paths
	OutputModeCount   OutputMode = "count"              // count per file
)

type SearchOptions

type SearchOptions struct {
	Pattern    *regexp.Regexp
	OutputMode OutputMode
	BeforeCtx  int  // lines of before-context (-B)
	AfterCtx   int  // lines of after-context (-A)
	Multiline  bool // match across lines (load whole file)
}

SearchOptions configures a content search operation.

type WalkOptions

type WalkOptions struct {
	RootPath      string
	Pattern       *regexp.Regexp // nil = list files only (no content search)
	IncludeGlob   string         // file name glob, e.g. "*.go"
	TypeFilter    string         // type name, e.g. "go" (expands via DefaultTypes)
	IgnoreMatcher *IgnoreMatcher // nil = no ignore filtering
	SearchOpts    SearchOptions
	MaxResults    int // 0 = unlimited
	Concurrency   int // 0 = runtime.NumCPU()
}

WalkOptions configures a file walk and optional content search.

Jump to

Keyboard shortcuts

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