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
use std::path::PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
use crate::diff::ColorMode;
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum FormatArg {
Org,
Latex,
Markdown,
Rst,
Plaintext,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum OutputFormat {
Text,
Json,
Sarif,
}
/// Control when colored output is used (ruff-style).
#[derive(Debug, Clone, Copy, ValueEnum, Default, PartialEq, Eq)]
pub enum ColorWhen {
/// Display colors if the output goes to an interactive terminal.
#[default]
Auto,
/// Always display colors.
Always,
/// Never display colors.
Never,
}
impl From<ColorWhen> for ColorMode {
fn from(value: ColorWhen) -> Self {
match value {
ColorWhen::Auto => ColorMode::Auto,
ColorWhen::Always => ColorMode::Always,
ColorWhen::Never => ColorMode::Never,
}
}
}
#[derive(Debug, Parser)]
#[command(name = "snapper", version, about = "Semantic line break formatter")]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
/// Input files. Reads stdin if omitted.
#[arg()]
pub files: Vec<PathBuf>,
/// Input format (auto-detected from extension if omitted).
#[arg(short, long)]
pub format: Option<FormatArg>,
/// Assume this filename when reading stdin (for format auto-detection).
#[arg(long)]
pub stdin_filepath: Option<PathBuf>,
/// Output file (stdout if omitted).
#[arg(short, long)]
pub output: Option<PathBuf>,
/// Modify files in place.
#[arg(short, long)]
pub in_place: bool,
/// Maximum line width (0 = unlimited).
#[arg(short = 'w', long, default_value_t = 0)]
pub max_width: usize,
/// Prefer soft breaks after independent-clause punctuation (comma,
/// semicolon, colon, em dash). With `--max-width 0` (the default),
/// insert a newline after every such mark that is already followed
/// by whitespace. With `--max-width` set, prefer those marks when
/// wrapping an overflowing sentence.
#[arg(long, default_value_t = false)]
pub clause_breaks: bool,
/// Use neural sentence detection (nnsplit LSTM model).
#[arg(long)]
pub neural: bool,
/// Language for neural sentence detection (default: en).
/// Available: en, de, fr, no, sv, zh, tr, ru, uk.
#[arg(long)]
pub lang: Option<String>,
/// Path to custom ONNX model file for neural detection.
#[arg(long)]
pub model_path: Option<PathBuf>,
/// Use pandoc as parser backend (universal format support).
#[arg(long)]
pub use_pandoc: bool,
/// Pandoc AST source when `--use-pandoc` is set:
/// `auto` (prefer in-process FFI, else CLI), `ffi` (`libsnapper_pandoc`),
/// or `cli` (`pandoc` subprocess). Default: `auto`.
/// `ffi` fails explicitly if the library is missing.
#[arg(long, default_value = "auto", value_name = "BACKEND")]
pub pandoc_backend: String,
/// Exit with code 1 if any file would change.
#[arg(long)]
pub check: bool,
/// Show a unified diff of what would change.
#[arg(long)]
pub diff: bool,
/// Control when colored output is used.
///
/// Possible values:
/// - auto: Display colors if the output goes to an interactive terminal
/// and the `NO_COLOR` environment variable is unset
/// - always: Always display colors
/// - never: Never display colors
#[arg(long, value_enum, default_value_t = ColorWhen::Auto, global = true, value_name = "WHEN")]
pub color: ColorWhen,
/// Path to config file (default: .snapperrc.toml in current or parent dirs).
#[arg(long)]
pub config: Option<PathBuf>,
/// Only format lines in this range (1-indexed, inclusive). Format: START:END.
#[arg(long)]
pub range: Option<String>,
/// Output format for --check mode.
#[arg(long, default_value = "text")]
pub output_format: OutputFormat,
/// Treat advisory `long` diagnostics as `--check` failures.
///
/// `long` never fails the check on its own unless this flag is set.
#[arg(long, default_value_t = false)]
pub strict_long: bool,
/// Pipe each code block's body through the per-language formatter
/// configured under `[code.<lang>.formatter]` in `.snapperrc.toml`.
/// The formatter runs after the in-block comment reflow. Missing
/// binaries, non-zero exits, and timeouts surface as stderr
/// diagnostics; snapper still exits 0.
#[arg(long, default_value_t = false)]
pub format_code: bool,
}
#[derive(Debug, Subcommand)]
pub enum Commands {
/// Initialize snapper for a project (generate config, pre-commit, gitattributes).
Init {
/// Preview what would be generated without writing files.
#[arg(long)]
dry_run: bool,
},
/// Sentence-level diff between two files.
Sdiff {
/// Original file.
old: PathBuf,
/// Modified file.
new: PathBuf,
/// Input format (auto-detected from extension if omitted).
#[arg(short, long)]
format: Option<FormatArg>,
/// Disable colored output (alias for `--color never`).
#[arg(long)]
no_color: bool,
},
/// Sentence-level diff against a git ref.
GitDiff {
/// Git ref to compare against (default: HEAD).
#[arg(default_value = "HEAD")]
git_ref: String,
/// Files to diff. If omitted, diffs all changed prose files.
#[arg()]
files: Vec<PathBuf>,
/// Input format (auto-detected from extension if omitted).
#[arg(short, long)]
format: Option<FormatArg>,
/// Disable colored output (alias for `--color never`).
#[arg(long)]
no_color: bool,
},
/// Start the LSP server (stdin/stdout).
Lsp,
/// Start the MCP server (stdin/stdout).
Mcp,
/// Watch files and reformat on change.
Watch {
/// Files or glob patterns to watch.
#[arg(required = true)]
patterns: Vec<String>,
/// Input format (auto-detected from extension if omitted).
#[arg(short, long)]
format: Option<FormatArg>,
},
}
/// Parse a range string "START:END" into (start, end) 1-indexed inclusive.
pub fn parse_range(s: &str) -> Option<(usize, usize)> {
let parts: Vec<&str> = s.split(':').collect();
if parts.len() != 2 {
return None;
}
let start = parts[0].parse::<usize>().ok()?;
let end = parts[1].parse::<usize>().ok()?;
if start == 0 || end == 0 || start > end {
return None;
}
Some((start, end))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_range_valid() {
assert_eq!(parse_range("1:10"), Some((1, 10)));
assert_eq!(parse_range("5:5"), Some((5, 5)));
assert_eq!(parse_range("1:1"), Some((1, 1)));
}
#[test]
fn parse_range_zero_rejected() {
assert_eq!(parse_range("0:5"), None);
assert_eq!(parse_range("5:0"), None);
assert_eq!(parse_range("0:0"), None);
}
#[test]
fn parse_range_reversed_rejected() {
assert_eq!(parse_range("10:5"), None);
}
#[test]
fn parse_range_bad_format() {
assert_eq!(parse_range("abc"), None);
assert_eq!(parse_range("1:2:3"), None);
assert_eq!(parse_range(""), None);
assert_eq!(parse_range("a:b"), None);
assert_eq!(parse_range(":5"), None);
assert_eq!(parse_range("5:"), None);
}
}