From 271052d46165d529c59d1c64d58665a37173f308 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 24 Aug 2024 10:32:00 +0200 Subject: [PATCH] Make ansi-str optional --- Cargo.toml | 3 +-- src/highlight.rs | 29 +++++++++++++++++++++++------ src/lib.rs | 2 +- src/test/mod.rs | 5 ++++- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a9847c984..1d5851e5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/highlight.rs b/src/highlight.rs index 4c2f2ab7e..228d72976 100644 --- a/src/highlight.rs +++ b/src/highlight.rs @@ -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; @@ -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 { @@ -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)] } @@ -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) @@ -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); diff --git a/src/lib.rs b/src/lib.rs index 10fd4b68a..b9e2f0fb2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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> { diff --git a/src/test/mod.rs b/src/test/mod.rs index 14ff9052b..18d45244e 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -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]