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

feat: add register for absolute path of the current file #12887

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
3 changes: 2 additions & 1 deletion book/src/registers.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ Some registers have special behavior when read from and written to.
| `_` | No values are returned | All values are discarded |
| `#` | Selection indices (first selection is `1`, second is `2`, etc.) | This register is not writable |
| `.` | Contents of the current selections | This register is not writable |
| `%` | Name of the current file | This register is not writable |
| `%` | Name of the current file, relative to Helix's current working directory | This register is not writable |
| `=` | Absolute path to the current file | This register is not writable |
| `+` | Reads from the system clipboard | Joins and yanks to the system clipboard |
| `*` | Reads from the primary clipboard | Joins and yanks to the primary clipboard |

Expand Down
28 changes: 21 additions & 7 deletions helix-view/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::{
/// * Black hole (`_`): all values read and written are discarded
/// * Selection indices (`#`): index number of each selection starting at 1
/// * Selection contents (`.`)
/// * Document path (`%`): filename of the current buffer
/// * Relative document path (`%`): filename of the current buffer, relative to Helix's current working directory
/// * Absolute document path (`%`): absolute path of the current buffer
/// * System clipboard (`*`)
/// * Primary clipboard (`+`)
pub struct Registers {
Expand Down Expand Up @@ -57,8 +58,16 @@ impl Registers {
let text = doc.text().slice(..);
Some(RegisterValues::new(doc.selection(view.id).fragments(text)))
}
'%' => {
let path = doc!(editor).display_name();
'%' | '=' => {
let doc = doc!(editor);
let path = match name {
'%' => doc.display_name(),
'=' => doc.path().map_or_else(
|| crate::document::SCRATCH_BUFFER_NAME.into(),
|path| path.to_string_lossy(),
),
_ => unreachable!(),
};
Some(RegisterValues::new(iter::once(path)))
}
'*' | '+' => Some(read_from_clipboard(
Expand All @@ -80,7 +89,9 @@ impl Registers {
pub fn write(&mut self, name: char, mut values: Vec<String>) -> Result<()> {
match name {
'_' => Ok(()),
'#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support writing")),
'#' | '.' | '%' | '=' => {
Err(anyhow::anyhow!("Register {name} does not support writing"))
}
'*' | '+' => {
self.clipboard_provider.load().set_contents(
&values.join(NATIVE_LINE_ENDING.as_str()),
Expand All @@ -105,7 +116,9 @@ impl Registers {
pub fn push(&mut self, name: char, mut value: String) -> Result<()> {
match name {
'_' => Ok(()),
'#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support pushing")),
'#' | '.' | '%' | '=' => {
Err(anyhow::anyhow!("Register {name} does not support pushing"))
}
'*' | '+' => {
let clipboard_type = match name {
'+' => ClipboardType::Clipboard,
Expand Down Expand Up @@ -165,7 +178,8 @@ impl Registers {
('_', "<empty>"),
('#', "<selection indices>"),
('.', "<selection contents>"),
('%', "<document path>"),
('%', "<relative document path>"),
('=', "<absolute document path>"),
('+', "<system clipboard>"),
('*', "<primary clipboard>"),
]
Expand All @@ -192,7 +206,7 @@ impl Registers {

true
}
'_' | '#' | '.' | '%' => false,
'_' | '#' | '.' | '%' | '=' => false,
_ => self.inner.remove(&name).is_some(),
}
}
Expand Down