snapper-fmt 0.10.0

Semantic line break formatter for Org, LaTeX, Markdown, RST, and plaintext
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
//! # snapper
//!
//! Semantic line break formatter for prose documents. Reformats text so each
//! sentence occupies its own line, producing minimal git diffs when
//! collaborating on papers and documentation.
//!
//! The crate is published as `snapper-fmt` on crates.io. Installers ship two
//! CLI names for the same program: `snapper` and `snapper-fmt` (the latter
//! avoids colliding with openSUSE's Btrfs snapshot tool of the same name).
//!
//! ## Supported formats
//!
//! - **Org-mode**: drawers, tables, keywords preserved; `#+BEGIN_SRC` is
//!   `Region::Code` (comment reflow via `[code.<lang>]`, optional formatters)
//! - **LaTeX**: preamble and math preserved; `minted` / `lstlisting` are code regions
//! - **Markdown**: front matter and headings preserved; fenced blocks are code regions
//! - **RST**: directives and literals preserved; `.. code-block::` is a code region
//! - **Plaintext**: everything treated as prose
//!
//! ## Library usage
//!
//! ```rust
//! use snapper_fmt::{format_text, FormatConfig};
//! use snapper_fmt::format::Format;
//!
//! let input = "Hello world. This is a test. Another sentence.";
//! let config = FormatConfig {
//!     format: Format::Plaintext,
//!     ..Default::default()
//! };
//! let output = format_text(input, &config).unwrap();
//! assert_eq!(output, "Hello world.\nThis is a test.\nAnother sentence.");
//! ```

pub mod abbreviations;
pub mod check;
#[cfg(feature = "cli")]
pub mod cli;
pub mod code_block;
pub mod config;
pub mod diff;
#[cfg(not(target_arch = "wasm32"))]
pub mod files;
pub mod format;
#[cfg(not(target_arch = "wasm32"))]
pub mod git_diff;
#[cfg(feature = "cli")]
pub mod init;
#[cfg(feature = "lsp")]
pub mod lsp;
#[cfg(feature = "mcp")]
pub mod mcp;
pub mod oracle;
pub mod output;
pub mod parser;
pub mod reflow;
#[cfg(not(target_arch = "wasm32"))]
pub mod sdiff;
pub mod sentence;
#[cfg(feature = "treesitter")]
mod ts_comments;
#[cfg(feature = "wasm")]
pub mod wasm;
#[cfg(feature = "watch")]
pub mod watch;

use std::collections::HashMap;

use anyhow::Result;

use crate::config::CodeLang;
use crate::format::Format;
use crate::reflow::ReflowConfig;
use crate::sentence::SentenceSplitter;
use crate::sentence::unicode::UnicodeSentenceSplitter;

/// Configuration for the formatting pipeline.
pub struct FormatConfig {
    pub format: Format,
    pub max_width: usize,
    pub use_neural: bool,
    pub neural_lang: String,
    pub neural_model_path: Option<std::path::PathBuf>,
    pub extra_abbreviations: Vec<String>,
    pub use_pandoc: bool,
    /// Pandoc input format string (for pandoc backend).
    pub pandoc_format: Option<String>,
    /// How to obtain the pandoc AST when `use_pandoc` is set.
    /// `Ffi` uses in-process Haskell/C bindings; `Cli` uses a subprocess.
    #[cfg(feature = "pandoc")]
    pub pandoc_backend: parser::pandoc::PandocBackend,
    /// Per-language code-block configuration loaded from `[code]` in
    /// `.snapperrc.toml`. Empty by default; an empty map disables all
    /// per-language code-block behaviour (block passes through untouched).
    pub code: HashMap<String, CodeLang>,
    /// When `true`, the reflow stage invokes each language's `formatter`
    /// after comment reflow. Default `false` preserves v0.7.7 behaviour
    /// (no subprocess is spawned).
    pub format_code: bool,
    /// Prefer soft breaks after independent-clause punctuation (sembr
    /// rule 5). When `max_width` is 0, every such mark that is already
    /// followed by whitespace starts a new line. When `max_width` is
    /// greater than 0, overflowing sentences prefer those marks.
    /// Default `false` keeps one sentence per line (greedy wrap only
    /// under `max_width`).
    pub clause_breaks: bool,
    /// Run `format_text` to a byte fixpoint (cap 4). Production default
    /// `true`; tests set `false` so a planner that needs the backstop fails.
    pub fixpoint_backstop: bool,
    /// After the fixpoint, a format-local oracle mismatch returns the
    /// original document. Production default `true`; tests set `false`
    /// and assert the oracle themselves.
    pub render_backstop: bool,
    /// Extra LaTeX environments treated as code (no reflow), added to
    /// minted/lstlisting/verbatim. Empty keeps the built-in list.
    pub latex_verbatim_envs: Vec<String>,
    /// Extra LaTeX environments treated as structure (no reflow), added
    /// to `NON_PROSE_ENVS`. Empty keeps the built-in list.
    pub latex_structure_envs: Vec<String>,
    /// Extra LaTeX command names tokenized like `\verb` before split.
    /// Empty keeps verb/lstinline.
    pub latex_verbatim_commands: Vec<String>,
}

impl Default for FormatConfig {
    fn default() -> Self {
        Self {
            format: Format::Plaintext,
            max_width: 0,
            use_neural: false,
            neural_lang: "en".to_string(),
            neural_model_path: None,
            extra_abbreviations: vec![],
            use_pandoc: false,
            pandoc_format: None,
            #[cfg(feature = "pandoc")]
            pandoc_backend: parser::pandoc::PandocBackend::default(),
            code: HashMap::new(),
            format_code: false,
            clause_breaks: false,
            fixpoint_backstop: true,
            render_backstop: true,
            latex_verbatim_envs: vec![],
            latex_structure_envs: vec![],
            latex_verbatim_commands: vec![],
        }
    }
}

/// Typed error for invalid UTF-8 input. Branch with `error.downcast_ref`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("input is not valid UTF-8")]
pub struct InvalidUtf8Error;

/// Pandoc's AST has no source offsets, so it cannot splice into original
/// bytes. `format_text` refuses rather than reconstruct.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("pandoc backend cannot splice original source bytes")]
pub struct PandocCannotSplice;

/// Maximum pipeline passes including the first. Cap hit (or an A/B cycle)
/// returns the original document unchanged.
const MAX_FORMAT_PASSES: usize = 4;

impl FormatConfig {
    /// Tests that assert planner properties (idempotence, oracle, splice)
    /// must disable both backstops so a planner that needs them fails.
    pub fn without_safety_backstops(mut self) -> Self {
        self.fixpoint_backstop = false;
        self.render_backstop = false;
        self
    }
}

/// Run `step` until the output is a byte fixpoint or the cap is hit.
///
/// A cycle (including A/B) or a cap miss returns `original`. `enabled`
/// false runs `step` once. Public so tests can inject a cycling step.
pub fn run_fixpoint<F>(original: &str, enabled: bool, mut step: F) -> Result<String>
where
    F: FnMut(&str) -> Result<String>,
{
    let once = step(original)?;
    if !enabled {
        return Ok(once);
    }
    let mut cur = once;
    let mut seen = std::collections::HashSet::new();
    seen.insert(original.to_string());
    seen.insert(cur.clone());
    for _ in 1..MAX_FORMAT_PASSES {
        let next = step(&cur)?;
        if next == cur {
            return Ok(cur);
        }
        if !seen.insert(next.clone()) {
            return Ok(original.to_string());
        }
        cur = next;
    }
    Ok(original.to_string())
}

/// Build the appropriate sentence splitter from config.
pub fn build_splitter(config: &FormatConfig) -> Result<Box<dyn SentenceSplitter>> {
    if config.use_neural {
        #[cfg(feature = "neural")]
        {
            let neural = if let Some(ref path) = config.neural_model_path {
                sentence::neural::NeuralSentenceSplitter::from_path_with_extras(
                    path,
                    &config.neural_lang,
                    &config.extra_abbreviations,
                )
            } else {
                sentence::neural::NeuralSentenceSplitter::with_extras(
                    &config.neural_lang,
                    &config.extra_abbreviations,
                )
            };
            Ok(Box::new(
                neural
                    .map_err(|e| anyhow::anyhow!("{e}"))?
                    .with_verbatim_commands(config.latex_verbatim_commands.clone()),
            ))
        }
        #[cfg(not(feature = "neural"))]
        {
            Err(anyhow::anyhow!(
                "neural sentence splitting requires the 'neural' feature"
            ))
        }
    } else {
        Ok(Box::new(
            UnicodeSentenceSplitter::for_lang(&config.neural_lang, &config.extra_abbreviations)
                .with_verbatim_commands(config.latex_verbatim_commands.clone()),
        ))
    }
}

/// Format text with semantic line breaks.
pub fn format_text(input: &str, config: &FormatConfig) -> Result<String> {
    let splitter = build_splitter(config)?;
    format_text_with_splitter(input, config, splitter.as_ref())
}

/// Format raw bytes. Invalid UTF-8 is a hard error ([`InvalidUtf8Error`]).
pub fn format_bytes(input: &[u8], config: &FormatConfig) -> Result<Vec<u8>> {
    let s = std::str::from_utf8(input).map_err(|_| anyhow::Error::new(InvalidUtf8Error))?;
    format_text(s, config).map(|s| s.into_bytes())
}

/// Format text using a pre-constructed splitter (avoids reloading models per file).
pub fn format_text_with_splitter(
    input: &str,
    config: &FormatConfig,
    splitter: &dyn SentenceSplitter,
) -> Result<String> {
    let had_trailing_newline = input.ends_with('\n');
    let uses_crlf = input.contains("\r\n");

    // Normalize to LF for processing, restore CRLF at the end if needed.
    let normalized;
    let work_input = if uses_crlf {
        normalized = input.replace("\r\n", "\n");
        &normalized
    } else {
        input
    };

    let once = format_once(work_input, config, splitter, config.format_code)?;
    // Later passes prove prose stability. External code formatters
    // already ran on the first pass; re-invoking them multiplies
    // timeout budgets and is not part of the planner fixpoint.
    let candidate = run_fixpoint(work_input, config.fixpoint_backstop, |cur| {
        if cur == work_input {
            Ok(once.clone())
        } else {
            format_once(cur, config, splitter, false)
        }
    })?;

    let candidate = if config.render_backstop
        && candidate != work_input
        && !oracle::matches_ex(
            config.format,
            work_input,
            &candidate,
            config.format_code,
            Some(config),
        ) {
        work_input.to_string()
    } else {
        candidate
    };

    let mut output = candidate;

    // Preserve the original file's trailing newline convention.
    if had_trailing_newline && !output.ends_with('\n') {
        output.push('\n');
    } else if !had_trailing_newline {
        while output.ends_with('\n') {
            output.pop();
        }
    }

    // Restore CRLF if the input used it.
    if uses_crlf {
        output = output.replace('\n', "\r\n");
    }

    Ok(output)
}

/// One parse+reflow pass. Native parsers splice into original bytes;
/// pandoc concatenates reconstructed regions.
fn format_once(
    work_input: &str,
    config: &FormatConfig,
    splitter: &dyn SentenceSplitter,
    format_code: bool,
) -> Result<String> {
    use crate::parser::SpannedRegion;
    use crate::reflow::reflow_spanned;

    let reflow_config = ReflowConfig {
        max_width: config.max_width,
        code: Some(&config.code),
        format_code,
        clause_breaks: config.clause_breaks,
        format: config.format,
    };

    // Two pipelines:
    // - use_pandoc: pandoc parses source → AST → regions by node kind → reflow prose only.
    // - else: native line parsers (markdown/org/…) then splice. Never mixed after success.
    if config.use_pandoc {
        #[cfg(feature = "pandoc")]
        {
            let pandoc_fmt = config
                .pandoc_format
                .as_deref()
                .unwrap_or(match config.format {
                    Format::Org => "org",
                    Format::Latex => "latex",
                    Format::Markdown => "markdown",
                    Format::Rst => "rst",
                    Format::Plaintext => "markdown",
                });
            let parser =
                parser::pandoc::PandocParser::with_backend(pandoc_fmt, config.pandoc_backend);
            // Surface parse errors (no silent all-prose). Splice still
            // requires source offsets the AST does not carry.
            parser
                .try_parse(work_input)
                .map_err(|e| anyhow::anyhow!("{e}"))?;
            return Err(anyhow::Error::new(PandocCannotSplice));
        }
        #[cfg(not(feature = "pandoc"))]
        {
            return Err(anyhow::anyhow!(
                "pandoc backend requires the 'pandoc' feature"
            ));
        }
    }

    let spanned: Vec<SpannedRegion> =
        parser::parser_for_format_config(config.format, Some(config)).parse_full(work_input);
    match reflow_spanned(work_input, &spanned, splitter, &reflow_config) {
        Ok(out) => Ok(out),
        Err(_) => Ok(work_input.to_string()),
    }
}

/// Format only lines within a range (1-indexed, inclusive).
/// Lines outside the range pass through unchanged.
pub fn format_range(
    input: &str,
    config: &FormatConfig,
    start: usize,
    end: usize,
) -> Result<String> {
    let lines: Vec<&str> = input.lines().collect();
    let total = lines.len();

    // Clamp range
    let start = start.max(1);
    let end = end.min(total);

    if start > total {
        return Ok(input.to_string());
    }

    // Extract the range as a contiguous block
    let range_text = lines[start - 1..end].join("\n");
    let formatted = format_text(&range_text, config)?;

    // Reassemble: before + formatted + after
    let mut result = String::new();
    for (i, line) in lines.iter().enumerate() {
        let line_num = i + 1;
        if line_num < start {
            result.push_str(line);
            result.push('\n');
        }
    }
    result.push_str(&formatted);
    if !formatted.ends_with('\n') && end < total {
        result.push('\n');
    }
    for (i, line) in lines.iter().enumerate() {
        let line_num = i + 1;
        if line_num > end {
            result.push_str(line);
            if line_num < total {
                result.push('\n');
            }
        }
    }

    // Preserve original trailing newline convention
    if input.ends_with('\n') && !result.ends_with('\n') {
        result.push('\n');
    } else if !input.ends_with('\n') {
        while result.ends_with('\n') {
            result.pop();
        }
    }

    Ok(result)
}