1+ mod associations;
12mod editorconfig;
23#[ cfg( feature = "napi" ) ]
34mod js_config;
@@ -11,6 +12,7 @@ pub use nested::NestedConfigCtx;
1112pub use scopes:: ConfigScopes ;
1213
1314use std:: {
15+ borrow:: Cow ,
1416 path:: { Path , PathBuf } ,
1517 sync:: Arc ,
1618} ;
@@ -24,18 +26,26 @@ use tracing::instrument;
2426use oxc_config:: { ConfigDiscovery , DiscoveredConfigFile , is_js_config_path, vp_version} ;
2527
2628use self :: {
29+ associations:: OxfmtrcAssociations ,
2730 editorconfig:: { apply_editorconfig, resolve_editorconfig_overrides, root_properties} ,
2831 overrides:: OxfmtrcOverrides ,
2932} ;
3033use super :: {
31- FormatPlan ,
34+ FormatPlan , Language ,
3235 global_ignore:: matches_with_ancestors,
3336 options:: { ValidatedOptions , validate} ,
3437 oxfmtrc:: { FormatConfig , Oxfmtrc } ,
35- support:: FormatStrategy ,
38+ support:: { FormatStrategy , classify_as , classify_file } ,
3639 utils,
3740} ;
3841
42+ /// `path` relative to the config directory, as written glob patterns expect.
43+ fn relative_path < ' a > ( base_dir : Option < & Path > , path : & ' a Path ) -> Cow < ' a , str > {
44+ // NOTE: On Windows, `to_string_lossy()` produces `\`-separated paths.
45+ // This is OK since `fast_glob::glob_match()` supports both `/` and `\` via `std::path::is_separator`.
46+ base_dir. and_then ( |dir| path. strip_prefix ( dir) . ok ( ) ) . unwrap_or ( path) . to_string_lossy ( )
47+ }
48+
3949pub fn config_discovery ( ) -> ConfigDiscovery {
4050 if cfg ! ( feature = "napi" ) && vp_version ( ) . is_some ( ) {
4151 ConfigDiscovery :: vite_plus ( )
@@ -275,6 +285,8 @@ pub struct ConfigResolver {
275285 base : Option < ( Arc < FormatConfig > , Arc < ValidatedOptions > ) > ,
276286 /// Resolved overrides from `.oxfmtrc` for file-specific matching.
277287 oxfmtrc_overrides : Option < OxfmtrcOverrides > ,
288+ /// Resolved `associations` from `.oxfmtrc`, consulted before the built-in classification.
289+ associations : Option < OxfmtrcAssociations > ,
278290 /// Ignore glob built from this config's `ignorePatterns`.
279291 ignore_glob : Option < Gitignore > ,
280292 /// Parsed `.editorconfig`, if any.
@@ -295,6 +307,7 @@ impl ConfigResolver {
295307 config_dir,
296308 base : None ,
297309 oxfmtrc_overrides : None ,
310+ associations : None ,
298311 ignore_glob : None ,
299312 editorconfig,
300313 }
@@ -348,6 +361,7 @@ impl ConfigResolver {
348361 /// - `self.base` is set to the validated `FormatConfig` snapshot
349362 /// (with `.editorconfig` `[*]` already folded in) paired with its gate artifacts
350363 /// - `self.oxfmtrc_overrides` is set if `overrides` exists
364+ /// - `self.associations` is set if `associations` is not empty
351365 /// - `self.ignore_glob` is built from `ignorePatterns`
352366 ///
353367 /// Validation runs eagerly via `validate()`,
@@ -359,10 +373,14 @@ impl ConfigResolver {
359373 pub fn build_and_validate ( & mut self ) -> Result < ( ) , String > {
360374 let oxfmtrc = Oxfmtrc :: deserialize ( & self . raw_config ) . map_err ( |err| err. to_string ( ) ) ?;
361375
362- // Resolve `overrides` from `Oxfmtrc` for later per-file matching
363- let base_dir = self . config_dir . clone ( ) ;
364- self . oxfmtrc_overrides =
365- oxfmtrc. overrides . map ( |overrides| OxfmtrcOverrides :: new ( overrides, base_dir) ) ;
376+ // Resolve `overrides` and `associations` from `Oxfmtrc` for later per-file matching
377+ self . oxfmtrc_overrides = oxfmtrc
378+ . overrides
379+ . map ( |overrides| OxfmtrcOverrides :: new ( overrides, self . config_dir . clone ( ) ) ) ;
380+ self . associations = oxfmtrc
381+ . associations
382+ . filter ( |associations| !associations. is_empty ( ) )
383+ . map ( |associations| OxfmtrcAssociations :: new ( associations, self . config_dir . clone ( ) ) ) ;
366384
367385 let mut format_config = oxfmtrc. format_config ;
368386
@@ -390,6 +408,18 @@ impl ConfigResolver {
390408 Ok ( ( ) )
391409 }
392410
411+ /// Classify `path`: the given `language` first (e.g. LSP `languageId`),
412+ /// then this config's `associations`, then the file name.
413+ ///
414+ /// A given or matched language decides alone, see [`classify_as`] for when it yields `None`.
415+ pub fn classify ( & self , path : & Path , language : Option < Language > ) -> Option < FormatStrategy > {
416+ let language = language. or_else ( || self . associations . as_ref ( ) ?. matching ( path) ) ;
417+ match language {
418+ Some ( language) => classify_as ( language, path) ,
419+ None => classify_file ( path) ,
420+ }
421+ }
422+
393423 /// Resolve options for a pre-classified file and build a [`ResolveOutcome`].
394424 ///
395425 /// Returns `Err` only when the merged config (after override application) fails validation.
0 commit comments