From cbda20014c971b2c7d217249e4bcc013ffc60e6f Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Mon, 10 Mar 2025 21:39:01 +0000 Subject: [PATCH 1/2] feat: add `current_working_directory` and `workspace_directory` variable expansions --- book/src/command-line.md | 2 ++ helix-view/src/expansion.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/book/src/command-line.md b/book/src/command-line.md index 7bf32bbda..c90b01e0f 100644 --- a/book/src/command-line.md +++ b/book/src/command-line.md @@ -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` | Aside from editor variables, the following expansions may be used: diff --git a/helix-view/src/expansion.rs b/helix-view/src/expansion.rs index 96a71b8e5..cda87603b 100644 --- a/helix-view/src/expansion.rs +++ b/helix-view/src/expansion.rs @@ -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, } impl Variable { @@ -49,6 +53,8 @@ impl Variable { Self::CursorColumn => "cursor_column", Self::BufferName => "buffer_name", Self::LineEnding => "line_ending", + Self::CurrentWorkingDirectory => "current_working_directory", + Self::WorkspaceDirectory => "workspace_directory", } } @@ -215,5 +221,16 @@ fn expand_variable(editor: &Editor, variable: Variable) -> Result 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(), + )), } } From 0a8f65a8266f4abcdb1d25836023c19d26565405 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Mon, 10 Mar 2025 21:54:24 +0000 Subject: [PATCH 2/2] fix: add to VARIANTS --- helix-view/src/expansion.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/helix-view/src/expansion.rs b/helix-view/src/expansion.rs index cda87603b..4fed2e188 100644 --- a/helix-view/src/expansion.rs +++ b/helix-view/src/expansion.rs @@ -45,6 +45,8 @@ impl Variable { Self::CursorColumn, Self::BufferName, Self::LineEnding, + Self::CurrentWorkingDirectory, + Self::WorkspaceDirectory, ]; pub const fn as_str(&self) -> &'static str { @@ -64,6 +66,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), _ => None, } }