Skip to content

Commit 6f5b876

Browse files
chore(core): add CSS variant information to the source file (#8095)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent b406db6 commit 6f5b876

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

crates/biome_css_parser/src/parser.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::lexer::CssReLexContext;
22
use crate::state::CssParserState;
33
use crate::token_source::{CssTokenSource, CssTokenSourceCheckpoint};
4-
use biome_css_syntax::CssSyntaxKind;
4+
use biome_css_syntax::{CssFileSource, CssSyntaxKind};
55
use biome_parser::ParserContext;
66
use biome_parser::diagnostic::merge_diagnostics;
77
use biome_parser::event::Event;
@@ -170,3 +170,17 @@ pub struct CssParserCheckpoint {
170170
// scoped properties that aren't only dependent on checkpoints and
171171
// should be reset manually when the scope of their use is exited.
172172
}
173+
174+
impl From<&CssFileSource> for CssParserOptions {
175+
fn from(file_source: &CssFileSource) -> Self {
176+
let mut options = Self::default();
177+
if file_source.is_css_modules() {
178+
options.css_modules = true;
179+
}
180+
if file_source.is_tailwind_css() {
181+
options.tailwind_directives = true;
182+
}
183+
184+
options
185+
}
186+
}

crates/biome_css_syntax/src/file_source.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ pub struct CssFileSource {
2222
Debug, Clone, Default, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize,
2323
)]
2424
#[serde(rename_all = "camelCase")]
25-
enum CssVariant {
25+
pub enum CssVariant {
2626
#[default]
2727
Standard,
28+
/// The file is a CSS module
29+
CssModules,
30+
/// The file belongs to tailwind
31+
TailwindCss,
2832
}
2933

3034
impl CssFileSource {
@@ -34,6 +38,24 @@ impl CssFileSource {
3438
}
3539
}
3640

41+
pub fn tailwind_css() -> Self {
42+
Self {
43+
variant: CssVariant::TailwindCss,
44+
}
45+
}
46+
47+
pub fn is_css_modules(&self) -> bool {
48+
self.variant == CssVariant::CssModules
49+
}
50+
51+
pub fn is_tailwind_css(&self) -> bool {
52+
self.variant == CssVariant::TailwindCss
53+
}
54+
55+
pub fn set_variant(&mut self, variant: CssVariant) {
56+
self.variant = variant;
57+
}
58+
3759
/// Try to return the CSS file source corresponding to this file name from well-known files
3860
pub fn try_from_well_known(_: &Utf8Path) -> Result<Self, FileSourceError> {
3961
// TODO: to be implemented
@@ -58,7 +80,7 @@ impl CssFileSource {
5880
pub fn try_from_language_id(language_id: &str) -> Result<Self, FileSourceError> {
5981
match language_id {
6082
"css" => Ok(Self::css()),
61-
"tailwindcss" => Ok(Self::css()),
83+
"tailwindcss" => Ok(Self::tailwind_css()),
6284
_ => Err(FileSourceError::UnknownLanguageId),
6385
}
6486
}

crates/biome_css_syntax/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use self::generated::*;
1212
pub use biome_rowan::{
1313
SyntaxNodeText, TextLen, TextRange, TextSize, TokenAtOffset, TriviaPieceKind, WalkEvent,
1414
};
15-
pub use file_source::CssFileSource;
15+
pub use file_source::{CssFileSource, CssVariant};
1616
pub use syntax_node::*;
1717

1818
use crate::CssSyntaxKind::*;

crates/biome_service/src/workspace/server.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use biome_configuration::bool::Bool;
2222
use biome_configuration::max_size::MaxSize;
2323
use biome_configuration::vcs::VcsClientKind;
2424
use biome_configuration::{BiomeDiagnostic, Configuration, ConfigurationPathHint};
25+
use biome_css_syntax::CssVariant;
2526
use biome_deserialize::json::deserialize_from_json_str;
2627
use biome_deserialize::{Deserialized, Merge};
2728
use biome_diagnostics::print_diagnostic_to_string;
@@ -352,6 +353,28 @@ impl WorkspaceServer {
352353
}
353354
}
354355

356+
if let DocumentFileSource::Css(css) = &mut source {
357+
if settings
358+
.languages
359+
.css
360+
.parser
361+
.css_modules_enabled
362+
.unwrap_or_default()
363+
.into()
364+
{
365+
css.set_variant(CssVariant::CssModules)
366+
} else if settings
367+
.languages
368+
.css
369+
.parser
370+
.tailwind_directives
371+
.unwrap_or_default()
372+
.into()
373+
{
374+
css.set_variant(CssVariant::TailwindCss)
375+
}
376+
}
377+
355378
let (content, version) = match content {
356379
FileContent::FromClient { content, version } => (content, Some(version)),
357380
FileContent::FromServer => (self.fs.read_file_from_path(&path)?, None),

packages/@biomejs/backend-jsonrpc/src/workspace.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)