Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add absolute file path command expansion value #12989

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
| `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. |

Aside from editor variables, the following expansions may be used:
Expand Down
15 changes: 15 additions & 0 deletions helix-view/src/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub enum Variable {
///
/// This corresponds to `crate::Document::display_name`.
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.
LineEnding,
}
Expand All @@ -40,6 +43,7 @@ impl Variable {
Self::CursorLine,
Self::CursorColumn,
Self::BufferName,
Self::FilePathAbsolute,
Self::LineEnding,
];

Expand All @@ -48,6 +52,7 @@ impl Variable {
Self::CursorLine => "cursor_line",
Self::CursorColumn => "cursor_column",
Self::BufferName => "buffer_name",
Self::FilePathAbsolute => "file_path_absolute",
Self::LineEnding => "line_ending",
}
}
Expand All @@ -57,6 +62,7 @@ impl Variable {
"cursor_line" => Some(Self::CursorLine),
"cursor_column" => Some(Self::CursorColumn),
"buffer_name" => Some(Self::BufferName),
"file_path_absolute" => Some(Self::FilePathAbsolute),
"line_ending" => Some(Self::LineEnding),
_ => None,
}
Expand Down Expand Up @@ -214,6 +220,15 @@ fn expand_variable(editor: &Editor, variable: Variable) -> Result<Cow<'static, s
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())),
}
}