fmt: merge prefix and use_prefix options (same for anti_prefix)

This commit is contained in:
Terts Diepraam 2023-11-28 12:05:35 +01:00
parent d78923e4cc
commit f5206ce783
2 changed files with 13 additions and 32 deletions

View file

@ -49,11 +49,9 @@ pub struct FmtOptions {
tagged: bool, tagged: bool,
mail: bool, mail: bool,
split_only: bool, split_only: bool,
use_prefix: bool, prefix: Option<String>,
prefix: String,
xprefix: bool, xprefix: bool,
use_anti_prefix: bool, anti_prefix: Option<String>,
anti_prefix: String,
xanti_prefix: bool, xanti_prefix: bool,
uniform: bool, uniform: bool,
quick: bool, quick: bool,
@ -83,19 +81,8 @@ impl FmtOptions {
let xprefix = matches.contains_id(OPT_EXACT_PREFIX); let xprefix = matches.contains_id(OPT_EXACT_PREFIX);
let xanti_prefix = matches.contains_id(OPT_SKIP_PREFIX); let xanti_prefix = matches.contains_id(OPT_SKIP_PREFIX);
let mut prefix = String::new(); let prefix = matches.get_one::<String>(OPT_PREFIX).map(String::from);
let mut use_prefix = false; let anti_prefix = matches.get_one::<String>(OPT_SKIP_PREFIX).map(String::from);
if let Some(s) = matches.get_one::<String>(OPT_PREFIX).map(String::from) {
prefix = s;
use_prefix = true;
};
let mut anti_prefix = String::new();
let mut use_anti_prefix = false;
if let Some(s) = matches.get_one::<String>(OPT_SKIP_PREFIX).map(String::from) {
anti_prefix = s;
use_anti_prefix = true;
};
let mut width = 75; let mut width = 75;
let mut goal = 70; let mut goal = 70;
@ -143,10 +130,8 @@ impl FmtOptions {
uniform, uniform,
quick, quick,
split_only, split_only,
use_prefix,
prefix, prefix,
xprefix, xprefix,
use_anti_prefix,
anti_prefix, anti_prefix,
xanti_prefix, xanti_prefix,
width, width,

View file

@ -80,24 +80,20 @@ impl<'a> FileLines<'a> {
/// returns true if this line should be formatted /// returns true if this line should be formatted
fn match_prefix(&self, line: &str) -> (bool, usize) { fn match_prefix(&self, line: &str) -> (bool, usize) {
if !self.opts.use_prefix { let Some(prefix) = &self.opts.prefix else {
return (true, 0); return (true, 0);
} };
FileLines::match_prefix_generic(&self.opts.prefix[..], line, self.opts.xprefix) FileLines::match_prefix_generic(prefix, line, self.opts.xprefix)
} }
/// returns true if this line should be formatted /// returns true if this line should be formatted
fn match_anti_prefix(&self, line: &str) -> bool { fn match_anti_prefix(&self, line: &str) -> bool {
if !self.opts.use_anti_prefix { let Some(anti_prefix) = &self.opts.anti_prefix else {
return true; return true;
} };
match FileLines::match_prefix_generic( match FileLines::match_prefix_generic(anti_prefix, line, self.opts.xanti_prefix) {
&self.opts.anti_prefix[..],
line,
self.opts.xanti_prefix,
) {
(true, _) => false, (true, _) => false,
(_, _) => true, (_, _) => true,
} }
@ -176,7 +172,7 @@ impl<'a> Iterator for FileLines<'a> {
// not truly blank we will not allow mail headers on the // not truly blank we will not allow mail headers on the
// following line) // following line)
if pmatch if pmatch
&& n[poffset + self.opts.prefix.len()..] && n[poffset + self.opts.prefix.as_ref().map_or(0, |s| s.len())..]
.chars() .chars()
.all(char::is_whitespace) .all(char::is_whitespace)
{ {
@ -190,7 +186,7 @@ impl<'a> Iterator for FileLines<'a> {
} }
// figure out the indent, prefix, and prefixindent ending points // figure out the indent, prefix, and prefixindent ending points
let prefix_end = poffset + self.opts.prefix.len(); let prefix_end = poffset + self.opts.prefix.as_ref().map_or(0, |s| s.len());
let (indent_end, prefix_len, indent_len) = self.compute_indent(&n[..], prefix_end); let (indent_end, prefix_len, indent_len) = self.compute_indent(&n[..], prefix_end);
Some(Line::FormatLine(FileLine { Some(Line::FormatLine(FileLine {
@ -357,7 +353,7 @@ impl<'a> Iterator for ParagraphStream<'a> {
} }
} else if in_mail { } else if in_mail {
// lines following mail headers must begin with spaces // lines following mail headers must begin with spaces
if fl.indent_end == 0 || (self.opts.use_prefix && fl.pfxind_end == 0) { if fl.indent_end == 0 || (self.opts.prefix.is_some() && fl.pfxind_end == 0) {
break; // this line does not begin with spaces break; // this line does not begin with spaces
} }
} else if !second_done { } else if !second_done {