1use std::collections::HashMap;
8use std::path::{Path, PathBuf};
9
10use serde::{Deserialize, Serialize};
11
12use crate::config::{
13 CaseStyle, Config, DangleAlign, FractionalTabPolicy, LineEnding, PerCommandConfig,
14};
15use crate::error::{Error, FileParseError, Result};
16
17#[derive(Debug, Clone, Deserialize, Default, schemars::JsonSchema)]
22#[schemars(title = "cmakefmt configuration")]
23#[serde(default)]
24struct FileConfig {
25 format: FormatSection,
27 style: StyleSection,
29 markup: MarkupSection,
31 #[serde(rename = "per_command_overrides")]
33 per_command_overrides: HashMap<String, PerCommandConfig>,
34 #[serde(rename = "per_command")]
35 #[schemars(skip)]
36 legacy_per_command: HashMap<String, PerCommandConfig>,
37}
38
39#[derive(Debug, Clone, Deserialize, Default, schemars::JsonSchema)]
40#[serde(default)]
41#[serde(deny_unknown_fields)]
42struct FormatSection {
43 disable: Option<bool>,
45 line_ending: Option<LineEnding>,
47 line_width: Option<usize>,
49 tab_size: Option<usize>,
51 use_tabs: Option<bool>,
53 fractional_tab_policy: Option<FractionalTabPolicy>,
55 max_empty_lines: Option<usize>,
57 max_hanging_wrap_lines: Option<usize>,
59 max_hanging_wrap_positional_args: Option<usize>,
61 max_hanging_wrap_groups: Option<usize>,
63 max_rows_cmdline: Option<usize>,
65 always_wrap: Option<Vec<String>>,
67 require_valid_layout: Option<bool>,
69 dangle_parens: Option<bool>,
71 dangle_align: Option<DangleAlign>,
73 min_prefix_length: Option<usize>,
75 max_prefix_length: Option<usize>,
77 space_before_control_paren: Option<bool>,
79 space_before_definition_paren: Option<bool>,
81}
82
83#[derive(Debug, Clone, Deserialize, Default, schemars::JsonSchema)]
84#[serde(default)]
85#[serde(deny_unknown_fields)]
86struct StyleSection {
87 command_case: Option<CaseStyle>,
89 keyword_case: Option<CaseStyle>,
91}
92
93#[derive(Debug, Clone, Deserialize, Default, schemars::JsonSchema)]
94#[serde(default)]
95#[serde(deny_unknown_fields)]
96struct MarkupSection {
97 enable_markup: Option<bool>,
99 reflow_comments: Option<bool>,
101 first_comment_is_literal: Option<bool>,
103 literal_comment_pattern: Option<String>,
105 bullet_char: Option<String>,
107 enum_char: Option<String>,
109 fence_pattern: Option<String>,
111 ruler_pattern: Option<String>,
113 hashruler_min_length: Option<usize>,
115 canonicalize_hashrulers: Option<bool>,
117 explicit_trailing_pattern: Option<String>,
119}
120
121const CONFIG_FILE_NAME_TOML: &str = ".cmakefmt.toml";
122const CONFIG_FILE_NAME_YAML: &str = ".cmakefmt.yaml";
123const CONFIG_FILE_NAME_YML: &str = ".cmakefmt.yml";
124const CONFIG_FILE_NAMES: &[&str] = &[
125 CONFIG_FILE_NAME_YAML,
126 CONFIG_FILE_NAME_YML,
127 CONFIG_FILE_NAME_TOML,
128];
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq)]
131pub(crate) enum ConfigFileFormat {
132 Toml,
133 Yaml,
134}
135
136impl ConfigFileFormat {
137 pub(crate) fn as_str(self) -> &'static str {
138 match self {
139 Self::Toml => "TOML",
140 Self::Yaml => "YAML",
141 }
142 }
143}
144
145#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
147pub enum DumpConfigFormat {
148 Yaml,
150 Toml,
152}
153
154pub fn default_config_template_for(format: DumpConfigFormat) -> String {
160 match format {
161 DumpConfigFormat::Yaml => default_config_template_yaml(),
162 DumpConfigFormat::Toml => default_config_template_toml(),
163 }
164}
165
166pub fn render_effective_config(config: &Config, format: DumpConfigFormat) -> Result<String> {
171 let view = EffectiveConfigFile::from(config);
172 match format {
173 DumpConfigFormat::Yaml => serde_yaml::to_string(&view).map_err(|err| {
174 Error::Formatter(format!("failed to render effective config as YAML: {err}"))
175 }),
176 DumpConfigFormat::Toml => toml::to_string_pretty(&view).map_err(|err| {
177 Error::Formatter(format!("failed to render effective config as TOML: {err}"))
178 }),
179 }
180}
181
182pub fn default_config_template() -> String {
184 default_config_template_for(DumpConfigFormat::Yaml)
185}
186
187pub fn generate_json_schema() -> String {
192 let schema = schemars::schema_for!(FileConfig);
193 serde_json::to_string_pretty(&schema).expect("JSON schema serialization failed")
194}
195
196fn default_config_template_toml() -> String {
197 format!(
198 concat!(
199 "# Default cmakefmt configuration.\n",
200 "# Copy this to .cmakefmt.toml and uncomment the optional settings\n",
201 "# you want to customize.\n\n",
202 "[format]\n",
203 "# Disable formatting entirely (return source unchanged).\n",
204 "# disable = true\n\n",
205 "# Output line-ending style: unix (LF), windows (CRLF), or auto (detect from input).\n",
206 "# line_ending = \"windows\"\n\n",
207 "# Maximum rendered line width before cmakefmt wraps a call.\n",
208 "line_width = {line_width}\n\n",
209 "# Number of spaces per indentation level when use_tabs is false.\n",
210 "tab_size = {tab_size}\n\n",
211 "# Indent with tab characters instead of spaces.\n",
212 "# use_tabs = true\n\n",
213 "# How to handle fractional indentation when use_tabs is true: use-space or round-up.\n",
214 "# fractional_tab_policy = \"round-up\"\n\n",
215 "# Maximum number of consecutive blank lines to preserve.\n",
216 "max_empty_lines = {max_empty_lines}\n\n",
217 "# Maximum wrapped lines to tolerate before switching to a more vertical layout.\n",
218 "max_hanging_wrap_lines = {max_lines_hwrap}\n\n",
219 "# Maximum positional arguments to keep in a hanging-wrap layout.\n",
220 "max_hanging_wrap_positional_args = {max_pargs_hwrap}\n\n",
221 "# Maximum keyword/flag subgroups to keep in a hanging-wrap layout.\n",
222 "max_hanging_wrap_groups = {max_subgroups_hwrap}\n\n",
223 "# Maximum rows a hanging-wrap positional group may consume before nesting is forced.\n",
224 "max_rows_cmdline = {max_rows_cmdline}\n\n",
225 "# Commands that must always use vertical (wrapped) layout.\n",
226 "# always_wrap = [\"target_link_libraries\"]\n\n",
227 "# Return an error if any formatted line exceeds line_width.\n",
228 "# require_valid_layout = true\n\n",
229 "# Put the closing ')' on its own line when a call wraps.\n",
230 "dangle_parens = {dangle_parens}\n\n",
231 "# Alignment strategy for a dangling ')': prefix, open, or close.\n",
232 "dangle_align = \"{dangle_align}\"\n\n",
233 "# Lower heuristic bound used when deciding between compact and wrapped layouts.\n",
234 "min_prefix_length = {min_prefix_chars}\n\n",
235 "# Upper heuristic bound used when deciding between compact and wrapped layouts.\n",
236 "max_prefix_length = {max_prefix_chars}\n\n",
237 "# Insert a space before '(' for control-flow commands like if/foreach.\n",
238 "# space_before_control_paren = true\n\n",
239 "# Insert a space before '(' for function() and macro() definitions.\n",
240 "# space_before_definition_paren = true\n\n",
241 "[style]\n",
242 "# Output casing for command names: lower, upper, or unchanged.\n",
243 "command_case = \"{command_case}\"\n\n",
244 "# Output casing for recognized keywords and flags: lower, upper, or unchanged.\n",
245 "keyword_case = \"{keyword_case}\"\n\n",
246 "[markup]\n",
247 "# Enable markup-aware comment handling.\n",
248 "enable_markup = {enable_markup}\n\n",
249 "# Reflow plain line comments to fit within the configured line width.\n",
250 "# reflow_comments = true\n\n",
251 "# Preserve the first comment block in a file literally.\n",
252 "first_comment_is_literal = {first_comment_is_literal}\n\n",
253 "# Preserve comments matching a custom regex literally.\n",
254 "# literal_comment_pattern = \"^\\\\s*NOTE:\"\n\n",
255 "# Preferred bullet character when normalizing markup lists.\n",
256 "bullet_char = \"{bullet_char}\"\n\n",
257 "# Preferred punctuation for numbered lists when normalizing markup.\n",
258 "enum_char = \"{enum_char}\"\n\n",
259 "# Regex describing fenced literal comment blocks.\n",
260 "fence_pattern = '{fence_pattern}'\n\n",
261 "# Regex describing ruler-style comments that should be treated specially.\n",
262 "ruler_pattern = '{ruler_pattern}'\n\n",
263 "# Minimum ruler length before a hash-only line is treated as a ruler.\n",
264 "hashruler_min_length = {hashruler_min_length}\n\n",
265 "# Normalize ruler comments when markup handling is enabled.\n",
266 "canonicalize_hashrulers = {canonicalize_hashrulers}\n\n",
267 "# Regex pattern for inline comments that are explicitly trailing their preceding\n",
268 "# argument (rendered on the same line). Default: '#<'.\n",
269 "# explicit_trailing_pattern = \"#<\"\n\n",
270 "# Uncomment and edit a block like this to override formatting knobs\n",
271 "# for a specific command. This changes layout behavior for that\n",
272 "# command name only; it does not define new command syntax.\n",
273 "#\n",
274 "# [per_command_overrides.my_custom_command]\n",
275 "# Override the line width just for this command.\n",
276 "# line_width = 120\n\n",
277 "# Override command casing just for this command.\n",
278 "# command_case = \"unchanged\"\n\n",
279 "# Override keyword casing just for this command.\n",
280 "# keyword_case = \"upper\"\n\n",
281 "# Override indentation width just for this command.\n",
282 "# tab_size = 4\n\n",
283 "# Override dangling-paren placement just for this command.\n",
284 "# dangle_parens = false\n\n",
285 "# Override dangling-paren alignment just for this command.\n",
286 "# dangle_align = \"prefix\"\n\n",
287 "# Override the positional-argument hanging-wrap threshold just for this command.\n",
288 "# max_hanging_wrap_positional_args = 8\n\n",
289 "# Override the subgroup hanging-wrap threshold just for this command.\n",
290 "# max_hanging_wrap_groups = 3\n\n",
291 "# TOML custom-command specs live under [commands.<name>]. For\n",
292 "# user config, prefer YAML once these specs grow beyond a couple\n",
293 "# of simple flags/kwargs.\n",
294 "# Command specs tell the formatter which tokens are positional\n",
295 "# arguments, standalone flags, and keyword sections.\n",
296 "#\n",
297 "# Example: one positional argument, one flag, and two keyword\n",
298 "# sections that each take one or more values.\n",
299 "#\n",
300 "# [commands.my_custom_command]\n",
301 "# pargs = 1\n",
302 "# flags = [\"QUIET\"]\n",
303 "# kwargs = {{ SOURCES = {{ nargs = \"+\" }}, LIBRARIES = {{ nargs = \"+\" }} }}\n",
304 ),
305 line_width = Config::default().line_width,
306 tab_size = Config::default().tab_size,
307 max_empty_lines = Config::default().max_empty_lines,
308 max_lines_hwrap = Config::default().max_lines_hwrap,
309 max_pargs_hwrap = Config::default().max_pargs_hwrap,
310 max_subgroups_hwrap = Config::default().max_subgroups_hwrap,
311 max_rows_cmdline = Config::default().max_rows_cmdline,
312 dangle_parens = Config::default().dangle_parens,
313 dangle_align = "prefix",
314 min_prefix_chars = Config::default().min_prefix_chars,
315 max_prefix_chars = Config::default().max_prefix_chars,
316 command_case = "lower",
317 keyword_case = "upper",
318 enable_markup = Config::default().enable_markup,
319 first_comment_is_literal = Config::default().first_comment_is_literal,
320 bullet_char = Config::default().bullet_char,
321 enum_char = Config::default().enum_char,
322 fence_pattern = Config::default().fence_pattern,
323 ruler_pattern = Config::default().ruler_pattern,
324 hashruler_min_length = Config::default().hashruler_min_length,
325 canonicalize_hashrulers = Config::default().canonicalize_hashrulers,
326 )
327}
328
329#[derive(Debug, Clone, Serialize)]
330struct EffectiveConfigFile {
331 format: EffectiveFormatSection,
332 style: EffectiveStyleSection,
333 markup: EffectiveMarkupSection,
334 per_command_overrides: HashMap<String, PerCommandConfig>,
335}
336
337#[derive(Debug, Clone, Serialize)]
338struct EffectiveFormatSection {
339 #[serde(skip_serializing_if = "std::ops::Not::not")]
340 disable: bool,
341 line_ending: LineEnding,
342 line_width: usize,
343 tab_size: usize,
344 use_tabs: bool,
345 fractional_tab_policy: FractionalTabPolicy,
346 max_empty_lines: usize,
347 max_hanging_wrap_lines: usize,
348 max_hanging_wrap_positional_args: usize,
349 max_hanging_wrap_groups: usize,
350 max_rows_cmdline: usize,
351 #[serde(skip_serializing_if = "Vec::is_empty")]
352 always_wrap: Vec<String>,
353 #[serde(skip_serializing_if = "std::ops::Not::not")]
354 require_valid_layout: bool,
355 dangle_parens: bool,
356 dangle_align: DangleAlign,
357 min_prefix_length: usize,
358 max_prefix_length: usize,
359 space_before_control_paren: bool,
360 space_before_definition_paren: bool,
361}
362
363#[derive(Debug, Clone, Serialize)]
364struct EffectiveStyleSection {
365 command_case: CaseStyle,
366 keyword_case: CaseStyle,
367}
368
369#[derive(Debug, Clone, Serialize)]
370struct EffectiveMarkupSection {
371 enable_markup: bool,
372 reflow_comments: bool,
373 first_comment_is_literal: bool,
374 literal_comment_pattern: String,
375 bullet_char: String,
376 enum_char: String,
377 fence_pattern: String,
378 ruler_pattern: String,
379 hashruler_min_length: usize,
380 canonicalize_hashrulers: bool,
381 explicit_trailing_pattern: String,
382}
383
384impl From<&Config> for EffectiveConfigFile {
385 fn from(config: &Config) -> Self {
386 Self {
387 format: EffectiveFormatSection {
388 disable: config.disable,
389 line_ending: config.line_ending,
390 line_width: config.line_width,
391 tab_size: config.tab_size,
392 use_tabs: config.use_tabchars,
393 fractional_tab_policy: config.fractional_tab_policy,
394 max_empty_lines: config.max_empty_lines,
395 max_hanging_wrap_lines: config.max_lines_hwrap,
396 max_hanging_wrap_positional_args: config.max_pargs_hwrap,
397 max_hanging_wrap_groups: config.max_subgroups_hwrap,
398 max_rows_cmdline: config.max_rows_cmdline,
399 always_wrap: config.always_wrap.clone(),
400 require_valid_layout: config.require_valid_layout,
401 dangle_parens: config.dangle_parens,
402 dangle_align: config.dangle_align,
403 min_prefix_length: config.min_prefix_chars,
404 max_prefix_length: config.max_prefix_chars,
405 space_before_control_paren: config.separate_ctrl_name_with_space,
406 space_before_definition_paren: config.separate_fn_name_with_space,
407 },
408 style: EffectiveStyleSection {
409 command_case: config.command_case,
410 keyword_case: config.keyword_case,
411 },
412 markup: EffectiveMarkupSection {
413 enable_markup: config.enable_markup,
414 reflow_comments: config.reflow_comments,
415 first_comment_is_literal: config.first_comment_is_literal,
416 literal_comment_pattern: config.literal_comment_pattern.clone(),
417 bullet_char: config.bullet_char.clone(),
418 enum_char: config.enum_char.clone(),
419 fence_pattern: config.fence_pattern.clone(),
420 ruler_pattern: config.ruler_pattern.clone(),
421 hashruler_min_length: config.hashruler_min_length,
422 canonicalize_hashrulers: config.canonicalize_hashrulers,
423 explicit_trailing_pattern: config.explicit_trailing_pattern.clone(),
424 },
425 per_command_overrides: config.per_command_overrides.clone(),
426 }
427 }
428}
429
430fn default_config_template_yaml() -> String {
431 format!(
432 concat!(
433 "# yaml-language-server: $schema=https://cmakefmt.dev/schemas/latest/schema.json\n",
434 "# Default cmakefmt configuration.\n",
435 "# Copy this to .cmakefmt.yaml and uncomment the optional settings\n",
436 "# you want to customize.\n\n",
437 "format:\n",
438 " # Disable formatting entirely (return source unchanged).\n",
439 " # disable: true\n\n",
440 " # Output line-ending style: unix (LF), windows (CRLF), or auto (detect from input).\n",
441 " # line_ending: windows\n\n",
442 " # Maximum rendered line width before cmakefmt wraps a call.\n",
443 " line_width: {line_width}\n\n",
444 " # Number of spaces per indentation level when use_tabs is false.\n",
445 " tab_size: {tab_size}\n\n",
446 " # Indent with tab characters instead of spaces.\n",
447 " # use_tabs: true\n\n",
448 " # How to handle fractional indentation when use_tabs is true: use-space or round-up.\n",
449 " # fractional_tab_policy: round-up\n\n",
450 " # Maximum number of consecutive blank lines to preserve.\n",
451 " max_empty_lines: {max_empty_lines}\n\n",
452 " # Maximum wrapped lines to tolerate before switching to a more vertical layout.\n",
453 " max_hanging_wrap_lines: {max_lines_hwrap}\n\n",
454 " # Maximum positional arguments to keep in a hanging-wrap layout.\n",
455 " max_hanging_wrap_positional_args: {max_pargs_hwrap}\n\n",
456 " # Maximum keyword/flag subgroups to keep in a hanging-wrap layout.\n",
457 " max_hanging_wrap_groups: {max_subgroups_hwrap}\n\n",
458 " # Maximum rows a hanging-wrap positional group may consume before nesting is forced.\n",
459 " max_rows_cmdline: {max_rows_cmdline}\n\n",
460 " # Commands that must always use vertical (wrapped) layout.\n",
461 " # always_wrap:\n",
462 " # - target_link_libraries\n\n",
463 " # Return an error if any formatted line exceeds line_width.\n",
464 " # require_valid_layout: true\n\n",
465 " # Put the closing ')' on its own line when a call wraps.\n",
466 " dangle_parens: {dangle_parens}\n\n",
467 " # Alignment strategy for a dangling ')': prefix, open, or close.\n",
468 " dangle_align: {dangle_align}\n\n",
469 " # Lower heuristic bound used when deciding between compact and wrapped layouts.\n",
470 " min_prefix_length: {min_prefix_chars}\n\n",
471 " # Upper heuristic bound used when deciding between compact and wrapped layouts.\n",
472 " max_prefix_length: {max_prefix_chars}\n\n",
473 " # Insert a space before '(' for control-flow commands like if/foreach.\n",
474 " # space_before_control_paren: true\n\n",
475 " # Insert a space before '(' for function() and macro() definitions.\n",
476 " # space_before_definition_paren: true\n\n",
477 "style:\n",
478 " # Output casing for command names: lower, upper, or unchanged.\n",
479 " command_case: {command_case}\n\n",
480 " # Output casing for recognized keywords and flags: lower, upper, or unchanged.\n",
481 " keyword_case: {keyword_case}\n\n",
482 "markup:\n",
483 " # Enable markup-aware comment handling.\n",
484 " enable_markup: {enable_markup}\n\n",
485 " # Reflow plain line comments to fit within the configured line width.\n",
486 " # reflow_comments: true\n\n",
487 " # Preserve the first comment block in a file literally.\n",
488 " first_comment_is_literal: {first_comment_is_literal}\n\n",
489 " # Preserve comments matching a custom regex literally.\n",
490 " # literal_comment_pattern: '^\\s*NOTE:'\n\n",
491 " # Preferred bullet character when normalizing markup lists.\n",
492 " bullet_char: '{bullet_char}'\n\n",
493 " # Preferred punctuation for numbered lists when normalizing markup.\n",
494 " enum_char: '{enum_char}'\n\n",
495 " # Regex describing fenced literal comment blocks.\n",
496 " fence_pattern: '{fence_pattern}'\n\n",
497 " # Regex describing ruler-style comments that should be treated specially.\n",
498 " ruler_pattern: '{ruler_pattern}'\n\n",
499 " # Minimum ruler length before a hash-only line is treated as a ruler.\n",
500 " hashruler_min_length: {hashruler_min_length}\n\n",
501 " # Normalize ruler comments when markup handling is enabled.\n",
502 " canonicalize_hashrulers: {canonicalize_hashrulers}\n\n",
503 " # Regex pattern for inline comments that are explicitly trailing their preceding\n",
504 " # argument (rendered on the same line). Default: '#<'.\n",
505 " # explicit_trailing_pattern: '#<'\n\n",
506 "# Uncomment and edit a block like this to override formatting knobs\n",
507 "# for a specific command. This changes layout behavior for that\n",
508 "# command name only; it does not define new command syntax.\n",
509 "#\n",
510 "# per_command_overrides:\n",
511 "# my_custom_command:\n",
512 "# # Override the line width just for this command.\n",
513 "# line_width: 120\n",
514 "#\n",
515 "# # Override command casing just for this command.\n",
516 "# command_case: unchanged\n",
517 "#\n",
518 "# # Override keyword casing just for this command.\n",
519 "# keyword_case: upper\n",
520 "#\n",
521 "# # Override indentation width just for this command.\n",
522 "# tab_size: 4\n",
523 "#\n",
524 "# # Override dangling-paren placement just for this command.\n",
525 "# dangle_parens: false\n",
526 "#\n",
527 "# # Override dangling-paren alignment just for this command.\n",
528 "# dangle_align: prefix\n",
529 "#\n",
530 "# # Override the positional-argument hanging-wrap threshold just for this command.\n",
531 "# max_hanging_wrap_positional_args: 8\n",
532 "#\n",
533 "# # Override the subgroup hanging-wrap threshold just for this command.\n",
534 "# max_hanging_wrap_groups: 3\n\n",
535 "# YAML custom-command specs live under commands:<name>. Command\n",
536 "# specs tell the formatter which tokens are positional arguments,\n",
537 "# standalone flags, and keyword sections.\n",
538 "#\n",
539 "# Example: one positional argument, one flag, and two keyword\n",
540 "# sections that each take one or more values.\n",
541 "#\n",
542 "# commands:\n",
543 "# my_custom_command:\n",
544 "# pargs: 1\n",
545 "# flags:\n",
546 "# - QUIET\n",
547 "# kwargs:\n",
548 "# SOURCES:\n",
549 "# nargs: \"+\"\n",
550 "# LIBRARIES:\n",
551 "# nargs: \"+\"\n",
552 ),
553 line_width = Config::default().line_width,
554 tab_size = Config::default().tab_size,
555 max_empty_lines = Config::default().max_empty_lines,
556 max_lines_hwrap = Config::default().max_lines_hwrap,
557 max_pargs_hwrap = Config::default().max_pargs_hwrap,
558 max_subgroups_hwrap = Config::default().max_subgroups_hwrap,
559 max_rows_cmdline = Config::default().max_rows_cmdline,
560 dangle_parens = Config::default().dangle_parens,
561 dangle_align = "prefix",
562 min_prefix_chars = Config::default().min_prefix_chars,
563 max_prefix_chars = Config::default().max_prefix_chars,
564 command_case = "lower",
565 keyword_case = "upper",
566 enable_markup = Config::default().enable_markup,
567 first_comment_is_literal = Config::default().first_comment_is_literal,
568 bullet_char = Config::default().bullet_char,
569 enum_char = Config::default().enum_char,
570 fence_pattern = Config::default().fence_pattern,
571 ruler_pattern = Config::default().ruler_pattern,
572 hashruler_min_length = Config::default().hashruler_min_length,
573 canonicalize_hashrulers = Config::default().canonicalize_hashrulers,
574 )
575}
576
577impl Config {
578 pub fn for_file(file_path: &Path) -> Result<Self> {
585 let config_paths = find_config_files(file_path);
586 Self::from_files(&config_paths)
587 }
588
589 pub fn from_file(path: &Path) -> Result<Self> {
591 let paths = [path.to_path_buf()];
592 Self::from_files(&paths)
593 }
594
595 pub fn from_files(paths: &[PathBuf]) -> Result<Self> {
599 let mut config = Config::default();
600 for path in paths {
601 let file_config = load_config_file(path)?;
602 config.apply(file_config);
603 }
604 Ok(config)
605 }
606
607 pub fn config_sources_for(file_path: &Path) -> Vec<PathBuf> {
613 find_config_files(file_path)
614 }
615
616 fn apply(&mut self, fc: FileConfig) {
617 if let Some(v) = fc.format.disable {
619 self.disable = v;
620 }
621 if let Some(v) = fc.format.line_ending {
622 self.line_ending = v;
623 }
624 if let Some(v) = fc.format.line_width {
625 self.line_width = v;
626 }
627 if let Some(v) = fc.format.tab_size {
628 self.tab_size = v;
629 }
630 if let Some(v) = fc.format.use_tabs {
631 self.use_tabchars = v;
632 }
633 if let Some(v) = fc.format.fractional_tab_policy {
634 self.fractional_tab_policy = v;
635 }
636 if let Some(v) = fc.format.max_empty_lines {
637 self.max_empty_lines = v;
638 }
639 if let Some(v) = fc.format.max_hanging_wrap_lines {
640 self.max_lines_hwrap = v;
641 }
642 if let Some(v) = fc.format.max_hanging_wrap_positional_args {
643 self.max_pargs_hwrap = v;
644 }
645 if let Some(v) = fc.format.max_hanging_wrap_groups {
646 self.max_subgroups_hwrap = v;
647 }
648 if let Some(v) = fc.format.max_rows_cmdline {
649 self.max_rows_cmdline = v;
650 }
651 if let Some(v) = fc.format.always_wrap {
652 self.always_wrap = v.into_iter().map(|s| s.to_ascii_lowercase()).collect();
653 }
654 if let Some(v) = fc.format.require_valid_layout {
655 self.require_valid_layout = v;
656 }
657 if let Some(v) = fc.format.dangle_parens {
658 self.dangle_parens = v;
659 }
660 if let Some(v) = fc.format.dangle_align {
661 self.dangle_align = v;
662 }
663 if let Some(v) = fc.format.min_prefix_length {
664 self.min_prefix_chars = v;
665 }
666 if let Some(v) = fc.format.max_prefix_length {
667 self.max_prefix_chars = v;
668 }
669 if let Some(v) = fc.format.space_before_control_paren {
670 self.separate_ctrl_name_with_space = v;
671 }
672 if let Some(v) = fc.format.space_before_definition_paren {
673 self.separate_fn_name_with_space = v;
674 }
675
676 if let Some(v) = fc.style.command_case {
678 self.command_case = v;
679 }
680 if let Some(v) = fc.style.keyword_case {
681 self.keyword_case = v;
682 }
683
684 if let Some(v) = fc.markup.enable_markup {
686 self.enable_markup = v;
687 }
688 if let Some(v) = fc.markup.reflow_comments {
689 self.reflow_comments = v;
690 }
691 if let Some(v) = fc.markup.first_comment_is_literal {
692 self.first_comment_is_literal = v;
693 }
694 if let Some(v) = fc.markup.literal_comment_pattern {
695 self.literal_comment_pattern = v;
696 }
697 if let Some(v) = fc.markup.bullet_char {
698 self.bullet_char = v;
699 }
700 if let Some(v) = fc.markup.enum_char {
701 self.enum_char = v;
702 }
703 if let Some(v) = fc.markup.fence_pattern {
704 self.fence_pattern = v;
705 }
706 if let Some(v) = fc.markup.ruler_pattern {
707 self.ruler_pattern = v;
708 }
709 if let Some(v) = fc.markup.hashruler_min_length {
710 self.hashruler_min_length = v;
711 }
712 if let Some(v) = fc.markup.canonicalize_hashrulers {
713 self.canonicalize_hashrulers = v;
714 }
715 if let Some(v) = fc.markup.explicit_trailing_pattern {
716 self.explicit_trailing_pattern = v;
717 }
718
719 for (name, overrides) in fc.per_command_overrides {
721 self.per_command_overrides.insert(name, overrides);
722 }
723 }
724}
725
726fn load_config_file(path: &Path) -> Result<FileConfig> {
727 let contents = std::fs::read_to_string(path).map_err(Error::Io)?;
728 let config: FileConfig = match detect_config_format(path)? {
729 ConfigFileFormat::Toml => toml::from_str(&contents).map_err(|source| {
730 let (line, column) = toml_line_col(&contents, source.span().map(|span| span.start));
731 Error::Config {
732 path: path.to_path_buf(),
733 details: FileParseError {
734 format: ConfigFileFormat::Toml.as_str(),
735 message: source.to_string().into_boxed_str(),
736 line,
737 column,
738 },
739 source_message: source.to_string().into_boxed_str(),
740 }
741 }),
742 ConfigFileFormat::Yaml => serde_yaml::from_str(&contents).map_err(|source| {
743 let location = source.location();
744 let line = location.as_ref().map(|loc| loc.line());
745 let column = location.as_ref().map(|loc| loc.column());
746 Error::Config {
747 path: path.to_path_buf(),
748 details: FileParseError {
749 format: ConfigFileFormat::Yaml.as_str(),
750 message: source.to_string().into_boxed_str(),
751 line,
752 column,
753 },
754 source_message: source.to_string().into_boxed_str(),
755 }
756 }),
757 }?;
758
759 if !config.legacy_per_command.is_empty() {
760 return Err(Error::Formatter(format!(
761 "{}: `per_command` has been renamed to `per_command_overrides`",
762 path.display()
763 )));
764 }
765
766 Ok(config)
767}
768
769fn find_config_files(file_path: &Path) -> Vec<PathBuf> {
776 let start_dir = if file_path.is_dir() {
777 file_path.to_path_buf()
778 } else {
779 file_path
780 .parent()
781 .map(Path::to_path_buf)
782 .unwrap_or_else(|| PathBuf::from("."))
783 };
784
785 let mut dir = Some(start_dir.as_path());
786 while let Some(d) = dir {
787 if let Some(candidate) = preferred_config_in_dir(d) {
788 return vec![candidate];
789 }
790
791 if d.join(".git").exists() {
792 break;
793 }
794
795 dir = d.parent();
796 }
797
798 if let Some(home) = home_dir() {
799 if let Some(home_config) = preferred_config_in_dir(&home) {
800 return vec![home_config];
801 }
802 }
803
804 Vec::new()
805}
806
807pub(crate) fn detect_config_format(path: &Path) -> Result<ConfigFileFormat> {
808 let file_name = path
809 .file_name()
810 .and_then(|name| name.to_str())
811 .unwrap_or_default();
812 if file_name == CONFIG_FILE_NAME_TOML
813 || path.extension().and_then(|ext| ext.to_str()) == Some("toml")
814 {
815 return Ok(ConfigFileFormat::Toml);
816 }
817 if matches!(file_name, CONFIG_FILE_NAME_YAML | CONFIG_FILE_NAME_YML)
818 || matches!(
819 path.extension().and_then(|ext| ext.to_str()),
820 Some("yaml" | "yml")
821 )
822 {
823 return Ok(ConfigFileFormat::Yaml);
824 }
825
826 Err(Error::Formatter(format!(
827 "{}: unsupported config format; use .cmakefmt.yaml, .cmakefmt.yml, or .cmakefmt.toml",
828 path.display()
829 )))
830}
831
832fn preferred_config_in_dir(dir: &Path) -> Option<PathBuf> {
833 CONFIG_FILE_NAMES
834 .iter()
835 .map(|name| dir.join(name))
836 .find(|candidate| candidate.is_file())
837}
838
839pub(crate) fn toml_line_col(
840 contents: &str,
841 offset: Option<usize>,
842) -> (Option<usize>, Option<usize>) {
843 let Some(offset) = offset else {
844 return (None, None);
845 };
846 let mut line = 1usize;
847 let mut column = 1usize;
848 for (index, ch) in contents.char_indices() {
849 if index >= offset {
850 break;
851 }
852 if ch == '\n' {
853 line += 1;
854 column = 1;
855 } else {
856 column += 1;
857 }
858 }
859 (Some(line), Some(column))
860}
861
862fn home_dir() -> Option<PathBuf> {
863 std::env::var_os("HOME")
864 .or_else(|| std::env::var_os("USERPROFILE"))
865 .map(PathBuf::from)
866}
867
868#[cfg(test)]
869mod tests {
870 use super::*;
871 use std::fs;
872
873 #[test]
874 fn parse_empty_config() {
875 let config: FileConfig = toml::from_str("").unwrap();
876 assert!(config.format.line_width.is_none());
877 }
878
879 #[test]
880 fn parse_full_config() {
881 let toml_str = r#"
882[format]
883line_width = 120
884tab_size = 4
885use_tabs = true
886max_empty_lines = 2
887dangle_parens = true
888dangle_align = "open"
889space_before_control_paren = true
890space_before_definition_paren = true
891max_hanging_wrap_positional_args = 3
892max_hanging_wrap_groups = 1
893
894[style]
895command_case = "upper"
896keyword_case = "lower"
897
898[markup]
899enable_markup = false
900hashruler_min_length = 20
901
902[per_command_overrides.message]
903dangle_parens = true
904line_width = 100
905"#;
906 let config: FileConfig = toml::from_str(toml_str).unwrap();
907 assert_eq!(config.format.line_width, Some(120));
908 assert_eq!(config.format.tab_size, Some(4));
909 assert_eq!(config.format.use_tabs, Some(true));
910 assert_eq!(config.format.dangle_parens, Some(true));
911 assert_eq!(config.format.dangle_align, Some(DangleAlign::Open));
912 assert_eq!(config.style.command_case, Some(CaseStyle::Upper));
913 assert_eq!(config.style.keyword_case, Some(CaseStyle::Lower));
914 assert_eq!(config.markup.enable_markup, Some(false));
915
916 let msg = config.per_command_overrides.get("message").unwrap();
917 assert_eq!(msg.dangle_parens, Some(true));
918 assert_eq!(msg.line_width, Some(100));
919 }
920
921 #[test]
922 fn old_format_key_aliases_are_rejected() {
923 let toml_str = r#"
924[format]
925use_tabchars = true
926max_lines_hwrap = 4
927max_pargs_hwrap = 3
928max_subgroups_hwrap = 2
929min_prefix_chars = 5
930max_prefix_chars = 11
931separate_ctrl_name_with_space = true
932separate_fn_name_with_space = true
933"#;
934 let err = toml::from_str::<FileConfig>(toml_str)
935 .unwrap_err()
936 .to_string();
937 assert!(err.contains("unknown field"));
938 }
939
940 #[test]
941 fn config_from_file_applies_overrides() {
942 let dir = tempfile::tempdir().unwrap();
943 let config_path = dir.path().join(CONFIG_FILE_NAME_TOML);
944 fs::write(
945 &config_path,
946 r#"
947[format]
948line_width = 100
949tab_size = 4
950
951[style]
952command_case = "upper"
953"#,
954 )
955 .unwrap();
956
957 let config = Config::from_file(&config_path).unwrap();
958 assert_eq!(config.line_width, 100);
959 assert_eq!(config.tab_size, 4);
960 assert_eq!(config.command_case, CaseStyle::Upper);
961 assert!(!config.use_tabchars);
963 assert_eq!(config.max_empty_lines, 1);
964 }
965
966 #[test]
967 fn default_yaml_config_template_parses() {
968 let template = default_config_template();
969 let parsed: FileConfig = serde_yaml::from_str(&template).unwrap();
970 assert_eq!(parsed.format.line_width, Some(Config::default().line_width));
971 assert_eq!(
972 parsed.style.command_case,
973 Some(Config::default().command_case)
974 );
975 assert_eq!(
976 parsed.markup.enable_markup,
977 Some(Config::default().enable_markup)
978 );
979 }
980
981 #[test]
982 fn toml_config_template_parses() {
983 let template = default_config_template_for(DumpConfigFormat::Toml);
984 let parsed: FileConfig = toml::from_str(&template).unwrap();
985 assert_eq!(parsed.format.line_width, Some(Config::default().line_width));
986 assert_eq!(
987 parsed.style.command_case,
988 Some(Config::default().command_case)
989 );
990 assert_eq!(
991 parsed.markup.enable_markup,
992 Some(Config::default().enable_markup)
993 );
994 }
995
996 #[test]
997 fn missing_config_file_uses_defaults() {
998 let dir = tempfile::tempdir().unwrap();
999 let fake_file = dir.path().join("CMakeLists.txt");
1000 fs::write(&fake_file, "").unwrap();
1001
1002 let config = Config::for_file(&fake_file).unwrap();
1003 assert_eq!(config, Config::default());
1004 }
1005
1006 #[test]
1007 fn config_file_in_parent_is_found() {
1008 let dir = tempfile::tempdir().unwrap();
1009 fs::create_dir(dir.path().join(".git")).unwrap();
1011 fs::write(
1012 dir.path().join(CONFIG_FILE_NAME_TOML),
1013 "[format]\nline_width = 120\n",
1014 )
1015 .unwrap();
1016
1017 let subdir = dir.path().join("src");
1018 fs::create_dir(&subdir).unwrap();
1019 let file = subdir.join("CMakeLists.txt");
1020 fs::write(&file, "").unwrap();
1021
1022 let config = Config::for_file(&file).unwrap();
1023 assert_eq!(config.line_width, 120);
1024 }
1025
1026 #[test]
1027 fn closer_config_wins() {
1028 let dir = tempfile::tempdir().unwrap();
1029 fs::create_dir(dir.path().join(".git")).unwrap();
1030 fs::write(
1031 dir.path().join(CONFIG_FILE_NAME_TOML),
1032 "[format]\nline_width = 120\ntab_size = 4\n",
1033 )
1034 .unwrap();
1035
1036 let subdir = dir.path().join("src");
1037 fs::create_dir(&subdir).unwrap();
1038 fs::write(
1039 subdir.join(CONFIG_FILE_NAME_TOML),
1040 "[format]\nline_width = 100\n",
1041 )
1042 .unwrap();
1043
1044 let file = subdir.join("CMakeLists.txt");
1045 fs::write(&file, "").unwrap();
1046
1047 let config = Config::for_file(&file).unwrap();
1048 assert_eq!(config.line_width, 100);
1050 assert_eq!(config.tab_size, Config::default().tab_size);
1051 }
1052
1053 #[test]
1054 fn from_files_merges_in_order() {
1055 let dir = tempfile::tempdir().unwrap();
1056 let first = dir.path().join("first.toml");
1057 let second = dir.path().join("second.toml");
1058 fs::write(&first, "[format]\nline_width = 120\ntab_size = 4\n").unwrap();
1059 fs::write(&second, "[format]\nline_width = 100\n").unwrap();
1060
1061 let config = Config::from_files(&[first, second]).unwrap();
1062 assert_eq!(config.line_width, 100);
1063 assert_eq!(config.tab_size, 4);
1064 }
1065
1066 #[test]
1067 fn yaml_config_from_file_applies_overrides() {
1068 let dir = tempfile::tempdir().unwrap();
1069 let config_path = dir.path().join(CONFIG_FILE_NAME_YAML);
1070 fs::write(
1071 &config_path,
1072 "format:\n line_width: 100\n tab_size: 4\nstyle:\n command_case: upper\n",
1073 )
1074 .unwrap();
1075
1076 let config = Config::from_file(&config_path).unwrap();
1077 assert_eq!(config.line_width, 100);
1078 assert_eq!(config.tab_size, 4);
1079 assert_eq!(config.command_case, CaseStyle::Upper);
1080 }
1081
1082 #[test]
1083 fn yml_config_from_file_applies_overrides() {
1084 let dir = tempfile::tempdir().unwrap();
1085 let config_path = dir.path().join(CONFIG_FILE_NAME_YML);
1086 fs::write(
1087 &config_path,
1088 "style:\n keyword_case: lower\nformat:\n line_width: 90\n",
1089 )
1090 .unwrap();
1091
1092 let config = Config::from_file(&config_path).unwrap();
1093 assert_eq!(config.line_width, 90);
1094 assert_eq!(config.keyword_case, CaseStyle::Lower);
1095 }
1096
1097 #[test]
1098 fn yaml_is_preferred_over_toml_during_discovery() {
1099 let dir = tempfile::tempdir().unwrap();
1100 fs::create_dir(dir.path().join(".git")).unwrap();
1101 fs::write(
1102 dir.path().join(CONFIG_FILE_NAME_TOML),
1103 "[format]\nline_width = 120\n",
1104 )
1105 .unwrap();
1106 fs::write(
1107 dir.path().join(CONFIG_FILE_NAME_YAML),
1108 "format:\n line_width: 90\n",
1109 )
1110 .unwrap();
1111
1112 let file = dir.path().join("CMakeLists.txt");
1113 fs::write(&file, "").unwrap();
1114
1115 let config = Config::for_file(&file).unwrap();
1116 assert_eq!(config.line_width, 90);
1117 assert_eq!(
1118 Config::config_sources_for(&file),
1119 vec![dir.path().join(CONFIG_FILE_NAME_YAML)]
1120 );
1121 }
1122
1123 #[test]
1124 fn invalid_config_returns_error() {
1125 let dir = tempfile::tempdir().unwrap();
1126 let path = dir.path().join(CONFIG_FILE_NAME_TOML);
1127 fs::write(&path, "this is not valid toml {{{").unwrap();
1128
1129 let result = Config::from_file(&path);
1130 assert!(result.is_err());
1131 let err = result.unwrap_err();
1132 assert!(err.to_string().contains("config error"));
1133 }
1134
1135 #[test]
1136 fn config_from_yaml_file_applies_all_sections_and_overrides() {
1137 let dir = tempfile::tempdir().unwrap();
1138 let config_path = dir.path().join(CONFIG_FILE_NAME_YAML);
1139 fs::write(
1140 &config_path,
1141 r#"
1142format:
1143 line_width: 96
1144 tab_size: 3
1145 use_tabs: true
1146 max_empty_lines: 2
1147 max_hanging_wrap_lines: 4
1148 max_hanging_wrap_positional_args: 7
1149 max_hanging_wrap_groups: 5
1150 dangle_parens: true
1151 dangle_align: open
1152 min_prefix_length: 2
1153 max_prefix_length: 12
1154 space_before_control_paren: true
1155 space_before_definition_paren: true
1156style:
1157 command_case: unchanged
1158 keyword_case: lower
1159markup:
1160 enable_markup: false
1161 reflow_comments: true
1162 first_comment_is_literal: false
1163 literal_comment_pattern: '^\\s*KEEP'
1164 bullet_char: '-'
1165 enum_char: ')'
1166 fence_pattern: '^\\s*(```+).*'
1167 ruler_pattern: '^\\s*={5,}\\s*$'
1168 hashruler_min_length: 42
1169 canonicalize_hashrulers: false
1170per_command_overrides:
1171 my_custom_command:
1172 line_width: 101
1173 tab_size: 5
1174 dangle_parens: false
1175 dangle_align: prefix
1176 max_hanging_wrap_positional_args: 8
1177 max_hanging_wrap_groups: 9
1178"#,
1179 )
1180 .unwrap();
1181
1182 let config = Config::from_file(&config_path).unwrap();
1183 assert_eq!(config.line_width, 96);
1184 assert_eq!(config.tab_size, 3);
1185 assert!(config.use_tabchars);
1186 assert_eq!(config.max_empty_lines, 2);
1187 assert_eq!(config.max_lines_hwrap, 4);
1188 assert_eq!(config.max_pargs_hwrap, 7);
1189 assert_eq!(config.max_subgroups_hwrap, 5);
1190 assert!(config.dangle_parens);
1191 assert_eq!(config.dangle_align, DangleAlign::Open);
1192 assert_eq!(config.min_prefix_chars, 2);
1193 assert_eq!(config.max_prefix_chars, 12);
1194 assert!(config.separate_ctrl_name_with_space);
1195 assert!(config.separate_fn_name_with_space);
1196 assert_eq!(config.command_case, CaseStyle::Unchanged);
1197 assert_eq!(config.keyword_case, CaseStyle::Lower);
1198 assert!(!config.enable_markup);
1199 assert!(config.reflow_comments);
1200 assert!(!config.first_comment_is_literal);
1201 assert_eq!(config.literal_comment_pattern, "^\\\\s*KEEP");
1202 assert_eq!(config.bullet_char, "-");
1203 assert_eq!(config.enum_char, ")");
1204 assert_eq!(config.fence_pattern, "^\\\\s*(```+).*");
1205 assert_eq!(config.ruler_pattern, "^\\\\s*={5,}\\\\s*$");
1206 assert_eq!(config.hashruler_min_length, 42);
1207 assert!(!config.canonicalize_hashrulers);
1208 let per_command = config
1209 .per_command_overrides
1210 .get("my_custom_command")
1211 .unwrap();
1212 assert_eq!(per_command.line_width, Some(101));
1213 assert_eq!(per_command.tab_size, Some(5));
1214 assert_eq!(per_command.dangle_parens, Some(false));
1215 assert_eq!(per_command.dangle_align, Some(DangleAlign::Prefix));
1216 assert_eq!(per_command.max_pargs_hwrap, Some(8));
1217 assert_eq!(per_command.max_subgroups_hwrap, Some(9));
1218 }
1219
1220 #[test]
1221 fn detect_config_format_supports_yaml_and_rejects_unknown() {
1222 assert!(matches!(
1223 detect_config_format(Path::new(".cmakefmt.yml")).unwrap(),
1224 ConfigFileFormat::Yaml
1225 ));
1226 assert!(matches!(
1227 detect_config_format(Path::new("tooling/settings.yaml")).unwrap(),
1228 ConfigFileFormat::Yaml
1229 ));
1230 assert!(matches!(
1231 detect_config_format(Path::new("project.toml")).unwrap(),
1232 ConfigFileFormat::Toml
1233 ));
1234 let err = detect_config_format(Path::new("config.json")).unwrap_err();
1235 assert!(err.to_string().contains("unsupported config format"));
1236 }
1237
1238 #[test]
1239 fn yaml_config_with_legacy_per_command_key_is_rejected() {
1240 let dir = tempfile::tempdir().unwrap();
1241 let config_path = dir.path().join(CONFIG_FILE_NAME_YAML);
1242 fs::write(
1243 &config_path,
1244 "per_command:\n message:\n line_width: 120\n",
1245 )
1246 .unwrap();
1247 let err = Config::from_file(&config_path).unwrap_err();
1248 assert!(err
1249 .to_string()
1250 .contains("`per_command` has been renamed to `per_command_overrides`"));
1251 }
1252
1253 #[test]
1254 fn invalid_yaml_reports_line_and_column() {
1255 let dir = tempfile::tempdir().unwrap();
1256 let config_path = dir.path().join(CONFIG_FILE_NAME_YAML);
1257 fs::write(&config_path, "format:\n line_width: [\n").unwrap();
1258
1259 let err = Config::from_file(&config_path).unwrap_err();
1260 match err {
1261 Error::Config { details, .. } => {
1262 assert_eq!(details.format, "YAML");
1263 assert!(details.line.is_some());
1264 assert!(details.column.is_some());
1265 }
1266 other => panic!("expected config parse error, got {other:?}"),
1267 }
1268 }
1269
1270 #[test]
1271 fn toml_line_col_returns_none_when_offset_is_missing() {
1272 assert_eq!(toml_line_col("line = true\n", None), (None, None));
1273 }
1274}