|
| 1 | +use crate::FormatHtmlSyntaxToken; |
| 2 | +use crate::prelude::*; |
| 3 | +use biome_formatter::separated::{ |
| 4 | + FormatSeparatedElementRule, FormatSeparatedIter, TrailingSeparator, |
| 5 | +}; |
| 6 | +use biome_formatter::{FormatRefWithRule, FormatRuleWithOptions}; |
| 7 | +use biome_html_syntax::{HtmlLanguage, HtmlSyntaxToken, SvelteName}; |
| 8 | +use biome_rowan::{AstNode, AstSeparatedList, AstSeparatedListElementsIterator}; |
| 9 | +use std::marker::PhantomData; |
| 10 | + |
| 11 | +#[derive(Clone)] |
| 12 | +pub(crate) struct HtmlFormatSeparatedElementRule<N> { |
| 13 | + node: PhantomData<N>, |
| 14 | +} |
| 15 | + |
| 16 | +impl<N> FormatSeparatedElementRule<N> for HtmlFormatSeparatedElementRule<N> |
| 17 | +where |
| 18 | + N: AstNode<Language = HtmlLanguage> + AsFormat<HtmlFormatContext> + 'static, |
| 19 | +{ |
| 20 | + type Context = HtmlFormatContext; |
| 21 | + type FormatNode<'a> = N::Format<'a>; |
| 22 | + type FormatSeparator<'a> = FormatRefWithRule<'a, HtmlSyntaxToken, FormatHtmlSyntaxToken>; |
| 23 | + |
| 24 | + fn format_node<'a>(&self, node: &'a N) -> Self::FormatNode<'a> { |
| 25 | + node.format() |
| 26 | + } |
| 27 | + |
| 28 | + fn format_separator<'a>(&self, separator: &'a HtmlSyntaxToken) -> Self::FormatSeparator<'a> { |
| 29 | + separator.format() |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +type HtmlFormatSeparatedIter<Node, C> = FormatSeparatedIter< |
| 34 | + AstSeparatedListElementsIterator<HtmlLanguage, Node>, |
| 35 | + Node, |
| 36 | + HtmlFormatSeparatedElementRule<Node>, |
| 37 | + C, |
| 38 | +>; |
| 39 | + |
| 40 | +/// AST Separated list formatting extension methods |
| 41 | +pub(crate) trait FormatAstSeparatedListExtension: |
| 42 | + AstSeparatedList<Language = HtmlLanguage> |
| 43 | +{ |
| 44 | + /// Prints a separated list of nodes |
| 45 | + /// |
| 46 | + /// Trailing separators will be reused from the original list or created by |
| 47 | + /// calling the `separator_factory` function. The last trailing separator |
| 48 | + /// will not be printed by default. Use `with_trailing_separator` to add it |
| 49 | + /// in where necessary. |
| 50 | + fn format_separated( |
| 51 | + &self, |
| 52 | + separator: &'static str, |
| 53 | + ) -> HtmlFormatSeparatedIter<Self::Node, HtmlFormatContext> { |
| 54 | + HtmlFormatSeparatedIter::new( |
| 55 | + self.elements(), |
| 56 | + separator, |
| 57 | + HtmlFormatSeparatedElementRule { node: PhantomData }, |
| 58 | + on_skipped, |
| 59 | + on_removed, |
| 60 | + ) |
| 61 | + .with_trailing_separator(TrailingSeparator::Disallowed) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<T> FormatAstSeparatedListExtension for T where T: AstSeparatedList<Language = HtmlLanguage> {} |
| 66 | + |
| 67 | +#[derive(Default, Debug, Clone, Copy)] |
| 68 | +pub(crate) struct HtmlFormatSeparatedElementRuleWithOptions<N, O> { |
| 69 | + node: PhantomData<N>, |
| 70 | + options: O, |
| 71 | +} |
| 72 | + |
| 73 | +impl<N, O> HtmlFormatSeparatedElementRuleWithOptions<N, O> { |
| 74 | + pub(crate) fn new(options: O) -> Self { |
| 75 | + Self { |
| 76 | + node: PhantomData, |
| 77 | + options, |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<N, O, R> FormatSeparatedElementRule<N> for HtmlFormatSeparatedElementRuleWithOptions<N, O> |
| 83 | +where |
| 84 | + O: Clone + Copy, |
| 85 | + R: FormatNodeRule<N> + FormatRuleWithOptions<N, Context = HtmlFormatContext, Options = O>, |
| 86 | + N: AstNode<Language = HtmlLanguage> |
| 87 | + + for<'a> AsFormat<HtmlFormatContext, Format<'a> = FormatRefWithRule<'a, N, R>> |
| 88 | + + 'static, |
| 89 | +{ |
| 90 | + type Context = HtmlFormatContext; |
| 91 | + type FormatNode<'a> = FormatRefWithRule<'a, N, R>; |
| 92 | + type FormatSeparator<'a> = FormatRefWithRule<'a, HtmlSyntaxToken, FormatHtmlSyntaxToken>; |
| 93 | + |
| 94 | + fn format_node<'a>(&self, node: &'a N) -> Self::FormatNode<'a> { |
| 95 | + node.format().with_options(self.options) |
| 96 | + } |
| 97 | + |
| 98 | + fn format_separator<'a>(&self, separator: &'a HtmlSyntaxToken) -> Self::FormatSeparator<'a> { |
| 99 | + separator.format() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +type HtmlFormatSeparatedIterWithOptions<Node, Options, C> = FormatSeparatedIter< |
| 104 | + AstSeparatedListElementsIterator<HtmlLanguage, Node>, |
| 105 | + Node, |
| 106 | + HtmlFormatSeparatedElementRuleWithOptions<Node, Options>, |
| 107 | + C, |
| 108 | +>; |
| 109 | + |
| 110 | +/// AST Separated list formatting extension methods with options |
| 111 | +#[expect(dead_code)] |
| 112 | +pub(crate) trait FormatAstSeparatedListWithOptionsExtension<O>: |
| 113 | + AstSeparatedList<Language = HtmlLanguage> |
| 114 | +{ |
| 115 | + /// Prints a separated list of nodes with options |
| 116 | + /// |
| 117 | + /// Trailing separators will be reused from the original list or created by |
| 118 | + /// calling the `separator_factory` function. The last trailing separator |
| 119 | + /// will not be printed by default. Use `with_trailing_separator` to add it |
| 120 | + /// in where necessary. |
| 121 | + fn format_separated_with_options( |
| 122 | + &self, |
| 123 | + separator: &'static str, |
| 124 | + options: O, |
| 125 | + ) -> HtmlFormatSeparatedIterWithOptions<Self::Node, O, HtmlFormatContext> { |
| 126 | + FormatSeparatedIter::new( |
| 127 | + self.elements(), |
| 128 | + separator, |
| 129 | + HtmlFormatSeparatedElementRuleWithOptions::new(options), |
| 130 | + on_skipped, |
| 131 | + on_removed, |
| 132 | + ) |
| 133 | + .with_trailing_separator(TrailingSeparator::Disallowed) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +impl<T, O> FormatAstSeparatedListWithOptionsExtension<O> for T where |
| 138 | + // TODO: probably it requires an update because it's tight to svelte grammar |
| 139 | + T: AstSeparatedList<Language = HtmlLanguage, Node = SvelteName> |
| 140 | +{ |
| 141 | +} |
0 commit comments