mirror of https://github.com/helix-editor/helix
Merge d8b68b79dd
into e773d6cc92
commit
ec72476fe8
|
@ -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_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_dir` | The path of the directory containing the currently focused document. Returns current working dir for scratch buffers and failures to evaluate parent. |
|
||||
| `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.|
|
||||
| `selection` | A string containing the contents of the primary selection of the currently focused document. |
|
||||
|
|
|
@ -31,6 +31,8 @@ pub enum Variable {
|
|||
///
|
||||
/// This corresponds to `crate::Document::display_name`.
|
||||
BufferName,
|
||||
/// The directory name housing the currently focused document.
|
||||
BufferDir,
|
||||
/// A string containing the line-ending of the currently focused document.
|
||||
LineEnding,
|
||||
// The name of current buffers language as set in `languages.toml`
|
||||
|
@ -44,6 +46,7 @@ impl Variable {
|
|||
Self::CursorLine,
|
||||
Self::CursorColumn,
|
||||
Self::BufferName,
|
||||
Self::BufferDir,
|
||||
Self::LineEnding,
|
||||
Self::Language,
|
||||
Self::Selection,
|
||||
|
@ -54,6 +57,7 @@ impl Variable {
|
|||
Self::CursorLine => "cursor_line",
|
||||
Self::CursorColumn => "cursor_column",
|
||||
Self::BufferName => "buffer_name",
|
||||
Self::BufferDir => "buffer_dir",
|
||||
Self::LineEnding => "line_ending",
|
||||
Self::Language => "language",
|
||||
Self::Selection => "selection",
|
||||
|
@ -65,6 +69,7 @@ impl Variable {
|
|||
"cursor_line" => Some(Self::CursorLine),
|
||||
"cursor_column" => Some(Self::CursorColumn),
|
||||
"buffer_name" => Some(Self::BufferName),
|
||||
"buffer_dir" => Some(Self::BufferDir),
|
||||
"line_ending" => Some(Self::LineEnding),
|
||||
"language" => Some(Self::Language),
|
||||
"selection" => Some(Self::Selection),
|
||||
|
@ -224,6 +229,20 @@ fn expand_variable(editor: &Editor, variable: Variable) -> Result<Cow<'static, s
|
|||
Ok(Cow::Borrowed(crate::document::SCRATCH_BUFFER_NAME))
|
||||
}
|
||||
}
|
||||
Variable::BufferDir => {
|
||||
// This follows the partial reimpl of `display_name` from `Variable::BufferName`
|
||||
if let Some(path) = doc.relative_path() {
|
||||
if let Some(dir_path) = path.parent() {
|
||||
return Ok(Cow::Owned(dir_path.to_string_lossy().into_owned()));
|
||||
};
|
||||
};
|
||||
// Default to current working dir for scratch buffers and files with no parent dir
|
||||
Ok(Cow::Owned(
|
||||
helix_stdx::env::current_working_dir()
|
||||
.to_string_lossy()
|
||||
.into_owned(),
|
||||
))
|
||||
}
|
||||
Variable::LineEnding => Ok(Cow::Borrowed(doc.line_ending.as_str())),
|
||||
Variable::Language => Ok(match doc.language_name() {
|
||||
Some(lang) => Cow::Owned(lang.to_owned()),
|
||||
|
|
Loading…
Reference in New Issue