feat: add workspace_directory and current_working_directory variable expansions (#13068)

Co-authored-by: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com>
This commit is contained in:
Nik Revenco
2025-07-26 19:58:32 +01:00
committed by GitHub
parent 4fd4588482
commit f72b6f758b
2 changed files with 23 additions and 0 deletions

View File

@@ -47,6 +47,8 @@ The following variables are supported:
| `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. |
| `current_working_directory` | Current working directory |
| `workspace_directory` | Nearest ancestor directory of the current working directory that contains `.git`, `.svn`, `jj` or `.helix` |
| `language` | A string containing the language name of the currently focused document.|
| `selection` | A string containing the contents of the primary selection of the currently focused document. |
| `selection_line_start` | The line number of the start of the primary selection in the currently focused document, starting at 1. |

View File

@@ -33,6 +33,10 @@ pub enum Variable {
BufferName,
/// A string containing the line-ending of the currently focused document.
LineEnding,
/// Curreng working directory
CurrentWorkingDirectory,
/// Nearest ancestor directory of the current working directory that contains `.git`, `.svn`, `jj` or `.helix`
WorkspaceDirectory,
// The name of current buffers language as set in `languages.toml`
Language,
// Primary selection
@@ -49,6 +53,8 @@ impl Variable {
Self::CursorColumn,
Self::BufferName,
Self::LineEnding,
Self::CurrentWorkingDirectory,
Self::WorkspaceDirectory,
Self::Language,
Self::Selection,
Self::SelectionLineStart,
@@ -61,6 +67,8 @@ impl Variable {
Self::CursorColumn => "cursor_column",
Self::BufferName => "buffer_name",
Self::LineEnding => "line_ending",
Self::CurrentWorkingDirectory => "current_working_directory",
Self::WorkspaceDirectory => "workspace_directory",
Self::Language => "language",
Self::Selection => "selection",
Self::SelectionLineStart => "selection_line_start",
@@ -74,6 +82,8 @@ impl Variable {
"cursor_column" => Some(Self::CursorColumn),
"buffer_name" => Some(Self::BufferName),
"line_ending" => Some(Self::LineEnding),
"workspace_directory" => Some(Self::WorkspaceDirectory),
"current_working_directory" => Some(Self::CurrentWorkingDirectory),
"language" => Some(Self::Language),
"selection" => Some(Self::Selection),
"selection_line_start" => Some(Self::SelectionLineStart),
@@ -235,6 +245,17 @@ fn expand_variable(editor: &Editor, variable: Variable) -> Result<Cow<'static, s
}
}
Variable::LineEnding => Ok(Cow::Borrowed(doc.line_ending.as_str())),
Variable::CurrentWorkingDirectory => Ok(std::borrow::Cow::Owned(
helix_stdx::env::current_working_dir()
.to_string_lossy()
.to_string(),
)),
Variable::WorkspaceDirectory => Ok(std::borrow::Cow::Owned(
helix_loader::find_workspace()
.0
.to_string_lossy()
.to_string(),
)),
Variable::Language => Ok(match doc.language_name() {
Some(lang) => Cow::Owned(lang.to_owned()),
None => Cow::Borrowed("text"),