create separated TextTag preset for syntax highlight buffer, add comments

This commit is contained in:
yggverse 2024-12-05 03:19:28 +02:00
parent 8cb1e63222
commit 8c9cabfcda
3 changed files with 47 additions and 33 deletions

View File

@ -81,8 +81,8 @@ impl Reader {
for line in gemtext.lines() { for line in gemtext.lines() {
// Is inline code // Is inline code
if let Some(code) = Inline::from(line) { if let Some(code) = Inline::from(line) {
// Append value to buffer // Try auto-detect code syntax for given `value`
match syntax.highlight(&code.value, &tag.code.text_tag, None) { match syntax.highlight(&code.value, None) {
Ok(highlight) => { Ok(highlight) => {
for (syntax_tag, entity) in highlight { for (syntax_tag, entity) in highlight {
// Register new tag // Register new tag
@ -98,6 +98,7 @@ impl Reader {
} }
} }
Err(_) => { Err(_) => {
// Nothing match, append default `Code` tag into the main buffer
buffer.insert_with_tags( buffer.insert_with_tags(
&mut buffer.end_iter(), &mut buffer.end_iter(),
&code.value, &code.value,
@ -149,8 +150,9 @@ impl Reader {
None => None, None => None,
}; };
// Insert multiline code into main buffer // Begin code block construction
match syntax.highlight(&this.value, &tag.code.text_tag, alt) { // Try auto-detect code syntax for given `value` and `alt`
match syntax.highlight(&this.value, alt) {
Ok(highlight) => { Ok(highlight) => {
for (syntax_tag, entity) in highlight { for (syntax_tag, entity) in highlight {
// Register new tag // Register new tag
@ -166,6 +168,9 @@ impl Reader {
} }
} }
Err(_) => { Err(_) => {
// Try ANSI/SGR highlight (terminal emulation)
// Nothing match, append default `Code` tag into the main buffer
buffer.insert_with_tags( buffer.insert_with_tags(
&mut buffer.end_iter(), &mut buffer.end_iter(),
&this.value, &this.value,

View File

@ -1,5 +1,8 @@
pub mod error; pub mod error;
mod tag;
pub use error::Error; pub use error::Error;
use tag::Tag;
use adw::StyleManager; use adw::StyleManager;
use gtk::{ use gtk::{
@ -50,25 +53,24 @@ impl Syntax {
pub fn highlight( pub fn highlight(
&self, &self,
source_code: &str, source_code: &str,
source_tag: &TextTag,
alt: Option<&String>, alt: Option<&String>,
) -> Result<Vec<(TextTag, String)>, Error> { ) -> Result<Vec<(TextTag, String)>, Error> {
if let Some(value) = alt { if let Some(value) = alt {
if let Some(reference) = self.syntax_set.find_syntax_by_name(value) { if let Some(reference) = self.syntax_set.find_syntax_by_name(value) {
return self.buffer(source_code, source_tag, reference); return self.buffer(source_code, reference);
} }
if let Some(reference) = self.syntax_set.find_syntax_by_token(value) { if let Some(reference) = self.syntax_set.find_syntax_by_token(value) {
return self.buffer(source_code, source_tag, reference); return self.buffer(source_code, reference);
} }
if let Some(reference) = self.syntax_set.find_syntax_by_path(value) { if let Some(reference) = self.syntax_set.find_syntax_by_path(value) {
return self.buffer(source_code, source_tag, reference); return self.buffer(source_code, reference);
} }
} }
if let Some(reference) = self.syntax_set.find_syntax_by_first_line(source_code) { if let Some(reference) = self.syntax_set.find_syntax_by_first_line(source_code) {
return self.buffer(source_code, source_tag, reference); return self.buffer(source_code, reference);
} }
Err(Error::Parse) Err(Error::Parse)
@ -77,7 +79,6 @@ impl Syntax {
fn buffer( fn buffer(
&self, &self,
source: &str, source: &str,
source_tag: &TextTag,
syntax_reference: &SyntaxReference, syntax_reference: &SyntaxReference,
) -> Result<Vec<(TextTag, String)>, Error> { ) -> Result<Vec<(TextTag, String)>, Error> {
// Init new line buffer // Init new line buffer
@ -98,16 +99,19 @@ impl Syntax {
// Build tags // Build tags
for (style, entity) in result { for (style, entity) in result {
// Create new tag preset from source // Create new tag preset from source
let tag = new_text_tag_from(source_tag); let tag = Tag::new();
// Tuneup using syntect conversion // Tuneup using syntect conversion
// tag.set_background_rgba(Some(&color_to_rgba(style.background))); // tag.set_background_rgba(Some(&color_to_rgba(style.background)));
tag.set_foreground_rgba(Some(&color_to_rgba(style.foreground))); tag.text_tag
tag.set_style(font_style_to_style(style.font_style)); .set_foreground_rgba(Some(&color_to_rgba(style.foreground)));
tag.set_underline(font_style_to_underline(style.font_style)); tag.text_tag
.set_style(font_style_to_style(style.font_style));
tag.text_tag
.set_underline(font_style_to_underline(style.font_style));
// Append // Append
buffer.push((tag, entity.to_string())); buffer.push((tag.text_tag, entity.to_string()));
} }
Ok(buffer) Ok(buffer)
} }
@ -140,21 +144,3 @@ fn font_style_to_underline(font_style: FontStyle) -> Underline {
_ => Underline::None, _ => Underline::None,
} }
} }
fn new_text_tag_from(source_tag: &TextTag) -> TextTag {
let text_tag = TextTag::builder()
.left_margin(source_tag.left_margin())
.scale(source_tag.scale())
.wrap_mode(source_tag.wrap_mode())
.build();
if let Some(ref family) = source_tag.family() {
text_tag.set_family(Some(family));
}
if let Some(ref foreground_rgba) = source_tag.foreground_rgba() {
text_tag.set_foreground_rgba(Some(foreground_rgba));
}
text_tag
}

View File

@ -0,0 +1,23 @@
use gtk::{TextTag, WrapMode};
/// Default [TextTag](https://docs.gtk.org/gtk4/class.TextTag.html) preset
/// for syntax highlight buffer
pub struct Tag {
pub text_tag: TextTag,
}
impl Tag {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self {
text_tag: TextTag::builder()
.family("monospace") // @TODO
.left_margin(28)
.scale(0.8)
.wrap_mode(WrapMode::None)
.build(),
}
}
}