This commit is contained in:
sxyazi 2024-08-05 13:18:10 +08:00
parent ed806bdced
commit 3c69104d33
No known key found for this signature in database
2 changed files with 6 additions and 6 deletions

View file

@ -3,7 +3,7 @@ use std::ops::Range;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use ratatui::{buffer::Buffer, layout::Rect, text::Line, widgets::{Block, BorderType, Paragraph, Widget}}; use ratatui::{buffer::Buffer, layout::Rect, text::Line, widgets::{Block, BorderType, Paragraph, Widget}};
use syntect::easy::HighlightLines; use syntect::easy::HighlightLines;
use yazi_config::THEME; use yazi_config::{PREVIEW, THEME};
use yazi_core::input::InputMode; use yazi_core::input::InputMode;
use yazi_plugin::external::Highlighter; use yazi_plugin::external::Highlighter;
@ -25,7 +25,7 @@ impl<'a> Input<'a> {
if let Some(syntax) = syntaxes.find_syntax_by_name("Bourne Again Shell (bash)") { if let Some(syntax) = syntaxes.find_syntax_by_name("Bourne Again Shell (bash)") {
let mut h = HighlightLines::new(syntax, theme); let mut h = HighlightLines::new(syntax, theme);
let regions = h.highlight_line(self.cx.input.value(), syntaxes)?; let regions = h.highlight_line(self.cx.input.value(), syntaxes)?;
return Ok(Highlighter::to_line_widget(regions)); return Ok(Highlighter::to_line_widget(regions, &" ".repeat(PREVIEW.tab_size as usize)));
} }
bail!("Failed to find syntax") bail!("Failed to find syntax")
} }

View file

@ -121,6 +121,7 @@ impl Highlighter {
h.highlight_line(&line, syntaxes).map_err(|e| anyhow!(e))?; h.highlight_line(&line, syntaxes).map_err(|e| anyhow!(e))?;
} }
let indent = " ".repeat(PREVIEW.tab_size as usize);
let mut lines = Vec::with_capacity(after.len()); let mut lines = Vec::with_capacity(after.len());
for line in after { for line in after {
if ticket != INCR.load(Ordering::Relaxed) { if ticket != INCR.load(Ordering::Relaxed) {
@ -128,7 +129,7 @@ impl Highlighter {
} }
let regions = h.highlight_line(&line, syntaxes).map_err(|e| anyhow!(e))?; let regions = h.highlight_line(&line, syntaxes).map_err(|e| anyhow!(e))?;
lines.push(Self::to_line_widget(regions)); lines.push(Self::to_line_widget(regions, &indent));
} }
Ok(Text::from(lines)) Ok(Text::from(lines))
@ -181,8 +182,7 @@ impl Highlighter {
} }
} }
pub fn to_line_widget(regions: Vec<(highlighting::Style, &str)>) -> Line<'static> { pub fn to_line_widget(regions: Vec<(highlighting::Style, &str)>, indent: &str) -> Line<'static> {
let indent = " ".repeat(PREVIEW.tab_size as usize);
let spans: Vec<_> = regions let spans: Vec<_> = regions
.into_iter() .into_iter()
.map(|(style, s)| { .map(|(style, s)| {
@ -198,7 +198,7 @@ impl Highlighter {
} }
Span { Span {
content: s.replace('\t', &indent).into(), content: s.replace('\t', indent).into(),
style: ratatui::style::Style { style: ratatui::style::Style {
fg: Self::to_ansi_color(style.foreground), fg: Self::to_ansi_color(style.foreground),
// bg: Self::to_ansi_color(style.background), // bg: Self::to_ansi_color(style.background),