1use rowan::ast::AstNode;
4use rowan::{TextRange, TextSize};
5
6use super::syntax::{DocumentationLanguage, SyntaxKind, SyntaxNode, SyntaxToken};
7
8macro_rules! ast_node {
9 ($name:ident, $($kind:path)|+) => {
10 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
11 pub struct $name(SyntaxNode);
12
13 impl AstNode for $name {
14 type Language = DocumentationLanguage;
15
16 fn can_cast(kind: SyntaxKind) -> bool {
17 matches!(kind, $($kind)|+)
18 }
19
20 fn cast(syntax: SyntaxNode) -> Option<Self> {
21 Self::can_cast(syntax.kind()).then_some(Self(syntax))
22 }
23
24 fn syntax(&self) -> &SyntaxNode {
25 &self.0
26 }
27 }
28 };
29}
30
31ast_node!(Document, SyntaxKind::ROOT);
32ast_node!(Paragraph, SyntaxKind::PARAGRAPH);
33ast_node!(
34 Heading,
35 SyntaxKind::ATX_HEADING | SyntaxKind::SETEXT_HEADING
36);
37ast_node!(BlockQuote, SyntaxKind::BLOCK_QUOTE);
38ast_node!(Admonition, SyntaxKind::ADMONITION);
39ast_node!(List, SyntaxKind::LIST);
40ast_node!(ListItem, SyntaxKind::LIST_ITEM);
41ast_node!(IndentedCodeBlock, SyntaxKind::INDENTED_CODE_BLOCK);
42ast_node!(CodeBlock, SyntaxKind::FENCED_CODE_BLOCK);
43ast_node!(MathBlock, SyntaxKind::MATH_BLOCK);
44ast_node!(FootnoteDefinition, SyntaxKind::FOOTNOTE_DEFINITION);
45ast_node!(Table, SyntaxKind::TABLE);
46ast_node!(TableRow, SyntaxKind::TABLE_ROW);
47ast_node!(TableCell, SyntaxKind::TABLE_CELL);
48ast_node!(ThematicBreak, SyntaxKind::THEMATIC_BREAK);
49ast_node!(Emphasis, SyntaxKind::EMPHASIS);
50ast_node!(Strong, SyntaxKind::STRONG);
51ast_node!(InlineCode, SyntaxKind::INLINE_CODE);
52ast_node!(InlineMath, SyntaxKind::INLINE_MATH);
53ast_node!(Link, SyntaxKind::LINK);
54ast_node!(Image, SyntaxKind::IMAGE);
55ast_node!(Autolink, SyntaxKind::AUTOLINK);
56ast_node!(FootnoteReference, SyntaxKind::FOOTNOTE_REFERENCE);
57ast_node!(Interpolation, SyntaxKind::INTERPOLATION);
58ast_node!(HardBreak, SyntaxKind::HARD_BREAK);
59
60#[derive(Debug, Clone, PartialEq, Eq, Hash)]
62pub enum Block {
63 Paragraph(Paragraph),
64 Heading(Heading),
65 BlockQuote(BlockQuote),
66 Admonition(Admonition),
67 List(List),
68 IndentedCodeBlock(IndentedCodeBlock),
69 CodeBlock(CodeBlock),
70 MathBlock(MathBlock),
71 FootnoteDefinition(FootnoteDefinition),
72 Table(Table),
73 ThematicBreak(ThematicBreak),
74 Interpolation(Interpolation),
75 Other(SyntaxNode),
77}
78
79impl Block {
80 fn cast(syntax: SyntaxNode) -> Self {
81 match syntax.kind() {
82 SyntaxKind::PARAGRAPH => Self::Paragraph(Paragraph(syntax)),
83 SyntaxKind::ATX_HEADING | SyntaxKind::SETEXT_HEADING => Self::Heading(Heading(syntax)),
84 SyntaxKind::BLOCK_QUOTE => Self::BlockQuote(BlockQuote(syntax)),
85 SyntaxKind::ADMONITION => Self::Admonition(Admonition(syntax)),
86 SyntaxKind::LIST => Self::List(List(syntax)),
87 SyntaxKind::INDENTED_CODE_BLOCK => Self::IndentedCodeBlock(IndentedCodeBlock(syntax)),
88 SyntaxKind::FENCED_CODE_BLOCK => Self::CodeBlock(CodeBlock(syntax)),
89 SyntaxKind::MATH_BLOCK => Self::MathBlock(MathBlock(syntax)),
90 SyntaxKind::FOOTNOTE_DEFINITION => Self::FootnoteDefinition(FootnoteDefinition(syntax)),
91 SyntaxKind::TABLE => Self::Table(Table(syntax)),
92 SyntaxKind::THEMATIC_BREAK => Self::ThematicBreak(ThematicBreak(syntax)),
93 SyntaxKind::INTERPOLATION => Self::Interpolation(Interpolation(syntax)),
94 _ => Self::Other(syntax),
95 }
96 }
97
98 pub fn syntax(&self) -> &SyntaxNode {
100 match self {
101 Self::Paragraph(node) => node.syntax(),
102 Self::Heading(node) => node.syntax(),
103 Self::BlockQuote(node) => node.syntax(),
104 Self::Admonition(node) => node.syntax(),
105 Self::List(node) => node.syntax(),
106 Self::IndentedCodeBlock(node) => node.syntax(),
107 Self::CodeBlock(node) => node.syntax(),
108 Self::MathBlock(node) => node.syntax(),
109 Self::FootnoteDefinition(node) => node.syntax(),
110 Self::Table(node) => node.syntax(),
111 Self::ThematicBreak(node) => node.syntax(),
112 Self::Interpolation(node) => node.syntax(),
113 Self::Other(node) => node,
114 }
115 }
116}
117
118impl Document {
119 pub fn blocks(&self) -> impl Iterator<Item = Block> + '_ {
121 self.syntax().children().map(Block::cast)
122 }
123}
124
125#[derive(Debug, Clone, PartialEq, Eq, Hash)]
127pub enum Inline {
128 Text(SyntaxToken),
129 Escape(SyntaxToken),
130 EnDash(SyntaxToken),
132 EmDash(SyntaxToken),
134 SoftBreak(SyntaxToken),
135 Emphasis(Emphasis),
136 Strong(Strong),
137 Code(InlineCode),
138 Math(InlineMath),
139 Link(Link),
140 Image(Image),
141 Autolink(Autolink),
142 FootnoteReference(FootnoteReference),
143 Interpolation(Interpolation),
144 HardBreak(HardBreak),
145}
146
147impl Paragraph {
148 pub fn inlines(&self) -> impl Iterator<Item = Inline> + '_ {
150 inline_children(self.syntax())
151 }
152}
153
154impl BlockQuote {
155 pub fn blocks(&self) -> impl Iterator<Item = Block> + '_ {
157 self.syntax().children().map(Block::cast)
158 }
159}
160
161impl Heading {
162 pub fn level(&self) -> u8 {
164 let marker = token(self.syntax(), SyntaxKind::HEADING_MARKER)
165 .map(|token| token.text().to_string())
166 .unwrap_or_default();
167 if self.syntax().kind() == SyntaxKind::SETEXT_HEADING {
168 if marker.trim_start().starts_with('=') {
169 1
170 } else {
171 2
172 }
173 } else {
174 marker.bytes().filter(|&byte| byte == b'#').count() as u8
175 }
176 }
177
178 pub fn content(&self) -> String {
180 visible_text(self.syntax())
181 }
182
183 pub fn inlines(&self) -> impl Iterator<Item = Inline> + '_ {
185 inline_children(self.syntax())
186 }
187
188 pub fn slug(&self) -> String {
194 slug(&self.content())
195 }
196}
197
198pub fn slug(heading: &str) -> String {
200 let mut out = String::new();
201 let mut separator = false;
202 for character in heading.chars().flat_map(char::to_lowercase) {
203 if character.is_alphanumeric() || character == '_' || character == '-' {
204 if separator && !out.is_empty() && !out.ends_with('-') {
205 out.push('-');
206 }
207 out.push(character);
208 separator = false;
209 } else if character.is_whitespace() {
210 separator = true;
211 }
212 }
213 out.trim_end_matches('-').to_string()
214}
215
216impl Admonition {
217 pub fn category(&self) -> String {
219 token(self.syntax(), SyntaxKind::ADMONITION_CATEGORY)
220 .map(|token| token.text().to_string())
221 .unwrap_or_default()
222 }
223
224 pub fn title(&self) -> String {
226 token(self.syntax(), SyntaxKind::ADMONITION_TITLE).map_or_else(
227 || uppercase_first(&self.category()),
228 |token| token.text().trim_matches('"').to_string(),
229 )
230 }
231
232 pub fn blocks(&self) -> impl Iterator<Item = Block> + '_ {
234 self.syntax().children().map(Block::cast)
235 }
236}
237
238impl List {
239 pub fn items(&self) -> impl Iterator<Item = ListItem> + '_ {
241 self.syntax().children().filter_map(ListItem::cast)
242 }
243
244 pub fn ordered_start(&self) -> Option<u64> {
246 nested_token(self.syntax(), SyntaxKind::LIST_MARKER)?
247 .text()
248 .trim_end_matches(['.', ')'])
249 .parse()
250 .ok()
251 }
252
253 pub fn is_loose(&self) -> bool {
255 self.syntax()
256 .descendants_with_tokens()
257 .filter_map(|element| element.into_token())
258 .any(|token| token.kind() == SyntaxKind::BLANK_LINE)
259 }
260}
261
262impl ListItem {
263 pub fn inlines(&self) -> impl Iterator<Item = Inline> + '_ {
265 inline_children(self.syntax())
266 }
267
268 pub fn blocks(&self) -> impl Iterator<Item = Block> + '_ {
270 self.syntax().children().map(Block::cast)
271 }
272}
273
274impl CodeBlock {
275 pub fn info(&self) -> String {
277 token(self.syntax(), SyntaxKind::INFO_STRING)
278 .map(|token| token.text().trim().to_string())
279 .unwrap_or_default()
280 }
281
282 pub fn content(&self) -> String {
284 let mut content = String::new();
285 for token in self
286 .syntax()
287 .children_with_tokens()
288 .filter_map(|element| element.into_token())
289 {
290 if token.kind() == SyntaxKind::CODE_CONTENT {
291 content.push_str(token.text());
292 }
293 }
294 content.trim_end_matches(['\r', '\n']).to_string()
295 }
296
297 pub fn content_range(&self) -> Option<TextRange> {
303 let mut tokens = self
304 .syntax()
305 .children_with_tokens()
306 .filter_map(|element| element.into_token())
307 .filter(|token| token.kind() == SyntaxKind::CODE_CONTENT);
308 let first = tokens.next()?.text_range();
309 Some(TextRange::new(
310 first.start(),
311 tokens
312 .last()
313 .map_or(first.end(), |token| token.text_range().end()),
314 ))
315 }
316
317 pub fn fence_kind(&self) -> FenceKind {
319 classify_fence(&self.info())
320 }
321}
322
323impl IndentedCodeBlock {
324 pub fn content(&self) -> String {
326 let mut out = String::new();
327 for token in self
328 .syntax()
329 .descendants_with_tokens()
330 .filter_map(|element| element.into_token())
331 {
332 if matches!(token.kind(), SyntaxKind::CODE_CONTENT | SyntaxKind::NEWLINE) {
333 out.push_str(token.text());
334 }
335 }
336 out.trim_end_matches(['\r', '\n']).to_string()
337 }
338}
339
340impl MathBlock {
341 pub fn content(&self) -> String {
343 let mut content = String::new();
344 for token in self
345 .syntax()
346 .children_with_tokens()
347 .filter_map(|element| element.into_token())
348 {
349 if token.kind() == SyntaxKind::MATH_CONTENT {
350 content.push_str(token.text());
351 }
352 }
353 content.trim_end_matches(['\r', '\n']).to_string()
354 }
355}
356
357impl FootnoteDefinition {
358 pub fn id(&self) -> String {
360 token(self.syntax(), SyntaxKind::FOOTNOTE_LABEL)
361 .map(|token| {
362 token
363 .text()
364 .strip_prefix("[^")
365 .and_then(|text| text.strip_suffix("]:"))
366 .unwrap_or(token.text())
367 .to_string()
368 })
369 .unwrap_or_default()
370 }
371
372 pub fn blocks(&self) -> impl Iterator<Item = Block> + '_ {
374 self.syntax().children().map(Block::cast)
375 }
376}
377
378impl Table {
379 pub fn rows(&self) -> impl Iterator<Item = TableRow> + '_ {
381 self.syntax().children().filter_map(TableRow::cast)
382 }
383}
384
385impl TableRow {
386 pub fn cells(&self) -> impl Iterator<Item = TableCell> + '_ {
388 self.syntax().children().filter_map(TableCell::cast)
389 }
390}
391
392impl TableCell {
393 pub fn inlines(&self) -> impl Iterator<Item = Inline> + '_ {
395 inline_children(self.syntax())
396 }
397}
398
399impl InlineCode {
400 pub fn content(&self) -> String {
402 inline_payload(self.syntax(), SyntaxKind::CODE_CONTENT)
403 }
404}
405
406impl InlineMath {
407 pub fn content(&self) -> String {
409 inline_payload(self.syntax(), SyntaxKind::MATH_CONTENT)
410 }
411}
412
413impl Link {
414 pub fn destination(&self) -> String {
416 payload_text(self.syntax(), SyntaxKind::LINK_DESTINATION)
417 }
418
419 pub fn destination_range(&self) -> Option<TextRange> {
424 let mut tokens = payload_tokens(self.syntax(), SyntaxKind::LINK_DESTINATION);
425 let first = tokens.next()?.text_range();
426 Some(TextRange::new(
427 first.start(),
428 tokens
429 .last()
430 .map_or(first.end(), |token| token.text_range().end()),
431 ))
432 }
433
434 pub fn label(&self) -> impl Iterator<Item = Inline> + '_ {
436 inline_children(self.syntax())
437 }
438}
439
440impl Image {
441 pub fn alt(&self) -> String {
443 payload_text(self.syntax(), SyntaxKind::TEXT)
444 }
445
446 pub fn destination(&self) -> String {
448 payload_text(self.syntax(), SyntaxKind::LINK_DESTINATION)
449 }
450}
451
452impl Autolink {
453 pub fn destination(&self) -> String {
455 payload_text(self.syntax(), SyntaxKind::LINK_DESTINATION)
456 }
457}
458
459impl FootnoteReference {
460 pub fn id(&self) -> String {
462 token(self.syntax(), SyntaxKind::FOOTNOTE_LABEL)
463 .map(|token| {
464 token
465 .text()
466 .strip_prefix("[^")
467 .and_then(|text| text.strip_suffix(']'))
468 .unwrap_or(token.text())
469 .to_string()
470 })
471 .unwrap_or_default()
472 }
473}
474
475impl Interpolation {
476 pub fn expression(&self) -> String {
478 payload_text(self.syntax(), SyntaxKind::TEXT)
479 }
480}
481
482#[derive(Debug, Clone, PartialEq, Eq)]
484pub enum FenceKind {
485 Plain,
486 Julia,
487 JuliaRepl,
488 JlDoctest {
489 label: Option<String>,
490 options: Option<String>,
491 },
492 Documenter {
493 directive: DocumenterDirective,
494 arguments: Option<String>,
495 },
496 DocumenterExtension {
497 directive: String,
498 arguments: Option<String>,
499 },
500 Other(String),
501}
502
503impl FenceKind {
504 pub fn contains_julia(&self) -> bool {
506 match self {
507 Self::Julia | Self::JuliaRepl | Self::JlDoctest { .. } => true,
508 Self::Documenter { directive, .. } => !matches!(directive, DocumenterDirective::Raw),
509 Self::Plain | Self::DocumenterExtension { .. } | Self::Other(_) => false,
510 }
511 }
512}
513
514#[derive(Debug, Clone, Copy, PartialEq, Eq)]
516pub enum DocumenterDirective {
517 Docs,
518 Autodocs,
519 Meta,
520 Index,
521 Contents,
522 Example,
523 Repl,
524 Setup,
525 Eval,
526 Raw,
527}
528
529impl Link {
530 pub fn documenter_link(&self) -> Option<DocumenterLink> {
532 let text = self.destination();
533 let destination = self.syntax().clone();
534 text.trim_start()
535 .starts_with('@')
536 .then_some(DocumenterLink { destination, text })
537 }
538}
539
540#[derive(Debug, Clone, PartialEq, Eq, Hash)]
542pub struct DocumenterLink {
543 destination: SyntaxNode,
544 text: String,
545}
546
547impl DocumenterLink {
548 pub fn kind(&self) -> DocumenterLinkKind<'_> {
550 let (name, _) = split_at_word(self.text.trim());
551 match name {
552 "@ref" => DocumenterLinkKind::Ref,
553 "@id" => DocumenterLinkKind::Id,
554 other => DocumenterLinkKind::Extension(other.trim_start_matches('@')),
555 }
556 }
557
558 pub fn target(&self) -> Option<&str> {
560 let (_, rest) = split_at_word(self.text.trim());
561 (!rest.is_empty()).then_some(rest)
562 }
563
564 pub fn target_range(&self) -> Option<TextRange> {
566 let raw = &self.text;
567 let trimmed_start = raw.len() - raw.trim_start().len();
568 let trimmed = raw.trim();
569 let name_end = trimmed.find(char::is_whitespace)?;
570 let target_start =
571 name_end + trimmed[name_end..].len() - trimmed[name_end..].trim_start().len();
572 if target_start >= trimmed.len() {
573 return None;
574 }
575 let target_end = trimmed.trim_end().len();
576 payload_range(
577 &self.destination,
578 SyntaxKind::LINK_DESTINATION,
579 trimmed_start + target_start,
580 trimmed_start + target_end,
581 )
582 }
583}
584
585#[derive(Debug, Clone, Copy, PartialEq, Eq)]
587pub enum DocumenterLinkKind<'a> {
588 Ref,
589 Id,
590 Extension(&'a str),
591}
592
593fn classify_fence(info: &str) -> FenceKind {
594 if info.is_empty() {
595 return FenceKind::Plain;
596 }
597 if info == "julia" {
598 return FenceKind::Julia;
599 }
600 if info == "julia-repl" {
601 return FenceKind::JuliaRepl;
602 }
603 if info == "jldoctest"
604 || info
605 .strip_prefix("jldoctest")
606 .is_some_and(|rest| rest.starts_with(|ch: char| ch.is_whitespace() || ch == ';'))
607 {
608 let rest = info.strip_prefix("jldoctest").unwrap().trim();
609 let (label, options) = split_label_options(rest);
610 return FenceKind::JlDoctest {
611 label: nonempty(label),
612 options: nonempty(options),
613 };
614 }
615 if let Some(at) = info.strip_prefix('@') {
616 let name_end = at
617 .find(|ch: char| ch.is_whitespace() || ch == ';')
618 .unwrap_or(at.len());
619 let name = &at[..name_end];
620 let arguments = nonempty(at[name_end..].trim().trim_start_matches(';').trim());
621 let directive = match name {
622 "docs" => Some(DocumenterDirective::Docs),
623 "autodocs" => Some(DocumenterDirective::Autodocs),
624 "meta" => Some(DocumenterDirective::Meta),
625 "index" => Some(DocumenterDirective::Index),
626 "contents" => Some(DocumenterDirective::Contents),
627 "example" => Some(DocumenterDirective::Example),
628 "repl" => Some(DocumenterDirective::Repl),
629 "setup" => Some(DocumenterDirective::Setup),
630 "eval" => Some(DocumenterDirective::Eval),
631 "raw" => Some(DocumenterDirective::Raw),
632 _ => None,
633 };
634 return match directive {
635 Some(directive) => FenceKind::Documenter {
636 directive,
637 arguments,
638 },
639 None => FenceKind::DocumenterExtension {
640 directive: name.to_string(),
641 arguments,
642 },
643 };
644 }
645 FenceKind::Other(info.to_string())
646}
647
648fn split_label_options(rest: &str) -> (&str, &str) {
649 match rest.split_once(';') {
650 Some((label, options)) => (label.trim(), options.trim()),
651 None => (rest.trim(), ""),
652 }
653}
654
655fn nonempty(text: &str) -> Option<String> {
656 (!text.is_empty()).then(|| text.to_string())
657}
658
659fn split_at_word(text: &str) -> (&str, &str) {
660 match text.find(char::is_whitespace) {
661 Some(index) => (&text[..index], text[index..].trim()),
662 None => (text, ""),
663 }
664}
665
666fn token(node: &SyntaxNode, kind: SyntaxKind) -> Option<SyntaxToken> {
672 node.children_with_tokens()
673 .filter_map(|element| element.into_token())
674 .find(|token| token.kind() == kind)
675}
676
677fn nested_token(node: &SyntaxNode, kind: SyntaxKind) -> Option<SyntaxToken> {
679 node.descendants_with_tokens()
680 .filter_map(|element| element.into_token())
681 .find(|token| token.kind() == kind)
682}
683
684fn visible_text(node: &SyntaxNode) -> String {
685 let mut out = String::new();
686 for token in node
687 .descendants_with_tokens()
688 .filter_map(|element| element.into_token())
689 {
690 match token.kind() {
691 SyntaxKind::TEXT | SyntaxKind::CODE_CONTENT | SyntaxKind::MATH_CONTENT => {
692 out.push_str(token.text())
693 }
694 SyntaxKind::ESCAPE => out.push_str(token.text().trim_start_matches('\\')),
695 SyntaxKind::EN_DASH => out.push('–'),
696 SyntaxKind::EM_DASH => out.push('—'),
697 SyntaxKind::SOFT_BREAK => out.push(' '),
698 _ => {}
699 }
700 }
701 out.trim().to_string()
702}
703
704fn inline_children(node: &SyntaxNode) -> impl Iterator<Item = Inline> + '_ {
705 node.children_with_tokens()
706 .filter_map(|element| match element {
707 rowan::NodeOrToken::Token(token) => match token.kind() {
708 SyntaxKind::TEXT => Some(Inline::Text(token)),
709 SyntaxKind::ESCAPE => Some(Inline::Escape(token)),
710 SyntaxKind::EN_DASH => Some(Inline::EnDash(token)),
711 SyntaxKind::EM_DASH => Some(Inline::EmDash(token)),
712 SyntaxKind::SOFT_BREAK => Some(Inline::SoftBreak(token)),
713 _ => None,
714 },
715 rowan::NodeOrToken::Node(node) => match node.kind() {
716 SyntaxKind::EMPHASIS => Some(Inline::Emphasis(Emphasis(node))),
717 SyntaxKind::STRONG => Some(Inline::Strong(Strong(node))),
718 SyntaxKind::INLINE_CODE => Some(Inline::Code(InlineCode(node))),
719 SyntaxKind::INLINE_MATH => Some(Inline::Math(InlineMath(node))),
720 SyntaxKind::LINK => Some(Inline::Link(Link(node))),
721 SyntaxKind::IMAGE => Some(Inline::Image(Image(node))),
722 SyntaxKind::AUTOLINK => Some(Inline::Autolink(Autolink(node))),
723 SyntaxKind::FOOTNOTE_REFERENCE => {
724 Some(Inline::FootnoteReference(FootnoteReference(node)))
725 }
726 SyntaxKind::INTERPOLATION => Some(Inline::Interpolation(Interpolation(node))),
727 SyntaxKind::HARD_BREAK => Some(Inline::HardBreak(HardBreak(node))),
728 _ => None,
729 },
730 })
731}
732
733fn inline_payload(node: &SyntaxNode, kind: SyntaxKind) -> String {
734 payload_text(node, kind).trim().to_string()
735}
736
737fn payload_tokens(node: &SyntaxNode, kind: SyntaxKind) -> impl Iterator<Item = SyntaxToken> + '_ {
738 node.children_with_tokens()
739 .filter_map(|element| element.into_token())
740 .filter(move |token| token.kind() == kind || token.kind() == SyntaxKind::NEWLINE)
741}
742
743fn payload_text(node: &SyntaxNode, kind: SyntaxKind) -> String {
744 let mut out = String::new();
745 for token in payload_tokens(node, kind) {
746 if token.kind() == SyntaxKind::NEWLINE {
747 out.push('\n');
748 } else if matches!(token.text(), "\n" | "\r\n") {
749 out.push(' ');
750 } else {
751 out.push_str(token.text());
752 }
753 }
754 out
755}
756
757fn payload_range(
758 node: &SyntaxNode,
759 kind: SyntaxKind,
760 start: usize,
761 end: usize,
762) -> Option<TextRange> {
763 let mut offset = 0;
764 let mut source_start = None;
765 for token in payload_tokens(node, kind) {
766 let length = if matches!(token.text(), "\n" | "\r\n") {
767 1
768 } else {
769 token.text().len()
770 };
771 if source_start.is_none() && start < offset + length {
772 source_start =
773 Some(token.text_range().start() + TextSize::from((start - offset) as u32));
774 }
775 if end <= offset + length {
776 let source_end = if end == offset + length {
777 token.text_range().end()
778 } else {
779 token.text_range().start() + TextSize::from((end - offset) as u32)
780 };
781 return Some(TextRange::new(source_start?, source_end));
782 }
783 offset += length;
784 }
785 None
786}
787
788fn uppercase_first(text: &str) -> String {
789 let mut chars = text.chars();
790 chars
791 .next()
792 .map(char::to_uppercase)
793 .into_iter()
794 .flatten()
795 .collect::<String>()
796 + chars.as_str()
797}