Skip to main content

Crate cmakefmt

Crate cmakefmt 

Source
Expand description

cmakefmt is a fast, configurable CMake formatter.

§Quick start

Format a CMake source string with the default configuration:

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");

To customise formatting, modify Config fields before passing it in:

use cmakefmt::{format_source, Config, CaseStyle};

let mut config = Config::default();
config.line_width = 100;
config.command_case = CaseStyle::Upper;

let cmake = "target_link_libraries(mylib PUBLIC dep1)\n";
let formatted = format_source(cmake, &config).unwrap();
assert_eq!(formatted, "TARGET_LINK_LIBRARIES(mylib PUBLIC dep1)\n");

§Organisation

ModulePurpose
configRuntime configuration types and config-file loading
errorError and result types
formatterSource-to-source formatting pipeline
parserCMake parser and AST definitions
specBuilt-in and user-extensible command registry

§Entry points (re-exported at the crate root)

ItemPurpose
format_source / format_source_with_registryFormat a source string
format_parsed_fileFormat an already-parsed parser::ast::File
format_source_with_debug / format_source_with_registry_debugFormat + collect debug decision log
Config, CommandConfig, PerCommandConfigRuntime configuration
CaseStyle, DangleAlign, LineEnding, FractionalTabPolicyConfig enums
CommandRegistryBuilt-in + user-override command specs
Error, Result, IoResultExtCrate-level error types and the path-context adapter for io::Result

§Features

FeatureDefaultPurpose
cliEnables the cmakefmt binary plus CLI-oriented public API (convert_legacy_config_files, default_config_template_for, generate_json_schema, render_effective_config, DumpConfigFormat). Implies lsp.
lsp✔ (via cli)Compiles the lsp::run Language Server Protocol entry point.

The crate also has a separate target path: when compiled for wasm32, wasm::format and friends are exposed via wasm-bindgen for the browser playground.

Re-exports§

pub use config::CaseStyle;
pub use config::CommandConfig;
pub use config::Config;
pub use config::ContinuationAlign;
pub use config::DangleAlign;
pub use config::FractionalTabPolicy;
pub use config::LineEnding;
pub use config::PerCommandConfig;
pub use config::default_config_template;
pub use config::convert_legacy_config_files;cli
pub use config::default_config_template_for;cli
pub use config::generate_json_schema;cli
pub use config::render_effective_config;cli
pub use config::DumpConfigFormat;cli
pub use error::Error;
pub use error::IoResultExt;
pub use error::Result;
pub use formatter::format_parsed_file;
pub use formatter::format_source;
pub use formatter::format_source_with_debug;
pub use formatter::format_source_with_registry;
pub use formatter::format_source_with_registry_debug;
pub use spec::registry::CommandRegistry;

Modules§

config
Runtime formatter configuration and config-file loading. Runtime formatter configuration.
error
Shared error types used across parsing, config loading, and formatting. Structured error types returned by parsing, config loading, and formatting.
formatter
Source-to-source formatting pipeline. Top-level formatter entry points.
lsplsp
LSP server entry point for cmakefmt.
parser
CMake parser and AST definitions. Parser entry points for CMake source text.
semantic
Semantic-level normalisation (strip comments, line endings, keyword casing) used by --verify and the idempotency tests. Semantic-level normalisation for parsed CMake.
spec
Built-in and user-extensible command specification registry. Command-spec data model used by the formatter.