From db39c05aca90e971f43caa668fd5aae26d64933c Mon Sep 17 00:00:00 2001 From: wiru Date: Wed, 1 May 2024 02:47:25 -0300 Subject: [PATCH] feat: syntax highlighting working as expected --- colors/src/colors.rs | 19 +++++++++++++------ reqtui/src/syntax/highlighter.rs | 2 +- reqtui/src/text_object/text_object.rs | 18 +++++++++++------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/colors/src/colors.rs b/colors/src/colors.rs index ee25163..062db8c 100644 --- a/colors/src/colors.rs +++ b/colors/src/colors.rs @@ -23,14 +23,21 @@ impl Default for Colors { fn token_highlight() -> HashMap { let mut tokens = HashMap::new(); - let colors = NormalColors::default(); + let colors = BrightColors::default(); tokens.insert("conceal".into(), Style::new().fg(colors.red)); - tokens.insert("number".into(), Style::new().fg(colors.blue)); - tokens.insert("property".into(), Style::new().fg(colors.green)); - tokens.insert("punctuation.bracket".into(), Style::new().fg(colors.yellow)); - tokens.insert("punctuation.delimiter".into(), Style::new().fg(colors.cyan)); - tokens.insert("string".into(), Style::new().fg(colors.magenta)); + tokens.insert("boolean".into(), Style::new().fg(colors.red)); + tokens.insert("number".into(), Style::new().fg(colors.yellow)); + tokens.insert("property".into(), Style::new().fg(colors.blue)); + tokens.insert( + "punctuation.bracket".into(), + Style::new().fg(colors.magenta), + ); + tokens.insert( + "punctuation.delimiter".into(), + Style::new().fg(colors.magenta), + ); + tokens.insert("string".into(), Style::new().fg(colors.green)); tokens } diff --git a/reqtui/src/syntax/highlighter.rs b/reqtui/src/syntax/highlighter.rs index 786991a..50e6af3 100644 --- a/reqtui/src/syntax/highlighter.rs +++ b/reqtui/src/syntax/highlighter.rs @@ -43,7 +43,7 @@ impl Default for Highlighter { } impl Highlighter { - pub fn parse<'a>(&mut self, buffer: &str) -> Option { + pub fn parse(&mut self, buffer: &str) -> Option { self.parser.parse(buffer, None) } diff --git a/reqtui/src/text_object/text_object.rs b/reqtui/src/text_object/text_object.rs index 4146c78..f754fd2 100644 --- a/reqtui/src/text_object/text_object.rs +++ b/reqtui/src/text_object/text_object.rs @@ -1,9 +1,11 @@ +use std::str::{Chars, FromStr}; + use ratatui::{ - style::Styled, + style::{Color, Style, Styled}, text::{Line, Span}, widgets::Paragraph, }; -use ropey::{iter::Chars, Rope}; +use ropey::Rope; use crate::syntax::highlighter::ColorInfo; @@ -38,14 +40,10 @@ impl TextObject { } impl TextObject { - pub fn chars(&self) -> Chars<'_> { - self.content.chars() - } - pub fn with_highlight(self, colors: Vec) -> Self { let mut lines: Vec = vec![]; let mut current_line: Vec = vec![]; - for (idx, c) in self.chars().enumerate() { + for (idx, c) in self.to_string().chars().enumerate() { let style = colors .iter() .find(|color| color.start <= idx && color.end >= idx) @@ -69,3 +67,9 @@ impl TextObject { } } } + +impl std::fmt::Display for TextObject { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.content.to_string()) + } +}