project

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package project finds the project a file belongs to and says what is in it: its files, the lines in them that match a search, and which file is another's test. It is the part of projectile that holds no editor state, so it can be tested against directories rather than through buffers, as the dired package is.

Index

Constants

View Source
const Marker = ".projectile"

Marker is the file that makes a directory a project when nothing else does, or a different project from the repository around it. It is projectile's, so a tree already set up for projectile works as it is, and it may list what to leave out of the project; see Files.

View Source
const MaxFiles = 200_000

MaxFiles is where listing a project stops. A tree that large is a home directory or a disk given a Marker, not a project, and listing it all would hold the editor up for as long as the disk takes.

View Source
const MaxMatches = 10_000

MaxMatches is where a search stops: past it the list is too long to read, and the search should be narrowed instead.

Variables

View Source
var ErrTooManyFiles = errors.New("the project has too many files; the list stops at 200000")

ErrTooManyFiles says a listing stopped at MaxFiles. The files it does return are still worth offering.

Functions

func Contains

func Contains(root, path string) bool

Contains reports whether path is root or lies beneath it.

func Counterparts

func Counterparts(rel string, files []string) []string

Counterparts lists the files among files that are the other half of rel: its tests if it is code, the code it tests if it is a test. The nearest come first - in rel's own directory, then those sharing most of its path - so the one wanted is almost always the first.

func Dirs

func Dirs(files []string) []string

Dirs lists the directories that hold files, each with a "/" after it: every directory on the way to one, not only those with files of their own, so a directory of directories can be found too.

func FileTypes added in v0.12.0

func FileTypes() []string

FileTypes lists the names -t and -T know.

func Files

func Files(root string) ([]string, error)

Files lists the files of the project at root, relative to it and sorted, with "/" between the parts of a path whatever the OS.

In a git repository git says what the files are - tracked, and new ones not ignored - so .gitignore is honoured and listing is as quick as git is. Elsewhere, or without git, the tree is walked, passing over the directories in skipDirs.

Either way a Marker at root can narrow the list, as projectile's does. Each line is a pattern: one starting with + keeps only the directory it names, one starting with - or with neither leaves out what it matches, and # starts a comment. A pattern starting with / is matched from root; one without is matched against every part of a path, so "*.log" leaves out every log and "tmp" every directory called tmp.

func FilesWith added in v0.12.0

func FilesWith(root string, noIgnore bool) ([]string, error)

FilesWith is Files, and with noIgnore - a search's -u - the files .gitignore leaves out as well, and the directories a walk passes over. Only a repository's own store is still left out.

func IsTest

func IsTest(name string) bool

IsTest reports whether name, a file's base name, is named as a test is.

func Name

func Name(root string) string

Name is how a project is called in prompts and messages: its directory's name.

func Root

func Root(dir string) (string, bool)

Root is the project dir belongs to: the nearest directory, dir or one above it, holding a Marker or a repository; failing that, the nearest holding a build file. False means dir is in no project.

Nearest rather than outermost, so a repository inside another - a submodule, a vendored checkout - is a project of its own, as is a directory given a Marker inside a monorepo.

The home directory and the filesystem root are passed over unless they hold a Marker. A home directory kept in git for its dotfiles would otherwise make every file anywhere under it one project of a hundred thousand files.

Types

type Case added in v0.12.0

type Case int

Case says how a query treats case.

const (
	// CaseSmart ignores case unless the pattern has a capital letter in it.
	CaseSmart Case = iota
	// CaseSensitive is -s.
	CaseSensitive
	// CaseIgnore is -i.
	CaseIgnore
)

type Lines

type Lines func(rel string) ([]string, bool)

Lines supplies a file's text for searching, as lines, when the caller has it already - an open buffer, whose unsaved edits are what should be searched, and whose line numbers are the ones the user will be taken to. False reads the file from disk.

type Match

type Match struct {
	// File is the file, relative to the project root, as Files lists it.
	File string
	// Line is the line's index, from 0, and Col the rune column where the
	// first match on it starts.
	Line, Col int
	// Text is the line as a result shows it: all of it, or for a long line
	// the part around the first match, with an ellipsis for what is left out.
	Text string
	// Spans are the rune ranges of Text that matched, start and end.
	Spans [][2]int
}

Match is a line that matched a search.

func MatchLines added in v0.8.0

func MatchLines(file string, lines []string, re *regexp.Regexp) []Match

MatchLines finds the lines re matches among lines, which file names: one file's part of a search, or a single buffer's, for occur.

func Search(root string, files []string, re *regexp.Regexp, open Lines) (matches []Match, more bool)

Search finds the lines of files, relative to root, that re matches, in the order of files and then of lines. More reports that it stopped at MaxMatches.

Files are searched in parallel, and a file that is not text - one with a NUL byte near its start - or is larger than any source file is passed over.

func SearchWith added in v0.12.0

func SearchWith(root string, files []string, re *regexp.Regexp, open Lines, opt Options) (matches []Match, more bool)

SearchWith is Search within opt's bounds. A search stopped by opt.Stop returns what it had found.

type Options added in v0.12.0

type Options struct {
	// Max is how many matches the search stops at: MaxMatches when 0.
	Max int
	// Stop abandons the search when it is set: one run as the pattern is
	// typed has been overtaken by the next keystroke.
	Stop *atomic.Bool
}

Options bounds a search.

type Query added in v0.12.0

type Query struct {
	// Pattern is what is looked for: a regexp, or text with Fixed.
	Pattern string
	// Fixed is -F: the pattern is text.
	Fixed bool
	// Word is -w: only whole words match.
	Word bool
	Case Case
	// Types and NotTypes are -t and -T: file types searched and passed over.
	Types, NotTypes []string
	// Globs are -g, in order; one starting with ! leaves out what it matches.
	Globs []string
	// NoIgnore is -u: files .gitignore leaves out are searched too.
	NoIgnore bool
	// contains filtered or unexported fields
}

Query is a search, as typed.

func ParseQuery added in v0.12.0

func ParseQuery(s string) (Query, error)

ParseQuery reads a query typed as ripgrep's command line: options, then the pattern. An option it does not know, or one missing its value, is an error; the pattern may be empty.

func (Query) Filter added in v0.12.0

func (q Query) Filter(files []string) []string

Filter is the files the query searches, of files.

func (Query) Keep added in v0.12.0

func (q Query) Keep(rel string) bool

Keep reports whether the query searches the file at rel, a path from the top of what is searched: it is of a type -t names, if any, and of none -T names, and the last glob matching it does not leave it out - and matches it, when any glob is not one that leaves out.

func (Query) Regexp added in v0.12.0

func (q Query) Regexp() *regexp.Regexp

Regexp is the query's pattern compiled, with its options. A pattern that is not a valid regexp - one half typed, or meant as text - is looked for as text, as though -F were given, rather than refused.

Jump to

Keyboard shortcuts

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