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
433
434
435
436
437
438
439
use pathfinding::prelude::dijkstra;
use unicode_width::UnicodeWidthStr;

pub struct FormatOpts {
    pub max_length: usize,
    pub tab_width: usize,
    pub last_line: bool,
    pub reduce_jaggedness: bool,
}

impl Default for FormatOpts {
    fn default() -> Self {
        FormatOpts {
            max_length: 72,
            last_line: false,
            reduce_jaggedness: false,
            tab_width: 4,
        }
    }
}

#[allow(dead_code)]
impl FormatOpts {
    pub fn with_max_length(max_length: usize) -> Self {
        Self {
            max_length,
            ..Default::default()
        }
    }

    pub fn new(
        max_length: usize,
        last_line: bool,
        reduce_jaggedness: bool,
        tab_width: usize,
    ) -> Self {
        Self {
            max_length,
            last_line,
            reduce_jaggedness,
            tab_width,
        }
    }
}

trait Width {
    fn width(&self) -> usize;
}

impl Width for String {
    fn width(&self) -> usize {
        UnicodeWidthStr::width(self.as_str())
    }
}

impl Width for &str {
    fn width(&self) -> usize {
        UnicodeWidthStr::width(*self)
    }
}

#[derive(Debug)]
struct Block {
    prefix: String,
    suffix: String,
    words: Vec<String>,
    newline_after: bool,
}

enum Dir {
    Forward,
    Reverse,
}

fn longest_common_affix(char_slices: &[Vec<char>], dir: Dir) -> Vec<char> {
    let get = |s: &[char], i| match dir {
        Dir::Forward => s[i],
        Dir::Reverse => s[s.len() - i - 1],
    };

    if char_slices.is_empty() {
        return vec![];
    }
    let mut ret = Vec::new();
    let mut i = 0;
    loop {
        let mut c = None;
        for s in char_slices {
            if i == s.len() {
                if let Dir::Reverse = dir {
                    ret.reverse();
                }
                return ret;
            }
            match c {
                None => {
                    c = Some(get(s, i));
                }
                Some(letter) if letter != get(s, i) => {
                    if let Dir::Reverse = dir {
                        ret.reverse();
                    }
                    return ret;
                }
                _ => continue,
            }
        }
        if let Some(letter) = c {
            ret.push(letter);
        }
        i += 1;
    }
}

fn spaces(n: usize) -> String {
    std::iter::repeat(" ").take(n).collect::<String>()
}

fn collect_blocks(char_slices: &[Vec<char>], prefix: Vec<char>, suffix: Vec<char>) -> Vec<Block> {
    let prefixstr: String = prefix.iter().collect();
    let suffixstr: String = suffix.iter().collect();

    let mut blocks: Vec<Block> = vec![];
    let mut words: Vec<String> = vec![];
    let mut indentation = 0;
    for (i, line) in char_slices.iter().enumerate() {
        let len = line.len();
        let trimmed = &line[std::cmp::min(prefix.len(), len)..(len - suffix.len())];
        let mut word = String::new();
        let mut found_non_blank = false;
        for c in trimmed {
            if c.is_whitespace() {
                if !word.is_empty() {
                    words.push(word);
                    word = String::new();
                }
                if words.is_empty() && i == 0 {
                    indentation += 1;
                }
            } else {
                found_non_blank = true;
                word.push(*c);
            }
        }
        if !word.is_empty() {
            words.push(word);
        } else if !found_non_blank {
            if !words.is_empty() {
                let mut s = spaces(indentation);
                s.push_str(&words[0]);
                words[0] = s;
            }
            blocks.push(Block {
                prefix: prefixstr.clone(),
                suffix: suffixstr.clone(),
                words,
                newline_after: true,
            });
            words = vec![];
            indentation = 0;
        }
    }
    if !words.is_empty() {
        let mut s = spaces(indentation);
        s.push_str(&words[0]);
        words[0] = s;

        blocks.push(Block {
            prefix: prefixstr,
            suffix: suffixstr,
            words,
            newline_after: false,
        });
    }
    blocks
}

fn is_quote_char(c: &char) -> bool {
    c.is_whitespace() || c == &'>'
}

fn is_not_quote_char(c: &char) -> bool {
    !is_quote_char(c)
}

fn get_quotes(chars: &[char]) -> (usize, Vec<char>) {
    let quote_chars = chars.splitn(2, is_not_quote_char).next().unwrap();
    let quote_vec: Vec<_> = quote_chars.to_vec();
    if quote_vec.is_empty() {
        (0, vec![])
    } else {
        let l = quote_vec.iter().filter(|&c| *c == '>').count();
        (l, quote_vec)
    }
}

fn analyze_quotes(char_slices: &[Vec<char>]) -> Option<Vec<Block>> {
    let quotes: Vec<_> = char_slices
        .iter()
        .map(|c| get_quotes(c.as_slice()))
        .collect();
    if quotes.iter().any(|&(l, _)| l > 0) {
        let mut blocks = vec![];
        let mut quote = &(0, vec![]);
        let mut i = 0;
        let mut idx = 0;
        for this_quote in quotes.iter() {
            if quotes[i].0 != quote.0 {
                if idx < i {
                    blocks.extend(collect_blocks(
                        &char_slices[idx..i],
                        quote.1.clone(),
                        vec![],
                    ));
                }
                quote = this_quote;
                idx = i;
            }
            i += 1;
        }
        if idx < i {
            blocks.extend(collect_blocks(
                &char_slices[idx..i],
                quote.1.clone(),
                vec![],
            ));
        }
        Some(blocks)
    } else {
        None
    }
}

fn analyze_surround(char_slices: &[Vec<char>]) -> Option<Vec<Block>> {
    let mut prefix = longest_common_affix(char_slices, Dir::Forward);
    let mut suffix = longest_common_affix(char_slices, Dir::Reverse);

    if prefix == suffix && !prefix.is_empty() {
        prefix = vec![];
        suffix = vec![];
    }

    Some(collect_blocks(char_slices, prefix, suffix))
}

fn analyze(lines: &[&str]) -> Vec<Block> {
    let charlines = lines
        .iter()
        .map(|l| l.chars().collect::<Vec<_>>())
        .collect::<Vec<_>>();
    let blocks = analyze_quotes(&charlines[..]).or_else(|| analyze_surround(&charlines[..]));
    blocks.unwrap()
}

#[derive(Debug)]
struct Entry {
    len: usize,
    offset: usize,
}

impl Entry {
    fn new(len: usize, offset: usize) -> Self {
        Entry { len, offset }
    }
}

#[derive(Default)]
pub struct Reformatter {
    blocks: Vec<Block>,
    target: usize,
    last_line: bool,
    fit: bool,
}

impl Reformatter {
    pub fn new(opts: &FormatOpts, data: &str) -> Self {
        let expanded = spaces(opts.tab_width);
        let data = data.replace("\t", &expanded);
        let input_lines: Vec<_> = data.lines().collect();
        let blocks = analyze(&input_lines);
        // eprintln!("Prefix: {}, Suffix: {}, Max: {}, Target: {}", prefix, suffix, opts.max_length, target);
        Reformatter {
            blocks,
            target: opts.max_length,
            last_line: opts.last_line,
            fit: opts.reduce_jaggedness,
        }
    }

    fn successors(
        &self,
        entries: &[Entry],
        i: usize,
        target: usize,
        allow_overage: bool,
    ) -> Vec<(usize, u64)> {
        let word1 = &entries[i];
        let count = entries.len();
        let mut results = vec![];
        for (j, word2) in entries.iter().enumerate().take(count).skip(i + 1) {
            let linew = word2.offset - word1.offset + j - i - 1;
            //width of all words + width of all spaces = total line width
            if linew > target {
                if results.is_empty() && allow_overage {
                    // ensure there's always at least a bail-out option
                    // for the next word, but very expensive
                    results.push((j, 100_000));
                }
                break;
            }
            let cost;
            if j > (count - 2) && !self.last_line {
                //handle last line
                cost = 0;
            } else {
                let diff = (target - linew) as u64;
                cost = diff * diff;
            };
            results.push((j, cost));
        }
        results
    }

    fn solve(&self, words: &[String], target: usize) -> (Vec<usize>, u64) {
        let count = words.len();
        let dummy = Entry::new(0, 0);

        let mut entries = vec![dummy];
        let mut offset = 0;
        for w in words {
            let wid = w.width();
            offset += wid;
            entries.push(Entry::new(wid, offset));
        }

        let result = dijkstra(
            &0,
            |i| self.successors(&entries, *i, target, false),
            |i| *i == count,
        );

        if let Some(tuple) = result {
            tuple
        } else {
            eprintln!("Warning: allowing some words to extend beyond target width");
            // try again, allowing overage
            dijkstra(
                &0,
                |i| self.successors(&entries, *i, target, true),
                |i| *i == count,
            )
            .expect("Unable to find optimum solution")
        }
    }

    fn reformat_section(&self, block: &Block) -> (Vec<String>, usize) {
        let words = &block.words;
        let rawtarget =
            self.target as i64 - block.prefix.width() as i64 - block.suffix.width() as i64;
        let target = std::cmp::max(rawtarget, 1) as usize;

        let min_target = if self.fit { target / 2 } else { target };
        let max_target = target;

        let mut path = vec![];
        let mut best_cost = 100_000_000;
        let mut best_target = max_target;

        for target in (min_target..=max_target).rev() {
            let (p, cost) = self.solve(words, target);
            // higher cost the further from original target
            let cost = cost + max_target as u64 - target as u64;
            if cost < best_cost {
                best_cost = cost;
                path = p;
                best_target = target;
            }
        }

        let mut lines: Vec<String> = vec![];

        for s in path.windows(2) {
            if let [start, end] = *s {
                let mut l = block.prefix.clone();
                l.push_str(
                    &words[start..end]
                        .iter()
                        .map(|w| w.to_string())
                        .collect::<Vec<_>>()
                        .join(" "),
                );
                lines.push(l);
            }
        }
        if block.newline_after {
            let extra = block.prefix.trim_end().to_string();
            lines.push(extra);
        }
        (lines, best_target)
    }

    pub fn reformatted(&self) -> String {
        // get "unadorned" body
        let sections: Vec<_> = self
            .blocks
            .iter()
            .map(|s| (s, self.reformat_section(s)))
            .collect();
        let max_padding = sections
            .iter()
            .map(|s| (s.1).1)
            .max()
            .unwrap_or(self.target) as i64;

        let mut output = vec![];

        for (block, (body, _)) in &sections {
            let suffix_length = block.suffix.width();
            let prefix_length = block.prefix.width();

            for l in body {
                let mut line = l.clone();
                if suffix_length > 0 {
                    let pad_amount = max_padding - l.width() as i64 + prefix_length as i64;
                    let pad = spaces(std::cmp::max(pad_amount, 0i64) as usize);
                    line.push_str(&pad);
                    line.push_str(&block.suffix);
                }
                output.push(line);
            }
        }
        output.join("\n")
    }
}

pub fn reformat(opts: &FormatOpts, data: &str) -> String {
    let rfmt = Reformatter::new(opts, data);
    rfmt.reformatted()
}