mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-03-10 12:41:34 +00:00
draft syntax highlight features by syntect
This commit is contained in:
parent
57c539c8ab
commit
fa1e2d39a2
@ -35,7 +35,11 @@ version = "0.32.1"
|
||||
[dependencies.openssl]
|
||||
version = "0.10.68"
|
||||
|
||||
[dependencies]
|
||||
syntect = "5.2.0"
|
||||
|
||||
# development
|
||||
[patch.crates-io]
|
||||
#ggemini = { path = "ggemini" }
|
||||
ggemtext = { git = "https://github.com/YGGverse/ggemtext.git" }
|
||||
#ggemtext = { git = "https://github.com/YGGverse/ggemtext.git" }
|
||||
ggemtext = { path = "ggemtext" }
|
||||
|
@ -1,8 +1,10 @@
|
||||
pub mod error;
|
||||
mod syntax;
|
||||
mod tag;
|
||||
mod widget;
|
||||
|
||||
pub use error::Error;
|
||||
use syntax::Syntax;
|
||||
use tag::Tag;
|
||||
use widget::Widget;
|
||||
|
||||
@ -53,6 +55,9 @@ impl Reader {
|
||||
// Init multiline code builder features
|
||||
let mut multiline = None;
|
||||
|
||||
// Init syntect highlight features
|
||||
let syntax = Syntax::new();
|
||||
|
||||
// Init tags
|
||||
let tag = Tag::new();
|
||||
|
||||
@ -66,7 +71,10 @@ impl Reader {
|
||||
// Append value to buffer
|
||||
buffer.insert_with_tags(
|
||||
&mut buffer.end_iter(),
|
||||
code.value.as_str(),
|
||||
&match syntax.highlight(&code.value, None) {
|
||||
Some(result) => result,
|
||||
None => code.value.to_string(),
|
||||
},
|
||||
&[&tag.code.text_tag],
|
||||
);
|
||||
buffer.insert(&mut buffer.end_iter(), NEW_LINE);
|
||||
@ -93,7 +101,8 @@ impl Reader {
|
||||
// Close tag found:
|
||||
if this.completed {
|
||||
// Is alt provided
|
||||
if let Some(alt) = &this.alt {
|
||||
let alt = match this.alt {
|
||||
Some(ref alt) => {
|
||||
// Insert alt value to the main buffer
|
||||
buffer.insert_with_tags(
|
||||
&mut buffer.end_iter(),
|
||||
@ -103,12 +112,20 @@ impl Reader {
|
||||
|
||||
// Append new line after alt text
|
||||
buffer.insert(&mut buffer.end_iter(), NEW_LINE);
|
||||
|
||||
// Return value as wanted also for syntax highlight detection
|
||||
Some(alt)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
// Insert multiline code buffer into main buffer
|
||||
buffer.insert_with_tags(
|
||||
&mut buffer.end_iter(),
|
||||
&this.value,
|
||||
&match syntax.highlight(&this.value, alt) {
|
||||
Some(result) => result,
|
||||
None => this.value.to_string(),
|
||||
},
|
||||
&[&tag.code.text_tag],
|
||||
);
|
||||
|
||||
@ -316,8 +333,8 @@ impl Reader {
|
||||
None::<&Window>,
|
||||
None::<&Cancellable>,
|
||||
|result| {
|
||||
if let Err(error) = result {
|
||||
println!("{error}")
|
||||
if let Err(e) = result {
|
||||
println!("{e}")
|
||||
}
|
||||
},
|
||||
),
|
||||
|
@ -0,0 +1,35 @@
|
||||
use syntect::{
|
||||
easy::HighlightLines, highlighting::ThemeSet, parsing::SyntaxSet,
|
||||
util::as_24_bit_terminal_escaped,
|
||||
};
|
||||
|
||||
pub const DEFAULT_THEME: &str = "base16-ocean.dark";
|
||||
|
||||
pub struct Syntax {
|
||||
syntax_set: SyntaxSet,
|
||||
theme_set: ThemeSet,
|
||||
}
|
||||
|
||||
impl Syntax {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
syntax_set: SyntaxSet::load_defaults_newlines(),
|
||||
theme_set: ThemeSet::load_defaults(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn highlight(&self, source: &str, extension: Option<&String>) -> Option<String> {
|
||||
match extension {
|
||||
Some(extension) => match self.syntax_set.find_syntax_by_extension(extension) {
|
||||
Some(syntax) => {
|
||||
let ranges = HighlightLines::new(syntax, &self.theme_set.themes[DEFAULT_THEME])
|
||||
.highlight_line(&source, &self.syntax_set)
|
||||
.unwrap(); // @TODO
|
||||
Some(as_24_bit_terminal_escaped(&ranges[..], true))
|
||||
}
|
||||
None => None,
|
||||
},
|
||||
None => None, // @TODO detect by source
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user