summaryrefslogtreecommitdiffstats
path: root/src/format.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/format.rs')
-rw-r--r--src/format.rs28
1 files changed, 26 insertions, 2 deletions
diff --git a/src/format.rs b/src/format.rs
index 67d1f509..1f8bfb00 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -1,4 +1,5 @@
use regex::Regex;
+use unicode_segmentation::UnicodeSegmentation;
#[derive(Debug, Default, PartialEq)]
pub struct FormatStringPlaceholderData<'a> {
@@ -7,6 +8,23 @@ pub struct FormatStringPlaceholderData<'a> {
pub alignment_spec: Option<&'a str>,
pub width: Option<usize>,
pub suffix: &'a str,
+ pub prefix_len: usize,
+ pub suffix_len: usize,
+}
+
+impl<'a> FormatStringPlaceholderData<'a> {
+ pub fn width(&self, hunk_max_line_number_width: usize) -> (usize, usize) {
+ // Only if Some(placeholder) is present will there be a number formatted
+ // by this placeholder, if not width is also None.
+ (
+ self.prefix_len
+ + std::cmp::max(
+ self.placeholder.map_or(0, |_| hunk_max_line_number_width),
+ self.width.unwrap_or(0),
+ ),
+ self.suffix_len,
+ )
+ }
}
pub type FormatStringData<'a> = Vec<FormatStringPlaceholderData<'a>>;
@@ -40,8 +58,10 @@ pub fn parse_line_number_format<'a>(
for captures in placeholder_regex.captures_iter(format_string) {
let _match = captures.get(0).unwrap();
+ let prefix = &format_string[offset.._match.start()];
+ let suffix = &format_string[_match.end()..];
format_data.push(FormatStringPlaceholderData {
- prefix: &format_string[offset.._match.start()],
+ prefix,
placeholder: captures.get(1).map(|m| m.as_str()),
alignment_spec: captures.get(3).map(|m| m.as_str()),
width: captures.get(4).map(|m| {
@@ -49,7 +69,9 @@ pub fn parse_line_number_format<'a>(
.parse()
.unwrap_or_else(|_| panic!("Invalid width in format string: {}", format_string))
}),
- suffix: &format_string[_match.end()..],
+ suffix,
+ prefix_len: prefix.graphemes(true).count(),
+ suffix_len: suffix.graphemes(true).count(),
});
offset = _match.end();
}
@@ -61,6 +83,8 @@ pub fn parse_line_number_format<'a>(
alignment_spec: None,
width: None,
suffix: &format_string[0..],
+ prefix_len: 0,
+ suffix_len: format_string.graphemes(true).count(),
})
}
format_data