From 07ab9dcb2bd455392271d7beb488b543d66fb361 Mon Sep 17 00:00:00 2001 From: moritz Date: Mon, 9 Feb 2026 22:02:59 +0100 Subject: [PATCH] feat: set path for stdin cli Setting the extension when checking file content through command line with cli, it was not possible to check with path based configurations. For example any rule set to check */documentation/en/*.md would not hit any markdown files because only the extension was passed. This adds the "path" parameter to the command line. This allows better integration into IDE plugins that may send the current unsaved file content to vale for a check but then also need to pass a path. --- cmd/vale/flag.go | 2 ++ internal/core/config.go | 1 + internal/core/file.go | 29 ++++++++++++++++++++--------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cmd/vale/flag.go b/cmd/vale/flag.go index 009e37a72..bdfe4c97c 100644 --- a/cmd/vale/flag.go +++ b/cmd/vale/flag.go @@ -26,6 +26,8 @@ func init() { pflag.StringVar(&Flags.Output, "output", "CLI", `An output style ("line", "JSON", or a template file).`) pflag.StringVar(&Flags.InExt, "ext", ".txt", fmt.Sprintf(`An extension to associate with stdin (%s).`, toCodeStyle(`--ext=.md`))) + pflag.StringVar(&Flags.InPath, "path", "", + fmt.Sprintf(`A file path to associate with stdin (%s).`, toCodeStyle(`--path=docs/example.md`))) pflag.StringVar(&Flags.AlertLevel, "minAlertLevel", "", fmt.Sprintf(`The minimum level to display (%s).`, toCodeStyle(`--minAlertLevel=error`))) diff --git a/internal/core/config.go b/internal/core/config.go index d68031115..eb0a7bc27 100644 --- a/internal/core/config.go +++ b/internal/core/config.go @@ -167,6 +167,7 @@ type CLIFlags struct { Built string Glob string InExt string + InPath string Output string Path string Sources string diff --git a/internal/core/file.go b/internal/core/file.go index c3990a6de..58d70d0f9 100755 --- a/internal/core/file.go +++ b/internal/core/file.go @@ -53,6 +53,7 @@ func NewFile(src string, config *Config) (*File, error) { var format, ext string var fbytes []byte var lookup bool + path := src if system.FileExists(src) { fbytes, _ = os.ReadFile(src) @@ -62,14 +63,24 @@ func NewFile(src string, config *Config) (*File, error) { ext, format = FormatFromExt(src, config.Formats) } } else { - ext, format = FormatFromExt(config.Flags.InExt, config.Formats) fbytes = []byte(src) - src = "stdin" + config.Flags.InExt lookup = true + // For stdin, allow an explicit path override to drive path-based config. + if config.Flags.InPath != "" { + path = config.Flags.InPath + } else { + path = "stdin" + config.Flags.InExt + } + // If --ext was explicitly set, respect it; otherwise infer from the path. + if config.Flags.InExt != ".txt" { + ext, format = FormatFromExt(config.Flags.InExt, config.Formats) + } else { + ext, format = FormatFromExt(path, config.Formats) + } } - filepaths := []string{src} - normed := system.ReplaceFileExt(src, config.Formats) + filepaths := []string{path} + normed := system.ReplaceFileExt(path, config.Formats) baseStyles := config.GBaseStyles checks := make(map[string]bool) @@ -95,7 +106,7 @@ func NewFile(src string, config *Config) (*File, error) { sec, err := glob.Compile(syntax) if err != nil { return &File{}, err - } else if sec.Match(src) { + } else if sec.Match(path) { lang = code break } @@ -105,8 +116,8 @@ func NewFile(src string, config *Config) (*File, error) { for sec, p := range config.Stylesheets { pat, err := glob.Compile(sec) if err != nil { - return &File{}, NewE100(src, err) - } else if pat.Match(src) { + return &File{}, NewE100(path, err) + } else if pat.Match(path) { transform = p break } @@ -120,11 +131,11 @@ func NewFile(src string, config *Config) (*File, error) { lines := strings.SplitAfter(strings.Clone(content), "\n") file := File{ - NormedExt: ext, Format: format, RealExt: filepath.Ext(src), + NormedExt: ext, Format: format, RealExt: filepath.Ext(path), BaseStyles: baseStyles, Checks: checks, Lines: lines, Content: content, Comments: make(map[string]bool), history: make(map[string]int), simple: config.Flags.Simple, Transform: transform, - limits: make(map[string]int), Path: src, Metrics: make(map[string]int), + limits: make(map[string]int), Path: path, Metrics: make(map[string]int), NLP: nlp.Info{Endpoint: config.NLPEndpoint, Lang: lang}, Lookup: lookup, NormedPath: normed, }