From fcc401f2034c471abd94c816018663e2f18979f3 Mon Sep 17 00:00:00 2001 From: ef3d0c3e Date: Thu, 8 Aug 2024 17:11:32 +0200 Subject: [PATCH] Update macro --- crates/auto-registry/src/lib.rs | 33 ++++++++++++- src/compiler/compiler.rs | 9 ++-- src/elements/code.rs | 2 +- src/elements/comment.rs | 16 ++----- src/elements/customstyle.rs | 2 +- src/elements/elemstyle.rs | 2 +- src/elements/graphviz.rs | 2 +- src/elements/import.rs | 2 +- src/elements/layout.rs | 2 +- src/elements/link.rs | 2 +- src/elements/list.rs | 2 +- src/elements/media.rs | 2 +- src/elements/paragraph.rs | 2 +- src/elements/raw.rs | 2 +- src/elements/reference.rs | 2 +- src/elements/script.rs | 2 +- src/elements/section.rs | 2 +- src/elements/style.rs | 2 +- src/elements/tex.rs | 2 +- src/elements/text.rs | 2 +- src/elements/variable.rs | 4 +- src/lsp/parser.rs | 82 --------------------------------- src/parser/util.rs | 5 +- 23 files changed, 63 insertions(+), 120 deletions(-) diff --git a/crates/auto-registry/src/lib.rs b/crates/auto-registry/src/lib.rs index 5a9196a..9e3617f 100644 --- a/crates/auto-registry/src/lib.rs +++ b/crates/auto-registry/src/lib.rs @@ -76,6 +76,12 @@ impl Parse for AutoRegistryArgs { /// - registry: (String) Name of the registry to collect the struct into /// - path: (Optional String) The crate path in which the struct is located /// If left empty, the path will be try to be automatically-deduced +/// +/// # Note +/// +/// Due to a lacking implementation of `proc_macro_span` in rust-analyzer, +/// it is highly advised the set the `path` attribute when using this macro. +/// See https://github.com/rust-lang/rust-analyzer/issues/15950 #[proc_macro_attribute] pub fn auto_registry(attr: TokenStream, input: TokenStream) -> TokenStream { let args = parse_macro_input!(attr as AutoRegistryArgs); @@ -84,7 +90,12 @@ pub fn auto_registry(attr: TokenStream, input: TokenStream) -> TokenStream { let ident = &input.ident; let path = if let Some(path) = args.path { - format!("{}::{}", path.value(), ident.to_string().as_str()) + let value = path.value(); + if value.is_empty() { + value + } else { + format!("{}::{}", value, ident.to_string().as_str()) + } } else { // Attempt to get the path in a hacky way in case the path wasn't // specified as an attribute to the macro @@ -229,6 +240,25 @@ impl Parse for GenerateRegistryArgs { /// comma-separated and create the resulting expression /// - return_type: (Type) The return type of the generated function. /// Must match the type of the macro invocation +/// +/// # Example +/// ``` +/// macro_rules! create_listeners { +/// ( $($construct:expr),+ $(,)? ) => {{ +/// vec![$(Box::new($construct) as Box,)+] +/// }}; +/// } +/// #[generate_registry( +/// registry = "listeners", +/// target = build_listeners, +/// return_type = Vec>, +/// maker = create_listeners)] +/// +/// fn main() +/// { +/// let all_listeners : Vec> = build_listeners(); +/// } +/// ``` #[proc_macro_attribute] pub fn generate_registry(attr: TokenStream, input: TokenStream) -> TokenStream { let args = parse_macro_input!(attr as GenerateRegistryArgs); @@ -250,7 +280,6 @@ pub fn generate_registry(attr: TokenStream, input: TokenStream) -> TokenStream { } let function = args.target; - //proc_macro2::Ident::new(args.target.value().as_str(), proc_macro2::Span::call_site()); let return_type = args.return_type; let maker = args.maker; diff --git a/src/compiler/compiler.rs b/src/compiler/compiler.rs index de7c04f..24c37a5 100644 --- a/src/compiler/compiler.rs +++ b/src/compiler/compiler.rs @@ -13,6 +13,7 @@ use crate::document::variable::Variable; #[derive(Clone, Copy)] pub enum Target { HTML, + #[allow(unused)] LATEX, } @@ -21,7 +22,6 @@ pub struct Compiler { cache: Option>, reference_count: RefCell>>, // TODO: External references, i.e resolved later - sections_counter: RefCell>, } @@ -44,11 +44,12 @@ impl Compiler { /// Gets the section counter for a given depth /// This function modifies the section counter - pub fn section_counter(&self, depth: usize) -> Ref<'_, Vec> - { + pub fn section_counter(&self, depth: usize) -> Ref<'_, Vec> { // Increment current counter if self.sections_counter.borrow().len() == depth { - self.sections_counter.borrow_mut().last_mut() + self.sections_counter + .borrow_mut() + .last_mut() .map(|id| *id += 1); return Ref::map(self.sections_counter.borrow(), |b| &*b); } diff --git a/src/elements/code.rs b/src/elements/code.rs index f7853fd..53f4de5 100644 --- a/src/elements/code.rs +++ b/src/elements/code.rs @@ -296,7 +296,7 @@ impl Element for Code { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::code")] pub struct CodeRule { re: [Regex; 2], properties: PropertyParser, diff --git a/src/elements/comment.rs b/src/elements/comment.rs index a5df792..a10dbb5 100644 --- a/src/elements/comment.rs +++ b/src/elements/comment.rs @@ -16,17 +16,9 @@ use std::rc::Rc; #[derive(Debug)] pub struct Comment { - location: Token, - content: String, -} - -impl Comment { - pub fn new(location: Token, content: String) -> Self { - Self { - location: location, - content, - } - } + pub location: Token, + #[allow(unused)] + pub content: String, } impl Element for Comment { @@ -38,7 +30,7 @@ impl Element for Comment { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::comment")] pub struct CommentRule { re: [Regex; 1], } diff --git a/src/elements/customstyle.rs b/src/elements/customstyle.rs index 06c94a9..f2d6f14 100644 --- a/src/elements/customstyle.rs +++ b/src/elements/customstyle.rs @@ -178,7 +178,7 @@ impl RuleState for CustomStyleState { static STATE_NAME: &'static str = "elements.custom_style"; -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::customstyle")] pub struct CustomStyleRule; impl CustomStyleRule { diff --git a/src/elements/elemstyle.rs b/src/elements/elemstyle.rs index 2cfab00..6862294 100644 --- a/src/elements/elemstyle.rs +++ b/src/elements/elemstyle.rs @@ -21,7 +21,7 @@ use crate::parser::rule::Rule; use crate::parser::source::Cursor; use crate::parser::source::Source; -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::elemstyle")] pub struct ElemStyleRule { start_re: Regex, } diff --git a/src/elements/graphviz.rs b/src/elements/graphviz.rs index 378e33e..9b3ecab 100644 --- a/src/elements/graphviz.rs +++ b/src/elements/graphviz.rs @@ -146,7 +146,7 @@ impl Element for Graphviz { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::graphviz")] pub struct GraphRule { re: [Regex; 1], properties: PropertyParser, diff --git a/src/elements/import.rs b/src/elements/import.rs index 57386c7..14e5086 100644 --- a/src/elements/import.rs +++ b/src/elements/import.rs @@ -17,7 +17,7 @@ use std::rc::Rc; use super::paragraph::Paragraph; -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::import")] pub struct ImportRule { re: [Regex; 1], } diff --git a/src/elements/layout.rs b/src/elements/layout.rs index 3128634..9e2a48a 100644 --- a/src/elements/layout.rs +++ b/src/elements/layout.rs @@ -284,7 +284,7 @@ impl RuleState for LayoutState { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::layout")] pub struct LayoutRule { re: [Regex; 3], } diff --git a/src/elements/link.rs b/src/elements/link.rs index ab388d1..f191e36 100644 --- a/src/elements/link.rs +++ b/src/elements/link.rs @@ -71,7 +71,7 @@ impl ContainerElement for Link { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::link")] pub struct LinkRule { re: [Regex; 1], } diff --git a/src/elements/list.rs b/src/elements/list.rs index ad9058b..e5fdc93 100644 --- a/src/elements/list.rs +++ b/src/elements/list.rs @@ -106,7 +106,7 @@ impl ContainerElement for ListEntry { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::list")] pub struct ListRule { start_re: Regex, continue_re: Regex, diff --git a/src/elements/media.rs b/src/elements/media.rs index d4d49ef..26a277a 100644 --- a/src/elements/media.rs +++ b/src/elements/media.rs @@ -221,7 +221,7 @@ impl ReferenceableElement for Medium { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::media")] pub struct MediaRule { re: [Regex; 1], properties: PropertyParser, diff --git a/src/elements/paragraph.rs b/src/elements/paragraph.rs index a631390..3c962b0 100644 --- a/src/elements/paragraph.rs +++ b/src/elements/paragraph.rs @@ -91,7 +91,7 @@ impl ContainerElement for Paragraph { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::paragraph")] pub struct ParagraphRule { re: Regex, } diff --git a/src/elements/raw.rs b/src/elements/raw.rs index bbb34d1..edb0b98 100644 --- a/src/elements/raw.rs +++ b/src/elements/raw.rs @@ -44,7 +44,7 @@ impl Element for Raw { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::raw")] pub struct RawRule { re: [Regex; 1], properties: PropertyParser, diff --git a/src/elements/reference.rs b/src/elements/reference.rs index 026ddee..5647091 100644 --- a/src/elements/reference.rs +++ b/src/elements/reference.rs @@ -62,7 +62,7 @@ impl Element for Reference { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::reference")] pub struct ReferenceRule { re: [Regex; 1], properties: PropertyParser, diff --git a/src/elements/script.rs b/src/elements/script.rs index 11d3ea2..cac0d30 100644 --- a/src/elements/script.rs +++ b/src/elements/script.rs @@ -20,7 +20,7 @@ use std::rc::Rc; use super::text::Text; -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::script")] pub struct ScriptRule { re: [Regex; 2], eval_kinds: [(&'static str, &'static str); 3], diff --git a/src/elements/section.rs b/src/elements/section.rs index 3711b73..6c257c6 100644 --- a/src/elements/section.rs +++ b/src/elements/section.rs @@ -135,7 +135,7 @@ impl ReferenceableElement for Section { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::section")] pub struct SectionRule { re: [Regex; 1], } diff --git a/src/elements/style.rs b/src/elements/style.rs index bd5fb04..d11ce0f 100644 --- a/src/elements/style.rs +++ b/src/elements/style.rs @@ -132,7 +132,7 @@ impl RuleState for StyleState { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::style")] pub struct StyleRule { re: [Regex; 4], } diff --git a/src/elements/tex.rs b/src/elements/tex.rs index e6ba28a..da7c091 100644 --- a/src/elements/tex.rs +++ b/src/elements/tex.rs @@ -219,7 +219,7 @@ impl Element for Tex { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::tex")] pub struct TexRule { re: [Regex; 2], properties: PropertyParser, diff --git a/src/elements/text.rs b/src/elements/text.rs index 58e2d38..16130a0 100644 --- a/src/elements/text.rs +++ b/src/elements/text.rs @@ -42,7 +42,7 @@ impl Element for Text { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::text")] pub struct TextRule; impl TextRule { diff --git a/src/elements/variable.rs b/src/elements/variable.rs index a62ae5b..8ca93a7 100644 --- a/src/elements/variable.rs +++ b/src/elements/variable.rs @@ -37,7 +37,7 @@ impl FromStr for VariableKind { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::variable")] pub struct VariableRule { re: [Regex; 1], kinds: Vec<(String, String)>, @@ -297,7 +297,7 @@ impl RegexRule for VariableRule { } } -#[auto_registry::auto_registry(registry = "rules")] +#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::variable")] pub struct VariableSubstitutionRule { re: [Regex; 1], } diff --git a/src/lsp/parser.rs b/src/lsp/parser.rs index b222432..d72952b 100644 --- a/src/lsp/parser.rs +++ b/src/lsp/parser.rs @@ -94,85 +94,3 @@ impl From<&LineCursor> for Cursor } } } - -#[derive(Debug)] -pub struct LsParser -{ - rules: Vec>, - colors: ReportColors, - - // Parser state - pub state: RefCell, - pub kernels: RefCell>, -} - -impl Parser for LsParser -{ - fn colors(&self) -> &ReportColors { &self.colors } - fn rules(&self) -> &Vec> { &self.rules } - fn rules_mut(&mut self) -> &mut Vec> { &mut self.rules } - - fn state(&self) -> Ref<'_, StateHolder> { self.state.borrow() } - fn state_mut(&self) -> std::cell::RefMut<'_, StateHolder> { self.state.borrow_mut() } - - fn has_error(&self) -> bool { true } - - fn push<'a>(&self, doc: &dyn Document, elem: Box) { - todo!() - } - - fn parse<'a>(&self, source: Rc, parent: Option<&'a dyn Document<'a>>) -> Box+'a> { - todo!() - } - - fn parse_into<'a>(&self, source: Rc, document: &'a dyn Document<'a>) { - todo!() - } -} - -impl KernelHolder for LsParser -{ - fn get_kernel(&self, name: &str) - -> Option> { - RefMut::filter_map(self.kernels.borrow_mut(), - |map| map.get_mut(name)).ok() - } - - fn insert_kernel(&self, name: String, kernel: Kernel) - -> RefMut<'_, Kernel> { - //TODO do not get - self.kernels.borrow_mut() - .insert(name.clone(), kernel); - self.get_kernel(name.as_str()).unwrap() - } -} - -impl StyleHolder for LsParser { - fn element_styles(&self) -> Ref<'_, HashMap>> { - todo!() - } - - fn element_styles_mut(&self) -> RefMut<'_, HashMap>> { - todo!() - } -} - -impl LayoutHolder for LsParser { - fn layouts(&self) -> Ref<'_, HashMap>> { - todo!() - } - - fn layouts_mut(&self) -> RefMut<'_, HashMap>> { - todo!() - } -} - -impl CustomStyleHolder for LsParser { - fn custom_styles(&self) -> Ref<'_, HashMap>> { - todo!() - } - - fn custom_styles_mut(&self) -> RefMut<'_, HashMap>> { - todo!() - } -} diff --git a/src/parser/util.rs b/src/parser/util.rs index 8c73daa..7e69b64 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -423,7 +423,10 @@ mod tests { (&doc as &dyn Document) .last_element_mut::() .unwrap() - .push(Box::new(Comment::new(tok.clone(), "COMMENT".to_string()))) + .push(Box::new(Comment { + location: tok.clone(), + content: "COMMENT".into(), + })) .unwrap(); assert_eq!(process_text(&doc, "\na"), "a");