From d4d8acfb979018d5cb8a67ee5d0ee682fe1d5695 Mon Sep 17 00:00:00 2001 From: ef3d0c3e Date: Sun, 3 Nov 2024 10:10:36 +0100 Subject: [PATCH] Fix bug with style --- src/elements/code.rs | 7 ++++++- src/elements/customstyle.rs | 13 ++++++++++--- src/elements/style.rs | 19 +++++++++++++------ src/parser/langparser.rs | 6 ++++++ src/parser/util.rs | 5 ++++- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/elements/code.rs b/src/elements/code.rs index 705b157..fa4b1fc 100644 --- a/src/elements/code.rs +++ b/src/elements/code.rs @@ -379,7 +379,12 @@ impl RegexRule for CodeRule { let mut code_content = if index == 0 { util::escape_text('\\', "```", matches.get(4).unwrap().as_str(), false) } else { - util::escape_text('\\', "``", matches.get(3).unwrap().as_str(), !matches.get(3).unwrap().as_str().contains('\n')) + util::escape_text( + '\\', + "``", + matches.get(3).unwrap().as_str(), + !matches.get(3).unwrap().as_str().contains('\n'), + ) }; if code_content.bytes().last() == Some(b'\n') // Remove newline diff --git a/src/elements/customstyle.rs b/src/elements/customstyle.rs index ae4f2ec..5c3e67a 100644 --- a/src/elements/customstyle.rs +++ b/src/elements/customstyle.rs @@ -116,9 +116,16 @@ impl RuleState for CustomStyleState { let mut reports = vec![]; self.toggled.iter().for_each(|(style, token)| { - let paragraph = document.last_element::().unwrap(); - let paragraph_end = paragraph - .content + let container = std::cell::Ref::filter_map(document.content().borrow(), |content| { + content.last().and_then(|last| last.as_container()) + }) + .ok(); + if container.is_none() { + return; + } + let paragraph_end = container + .unwrap() + .contained() .last() .map(|last| { ( diff --git a/src/elements/style.rs b/src/elements/style.rs index 0adc8ce..05b06e0 100644 --- a/src/elements/style.rs +++ b/src/elements/style.rs @@ -1,7 +1,6 @@ use crate::compiler::compiler::Compiler; use crate::compiler::compiler::Target; use crate::document::document::Document; -use crate::document::document::DocumentAccessors; use crate::document::element::ElemKind; use crate::document::element::Element; use crate::lsp::semantic::Semantics; @@ -22,8 +21,6 @@ use std::cell::RefCell; use std::rc::Rc; use std::sync::Arc; -use super::paragraph::Paragraph; - #[derive(Debug)] pub struct Style { location: Token, @@ -96,9 +93,17 @@ impl RuleState for StyleState { } // Style not enabled let token = token.as_ref().unwrap(); - let paragraph = document.last_element::().unwrap(); - let paragraph_end = paragraph - .content + let container = + std::cell::Ref::filter_map(document.content().borrow(), |content| { + content.last().and_then(|last| last.as_container()) + }) + .ok(); + if container.is_none() { + return; + } + let paragraph_end = container + .unwrap() + .contained() .last() .map(|last| { ( @@ -277,6 +282,8 @@ impl RegexRule for StyleRule { #[cfg(test)] mod tests { + use elements::paragraph::Paragraph; + use crate::elements::text::Text; use crate::parser::langparser::LangParser; use crate::parser::parser::Parser; diff --git a/src/parser/langparser.rs b/src/parser/langparser.rs index effdceb..a9f8197 100644 --- a/src/parser/langparser.rs +++ b/src/parser/langparser.rs @@ -216,6 +216,12 @@ impl<'b> Parser for LangParser<'b> { doc.source(), ))), ); + } else { + self.handle_reports(state.shared.rule_state.borrow_mut().on_scope_end( + &state, + &doc, + super::state::Scope::PARAGRAPH, + )); } if path.is_some() { diff --git a/src/parser/util.rs b/src/parser/util.rs index 626d92c..cc0c1f4 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -329,6 +329,9 @@ mod tests { "Unescaped \\\\\\\\]".to_string() ); assert_eq!(escape_text('\\', ")", "A\\)B\\", true), "A)B".to_string(),); - assert_eq!(escape_text('\\', ")", "A\\)B\\\\", false), "A)B\\".to_string(),); + assert_eq!( + escape_text('\\', ")", "A\\)B\\\\", false), + "A)B\\".to_string(), + ); } }