Files
Kigi-CLI/crates/codegen/kigi-pager-render/src/syntax.rs
T
ZacharyZhang-NY a02b555e66 docs(comments): rewrite comments across all crates to the guidelines
Sweep every first-party crate source (1956 .rs files) to the project comment
guidelines: delete redundant restatements, decorative banners, change
narration, and end-of-line comments; keep and tighten the crucial ones
(invariants, bug rationale, SAFETY blocks, ported-source attribution).

No functional code changed. Every edit is proven comment-only against the
prior tree by a comment-stripping lexer (string/char/raw-string aware) plus a
separate doctest-fence check. Where removing a comment made rustfmt or clippy
want to re-lay-out adjacent code, the minimal triggering comment is restored so
code tokens stay byte-identical.

Gates green: cargo fmt --all --check (0 diffs), cargo check and cargo clippy
--workspace --all-targets (0 warnings).

Adds scripts/check_codegen_comment_guidelines.py — the enforcement gate for
these guidelines (flags banners, end-of-line comments, change narration, and
commented-out code).
2026-07-23 16:55:39 -04:00

77 lines
2.7 KiB
Rust

//! Syntax highlighting initialization.
//!
//! One lazily-initialized `Syntect` per tmTheme, shared by every `ThemeKind`
//! that maps to it. KigiDay needs its own because `kigi-day.tmTheme` deepens
//! the palette for light backgrounds.
use std::sync::OnceLock;
pub use kigi_markdown::Syntect;
use crate::theme::ThemeKind;
static SYNTECT_KIGINIGHT: OnceLock<Syntect> = OnceLock::new();
static SYNTECT_TOKYONIGHT: OnceLock<Syntect> = OnceLock::new();
static SYNTECT_KIGIDAY: OnceLock<Syntect> = OnceLock::new();
pub fn syntect_to_ratatui_fg(style: syntect::highlighting::Style) -> ratatui::style::Style {
let fg = crate::theme::quantize(ratatui::style::Color::Rgb(
style.foreground.r,
style.foreground.g,
style.foreground.b,
));
let mut out = ratatui::style::Style::default().fg(fg);
use syntect::highlighting::FontStyle;
if style.font_style.contains(FontStyle::BOLD) {
out = out.add_modifier(ratatui::style::Modifier::BOLD);
}
if style.font_style.contains(FontStyle::ITALIC) {
out = out.add_modifier(ratatui::style::Modifier::ITALIC);
}
if style.font_style.contains(FontStyle::UNDERLINE) {
out = out.add_modifier(ratatui::style::Modifier::UNDERLINED);
}
out
}
pub fn highlight_line(
text: &str,
highlighter: &mut Option<syntect::easy::HighlightLines<'_>>,
syntect: &Syntect,
fallback: ratatui::style::Style,
) -> Vec<ratatui::text::Span<'static>> {
if let Some(hl) = highlighter.as_mut()
&& let Ok(ranges) = hl.highlight_line(&format!("{text}\n"), &syntect.syntax_set)
{
let mut spans = Vec::new();
for (style, segment) in ranges {
let mut s = segment.to_owned();
while s.ends_with('\n') || s.ends_with('\r') {
s.pop();
}
if s.is_empty() {
continue;
}
spans.push(ratatui::text::Span::styled(s, syntect_to_ratatui_fg(style)));
}
if !spans.is_empty() {
return spans;
}
}
vec![ratatui::text::Span::styled(text.to_string(), fallback)]
}
pub fn get_syntect() -> &'static Syntect {
match crate::theme::Theme::current_kind() {
ThemeKind::KigiNight
| ThemeKind::RosePineMoon
| ThemeKind::OscuraMidnight
| ThemeKind::Auto => SYNTECT_KIGINIGHT
.get_or_init(|| Syntect::new(include_bytes!("../assets/kigi-night.tmTheme"))),
ThemeKind::TokyoNight => SYNTECT_TOKYONIGHT
.get_or_init(|| Syntect::new(include_bytes!("../assets/tokyo-night.tmTheme"))),
ThemeKind::KigiDay => SYNTECT_KIGIDAY
.get_or_init(|| Syntect::new(include_bytes!("../assets/kigi-day.tmTheme"))),
}
}