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
- Variables
- func Contains(root, path string) bool
- func Counterparts(rel string, files []string) []string
- func Dirs(files []string) []string
- func FileTypes() []string
- func Files(root string) ([]string, error)
- func FilesWith(root string, noIgnore bool) ([]string, error)
- func IsTest(name string) bool
- func Name(root string) string
- func Root(dir string) (string, bool)
- type Case
- type Lines
- type Match
- type Options
- type Query
Constants ¶
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.
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.
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 ¶
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 Counterparts ¶
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 ¶
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 ¶
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
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 Root ¶
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 Lines ¶
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
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 ¶
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.
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
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) Keep ¶ added in v0.12.0
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.