diff --git a/crates/biome_css_parser/src/parser.rs b/crates/biome_css_parser/src/parser.rs index e97b957fdefb..dc95d2e77606 100644 --- a/crates/biome_css_parser/src/parser.rs +++ b/crates/biome_css_parser/src/parser.rs @@ -1,7 +1,7 @@ use crate::lexer::CssReLexContext; use crate::state::CssParserState; use crate::token_source::{CssTokenSource, CssTokenSourceCheckpoint}; -use biome_css_syntax::CssSyntaxKind; +use biome_css_syntax::{CssFileSource, CssSyntaxKind}; use biome_parser::ParserContext; use biome_parser::diagnostic::merge_diagnostics; use biome_parser::event::Event; @@ -170,3 +170,17 @@ pub struct CssParserCheckpoint { // scoped properties that aren't only dependent on checkpoints and // should be reset manually when the scope of their use is exited. } + +impl From<&CssFileSource> for CssParserOptions { + fn from(file_source: &CssFileSource) -> Self { + let mut options = Self::default(); + if file_source.is_css_modules() { + options.css_modules = true; + } + if file_source.is_tailwind_css() { + options.tailwind_directives = true; + } + + options + } +} diff --git a/crates/biome_css_syntax/src/file_source.rs b/crates/biome_css_syntax/src/file_source.rs index c803b2e11cd2..a3f0de6fe18f 100644 --- a/crates/biome_css_syntax/src/file_source.rs +++ b/crates/biome_css_syntax/src/file_source.rs @@ -22,9 +22,13 @@ pub struct CssFileSource { Debug, Clone, Default, Copy, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize, )] #[serde(rename_all = "camelCase")] -enum CssVariant { +pub enum CssVariant { #[default] Standard, + /// The file is a CSS module + CssModules, + /// The file belongs to tailwind + TailwindCss, } impl CssFileSource { @@ -34,6 +38,24 @@ impl CssFileSource { } } + pub fn tailwind_css() -> Self { + Self { + variant: CssVariant::TailwindCss, + } + } + + pub fn is_css_modules(&self) -> bool { + self.variant == CssVariant::CssModules + } + + pub fn is_tailwind_css(&self) -> bool { + self.variant == CssVariant::TailwindCss + } + + pub fn set_variant(&mut self, variant: CssVariant) { + self.variant = variant; + } + /// Try to return the CSS file source corresponding to this file name from well-known files pub fn try_from_well_known(_: &Utf8Path) -> Result { // TODO: to be implemented @@ -58,7 +80,7 @@ impl CssFileSource { pub fn try_from_language_id(language_id: &str) -> Result { match language_id { "css" => Ok(Self::css()), - "tailwindcss" => Ok(Self::css()), + "tailwindcss" => Ok(Self::tailwind_css()), _ => Err(FileSourceError::UnknownLanguageId), } } diff --git a/crates/biome_css_syntax/src/lib.rs b/crates/biome_css_syntax/src/lib.rs index a1e08f3f38f9..1658957b52fd 100644 --- a/crates/biome_css_syntax/src/lib.rs +++ b/crates/biome_css_syntax/src/lib.rs @@ -12,7 +12,7 @@ pub use self::generated::*; pub use biome_rowan::{ SyntaxNodeText, TextLen, TextRange, TextSize, TokenAtOffset, TriviaPieceKind, WalkEvent, }; -pub use file_source::CssFileSource; +pub use file_source::{CssFileSource, CssVariant}; pub use syntax_node::*; use crate::CssSyntaxKind::*; diff --git a/crates/biome_service/src/workspace/server.rs b/crates/biome_service/src/workspace/server.rs index a49ad67a1577..853566d0680a 100644 --- a/crates/biome_service/src/workspace/server.rs +++ b/crates/biome_service/src/workspace/server.rs @@ -22,6 +22,7 @@ use biome_configuration::bool::Bool; use biome_configuration::max_size::MaxSize; use biome_configuration::vcs::VcsClientKind; use biome_configuration::{BiomeDiagnostic, Configuration, ConfigurationPathHint}; +use biome_css_syntax::CssVariant; use biome_deserialize::json::deserialize_from_json_str; use biome_deserialize::{Deserialized, Merge}; use biome_diagnostics::print_diagnostic_to_string; @@ -352,6 +353,28 @@ impl WorkspaceServer { } } + if let DocumentFileSource::Css(css) = &mut source { + if settings + .languages + .css + .parser + .css_modules_enabled + .unwrap_or_default() + .into() + { + css.set_variant(CssVariant::CssModules) + } else if settings + .languages + .css + .parser + .tailwind_directives + .unwrap_or_default() + .into() + { + css.set_variant(CssVariant::TailwindCss) + } + } + let (content, version) = match content { FileContent::FromClient { content, version } => (content, Some(version)), FileContent::FromServer => (self.fs.read_file_from_path(&path)?, None), diff --git a/packages/@biomejs/backend-jsonrpc/src/workspace.ts b/packages/@biomejs/backend-jsonrpc/src/workspace.ts index 5cc0cf9e1c22..5f9d98b7d481 100644 --- a/packages/@biomejs/backend-jsonrpc/src/workspace.ts +++ b/packages/@biomejs/backend-jsonrpc/src/workspace.ts @@ -7131,7 +7131,7 @@ the latest Recommendation level standards. It also supports Tailwind CSS syntax additions, when the parser option is enabled. */ -export type CssVariant = "standard"; +export type CssVariant = "standard" | "cssModules" | "tailwindCss"; /** * The style of GraphQL contained in the file. */