Documentation
¶
Overview ¶
Package outline returns the declarations of a source file, with line ranges, and the parse verdict of the parse that produced them: whether the file was parsed, why not if it was not, and the syntax errors the parse found. Go is parsed with go/parser in every build; fourteen other languages with their tree-sitter grammars in a build with cgo. One parse gives both the declarations and the verdict, so a file is never outlined by one backend and checked by another.
A syntax error is a position and a kind (unexpected text, or a token that is missing where it was expected), never a message. Semantic errors (unresolved names, types, unused imports) are not reported: only what the parser rejects.
A grammar can lag its language: a file that uses syntax newer than the grammar of this build is reported with syntax errors although the language accepts it. A caller that refuses files, or edits, on this verdict should offer a way past it.
Index ¶
- Constants
- func CommentAndStringSpans(ctx context.Context, path string, src []byte) (spans [][2]int, ok bool)
- func IsTestFile(rel string) bool
- func LangFor(path string, src []byte) string
- func LangForExt(ext string) string
- type Backend
- type Decl
- type File
- type Kind
- type NotParsedReason
- type Options
- type Source
- type SyntaxError
- type SyntaxErrorKind
Constants ¶
const ( LangGo = "Go" LangPython = "Python" LangJavaScript = "JavaScript" LangTypeScript = "TypeScript" LangRust = "Rust" LangJava = "Java" LangKotlin = "Kotlin" LangCSharp = "C#" LangRuby = "Ruby" LangC = "C" LangCPP = "C++" LangPHP = "PHP" LangScala = "Scala" LangLua = "Lua" LangShell = "Shell" )
Language names returned by langForExt.
Variables ¶
This section is empty.
Functions ¶
func CommentAndStringSpans ¶
CommentAndStringSpans returns the byte ranges [start, end) of the comments and string literals in src, in order and not overlapping.
For a Go file it answers in every build, from Go's tokens: every line and block comment, interpreted and raw string, and rune literal. ok is then true whether or not the file has syntax errors, and ctx is not consulted. An unterminated raw string or block comment runs to the end of src; an unterminated interpreted string or rune literal ends with its line. An empty or nil src has no spans.
For any other language the spans come from the language's tree-sitter grammar, in a build with cgo; ok is false when there is no grammar, in a build without cgo, or when ctx ended.
func IsTestFile ¶
IsTestFile reports whether rel, a slash-separated path relative to a workspace root, is a test, test data or test support code, by the naming and directory conventions of the languages in the extension table (LangForExt). It is the one classifier the tools rank by, so a consumer that ranks, filters or splits files the same way can agree with them.
Any file under a directory named test, tests, __tests__, spec, testdata, testutil, testutils or testhelpers counts. Otherwise a source file (one whose extension is in the table) counts when its name, without the extension:
- ends in _test or starts with test_ (Go, Python, Rust, C, Ruby, ...);
- ends in _spec (Ruby) or in .test or .spec (JavaScript, TypeScript);
- ends in Test or Tests after at least one character (Java, Kotlin, C#, Ruby), or in IT (Java integration tests);
- is test support: conftest.py, setupTests, jest.setup or vitest.setup.
The rule is a convention, not a parse: it reads no file.
func LangFor ¶
LangFor returns the language of the file at path, whose content is src: the extension table's answer, except that a `.h` header containing C++ is C++. src may be nil, in which case the extension alone decides.
func LangForExt ¶
LangForExt returns the language name for a file extension (with the leading dot, in any case), or "" if the extension is not in the table. It is the exported form of the table's lookup, so a package that names languages can use the table instead of keeping a copy.
Types ¶
type Decl ¶
type Decl struct {
Kind Kind
Name string
Container string
Signature string
Exported bool
StartLine int // 1-based, inclusive
EndLine int // 1-based, inclusive; 0 when unknown
}
Decl is one declaration in a source file.
type File ¶
type File struct {
Path string // slash-separated, relative to Options.Root or absolute
Lang string // language name from the extension table
Backend Backend // which backend produced the declarations
Decls []Decl // sorted by StartLine, ties broken by Name; never nil
// Parsed is true when a backend parsed the file's bytes, whether or not
// it found syntax errors; Backend is then not BackendNone.
Parsed bool
// NotParsed is empty when Parsed is true, and otherwise says why the
// file was not parsed. Backend is then BackendNone and Decls empty.
NotParsed NotParsedReason
// SyntaxErrors are the errors the parse found, in position order, at
// most Options.MaxSyntaxErrors of them; empty and never nil when there
// are none or the file was not parsed.
SyntaxErrors []SyntaxError
// SyntaxErrorsTruncated is true when the parse found more errors than
// SyntaxErrors holds.
SyntaxErrorsTruncated bool
}
File is the outline of one source file, with the parse verdict of the parse that produced its declarations.
type NotParsedReason ¶ added in v0.11.0
type NotParsedReason string
NotParsedReason says why a file was not parsed: a closed set.
const ( // NotParsedUnsupported: the extension is not in the table, or this build // has no backend for the language (any language but Go without cgo). NotParsedUnsupported NotParsedReason = "unsupported" // NotParsedTooLarge: the file is over Options.MaxFileBytes. NotParsedTooLarge NotParsedReason = "too_large" // NotParsedBinary: a NUL byte in the file's first 8 KiB. NotParsedBinary NotParsedReason = "binary" // NotParsedFailed: the backend returned no syntax tree, or the file // could not be read. NotParsedFailed NotParsedReason = "failed" )
type Options ¶
type Options struct {
Root string // when non-empty, File.Path is relative to this
MaxFileBytes int64 // 0 means 1 MiB
// MaxSyntaxErrors caps File.SyntaxErrors: 0 means 20, and a negative
// value is treated as 0.
MaxSyntaxErrors int
}
Options configures outlining.
type Source ¶
type Source struct {
Abs string // absolute path
Src []byte // optional; when nil the file is read from disk
}
Source identifies a file to outline.
type SyntaxError ¶ added in v0.11.0
type SyntaxError struct {
Line int // 1-based
Column int // 1-based, in bytes
Kind SyntaxErrorKind
}
SyntaxError is one syntax error a parse found: a position and a kind, with no message.
type SyntaxErrorKind ¶ added in v0.11.0
type SyntaxErrorKind string
SyntaxErrorKind is the closed set of syntax error kinds.
const ( // SyntaxErrorUnexpected is text the grammar could not place. SyntaxErrorUnexpected SyntaxErrorKind = "unexpected" // SyntaxErrorMissing is a token the grammar needed and did not find, // reported where it was expected. SyntaxErrorMissing SyntaxErrorKind = "missing" )