editorconfig

package
v0.9.4 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package editorconfig reads .editorconfig files: for a file, what the .editorconfig files in its directory and the directories above it say about it.

It follows the EditorConfig specification (https://spec.editorconfig.org). The files are gathered from the file's own directory upward until one says root = true, or the filesystem runs out. A nearer file is applied after a farther one, so it wins; within a file, a later section wins over an earlier one; and a value of "unset" takes a property back out. Only the properties nem acts on are interpreted. The rest are read and ignored, which is what the specification asks of an editor that does not support them.

It depends on nothing but the standard library and holds no editor state, so it is tested against directories on disk, as the project package is.

Index

Constants

View Source
const FileName = ".editorconfig"

FileName is the name of the file this package reads.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool int8

Bool is a property that can be true, false, or not said at all.

Not said is not false, and the difference matters: insert_final_newline = false takes a file's last newline away, while a file no .editorconfig mentions is saved as it is.

const (
	Unset Bool = iota
	True
	False
)

The three values of a Bool.

type Cache

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

Cache remembers the .editorconfig files it has read, so a lookup costs a stat of each directory's .editorconfig rather than a read and a parse of it. Pressing TAB asks which way to indent; it should not reread the project's configuration to find out. A file whose modification time or size has changed since is read again, so an edit to one applies at once.

A Cache is safe for concurrent use.

func NewCache

func NewCache() *Cache

NewCache returns an empty cache.

func (*Cache) Lookup

func (c *Cache) Lookup(path string) Props

Lookup is what the .editorconfig files say about the file at path. The file itself need not exist - a buffer not yet saved is configured by where it will go.

An .editorconfig that cannot be read is treated as absent rather than as an error: it is advice about whitespace, and there is nothing better to do without it than what would be done if it were not there.

type Props

type Props struct {
	// IndentStyle is "tab" or "space", or "" when nothing says.
	IndentStyle string

	// IndentSize is how many columns one level of indentation takes, and
	// TabWidth how many a tab stands for; 0 when nothing says. Each falls
	// back on the other as the specification lays out: tab_width defaults to
	// indent_size, and indent_size = tab - or no indent_size at all in a file
	// indented with tabs - means the tab width.
	IndentSize, TabWidth int

	// TrimTrailingWhitespace asks for the spaces and tabs at the ends of
	// lines to be removed when the file is saved.
	TrimTrailingWhitespace Bool

	// InsertFinalNewline asks for the saved file to end with a newline, or,
	// when False, not to.
	InsertFinalNewline Bool
}

Props is what the .editorconfig files say about one file. A zero field is one no file mentions.

func Lookup

func Lookup(path string) Props

Lookup is what the .editorconfig files say about the file at path, read through a cache shared by the whole program.

Jump to

Keyboard shortcuts

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