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
|
use chrono::{Duration, NaiveDate};
use std::borrow::Cow;
use ropey::RopeSlice;
use crate::{
textobject::{textobject_word, TextObject},
Range, Tendril,
};
// Only support formats that aren't region specific.
static FORMATS: &[&str] = &["%Y-%m-%d", "%Y/%m/%d"];
// We don't want to parse ambiguous dates like 10/11/12 or 7/8/10.
// They must be YYYY-mm-dd or YYYY/mm/dd.
// So 2021-01-05 works, but 2021-1-5 doesn't.
const DATE_LENGTH: usize = 10;
#[derive(Debug, PartialEq, Eq)]
pub struct DateIncrementor {
pub date: NaiveDate,
pub range: Range,
pub format: &'static str,
}
impl DateIncrementor {
pub fn from_range(text: RopeSlice, range: Range) -> Option<DateIncrementor> {
// Don't increment if the cursor is one right of the date text.
if text.char(range.from()).is_whitespace() {
return None;
}
let range = textobject_word(text, range, TextObject::Inside, 1, true);
let text: Cow<str> = text.slice(range.from()..range.to()).into();
let first = text.chars().next()?;
let last = text.chars().next_back()?;
// Allow date strings in quotes.
let (range, text) = if first == last && (first == '"' || first == '\'') {
(
Range::new(range.from() + 1, range.to() - 1),
Cow::from(&text[1..text.len() - 1]),
)
} else {
(range, text)
};
if text.len() != DATE_LENGTH {
return None;
}
FORMATS.iter().find_map(|format| {
NaiveDate::parse_from_str(&text, format)
.ok()
.map(|date| DateIncrementor {
date,
range,
format,
})
})
}
pub fn incremented_text(&self, amount: i64) -> Tendril {
let incremented_date = self.date + Duration::days(amount);
incremented_date.format(self.format).to_string().into()
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::Rope;
#[test]
fn test_date_dashes() {
let rope = Rope::from_str("2021-11-15");
let range = Range::point(0);
assert_eq!(
DateIncrementor::from_range(rope.slice(..), range),
Some(DateIncrementor {
date: NaiveDate::from_ymd(2021, 11, 15),
range: Range::new(0, 10),
format: "%Y-%m-%d",
})
);
}
#[test]
fn test_date_slashes() {
let rope = Rope::from_str("2021/11/15");
let range = Range::point(0);
assert_eq!(
DateIncrementor::from_range(rope.slice(..), range),
Some(DateIncrementor {
date: NaiveDate::from_ymd(2021, 11, 15),
range: Range::new(0, 10),
format: "%Y/%m/%d",
})
);
}
#[test]
fn test_date_surrounded_by_spaces() {
let rope = Rope::from_str(" 2021-11-15 ");
let range = Range::point(10);
assert_eq!(
DateIncrementor::from_range(rope.slice(..), range),
Some(DateIncrementor {
date: NaiveDate::from_ymd(2021, 11, 15),
range: Range::new(3, 13),
format: "%Y-%m-%d",
})
);
}
#[test]
fn test_date_in_single_quotes() {
let rope = Rope::from_str("date = '2021-11-15'");
let range = Range::point(10);
assert_eq!(
DateIncrementor::from_range(rope.slice(..), range),
Some(DateIncrementor {
date: NaiveDate::from_ymd(2021, 11, 15),
range: Range::new(8, 18),
format: "%Y-%m-%d",
})
);
}
#[test]
fn test_date_in_double_quotes() {
let rope = Rope::from_str("date = \"2021-11-15\"");
let range = Range::point(10);
assert_eq!(
DateIncrementor::from_range(rope.slice(..), range),
Some(DateIncrementor {
date: NaiveDate::from_ymd(2021, 11, 15),
range: Range::new(8, 18),
format: "%Y-%m-%d",
})
);
}
#[test]
fn test_date_cursor_one_right_of_date() {
let rope = Rope::from_str("2021-11-15 ");
let range = Range::point(10);
assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None);
}
#[test]
fn test_date_cursor_one_left_of_number() {
let rope = Rope::from_str(" 2021-11-15");
let range = Range::point(0);
assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None);
}
#[test]
fn test_invalid_dates() {
let tests = [
"0000-00-00",
"1980-2-21",
"1980-12-1",
"12345",
"2020-02-30",
"1999-12-32",
"19-12-32",
"1-2-3",
"0000/00/00",
"1980/2/21",
"1980/12/1",
"12345",
"2020/02/30",
"1999/12/32",
"19/12/32",
"1/2/3",
];
for invalid in tests {
let rope = Rope::from_str(invalid);
let range = Range::point(0);
assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None);
}
}
#[test]
fn test_increment_dates() {
let tests = [
("1980-12-21", 1, "1980-12-22"),
("1980-12-21", -1, "1980-12-20"),
("1980-12-21", 100, "1981-03-31"),
("1980-12-21", -100, "1980-09-12"),
("1980-12-21", 1000, "1983-09-17"),
("1980-12-21", -1000, "1978-03-27"),
("1980/12/21", 1, "1980/12/22"),
("1980/12/21", -1, "1980/12/20"),
("1980/12/21", 100, "1981/03/31"),
("1980/12/21", -100, "1980/09/12"),
("1980/12/21", 1000, "1983/09/17"),
("1980/12/21", -1000, "1978/03/27"),
];
for (original, amount, expected) in tests {
let rope = Rope::from_str(original);
let range = Range::point(0);
assert_eq!(
DateIncrementor::from_range(rope.slice(..), range)
.unwrap()
.incremented_text(amount),
expected.into()
);
}
}
}
|