feat: add content_begin, content_end and content_raw

This commit is contained in:
PoiScript 2024-04-24 14:56:02 +08:00
parent 8c3ca13e8a
commit 1807f19407
No known key found for this signature in database
GPG key ID: 22C2B1249D99985E
2 changed files with 80 additions and 5 deletions

View file

@ -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);

View file

@ -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<Token, Token> {
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()
}
}