Compare commits

...

2 Commits

Author SHA1 Message Date
Tom Schafer f56981a63b
Merge 3a30eb5a84 into 22b184b570 2025-07-21 09:31:06 +01:00
Thomas Schafer 3a30eb5a84
Add absolute file path command expansion value 2025-02-28 13:19:38 +00:00
2 changed files with 16 additions and 0 deletions

View File

@ -46,6 +46,7 @@ The following variables are supported:
| `cursor_line` | The line number of the primary cursor in the currently focused document, starting at 1. | | `cursor_line` | The line number of the primary cursor in the currently focused document, starting at 1. |
| `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. | | `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. | | `buffer_name` | The relative path of the currently focused document. `[scratch]` is expanded instead for scratch buffers. |
| `file_path_absolute` | The absolute path of the currently focused document. For scratch buffers this will default to the current working directory. |
| `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. | | `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.| | `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` | A string containing the contents of the primary selection of the currently focused document. |

View File

@ -31,6 +31,9 @@ pub enum Variable {
/// ///
/// This corresponds to `crate::Document::display_name`. /// This corresponds to `crate::Document::display_name`.
BufferName, BufferName,
/// The absolute path of the currently focused document. For scratch buffers this will default
/// to the current working directory.
FilePathAbsolute,
/// A string containing the line-ending of the currently focused document. /// A string containing the line-ending of the currently focused document.
LineEnding, LineEnding,
// The name of current buffers language as set in `languages.toml` // The name of current buffers language as set in `languages.toml`
@ -48,6 +51,7 @@ impl Variable {
Self::CursorLine, Self::CursorLine,
Self::CursorColumn, Self::CursorColumn,
Self::BufferName, Self::BufferName,
Self::FilePathAbsolute,
Self::LineEnding, Self::LineEnding,
Self::Language, Self::Language,
Self::Selection, Self::Selection,
@ -60,6 +64,7 @@ impl Variable {
Self::CursorLine => "cursor_line", Self::CursorLine => "cursor_line",
Self::CursorColumn => "cursor_column", Self::CursorColumn => "cursor_column",
Self::BufferName => "buffer_name", Self::BufferName => "buffer_name",
Self::FilePathAbsolute => "file_path_absolute",
Self::LineEnding => "line_ending", Self::LineEnding => "line_ending",
Self::Language => "language", Self::Language => "language",
Self::Selection => "selection", Self::Selection => "selection",
@ -73,6 +78,7 @@ impl Variable {
"cursor_line" => Some(Self::CursorLine), "cursor_line" => Some(Self::CursorLine),
"cursor_column" => Some(Self::CursorColumn), "cursor_column" => Some(Self::CursorColumn),
"buffer_name" => Some(Self::BufferName), "buffer_name" => Some(Self::BufferName),
"file_path_absolute" => Some(Self::FilePathAbsolute),
"line_ending" => Some(Self::LineEnding), "line_ending" => Some(Self::LineEnding),
"language" => Some(Self::Language), "language" => Some(Self::Language),
"selection" => Some(Self::Selection), "selection" => Some(Self::Selection),
@ -234,6 +240,15 @@ fn expand_variable(editor: &Editor, variable: Variable) -> Result<Cow<'static, s
Ok(Cow::Borrowed(crate::document::SCRATCH_BUFFER_NAME)) Ok(Cow::Borrowed(crate::document::SCRATCH_BUFFER_NAME))
} }
} }
Variable::FilePathAbsolute => {
let path = match doc.path() {
Some(path) => path.to_owned(),
None => helix_stdx::env::current_working_dir(),
}
.to_string_lossy()
.to_string();
Ok(Cow::Owned(path))
}
Variable::LineEnding => Ok(Cow::Borrowed(doc.line_ending.as_str())), Variable::LineEnding => Ok(Cow::Borrowed(doc.line_ending.as_str())),
Variable::Language => Ok(match doc.language_name() { Variable::Language => Ok(match doc.language_name() {
Some(lang) => Cow::Owned(lang.to_owned()), Some(lang) => Cow::Owned(lang.to_owned()),