cmakefmt-rust 1.4.2

A fast, correct CMake formatter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// SPDX-FileCopyrightText: Copyright 2026 Puneet Matharu
//
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Top-level formatter entry points.
//!
//! These functions parse input, apply barrier handling, and render a formatted
//! output string using the command registry and runtime configuration.
//!
//! # Format-barrier directives
//!
//! The source-string entry points ([`format_source`],
//! [`format_source_with_registry`], [`format_source_with_debug`],
//! [`format_source_with_registry_debug`]) scan each input line for
//! *barrier directives* that toggle formatting on and off:
//!
//! | Directive | Effect |
//! |-----------|--------|
//! | `# cmake-format: off` / `# cmake-format: on` | Skip / resume formatting |
//! | `# cmakefmt: off` / `# cmakefmt: on` | Same, cmakefmt-branded |
//! | `# fmt: off` / `# fmt: on` | Generic alias |
//! | `# ~~~` (matched pair) | Fence region — content between fences is emitted verbatim |
//!
//! Leading whitespace before the `#` is allowed. Lines inside a
//! disabled region are passed through unchanged.
//!
//! Note: [`format_parsed_file`] does **not** honour these directives
//! — barrier detection happens pre-parse, so if you have the AST
//! already you've bypassed that step. Use a source-string entry
//! point if you need barriers.

pub(crate) mod comment;
pub(crate) mod node;

// `dump.rs` is the only consumer of these re-exports and is itself
// `#[cfg(feature = "cli")]`. Without the gate this `use` warns under
// `--no-default-features` and `--features lsp` builds.
#[cfg(feature = "cli")]
pub(crate) use node::{split_sections, HeaderKind};

use std::path::PathBuf;

use crate::config::{Config, LineEnding};
use crate::error::{Error, FileParseError, Result};
use crate::parser::{self, ast::File, ast::Statement};
use crate::spec::registry::CommandRegistry;

/// Format raw CMake source using the built-in command registry.
///
/// The output always ends with a newline. When
/// [`Config::line_ending`] is [`LineEnding::Auto`], the output line
/// ending is detected from the input (CRLF if the source contains
/// any `\r\n`, otherwise LF).
///
/// # Examples
///
/// ```
/// use cmakefmt::{format_source, Config};
///
/// let cmake = "CMAKE_MINIMUM_REQUIRED(VERSION 3.20)\n";
/// let formatted = format_source(cmake, &Config::default()).unwrap();
/// assert_eq!(formatted, "cmake_minimum_required(VERSION 3.20)\n");
/// ```
pub fn format_source(source: &str, config: &Config) -> Result<String> {
    format_source_with_registry(source, config, CommandRegistry::builtins())
}

/// Format raw CMake source using the built-in registry and also return debug
/// lines describing the formatter's decisions.
///
/// The returned `Vec<String>` contains one human-readable log line
/// per formatting decision (layout choice, section split, fallback
/// paths, barrier events). The exact wording is **unstable across
/// releases** and intended for interactive debugging and bug
/// reports, not programmatic consumption.
pub fn format_source_with_debug(source: &str, config: &Config) -> Result<(String, Vec<String>)> {
    format_source_with_registry_debug(source, config, CommandRegistry::builtins())
}

/// Format raw CMake source using an explicit command registry.
///
/// Use this when you need a registry that merges the built-ins with a user
/// override file.
///
/// # Examples
///
/// ```
/// use cmakefmt::{format_source_with_registry, Config, CommandRegistry};
///
/// let registry = CommandRegistry::from_builtins_and_overrides(
///     None::<&std::path::Path>,
/// ).unwrap();
/// let cmake = "TARGET_LINK_LIBRARIES(mylib PUBLIC dep1)\n";
/// let formatted = format_source_with_registry(
///     cmake, &Config::default(), &registry,
/// ).unwrap();
/// assert_eq!(formatted, "target_link_libraries(mylib PUBLIC dep1)\n");
/// ```
pub fn format_source_with_registry(
    source: &str,
    config: &Config,
    registry: &CommandRegistry,
) -> Result<String> {
    if config.disable {
        return Ok(source.to_owned());
    }
    validate_runtime_config(config)?;
    let formatted = format_source_impl(source, config, registry, &mut DebugLog::disabled())?.0;
    Ok(apply_line_ending(source, &formatted, config.line_ending))
}

/// Format raw CMake source using an explicit registry and return debug output.
pub fn format_source_with_registry_debug(
    source: &str,
    config: &Config,
    registry: &CommandRegistry,
) -> Result<(String, Vec<String>)> {
    if config.disable {
        return Ok((source.to_owned(), Vec::new()));
    }
    validate_runtime_config(config)?;
    let mut lines = Vec::new();
    let mut debug = DebugLog::enabled(&mut lines);
    let (formatted, _) = format_source_impl(source, config, registry, &mut debug)?;
    Ok((
        apply_line_ending(source, &formatted, config.line_ending),
        lines,
    ))
}

/// Format an already parsed AST file using the original source text.
///
/// This entry point preserves the same high-level config semantics as
/// [`format_source_with_registry`]: `disable` returns the original `source`
/// unchanged and `line_ending` is applied relative to the original source.
///
/// Useful when you want to parse once and format the same AST repeatedly with
/// different [`Config`] or registry settings, avoiding re-parsing overhead.
///
/// # Caveat: no barrier handling
///
/// Unlike [`format_source`] and its siblings, this function does
/// **not** honour `# cmake-format: off/on`, `# cmakefmt: off/on`,
/// `# fmt: off/on`, or `# ~~~` fence regions. Barrier detection
/// happens pre-parse in the source-string pipeline, so by the time
/// you hand in a parsed AST the opportunity has passed. Use one of
/// the source-string entry points if your input contains barrier
/// directives.
///
/// # Examples
///
/// ```
/// use cmakefmt::{format_parsed_file, Config, CommandRegistry};
///
/// let cmake = "PROJECT(MyProject)\n";
/// let file = cmakefmt::parser::parse(cmake).unwrap();
/// let formatted = format_parsed_file(
///     cmake,
///     &file,
///     &Config::default(),
///     CommandRegistry::builtins(),
/// ).unwrap();
/// assert_eq!(formatted, "project(MyProject)\n");
/// ```
pub fn format_parsed_file(
    source: &str,
    file: &File,
    config: &Config,
    registry: &CommandRegistry,
) -> Result<String> {
    if config.disable {
        return Ok(source.to_owned());
    }
    validate_runtime_config(config)?;
    let formatted =
        format_parsed_file_with_debug(file, config, registry, &mut DebugLog::disabled())?;
    Ok(apply_line_ending(source, &formatted, config.line_ending))
}

fn format_parsed_file_with_debug(
    file: &File,
    config: &Config,
    registry: &CommandRegistry,
    debug: &mut DebugLog<'_>,
) -> Result<String> {
    let patterns = config.compiled_patterns().map_err(runtime_config_error)?;
    let mut output = String::new();
    let mut previous_was_content = false;
    let mut block_depth = 0usize;

    for statement in &file.statements {
        match statement {
            Statement::Command(command) => {
                block_depth = block_depth.saturating_sub(block_dedent_before(&command.name));

                if previous_was_content {
                    output.push('\n');
                }

                output.push_str(&node::format_command(
                    command,
                    config,
                    &patterns,
                    registry,
                    block_depth,
                    debug,
                )?);

                if let Some(trailing) = &command.trailing_comment {
                    let comment_indent_width = output
                        .rsplit('\n')
                        .next()
                        .unwrap_or_default()
                        .chars()
                        .count()
                        + 1;
                    let comment_lines = comment::format_comment_lines(
                        trailing,
                        config,
                        &patterns,
                        comment_indent_width,
                        config.line_width,
                    );
                    if let Some((first, rest)) = comment_lines.split_first() {
                        output.push(' ');
                        output.push_str(first);
                        let continuation_indent = " ".repeat(comment_indent_width);
                        for line in rest {
                            output.push('\n');
                            output.push_str(&continuation_indent);
                            output.push_str(line);
                        }
                    }
                }

                previous_was_content = true;
                block_depth += block_indent_after(&command.name);
            }
            Statement::TemplatePlaceholder(placeholder) => {
                if previous_was_content {
                    output.push('\n');
                }

                output.push_str(placeholder);
                previous_was_content = true;
            }
            Statement::BlankLines(count) => {
                let newline_count = if previous_was_content {
                    count + 1
                } else {
                    *count
                };
                let newline_count = newline_count.min(config.max_empty_lines + 1);
                for _ in 0..newline_count {
                    output.push('\n');
                }
                previous_was_content = false;
            }
            Statement::Comment(c) => {
                if previous_was_content {
                    output.push('\n');
                }

                let indent = config.indent_str().repeat(block_depth);
                let comment_lines = comment::format_comment_lines(
                    c,
                    config,
                    &patterns,
                    indent.chars().count(),
                    config.line_width,
                );
                for (index, line) in comment_lines.iter().enumerate() {
                    if index > 0 {
                        output.push('\n');
                    }
                    output.push_str(&indent);
                    output.push_str(line);
                }
                previous_was_content = true;
            }
        }
    }

    if !output.ends_with('\n') {
        output.push('\n');
    }

    if config.require_valid_layout {
        for (i, line) in output.split('\n').enumerate() {
            // Skip the final empty string produced by the trailing newline.
            if line.is_empty() {
                continue;
            }
            let width = line.chars().count();
            if width > config.line_width {
                return Err(Error::LayoutTooWide {
                    line_no: i + 1,
                    width,
                    limit: config.line_width,
                });
            }
        }
    }

    Ok(output)
}

/// Apply the configured line-ending style to `formatted` output.
///
/// The formatter always emits LF internally. `source` is consulted when
/// `line_ending` is [`LineEnding::Auto`] to detect the predominant style.
fn apply_line_ending(source: &str, formatted: &str, line_ending: LineEnding) -> String {
    let use_crlf = match line_ending {
        LineEnding::Unix => false,
        LineEnding::Windows => true,
        LineEnding::Auto => {
            // Detect from input: if any \r\n is present, assume CRLF.
            source.contains("\r\n")
        }
    };
    if use_crlf {
        formatted.replace('\n', "\r\n")
    } else {
        formatted.to_owned()
    }
}

fn format_source_impl(
    source: &str,
    config: &Config,
    registry: &CommandRegistry,
    debug: &mut DebugLog<'_>,
) -> Result<(String, usize)> {
    let mut output = String::new();
    let mut enabled_chunk = String::new();
    let mut total_statements = 0usize;
    let mut mode = BarrierMode::Enabled;
    let mut enabled_chunk_start_line = 1usize;
    let mut saw_barrier = false;

    for (line_index, line) in source.split_inclusive('\n').enumerate() {
        let line_no = line_index + 1;
        match detect_barrier(line) {
            Some(BarrierEvent::DisableByDirective(kind)) => {
                let statements = flush_enabled_chunk(
                    &mut output,
                    &mut enabled_chunk,
                    config,
                    registry,
                    debug,
                    enabled_chunk_start_line,
                    saw_barrier,
                )?;
                total_statements += statements;
                debug.log(format!(
                    "formatter: disabled formatting at line {line_no} via {kind}: off"
                ));
                output.push_str(line);
                mode = BarrierMode::DisabledByDirective;
                saw_barrier = true;
            }
            Some(BarrierEvent::EnableByDirective(kind)) => {
                let statements = flush_enabled_chunk(
                    &mut output,
                    &mut enabled_chunk,
                    config,
                    registry,
                    debug,
                    enabled_chunk_start_line,
                    saw_barrier,
                )?;
                total_statements += statements;
                debug.log(format!(
                    "formatter: enabled formatting at line {line_no} via {kind}: on"
                ));
                output.push_str(line);
                if matches!(mode, BarrierMode::DisabledByDirective) {
                    mode = BarrierMode::Enabled;
                }
                saw_barrier = true;
            }
            Some(BarrierEvent::Fence) => {
                let statements = flush_enabled_chunk(
                    &mut output,
                    &mut enabled_chunk,
                    config,
                    registry,
                    debug,
                    enabled_chunk_start_line,
                    saw_barrier,
                )?;
                total_statements += statements;
                let next_mode = if matches!(mode, BarrierMode::DisabledByFence) {
                    BarrierMode::Enabled
                } else {
                    BarrierMode::DisabledByFence
                };
                debug.log(format!(
                    "formatter: toggled fence region at line {line_no} -> {}",
                    next_mode.as_str()
                ));
                output.push_str(line);
                mode = next_mode;
                saw_barrier = true;
            }
            None => {
                if matches!(mode, BarrierMode::Enabled) {
                    if enabled_chunk.is_empty() {
                        enabled_chunk_start_line = line_no;
                    }
                    enabled_chunk.push_str(line);
                } else {
                    output.push_str(line);
                }
            }
        }
    }

    total_statements += flush_enabled_chunk(
        &mut output,
        &mut enabled_chunk,
        config,
        registry,
        debug,
        enabled_chunk_start_line,
        saw_barrier,
    )?;
    Ok((output, total_statements))
}

fn flush_enabled_chunk(
    output: &mut String,
    enabled_chunk: &mut String,
    config: &Config,
    registry: &CommandRegistry,
    debug: &mut DebugLog<'_>,
    chunk_start_line: usize,
    barrier_context: bool,
) -> Result<usize> {
    if enabled_chunk.is_empty() {
        return Ok(0);
    }

    let file = match parser::parse(enabled_chunk) {
        Ok(file) => file,
        Err(Error::Parse(parse_error)) => {
            let _ = barrier_context;
            return Err(Error::Parse(crate::error::ParseError {
                display_name: "<source>".to_owned(),
                source_text: enabled_chunk.clone().into_boxed_str(),
                start_line: chunk_start_line,
                diagnostic: parse_error.diagnostic,
            }));
        }
        Err(err) => return Err(err),
    };
    let statement_count = file.statements.len();
    debug.log(format!(
        "formatter: formatting enabled chunk with {statement_count} statement(s) starting at source line {chunk_start_line}"
    ));
    let formatted = format_parsed_file_with_debug(&file, config, registry, debug)?;
    output.push_str(&formatted);
    enabled_chunk.clear();
    Ok(statement_count)
}

fn validate_runtime_config(config: &Config) -> Result<()> {
    config.validate_patterns().map_err(runtime_config_error)?;
    Ok(())
}

fn runtime_config_error(message: String) -> Error {
    Error::Config(crate::error::ConfigError {
        path: PathBuf::from("<programmatic-config>"),
        details: FileParseError {
            format: "runtime",
            message: message.into_boxed_str(),
            line: None,
            column: None,
        },
    })
}

fn detect_barrier(line: &str) -> Option<BarrierEvent<'_>> {
    let trimmed = line.trim_start();
    if !trimmed.starts_with('#') {
        return None;
    }

    let body = trimmed[1..].trim_start().trim_end();
    if body.starts_with("~~~") {
        return Some(BarrierEvent::Fence);
    }

    if body == "cmake-format: off" {
        return Some(BarrierEvent::DisableByDirective("cmake-format"));
    }
    if body == "cmake-format: on" {
        return Some(BarrierEvent::EnableByDirective("cmake-format"));
    }
    if body == "cmakefmt: off" {
        return Some(BarrierEvent::DisableByDirective("cmakefmt"));
    }
    if body == "cmakefmt: on" {
        return Some(BarrierEvent::EnableByDirective("cmakefmt"));
    }
    if body == "fmt: off" {
        return Some(BarrierEvent::DisableByDirective("fmt"));
    }
    if body == "fmt: on" {
        return Some(BarrierEvent::EnableByDirective("fmt"));
    }

    None
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BarrierMode {
    Enabled,
    DisabledByDirective,
    DisabledByFence,
}

impl BarrierMode {
    fn as_str(self) -> &'static str {
        match self {
            BarrierMode::Enabled => "enabled",
            BarrierMode::DisabledByDirective => "disabled-by-directive",
            BarrierMode::DisabledByFence => "disabled-by-fence",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BarrierEvent<'a> {
    DisableByDirective(&'a str),
    EnableByDirective(&'a str),
    Fence,
}

pub(crate) struct DebugLog<'a> {
    lines: Option<&'a mut Vec<String>>,
}

impl<'a> DebugLog<'a> {
    fn disabled() -> Self {
        Self { lines: None }
    }

    fn enabled(lines: &'a mut Vec<String>) -> Self {
        Self { lines: Some(lines) }
    }

    fn log(&mut self, message: impl Into<String>) {
        if let Some(lines) = self.lines.as_deref_mut() {
            lines.push(message.into());
        }
    }
}

fn block_dedent_before(command_name: &str) -> usize {
    usize::from(matches_ascii_insensitive(
        command_name,
        &[
            "elseif",
            "else",
            "endif",
            "endforeach",
            "endwhile",
            "endfunction",
            "endmacro",
            "endblock",
        ],
    ))
}

fn block_indent_after(command_name: &str) -> usize {
    usize::from(matches_ascii_insensitive(
        command_name,
        &[
            "if", "foreach", "while", "function", "macro", "block", "elseif", "else",
        ],
    ))
}

fn matches_ascii_insensitive(input: &str, candidates: &[&str]) -> bool {
    candidates
        .iter()
        .any(|candidate| input.eq_ignore_ascii_case(candidate))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_parsed_file_honors_disable() {
        let source = "set(  X  1 )\n";
        let file = parser::parse(source).unwrap();
        let config = Config {
            disable: true,
            ..Config::default()
        };

        let formatted =
            format_parsed_file(source, &file, &config, CommandRegistry::builtins()).unwrap();

        assert_eq!(formatted, source);
    }

    #[test]
    fn format_parsed_file_applies_line_endings_relative_to_source() {
        let source = "set(  X  1 )\r\n";
        let file = parser::parse(source).unwrap();
        let config = Config {
            line_ending: LineEnding::Auto,
            ..Config::default()
        };

        let formatted =
            format_parsed_file(source, &file, &config, CommandRegistry::builtins()).unwrap();

        assert_eq!(formatted, "set(X 1)\r\n");
    }

    #[test]
    fn format_source_rejects_invalid_programmatic_regex_config() {
        let config = Config {
            fence_pattern: "[".to_owned(),
            ..Config::default()
        };

        let err = format_source("set(X 1)\n", &config).unwrap_err();
        match err {
            Error::Config(config_err) => {
                assert_eq!(config_err.path, PathBuf::from("<programmatic-config>"));
                assert_eq!(config_err.details.format, "runtime");
                assert!(config_err.details.message.contains("invalid regex"));
            }
            other => panic!("expected config error, got {other:?}"),
        }
    }
}