Skip to content

Commit

Permalink
Make ansi-str optional
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Aug 24, 2024
1 parent 92bcae0 commit 271052d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ with-sqlite-history = ["rusqlite"]
with-fuzzy = ["skim"]
case_insensitive_history_search = ["regex"]
# For continuation prompt, indentation, scrolling
# TODO make ansi-str optional
split-highlight = ["ansi-str"]
split-highlight = []

[[example]]
name = "custom_key_bindings"
Expand Down
29 changes: 23 additions & 6 deletions src/highlight.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Syntax highlighting
use crate::config::CompletionType;
use std::borrow::Cow::{self, Borrowed, Owned};
use std::borrow::Cow::{self, Borrowed};
use std::cell::Cell;
#[cfg(feature = "split-highlight")]
use std::fmt::Display;
Expand All @@ -15,6 +15,18 @@ pub trait Style {
/// Produce a ansi sequences which ends the graphic mode
fn end(&self) -> impl Display;
}

#[cfg(feature = "split-highlight")]
impl Style for () {
fn start(&self) -> impl Display {
""
}

fn end(&self) -> impl Display {
""
}
}

#[cfg(feature = "ansi-str")]
#[cfg_attr(docsrs, doc(cfg(feature = "ansi-str")))]
impl Style for ansi_str::Style {
Expand Down Expand Up @@ -121,7 +133,7 @@ pub trait Highlighter {
Self: Sized,
{
let _ = pos;
// TODO default style vs empty vec to indicated no highlighting
// TODO default style vs empty vec to indicate no highlighting
vec![(Self::Style::default(), line)]
}

Expand Down Expand Up @@ -165,10 +177,15 @@ pub trait Highlighter {
}
}

#[cfg(any(not(feature = "split-highlight"), feature = "ansi-str"))] // FIXME
impl Highlighter for () {}
impl Highlighter for () {
#[cfg(all(feature = "split-highlight", not(feature = "ansi-str")))]
type Style = ();
}

impl<'r, H: Highlighter> Highlighter for &'r H {
#[cfg(all(feature = "split-highlight", not(feature = "ansi-str")))]
type Style = H::Style;

impl<'r, H: ?Sized + Highlighter> Highlighter for &'r H {
#[cfg(any(not(feature = "split-highlight"), feature = "ansi-str"))]
fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> {
(**self).highlight(line, pos)
Expand Down Expand Up @@ -225,8 +242,8 @@ impl MatchingBracketHighlighter {
}
}

#[cfg(any(not(feature = "split-highlight"), feature = "ansi-str"))]
impl Highlighter for MatchingBracketHighlighter {
#[cfg(any(not(feature = "split-highlight"), feature = "ansi-str"))]
fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> {
if line.len() <= 1 {
return Borrowed(line);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ where

impl Helper for () {}

impl<'h, H: ?Sized + Helper> Helper for &'h H {}
impl<'h, H: Helper> Helper for &'h H {}

/// Completion/suggestion context
pub struct Context<'h> {
Expand Down
5 changes: 4 additions & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ impl Hinter for SimpleCompleter {
}

impl Helper for SimpleCompleter {}
impl Highlighter for SimpleCompleter {}
impl Highlighter for SimpleCompleter {
#[cfg(all(feature = "split-highlight", not(feature = "ansi-str")))]
type Style = ();
}
impl Validator for SimpleCompleter {}

#[test]
Expand Down

0 comments on commit 271052d

Please sign in to comment.