mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 17:20:08 +00:00
implement error handler, return Error::Parse on source could not be processed
This commit is contained in:
parent
3f1e8a9780
commit
3e2205015a
@ -1,3 +1,6 @@
|
|||||||
|
pub mod error;
|
||||||
|
pub use error::Error;
|
||||||
|
|
||||||
use adw::StyleManager;
|
use adw::StyleManager;
|
||||||
use gtk::{
|
use gtk::{
|
||||||
gdk::RGBA,
|
gdk::RGBA,
|
||||||
@ -9,7 +12,6 @@ use syntect::{
|
|||||||
easy::HighlightLines,
|
easy::HighlightLines,
|
||||||
highlighting::{Color, FontStyle, ThemeSet},
|
highlighting::{Color, FontStyle, ThemeSet},
|
||||||
parsing::{SyntaxReference, SyntaxSet},
|
parsing::{SyntaxReference, SyntaxSet},
|
||||||
Error,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Default theme
|
/* Default theme
|
||||||
@ -46,38 +48,26 @@ impl Syntax {
|
|||||||
) -> 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.syntect_buffer(source_code, source_tag, reference);
|
return self.buffer(source_code, source_tag, 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.syntect_buffer(source_code, source_tag, reference);
|
return self.buffer(source_code, source_tag, 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.syntect_buffer(source_code, source_tag, reference);
|
return self.buffer(source_code, source_tag, 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.syntect_buffer(source_code, source_tag, reference);
|
return self.buffer(source_code, source_tag, reference);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(self.default_buffer(source_code, source_tag))
|
Err(Error::Parse)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_buffer(&self, source: &str, source_tag: &TextTag) -> Vec<(TextTag, String)> {
|
fn buffer(
|
||||||
// Init new line buffer
|
|
||||||
let mut buffer = Vec::new();
|
|
||||||
|
|
||||||
// Create new tag preset from source
|
|
||||||
let tag = new_text_tag_from(source_tag);
|
|
||||||
|
|
||||||
// Append
|
|
||||||
buffer.push((tag, source.to_string()));
|
|
||||||
buffer
|
|
||||||
}
|
|
||||||
|
|
||||||
fn syntect_buffer(
|
|
||||||
&self,
|
&self,
|
||||||
source: &str,
|
source: &str,
|
||||||
source_tag: &TextTag,
|
source_tag: &TextTag,
|
||||||
@ -87,32 +77,35 @@ impl Syntax {
|
|||||||
let mut buffer = Vec::new();
|
let mut buffer = Vec::new();
|
||||||
|
|
||||||
// Apply syntect decorator
|
// Apply syntect decorator
|
||||||
let ranges = HighlightLines::new(
|
let mut ranges = HighlightLines::new(
|
||||||
syntax_reference,
|
syntax_reference,
|
||||||
&self.theme_set.themes[if StyleManager::default().is_dark() {
|
&self.theme_set.themes[if StyleManager::default().is_dark() {
|
||||||
DEFAULT_THEME_DARK
|
DEFAULT_THEME_DARK
|
||||||
} else {
|
} else {
|
||||||
DEFAULT_THEME_LIGHT
|
DEFAULT_THEME_LIGHT
|
||||||
}], // @TODO apply on env change
|
}], // @TODO apply on env change
|
||||||
)
|
);
|
||||||
.highlight_line(source, &self.syntax_set)?;
|
|
||||||
|
|
||||||
// Build tags
|
match ranges.highlight_line(source, &self.syntax_set) {
|
||||||
for (style, entity) in ranges {
|
Ok(result) => {
|
||||||
// Create new tag preset from source
|
// Build tags
|
||||||
let tag = new_text_tag_from(source_tag);
|
for (style, entity) in result {
|
||||||
|
// Create new tag preset from source
|
||||||
|
let tag = new_text_tag_from(source_tag);
|
||||||
|
|
||||||
// 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.set_foreground_rgba(Some(&color_to_rgba(style.foreground)));
|
||||||
tag.set_style(font_style_to_style(style.font_style));
|
tag.set_style(font_style_to_style(style.font_style));
|
||||||
tag.set_underline(font_style_to_underline(style.font_style));
|
tag.set_underline(font_style_to_underline(style.font_style));
|
||||||
|
|
||||||
// Append
|
// Append
|
||||||
buffer.push((tag, entity.to_string()));
|
buffer.push((tag, entity.to_string()));
|
||||||
|
}
|
||||||
|
Ok(buffer)
|
||||||
|
}
|
||||||
|
Err(e) => Err(Error::Syntect(e)),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(buffer)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,18 @@
|
|||||||
|
use std::fmt::{Display, Formatter, Result};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
Parse,
|
||||||
|
Syntect(syntect::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Error {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||||
|
match self {
|
||||||
|
Self::Parse => write!(f, "Parse error"),
|
||||||
|
Self::Syntect(e) => {
|
||||||
|
write!(f, "Syntect error: {e}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user