Skip to content

Commit

Permalink
feat: syntax highlighting working as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 1, 2024
1 parent 5c37430 commit db39c05
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
19 changes: 13 additions & 6 deletions colors/src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ impl Default for Colors {

fn token_highlight() -> HashMap<String, Style> {
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
}
Expand Down
2 changes: 1 addition & 1 deletion reqtui/src/syntax/highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Default for Highlighter {
}

impl Highlighter {
pub fn parse<'a>(&mut self, buffer: &str) -> Option<Tree> {
pub fn parse(&mut self, buffer: &str) -> Option<Tree> {
self.parser.parse(buffer, None)
}

Expand Down
18 changes: 11 additions & 7 deletions reqtui/src/text_object/text_object.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -38,14 +40,10 @@ impl TextObject<Readonly> {
}

impl TextObject {
pub fn chars(&self) -> Chars<'_> {
self.content.chars()
}

pub fn with_highlight(self, colors: Vec<ColorInfo>) -> Self {
let mut lines: Vec<Line> = vec![];
let mut current_line: Vec<Span> = 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)
Expand All @@ -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())
}
}

0 comments on commit db39c05

Please sign in to comment.