Something went wrong. Try again.
wip text editor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639use bitflags::bitflags;use crossterm::style::Color;use gen_lsp_types::DiagnosticSeverity;
use crate::highlight::TokenKind;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub(crate) struct Style { foreground: Option<Color>, background: Option<Color>, underline_color: Option<Color>, attributes: StyleAttributes,}
impl Style { pub(crate) const BACKGROUND: Self = Self::new().with_bg(colors::LIGHT0); pub(crate) const COMMAND_LIST: Self = Self::new().with_bg(colors::LIGHT1); pub(crate) const COMMAND_LIST_BORDER: Self = Self::new().with_fg(colors::DARK1); pub(crate) const COMMAND_LIST_INPUT_TEXT: Self = Self::new().with_fg(colors::DARK1); pub(crate) const COMMAND_LIST_ITEM: Self = Self::new().with_fg(colors::DARK1); pub(crate) const COMMAND_LIST_ITEM_SELECTED: Self = Self::new().with_fg(colors::LIGHT0).with_bg(colors::DARK1); pub(crate) const DIFF_ADDED: Self = Self::new().with_fg(colors::FADED_GREEN); pub(crate) const DIFF_REMOVED: Self = Self::new().with_fg(colors::FADED_RED); pub(crate) const FILTER_MATCH: Self = Self::new() .with_fg(colors::LIGHT0) .with_bg(colors::FADED_BLUE); pub(crate) const GUTTER: Self = Self::new().with_fg(colors::LIGHT4).with_bg(colors::LIGHT0); pub(crate) const GUTTER_SELECTED: Self = Self::new() .with_fg(colors::FADED_YELLOW) .with_bg(colors::LIGHT0); pub(crate) const HINTS: Self = Self::new().with_fg(colors::DARK1).with_bg(colors::LIGHT1); pub(crate) const STATUS_LINE: Self = Self::new().with_bg(colors::LIGHT1); pub(crate) const STATUS_LINE_ERROR: Self = Self::new().with_fg(colors::FADED_RED); pub(crate) const STATUS_LINE_MESSAGES: Self = Self::new().with_bg(colors::LIGHT0); pub(crate) const STATUS_LINE_MODE: Self = Self::new() .with_fg(colors::LIGHT0) .with_bg(colors::FADED_AQUA); pub(crate) const STATUS_LINE_TEXT: Self = Self::new().with_fg(colors::DARK1); pub(crate) const TEXT: Self = Self::new().with_fg(colors::DARK1); pub(crate) const TEXT_SELECTED: Self = Self::new().with_bg(colors::LIGHT3);
pub(crate) const fn new() -> Self { Self { foreground: None, background: None, underline_color: None, attributes: StyleAttributes::empty(), } }
pub(crate) const fn with_fg(self, foreground: Color) -> Self { Self { foreground: Some(foreground), ..self } }
pub(crate) const fn with_bg(self, background: Color) -> Self { Self { background: Some(background), ..self } }
pub(crate) const fn with_attributes(self, attributes: StyleAttributes) -> Self { Self { attributes, ..self } }
pub(crate) const fn with_undercurl(self, color: Color) -> Self { Self { underline_color: Some(color), attributes: self.attributes.union(StyleAttributes::Undercurled), ..self } }
pub(crate) const fn foreground(self) -> Option<Color> { self.foreground }
pub(crate) const fn background(self) -> Option<Color> { self.background }
pub(crate) const fn underline_color(self) -> Option<Color> { self.underline_color }
pub(crate) const fn attributes(&self) -> StyleAttributes { self.attributes }
/// Merges `other` into `self`. For each style in `other`: /// - if style is `Some`, overwrite /// - otherwise, do not overwrite pub(crate) fn merge(self, other: Self) -> Self { Self { foreground: other.foreground.or(self.foreground), background: other.background.or(self.background), underline_color: other.underline_color.or(self.underline_color), attributes: self.attributes | other.attributes, } }
pub(crate) const fn diagnostic(severity: DiagnosticSeverity) -> Self { Self::new().with_undercurl(diagnostic_color(severity)) }
pub(crate) const fn diagnostic_icon(severity: DiagnosticSeverity) -> Self { Self::new().with_fg(diagnostic_color(severity)) }}
impl From<TokenKind> for Style { fn from(kind: TokenKind) -> Self { match kind { TokenKind::Identifier | TokenKind::Unknown => Self::TEXT, TokenKind::Keyword => Self::new().with_fg(colors::FADED_RED), TokenKind::Lifetime | TokenKind::Macro => Self::new().with_fg(colors::FADED_AQUA), TokenKind::String => { Self::new() .with_fg(colors::FADED_GREEN) .with_attributes(StyleAttributes::Italic) } TokenKind::Type => Self::new().with_fg(colors::FADED_YELLOW), TokenKind::Comment => { Self::new() .with_fg(colors::GRAY) .with_attributes(StyleAttributes::Italic) } TokenKind::Operator | TokenKind::Punctuation => { Self::new().with_fg(colors::FADED_ORANGE) } TokenKind::Character | TokenKind::Number | TokenKind::Constant | TokenKind::EnumMember => Self::new().with_fg(colors::FADED_PURPLE), TokenKind::FunctionName => { Self::new() .with_fg(colors::FADED_GREEN) .with_attributes(StyleAttributes::Bold) } TokenKind::Title => { Self::new() .with_fg(colors::FADED_PURPLE) .with_attributes(StyleAttributes::Bold) } TokenKind::Property | TokenKind::PropertyAccess => { Self::new().with_fg(colors::FADED_BLUE) } TokenKind::Marker => { Self::new() .with_fg(colors::LIGHT0) .with_bg(colors::FADED_AQUA) } TokenKind::Whitespace => Self::new(), } }}
const fn diagnostic_color(severity: DiagnosticSeverity) -> Color { match severity { DiagnosticSeverity::Error => colors::FADED_RED, DiagnosticSeverity::Warning => colors::FADED_YELLOW, DiagnosticSeverity::Information => colors::FADED_BLUE, DiagnosticSeverity::Hint => colors::FADED_GREEN, }}
bitflags! { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) struct StyleAttributes: u8 { const Bold = 0b0000_0001; const Italic = 0b0000_0010; const Underlined = 0b0000_0100; const Undercurled = 0b0000_1000; }}
#[cfg(test)]mod tests { use super::*;
#[test] fn merge_keeps_original_when_other_is_empty() { let base = Style { foreground: Some(Color::Red), background: Some(Color::Black), underline_color: None, attributes: StyleAttributes::Bold, };
let other = Style { foreground: None, background: None, underline_color: None, attributes: StyleAttributes::empty(), };
let merged = base.merge(other);
assert_eq!(merged, Style { foreground: Some(Color::Red), background: Some(Color::Black), underline_color: None, attributes: StyleAttributes::Bold, }); }
#[test] fn merge_overwrites_foreground_when_present() { let base = Style { foreground: Some(Color::Red), background: Some(Color::Black), underline_color: None, attributes: StyleAttributes::Italic, };
let other = Style { foreground: Some(Color::Blue), background: None, underline_color: None, attributes: StyleAttributes::empty(), };
let merged = base.merge(other);
assert_eq!(merged, Style { foreground: Some(Color::Blue), background: Some(Color::Black), underline_color: None, attributes: StyleAttributes::Italic, }); }
#[test] fn merge_overwrites_background_when_present() { let base = Style { foreground: Some(Color::White), background: Some(Color::Black), underline_color: None, attributes: StyleAttributes::Underlined, };
let other = Style { foreground: None, background: Some(Color::Green), underline_color: None, attributes: StyleAttributes::empty(), };
let merged = base.merge(other);
assert_eq!(merged, Style { foreground: Some(Color::White), background: Some(Color::Green), underline_color: None, attributes: StyleAttributes::Underlined }); }
#[test] fn merge_overwrites_all_colors_when_present() { let base = Style { foreground: Some(Color::Red), background: Some(Color::Black), underline_color: Some(Color::Yellow), attributes: StyleAttributes::Bold | StyleAttributes::Undercurled, };
let other = Style { foreground: Some(Color::Blue), background: Some(Color::White), underline_color: Some(Color::Green), attributes: StyleAttributes::Italic | StyleAttributes::Undercurled, };
let merged = base.merge(other);
assert_eq!(merged, Style { foreground: Some(Color::Blue), background: Some(Color::White), underline_color: Some(Color::Green), attributes: StyleAttributes::Bold | StyleAttributes::Italic | StyleAttributes::Undercurled, }); }
#[test] fn merge_can_fill_missing_fields() { let base = Style { foreground: None, background: Some(Color::Black), underline_color: None, attributes: StyleAttributes::empty(), };
let other = Style { foreground: Some(Color::Green), background: None, underline_color: None, attributes: StyleAttributes::Bold, };
let merged = base.merge(other);
assert_eq!(merged, Style { foreground: Some(Color::Green), background: Some(Color::Black), underline_color: None, attributes: StyleAttributes::Bold, }); }
#[test] fn merge_with_all_none_results_in_no_changes() { let base = Style { foreground: None, background: None, underline_color: None, attributes: StyleAttributes::empty(), };
let other = Style { foreground: None, background: None, underline_color: None, attributes: StyleAttributes::empty(), };
let merged = base.merge(other);
assert_eq!(merged, Style { foreground: None, background: None, underline_color: None, attributes: StyleAttributes::empty(), }); }
#[test] fn merge_combines_attributes() { let base = Style { foreground: None, background: None, underline_color: None, attributes: StyleAttributes::Bold, };
let other = Style { foreground: None, background: None, underline_color: None, attributes: StyleAttributes::Italic | StyleAttributes::Underlined, };
let merged = base.merge(other);
assert_eq!(merged, Style { foreground: None, background: None, underline_color: None, attributes: StyleAttributes::Bold | StyleAttributes::Italic | StyleAttributes::Underlined }); }
#[test] fn with_undercurl_sets_color_and_attribute() { let style = Style::new() .with_attributes(StyleAttributes::Bold) .with_undercurl(Color::Red);
assert_eq!(style.underline_color(), Some(Color::Red)); assert_eq!( style.attributes(), StyleAttributes::Bold | StyleAttributes::Undercurled ); }}
/// <https://github.com/ellisonleao/gruvbox.nvim/blob/main/lua/gruvbox.lua>.mod colors {
use crossterm::style::Color;
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const DARK0: Color = Color::Rgb { r: 29, g: 32, b: 33, };
pub(super) const DARK1: Color = Color::Rgb { r: 60, g: 56, b: 54, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const DARK2: Color = Color::Rgb { r: 80, g: 73, b: 69, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const DARK3: Color = Color::Rgb { r: 102, g: 92, b: 84, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const DARK4: Color = Color::Rgb { r: 124, g: 111, b: 100, };
pub(super) const LIGHT0: Color = Color::Rgb { r: 249, g: 245, b: 215, };
pub(super) const LIGHT1: Color = Color::Rgb { r: 235, g: 219, b: 178, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const LIGHT2: Color = Color::Rgb { r: 213, g: 196, b: 161, };
pub(super) const LIGHT3: Color = Color::Rgb { r: 189, g: 174, b: 147, };
pub(super) const LIGHT4: Color = Color::Rgb { r: 168, g: 153, b: 132, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const BRIGHT_RED: Color = Color::Rgb { r: 251, g: 73, b: 52, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const BRIGHT_GREEN: Color = Color::Rgb { r: 184, g: 187, b: 38, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const BRIGHT_YELLOW: Color = Color::Rgb { r: 250, g: 189, b: 47, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const BRIGHT_BLUE: Color = Color::Rgb { r: 131, g: 165, b: 152, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const BRIGHT_PURPLE: Color = Color::Rgb { r: 211, g: 134, b: 155, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const BRIGHT_AQUA: Color = Color::Rgb { r: 142, g: 192, b: 124, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const BRIGHT_ORANGE: Color = Color::Rgb { r: 254, g: 128, b: 25, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const NEUTRAL_RED: Color = Color::Rgb { r: 204, g: 36, b: 29, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const NEUTRAL_GREEN: Color = Color::Rgb { r: 152, g: 151, b: 26, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const NEUTRAL_YELLOW: Color = Color::Rgb { r: 215, g: 153, b: 33, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const NEUTRAL_BLUE: Color = Color::Rgb { r: 69, g: 133, b: 136, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const NEUTRAL_PURPLE: Color = Color::Rgb { r: 177, g: 98, b: 134, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const NEUTRAL_AQUA: Color = Color::Rgb { r: 104, g: 157, b: 106, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const NEUTRAL_ORANGE: Color = Color::Rgb { r: 214, g: 93, b: 14, };
pub(super) const FADED_RED: Color = Color::Rgb { r: 157, g: 0, b: 6 };
pub(super) const FADED_GREEN: Color = Color::Rgb { r: 121, g: 116, b: 14, };
pub(super) const FADED_YELLOW: Color = Color::Rgb { r: 181, g: 118, b: 20, };
pub(super) const FADED_BLUE: Color = Color::Rgb { r: 7, g: 102, b: 120, };
pub(super) const FADED_PURPLE: Color = Color::Rgb { r: 143, g: 63, b: 113, };
pub(super) const FADED_AQUA: Color = Color::Rgb { r: 66, g: 123, b: 88, };
pub(super) const FADED_ORANGE: Color = Color::Rgb { r: 175, g: 58, b: 3, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const DARK_RED: Color = Color::Rgb { r: 121, g: 35, b: 41, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const LIGHT_RED: Color = Color::Rgb { r: 252, g: 150, b: 144, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const DARK_GREEN: Color = Color::Rgb { r: 90, g: 99, b: 58, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const LIGHT_GREEN: Color = Color::Rgb { r: 211, g: 214, b: 165, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const DARK_AQUA: Color = Color::Rgb { r: 62, g: 73, b: 52, };
#[expect(unused, reason = "unused colours may be useful in future")] pub(super) const LIGHT_AQUA: Color = Color::Rgb { r: 230, g: 233, b: 193, };
pub(super) const GRAY: Color = Color::Rgb { r: 146, g: 131, b: 116, };}