mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-03-10 20:51:45 +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]
|
[dependencies.openssl]
|
||||||
version = "0.10.68"
|
version = "0.10.68"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
syntect = "5.2.0"
|
||||||
|
|
||||||
# development
|
# development
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
#ggemini = { path = "ggemini" }
|
#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;
|
pub mod error;
|
||||||
|
mod syntax;
|
||||||
mod tag;
|
mod tag;
|
||||||
mod widget;
|
mod widget;
|
||||||
|
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
|
use syntax::Syntax;
|
||||||
use tag::Tag;
|
use tag::Tag;
|
||||||
use widget::Widget;
|
use widget::Widget;
|
||||||
|
|
||||||
@ -53,6 +55,9 @@ impl Reader {
|
|||||||
// Init multiline code builder features
|
// Init multiline code builder features
|
||||||
let mut multiline = None;
|
let mut multiline = None;
|
||||||
|
|
||||||
|
// Init syntect highlight features
|
||||||
|
let syntax = Syntax::new();
|
||||||
|
|
||||||
// Init tags
|
// Init tags
|
||||||
let tag = Tag::new();
|
let tag = Tag::new();
|
||||||
|
|
||||||
@ -66,7 +71,10 @@ impl Reader {
|
|||||||
// Append value to buffer
|
// Append value to buffer
|
||||||
buffer.insert_with_tags(
|
buffer.insert_with_tags(
|
||||||
&mut buffer.end_iter(),
|
&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],
|
&[&tag.code.text_tag],
|
||||||
);
|
);
|
||||||
buffer.insert(&mut buffer.end_iter(), NEW_LINE);
|
buffer.insert(&mut buffer.end_iter(), NEW_LINE);
|
||||||
@ -93,22 +101,31 @@ impl Reader {
|
|||||||
// Close tag found:
|
// Close tag found:
|
||||||
if this.completed {
|
if this.completed {
|
||||||
// Is alt provided
|
// Is alt provided
|
||||||
if let Some(alt) = &this.alt {
|
let alt = match this.alt {
|
||||||
// Insert alt value to the main buffer
|
Some(ref alt) => {
|
||||||
buffer.insert_with_tags(
|
// Insert alt value to the main buffer
|
||||||
&mut buffer.end_iter(),
|
buffer.insert_with_tags(
|
||||||
alt.as_str(),
|
&mut buffer.end_iter(),
|
||||||
&[&tag.title.text_tag],
|
alt.as_str(),
|
||||||
);
|
&[&tag.title.text_tag],
|
||||||
|
);
|
||||||
|
|
||||||
// Append new line after alt text
|
// Append new line after alt text
|
||||||
buffer.insert(&mut buffer.end_iter(), NEW_LINE);
|
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
|
// Insert multiline code buffer into main buffer
|
||||||
buffer.insert_with_tags(
|
buffer.insert_with_tags(
|
||||||
&mut buffer.end_iter(),
|
&mut buffer.end_iter(),
|
||||||
&this.value,
|
&match syntax.highlight(&this.value, alt) {
|
||||||
|
Some(result) => result,
|
||||||
|
None => this.value.to_string(),
|
||||||
|
},
|
||||||
&[&tag.code.text_tag],
|
&[&tag.code.text_tag],
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -316,8 +333,8 @@ impl Reader {
|
|||||||
None::<&Window>,
|
None::<&Window>,
|
||||||
None::<&Cancellable>,
|
None::<&Cancellable>,
|
||||||
|result| {
|
|result| {
|
||||||
if let Err(error) = result {
|
if let Err(e) = result {
|
||||||
println!("{error}")
|
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