|
1 | 1 | use crate::prelude::*; |
2 | 2 | use biome_css_syntax::{ |
3 | | - AnyCssDeclarationName, AnyCssRoot, CssComplexSelector, CssFunction, CssIdentifier, CssLanguage, |
4 | | - CssSyntaxKind, TextLen, TextSize, |
| 3 | + AnyCssDeclarationName, AnyCssRoot, CssComplexSelector, CssFunction, CssGenericProperty, |
| 4 | + CssIdentifier, CssLanguage, CssSyntaxKind, TextLen, TextSize, |
5 | 5 | }; |
6 | 6 | use biome_diagnostics::category; |
7 | 7 | use biome_formatter::comments::{ |
@@ -99,21 +99,70 @@ impl CommentStyle for CssCommentStyle { |
99 | 99 | ) -> CommentPlacement<Self::Language> { |
100 | 100 | match comment.text_position() { |
101 | 101 | CommentTextPosition::EndOfLine => handle_function_comment(comment) |
| 102 | + .or_else(handle_generic_property_comment) |
102 | 103 | .or_else(handle_declaration_name_comment) |
103 | 104 | .or_else(handle_complex_selector_comment) |
104 | 105 | .or_else(handle_global_suppression), |
105 | 106 | CommentTextPosition::OwnLine => handle_function_comment(comment) |
| 107 | + .or_else(handle_generic_property_comment) |
106 | 108 | .or_else(handle_declaration_name_comment) |
107 | 109 | .or_else(handle_complex_selector_comment) |
108 | 110 | .or_else(handle_global_suppression), |
109 | 111 | CommentTextPosition::SameLine => handle_function_comment(comment) |
| 112 | + .or_else(handle_generic_property_comment) |
110 | 113 | .or_else(handle_declaration_name_comment) |
111 | 114 | .or_else(handle_complex_selector_comment) |
112 | 115 | .or_else(handle_global_suppression), |
113 | 116 | } |
114 | 117 | } |
115 | 118 | } |
116 | 119 |
|
| 120 | +fn handle_generic_property_comment( |
| 121 | + comment: DecoratedComment<CssLanguage>, |
| 122 | +) -> CommentPlacement<CssLanguage> { |
| 123 | + // Check if the comment is inside a CSS generic property (e.g., color: value) |
| 124 | + let Some(generic_property) = comment |
| 125 | + .enclosing_node() |
| 126 | + .ancestors() |
| 127 | + .find_map(CssGenericProperty::cast) |
| 128 | + else { |
| 129 | + return CommentPlacement::Default(comment); |
| 130 | + }; |
| 131 | + |
| 132 | + let Ok(name) = generic_property.name() else { |
| 133 | + return CommentPlacement::Default(comment); |
| 134 | + }; |
| 135 | + |
| 136 | + let comment_piece = comment.piece(); |
| 137 | + |
| 138 | + // Check if the comment is in the name's trailing trivia (before colon) |
| 139 | + // Example: `color /* comment */: value` |
| 140 | + if let Some(name_token) = name.syntax().last_token() { |
| 141 | + for piece in name_token.trailing_trivia().pieces() { |
| 142 | + if piece.is_comments() && piece.text() == comment_piece.text() { |
| 143 | + // Our placement is slightly better than Prettier because it adds some spacing |
| 144 | + return CommentPlacement::trailing(name.into_syntax(), comment); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if let (Some(preceding), Some(following)) = (comment.preceding_node(), comment.following_node()) |
| 150 | + { |
| 151 | + // If preceding is the property name and following is in the value list |
| 152 | + if preceding == name.syntax() |
| 153 | + && following |
| 154 | + .parent() |
| 155 | + .is_some_and(|p| p.kind() == CssSyntaxKind::CSS_GENERIC_COMPONENT_VALUE_LIST) |
| 156 | + { |
| 157 | + // Place comment as dangling on the property so it can be formatted inline |
| 158 | + // between the colon and values |
| 159 | + return CommentPlacement::trailing(generic_property.into_syntax(), comment); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + CommentPlacement::Default(comment) |
| 164 | +} |
| 165 | + |
117 | 166 | fn handle_declaration_name_comment( |
118 | 167 | comment: DecoratedComment<CssLanguage>, |
119 | 168 | ) -> CommentPlacement<CssLanguage> { |
|
0 commit comments