working syntax highlighting

This commit is contained in:
Priec
2025-08-19 00:51:57 +02:00
parent 3fdb7e4e37
commit 0215f2824a

View File

@@ -7,7 +7,7 @@ use std::hash::{Hash, Hasher};
use ratatui::style::{Modifier, Style}; use ratatui::style::{Modifier, Style};
use syntect::{ use syntect::{
highlighting::{ highlighting::{
HighlightIterator, Highlighter, Style as SynStyle, Theme, ThemeSet, HighlightIterator, HighlightState, Highlighter, Style as SynStyle, Theme, ThemeSet,
}, },
parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet}, parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet},
}; };
@@ -160,8 +160,8 @@ impl SyntectEngine {
} }
let syntax = self.syntax_ref(); let syntax = self.syntax_ref();
let theme = self.theme(); let theme = self.theme().clone(); // Clone to avoid borrow conflicts
let highlighter = Highlighter::new(theme); let highlighter = Highlighter::new(&theme);
let mut ps = if self.parse_after.is_empty() { let mut ps = if self.parse_after.is_empty() {
ParseState::new(syntax) ParseState::new(syntax)
@@ -177,13 +177,25 @@ impl SyntectEngine {
let start = self.parse_after.len(); let start = self.parse_after.len();
for i in start..line_idx { for i in start..line_idx {
let s = provider.field_value(i); let s = provider.field_value(i);
let ops = ps.parse_line(s);
// Advance stack by applying ops using HighlightIterator // Fix: parse_line takes 2 arguments: line and &SyntaxSet
let mut it = HighlightIterator::new(&highlighter, &ops[..], s, &mut stack); let ops = match ps.parse_line(s, &self.ps) {
Ok(ops) => ops,
Err(_) => Vec::new(), // Handle parsing errors gracefully
};
// Fix: HighlightState::new requires &Highlighter and ScopeStack
let mut highlight_state = HighlightState::new(&highlighter, stack.clone());
// Fix: HighlightIterator::new expects &mut HighlightState as first parameter
let mut it = HighlightIterator::new(&mut highlight_state, &ops[..], s, &highlighter);
while let Some((_style, _text)) = it.next() { while let Some((_style, _text)) = it.next() {
// Iterate to apply ops; we don't need the tokens here. // Iterate to apply ops; we don't need the tokens here.
} }
// Update the stack from the highlight state
stack = highlight_state.path.clone();
let h = Self::hash_line(s); let h = Self::hash_line(s);
self.parse_after.push(ps.clone()); self.parse_after.push(ps.clone());
@@ -209,8 +221,8 @@ impl SyntectEngine {
self.ensure_state_before(line_idx, provider); self.ensure_state_before(line_idx, provider);
let syntax = self.syntax_ref(); let syntax = self.syntax_ref();
let theme = self.theme(); let theme = self.theme().clone(); // Clone to avoid borrow conflicts
let highlighter = Highlighter::new(theme); let highlighter = Highlighter::new(&theme);
let mut ps = if line_idx == 0 { let mut ps = if line_idx == 0 {
ParseState::new(syntax) ParseState::new(syntax)
@@ -220,7 +232,7 @@ impl SyntectEngine {
ParseState::new(syntax) ParseState::new(syntax)
}; };
let mut stack = if line_idx == 0 { let stack = if line_idx == 0 {
ScopeStack::new() ScopeStack::new()
} else if self.stack_after.len() >= line_idx { } else if self.stack_after.len() >= line_idx {
self.stack_after[line_idx - 1].clone() self.stack_after[line_idx - 1].clone()
@@ -228,8 +240,17 @@ impl SyntectEngine {
ScopeStack::new() ScopeStack::new()
}; };
let ops = ps.parse_line(line); // Fix: parse_line takes 2 arguments: line and &SyntaxSet
let mut iter = HighlightIterator::new(&highlighter, &ops[..], line, &mut stack); let ops = match ps.parse_line(line, &self.ps) {
Ok(ops) => ops,
Err(_) => Vec::new(), // Handle parsing errors gracefully
};
// Fix: HighlightState::new requires &Highlighter and ScopeStack
let mut highlight_state = HighlightState::new(&highlighter, stack);
// Fix: HighlightIterator::new expects &mut HighlightState as first parameter
let mut iter = HighlightIterator::new(&mut highlight_state, &ops[..], line, &highlighter);
let mut out: Vec<StyledChunk> = Vec::new(); let mut out: Vec<StyledChunk> = Vec::new();
while let Some((syn_style, slice)) = iter.next() { while let Some((syn_style, slice)) = iter.next() {
@@ -253,11 +274,15 @@ impl SyntectEngine {
} else { } else {
self.parse_after[line_idx] = ps; self.parse_after[line_idx] = ps;
} }
// Update stack from highlight state
let final_stack = highlight_state.path.clone();
if line_idx >= self.stack_after.len() { if line_idx >= self.stack_after.len() {
self.stack_after.push(stack); self.stack_after.push(final_stack);
} else { } else {
self.stack_after[line_idx] = stack; self.stack_after[line_idx] = final_stack;
} }
if line_idx >= self.line_hashes.len() { if line_idx >= self.line_hashes.len() {
self.line_hashes.push(h); self.line_hashes.push(h);
} else { } else {