editor

package
v0.1.23-oss Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExecCommand = exec.Command

ExecCommand is used to launch external editor processes and can be overridden in tests.

Functions

func EditWithValidation

func EditWithValidation(opts EditOptions) error

EditWithValidation copies TargetFile to a temporary file, opens the user's editor, validates the result, and re-opens the editor with inline error comments until validation succeeds or the user aborts.

func IsVim

func IsVim(bin string) bool

IsVim reports whether the given editor binary name belongs to the Vim family (vi, vim, nvim, gvim, mvim, vim.basic, vim.tiny, etc.).

func Open

func Open(filePath string, extraArgs ...string) error

Open launches the user's preferred interactive editor on filePath, passing any extraArgs before filePath.

func PrependErrorHeader

func PrependErrorHeader(filePath string, valErr error) ([]byte, error)

PrependErrorHeader adds a commented validation error block to the top of filePath.

func Resolve

func Resolve() []string

Resolve returns the user's preferred editor command and initial arguments. It checks $VISUAL, $EDITOR, `git config --get core.editor`, and falls back to `vi`.

func SetupVimSupport

func SetupVimSupport(dir string, filetype string, specs []VimKeySpec) (vimScriptPath string, cleanup func(), err error)

SetupVimSupport creates a temporary dictionary file and Vimscript file in dir to configure syntax highlighting, keyword characters, and key/value autocomplete.

func StripErrorHeader

func StripErrorHeader(filePath string) error

StripErrorHeader removes the automatically generated validation error comment block from filePath if present.

Types

type EditOptions

type EditOptions struct {
	// TargetFile is the path to the file to copy before editing and atomically replace upon validation.
	// If TargetFile does not exist yet, editing starts from an empty file.
	TargetFile string

	// TempDir is the directory where temporary editing files are created.
	// If empty, defaults to filepath.Dir(TargetFile) when TargetFile is set, or os.TempDir().
	TempDir string

	// TempPrefix is the prefix used when creating the temporary file.
	TempPrefix string

	// FileExtension is the suffix for the temporary file (e.g. ".yaml"). Defaults to ".tmp".
	FileExtension string

	// RejectedFile is the file path where unapplied changes are saved if the user aborts
	// after a validation failure. If empty and TargetFile is set, defaults to TargetFile + ".rejected".
	RejectedFile string

	// Filetype specifies the syntax filetype (e.g. "yaml") for Vim editors.
	Filetype string

	// VimSpecs specifies optional autocomplete keys/values when the editor is Vim/Neovim.
	VimSpecs []VimKeySpec

	// Validate is called on the temporary file after each edit.
	// If Validate returns a non-nil error, the error is prepended as a comment block at the top
	// of the temporary file and the editor is re-opened so the user can fix the issue.
	Validate func(editedPath string) error

	// Apply is an optional callback to apply the validated temporary file.
	// If nil and TargetFile is set, the validated temporary file is atomically renamed to TargetFile.
	Apply func(validatedPath string) error
}

EditOptions configures an interactive file editing session with validation and retry support.

type VimKeySpec

type VimKeySpec struct {
	Key    string
	Menu   string
	Values []VimValueOption
}

VimKeySpec represents a YAML key and its allowed value completion candidates in Vim.

func BuildStructVimSpecs

func BuildStructVimSpecs[T any]() []VimKeySpec

BuildStructVimSpecs extracts YAML tags, Go types, `desc` struct tags, and enum value options (via EnumValues() [][2]string) using reflection on struct T.

type VimValueOption

type VimValueOption struct {
	Word string
	Menu string
}

VimValueOption represents a single value completion candidate for a YAML key in Vim.

Jump to

Keyboard shortcuts

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