patchloom 0.10.0

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations, multi-file batching, markdown operations, and MCP server
Documentation
//! Write-policy construction for pending tx files.
use crate::cli::global::GlobalFlags;
use crate::plan::Plan;
use crate::tx::context::EngineContext;
use crate::write::WritePolicy;
use std::path::Path;

// ---------------------------------------------------------------------------
// Write policy
// ---------------------------------------------------------------------------

/// Build the effective write policy for a pending file.
///
/// Start from the CLI-derived per-file defaults, including any EditorConfig
/// values resolved by `policy_from_flags()`, then let plan-level `write_policy`
/// entries override only the keys they set.
pub(crate) fn build_write_policy(
    plan: &Plan,
    ctx: &EngineContext,
    path: &Path,
) -> anyhow::Result<WritePolicy> {
    // If the plan explicitly sets respect_editorconfig, build the policy
    // with that flag applied. This must happen before policy_from_flags
    // because EditorConfig properties are resolved during construction,
    // not during apply_override (#1111.3).
    let mut base_flags = ctx.to_global_flags();
    if let Some(ov) = &plan.write_policy
        && let Some(ec) = ov.respect_editorconfig
    {
        base_flags = GlobalFlags::with_editorconfig(&base_flags, ec);
    }
    let mut write_policy = crate::write::policy_from_flags(&base_flags, Some(path));
    if let Some(ov) = &plan.write_policy {
        write_policy.apply_override(ov)?;
    }
    Ok(write_policy)
}