Skip to content

Commit

Permalink
Add lang and selection variables for command expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
irh committed Jan 21, 2025
1 parent 840f408 commit c5679f3
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions helix-view/src/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub enum Variable {
BufferName,
/// A string containing the line-ending of the currently focused document.
LineEnding,
/// The language of the currently focused document.
Lang,
/// The currently focused document's selection.
Selection,
}

impl Variable {
Expand All @@ -41,6 +45,8 @@ impl Variable {
Self::CursorColumn,
Self::BufferName,
Self::LineEnding,
Self::Lang,
Self::Selection,
];

pub const fn as_str(&self) -> &'static str {
Expand All @@ -49,6 +55,8 @@ impl Variable {
Self::CursorColumn => "cursor_column",
Self::BufferName => "buffer_name",
Self::LineEnding => "line_ending",
Self::Lang => "lang",
Self::Selection => "selection",
}
}

Expand All @@ -58,6 +66,8 @@ impl Variable {
"cursor_column" => Some(Self::CursorColumn),
"buffer_name" => Some(Self::BufferName),
"line_ending" => Some(Self::LineEnding),
"lang" => Some(Self::Lang),
"selection" => Some(Self::Selection),
_ => None,
}
}
Expand Down Expand Up @@ -219,5 +229,14 @@ fn expand_variable(editor: &Editor, variable: Variable) -> Result<Cow<'static, s
}
}
Variable::LineEnding => Ok(Cow::Borrowed(doc.line_ending.as_str())),
Variable::Lang => Ok(Cow::Owned(
doc.language_name().unwrap_or("text").to_string(),
)),
Variable::Selection => Ok(Cow::Owned(
doc.selection(view.id)
.primary()
.fragment(doc.text().slice(..))
.to_string(),
)),
}
}

0 comments on commit c5679f3

Please sign in to comment.