Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/expansion.rs')
| -rw-r--r-- | helix-view/src/expansion.rs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/helix-view/src/expansion.rs b/helix-view/src/expansion.rs index d2660dd0..db68c614 100644 --- a/helix-view/src/expansion.rs +++ b/helix-view/src/expansion.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use helix_core::command_line::{ExpansionKind, Token, TokenKind, Tokenizer}; -use anyhow::{anyhow, bail, Result}; +use anyhow::{anyhow, bail, ensure, Result}; use crate::Editor; @@ -128,6 +128,7 @@ pub fn expand<'a>(editor: &Editor, token: Token<'a>) -> Result<Cow<'a, str>> { } TokenKind::Expand => expand_inner(editor, token.content), TokenKind::Expansion(ExpansionKind::Shell) => expand_shell(editor, token.content), + TokenKind::Expansion(ExpansionKind::Register) => expand_register(editor, token.content), // Note: see the docs for this variant. TokenKind::ExpansionKind => unreachable!( "expansion name tokens cannot be emitted when command line validation is enabled" @@ -183,6 +184,22 @@ pub fn expand_shell<'a>(editor: &Editor, content: Cow<'a, str>) -> Result<Cow<'a Ok(Cow::Owned(text)) } +/// Expand a register's contents +pub fn expand_register<'a>(editor: &Editor, content: Cow<'a, str>) -> Result<Cow<'a, str>> { + let mut chars = content.chars(); + let Some(r) = chars.next() else { + bail!("Missing register name"); + }; + ensure!( + chars.next().is_none(), + "Invalid register `{content}`: should only be a single character" + ); + match editor.registers.read(r, editor) { + Some(values) => Ok(Cow::Owned(values.collect())), + None => Ok(Cow::Borrowed("")), + } +} + /// Expand a token's contents recursively. fn expand_inner<'a>(editor: &Editor, content: Cow<'a, str>) -> Result<Cow<'a, str>> { let mut escaped = String::new(); |