changeset 6759:f939397387e8

feat(commands): add `language` variable expansion (#13466)
author RoloEdits <RoloEdits@gmail.com>
date Sun, 04 May 2025 06:35:58 -0700
parents 9766d2a1e3ad
children 5a70b8c5db07
files book/src/command-line.md helix-view/src/expansion.rs
diffstat 2 files changed, 10 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/book/src/command-line.md	Fri May 02 09:45:21 2025 -0400
+++ b/book/src/command-line.md	Sun May 04 06:35:58 2025 -0700
@@ -47,6 +47,7 @@
 | `cursor_column` | The column number of the primary cursor in the currently focused document, starting at 1. This is counted as the number of grapheme clusters from the start of the line rather than bytes or codepoints. |
 | `buffer_name` | The relative path of the currently focused document. `[scratch]` is expanded instead for scratch buffers. |
 | `line_ending` | A string containing the line ending of the currently focused document. For example on Unix systems this is usually a line-feed character (`\n`) but on Windows systems this may be a carriage-return plus a line-feed (`\r\n`). The line ending kind of the currently focused document can be inspected with the `:line-ending` command. |
+| `language` | A string containing the language name of the currently focused document.|
 
 Aside from editor variables, the following expansions may be used:
 
--- a/helix-view/src/expansion.rs	Fri May 02 09:45:21 2025 -0400
+++ b/helix-view/src/expansion.rs	Sun May 04 06:35:58 2025 -0700
@@ -33,6 +33,8 @@
     BufferName,
     /// A string containing the line-ending of the currently focused document.
     LineEnding,
+    // The name of current buffers language as set in `languages.toml`
+    Language,
 }
 
 impl Variable {
@@ -41,6 +43,7 @@
         Self::CursorColumn,
         Self::BufferName,
         Self::LineEnding,
+        Self::Language,
     ];
 
     pub const fn as_str(&self) -> &'static str {
@@ -49,6 +52,7 @@
             Self::CursorColumn => "cursor_column",
             Self::BufferName => "buffer_name",
             Self::LineEnding => "line_ending",
+            Self::Language => "language",
         }
     }
 
@@ -58,6 +62,7 @@
             "cursor_column" => Some(Self::CursorColumn),
             "buffer_name" => Some(Self::BufferName),
             "line_ending" => Some(Self::LineEnding),
+            "language" => Some(Self::Language),
             _ => None,
         }
     }
@@ -215,5 +220,9 @@
             }
         }
         Variable::LineEnding => Ok(Cow::Borrowed(doc.line_ending.as_str())),
+        Variable::Language => Ok(match doc.language_name() {
+            Some(lang) => Cow::Owned(lang.to_owned()),
+            None => Cow::Borrowed("text"),
+        }),
     }
 }