From 1807f194074d0f4287c186e7f97a687094784359 Mon Sep 17 00:00:00 2001 From: PoiScript Date: Wed, 24 Apr 2024 14:56:02 +0800 Subject: [PATCH] feat: add content_begin, content_end and content_raw --- src/ast/block.rs | 40 +++++++++++++++++++++++++++++++++++++--- src/ast/drawer.rs | 45 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/src/ast/block.rs b/src/ast/block.rs index 0138936..4b39b0f 100644 --- a/src/ast/block.rs +++ b/src/ast/block.rs @@ -1,6 +1,8 @@ -use crate::SyntaxKind; - -use super::{filter_token, ExportBlock, SourceBlock, Token}; +use super::{ + filter_token, CenterBlock, CommentBlock, DynBlock, ExampleBlock, ExportBlock, QuoteBlock, + SourceBlock, SpecialBlock, SyntaxKind, Token, VerseBlock, +}; +use rowan::TextSize; impl SourceBlock { /// ```rust @@ -107,3 +109,35 @@ impl ExportBlock { .find_map(filter_token(SyntaxKind::EXPORT_BLOCK_TYPE)) } } + +macro_rules! impl_content_border { + ($block:ident) => { + impl $block { + /// Beginning position of block content + pub fn content_start(&self) -> TextSize { + self.syntax + .first_child() + .map(|n| n.text_range().end()) + .unwrap_or_else(|| self.syntax.text_range().start()) + } + + /// Ending position of block content + pub fn content_end(&self) -> TextSize { + self.syntax + .last_child() + .map(|n| n.text_range().start()) + .unwrap_or_else(|| self.syntax.text_range().end()) + } + } + }; +} + +impl_content_border!(SourceBlock); +impl_content_border!(ExportBlock); +impl_content_border!(CenterBlock); +impl_content_border!(CommentBlock); +impl_content_border!(ExampleBlock); +impl_content_border!(QuoteBlock); +impl_content_border!(SpecialBlock); +impl_content_border!(VerseBlock); +impl_content_border!(DynBlock); diff --git a/src/ast/drawer.rs b/src/ast/drawer.rs index f9262c8..4ee6071 100644 --- a/src/ast/drawer.rs +++ b/src/ast/drawer.rs @@ -1,7 +1,7 @@ +use rowan::TextSize; use std::collections::HashMap; -use super::{filter_token, Drawer, SyntaxKind, Token}; -use crate::ast::PropertyDrawer; +use super::{filter_token, Drawer, PropertyDrawer, SyntaxKind, Token}; impl PropertyDrawer { /// ```rust @@ -61,6 +61,22 @@ impl PropertyDrawer { pub fn to_index_map(&self) -> indexmap::IndexMap { self.iter().collect() } + + /// Beginning position of drawer content + pub fn content_start(&self) -> TextSize { + self.syntax + .first_child() + .map(|n| n.text_range().end()) + .unwrap_or_else(|| self.syntax.text_range().start()) + } + + /// Ending position of drawer content + pub fn content_end(&self) -> TextSize { + self.syntax + .last_child() + .map(|n| n.text_range().start()) + .unwrap_or_else(|| self.syntax.text_range().end()) + } } impl Drawer { @@ -82,4 +98,29 @@ impl Drawer { .map(|t| Token(Some(t))) .unwrap_or_default() } + + /// Beginning position of drawer content + pub fn content_start(&self) -> TextSize { + self.syntax + .first_child() + .map(|n| n.text_range().end()) + .unwrap_or_else(|| self.syntax.text_range().start()) + } + + /// Ending position of drawer content + pub fn content_end(&self) -> TextSize { + self.syntax + .last_child() + .map(|n| n.text_range().start()) + .unwrap_or_else(|| self.syntax.text_range().end()) + } + + /// Raw text of drawer content + pub fn content_raw(&self) -> String { + self.syntax + .children() + .find(|n| n.kind() == SyntaxKind::DRAWER_CONTENT) + .map(|n| n.to_string()) + .unwrap_or_default() + } }