From 6010024934578ed08516712385adcb1d589ec5d7 Mon Sep 17 00:00:00 2001 From: ef3d0c3e Date: Sat, 2 Nov 2024 09:29:37 +0100 Subject: [PATCH] QoL Fixes --- src/elements/code.rs | 4 ++-- src/elements/graphviz.rs | 2 +- src/elements/media.rs | 2 +- src/elements/raw.rs | 2 +- src/elements/tex.rs | 1 + src/parser/langparser.rs | 14 +++++++------- src/parser/util.rs | 21 ++++++++++++--------- 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/elements/code.rs b/src/elements/code.rs index 8739f56..be727be 100644 --- a/src/elements/code.rs +++ b/src/elements/code.rs @@ -377,9 +377,9 @@ impl RegexRule for CodeRule { }; let mut code_content = if index == 0 { - util::escape_text('\\', "```", matches.get(4).unwrap().as_str()) + util::escape_text('\\', "```", matches.get(4).unwrap().as_str(), false) } else { - util::escape_text('\\', "``", matches.get(3).unwrap().as_str()) + util::escape_text('\\', "``", matches.get(3).unwrap().as_str(), false) }; if code_content.bytes().last() == Some(b'\n') // Remove newline diff --git a/src/elements/graphviz.rs b/src/elements/graphviz.rs index f0fd3c6..763c6d3 100644 --- a/src/elements/graphviz.rs +++ b/src/elements/graphviz.rs @@ -222,7 +222,7 @@ impl RegexRule for GraphRule { return reports; } Some(content) => { - let processed = util::escape_text('\\', "[/graph]", content.as_str()); + let processed = util::escape_text('\\', "[/graph]", content.as_str(), true); if processed.is_empty() { report_err!( diff --git a/src/elements/media.rs b/src/elements/media.rs index 88092ee..016b51f 100644 --- a/src/elements/media.rs +++ b/src/elements/media.rs @@ -329,7 +329,7 @@ impl RegexRule for MediaRule { matches.get(2).unwrap(), MediaRule::validate_uri(matches.get(2).unwrap().as_str()), ) { - (_, Ok(uri)) => util::escape_text('\\', ")", uri), + (_, Ok(uri)) => util::escape_text('\\', ")", uri, true), (m, Err(err)) => { report_err!( &mut reports, diff --git a/src/elements/raw.rs b/src/elements/raw.rs index 68fe1a9..7e58b48 100644 --- a/src/elements/raw.rs +++ b/src/elements/raw.rs @@ -112,7 +112,7 @@ impl RegexRule for RawRule { } Some(content) => { let processed = - util::escape_text('\\', "?}", content.as_str().trim_start().trim_end()); + util::escape_text('\\', "?}", content.as_str().trim_start().trim_end(), true); if processed.is_empty() { report_warn!( diff --git a/src/elements/tex.rs b/src/elements/tex.rs index 9f55a93..8d9215b 100644 --- a/src/elements/tex.rs +++ b/src/elements/tex.rs @@ -296,6 +296,7 @@ impl RegexRule for TexRule { '\\', ["|$", "$"][index], content.as_str().trim_start().trim_end(), + true, ); if processed.is_empty() { diff --git a/src/parser/langparser.rs b/src/parser/langparser.rs index 967757b..effdceb 100644 --- a/src/parser/langparser.rs +++ b/src/parser/langparser.rs @@ -201,14 +201,14 @@ impl<'b> Parser for LangParser<'b> { // Process the end of the semantics queue Semantics::on_document_end(&state.shared.lsp, source.clone()); - // Rule States - self.handle_reports(state.shared.rule_state.borrow_mut().on_scope_end( - &state, - &doc, - super::state::Scope::DOCUMENT, - )); - if parent.is_none() { + // Rule States + self.handle_reports(state.shared.rule_state.borrow_mut().on_scope_end( + &state, + &doc, + super::state::Scope::DOCUMENT, + )); + state.push( &doc, Box::new(DocumentEnd(Token::new( diff --git a/src/parser/util.rs b/src/parser/util.rs index 50afd95..e4df3e8 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -159,18 +159,21 @@ pub fn escape_source( /// /// If you need to create a source, do not use this function, use [`escape_source`] instead /// as it will populate an offsets to get accurate diagnostics and semantics. -pub fn escape_text>(escape: char, token: &'static str, content: S) -> String { +pub fn escape_text>( + escape: char, + token: &'static str, + content: S, + trim: bool, +) -> String { let mut processed = String::new(); let mut escaped = 0; let mut token_it = token.chars().peekable(); - for c in content - .as_ref() - .chars() - .as_str() - .trim_start() - .trim_end() - .chars() - { + let data = if trim { + content.as_ref().chars().as_str().trim_start().trim_end() + } else { + content.as_ref().chars().as_str() + }; + for c in data.chars() { if c == escape { escaped += 1; } else if escaped % 2 == 1 && token_it.peek().map_or(false, |p| *p == c) {