changeset 6753:ddf879daa606

feat: give formatters access to filename (#13429) Co-authored-by: Michael Davis <[email protected]>
author Joffrey Bluth? <joffrey.bluthe@e.email>
date Tue, 29 Apr 2025 00:34:05 +0200
parents 26763ac2b61e
children 6539757b6bbf
files book/src/languages.md helix-core/src/command_line.rs helix-term/src/commands/typed.rs helix-view/src/document.rs
diffstat 4 files changed, 51 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/book/src/languages.md	Mon Apr 28 07:48:54 2025 -0700
+++ b/book/src/languages.md	Tue Apr 29 00:34:05 2025 +0200
@@ -66,7 +66,7 @@
 | `indent`              | The indent to use. Has sub keys `unit` (the text inserted into the document when indenting; usually set to N spaces or `"\t"` for tabs) and `tab-width` (the number of spaces rendered for a tab) |
 | `language-servers`    | The Language Servers used for this language. See below for more information in the section [Configuring Language Servers for a language](#configuring-language-servers-for-a-language)   |
 | `grammar`             | The tree-sitter grammar to use (defaults to the value of `name`) |
-| `formatter`           | The formatter for the language, it will take precedence over the lsp when defined. The formatter must be able to take the original file as input from stdin and write the formatted file to stdout |
+| `formatter`           | The formatter for the language, it will take precedence over the lsp when defined. The formatter must be able to take the original file as input from stdin and write the formatted file to stdout. The filename of the current buffer can be passed as argument by using the `%{buffer_name}` expansion variable. See below for more information in the [Configuring the formatter command](#configuring-the-formatter-command) |
 | `soft-wrap`           | [editor.softwrap](./editor.md#editorsoft-wrap-section)
 | `text-width`          |  Maximum line length. Used for the `:reflow` command and soft-wrapping if `soft-wrap.wrap-at-text-width` is set, defaults to `editor.text-width`   |
 | `rulers`              | Overrides the `editor.rulers` config key for the language. |
@@ -102,6 +102,16 @@
    the file extension of a given file wins. In the example above, the `"toml"`
    config matches files like `Cargo.toml` or `languages.toml`.
 
+### Configuring the formatter command
+
+[Command line expansions](./command-line.md#expansions) are supported in the arguments
+of the formatter command. In particular, the `%{buffer_name}` variable can be passed as
+argument to the formatter:
+
+```toml
+formatter = { command = "mylang-formatter" , args = ["--stdin", "--stdin-filename %{buffer_name}"] }
+```
+
 ## Language Server configuration
 
 Language servers are configured separately in the table `language-server` in the same file as the languages `languages.toml`
--- a/helix-core/src/command_line.rs	Mon Apr 28 07:48:54 2025 -0700
+++ b/helix-core/src/command_line.rs	Tue Apr 29 00:34:05 2025 +0200
@@ -357,7 +357,7 @@
     pub is_terminated: bool,
 }
 
-impl Token<'_> {
+impl<'a> Token<'a> {
     pub fn empty_at(content_start: usize) -> Self {
         Self {
             kind: TokenKind::Unquoted,
@@ -366,6 +366,15 @@
             is_terminated: false,
         }
     }
+
+    pub fn expand(content: impl Into<Cow<'a, str>>) -> Self {
+        Self {
+            kind: TokenKind::Expand,
+            content_start: 0,
+            content: content.into(),
+            is_terminated: true,
+        }
+    }
 }
 
 #[derive(Debug)]
--- a/helix-term/src/commands/typed.rs	Mon Apr 28 07:48:54 2025 -0700
+++ b/helix-term/src/commands/typed.rs	Tue Apr 29 00:34:05 2025 +0200
@@ -339,8 +339,9 @@
     // Save an undo checkpoint for any outstanding changes.
     doc.append_changes_to_history(view);
 
+    let (view, doc) = current_ref!(cx.editor);
     let fmt = if config.auto_format {
-        doc.auto_format().map(|fmt| {
+        doc.auto_format(cx.editor).map(|fmt| {
             let callback = make_format_callback(
                 doc.id(),
                 doc.version(),
@@ -483,8 +484,8 @@
         return Ok(());
     }
 
-    let (view, doc) = current!(cx.editor);
-    let format = doc.format().context(
+    let (view, doc) = current_ref!(cx.editor);
+    let format = doc.format(cx.editor).context(
         "A formatter isn't available, and no language server provides formatting capabilities",
     )?;
     let callback = make_format_callback(doc.id(), doc.version(), view.id, format, None);
@@ -752,7 +753,8 @@
         doc.append_changes_to_history(view);
 
         let fmt = if options.auto_format && config.auto_format {
-            doc.auto_format().map(|fmt| {
+            let doc = doc!(cx.editor, &doc_id);
+            doc.auto_format(cx.editor).map(|fmt| {
                 let callback = make_format_callback(
                     doc_id,
                     doc.version(),
--- a/helix-view/src/document.rs	Mon Apr 28 07:48:54 2025 -0700
+++ b/helix-view/src/document.rs	Tue Apr 29 00:34:05 2025 +0200
@@ -5,6 +5,7 @@
 use futures_util::FutureExt;
 use helix_core::auto_pairs::AutoPairs;
 use helix_core::chars::char_is_word;
+use helix_core::command_line::Token;
 use helix_core::diagnostic::DiagnosticProvider;
 use helix_core::doc_formatter::TextFormat;
 use helix_core::encoding::Encoding;
@@ -45,6 +46,7 @@
 use crate::{
     editor::Config,
     events::{DocumentDidChange, SelectionDidChange},
+    expansion,
     view::ViewPosition,
     DocumentId, Editor, Theme, View, ViewId,
 };
@@ -777,9 +779,12 @@
 
     /// The same as [`format`], but only returns formatting changes if auto-formatting
     /// is configured.
-    pub fn auto_format(&self) -> Option<BoxFuture<'static, Result<Transaction, FormatterError>>> {
+    pub fn auto_format(
+        &self,
+        editor: &Editor,
+    ) -> Option<BoxFuture<'static, Result<Transaction, FormatterError>>> {
         if self.language_config()?.auto_format {
-            self.format()
+            self.format(editor)
         } else {
             None
         }
@@ -789,7 +794,10 @@
     /// to format it nicely.
     // We can't use anyhow::Result here since the output of the future has to be
     // clonable to be used as shared future. So use a custom error type.
-    pub fn format(&self) -> Option<BoxFuture<'static, Result<Transaction, FormatterError>>> {
+    pub fn format(
+        &self,
+        editor: &Editor,
+    ) -> Option<BoxFuture<'static, Result<Transaction, FormatterError>>> {
         if let Some((fmt_cmd, fmt_args)) = self
             .language_config()
             .and_then(|c| c.formatter.as_ref())
@@ -814,8 +822,20 @@
                 process.current_dir(doc_dir);
             }
 
+            let args = match fmt_args
+                .iter()
+                .map(|content| expansion::expand(editor, Token::expand(content)))
+                .collect::<Result<Vec<_>, _>>()
+            {
+                Ok(args) => args,
+                Err(err) => {
+                    log::error!("Failed to expand formatter arguments: {err}");
+                    return None;
+                }
+            };
+
             process
-                .args(fmt_args)
+                .args(args.iter().map(AsRef::as_ref))
                 .stdin(Stdio::piped())
                 .stdout(Stdio::piped())
                 .stderr(Stdio::piped());