From 1d773bcefb40f69fe31dc048bfbdd83601fe0e62 Mon Sep 17 00:00:00 2001 From: ath3 Date: Sun, 28 Nov 2021 02:21:40 +0100 Subject: Implement black hole register (#1165) --- helix-core/src/register.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/register.rs b/helix-core/src/register.rs index c5444eb7..b9eb497d 100644 --- a/helix-core/src/register.rs +++ b/helix-core/src/register.rs @@ -15,7 +15,11 @@ impl Register { } pub fn new_with_values(name: char, values: Vec) -> Self { - Self { name, values } + if name == '_' { + Self::new(name) + } else { + Self { name, values } + } } pub const fn name(&self) -> char { @@ -27,11 +31,15 @@ impl Register { } pub fn write(&mut self, values: Vec) { - self.values = values; + if self.name != '_' { + self.values = values; + } } pub fn push(&mut self, value: String) { - self.values.push(value); + if self.name != '_' { + self.values.push(value); + } } } -- cgit v1.2.3-70-g09d2 From dc53e65b9e9be71c49eaa86e0f4dabb69f586e2e Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Mon, 29 Nov 2021 07:03:53 +0530 Subject: Fix surround cursor position calculation (#1183) Fixes #1077. This was caused by the assumption that a block cursor is represented as zero width internally and simply rendered to be a single width selection, where as in reality a block cursor is an actual single width selection in form and function. Behavioural changes: 1. Surround selection no longer works when cursor is _on_ a surround character that has matching pairs (like `'` or `"`). This was the intended behaviour from the start but worked till now because of the cursor position calculation mismatch.--- helix-core/src/selection.rs | 6 +- helix-core/src/surround.rs | 148 ++++++++++++++++++++++++------------------- helix-core/src/textobject.rs | 10 +-- 3 files changed, 92 insertions(+), 72 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/selection.rs b/helix-core/src/selection.rs index b4d1dffa..116a1c7c 100644 --- a/helix-core/src/selection.rs +++ b/helix-core/src/selection.rs @@ -308,10 +308,10 @@ impl Range { } impl From<(usize, usize)> for Range { - fn from(tuple: (usize, usize)) -> Self { + fn from((anchor, head): (usize, usize)) -> Self { Self { - anchor: tuple.0, - head: tuple.1, + anchor, + head, horiz: None, } } diff --git a/helix-core/src/surround.rs b/helix-core/src/surround.rs index 32161b70..b53b0a78 100644 --- a/helix-core/src/surround.rs +++ b/helix-core/src/surround.rs @@ -1,4 +1,4 @@ -use crate::{search, Selection}; +use crate::{search, Range, Selection}; use ropey::RopeSlice; pub const PAIRS: &[(char, char)] = &[ @@ -35,33 +35,27 @@ pub fn get_pair(ch: char) -> (char, char) { pub fn find_nth_pairs_pos( text: RopeSlice, ch: char, - pos: usize, + range: Range, n: usize, ) -> Option<(usize, usize)> { - let (open, close) = get_pair(ch); - - if text.len_chars() < 2 || pos >= text.len_chars() { + if text.len_chars() < 2 || range.to() >= text.len_chars() { return None; } + let (open, close) = get_pair(ch); + let pos = range.cursor(text); + if open == close { if Some(open) == text.get_char(pos) { - // Special case: cursor is directly on a matching char. - match pos { - 0 => Some((pos, search::find_nth_next(text, close, pos + 1, n)?)), - _ if (pos + 1) == text.len_chars() => { - Some((search::find_nth_prev(text, open, pos, n)?, pos)) - } - // We return no match because there's no way to know which - // side of the char we should be searching on. - _ => None, - } - } else { - Some(( - search::find_nth_prev(text, open, pos, n)?, - search::find_nth_next(text, close, pos, n)?, - )) + // Cursor is directly on match char. We return no match + // because there's no way to know which side of the char + // we should be searching on. + return None; } + Some(( + search::find_nth_prev(text, open, pos, n)?, + search::find_nth_next(text, close, pos, n)?, + )) } else { Some(( find_nth_open_pair(text, open, close, pos, n)?, @@ -160,8 +154,8 @@ pub fn get_surround_pos( ) -> Option> { let mut change_pos = Vec::new(); - for range in selection { - let (open_pos, close_pos) = find_nth_pairs_pos(text, ch, range.head, skip)?; + for &range in selection { + let (open_pos, close_pos) = find_nth_pairs_pos(text, ch, range, skip)?; if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) { return None; } @@ -178,67 +172,91 @@ mod test { use ropey::Rope; use smallvec::SmallVec; - #[test] - fn test_find_nth_pairs_pos() { - let doc = Rope::from("some (text) here"); + fn check_find_nth_pair_pos( + text: &str, + cases: Vec<(usize, char, usize, Option<(usize, usize)>)>, + ) { + let doc = Rope::from(text); let slice = doc.slice(..); - // cursor on [t]ext - assert_eq!(find_nth_pairs_pos(slice, '(', 6, 1), Some((5, 10))); - assert_eq!(find_nth_pairs_pos(slice, ')', 6, 1), Some((5, 10))); - // cursor on so[m]e - assert_eq!(find_nth_pairs_pos(slice, '(', 2, 1), None); - // cursor on bracket itself - assert_eq!(find_nth_pairs_pos(slice, '(', 5, 1), Some((5, 10))); - assert_eq!(find_nth_pairs_pos(slice, '(', 10, 1), Some((5, 10))); + for (cursor_pos, ch, n, expected_range) in cases { + let range = find_nth_pairs_pos(slice, ch, (cursor_pos, cursor_pos + 1).into(), n); + assert_eq!( + range, expected_range, + "Expected {:?}, got {:?}", + expected_range, range + ); + } } #[test] - fn test_find_nth_pairs_pos_skip() { - let doc = Rope::from("(so (many (good) text) here)"); - let slice = doc.slice(..); + fn test_find_nth_pairs_pos() { + check_find_nth_pair_pos( + "some (text) here", + vec![ + // cursor on [t]ext + (6, '(', 1, Some((5, 10))), + (6, ')', 1, Some((5, 10))), + // cursor on so[m]e + (2, '(', 1, None), + // cursor on bracket itself + (5, '(', 1, Some((5, 10))), + (10, '(', 1, Some((5, 10))), + ], + ); + } - // cursor on go[o]d - assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((10, 15))); - assert_eq!(find_nth_pairs_pos(slice, '(', 13, 2), Some((4, 21))); - assert_eq!(find_nth_pairs_pos(slice, '(', 13, 3), Some((0, 27))); + #[test] + fn test_find_nth_pairs_pos_skip() { + check_find_nth_pair_pos( + "(so (many (good) text) here)", + vec![ + // cursor on go[o]d + (13, '(', 1, Some((10, 15))), + (13, '(', 2, Some((4, 21))), + (13, '(', 3, Some((0, 27))), + ], + ); } #[test] fn test_find_nth_pairs_pos_same() { - let doc = Rope::from("'so 'many 'good' text' here'"); - let slice = doc.slice(..); - - // cursor on go[o]d - assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 1), Some((10, 15))); - assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 2), Some((4, 21))); - assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 3), Some((0, 27))); - // cursor on the quotes - assert_eq!(find_nth_pairs_pos(slice, '\'', 10, 1), None); - // this is the best we can do since opening and closing pairs are same - assert_eq!(find_nth_pairs_pos(slice, '\'', 0, 1), Some((0, 4))); - assert_eq!(find_nth_pairs_pos(slice, '\'', 27, 1), Some((21, 27))); + check_find_nth_pair_pos( + "'so 'many 'good' text' here'", + vec![ + // cursor on go[o]d + (13, '\'', 1, Some((10, 15))), + (13, '\'', 2, Some((4, 21))), + (13, '\'', 3, Some((0, 27))), + // cursor on the quotes + (10, '\'', 1, None), + ], + ) } #[test] fn test_find_nth_pairs_pos_step() { - let doc = Rope::from("((so)((many) good (text))(here))"); - let slice = doc.slice(..); - - // cursor on go[o]d - assert_eq!(find_nth_pairs_pos(slice, '(', 15, 1), Some((5, 24))); - assert_eq!(find_nth_pairs_pos(slice, '(', 15, 2), Some((0, 31))); + check_find_nth_pair_pos( + "((so)((many) good (text))(here))", + vec![ + // cursor on go[o]d + (15, '(', 1, Some((5, 24))), + (15, '(', 2, Some((0, 31))), + ], + ) } #[test] fn test_find_nth_pairs_pos_mixed() { - let doc = Rope::from("(so [many {good} text] here)"); - let slice = doc.slice(..); - - // cursor on go[o]d - assert_eq!(find_nth_pairs_pos(slice, '{', 13, 1), Some((10, 15))); - assert_eq!(find_nth_pairs_pos(slice, '[', 13, 1), Some((4, 21))); - assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((0, 27))); + check_find_nth_pair_pos( + "(so [many {good} text] here)", + vec![ + // cursor on go[o]d + (13, '{', 1, Some((10, 15))), + (13, '[', 1, Some((4, 21))), + (13, '(', 1, Some((0, 27))), + ], + ) } #[test] diff --git a/helix-core/src/textobject.rs b/helix-core/src/textobject.rs index 24f063d4..21ceec04 100644 --- a/helix-core/src/textobject.rs +++ b/helix-core/src/textobject.rs @@ -114,7 +114,7 @@ pub fn textobject_surround( ch: char, count: usize, ) -> Range { - surround::find_nth_pairs_pos(slice, ch, range.head, count) + surround::find_nth_pairs_pos(slice, ch, range, count) .map(|(anchor, head)| match textobject { TextObject::Inside => Range::new(next_grapheme_boundary(slice, anchor), head), TextObject::Around => Range::new(anchor, next_grapheme_boundary(slice, head)), @@ -170,7 +170,7 @@ mod test { #[test] fn test_textobject_word() { - // (text, [(cursor position, textobject, final range), ...]) + // (text, [(char position, textobject, final range), ...]) let tests = &[ ( "cursor at beginning of doc", @@ -269,7 +269,9 @@ mod test { let slice = doc.slice(..); for &case in scenario { let (pos, objtype, expected_range) = case; - let result = textobject_word(slice, Range::point(pos), objtype, 1, false); + // cursor is a single width selection + let range = Range::new(pos, pos + 1); + let result = textobject_word(slice, range, objtype, 1, false); assert_eq!( result, expected_range.into(), @@ -283,7 +285,7 @@ mod test { #[test] fn test_textobject_surround() { - // (text, [(cursor position, textobject, final range, count), ...]) + // (text, [(cursor position, textobject, final range, surround char, count), ...]) let tests = &[ ( "simple (single) surround pairs", -- cgit v1.2.3-70-g09d2 From 30171416cb5b801086da69566a82462fca16ea14 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Fri, 24 Sep 2021 10:29:41 +0900 Subject: Gutter functions --- helix-core/src/diagnostic.rs | 4 +- helix-term/src/ui/editor.rs | 150 ++++++++++++++++++++++++++----------------- helix-view/src/editor.rs | 2 +- 3 files changed, 94 insertions(+), 62 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/diagnostic.rs b/helix-core/src/diagnostic.rs index ad1ba16a..4fcf51c9 100644 --- a/helix-core/src/diagnostic.rs +++ b/helix-core/src/diagnostic.rs @@ -1,7 +1,7 @@ //! LSP diagnostic utility types. /// Describes the severity level of a [`Diagnostic`]. -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum Severity { Error, Warning, @@ -17,7 +17,7 @@ pub struct Range { } /// Corresponds to [`lsp_types::Diagnostic`](https://docs.rs/lsp-types/0.91.0/lsp_types/struct.Diagnostic.html) -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Diagnostic { pub range: Range, pub line: usize, diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 27d33d22..8c4ea9cc 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -17,7 +17,7 @@ use helix_core::{ }; use helix_view::{ document::{Mode, SCRATCH_BUFFER_NAME}, - editor::LineNumber, + editor::{Config, LineNumber}, graphics::{CursorKind, Modifier, Rect, Style}, info::Info, input::KeyEvent, @@ -412,22 +412,6 @@ impl EditorView { let text = doc.text().slice(..); let last_line = view.last_line(doc); - let linenr = theme.get("ui.linenr"); - let linenr_select: Style = theme.try_get("ui.linenr.selected").unwrap_or(linenr); - - let warning = theme.get("warning"); - let error = theme.get("error"); - let info = theme.get("info"); - let hint = theme.get("hint"); - - // Whether to draw the line number for the last line of the - // document or not. We only draw it if it's not an empty line. - let draw_last = text.line_to_byte(last_line) < text.len_bytes(); - - let current_line = doc - .text() - .char_to_line(doc.selection(view.id).primary().cursor(text)); - // it's used inside an iterator so the collect isn't needless: // https://github.com/rust-lang/rust-clippy/issues/6164 #[allow(clippy::needless_collect)] @@ -437,51 +421,99 @@ impl EditorView { .map(|range| range.cursor_line(text)) .collect(); - for (i, line) in (view.offset.row..(last_line + 1)).enumerate() { - use helix_core::diagnostic::Severity; - if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) { - surface.set_stringn( - viewport.x, - viewport.y + i as u16, - "●", - 1, - match diagnostic.severity { - Some(Severity::Error) => error, - Some(Severity::Warning) | None => warning, - Some(Severity::Info) => info, - Some(Severity::Hint) => hint, - }, - ); - } + fn diagnostic( + doc: &Document, + _view: &View, + theme: &Theme, + _config: &Config, + _is_focused: bool, + _width: usize, + ) -> GutterFn { + let warning = theme.get("warning"); + let error = theme.get("error"); + let info = theme.get("info"); + let hint = theme.get("hint"); + let diagnostics = doc.diagnostics().to_vec(); // TODO + + Box::new(move |line: usize, _selected: bool| { + use helix_core::diagnostic::Severity; + if let Some(diagnostic) = diagnostics.iter().find(|d| d.line == line) { + return Some(( + "●".to_string(), + match diagnostic.severity { + Some(Severity::Error) => error, + Some(Severity::Warning) | None => warning, + Some(Severity::Info) => info, + Some(Severity::Hint) => hint, + }, + )); + } + None + }) + } - let selected = cursors.contains(&line); + fn line_number( + doc: &Document, + view: &View, + theme: &Theme, + config: &Config, + is_focused: bool, + width: usize, + ) -> GutterFn { + let text = doc.text().slice(..); + let last_line = view.last_line(doc); + // Whether to draw the line number for the last line of the + // document or not. We only draw it if it's not an empty line. + let draw_last = text.line_to_byte(last_line) < text.len_bytes(); - let text = if line == last_line && !draw_last { - " ~".into() - } else { - let line = match config.line_number { - LineNumber::Absolute => line + 1, - LineNumber::Relative => { - if current_line == line { - line + 1 - } else { - abs_diff(current_line, line) - } - } - }; - format!("{:>5}", line) - }; - surface.set_stringn( - viewport.x + 1, - viewport.y + i as u16, - text, - 5, - if selected && is_focused { - linenr_select + let linenr = theme.get("ui.linenr"); + let linenr_select: Style = theme.try_get("ui.linenr.selected").unwrap_or(linenr); + + let current_line = doc + .text() + .char_to_line(doc.selection(view.id).primary().cursor(text)); + + let config = config.line_number; + + Box::new(move |line: usize, selected: bool| { + if line == last_line && !draw_last { + Some((format!("{:>1$}", '~', width), linenr)) } else { - linenr - }, - ); + let line = match config { + LineNumber::Absolute => line + 1, + LineNumber::Relative => { + if current_line == line { + line + 1 + } else { + abs_diff(current_line, line) + } + } + }; + let style = if selected && is_focused { + linenr_select + } else { + linenr + }; + Some((format!("{:>1$}", line, width), style)) + } + }) + } + + type GutterFn = Box Option<(String, Style)>>; + type Gutter = fn(&Document, &View, &Theme, &Config, bool, usize) -> GutterFn; + let gutters: &[(Gutter, usize)] = &[(diagnostic, 1), (line_number, 5)]; + + let mut offset = 0; + for (constructor, width) in gutters { + let gutter = constructor(doc, view, theme, config, is_focused, *width); + for (i, line) in (view.offset.row..(last_line + 1)).enumerate() { + let selected = cursors.contains(&line); + + if let Some((text, style)) = gutter(line, selected) { + surface.set_stringn(viewport.x + offset, viewport.y + i as u16, text, 5, style); + } + } + offset += *width as u16; } } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 77cea783..d5913a51 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -106,7 +106,7 @@ pub struct Config { pub file_picker: FilePickerConfig, } -#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum LineNumber { /// Show absolute line number -- cgit v1.2.3-70-g09d2 From 3e15aead4adc5139417230d15b38112cdc4f7043 Mon Sep 17 00:00:00 2001 From: George Rodrigues Date: Tue, 30 Nov 2021 21:11:25 -0300 Subject: Fix typo on docs (#1201) --- book/src/guides/adding_languages.md | 2 +- helix-core/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'helix-core/src') diff --git a/book/src/guides/adding_languages.md b/book/src/guides/adding_languages.md index 446eb479..9ad2c285 100644 --- a/book/src/guides/adding_languages.md +++ b/book/src/guides/adding_languages.md @@ -2,7 +2,7 @@ ## Submodules -To add a new langauge, you should first add a tree-sitter submodule. To do this, +To add a new language, you should first add a tree-sitter submodule. To do this, you can run the command ```sh git submodule add -f helix-syntax/languages/tree-sitter- diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index de7e95c1..8ef41ef3 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -158,7 +158,7 @@ mod merge_toml_tests { "; let base: Value = toml::from_slice(include_bytes!("../../languages.toml")) - .expect("Couldn't parse built-in langauges config"); + .expect("Couldn't parse built-in languages config"); let user: Value = toml::from_str(USER).unwrap(); let merged = merge_toml_values(base, user); -- cgit v1.2.3-70-g09d2 From 119dee2980708f75150e39c19f92de029d92dad0 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Thu, 2 Dec 2021 23:49:54 +0900 Subject: fix: Correctly detect empty transactions Fixes #1221 --- helix-core/src/transaction.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'helix-core/src') diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index dfc18fbe..9c07be2c 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -330,7 +330,7 @@ impl ChangeSet { /// `true` when the set is empty. #[inline] pub fn is_empty(&self) -> bool { - self.changes.is_empty() + self.changes.is_empty() || self.changes == [Operation::Retain(self.len)] } /// Map a position through the changes. -- cgit v1.2.3-70-g09d2 From 01f7a312d0bdf53184fb579bf41c619230449cce Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Fri, 3 Dec 2021 10:02:44 +0900 Subject: Address new lint on 1.57 --- helix-core/src/transaction.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index 9c07be2c..b62f4a9b 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -22,7 +22,7 @@ pub enum Assoc { } // ChangeSpec = Change | ChangeSet | Vec -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct ChangeSet { pub(crate) changes: Vec, /// The required document length. Will refuse to apply changes unless it matches. @@ -30,16 +30,6 @@ pub struct ChangeSet { len_after: usize, } -impl Default for ChangeSet { - fn default() -> Self { - Self { - changes: Vec::new(), - len: 0, - len_after: 0, - } - } -} - impl ChangeSet { pub fn with_capacity(capacity: usize) -> Self { Self { -- cgit v1.2.3-70-g09d2 From 70c62530ee0b6fba28816ca4454557baf1f2e440 Mon Sep 17 00:00:00 2001 From: ath3 Date: Fri, 3 Dec 2021 16:13:24 +0100 Subject: Support env flags in shebang (#1224) --- helix-core/src/syntax.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 142265a8..ba78adaa 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -310,8 +310,9 @@ impl Loader { pub fn language_config_for_shebang(&self, source: &Rope) -> Option> { let line = Cow::from(source.line(0)); - static SHEBANG_REGEX: Lazy = - Lazy::new(|| Regex::new(r"^#!\s*(?:\S*[/\\](?:env\s+)?)?([^\s\.\d]+)").unwrap()); + static SHEBANG_REGEX: Lazy = Lazy::new(|| { + Regex::new(r"^#!\s*(?:\S*[/\\](?:env\s+(?:\-\S+\s+)*)?)?([^\s\.\d]+)").unwrap() + }); let configuration_id = SHEBANG_REGEX .captures(&line) .and_then(|cap| self.language_config_ids_by_shebang.get(&cap[1])); -- cgit v1.2.3-70-g09d2 From c1f6167e37909517676c30b0a80203739e8492a5 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Mon, 15 Nov 2021 19:51:10 -0700 Subject: Add support for dates for increment/decrement --- Cargo.lock | 1 + helix-core/Cargo.toml | 2 + helix-core/src/date.rs | 217 +++++++++++++++++++++++++++++++++++++++++++++ helix-core/src/lib.rs | 1 + helix-term/src/commands.rs | 28 ++++-- 5 files changed, 241 insertions(+), 8 deletions(-) create mode 100644 helix-core/src/date.rs (limited to 'helix-core/src') diff --git a/Cargo.lock b/Cargo.lock index 5de6e610..47a6c01e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -369,6 +369,7 @@ name = "helix-core" version = "0.5.0" dependencies = [ "arc-swap", + "chrono", "etcetera", "helix-syntax", "log", diff --git a/helix-core/Cargo.toml b/helix-core/Cargo.toml index ea695d34..0a2a56d9 100644 --- a/helix-core/Cargo.toml +++ b/helix-core/Cargo.toml @@ -36,5 +36,7 @@ similar = "2.1" etcetera = "0.3" +chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] } + [dev-dependencies] quickcheck = { version = "1", default-features = false } diff --git a/helix-core/src/date.rs b/helix-core/src/date.rs new file mode 100644 index 00000000..1332670d --- /dev/null +++ b/helix-core/src/date.rs @@ -0,0 +1,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 { + // 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 = 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() + ); + } + } +} diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 8ef41ef3..b16a716f 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -1,6 +1,7 @@ pub mod auto_pairs; pub mod chars; pub mod comment; +pub mod date; pub mod diagnostic; pub mod diff; pub mod graphemes; diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 99d1432c..639bbd83 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1,5 +1,7 @@ use helix_core::{ - comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, + comment, coords_at_pos, + date::DateIncrementor, + find_first_non_whitespace_char, find_root, graphemes, history::UndoKind, indent, indent::IndentStyle, @@ -5802,13 +5804,23 @@ fn increment_impl(cx: &mut Context, amount: i64) { let text = doc.text(); let changes = selection.ranges().iter().filter_map(|range| { - let incrementor = NumberIncrementor::from_range(text.slice(..), *range)?; - let new_text = incrementor.incremented_text(amount); - Some(( - incrementor.range.from(), - incrementor.range.to(), - Some(new_text), - )) + if let Some(incrementor) = DateIncrementor::from_range(text.slice(..), *range) { + let new_text = incrementor.incremented_text(amount); + Some(( + incrementor.range.from(), + incrementor.range.to(), + Some(new_text), + )) + } else if let Some(incrementor) = NumberIncrementor::from_range(text.slice(..), *range) { + let new_text = incrementor.incremented_text(amount); + Some(( + incrementor.range.from(), + incrementor.range.to(), + Some(new_text), + )) + } else { + None + } }); if changes.clone().count() > 0 { -- cgit v1.2.3-70-g09d2 From 95cfeed2fa60420f584ef1ab051275780f55d4e9 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Thu, 18 Nov 2021 06:23:27 -0700 Subject: Add support for incrementing year and month --- Cargo.lock | 11 +- helix-core/Cargo.toml | 2 +- helix-core/src/date.rs | 332 ++++++++++++++++++++++++++++++++++++------------- 3 files changed, 258 insertions(+), 87 deletions(-) (limited to 'helix-core/src') diff --git a/Cargo.lock b/Cargo.lock index 47a6c01e..ad695995 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -325,6 +325,15 @@ dependencies = [ "regex", ] +[[package]] +name = "gregorian" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3452972f2c995c38dc9b84f09fe14f1ca462a0580642fe6756fed58fdef29050" +dependencies = [ + "libc", +] + [[package]] name = "grep-matcher" version = "0.1.5" @@ -369,8 +378,8 @@ name = "helix-core" version = "0.5.0" dependencies = [ "arc-swap", - "chrono", "etcetera", + "gregorian", "helix-syntax", "log", "once_cell", diff --git a/helix-core/Cargo.toml b/helix-core/Cargo.toml index 0a2a56d9..ffccbb9c 100644 --- a/helix-core/Cargo.toml +++ b/helix-core/Cargo.toml @@ -36,7 +36,7 @@ similar = "2.1" etcetera = "0.3" -chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] } +gregorian = "0.2.1" [dev-dependencies] quickcheck = { version = "1", default-features = false } diff --git a/helix-core/src/date.rs b/helix-core/src/date.rs index 1332670d..e189fe03 100644 --- a/helix-core/src/date.rs +++ b/helix-core/src/date.rs @@ -1,70 +1,124 @@ -use chrono::{Duration, NaiveDate}; +use gregorian::{Date, DateResultExt}; +use regex::Regex; use std::borrow::Cow; use ropey::RopeSlice; -use crate::{ - textobject::{textobject_word, TextObject}, - Range, Tendril, -}; +use crate::{Range, Tendril}; + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +struct Format { + regex: &'static str, + separator: char, +} // Only support formats that aren't region specific. -static FORMATS: &[&str] = &["%Y-%m-%d", "%Y/%m/%d"]; +static FORMATS: &[Format] = &[ + Format { + regex: r"(\d{4})-(\d{2})-(\d{2})", + separator: '-', + }, + Format { + regex: r"(\d{4})/(\d{2})/(\d{2})", + separator: '/', + }, +]; -// 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(Copy, Clone, Debug, PartialEq, Eq)] +enum DateField { + Year, + Month, + Day, +} + #[derive(Debug, PartialEq, Eq)] pub struct DateIncrementor { - pub date: NaiveDate, + pub date: Date, pub range: Range, - pub format: &'static str, + + field: DateField, + format: Format, } impl DateIncrementor { pub fn from_range(text: RopeSlice, range: Range) -> Option { - // Don't increment if the cursor is one right of the date text. - if text.char(range.from()).is_whitespace() { - return None; - } + let from = range.from().saturating_sub(DATE_LENGTH); + let to = (range.from() + DATE_LENGTH).min(text.len_chars()); + let (from_in_text, to_in_text) = (range.from() - from, range.to() - from); + let text: Cow = text.slice(from..to).into(); - let range = textobject_word(text, range, TextObject::Inside, 1, true); - let text: Cow = text.slice(range.from()..range.to()).into(); + FORMATS.iter().find_map(|&format| { + let re = Regex::new(format.regex).ok()?; + let captures = re.captures(&text)?; - let first = text.chars().next()?; - let last = text.chars().next_back()?; + let date = captures.get(0)?; + let offset = range.from() - from_in_text; + let range = Range::new(date.start() + offset, date.end() + offset); - // 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) - }; + let year = captures.get(1)?; + let month = captures.get(2)?; + let day = captures.get(3)?; - if text.len() != DATE_LENGTH { - return None; - } + let year_range = year.range(); + let month_range = month.range(); + let day_range = day.range(); - FORMATS.iter().find_map(|format| { - NaiveDate::parse_from_str(&text, format) - .ok() - .map(|date| DateIncrementor { - date, - range, - format, - }) + let to_inclusive = if to_in_text > from_in_text { + to_in_text - 1 + } else { + to_in_text + }; + let field = if year_range.contains(&from_in_text) && year_range.contains(&to_inclusive) + { + DateField::Year + } else if month_range.contains(&from_in_text) && month_range.contains(&to_inclusive) { + DateField::Month + } else if day_range.contains(&from_in_text) && day_range.contains(&to_inclusive) { + DateField::Day + } else { + return None; + }; + + let year: i16 = year.as_str().parse().ok()?; + let month: u8 = month.as_str().parse().ok()?; + let day: u8 = day.as_str().parse().ok()?; + + let date = Date::new(year, month, day).ok()?; + + Some(DateIncrementor { + date, + field, + 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() + let date = match self.field { + DateField::Year => self + .date + .add_years(amount.try_into().unwrap_or(0)) + .or_next_valid(), + DateField::Month => self + .date + .add_months(amount.try_into().unwrap_or(0)) + .or_prev_valid(), + DateField::Day => self.date.add_days(amount.try_into().unwrap_or(0)), + }; + + format!( + "{:04}{}{:02}{}{:02}", + date.year(), + self.format.separator, + date.month().to_number(), + self.format.separator, + date.day() + ) + .into() } } @@ -74,43 +128,144 @@ mod test { use crate::Rope; #[test] - fn test_date_dashes() { + fn test_create_incrementor_for_year_with_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", - }) - ); + + for head in 0..=3 { + let range = Range::point(head); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: Date::new(2021, 11, 15).unwrap(), + range: Range::new(0, 10), + field: DateField::Year, + format: FORMATS[0], + }) + ); + } + } + + #[test] + fn test_create_incrementor_for_month_with_dashes() { + let rope = Rope::from_str("2021-11-15"); + + for head in 5..=6 { + let range = Range::point(head); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: Date::new(2021, 11, 15).unwrap(), + range: Range::new(0, 10), + field: DateField::Month, + format: FORMATS[0], + }) + ); + } + } + + #[test] + fn test_create_incrementor_for_day_with_dashes() { + let rope = Rope::from_str("2021-11-15"); + + for head in 8..=9 { + let range = Range::point(head); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: Date::new(2021, 11, 15).unwrap(), + range: Range::new(0, 10), + field: DateField::Day, + format: FORMATS[0], + }) + ); + } } #[test] - fn test_date_slashes() { + fn test_try_create_incrementor_on_dashes() { + let rope = Rope::from_str("2021-11-15"); + + for head in &[4, 7] { + let range = Range::point(*head); + assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None,); + } + } + + #[test] + fn test_create_incrementor_for_year_with_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", - }) - ); + + for head in 0..=3 { + let range = Range::point(head); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: Date::new(2021, 11, 15).unwrap(), + range: Range::new(0, 10), + field: DateField::Year, + format: FORMATS[1], + }) + ); + } + } + + #[test] + fn test_create_incrementor_for_month_with_slashes() { + let rope = Rope::from_str("2021/11/15"); + + for head in 5..=6 { + let range = Range::point(head); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: Date::new(2021, 11, 15).unwrap(), + range: Range::new(0, 10), + field: DateField::Month, + format: FORMATS[1], + }) + ); + } + } + + #[test] + fn test_create_incrementor_for_day_with_slashes() { + let rope = Rope::from_str("2021/11/15"); + + for head in 8..=9 { + let range = Range::point(head); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: Date::new(2021, 11, 15).unwrap(), + range: Range::new(0, 10), + field: DateField::Day, + format: FORMATS[1], + }) + ); + } + } + + #[test] + fn test_try_create_incrementor_on_slashes() { + let rope = Rope::from_str("2021/11/15"); + + for head in &[4, 7] { + let range = Range::point(*head); + assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None,); + } } #[test] fn test_date_surrounded_by_spaces() { let rope = Rope::from_str(" 2021-11-15 "); - let range = Range::point(10); + let range = Range::point(3); assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), + date: Date::new(2021, 11, 15).unwrap(), range: Range::new(3, 13), - format: "%Y-%m-%d", + field: DateField::Year, + format: FORMATS[0], }) ); } @@ -122,23 +277,25 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), + date: Date::new(2021, 11, 15).unwrap(), range: Range::new(8, 18), - format: "%Y-%m-%d", + field: DateField::Year, + format: FORMATS[0], }) ); } #[test] fn test_date_in_double_quotes() { - let rope = Rope::from_str("date = \"2021-11-15\""); - let range = Range::point(10); + let rope = Rope::from_str("let date = \"2021-11-15\";"); + let range = Range::point(12); 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", + date: Date::new(2021, 11, 15).unwrap(), + range: Range::new(12, 22), + field: DateField::Year, + format: FORMATS[0], }) ); } @@ -189,23 +346,28 @@ mod test { #[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"), + // (original, cursor, amount, expected) + ("2020-02-28", 0, 1, "2021-02-28"), + ("2020-02-29", 0, 1, "2021-03-01"), + ("2020-01-31", 5, 1, "2020-02-29"), + ("2020-01-20", 5, 1, "2020-02-20"), + ("2020-02-28", 8, 1, "2020-02-29"), + ("2021-02-28", 8, 1, "2021-03-01"), + ("2021-02-28", 0, -1, "2020-02-28"), + ("2021-03-01", 0, -1, "2020-03-01"), + ("2020-02-29", 5, -1, "2020-01-29"), + ("2020-02-20", 5, -1, "2020-01-20"), + ("2020-02-29", 8, -1, "2020-02-28"), + ("2021-03-01", 8, -1, "2021-02-28"), + ("1980/12/21", 8, 100, "1981/03/31"), + ("1980/12/21", 8, -100, "1980/09/12"), + ("1980/12/21", 8, 1000, "1983/09/17"), + ("1980/12/21", 8, -1000, "1978/03/27"), ]; - for (original, amount, expected) in tests { + for (original, cursor, amount, expected) in tests { let rope = Rope::from_str(original); - let range = Range::point(0); + let range = Range::point(cursor); assert_eq!( DateIncrementor::from_range(rope.slice(..), range) .unwrap() -- cgit v1.2.3-70-g09d2 From 64afd546544b112466e35ef52c492aa604ef7f39 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Thu, 18 Nov 2021 12:27:01 -0700 Subject: Cleanup --- helix-core/src/date.rs | 127 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 85 insertions(+), 42 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/date.rs b/helix-core/src/date.rs index e189fe03..e7205c94 100644 --- a/helix-core/src/date.rs +++ b/helix-core/src/date.rs @@ -45,8 +45,21 @@ pub struct DateIncrementor { impl DateIncrementor { pub fn from_range(text: RopeSlice, range: Range) -> Option { + let range = if range.is_empty() { + if range.anchor < text.len_bytes() { + // Treat empty range as a cursor range. + range.put_cursor(text, range.anchor + 1, true) + } else { + // The range is empty and at the end of the text. + return None; + } + } else { + range + }; + let from = range.from().saturating_sub(DATE_LENGTH); let to = (range.from() + DATE_LENGTH).min(text.len_chars()); + let (from_in_text, to_in_text) = (range.from() - from, range.to() - from); let text: Cow = text.slice(from..to).into(); @@ -58,35 +71,28 @@ impl DateIncrementor { let offset = range.from() - from_in_text; let range = Range::new(date.start() + offset, date.end() + offset); - let year = captures.get(1)?; - let month = captures.get(2)?; - let day = captures.get(3)?; - - let year_range = year.range(); - let month_range = month.range(); - let day_range = day.range(); + let (year, month, day) = (captures.get(1)?, captures.get(2)?, captures.get(3)?); + let (year_range, month_range, day_range) = (year.range(), month.range(), day.range()); - let to_inclusive = if to_in_text > from_in_text { - to_in_text - 1 - } else { - to_in_text - }; - let field = if year_range.contains(&from_in_text) && year_range.contains(&to_inclusive) + let field = if year_range.contains(&from_in_text) + && year_range.contains(&(to_in_text - 1)) { DateField::Year - } else if month_range.contains(&from_in_text) && month_range.contains(&to_inclusive) { + } else if month_range.contains(&from_in_text) && month_range.contains(&(to_in_text - 1)) + { DateField::Month - } else if day_range.contains(&from_in_text) && day_range.contains(&to_inclusive) { + } else if day_range.contains(&from_in_text) && day_range.contains(&(to_in_text - 1)) { DateField::Day } else { return None; }; - let year: i16 = year.as_str().parse().ok()?; - let month: u8 = month.as_str().parse().ok()?; - let day: u8 = day.as_str().parse().ok()?; - - let date = Date::new(year, month, day).ok()?; + let date = Date::new( + year.as_str().parse::().ok()?, + month.as_str().parse::().ok()?, + day.as_str().parse::().ok()?, + ) + .ok()?; Some(DateIncrementor { date, @@ -131,8 +137,8 @@ mod test { fn test_create_incrementor_for_year_with_dashes() { let rope = Rope::from_str("2021-11-15"); - for head in 0..=3 { - let range = Range::point(head); + for cursor in 0..=3 { + let range = Range::new(cursor, cursor + 1); assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { @@ -149,8 +155,8 @@ mod test { fn test_create_incrementor_for_month_with_dashes() { let rope = Rope::from_str("2021-11-15"); - for head in 5..=6 { - let range = Range::point(head); + for cursor in 5..=6 { + let range = Range::new(cursor, cursor + 1); assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { @@ -167,8 +173,8 @@ mod test { fn test_create_incrementor_for_day_with_dashes() { let rope = Rope::from_str("2021-11-15"); - for head in 8..=9 { - let range = Range::point(head); + for cursor in 8..=9 { + let range = Range::new(cursor, cursor + 1); assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { @@ -185,8 +191,8 @@ mod test { fn test_try_create_incrementor_on_dashes() { let rope = Rope::from_str("2021-11-15"); - for head in &[4, 7] { - let range = Range::point(*head); + for &cursor in &[4, 7] { + let range = Range::new(cursor, cursor + 1); assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None,); } } @@ -195,8 +201,8 @@ mod test { fn test_create_incrementor_for_year_with_slashes() { let rope = Rope::from_str("2021/11/15"); - for head in 0..=3 { - let range = Range::point(head); + for cursor in 0..=3 { + let range = Range::new(cursor, cursor + 1); assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { @@ -213,8 +219,8 @@ mod test { fn test_create_incrementor_for_month_with_slashes() { let rope = Rope::from_str("2021/11/15"); - for head in 5..=6 { - let range = Range::point(head); + for cursor in 5..=6 { + let range = Range::new(cursor, cursor + 1); assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { @@ -231,8 +237,8 @@ mod test { fn test_create_incrementor_for_day_with_slashes() { let rope = Rope::from_str("2021/11/15"); - for head in 8..=9 { - let range = Range::point(head); + for cursor in 8..=9 { + let range = Range::new(cursor, cursor + 1); assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { @@ -249,8 +255,8 @@ mod test { fn test_try_create_incrementor_on_slashes() { let rope = Rope::from_str("2021/11/15"); - for head in &[4, 7] { - let range = Range::point(*head); + for &cursor in &[4, 7] { + let range = Range::new(cursor, cursor + 1); assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None,); } } @@ -258,7 +264,7 @@ mod test { #[test] fn test_date_surrounded_by_spaces() { let rope = Rope::from_str(" 2021-11-15 "); - let range = Range::point(3); + let range = Range::new(3, 4); assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { @@ -273,7 +279,7 @@ mod test { #[test] fn test_date_in_single_quotes() { let rope = Rope::from_str("date = '2021-11-15'"); - let range = Range::point(10); + let range = Range::new(10, 11); assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { @@ -288,7 +294,7 @@ mod test { #[test] fn test_date_in_double_quotes() { let rope = Rope::from_str("let date = \"2021-11-15\";"); - let range = Range::point(12); + let range = Range::new(12, 13); assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { @@ -303,14 +309,51 @@ mod test { #[test] fn test_date_cursor_one_right_of_date() { let rope = Rope::from_str("2021-11-15 "); - let range = Range::point(10); + let range = Range::new(10, 11); 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::new(0, 1); + assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None); + } + + #[test] + fn test_date_empty_range_at_beginning() { + let rope = Rope::from_str("2021-11-15"); let range = Range::point(0); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: Date::new(2021, 11, 15).unwrap(), + range: Range::new(0, 10), + field: DateField::Year, + format: FORMATS[0], + }) + ); + } + + #[test] + fn test_date_empty_range_at_in_middle() { + let rope = Rope::from_str("2021-11-15"); + let range = Range::point(5); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: Date::new(2021, 11, 15).unwrap(), + range: Range::new(0, 10), + field: DateField::Month, + format: FORMATS[0], + }) + ); + } + + #[test] + fn test_date_empty_range_at_end() { + let rope = Rope::from_str("2021-11-15"); + let range = Range::point(10); assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None); } @@ -337,7 +380,7 @@ mod test { for invalid in tests { let rope = Rope::from_str(invalid); - let range = Range::point(0); + let range = Range::new(0, 1); assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None); } @@ -367,7 +410,7 @@ mod test { for (original, cursor, amount, expected) in tests { let rope = Rope::from_str(original); - let range = Range::point(cursor); + let range = Range::new(cursor, cursor + 1); assert_eq!( DateIncrementor::from_range(rope.slice(..), range) .unwrap() -- cgit v1.2.3-70-g09d2 From 2a0c685a7857a94db2ac61b586c92425da273ea7 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Sun, 21 Nov 2021 09:36:03 -0700 Subject: Remove dependency on gregorian crate --- Cargo.lock | 8 +--- helix-core/Cargo.toml | 2 +- helix-core/src/date.rs | 103 +++++++++++++++++++++++++++++++++++-------------- 3 files changed, 75 insertions(+), 38 deletions(-) (limited to 'helix-core/src') diff --git a/Cargo.lock b/Cargo.lock index 0f4db606..47a6c01e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -325,12 +325,6 @@ dependencies = [ "regex", ] -[[package]] -name = "gregorian" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3452972f2c995c38dc9b84f09fe14f1ca462a0580642fe6756fed58fdef29050" - [[package]] name = "grep-matcher" version = "0.1.5" @@ -375,8 +369,8 @@ name = "helix-core" version = "0.5.0" dependencies = [ "arc-swap", + "chrono", "etcetera", - "gregorian", "helix-syntax", "log", "once_cell", diff --git a/helix-core/Cargo.toml b/helix-core/Cargo.toml index a58c7911..0a2a56d9 100644 --- a/helix-core/Cargo.toml +++ b/helix-core/Cargo.toml @@ -36,7 +36,7 @@ similar = "2.1" etcetera = "0.3" -gregorian = { version = "0.2.1", default-features = false } +chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] } [dev-dependencies] quickcheck = { version = "1", default-features = false } diff --git a/helix-core/src/date.rs b/helix-core/src/date.rs index e7205c94..c447ef70 100644 --- a/helix-core/src/date.rs +++ b/helix-core/src/date.rs @@ -1,12 +1,61 @@ -use gregorian::{Date, DateResultExt}; use regex::Regex; use std::borrow::Cow; +use std::cmp; use ropey::RopeSlice; use crate::{Range, Tendril}; +use chrono::{Datelike, Duration, NaiveDate}; + +fn ndays_in_month(year: i32, month: u32) -> u32 { + // The first day of the next month... + let (y, m) = if month == 12 { + (year + 1, 1) + } else { + (year, month + 1) + }; + let d = NaiveDate::from_ymd(y, m, 1); + + // ...is preceded by the last day of the original month. + d.pred().day() +} + +fn add_days(date: NaiveDate, amount: i64) -> Option { + date.checked_add_signed(Duration::days(amount)) +} + +fn add_months(date: NaiveDate, amount: i64) -> Option { + let month = date.month0() as i64 + amount; + let year = date.year() + i32::try_from(month / 12).ok()?; + + // Normalize month + let month = month % 12; + let month = if month.is_negative() { + month + 13 + } else { + month + 1 + } as u32; + + let day = cmp::min(date.day(), ndays_in_month(year, month)); + + Some(NaiveDate::from_ymd(year, month, day)) +} + +fn add_years(date: NaiveDate, amount: i64) -> Option { + let year = i32::try_from(date.year() as i64 + amount).ok()?; + + let ndays = ndays_in_month(year, date.month()); + + if date.day() > ndays { + let d = NaiveDate::from_ymd(year, date.month(), ndays); + Some(d.succ()) + } else { + date.with_year(year) + } +} + #[derive(Copy, Clone, Debug, PartialEq, Eq)] struct Format { regex: &'static str, @@ -36,7 +85,7 @@ enum DateField { #[derive(Debug, PartialEq, Eq)] pub struct DateIncrementor { - pub date: Date, + pub date: NaiveDate, pub range: Range, field: DateField, @@ -87,12 +136,11 @@ impl DateIncrementor { return None; }; - let date = Date::new( - year.as_str().parse::().ok()?, - month.as_str().parse::().ok()?, - day.as_str().parse::().ok()?, - ) - .ok()?; + let date = NaiveDate::from_ymd_opt( + year.as_str().parse::().ok()?, + month.as_str().parse::().ok()?, + day.as_str().parse::().ok()?, + )?; Some(DateIncrementor { date, @@ -105,22 +153,17 @@ impl DateIncrementor { pub fn incremented_text(&self, amount: i64) -> Tendril { let date = match self.field { - DateField::Year => self - .date - .add_years(amount.try_into().unwrap_or(0)) - .or_next_valid(), - DateField::Month => self - .date - .add_months(amount.try_into().unwrap_or(0)) - .or_prev_valid(), - DateField::Day => self.date.add_days(amount.try_into().unwrap_or(0)), - }; + DateField::Year => add_years(self.date, amount), + DateField::Month => add_months(self.date, amount), + DateField::Day => add_days(self.date, amount), + } + .unwrap_or(self.date); format!( "{:04}{}{:02}{}{:02}", date.year(), self.format.separator, - date.month().to_number(), + date.month(), self.format.separator, date.day() ) @@ -142,7 +185,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(0, 10), field: DateField::Year, format: FORMATS[0], @@ -160,7 +203,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(0, 10), field: DateField::Month, format: FORMATS[0], @@ -178,7 +221,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(0, 10), field: DateField::Day, format: FORMATS[0], @@ -206,7 +249,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(0, 10), field: DateField::Year, format: FORMATS[1], @@ -224,7 +267,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(0, 10), field: DateField::Month, format: FORMATS[1], @@ -242,7 +285,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(0, 10), field: DateField::Day, format: FORMATS[1], @@ -268,7 +311,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(3, 13), field: DateField::Year, format: FORMATS[0], @@ -283,7 +326,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(8, 18), field: DateField::Year, format: FORMATS[0], @@ -298,7 +341,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(12, 22), field: DateField::Year, format: FORMATS[0], @@ -327,7 +370,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(0, 10), field: DateField::Year, format: FORMATS[0], @@ -342,7 +385,7 @@ mod test { assert_eq!( DateIncrementor::from_range(rope.slice(..), range), Some(DateIncrementor { - date: Date::new(2021, 11, 15).unwrap(), + date: NaiveDate::from_ymd(2021, 11, 15), range: Range::new(0, 10), field: DateField::Month, format: FORMATS[0], -- cgit v1.2.3-70-g09d2 From c9641fccedc51737a74ed47009279fa688462ea9 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Sun, 21 Nov 2021 10:38:41 -0700 Subject: Add `Increment` trait --- helix-core/src/date.rs | 465 ---------------------------------- helix-core/src/increment/date.rs | 474 ++++++++++++++++++++++++++++++++++ helix-core/src/increment/mod.rs | 8 + helix-core/src/increment/number.rs | 507 +++++++++++++++++++++++++++++++++++++ helix-core/src/lib.rs | 3 +- helix-core/src/numbers.rs | 499 ------------------------------------ helix-term/src/commands.rs | 30 +-- 7 files changed, 1002 insertions(+), 984 deletions(-) delete mode 100644 helix-core/src/date.rs create mode 100644 helix-core/src/increment/date.rs create mode 100644 helix-core/src/increment/mod.rs create mode 100644 helix-core/src/increment/number.rs delete mode 100644 helix-core/src/numbers.rs (limited to 'helix-core/src') diff --git a/helix-core/src/date.rs b/helix-core/src/date.rs deleted file mode 100644 index c447ef70..00000000 --- a/helix-core/src/date.rs +++ /dev/null @@ -1,465 +0,0 @@ -use regex::Regex; - -use std::borrow::Cow; -use std::cmp; - -use ropey::RopeSlice; - -use crate::{Range, Tendril}; - -use chrono::{Datelike, Duration, NaiveDate}; - -fn ndays_in_month(year: i32, month: u32) -> u32 { - // The first day of the next month... - let (y, m) = if month == 12 { - (year + 1, 1) - } else { - (year, month + 1) - }; - let d = NaiveDate::from_ymd(y, m, 1); - - // ...is preceded by the last day of the original month. - d.pred().day() -} - -fn add_days(date: NaiveDate, amount: i64) -> Option { - date.checked_add_signed(Duration::days(amount)) -} - -fn add_months(date: NaiveDate, amount: i64) -> Option { - let month = date.month0() as i64 + amount; - let year = date.year() + i32::try_from(month / 12).ok()?; - - // Normalize month - let month = month % 12; - let month = if month.is_negative() { - month + 13 - } else { - month + 1 - } as u32; - - let day = cmp::min(date.day(), ndays_in_month(year, month)); - - Some(NaiveDate::from_ymd(year, month, day)) -} - -fn add_years(date: NaiveDate, amount: i64) -> Option { - let year = i32::try_from(date.year() as i64 + amount).ok()?; - - let ndays = ndays_in_month(year, date.month()); - - if date.day() > ndays { - let d = NaiveDate::from_ymd(year, date.month(), ndays); - Some(d.succ()) - } else { - date.with_year(year) - } -} - -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -struct Format { - regex: &'static str, - separator: char, -} - -// Only support formats that aren't region specific. -static FORMATS: &[Format] = &[ - Format { - regex: r"(\d{4})-(\d{2})-(\d{2})", - separator: '-', - }, - Format { - regex: r"(\d{4})/(\d{2})/(\d{2})", - separator: '/', - }, -]; - -const DATE_LENGTH: usize = 10; - -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -enum DateField { - Year, - Month, - Day, -} - -#[derive(Debug, PartialEq, Eq)] -pub struct DateIncrementor { - pub date: NaiveDate, - pub range: Range, - - field: DateField, - format: Format, -} - -impl DateIncrementor { - pub fn from_range(text: RopeSlice, range: Range) -> Option { - let range = if range.is_empty() { - if range.anchor < text.len_bytes() { - // Treat empty range as a cursor range. - range.put_cursor(text, range.anchor + 1, true) - } else { - // The range is empty and at the end of the text. - return None; - } - } else { - range - }; - - let from = range.from().saturating_sub(DATE_LENGTH); - let to = (range.from() + DATE_LENGTH).min(text.len_chars()); - - let (from_in_text, to_in_text) = (range.from() - from, range.to() - from); - let text: Cow = text.slice(from..to).into(); - - FORMATS.iter().find_map(|&format| { - let re = Regex::new(format.regex).ok()?; - let captures = re.captures(&text)?; - - let date = captures.get(0)?; - let offset = range.from() - from_in_text; - let range = Range::new(date.start() + offset, date.end() + offset); - - let (year, month, day) = (captures.get(1)?, captures.get(2)?, captures.get(3)?); - let (year_range, month_range, day_range) = (year.range(), month.range(), day.range()); - - let field = if year_range.contains(&from_in_text) - && year_range.contains(&(to_in_text - 1)) - { - DateField::Year - } else if month_range.contains(&from_in_text) && month_range.contains(&(to_in_text - 1)) - { - DateField::Month - } else if day_range.contains(&from_in_text) && day_range.contains(&(to_in_text - 1)) { - DateField::Day - } else { - return None; - }; - - let date = NaiveDate::from_ymd_opt( - year.as_str().parse::().ok()?, - month.as_str().parse::().ok()?, - day.as_str().parse::().ok()?, - )?; - - Some(DateIncrementor { - date, - field, - range, - format, - }) - }) - } - - pub fn incremented_text(&self, amount: i64) -> Tendril { - let date = match self.field { - DateField::Year => add_years(self.date, amount), - DateField::Month => add_months(self.date, amount), - DateField::Day => add_days(self.date, amount), - } - .unwrap_or(self.date); - - format!( - "{:04}{}{:02}{}{:02}", - date.year(), - self.format.separator, - date.month(), - self.format.separator, - date.day() - ) - .into() - } -} - -#[cfg(test)] -mod test { - use super::*; - use crate::Rope; - - #[test] - fn test_create_incrementor_for_year_with_dashes() { - let rope = Rope::from_str("2021-11-15"); - - for cursor in 0..=3 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Year, - format: FORMATS[0], - }) - ); - } - } - - #[test] - fn test_create_incrementor_for_month_with_dashes() { - let rope = Rope::from_str("2021-11-15"); - - for cursor in 5..=6 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Month, - format: FORMATS[0], - }) - ); - } - } - - #[test] - fn test_create_incrementor_for_day_with_dashes() { - let rope = Rope::from_str("2021-11-15"); - - for cursor in 8..=9 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Day, - format: FORMATS[0], - }) - ); - } - } - - #[test] - fn test_try_create_incrementor_on_dashes() { - let rope = Rope::from_str("2021-11-15"); - - for &cursor in &[4, 7] { - let range = Range::new(cursor, cursor + 1); - assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None,); - } - } - - #[test] - fn test_create_incrementor_for_year_with_slashes() { - let rope = Rope::from_str("2021/11/15"); - - for cursor in 0..=3 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Year, - format: FORMATS[1], - }) - ); - } - } - - #[test] - fn test_create_incrementor_for_month_with_slashes() { - let rope = Rope::from_str("2021/11/15"); - - for cursor in 5..=6 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Month, - format: FORMATS[1], - }) - ); - } - } - - #[test] - fn test_create_incrementor_for_day_with_slashes() { - let rope = Rope::from_str("2021/11/15"); - - for cursor in 8..=9 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Day, - format: FORMATS[1], - }) - ); - } - } - - #[test] - fn test_try_create_incrementor_on_slashes() { - let rope = Rope::from_str("2021/11/15"); - - for &cursor in &[4, 7] { - let range = Range::new(cursor, cursor + 1); - assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None,); - } - } - - #[test] - fn test_date_surrounded_by_spaces() { - let rope = Rope::from_str(" 2021-11-15 "); - let range = Range::new(3, 4); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(3, 13), - field: DateField::Year, - format: FORMATS[0], - }) - ); - } - - #[test] - fn test_date_in_single_quotes() { - let rope = Rope::from_str("date = '2021-11-15'"); - let range = Range::new(10, 11); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(8, 18), - field: DateField::Year, - format: FORMATS[0], - }) - ); - } - - #[test] - fn test_date_in_double_quotes() { - let rope = Rope::from_str("let date = \"2021-11-15\";"); - let range = Range::new(12, 13); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(12, 22), - field: DateField::Year, - format: FORMATS[0], - }) - ); - } - - #[test] - fn test_date_cursor_one_right_of_date() { - let rope = Rope::from_str("2021-11-15 "); - let range = Range::new(10, 11); - 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::new(0, 1); - assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None); - } - - #[test] - fn test_date_empty_range_at_beginning() { - 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), - field: DateField::Year, - format: FORMATS[0], - }) - ); - } - - #[test] - fn test_date_empty_range_at_in_middle() { - let rope = Rope::from_str("2021-11-15"); - let range = Range::point(5); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Month, - format: FORMATS[0], - }) - ); - } - - #[test] - fn test_date_empty_range_at_end() { - 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_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::new(0, 1); - - assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None); - } - } - - #[test] - fn test_increment_dates() { - let tests = [ - // (original, cursor, amount, expected) - ("2020-02-28", 0, 1, "2021-02-28"), - ("2020-02-29", 0, 1, "2021-03-01"), - ("2020-01-31", 5, 1, "2020-02-29"), - ("2020-01-20", 5, 1, "2020-02-20"), - ("2020-02-28", 8, 1, "2020-02-29"), - ("2021-02-28", 8, 1, "2021-03-01"), - ("2021-02-28", 0, -1, "2020-02-28"), - ("2021-03-01", 0, -1, "2020-03-01"), - ("2020-02-29", 5, -1, "2020-01-29"), - ("2020-02-20", 5, -1, "2020-01-20"), - ("2020-02-29", 8, -1, "2020-02-28"), - ("2021-03-01", 8, -1, "2021-02-28"), - ("1980/12/21", 8, 100, "1981/03/31"), - ("1980/12/21", 8, -100, "1980/09/12"), - ("1980/12/21", 8, 1000, "1983/09/17"), - ("1980/12/21", 8, -1000, "1978/03/27"), - ]; - - for (original, cursor, amount, expected) in tests { - let rope = Rope::from_str(original); - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range) - .unwrap() - .incremented_text(amount), - expected.into() - ); - } - } -} diff --git a/helix-core/src/increment/date.rs b/helix-core/src/increment/date.rs new file mode 100644 index 00000000..05442990 --- /dev/null +++ b/helix-core/src/increment/date.rs @@ -0,0 +1,474 @@ +use regex::Regex; + +use std::borrow::Cow; +use std::cmp; + +use ropey::RopeSlice; + +use crate::{Range, Tendril}; + +use chrono::{Datelike, Duration, NaiveDate}; + +use super::Increment; + +fn ndays_in_month(year: i32, month: u32) -> u32 { + // The first day of the next month... + let (y, m) = if month == 12 { + (year + 1, 1) + } else { + (year, month + 1) + }; + let d = NaiveDate::from_ymd(y, m, 1); + + // ...is preceded by the last day of the original month. + d.pred().day() +} + +fn add_days(date: NaiveDate, amount: i64) -> Option { + date.checked_add_signed(Duration::days(amount)) +} + +fn add_months(date: NaiveDate, amount: i64) -> Option { + let month = date.month0() as i64 + amount; + let year = date.year() + i32::try_from(month / 12).ok()?; + let year = if month.is_negative() { year - 1 } else { year }; + + // Normalize month + let month = month % 12; + let month = if month.is_negative() { + month + 13 + } else { + month + 1 + } as u32; + + let day = cmp::min(date.day(), ndays_in_month(year, month)); + + Some(NaiveDate::from_ymd(year, month, day)) +} + +fn add_years(date: NaiveDate, amount: i64) -> Option { + let year = i32::try_from(date.year() as i64 + amount).ok()?; + let ndays = ndays_in_month(year, date.month()); + + if date.day() > ndays { + let d = NaiveDate::from_ymd(year, date.month(), ndays); + Some(d.succ()) + } else { + date.with_year(year) + } +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +struct Format { + regex: &'static str, + separator: char, +} + +// Only support formats that aren't region specific. +static FORMATS: &[Format] = &[ + Format { + regex: r"(\d{4})-(\d{2})-(\d{2})", + separator: '-', + }, + Format { + regex: r"(\d{4})/(\d{2})/(\d{2})", + separator: '/', + }, +]; + +const DATE_LENGTH: usize = 10; + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +enum DateField { + Year, + Month, + Day, +} + +#[derive(Debug, PartialEq, Eq)] +pub struct DateIncrementor { + date: NaiveDate, + range: Range, + field: DateField, + format: Format, +} + +impl DateIncrementor { + pub fn from_range(text: RopeSlice, range: Range) -> Option { + let range = if range.is_empty() { + if range.anchor < text.len_bytes() { + // Treat empty range as a cursor range. + range.put_cursor(text, range.anchor + 1, true) + } else { + // The range is empty and at the end of the text. + return None; + } + } else { + range + }; + + let from = range.from().saturating_sub(DATE_LENGTH); + let to = (range.from() + DATE_LENGTH).min(text.len_chars()); + + let (from_in_text, to_in_text) = (range.from() - from, range.to() - from); + let text: Cow = text.slice(from..to).into(); + + FORMATS.iter().find_map(|&format| { + let re = Regex::new(format.regex).ok()?; + let captures = re.captures(&text)?; + + let date = captures.get(0)?; + let offset = range.from() - from_in_text; + let range = Range::new(date.start() + offset, date.end() + offset); + + let (year, month, day) = (captures.get(1)?, captures.get(2)?, captures.get(3)?); + let (year_range, month_range, day_range) = (year.range(), month.range(), day.range()); + + let field = if year_range.contains(&from_in_text) + && year_range.contains(&(to_in_text - 1)) + { + DateField::Year + } else if month_range.contains(&from_in_text) && month_range.contains(&(to_in_text - 1)) + { + DateField::Month + } else if day_range.contains(&from_in_text) && day_range.contains(&(to_in_text - 1)) { + DateField::Day + } else { + return None; + }; + + let date = NaiveDate::from_ymd_opt( + year.as_str().parse::().ok()?, + month.as_str().parse::().ok()?, + day.as_str().parse::().ok()?, + )?; + + Some(DateIncrementor { + date, + field, + range, + format, + }) + }) + } +} + +impl Increment for DateIncrementor { + fn increment(&self, amount: i64) -> (Range, Tendril) { + let date = match self.field { + DateField::Year => add_years(self.date, amount), + DateField::Month => add_months(self.date, amount), + DateField::Day => add_days(self.date, amount), + } + .unwrap_or(self.date); + + ( + self.range, + format!( + "{:04}{}{:02}{}{:02}", + date.year(), + self.format.separator, + date.month(), + self.format.separator, + date.day() + ) + .into(), + ) + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::Rope; + + #[test] + fn test_create_incrementor_for_year_with_dashes() { + let rope = Rope::from_str("2021-11-15"); + + for cursor in 0..=3 { + let range = Range::new(cursor, cursor + 1); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: NaiveDate::from_ymd(2021, 11, 15), + range: Range::new(0, 10), + field: DateField::Year, + format: FORMATS[0], + }) + ); + } + } + + #[test] + fn test_create_incrementor_for_month_with_dashes() { + let rope = Rope::from_str("2021-11-15"); + + for cursor in 5..=6 { + let range = Range::new(cursor, cursor + 1); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: NaiveDate::from_ymd(2021, 11, 15), + range: Range::new(0, 10), + field: DateField::Month, + format: FORMATS[0], + }) + ); + } + } + + #[test] + fn test_create_incrementor_for_day_with_dashes() { + let rope = Rope::from_str("2021-11-15"); + + for cursor in 8..=9 { + let range = Range::new(cursor, cursor + 1); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: NaiveDate::from_ymd(2021, 11, 15), + range: Range::new(0, 10), + field: DateField::Day, + format: FORMATS[0], + }) + ); + } + } + + #[test] + fn test_try_create_incrementor_on_dashes() { + let rope = Rope::from_str("2021-11-15"); + + for &cursor in &[4, 7] { + let range = Range::new(cursor, cursor + 1); + assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None,); + } + } + + #[test] + fn test_create_incrementor_for_year_with_slashes() { + let rope = Rope::from_str("2021/11/15"); + + for cursor in 0..=3 { + let range = Range::new(cursor, cursor + 1); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: NaiveDate::from_ymd(2021, 11, 15), + range: Range::new(0, 10), + field: DateField::Year, + format: FORMATS[1], + }) + ); + } + } + + #[test] + fn test_create_incrementor_for_month_with_slashes() { + let rope = Rope::from_str("2021/11/15"); + + for cursor in 5..=6 { + let range = Range::new(cursor, cursor + 1); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: NaiveDate::from_ymd(2021, 11, 15), + range: Range::new(0, 10), + field: DateField::Month, + format: FORMATS[1], + }) + ); + } + } + + #[test] + fn test_create_incrementor_for_day_with_slashes() { + let rope = Rope::from_str("2021/11/15"); + + for cursor in 8..=9 { + let range = Range::new(cursor, cursor + 1); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: NaiveDate::from_ymd(2021, 11, 15), + range: Range::new(0, 10), + field: DateField::Day, + format: FORMATS[1], + }) + ); + } + } + + #[test] + fn test_try_create_incrementor_on_slashes() { + let rope = Rope::from_str("2021/11/15"); + + for &cursor in &[4, 7] { + let range = Range::new(cursor, cursor + 1); + assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None,); + } + } + + #[test] + fn test_date_surrounded_by_spaces() { + let rope = Rope::from_str(" 2021-11-15 "); + let range = Range::new(3, 4); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: NaiveDate::from_ymd(2021, 11, 15), + range: Range::new(3, 13), + field: DateField::Year, + format: FORMATS[0], + }) + ); + } + + #[test] + fn test_date_in_single_quotes() { + let rope = Rope::from_str("date = '2021-11-15'"); + let range = Range::new(10, 11); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: NaiveDate::from_ymd(2021, 11, 15), + range: Range::new(8, 18), + field: DateField::Year, + format: FORMATS[0], + }) + ); + } + + #[test] + fn test_date_in_double_quotes() { + let rope = Rope::from_str("let date = \"2021-11-15\";"); + let range = Range::new(12, 13); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: NaiveDate::from_ymd(2021, 11, 15), + range: Range::new(12, 22), + field: DateField::Year, + format: FORMATS[0], + }) + ); + } + + #[test] + fn test_date_cursor_one_right_of_date() { + let rope = Rope::from_str("2021-11-15 "); + let range = Range::new(10, 11); + 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::new(0, 1); + assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None); + } + + #[test] + fn test_date_empty_range_at_beginning() { + 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), + field: DateField::Year, + format: FORMATS[0], + }) + ); + } + + #[test] + fn test_date_empty_range_at_in_middle() { + let rope = Rope::from_str("2021-11-15"); + let range = Range::point(5); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range), + Some(DateIncrementor { + date: NaiveDate::from_ymd(2021, 11, 15), + range: Range::new(0, 10), + field: DateField::Month, + format: FORMATS[0], + }) + ); + } + + #[test] + fn test_date_empty_range_at_end() { + 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_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::new(0, 1); + + assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None); + } + } + + #[test] + fn test_increment_dates() { + let tests = [ + // (original, cursor, amount, expected) + ("2020-02-28", 0, 1, "2021-02-28"), + ("2020-02-29", 0, 1, "2021-03-01"), + ("2020-01-31", 5, 1, "2020-02-29"), + ("2020-01-20", 5, 1, "2020-02-20"), + ("2021-01-01", 5, -1, "2020-12-01"), + ("2021-01-31", 5, -2, "2020-11-30"), + ("2020-02-28", 8, 1, "2020-02-29"), + ("2021-02-28", 8, 1, "2021-03-01"), + ("2021-02-28", 0, -1, "2020-02-28"), + ("2021-03-01", 0, -1, "2020-03-01"), + ("2020-02-29", 5, -1, "2020-01-29"), + ("2020-02-20", 5, -1, "2020-01-20"), + ("2020-02-29", 8, -1, "2020-02-28"), + ("2021-03-01", 8, -1, "2021-02-28"), + ("1980/12/21", 8, 100, "1981/03/31"), + ("1980/12/21", 8, -100, "1980/09/12"), + ("1980/12/21", 8, 1000, "1983/09/17"), + ("1980/12/21", 8, -1000, "1978/03/27"), + ]; + + for (original, cursor, amount, expected) in tests { + let rope = Rope::from_str(original); + let range = Range::new(cursor, cursor + 1); + assert_eq!( + DateIncrementor::from_range(rope.slice(..), range) + .unwrap() + .increment(amount) + .1, + expected.into() + ); + } + } +} diff --git a/helix-core/src/increment/mod.rs b/helix-core/src/increment/mod.rs new file mode 100644 index 00000000..71a1f183 --- /dev/null +++ b/helix-core/src/increment/mod.rs @@ -0,0 +1,8 @@ +pub mod date; +pub mod number; + +use crate::{Range, Tendril}; + +pub trait Increment { + fn increment(&self, amount: i64) -> (Range, Tendril); +} diff --git a/helix-core/src/increment/number.rs b/helix-core/src/increment/number.rs new file mode 100644 index 00000000..a19b7e75 --- /dev/null +++ b/helix-core/src/increment/number.rs @@ -0,0 +1,507 @@ +use std::borrow::Cow; + +use ropey::RopeSlice; + +use super::Increment; + +use crate::{ + textobject::{textobject_word, TextObject}, + Range, Tendril, +}; + +#[derive(Debug, PartialEq, Eq)] +pub struct NumberIncrementor<'a> { + value: i64, + radix: u32, + range: Range, + + text: RopeSlice<'a>, +} + +impl<'a> NumberIncrementor<'a> { + /// Return information about number under rang if there is one. + pub fn from_range(text: RopeSlice, range: Range) -> Option { + // If the cursor is on the minus sign of a number we want to get the word textobject to the + // right of it. + let range = if range.to() < text.len_chars() + && range.to() - range.from() <= 1 + && text.char(range.from()) == '-' + { + Range::new(range.from() + 1, range.to() + 1) + } else { + range + }; + + let range = textobject_word(text, range, TextObject::Inside, 1, false); + + // If there is a minus sign to the left of the word object, we want to include it in the range. + let range = if range.from() > 0 && text.char(range.from() - 1) == '-' { + range.extend(range.from() - 1, range.from()) + } else { + range + }; + + let word: String = text + .slice(range.from()..range.to()) + .chars() + .filter(|&c| c != '_') + .collect(); + let (radix, prefixed) = if word.starts_with("0x") { + (16, true) + } else if word.starts_with("0o") { + (8, true) + } else if word.starts_with("0b") { + (2, true) + } else { + (10, false) + }; + + let number = if prefixed { &word[2..] } else { &word }; + + let value = i128::from_str_radix(number, radix).ok()?; + if (value.is_positive() && value.leading_zeros() < 64) + || (value.is_negative() && value.leading_ones() < 64) + { + return None; + } + + let value = value as i64; + Some(NumberIncrementor { + range, + value, + radix, + text, + }) + } +} + +impl<'a> Increment for NumberIncrementor<'a> { + fn increment(&self, amount: i64) -> (Range, Tendril) { + let old_text: Cow = self.text.slice(self.range.from()..self.range.to()).into(); + let old_length = old_text.len(); + let new_value = self.value.wrapping_add(amount); + + // Get separator indexes from right to left. + let separator_rtl_indexes: Vec = old_text + .chars() + .rev() + .enumerate() + .filter_map(|(i, c)| if c == '_' { Some(i) } else { None }) + .collect(); + + let format_length = if self.radix == 10 { + match (self.value.is_negative(), new_value.is_negative()) { + (true, false) => old_length - 1, + (false, true) => old_length + 1, + _ => old_text.len(), + } + } else { + old_text.len() - 2 + } - separator_rtl_indexes.len(); + + let mut new_text = match self.radix { + 2 => format!("0b{:01$b}", new_value, format_length), + 8 => format!("0o{:01$o}", new_value, format_length), + 10 if old_text.starts_with('0') || old_text.starts_with("-0") => { + format!("{:01$}", new_value, format_length) + } + 10 => format!("{}", new_value), + 16 => { + let (lower_count, upper_count): (usize, usize) = + old_text.chars().skip(2).fold((0, 0), |(lower, upper), c| { + ( + lower + c.is_ascii_lowercase().then(|| 1).unwrap_or(0), + upper + c.is_ascii_uppercase().then(|| 1).unwrap_or(0), + ) + }); + if upper_count > lower_count { + format!("0x{:01$X}", new_value, format_length) + } else { + format!("0x{:01$x}", new_value, format_length) + } + } + _ => unimplemented!("radix not supported: {}", self.radix), + }; + + // Add separators from original number. + for &rtl_index in &separator_rtl_indexes { + if rtl_index < new_text.len() { + let new_index = new_text.len() - rtl_index; + new_text.insert(new_index, '_'); + } + } + + // Add in additional separators if necessary. + if new_text.len() > old_length && !separator_rtl_indexes.is_empty() { + let spacing = match separator_rtl_indexes.as_slice() { + [.., b, a] => a - b - 1, + _ => separator_rtl_indexes[0], + }; + + let prefix_length = if self.radix == 10 { 0 } else { 2 }; + if let Some(mut index) = new_text.find('_') { + while index - prefix_length > spacing { + index -= spacing; + new_text.insert(index, '_'); + } + } + } + + (self.range, new_text.into()) + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::Rope; + + #[test] + fn test_decimal_at_point() { + let rope = Rope::from_str("Test text 12345 more text."); + let range = Range::point(12); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(10, 15), + value: 12345, + radix: 10, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_uppercase_hexadecimal_at_point() { + let rope = Rope::from_str("Test text 0x123ABCDEF more text."); + let range = Range::point(12); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(10, 21), + value: 0x123ABCDEF, + radix: 16, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_lowercase_hexadecimal_at_point() { + let rope = Rope::from_str("Test text 0xfa3b4e more text."); + let range = Range::point(12); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(10, 18), + value: 0xfa3b4e, + radix: 16, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_octal_at_point() { + let rope = Rope::from_str("Test text 0o1074312 more text."); + let range = Range::point(12); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(10, 19), + value: 0o1074312, + radix: 8, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_binary_at_point() { + let rope = Rope::from_str("Test text 0b10111010010101 more text."); + let range = Range::point(12); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(10, 26), + value: 0b10111010010101, + radix: 2, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_negative_decimal_at_point() { + let rope = Rope::from_str("Test text -54321 more text."); + let range = Range::point(12); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(10, 16), + value: -54321, + radix: 10, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_decimal_with_leading_zeroes_at_point() { + let rope = Rope::from_str("Test text 000045326 more text."); + let range = Range::point(12); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(10, 19), + value: 45326, + radix: 10, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_negative_decimal_cursor_on_minus_sign() { + let rope = Rope::from_str("Test text -54321 more text."); + let range = Range::point(10); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(10, 16), + value: -54321, + radix: 10, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_number_under_range_start_of_rope() { + let rope = Rope::from_str("100"); + let range = Range::point(0); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(0, 3), + value: 100, + radix: 10, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_number_under_range_end_of_rope() { + let rope = Rope::from_str("100"); + let range = Range::point(2); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(0, 3), + value: 100, + radix: 10, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_number_surrounded_by_punctuation() { + let rope = Rope::from_str(",100;"); + let range = Range::point(1); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range), + Some(NumberIncrementor { + range: Range::new(1, 4), + value: 100, + radix: 10, + text: rope.slice(..), + }) + ); + } + + #[test] + fn test_not_a_number_point() { + let rope = Rope::from_str("Test text 45326 more text."); + let range = Range::point(6); + assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None); + } + + #[test] + fn test_number_too_large_at_point() { + let rope = Rope::from_str("Test text 0xFFFFFFFFFFFFFFFFF more text."); + let range = Range::point(12); + assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None); + } + + #[test] + fn test_number_cursor_one_right_of_number() { + let rope = Rope::from_str("100 "); + let range = Range::point(3); + assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None); + } + + #[test] + fn test_number_cursor_one_left_of_number() { + let rope = Rope::from_str(" 100"); + let range = Range::point(0); + assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None); + } + + #[test] + fn test_increment_basic_decimal_numbers() { + let tests = [ + ("100", 1, "101"), + ("100", -1, "99"), + ("99", 1, "100"), + ("100", 1000, "1100"), + ("100", -1000, "-900"), + ("-1", 1, "0"), + ("-1", 2, "1"), + ("1", -1, "0"), + ("1", -2, "-1"), + ]; + + for (original, amount, expected) in tests { + let rope = Rope::from_str(original); + let range = Range::point(0); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range) + .unwrap() + .increment(amount) + .1, + expected.into() + ); + } + } + + #[test] + fn test_increment_basic_hexadedimal_numbers() { + let tests = [ + ("0x0100", 1, "0x0101"), + ("0x0100", -1, "0x00ff"), + ("0x0001", -1, "0x0000"), + ("0x0000", -1, "0xffffffffffffffff"), + ("0xffffffffffffffff", 1, "0x0000000000000000"), + ("0xffffffffffffffff", 2, "0x0000000000000001"), + ("0xffffffffffffffff", -1, "0xfffffffffffffffe"), + ("0xABCDEF1234567890", 1, "0xABCDEF1234567891"), + ("0xabcdef1234567890", 1, "0xabcdef1234567891"), + ]; + + for (original, amount, expected) in tests { + let rope = Rope::from_str(original); + let range = Range::point(0); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range) + .unwrap() + .increment(amount) + .1, + expected.into() + ); + } + } + + #[test] + fn test_increment_basic_octal_numbers() { + let tests = [ + ("0o0107", 1, "0o0110"), + ("0o0110", -1, "0o0107"), + ("0o0001", -1, "0o0000"), + ("0o7777", 1, "0o10000"), + ("0o1000", -1, "0o0777"), + ("0o0107", 10, "0o0121"), + ("0o0000", -1, "0o1777777777777777777777"), + ("0o1777777777777777777777", 1, "0o0000000000000000000000"), + ("0o1777777777777777777777", 2, "0o0000000000000000000001"), + ("0o1777777777777777777777", -1, "0o1777777777777777777776"), + ]; + + for (original, amount, expected) in tests { + let rope = Rope::from_str(original); + let range = Range::point(0); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range) + .unwrap() + .increment(amount) + .1, + expected.into() + ); + } + } + + #[test] + fn test_increment_basic_binary_numbers() { + let tests = [ + ("0b00000100", 1, "0b00000101"), + ("0b00000100", -1, "0b00000011"), + ("0b00000100", 2, "0b00000110"), + ("0b00000100", -2, "0b00000010"), + ("0b00000001", -1, "0b00000000"), + ("0b00111111", 10, "0b01001001"), + ("0b11111111", 1, "0b100000000"), + ("0b10000000", -1, "0b01111111"), + ( + "0b0000", + -1, + "0b1111111111111111111111111111111111111111111111111111111111111111", + ), + ( + "0b1111111111111111111111111111111111111111111111111111111111111111", + 1, + "0b0000000000000000000000000000000000000000000000000000000000000000", + ), + ( + "0b1111111111111111111111111111111111111111111111111111111111111111", + 2, + "0b0000000000000000000000000000000000000000000000000000000000000001", + ), + ( + "0b1111111111111111111111111111111111111111111111111111111111111111", + -1, + "0b1111111111111111111111111111111111111111111111111111111111111110", + ), + ]; + + for (original, amount, expected) in tests { + let rope = Rope::from_str(original); + let range = Range::point(0); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range) + .unwrap() + .increment(amount) + .1, + expected.into() + ); + } + } + + #[test] + fn test_increment_with_separators() { + let tests = [ + ("999_999", 1, "1_000_000"), + ("1_000_000", -1, "999_999"), + ("-999_999", -1, "-1_000_000"), + ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"), + ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"), + ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"), + ("0x0000_0000", -1, "0xffff_ffff_ffff_ffff"), + ("0x0000_0000_0000", -1, "0xffff_ffff_ffff_ffff"), + ("0b01111111_11111111", 1, "0b10000000_00000000"), + ("0b11111111_11111111", 1, "0b1_00000000_00000000"), + ]; + + for (original, amount, expected) in tests { + let rope = Rope::from_str(original); + let range = Range::point(0); + assert_eq!( + NumberIncrementor::from_range(rope.slice(..), range) + .unwrap() + .increment(amount) + .1, + expected.into() + ); + } + } +} diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index b16a716f..4ae044cc 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -1,17 +1,16 @@ pub mod auto_pairs; pub mod chars; pub mod comment; -pub mod date; pub mod diagnostic; pub mod diff; pub mod graphemes; pub mod history; +pub mod increment; pub mod indent; pub mod line_ending; pub mod macros; pub mod match_brackets; pub mod movement; -pub mod numbers; pub mod object; pub mod path; mod position; diff --git a/helix-core/src/numbers.rs b/helix-core/src/numbers.rs deleted file mode 100644 index e9f3c898..00000000 --- a/helix-core/src/numbers.rs +++ /dev/null @@ -1,499 +0,0 @@ -use std::borrow::Cow; - -use ropey::RopeSlice; - -use crate::{ - textobject::{textobject_word, TextObject}, - Range, Tendril, -}; - -#[derive(Debug, PartialEq, Eq)] -pub struct NumberIncrementor<'a> { - pub range: Range, - pub value: i64, - pub radix: u32, - - text: RopeSlice<'a>, -} - -impl<'a> NumberIncrementor<'a> { - /// Return information about number under rang if there is one. - pub fn from_range(text: RopeSlice, range: Range) -> Option { - // If the cursor is on the minus sign of a number we want to get the word textobject to the - // right of it. - let range = if range.to() < text.len_chars() - && range.to() - range.from() <= 1 - && text.char(range.from()) == '-' - { - Range::new(range.from() + 1, range.to() + 1) - } else { - range - }; - - let range = textobject_word(text, range, TextObject::Inside, 1, false); - - // If there is a minus sign to the left of the word object, we want to include it in the range. - let range = if range.from() > 0 && text.char(range.from() - 1) == '-' { - range.extend(range.from() - 1, range.from()) - } else { - range - }; - - let word: String = text - .slice(range.from()..range.to()) - .chars() - .filter(|&c| c != '_') - .collect(); - let (radix, prefixed) = if word.starts_with("0x") { - (16, true) - } else if word.starts_with("0o") { - (8, true) - } else if word.starts_with("0b") { - (2, true) - } else { - (10, false) - }; - - let number = if prefixed { &word[2..] } else { &word }; - - let value = i128::from_str_radix(number, radix).ok()?; - if (value.is_positive() && value.leading_zeros() < 64) - || (value.is_negative() && value.leading_ones() < 64) - { - return None; - } - - let value = value as i64; - Some(NumberIncrementor { - range, - value, - radix, - text, - }) - } - - /// Add `amount` to the number and return the formatted text. - pub fn incremented_text(&self, amount: i64) -> Tendril { - let old_text: Cow = self.text.slice(self.range.from()..self.range.to()).into(); - let old_length = old_text.len(); - let new_value = self.value.wrapping_add(amount); - - // Get separator indexes from right to left. - let separator_rtl_indexes: Vec = old_text - .chars() - .rev() - .enumerate() - .filter_map(|(i, c)| if c == '_' { Some(i) } else { None }) - .collect(); - - let format_length = if self.radix == 10 { - match (self.value.is_negative(), new_value.is_negative()) { - (true, false) => old_length - 1, - (false, true) => old_length + 1, - _ => old_text.len(), - } - } else { - old_text.len() - 2 - } - separator_rtl_indexes.len(); - - let mut new_text = match self.radix { - 2 => format!("0b{:01$b}", new_value, format_length), - 8 => format!("0o{:01$o}", new_value, format_length), - 10 if old_text.starts_with('0') || old_text.starts_with("-0") => { - format!("{:01$}", new_value, format_length) - } - 10 => format!("{}", new_value), - 16 => { - let (lower_count, upper_count): (usize, usize) = - old_text.chars().skip(2).fold((0, 0), |(lower, upper), c| { - ( - lower + c.is_ascii_lowercase().then(|| 1).unwrap_or(0), - upper + c.is_ascii_uppercase().then(|| 1).unwrap_or(0), - ) - }); - if upper_count > lower_count { - format!("0x{:01$X}", new_value, format_length) - } else { - format!("0x{:01$x}", new_value, format_length) - } - } - _ => unimplemented!("radix not supported: {}", self.radix), - }; - - // Add separators from original number. - for &rtl_index in &separator_rtl_indexes { - if rtl_index < new_text.len() { - let new_index = new_text.len() - rtl_index; - new_text.insert(new_index, '_'); - } - } - - // Add in additional separators if necessary. - if new_text.len() > old_length && !separator_rtl_indexes.is_empty() { - let spacing = match separator_rtl_indexes.as_slice() { - [.., b, a] => a - b - 1, - _ => separator_rtl_indexes[0], - }; - - let prefix_length = if self.radix == 10 { 0 } else { 2 }; - if let Some(mut index) = new_text.find('_') { - while index - prefix_length > spacing { - index -= spacing; - new_text.insert(index, '_'); - } - } - } - - new_text.into() - } -} - -#[cfg(test)] -mod test { - use super::*; - use crate::Rope; - - #[test] - fn test_decimal_at_point() { - let rope = Rope::from_str("Test text 12345 more text."); - let range = Range::point(12); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(10, 15), - value: 12345, - radix: 10, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_uppercase_hexadecimal_at_point() { - let rope = Rope::from_str("Test text 0x123ABCDEF more text."); - let range = Range::point(12); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(10, 21), - value: 0x123ABCDEF, - radix: 16, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_lowercase_hexadecimal_at_point() { - let rope = Rope::from_str("Test text 0xfa3b4e more text."); - let range = Range::point(12); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(10, 18), - value: 0xfa3b4e, - radix: 16, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_octal_at_point() { - let rope = Rope::from_str("Test text 0o1074312 more text."); - let range = Range::point(12); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(10, 19), - value: 0o1074312, - radix: 8, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_binary_at_point() { - let rope = Rope::from_str("Test text 0b10111010010101 more text."); - let range = Range::point(12); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(10, 26), - value: 0b10111010010101, - radix: 2, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_negative_decimal_at_point() { - let rope = Rope::from_str("Test text -54321 more text."); - let range = Range::point(12); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(10, 16), - value: -54321, - radix: 10, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_decimal_with_leading_zeroes_at_point() { - let rope = Rope::from_str("Test text 000045326 more text."); - let range = Range::point(12); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(10, 19), - value: 45326, - radix: 10, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_negative_decimal_cursor_on_minus_sign() { - let rope = Rope::from_str("Test text -54321 more text."); - let range = Range::point(10); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(10, 16), - value: -54321, - radix: 10, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_number_under_range_start_of_rope() { - let rope = Rope::from_str("100"); - let range = Range::point(0); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(0, 3), - value: 100, - radix: 10, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_number_under_range_end_of_rope() { - let rope = Rope::from_str("100"); - let range = Range::point(2); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(0, 3), - value: 100, - radix: 10, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_number_surrounded_by_punctuation() { - let rope = Rope::from_str(",100;"); - let range = Range::point(1); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range), - Some(NumberIncrementor { - range: Range::new(1, 4), - value: 100, - radix: 10, - text: rope.slice(..), - }) - ); - } - - #[test] - fn test_not_a_number_point() { - let rope = Rope::from_str("Test text 45326 more text."); - let range = Range::point(6); - assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None); - } - - #[test] - fn test_number_too_large_at_point() { - let rope = Rope::from_str("Test text 0xFFFFFFFFFFFFFFFFF more text."); - let range = Range::point(12); - assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None); - } - - #[test] - fn test_number_cursor_one_right_of_number() { - let rope = Rope::from_str("100 "); - let range = Range::point(3); - assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None); - } - - #[test] - fn test_number_cursor_one_left_of_number() { - let rope = Rope::from_str(" 100"); - let range = Range::point(0); - assert_eq!(NumberIncrementor::from_range(rope.slice(..), range), None); - } - - #[test] - fn test_increment_basic_decimal_numbers() { - let tests = [ - ("100", 1, "101"), - ("100", -1, "99"), - ("99", 1, "100"), - ("100", 1000, "1100"), - ("100", -1000, "-900"), - ("-1", 1, "0"), - ("-1", 2, "1"), - ("1", -1, "0"), - ("1", -2, "-1"), - ]; - - for (original, amount, expected) in tests { - let rope = Rope::from_str(original); - let range = Range::point(0); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range) - .unwrap() - .incremented_text(amount), - expected.into() - ); - } - } - - #[test] - fn test_increment_basic_hexadedimal_numbers() { - let tests = [ - ("0x0100", 1, "0x0101"), - ("0x0100", -1, "0x00ff"), - ("0x0001", -1, "0x0000"), - ("0x0000", -1, "0xffffffffffffffff"), - ("0xffffffffffffffff", 1, "0x0000000000000000"), - ("0xffffffffffffffff", 2, "0x0000000000000001"), - ("0xffffffffffffffff", -1, "0xfffffffffffffffe"), - ("0xABCDEF1234567890", 1, "0xABCDEF1234567891"), - ("0xabcdef1234567890", 1, "0xabcdef1234567891"), - ]; - - for (original, amount, expected) in tests { - let rope = Rope::from_str(original); - let range = Range::point(0); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range) - .unwrap() - .incremented_text(amount), - expected.into() - ); - } - } - - #[test] - fn test_increment_basic_octal_numbers() { - let tests = [ - ("0o0107", 1, "0o0110"), - ("0o0110", -1, "0o0107"), - ("0o0001", -1, "0o0000"), - ("0o7777", 1, "0o10000"), - ("0o1000", -1, "0o0777"), - ("0o0107", 10, "0o0121"), - ("0o0000", -1, "0o1777777777777777777777"), - ("0o1777777777777777777777", 1, "0o0000000000000000000000"), - ("0o1777777777777777777777", 2, "0o0000000000000000000001"), - ("0o1777777777777777777777", -1, "0o1777777777777777777776"), - ]; - - for (original, amount, expected) in tests { - let rope = Rope::from_str(original); - let range = Range::point(0); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range) - .unwrap() - .incremented_text(amount), - expected.into() - ); - } - } - - #[test] - fn test_increment_basic_binary_numbers() { - let tests = [ - ("0b00000100", 1, "0b00000101"), - ("0b00000100", -1, "0b00000011"), - ("0b00000100", 2, "0b00000110"), - ("0b00000100", -2, "0b00000010"), - ("0b00000001", -1, "0b00000000"), - ("0b00111111", 10, "0b01001001"), - ("0b11111111", 1, "0b100000000"), - ("0b10000000", -1, "0b01111111"), - ( - "0b0000", - -1, - "0b1111111111111111111111111111111111111111111111111111111111111111", - ), - ( - "0b1111111111111111111111111111111111111111111111111111111111111111", - 1, - "0b0000000000000000000000000000000000000000000000000000000000000000", - ), - ( - "0b1111111111111111111111111111111111111111111111111111111111111111", - 2, - "0b0000000000000000000000000000000000000000000000000000000000000001", - ), - ( - "0b1111111111111111111111111111111111111111111111111111111111111111", - -1, - "0b1111111111111111111111111111111111111111111111111111111111111110", - ), - ]; - - for (original, amount, expected) in tests { - let rope = Rope::from_str(original); - let range = Range::point(0); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range) - .unwrap() - .incremented_text(amount), - expected.into() - ); - } - } - - #[test] - fn test_increment_with_separators() { - let tests = [ - ("999_999", 1, "1_000_000"), - ("1_000_000", -1, "999_999"), - ("-999_999", -1, "-1_000_000"), - ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"), - ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"), - ("0x0000_0000_0001", 0x1_ffff_0000, "0x0001_ffff_0001"), - ("0x0000_0000", -1, "0xffff_ffff_ffff_ffff"), - ("0x0000_0000_0000", -1, "0xffff_ffff_ffff_ffff"), - ("0b01111111_11111111", 1, "0b10000000_00000000"), - ("0b11111111_11111111", 1, "0b1_00000000_00000000"), - ]; - - for (original, amount, expected) in tests { - let rope = Rope::from_str(original); - let range = Range::point(0); - assert_eq!( - NumberIncrementor::from_range(rope.slice(..), range) - .unwrap() - .incremented_text(amount), - expected.into() - ); - } - } -} diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 639bbd83..6329dec7 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1,14 +1,13 @@ use helix_core::{ - comment, coords_at_pos, - date::DateIncrementor, - find_first_non_whitespace_char, find_root, graphemes, + comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, history::UndoKind, + increment::date::DateIncrementor, + increment::{number::NumberIncrementor, Increment}, indent, indent::IndentStyle, line_ending::{get_line_ending_of_str, line_end_char_index, str_is_line_ending}, match_brackets, movement::{self, Direction}, - numbers::NumberIncrementor, object, pos_at_coords, regex::{self, Regex, RegexBuilder}, search, selection, surround, textobject, @@ -5804,23 +5803,18 @@ fn increment_impl(cx: &mut Context, amount: i64) { let text = doc.text(); let changes = selection.ranges().iter().filter_map(|range| { - if let Some(incrementor) = DateIncrementor::from_range(text.slice(..), *range) { - let new_text = incrementor.incremented_text(amount); - Some(( - incrementor.range.from(), - incrementor.range.to(), - Some(new_text), - )) + let incrementor: Option> = if let Some(incrementor) = + DateIncrementor::from_range(text.slice(..), *range) + { + Some(Box::new(incrementor)) } else if let Some(incrementor) = NumberIncrementor::from_range(text.slice(..), *range) { - let new_text = incrementor.incremented_text(amount); - Some(( - incrementor.range.from(), - incrementor.range.to(), - Some(new_text), - )) + Some(Box::new(incrementor)) } else { None - } + }; + + let (range, new_text) = incrementor?.increment(amount); + Some((range.from(), range.to(), Some(new_text))) }); if changes.clone().count() > 0 { -- cgit v1.2.3-70-g09d2 From 37e484ee38eb5a9b4da280960fb1e29939ee9d39 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Thu, 25 Nov 2021 19:58:23 -0700 Subject: Add support for time and more date formats --- helix-core/src/increment/date.rs | 474 ------------------------------- helix-core/src/increment/date_time.rs | 515 ++++++++++++++++++++++++++++++++++ helix-core/src/increment/mod.rs | 2 +- helix-term/src/commands.rs | 4 +- 4 files changed, 518 insertions(+), 477 deletions(-) delete mode 100644 helix-core/src/increment/date.rs create mode 100644 helix-core/src/increment/date_time.rs (limited to 'helix-core/src') diff --git a/helix-core/src/increment/date.rs b/helix-core/src/increment/date.rs deleted file mode 100644 index 05442990..00000000 --- a/helix-core/src/increment/date.rs +++ /dev/null @@ -1,474 +0,0 @@ -use regex::Regex; - -use std::borrow::Cow; -use std::cmp; - -use ropey::RopeSlice; - -use crate::{Range, Tendril}; - -use chrono::{Datelike, Duration, NaiveDate}; - -use super::Increment; - -fn ndays_in_month(year: i32, month: u32) -> u32 { - // The first day of the next month... - let (y, m) = if month == 12 { - (year + 1, 1) - } else { - (year, month + 1) - }; - let d = NaiveDate::from_ymd(y, m, 1); - - // ...is preceded by the last day of the original month. - d.pred().day() -} - -fn add_days(date: NaiveDate, amount: i64) -> Option { - date.checked_add_signed(Duration::days(amount)) -} - -fn add_months(date: NaiveDate, amount: i64) -> Option { - let month = date.month0() as i64 + amount; - let year = date.year() + i32::try_from(month / 12).ok()?; - let year = if month.is_negative() { year - 1 } else { year }; - - // Normalize month - let month = month % 12; - let month = if month.is_negative() { - month + 13 - } else { - month + 1 - } as u32; - - let day = cmp::min(date.day(), ndays_in_month(year, month)); - - Some(NaiveDate::from_ymd(year, month, day)) -} - -fn add_years(date: NaiveDate, amount: i64) -> Option { - let year = i32::try_from(date.year() as i64 + amount).ok()?; - let ndays = ndays_in_month(year, date.month()); - - if date.day() > ndays { - let d = NaiveDate::from_ymd(year, date.month(), ndays); - Some(d.succ()) - } else { - date.with_year(year) - } -} - -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -struct Format { - regex: &'static str, - separator: char, -} - -// Only support formats that aren't region specific. -static FORMATS: &[Format] = &[ - Format { - regex: r"(\d{4})-(\d{2})-(\d{2})", - separator: '-', - }, - Format { - regex: r"(\d{4})/(\d{2})/(\d{2})", - separator: '/', - }, -]; - -const DATE_LENGTH: usize = 10; - -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -enum DateField { - Year, - Month, - Day, -} - -#[derive(Debug, PartialEq, Eq)] -pub struct DateIncrementor { - date: NaiveDate, - range: Range, - field: DateField, - format: Format, -} - -impl DateIncrementor { - pub fn from_range(text: RopeSlice, range: Range) -> Option { - let range = if range.is_empty() { - if range.anchor < text.len_bytes() { - // Treat empty range as a cursor range. - range.put_cursor(text, range.anchor + 1, true) - } else { - // The range is empty and at the end of the text. - return None; - } - } else { - range - }; - - let from = range.from().saturating_sub(DATE_LENGTH); - let to = (range.from() + DATE_LENGTH).min(text.len_chars()); - - let (from_in_text, to_in_text) = (range.from() - from, range.to() - from); - let text: Cow = text.slice(from..to).into(); - - FORMATS.iter().find_map(|&format| { - let re = Regex::new(format.regex).ok()?; - let captures = re.captures(&text)?; - - let date = captures.get(0)?; - let offset = range.from() - from_in_text; - let range = Range::new(date.start() + offset, date.end() + offset); - - let (year, month, day) = (captures.get(1)?, captures.get(2)?, captures.get(3)?); - let (year_range, month_range, day_range) = (year.range(), month.range(), day.range()); - - let field = if year_range.contains(&from_in_text) - && year_range.contains(&(to_in_text - 1)) - { - DateField::Year - } else if month_range.contains(&from_in_text) && month_range.contains(&(to_in_text - 1)) - { - DateField::Month - } else if day_range.contains(&from_in_text) && day_range.contains(&(to_in_text - 1)) { - DateField::Day - } else { - return None; - }; - - let date = NaiveDate::from_ymd_opt( - year.as_str().parse::().ok()?, - month.as_str().parse::().ok()?, - day.as_str().parse::().ok()?, - )?; - - Some(DateIncrementor { - date, - field, - range, - format, - }) - }) - } -} - -impl Increment for DateIncrementor { - fn increment(&self, amount: i64) -> (Range, Tendril) { - let date = match self.field { - DateField::Year => add_years(self.date, amount), - DateField::Month => add_months(self.date, amount), - DateField::Day => add_days(self.date, amount), - } - .unwrap_or(self.date); - - ( - self.range, - format!( - "{:04}{}{:02}{}{:02}", - date.year(), - self.format.separator, - date.month(), - self.format.separator, - date.day() - ) - .into(), - ) - } -} - -#[cfg(test)] -mod test { - use super::*; - use crate::Rope; - - #[test] - fn test_create_incrementor_for_year_with_dashes() { - let rope = Rope::from_str("2021-11-15"); - - for cursor in 0..=3 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Year, - format: FORMATS[0], - }) - ); - } - } - - #[test] - fn test_create_incrementor_for_month_with_dashes() { - let rope = Rope::from_str("2021-11-15"); - - for cursor in 5..=6 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Month, - format: FORMATS[0], - }) - ); - } - } - - #[test] - fn test_create_incrementor_for_day_with_dashes() { - let rope = Rope::from_str("2021-11-15"); - - for cursor in 8..=9 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Day, - format: FORMATS[0], - }) - ); - } - } - - #[test] - fn test_try_create_incrementor_on_dashes() { - let rope = Rope::from_str("2021-11-15"); - - for &cursor in &[4, 7] { - let range = Range::new(cursor, cursor + 1); - assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None,); - } - } - - #[test] - fn test_create_incrementor_for_year_with_slashes() { - let rope = Rope::from_str("2021/11/15"); - - for cursor in 0..=3 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Year, - format: FORMATS[1], - }) - ); - } - } - - #[test] - fn test_create_incrementor_for_month_with_slashes() { - let rope = Rope::from_str("2021/11/15"); - - for cursor in 5..=6 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Month, - format: FORMATS[1], - }) - ); - } - } - - #[test] - fn test_create_incrementor_for_day_with_slashes() { - let rope = Rope::from_str("2021/11/15"); - - for cursor in 8..=9 { - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Day, - format: FORMATS[1], - }) - ); - } - } - - #[test] - fn test_try_create_incrementor_on_slashes() { - let rope = Rope::from_str("2021/11/15"); - - for &cursor in &[4, 7] { - let range = Range::new(cursor, cursor + 1); - assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None,); - } - } - - #[test] - fn test_date_surrounded_by_spaces() { - let rope = Rope::from_str(" 2021-11-15 "); - let range = Range::new(3, 4); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(3, 13), - field: DateField::Year, - format: FORMATS[0], - }) - ); - } - - #[test] - fn test_date_in_single_quotes() { - let rope = Rope::from_str("date = '2021-11-15'"); - let range = Range::new(10, 11); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(8, 18), - field: DateField::Year, - format: FORMATS[0], - }) - ); - } - - #[test] - fn test_date_in_double_quotes() { - let rope = Rope::from_str("let date = \"2021-11-15\";"); - let range = Range::new(12, 13); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(12, 22), - field: DateField::Year, - format: FORMATS[0], - }) - ); - } - - #[test] - fn test_date_cursor_one_right_of_date() { - let rope = Rope::from_str("2021-11-15 "); - let range = Range::new(10, 11); - 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::new(0, 1); - assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None); - } - - #[test] - fn test_date_empty_range_at_beginning() { - 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), - field: DateField::Year, - format: FORMATS[0], - }) - ); - } - - #[test] - fn test_date_empty_range_at_in_middle() { - let rope = Rope::from_str("2021-11-15"); - let range = Range::point(5); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range), - Some(DateIncrementor { - date: NaiveDate::from_ymd(2021, 11, 15), - range: Range::new(0, 10), - field: DateField::Month, - format: FORMATS[0], - }) - ); - } - - #[test] - fn test_date_empty_range_at_end() { - 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_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::new(0, 1); - - assert_eq!(DateIncrementor::from_range(rope.slice(..), range), None); - } - } - - #[test] - fn test_increment_dates() { - let tests = [ - // (original, cursor, amount, expected) - ("2020-02-28", 0, 1, "2021-02-28"), - ("2020-02-29", 0, 1, "2021-03-01"), - ("2020-01-31", 5, 1, "2020-02-29"), - ("2020-01-20", 5, 1, "2020-02-20"), - ("2021-01-01", 5, -1, "2020-12-01"), - ("2021-01-31", 5, -2, "2020-11-30"), - ("2020-02-28", 8, 1, "2020-02-29"), - ("2021-02-28", 8, 1, "2021-03-01"), - ("2021-02-28", 0, -1, "2020-02-28"), - ("2021-03-01", 0, -1, "2020-03-01"), - ("2020-02-29", 5, -1, "2020-01-29"), - ("2020-02-20", 5, -1, "2020-01-20"), - ("2020-02-29", 8, -1, "2020-02-28"), - ("2021-03-01", 8, -1, "2021-02-28"), - ("1980/12/21", 8, 100, "1981/03/31"), - ("1980/12/21", 8, -100, "1980/09/12"), - ("1980/12/21", 8, 1000, "1983/09/17"), - ("1980/12/21", 8, -1000, "1978/03/27"), - ]; - - for (original, cursor, amount, expected) in tests { - let rope = Rope::from_str(original); - let range = Range::new(cursor, cursor + 1); - assert_eq!( - DateIncrementor::from_range(rope.slice(..), range) - .unwrap() - .increment(amount) - .1, - expected.into() - ); - } - } -} diff --git a/helix-core/src/increment/date_time.rs b/helix-core/src/increment/date_time.rs new file mode 100644 index 00000000..39380104 --- /dev/null +++ b/helix-core/src/increment/date_time.rs @@ -0,0 +1,515 @@ +use chrono::{Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime, Timelike}; +use once_cell::sync::Lazy; +use regex::Regex; +use ropey::RopeSlice; + +use std::borrow::Cow; +use std::cmp; + +use super::Increment; +use crate::{Range, Tendril}; + +#[derive(Debug, PartialEq, Eq)] +pub struct DateTimeIncrementor { + date_time: NaiveDateTime, + range: Range, + format: Format, + field: DateField, +} + +impl DateTimeIncrementor { + pub fn from_range(text: RopeSlice, range: Range) -> Option { + let range = if range.is_empty() { + if range.anchor < text.len_chars() { + // Treat empty range as a cursor range. + range.put_cursor(text, range.anchor + 1, true) + } else { + // The range is empty and at the end of the text. + return None; + } + } else { + range + }; + + FORMATS.iter().find_map(|format| { + let from = range.from().saturating_sub(format.max_len); + let to = (range.from() + format.max_len).min(text.len_chars()); + + let (from_in_text, to_in_text) = (range.from() - from, range.to() - from); + let text: Cow = text.slice(from..to).into(); + + let captures = format.regex.captures(&text)?; + if captures.len() - 1 != format.fields.len() { + return None; + } + + let date_time = captures.get(0)?; + let offset = range.from() - from_in_text; + let range = Range::new(date_time.start() + offset, date_time.end() + offset); + + let field = captures + .iter() + .skip(1) + .enumerate() + .find_map(|(i, capture)| { + let capture = capture?; + let capture_range = capture.range(); + + if capture_range.contains(&from_in_text) + && capture_range.contains(&(to_in_text - 1)) + { + Some(format.fields[i]) + } else { + None + } + })?; + + let has_date = format.fields.iter().any(|f| f.unit.is_date()); + let has_time = format.fields.iter().any(|f| f.unit.is_time()); + + let date_time = match (has_date, has_time) { + (true, true) => NaiveDateTime::parse_from_str( + &text[date_time.start()..date_time.end()], + format.fmt, + ) + .ok()?, + (true, false) => { + let date = NaiveDate::parse_from_str( + &text[date_time.start()..date_time.end()], + format.fmt, + ) + .ok()?; + + date.and_hms(0, 0, 0) + } + (false, true) => { + let time = NaiveTime::parse_from_str( + &text[date_time.start()..date_time.end()], + format.fmt, + ) + .ok()?; + + NaiveDate::from_ymd(0, 1, 1).and_time(time) + } + (false, false) => return None, + }; + + Some(DateTimeIncrementor { + date_time, + range, + format: format.clone(), + field, + }) + }) + } +} + +impl Increment for DateTimeIncrementor { + fn increment(&self, amount: i64) -> (Range, Tendril) { + let date_time = match self.field.unit { + DateUnit::Years => add_years(self.date_time, amount), + DateUnit::Months => add_months(self.date_time, amount), + DateUnit::Days => add_duration(self.date_time, Duration::days(amount)), + DateUnit::Hours => add_duration(self.date_time, Duration::hours(amount)), + DateUnit::Minutes => add_duration(self.date_time, Duration::minutes(amount)), + DateUnit::Seconds => add_duration(self.date_time, Duration::seconds(amount)), + DateUnit::AmPm => toggle_am_pm(self.date_time), + } + .unwrap_or(self.date_time); + + ( + self.range, + date_time.format(self.format.fmt).to_string().into(), + ) + } +} + +static FORMATS: Lazy> = Lazy::new(|| { + vec![ + Format::new("%Y-%m-%d %H:%M:%S"), // 2021-11-24 07:12:23 + Format::new("%Y/%m/%d %H:%M:%S"), // 2021/11/24 07:12:23 + Format::new("%Y-%m-%d %H:%M"), // 2021-11-24 07:12 + Format::new("%Y/%m/%d %H:%M"), // 2021/11/24 07:12 + Format::new("%Y-%m-%d"), // 2021-11-24 + Format::new("%Y/%m/%d"), // 2021/11/24 + Format::new("%a %b %d %Y"), // Wed Nov 24 2021 + Format::new("%d-%b-%Y"), // 24-Nov-2021 + Format::new("%Y %b %d"), // 2021 Nov 24 + Format::new("%b %d, %Y"), // Nov 24, 2021 + Format::new("%-I:%M:%S %P"), // 7:21:53 am + Format::new("%-I:%M %P"), // 7:21 am + Format::new("%-I:%M:%S %p"), // 7:21:53 AM + Format::new("%-I:%M %p"), // 7:21 AM + Format::new("%H:%M:%S"), // 23:24:23 + Format::new("%H:%M"), // 23:24 + ] +}); + +#[derive(Clone, Debug)] +struct Format { + fmt: &'static str, + fields: Vec, + regex: Regex, + max_len: usize, +} + +impl Format { + fn new(fmt: &'static str) -> Self { + let mut remaining = fmt; + let mut fields = Vec::new(); + let mut regex = String::new(); + let mut max_len = 0; + + while let Some(i) = remaining.find('%') { + let mut chars = remaining[i + 1..].chars(); + let spec_len = if let Some(c) = chars.next() { + if c == '-' { + if chars.next().is_some() { + 2 + } else { + 0 + } + } else { + 1 + } + } else { + 0 + }; + + if i < remaining.len() - spec_len { + let specifier = &remaining[i + 1..i + 1 + spec_len]; + if let Some(field) = DateField::from_specifier(specifier) { + fields.push(field); + max_len += field.max_len + remaining[..i].len(); + regex += &remaining[..i]; + regex += &format!("({})", field.regex); + remaining = &remaining[i + spec_len + 1..]; + } else { + regex += &remaining[..=i]; + } + } else { + regex += remaining; + } + } + + let regex = Regex::new(®ex).unwrap(); + + Self { + fmt, + fields, + regex, + max_len, + } + } +} + +impl PartialEq for Format { + fn eq(&self, other: &Self) -> bool { + self.fmt == other.fmt && self.fields == other.fields && self.max_len == other.max_len + } +} + +impl Eq for Format {} + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +struct DateField { + regex: &'static str, + unit: DateUnit, + max_len: usize, +} + +impl DateField { + fn from_specifier(specifier: &str) -> Option { + match specifier { + "Y" => Some(DateField { + regex: r"\d{4}", + unit: DateUnit::Years, + max_len: 5, + }), + "y" => Some(DateField { + regex: r"\d\d", + unit: DateUnit::Years, + max_len: 2, + }), + "m" => Some(DateField { + regex: r"[0-1]\d", + unit: DateUnit::Months, + max_len: 2, + }), + "d" => Some(DateField { + regex: r"[0-3]\d", + unit: DateUnit::Days, + max_len: 2, + }), + "-d" => Some(DateField { + regex: r"[1-3]?\d", + unit: DateUnit::Days, + max_len: 2, + }), + "a" => Some(DateField { + regex: r"Sun|Mon|Tue|Wed|Thu|Fri|Sat", + unit: DateUnit::Days, + max_len: 3, + }), + "A" => Some(DateField { + regex: r"Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday", + unit: DateUnit::Days, + max_len: 9, + }), + "b" | "h" => Some(DateField { + regex: r"Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", + unit: DateUnit::Months, + max_len: 3, + }), + "B" => Some(DateField { + regex: r"January|February|March|April|May|June|July|August|September|October|November|December", + unit: DateUnit::Months, + max_len: 9, + }), + "H" => Some(DateField { + regex: r"[0-2]\d", + unit: DateUnit::Hours, + max_len: 2, + }), + "M" => Some(DateField { + regex: r"[0-5]\d", + unit: DateUnit::Minutes, + max_len: 2, + }), + "S" => Some(DateField { + regex: r"[0-5]\d", + unit: DateUnit::Seconds, + max_len: 2, + }), + "I" => Some(DateField { + regex: r"[0-1]\d", + unit: DateUnit::Hours, + max_len: 2, + }), + "-I" => Some(DateField { + regex: r"1?\d", + unit: DateUnit::Hours, + max_len: 2, + }), + "P" => Some(DateField { + regex: r"am|pm", + unit: DateUnit::AmPm, + max_len: 2, + }), + "p" => Some(DateField { + regex: r"AM|PM", + unit: DateUnit::AmPm, + max_len: 2, + }), + _ => None, + } + } +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +enum DateUnit { + Years, + Months, + Days, + Hours, + Minutes, + Seconds, + AmPm, +} + +impl DateUnit { + fn is_date(self) -> bool { + matches!(self, DateUnit::Years | DateUnit::Months | DateUnit::Days) + } + + fn is_time(self) -> bool { + matches!( + self, + DateUnit::Hours | DateUnit::Minutes | DateUnit::Seconds + ) + } +} + +fn ndays_in_month(year: i32, month: u32) -> u32 { + // The first day of the next month... + let (y, m) = if month == 12 { + (year + 1, 1) + } else { + (year, month + 1) + }; + let d = NaiveDate::from_ymd(y, m, 1); + + // ...is preceded by the last day of the original month. + d.pred().day() +} + +fn add_months(date_time: NaiveDateTime, amount: i64) -> Option { + let month = date_time.month0() as i64 + amount; + let year = date_time.year() + i32::try_from(month / 12).ok()?; + let year = if month.is_negative() { year - 1 } else { year }; + + // Normalize month + let month = month % 12; + let month = if month.is_negative() { + month + 13 + } else { + month + 1 + } as u32; + + let day = cmp::min(date_time.day(), ndays_in_month(year, month)); + + Some(NaiveDate::from_ymd(year, month, day).and_time(date_time.time())) +} + +fn add_years(date_time: NaiveDateTime, amount: i64) -> Option { + let year = i32::try_from(date_time.year() as i64 + amount).ok()?; + let ndays = ndays_in_month(year, date_time.month()); + + if date_time.day() > ndays { + let d = NaiveDate::from_ymd(year, date_time.month(), ndays); + Some(d.succ().and_time(date_time.time())) + } else { + date_time.with_year(year) + } +} + +fn add_duration(date_time: NaiveDateTime, duration: Duration) -> Option { + date_time.checked_add_signed(duration) +} + +fn toggle_am_pm(date_time: NaiveDateTime) -> Option { + if date_time.hour() < 12 { + add_duration(date_time, Duration::hours(12)) + } else { + add_duration(date_time, Duration::hours(-12)) + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::Rope; + + #[test] + fn test_increment_date_times() { + let tests = [ + // (original, cursor, amount, expected) + ("2020-02-28", 0, 1, "2021-02-28"), + ("2020-02-29", 0, 1, "2021-03-01"), + ("2020-01-31", 5, 1, "2020-02-29"), + ("2020-01-20", 5, 1, "2020-02-20"), + ("2021-01-01", 5, -1, "2020-12-01"), + ("2021-01-31", 5, -2, "2020-11-30"), + ("2020-02-28", 8, 1, "2020-02-29"), + ("2021-02-28", 8, 1, "2021-03-01"), + ("2021-02-28", 0, -1, "2020-02-28"), + ("2021-03-01", 0, -1, "2020-03-01"), + ("2020-02-29", 5, -1, "2020-01-29"), + ("2020-02-20", 5, -1, "2020-01-20"), + ("2020-02-29", 8, -1, "2020-02-28"), + ("2021-03-01", 8, -1, "2021-02-28"), + ("1980/12/21", 8, 100, "1981/03/31"), + ("1980/12/21", 8, -100, "1980/09/12"), + ("1980/12/21", 8, 1000, "1983/09/17"), + ("1980/12/21", 8, -1000, "1978/03/27"), + ("2021-11-24 07:12:23", 0, 1, "2022-11-24 07:12:23"), + ("2021-11-24 07:12:23", 5, 1, "2021-12-24 07:12:23"), + ("2021-11-24 07:12:23", 8, 1, "2021-11-25 07:12:23"), + ("2021-11-24 07:12:23", 11, 1, "2021-11-24 08:12:23"), + ("2021-11-24 07:12:23", 14, 1, "2021-11-24 07:13:23"), + ("2021-11-24 07:12:23", 17, 1, "2021-11-24 07:12:24"), + ("2021/11/24 07:12:23", 0, 1, "2022/11/24 07:12:23"), + ("2021/11/24 07:12:23", 5, 1, "2021/12/24 07:12:23"), + ("2021/11/24 07:12:23", 8, 1, "2021/11/25 07:12:23"), + ("2021/11/24 07:12:23", 11, 1, "2021/11/24 08:12:23"), + ("2021/11/24 07:12:23", 14, 1, "2021/11/24 07:13:23"), + ("2021/11/24 07:12:23", 17, 1, "2021/11/24 07:12:24"), + ("2021-11-24 07:12", 0, 1, "2022-11-24 07:12"), + ("2021-11-24 07:12", 5, 1, "2021-12-24 07:12"), + ("2021-11-24 07:12", 8, 1, "2021-11-25 07:12"), + ("2021-11-24 07:12", 11, 1, "2021-11-24 08:12"), + ("2021-11-24 07:12", 14, 1, "2021-11-24 07:13"), + ("2021/11/24 07:12", 0, 1, "2022/11/24 07:12"), + ("2021/11/24 07:12", 5, 1, "2021/12/24 07:12"), + ("2021/11/24 07:12", 8, 1, "2021/11/25 07:12"), + ("2021/11/24 07:12", 11, 1, "2021/11/24 08:12"), + ("2021/11/24 07:12", 14, 1, "2021/11/24 07:13"), + ("Wed Nov 24 2021", 0, 1, "Thu Nov 25 2021"), + ("Wed Nov 24 2021", 4, 1, "Fri Dec 24 2021"), + ("Wed Nov 24 2021", 8, 1, "Thu Nov 25 2021"), + ("Wed Nov 24 2021", 11, 1, "Thu Nov 24 2022"), + ("24-Nov-2021", 0, 1, "25-Nov-2021"), + ("24-Nov-2021", 3, 1, "24-Dec-2021"), + ("24-Nov-2021", 7, 1, "24-Nov-2022"), + ("2021 Nov 24", 0, 1, "2022 Nov 24"), + ("2021 Nov 24", 5, 1, "2021 Dec 24"), + ("2021 Nov 24", 9, 1, "2021 Nov 25"), + ("Nov 24, 2021", 0, 1, "Dec 24, 2021"), + ("Nov 24, 2021", 4, 1, "Nov 25, 2021"), + ("Nov 24, 2021", 8, 1, "Nov 24, 2022"), + ("7:21:53 am", 0, 1, "8:21:53 am"), + ("7:21:53 am", 3, 1, "7:22:53 am"), + ("7:21:53 am", 5, 1, "7:21:54 am"), + ("7:21:53 am", 8, 1, "7:21:53 pm"), + ("7:21:53 AM", 0, 1, "8:21:53 AM"), + ("7:21:53 AM", 3, 1, "7:22:53 AM"), + ("7:21:53 AM", 5, 1, "7:21:54 AM"), + ("7:21:53 AM", 8, 1, "7:21:53 PM"), + ("7:21 am", 0, 1, "8:21 am"), + ("7:21 am", 3, 1, "7:22 am"), + ("7:21 am", 5, 1, "7:21 pm"), + ("7:21 AM", 0, 1, "8:21 AM"), + ("7:21 AM", 3, 1, "7:22 AM"), + ("7:21 AM", 5, 1, "7:21 PM"), + ("23:24:23", 1, 1, "00:24:23"), + ("23:24:23", 3, 1, "23:25:23"), + ("23:24:23", 6, 1, "23:24:24"), + ("23:24", 1, 1, "00:24"), + ("23:24", 3, 1, "23:25"), + ]; + + for (original, cursor, amount, expected) in tests { + let rope = Rope::from_str(original); + let range = Range::new(cursor, cursor + 1); + assert_eq!( + DateTimeIncrementor::from_range(rope.slice(..), range) + .unwrap() + .increment(amount) + .1, + expected.into() + ); + } + } + + #[test] + fn test_invalid_date_times() { + 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", + "123:456:789", + "11:61", + "2021-55-12 08:12:54", + ]; + + for invalid in tests { + let rope = Rope::from_str(invalid); + let range = Range::new(0, 1); + + assert_eq!(DateTimeIncrementor::from_range(rope.slice(..), range), None) + } + } +} diff --git a/helix-core/src/increment/mod.rs b/helix-core/src/increment/mod.rs index 71a1f183..f5945774 100644 --- a/helix-core/src/increment/mod.rs +++ b/helix-core/src/increment/mod.rs @@ -1,4 +1,4 @@ -pub mod date; +pub mod date_time; pub mod number; use crate::{Range, Tendril}; diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 6329dec7..4869a135 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1,7 +1,7 @@ use helix_core::{ comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, history::UndoKind, - increment::date::DateIncrementor, + increment::date_time::DateTimeIncrementor, increment::{number::NumberIncrementor, Increment}, indent, indent::IndentStyle, @@ -5804,7 +5804,7 @@ fn increment_impl(cx: &mut Context, amount: i64) { let changes = selection.ranges().iter().filter_map(|range| { let incrementor: Option> = if let Some(incrementor) = - DateIncrementor::from_range(text.slice(..), *range) + DateTimeIncrementor::from_range(text.slice(..), *range) { Some(Box::new(incrementor)) } else if let Some(incrementor) = NumberIncrementor::from_range(text.slice(..), *range) { -- cgit v1.2.3-70-g09d2 From febee2dc0c20cab360f75c2088c8f59b13a12e95 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Sun, 28 Nov 2021 09:40:33 -0700 Subject: No need to clone format --- helix-core/src/increment/date_time.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/increment/date_time.rs b/helix-core/src/increment/date_time.rs index 39380104..195bc14b 100644 --- a/helix-core/src/increment/date_time.rs +++ b/helix-core/src/increment/date_time.rs @@ -13,7 +13,7 @@ use crate::{Range, Tendril}; pub struct DateTimeIncrementor { date_time: NaiveDateTime, range: Range, - format: Format, + fmt: &'static str, field: DateField, } @@ -97,7 +97,7 @@ impl DateTimeIncrementor { Some(DateTimeIncrementor { date_time, range, - format: format.clone(), + fmt: format.fmt, field, }) }) @@ -117,10 +117,7 @@ impl Increment for DateTimeIncrementor { } .unwrap_or(self.date_time); - ( - self.range, - date_time.format(self.format.fmt).to_string().into(), - ) + (self.range, date_time.format(self.fmt).to_string().into()) } } -- cgit v1.2.3-70-g09d2 From c74cd48f38863b3004eaf2b52336ed0597337044 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Sun, 28 Nov 2021 10:58:52 -0700 Subject: Cleanup --- helix-core/src/increment/date_time.rs | 104 +++++++++++++++++----------------- helix-term/src/commands.rs | 10 ++-- 2 files changed, 57 insertions(+), 57 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/increment/date_time.rs b/helix-core/src/increment/date_time.rs index 195bc14b..39646a17 100644 --- a/helix-core/src/increment/date_time.rs +++ b/helix-core/src/increment/date_time.rs @@ -67,27 +67,16 @@ impl DateTimeIncrementor { let has_date = format.fields.iter().any(|f| f.unit.is_date()); let has_time = format.fields.iter().any(|f| f.unit.is_time()); + let date_time = &text[date_time.start()..date_time.end()]; let date_time = match (has_date, has_time) { - (true, true) => NaiveDateTime::parse_from_str( - &text[date_time.start()..date_time.end()], - format.fmt, - ) - .ok()?, + (true, true) => NaiveDateTime::parse_from_str(date_time, format.fmt).ok()?, (true, false) => { - let date = NaiveDate::parse_from_str( - &text[date_time.start()..date_time.end()], - format.fmt, - ) - .ok()?; + let date = NaiveDate::parse_from_str(date_time, format.fmt).ok()?; date.and_hms(0, 0, 0) } (false, true) => { - let time = NaiveTime::parse_from_str( - &text[date_time.start()..date_time.end()], - format.fmt, - ) - .ok()?; + let time = NaiveTime::parse_from_str(date_time, format.fmt).ok()?; NaiveDate::from_ymd(0, 1, 1).and_time(time) } @@ -123,22 +112,22 @@ impl Increment for DateTimeIncrementor { static FORMATS: Lazy> = Lazy::new(|| { vec![ - Format::new("%Y-%m-%d %H:%M:%S"), // 2021-11-24 07:12:23 - Format::new("%Y/%m/%d %H:%M:%S"), // 2021/11/24 07:12:23 - Format::new("%Y-%m-%d %H:%M"), // 2021-11-24 07:12 - Format::new("%Y/%m/%d %H:%M"), // 2021/11/24 07:12 - Format::new("%Y-%m-%d"), // 2021-11-24 - Format::new("%Y/%m/%d"), // 2021/11/24 - Format::new("%a %b %d %Y"), // Wed Nov 24 2021 - Format::new("%d-%b-%Y"), // 24-Nov-2021 - Format::new("%Y %b %d"), // 2021 Nov 24 - Format::new("%b %d, %Y"), // Nov 24, 2021 - Format::new("%-I:%M:%S %P"), // 7:21:53 am - Format::new("%-I:%M %P"), // 7:21 am - Format::new("%-I:%M:%S %p"), // 7:21:53 AM - Format::new("%-I:%M %p"), // 7:21 AM - Format::new("%H:%M:%S"), // 23:24:23 - Format::new("%H:%M"), // 23:24 + Format::new("%Y-%m-%d %H:%M:%S").unwrap(), // 2021-11-24 07:12:23 + Format::new("%Y/%m/%d %H:%M:%S").unwrap(), // 2021/11/24 07:12:23 + Format::new("%Y-%m-%d %H:%M").unwrap(), // 2021-11-24 07:12 + Format::new("%Y/%m/%d %H:%M").unwrap(), // 2021/11/24 07:12 + Format::new("%Y-%m-%d").unwrap(), // 2021-11-24 + Format::new("%Y/%m/%d").unwrap(), // 2021/11/24 + Format::new("%a %b %d %Y").unwrap(), // Wed Nov 24 2021 + Format::new("%d-%b-%Y").unwrap(), // 24-Nov-2021 + Format::new("%Y %b %d").unwrap(), // 2021 Nov 24 + Format::new("%b %d, %Y").unwrap(), // Nov 24, 2021 + Format::new("%-I:%M:%S %P").unwrap(), // 7:21:53 am + Format::new("%-I:%M %P").unwrap(), // 7:21 am + Format::new("%-I:%M:%S %p").unwrap(), // 7:21:53 AM + Format::new("%-I:%M %p").unwrap(), // 7:21 AM + Format::new("%H:%M:%S").unwrap(), // 23:24:23 + Format::new("%H:%M").unwrap(), // 23:24 ] }); @@ -151,55 +140,65 @@ struct Format { } impl Format { - fn new(fmt: &'static str) -> Self { + fn new(fmt: &'static str) -> Result { let mut remaining = fmt; let mut fields = Vec::new(); let mut regex = String::new(); let mut max_len = 0; while let Some(i) = remaining.find('%') { - let mut chars = remaining[i + 1..].chars(); - let spec_len = if let Some(c) = chars.next() { - if c == '-' { - if chars.next().is_some() { - 2 - } else { - 0 - } + let after = &remaining[i + 1..]; + let mut chars = after.chars(); + let c = chars + .next() + .ok_or(FormatError::UnexpectedEndOfFormatString)?; + + let spec_len = if c == '-' { + if let Some(c) = chars.next() { + 1 + c.len_utf8() } else { - 1 + return Err(FormatError::UnexpectedEndOfFormatString); } } else { - 0 + c.len_utf8() }; if i < remaining.len() - spec_len { - let specifier = &remaining[i + 1..i + 1 + spec_len]; + let specifier = &after[..spec_len]; if let Some(field) = DateField::from_specifier(specifier) { fields.push(field); max_len += field.max_len + remaining[..i].len(); regex += &remaining[..i]; regex += &format!("({})", field.regex); - remaining = &remaining[i + spec_len + 1..]; + remaining = &after[spec_len..]; } else { - regex += &remaining[..=i]; + return Err(FormatError::UnsupportedSpecifier( + &remaining[i..i + 1 + spec_len], + )); } } else { - regex += remaining; + return Err(FormatError::UnexpectedEndOfFormatString); } } - let regex = Regex::new(®ex).unwrap(); + let regex = Regex::new(®ex).map_err(FormatError::Regex)?; - Self { + Ok(Self { fmt, fields, regex, max_len, - } + }) } } +#[derive(Clone, Debug)] +enum FormatError { + UnexpectedEndOfFormatString, + UnsupportedSpecifier(&'static str), + Regex(regex::Error), +} + impl PartialEq for Format { fn eq(&self, other: &Self) -> bool { self.fmt == other.fmt && self.fields == other.fields && self.max_len == other.max_len @@ -348,10 +347,11 @@ fn add_months(date_time: NaiveDateTime, amount: i64) -> Option { // Normalize month let month = month % 12; let month = if month.is_negative() { - month + 13 + month + 12 } else { - month + 1 - } as u32; + month + } as u32 + + 1; let day = cmp::min(date_time.day(), ndays_in_month(year, month)); diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 4869a135..c89c81db 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -5803,17 +5803,17 @@ fn increment_impl(cx: &mut Context, amount: i64) { let text = doc.text(); let changes = selection.ranges().iter().filter_map(|range| { - let incrementor: Option> = if let Some(incrementor) = + let incrementor: Box = if let Some(incrementor) = DateTimeIncrementor::from_range(text.slice(..), *range) { - Some(Box::new(incrementor)) + Box::new(incrementor) } else if let Some(incrementor) = NumberIncrementor::from_range(text.slice(..), *range) { - Some(Box::new(incrementor)) + Box::new(incrementor) } else { - None + return None; }; - let (range, new_text) = incrementor?.increment(amount); + let (range, new_text) = incrementor.increment(amount); Some((range.from(), range.to(), Some(new_text))) }); -- cgit v1.2.3-70-g09d2 From 584a31cd9047ce8f93b9eab9233c1b62a9278054 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Mon, 29 Nov 2021 18:19:51 -0700 Subject: Used checked_add for years and months --- helix-core/src/increment/date_time.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/increment/date_time.rs b/helix-core/src/increment/date_time.rs index 39646a17..e4227386 100644 --- a/helix-core/src/increment/date_time.rs +++ b/helix-core/src/increment/date_time.rs @@ -340,7 +340,7 @@ fn ndays_in_month(year: i32, month: u32) -> u32 { } fn add_months(date_time: NaiveDateTime, amount: i64) -> Option { - let month = date_time.month0() as i64 + amount; + let month = (date_time.month0() as i64).checked_add(amount)?; let year = date_time.year() + i32::try_from(month / 12).ok()?; let year = if month.is_negative() { year - 1 } else { year }; @@ -359,7 +359,7 @@ fn add_months(date_time: NaiveDateTime, amount: i64) -> Option { } fn add_years(date_time: NaiveDateTime, amount: i64) -> Option { - let year = i32::try_from(date_time.year() as i64 + amount).ok()?; + let year = i32::try_from((date_time.year() as i64).checked_add(amount)?).ok()?; let ndays = ndays_in_month(year, date_time.month()); if date_time.day() > ndays { -- cgit v1.2.3-70-g09d2 From 0b7911d9217f63b0e110923fcddd4534e4d9da38 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Sat, 4 Dec 2021 11:30:05 -0700 Subject: Remove `FormatError` --- helix-core/src/increment/date_time.rs | 80 +++++++++++++---------------------- 1 file changed, 29 insertions(+), 51 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/increment/date_time.rs b/helix-core/src/increment/date_time.rs index e4227386..80f2bf53 100644 --- a/helix-core/src/increment/date_time.rs +++ b/helix-core/src/increment/date_time.rs @@ -112,22 +112,22 @@ impl Increment for DateTimeIncrementor { static FORMATS: Lazy> = Lazy::new(|| { vec![ - Format::new("%Y-%m-%d %H:%M:%S").unwrap(), // 2021-11-24 07:12:23 - Format::new("%Y/%m/%d %H:%M:%S").unwrap(), // 2021/11/24 07:12:23 - Format::new("%Y-%m-%d %H:%M").unwrap(), // 2021-11-24 07:12 - Format::new("%Y/%m/%d %H:%M").unwrap(), // 2021/11/24 07:12 - Format::new("%Y-%m-%d").unwrap(), // 2021-11-24 - Format::new("%Y/%m/%d").unwrap(), // 2021/11/24 - Format::new("%a %b %d %Y").unwrap(), // Wed Nov 24 2021 - Format::new("%d-%b-%Y").unwrap(), // 24-Nov-2021 - Format::new("%Y %b %d").unwrap(), // 2021 Nov 24 - Format::new("%b %d, %Y").unwrap(), // Nov 24, 2021 - Format::new("%-I:%M:%S %P").unwrap(), // 7:21:53 am - Format::new("%-I:%M %P").unwrap(), // 7:21 am - Format::new("%-I:%M:%S %p").unwrap(), // 7:21:53 AM - Format::new("%-I:%M %p").unwrap(), // 7:21 AM - Format::new("%H:%M:%S").unwrap(), // 23:24:23 - Format::new("%H:%M").unwrap(), // 23:24 + Format::new("%Y-%m-%d %H:%M:%S"), // 2021-11-24 07:12:23 + Format::new("%Y/%m/%d %H:%M:%S"), // 2021/11/24 07:12:23 + Format::new("%Y-%m-%d %H:%M"), // 2021-11-24 07:12 + Format::new("%Y/%m/%d %H:%M"), // 2021/11/24 07:12 + Format::new("%Y-%m-%d"), // 2021-11-24 + Format::new("%Y/%m/%d"), // 2021/11/24 + Format::new("%a %b %d %Y"), // Wed Nov 24 2021 + Format::new("%d-%b-%Y"), // 24-Nov-2021 + Format::new("%Y %b %d"), // 2021 Nov 24 + Format::new("%b %d, %Y"), // Nov 24, 2021 + Format::new("%-I:%M:%S %P"), // 7:21:53 am + Format::new("%-I:%M %P"), // 7:21 am + Format::new("%-I:%M:%S %p"), // 7:21:53 AM + Format::new("%-I:%M %p"), // 7:21 AM + Format::new("%H:%M:%S"), // 23:24:23 + Format::new("%H:%M"), // 23:24 ] }); @@ -140,7 +140,7 @@ struct Format { } impl Format { - fn new(fmt: &'static str) -> Result { + fn new(fmt: &'static str) -> Self { let mut remaining = fmt; let mut fields = Vec::new(); let mut regex = String::new(); @@ -149,56 +149,34 @@ impl Format { while let Some(i) = remaining.find('%') { let after = &remaining[i + 1..]; let mut chars = after.chars(); - let c = chars - .next() - .ok_or(FormatError::UnexpectedEndOfFormatString)?; + let c = chars.next().unwrap(); let spec_len = if c == '-' { - if let Some(c) = chars.next() { - 1 + c.len_utf8() - } else { - return Err(FormatError::UnexpectedEndOfFormatString); - } + 1 + chars.next().unwrap().len_utf8() } else { c.len_utf8() }; - if i < remaining.len() - spec_len { - let specifier = &after[..spec_len]; - if let Some(field) = DateField::from_specifier(specifier) { - fields.push(field); - max_len += field.max_len + remaining[..i].len(); - regex += &remaining[..i]; - regex += &format!("({})", field.regex); - remaining = &after[spec_len..]; - } else { - return Err(FormatError::UnsupportedSpecifier( - &remaining[i..i + 1 + spec_len], - )); - } - } else { - return Err(FormatError::UnexpectedEndOfFormatString); - } + let specifier = &after[..spec_len]; + let field = DateField::from_specifier(specifier).unwrap(); + fields.push(field); + max_len += field.max_len + remaining[..i].len(); + regex += &remaining[..i]; + regex += &format!("({})", field.regex); + remaining = &after[spec_len..]; } - let regex = Regex::new(®ex).map_err(FormatError::Regex)?; + let regex = Regex::new(®ex).unwrap(); - Ok(Self { + Self { fmt, fields, regex, max_len, - }) + } } } -#[derive(Clone, Debug)] -enum FormatError { - UnexpectedEndOfFormatString, - UnsupportedSpecifier(&'static str), - Regex(regex::Error), -} - impl PartialEq for Format { fn eq(&self, other: &Self) -> bool { self.fmt == other.fmt && self.fields == other.fields && self.max_len == other.max_len -- cgit v1.2.3-70-g09d2 From 539c27e3f52a7c2744db7e92a45d7185eeeba917 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Sat, 4 Dec 2021 11:31:21 -0700 Subject: Remove `Clone` derive --- helix-core/src/increment/date_time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'helix-core/src') diff --git a/helix-core/src/increment/date_time.rs b/helix-core/src/increment/date_time.rs index 80f2bf53..e3cfe107 100644 --- a/helix-core/src/increment/date_time.rs +++ b/helix-core/src/increment/date_time.rs @@ -131,7 +131,7 @@ static FORMATS: Lazy> = Lazy::new(|| { ] }); -#[derive(Clone, Debug)] +#[derive(Debug)] struct Format { fmt: &'static str, fields: Vec, -- cgit v1.2.3-70-g09d2 From a78b7894066b6ccf56c404b7543b45e2dfd99982 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Mon, 22 Nov 2021 00:25:08 +0530 Subject: Auto generate docs for language support --- Cargo.lock | 2 + book/src/SUMMARY.md | 1 + book/src/generated/lang-support.md | 41 +++++++ book/src/generated/typable-cmd.md | 2 +- book/src/lang-support.md | 10 ++ docs/CONTRIBUTING.md | 2 +- helix-core/src/indent.rs | 1 + helix-core/src/syntax.rs | 3 +- languages.toml | 40 +++++++ xtask/Cargo.toml | 2 + xtask/src/main.rs | 229 ++++++++++++++++++++++++++++++++++--- 11 files changed, 311 insertions(+), 22 deletions(-) create mode 100644 book/src/generated/lang-support.md create mode 100644 book/src/lang-support.md (limited to 'helix-core/src') diff --git a/Cargo.lock b/Cargo.lock index b3032465..25b4f6cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1264,5 +1264,7 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" name = "xtask" version = "0.5.0" dependencies = [ + "helix-core", "helix-term", + "toml", ] diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 4da79925..a8f165c0 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -4,6 +4,7 @@ - [Usage](./usage.md) - [Keymap](./keymap.md) - [Commands](./commands.md) + - [Language Support](./lang-support.md) - [Migrating from Vim](./from-vim.md) - [Configuration](./configuration.md) - [Themes](./themes.md) diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md new file mode 100644 index 00000000..729801ad --- /dev/null +++ b/book/src/generated/lang-support.md @@ -0,0 +1,41 @@ +| Language | Syntax Highlighting | Treesitter Textobjects | Auto Indent | Default LSP | +| --- | --- | --- | --- | --- | +| Bash | ✓ | | | `bash-language-server` | +| C | ✓ | | | `clangd` | +| C# | ✓ | | | | +| CMake | ✓ | | | `cmake-language-server` | +| C++ | ✓ | | | `clangd` | +| CSS | ✓ | | | | +| Elixir | ✓ | | | `elixir-ls` | +| GLSL | ✓ | | ✓ | | +| Go | ✓ | ✓ | ✓ | `gopls` | +| HTML | ✓ | | | | +| Java | ✓ | | | | +| JavaScript | ✓ | | ✓ | | +| JSON | ✓ | | ✓ | | +| Julia | ✓ | | | `julia` | +| LaTeX | ✓ | | | | +| Ledger | ✓ | | | | +| LLVM | ✓ | | | | +| Lua | ✓ | | ✓ | | +| Mint | | | | `mint` | +| Nix | ✓ | | ✓ | `rnix-lsp` | +| OCaml | ✓ | | ✓ | | +| OCaml-Interface | ✓ | | | | +| Perl | ✓ | ✓ | | | +| PHP | ✓ | | ✓ | | +| Prolog | | | | `swipl` | +| Protobuf | ✓ | | ✓ | | +| Python | ✓ | ✓ | ✓ | `pylsp` | +| Racket | | | | `racket` | +| Ruby | ✓ | | | `solargraph` | +| Rust | ✓ | ✓ | ✓ | `rust-analyzer` | +| Svelte | ✓ | | ✓ | `svelteserver` | +| TOML | ✓ | | | | +| TSQ | ✓ | | | | +| TSX | ✓ | | | `typescript-language-server` | +| TypeScript | ✓ | | ✓ | `typescript-language-server` | +| Vue | ✓ | | | | +| WGSL | ✓ | | | | +| YAML | ✓ | | ✓ | | +| Zig | ✓ | | ✓ | `zls` | diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index 5de5c787..bb21fd6b 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -1,5 +1,5 @@ | Name | Description | -| --- | --- | +| --- | --- | | `:quit`, `:q` | Close the current view. | | `:quit!`, `:q!` | Close the current view forcefully (ignoring unsaved changes). | | `:open`, `:o` | Open a file from disk into the current view. | diff --git a/book/src/lang-support.md b/book/src/lang-support.md new file mode 100644 index 00000000..3920f342 --- /dev/null +++ b/book/src/lang-support.md @@ -0,0 +1,10 @@ +# Language Support + +For more information like arguments passed to default LSP server, +extensions assosciated with a filetype, custom LSP settings, filetype +specific indent settings, etc see the default +[`languages.toml`][languages.toml] file. + +{{#include ./generated/lang-support.md}} + +[languages.toml]: https://github.com/helix-editor/helix/blob/master/languages.toml diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index 7b923db8..bdd771aa 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -23,7 +23,7 @@ like the list of `:commands` and supported languages. To generate these files, run ```shell -cargo xtask bookgen +cargo xtask docgen ``` inside the project. We use [xtask][xtask] as an ad-hoc task runner and diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index b6f5081a..3ce3620a 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -452,6 +452,7 @@ where file_types: vec!["rs".to_string()], shebangs: vec![], language_id: "Rust".to_string(), + display_name: "Rust".to_string(), highlight_config: OnceCell::new(), config: None, // diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index ba78adaa..3c65ae33 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -50,7 +50,8 @@ pub struct Configuration { #[serde(rename_all = "kebab-case", deny_unknown_fields)] pub struct LanguageConfiguration { #[serde(rename = "name")] - pub language_id: String, + pub language_id: String, // c-sharp, rust + pub display_name: String, // C#, Rust pub scope: String, // source.rust pub file_types: Vec, // filename ends_with? #[serde(default)] diff --git a/languages.toml b/languages.toml index 4208e4b6..ca339c98 100644 --- a/languages.toml +++ b/languages.toml @@ -1,5 +1,6 @@ [[language]] name = "rust" +display-name = "Rust" scope = "source.rust" injection-regex = "rust" file-types = ["rs"] @@ -14,6 +15,7 @@ procMacro = { enable = false } [[language]] name = "toml" +display-name = "TOML" scope = "source.toml" injection-regex = "toml" file-types = ["toml"] @@ -24,6 +26,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "protobuf" +display-name = "Protobuf" scope = "source.proto" injection-regex = "protobuf" file-types = ["proto"] @@ -34,6 +37,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "elixir" +display-name = "Elixir" scope = "source.elixir" injection-regex = "elixir" file-types = ["ex", "exs"] @@ -46,6 +50,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "mint" +display-name = "Mint" scope = "source.mint" injection-regex = "mint" file-types = ["mint"] @@ -58,6 +63,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "json" +display-name = "JSON" scope = "source.json" injection-regex = "json" file-types = ["json"] @@ -67,6 +73,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "c" +display-name = "C" scope = "source.c" injection-regex = "c" file-types = ["c"] # TODO: ["h"] @@ -78,6 +85,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "cpp" +display-name = "C++" scope = "source.cpp" injection-regex = "cpp" file-types = ["cc", "hh", "cpp", "hpp", "h", "ipp", "tpp", "cxx", "hxx", "ixx", "txx", "ino"] @@ -89,6 +97,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "c-sharp" +display-name = "C#" scope = "source.csharp" injection-regex = "c-?sharp" file-types = ["cs"] @@ -99,6 +108,7 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "go" +display-name = "Go" scope = "source.go" injection-regex = "go" file-types = ["go"] @@ -112,6 +122,7 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "javascript" +display-name = "JavaScript" scope = "source.js" injection-regex = "^(js|javascript)$" file-types = ["js", "mjs"] @@ -124,6 +135,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "typescript" +display-name = "TypeScript" scope = "source.ts" injection-regex = "^(ts|typescript)$" file-types = ["ts"] @@ -136,6 +148,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "tsx" +display-name = "TSX" scope = "source.tsx" injection-regex = "^(tsx)$" # |typescript file-types = ["tsx"] @@ -147,6 +160,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "css" +display-name = "CSS" scope = "source.css" injection-regex = "css" file-types = ["css"] @@ -156,6 +170,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "html" +display-name = "HTML" scope = "text.html.basic" injection-regex = "html" file-types = ["html"] @@ -165,6 +180,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "python" +display-name = "Python" scope = "source.python" injection-regex = "python" file-types = ["py"] @@ -178,6 +194,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "nix" +display-name = "Nix" scope = "source.nix" injection-regex = "nix" file-types = ["nix"] @@ -190,6 +207,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "ruby" +display-name = "Ruby" scope = "source.ruby" injection-regex = "ruby" file-types = ["rb"] @@ -202,6 +220,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "bash" +display-name = "Bash" scope = "source.bash" injection-regex = "bash" file-types = ["sh", "bash"] @@ -214,6 +233,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "php" +display-name = "PHP" scope = "source.php" injection-regex = "php" file-types = ["php"] @@ -224,6 +244,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "latex" +display-name = "LaTeX" scope = "source.tex" injection-regex = "tex" file-types = ["tex"] @@ -234,6 +255,7 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "julia" +display-name = "Julia" scope = "source.julia" injection-regex = "julia" file-types = ["jl"] @@ -259,6 +281,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "java" +display-name = "Java" scope = "source.java" injection-regex = "java" file-types = ["java"] @@ -267,6 +290,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "ledger" +display-name = "Ledger" scope = "source.ledger" injection-regex = "ledger" file-types = ["ldg", "ledger", "journal"] @@ -276,6 +300,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "ocaml" +display-name = "OCaml" scope = "source.ocaml" injection-regex = "ocaml" file-types = ["ml"] @@ -286,6 +311,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "ocaml-interface" +display-name = "OCaml-Interface" scope = "source.ocaml.interface" file-types = ["mli"] shebangs = [] @@ -295,6 +321,7 @@ indent = { tab-width = 2, unit = " "} [[language]] name = "lua" +display-name = "Lua" scope = "source.lua" file-types = ["lua"] shebangs = ["lua"] @@ -304,6 +331,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "svelte" +display-name = "Svelte" scope = "source.svelte" injection-regex = "svelte" file-types = ["svelte"] @@ -314,6 +342,7 @@ language-server = { command = "svelteserver", args = ["--stdio"] } [[language]] name = "vue" +display-name = "Vue" scope = "source.vue" injection-regex = "vue" file-types = ["vue"] @@ -322,6 +351,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "yaml" +display-name = "YAML" scope = "source.yaml" file-types = ["yml", "yaml"] roots = [] @@ -330,6 +360,7 @@ indent = { tab-width = 2, unit = " " } # [[language]] # name = "haskell" +# display-name = "Haskell" # scope = "source.haskell" # injection-regex = "haskell" # file-types = ["hs"] @@ -340,6 +371,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "zig" +display-name = "Zig" scope = "source.zig" injection-regex = "zig" file-types = ["zig"] @@ -352,6 +384,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "prolog" +display-name = "Prolog" scope = "source.prolog" roots = [] file-types = ["pl", "prolog"] @@ -365,6 +398,7 @@ language-server = { command = "swipl", args = [ [[language]] name = "tsq" +display-name = "TSQ" scope = "source.tsq" file-types = ["scm"] roots = [] @@ -373,6 +407,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "cmake" +display-name = "CMake" scope = "source.cmake" file-types = ["cmake", "CMakeLists.txt"] roots = [] @@ -382,6 +417,7 @@ language-server = { command = "cmake-language-server" } [[language]] name = "glsl" +display-name = "GLSL" scope = "source.glsl" file-types = ["glsl", "vert", "tesc", "tese", "geom", "frag", "comp" ] roots = [] @@ -390,6 +426,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "perl" +display-name = "Perl" scope = "source.perl" file-types = ["pl", "pm"] shebangs = ["perl"] @@ -399,6 +436,7 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "racket" +display-name = "Racket" scope = "source.rkt" roots = [] file-types = ["rkt"] @@ -408,6 +446,7 @@ language-server = { command = "racket", args = ["-l", "racket-langserver"] } [[language]] name = "wgsl" +display-name = "WGSL" scope = "source.wgsl" file-types = ["wgsl"] roots = [] @@ -416,6 +455,7 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "llvm" +display-name = "LLVM" scope = "source.llvm" roots = [] file-types = ["ll"] diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index cb890de9..fe5d55d4 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" [dependencies] helix-term = { version = "0.5", path = "../helix-term" } +helix-core = { version = "0.5", path = "../helix-core" } +toml = "0.5" diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 4bf0ae9f..37e70592 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,17 +1,138 @@ -use std::env; +use std::{env, error::Error}; + +type DynError = Box; + +pub mod helpers { + use std::{ + fmt::Display, + path::{Path, PathBuf}, + }; + + use crate::path; + use helix_core::syntax::Configuration as LangConfig; + + #[derive(Copy, Clone)] + pub enum TsFeature { + Highlight, + TextObjects, + AutoIndent, + } + + impl TsFeature { + pub fn all() -> &'static [Self] { + &[Self::Highlight, Self::TextObjects, Self::AutoIndent] + } + + pub fn runtime_filename(&self) -> &'static str { + match *self { + Self::Highlight => "highlights.scm", + Self::TextObjects => "textobjects.scm", + Self::AutoIndent => "indents.toml", + } + } + } + + impl Display for TsFeature { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match *self { + Self::Highlight => "Syntax Highlighting", + Self::TextObjects => "Treesitter Textobjects", + Self::AutoIndent => "Auto Indent", + } + ) + } + } + + /// Get the list of languages that support a particular tree-sitter + /// based feature. + pub fn ts_lang_support(feat: TsFeature) -> Vec { + let queries_dir = path::ts_queries(); + + find_files(&queries_dir, feat.runtime_filename()) + .iter() + .map(|f| { + // .../helix/runtime/queries/python/highlights.scm + let tail = f.strip_prefix(&queries_dir).unwrap(); // python/highlights.scm + let lang = tail.components().next().unwrap(); // python + lang.as_os_str().to_string_lossy().to_string() + }) + .collect() + } + + /// Get the list of languages that have any form of tree-sitter + /// queries defined in the runtime directory. + pub fn langs_with_ts_queries() -> Vec { + std::fs::read_dir(path::ts_queries()) + .unwrap() + .filter_map(|entry| { + let entry = entry.ok()?; + entry + .file_type() + .ok()? + .is_dir() + .then(|| entry.file_name().to_string_lossy().to_string()) + }) + .collect() + } + + // naive implementation, but suffices for our needs + pub fn find_files(dir: &Path, filename: &str) -> Vec { + std::fs::read_dir(dir) + .unwrap() + .filter_map(|entry| { + let path = entry.ok()?.path(); + if path.is_dir() { + Some(find_files(&path, filename)) + } else { + (path.file_name()?.to_string_lossy() == filename).then(|| vec![path]) + } + }) + .flatten() + .collect() + } + + pub fn lang_config() -> LangConfig { + let bytes = std::fs::read(path::lang_config()).unwrap(); + toml::from_slice(&bytes).unwrap() + } +} pub mod md_gen { - use super::path; + use crate::DynError; + + use crate::helpers; + use crate::path; use std::fs; use helix_term::commands::cmd::TYPABLE_COMMAND_LIST; pub const TYPABLE_COMMANDS_MD_OUTPUT: &str = "typable-cmd.md"; + pub const LANG_SUPPORT_MD_OUTPUT: &str = "lang-support.md"; + + fn md_table_heading(cols: &[String]) -> String { + let mut header = String::new(); + header += &md_table_row(cols); + header += &md_table_row(&vec!["---".to_string(); cols.len()]); + header + } + + fn md_table_row(cols: &[String]) -> String { + "| ".to_owned() + &cols.join(" | ") + " |\n" + } + + fn md_mono(s: &str) -> String { + format!("`{}`", s) + } - pub fn typable_commands() -> String { + pub fn typable_commands() -> Result { let mut md = String::new(); - md.push_str("| Name | Description |\n"); - md.push_str("| --- | --- |\n"); + md.push_str(&md_table_heading(&[ + "Name".to_owned(), + "Description".to_owned(), + ])); let cmdify = |s: &str| format!("`:{}`", s); @@ -22,11 +143,72 @@ pub mod md_gen { .collect::>() .join(", "); - let entry = format!("| {} | {} |\n", names, cmd.doc); - md.push_str(&entry); + md.push_str(&md_table_row(&[names.to_owned(), cmd.doc.to_owned()])); + } + + Ok(md) + } + + pub fn lang_features() -> Result { + let mut md = String::new(); + let ts_features = helpers::TsFeature::all(); + + let mut cols = vec!["Language".to_owned()]; + cols.append( + &mut ts_features + .iter() + .map(|t| t.to_string()) + .collect::>(), + ); + cols.push("Default LSP".to_owned()); + + md.push_str(&md_table_heading(&cols)); + let config = helpers::lang_config(); + + let mut langs = config + .language + .iter() + .map(|l| l.language_id.clone()) + .collect::>(); + langs.sort_unstable(); + + let mut ts_features_to_langs = Vec::new(); + for &feat in ts_features { + ts_features_to_langs.push((feat, helpers::ts_lang_support(feat))); } - md + let mut row = Vec::new(); + for lang in langs { + let lc = config + .language + .iter() + .find(|l| l.language_id == lang) + .unwrap(); // lang comes from config + row.push(lc.display_name.clone()); + + for (_feat, support_list) in &ts_features_to_langs { + row.push( + if support_list.contains(&lang) { + "✓" + } else { + "" + } + .to_owned(), + ); + } + row.push( + lc.language_server + .as_ref() + .map(|s| s.command.clone()) + .map(|c| md_mono(&c)) + .unwrap_or_default(), + ); + + md.push_str(&md_table_row(&row)); + row.clear(); + } + + Ok(md) } pub fn write(filename: &str, data: &str) { @@ -49,37 +231,46 @@ pub mod path { pub fn book_gen() -> PathBuf { project_root().join("book/src/generated/") } + + pub fn ts_queries() -> PathBuf { + project_root().join("runtime/queries") + } + + pub fn lang_config() -> PathBuf { + project_root().join("languages.toml") + } } pub mod tasks { - use super::md_gen; + use crate::md_gen; + use crate::DynError; - pub fn bookgen() { - md_gen::write( - md_gen::TYPABLE_COMMANDS_MD_OUTPUT, - &md_gen::typable_commands(), - ); + pub fn docgen() -> Result<(), DynError> { + use md_gen::*; + write(TYPABLE_COMMANDS_MD_OUTPUT, &typable_commands()?); + write(LANG_SUPPORT_MD_OUTPUT, &lang_features()?); + Ok(()) } pub fn print_help() { println!( " -Usage: Run with `cargo xtask `, eg. `cargo xtask bookgen`. +Usage: Run with `cargo xtask `, eg. `cargo xtask docgen`. Tasks: - bookgen: Generate files to be included in the mdbook output. + docgen: Generate files to be included in the mdbook output. " ); } } -fn main() -> Result<(), String> { +fn main() -> Result<(), DynError> { let task = env::args().nth(1); match task { None => tasks::print_help(), Some(t) => match t.as_str() { - "bookgen" => tasks::bookgen(), - invalid => return Err(format!("Invalid task name: {}", invalid)), + "docgen" => tasks::docgen()?, + invalid => return Err(format!("Invalid task name: {}", invalid).into()), }, }; Ok(()) -- cgit v1.2.3-70-g09d2 From d08bdfa838098769afc59146b62f9d613d4a7ff4 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Tue, 7 Dec 2021 21:27:21 +0530 Subject: Use same name used in config files for langs in docs --- book/src/generated/lang-support.md | 78 +++++++++++++++++++------------------- helix-core/src/indent.rs | 1 - helix-core/src/syntax.rs | 1 - languages.toml | 41 -------------------- xtask/src/main.rs | 2 +- 5 files changed, 40 insertions(+), 83 deletions(-) (limited to 'helix-core/src') diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md index 729801ad..96d9b6a0 100644 --- a/book/src/generated/lang-support.md +++ b/book/src/generated/lang-support.md @@ -1,41 +1,41 @@ | Language | Syntax Highlighting | Treesitter Textobjects | Auto Indent | Default LSP | | --- | --- | --- | --- | --- | -| Bash | ✓ | | | `bash-language-server` | -| C | ✓ | | | `clangd` | -| C# | ✓ | | | | -| CMake | ✓ | | | `cmake-language-server` | -| C++ | ✓ | | | `clangd` | -| CSS | ✓ | | | | -| Elixir | ✓ | | | `elixir-ls` | -| GLSL | ✓ | | ✓ | | -| Go | ✓ | ✓ | ✓ | `gopls` | -| HTML | ✓ | | | | -| Java | ✓ | | | | -| JavaScript | ✓ | | ✓ | | -| JSON | ✓ | | ✓ | | -| Julia | ✓ | | | `julia` | -| LaTeX | ✓ | | | | -| Ledger | ✓ | | | | -| LLVM | ✓ | | | | -| Lua | ✓ | | ✓ | | -| Mint | | | | `mint` | -| Nix | ✓ | | ✓ | `rnix-lsp` | -| OCaml | ✓ | | ✓ | | -| OCaml-Interface | ✓ | | | | -| Perl | ✓ | ✓ | | | -| PHP | ✓ | | ✓ | | -| Prolog | | | | `swipl` | -| Protobuf | ✓ | | ✓ | | -| Python | ✓ | ✓ | ✓ | `pylsp` | -| Racket | | | | `racket` | -| Ruby | ✓ | | | `solargraph` | -| Rust | ✓ | ✓ | ✓ | `rust-analyzer` | -| Svelte | ✓ | | ✓ | `svelteserver` | -| TOML | ✓ | | | | -| TSQ | ✓ | | | | -| TSX | ✓ | | | `typescript-language-server` | -| TypeScript | ✓ | | ✓ | `typescript-language-server` | -| Vue | ✓ | | | | -| WGSL | ✓ | | | | -| YAML | ✓ | | ✓ | | -| Zig | ✓ | | ✓ | `zls` | +| bash | ✓ | | | `bash-language-server` | +| c | ✓ | | | `clangd` | +| c-sharp | ✓ | | | | +| cmake | ✓ | | | `cmake-language-server` | +| cpp | ✓ | | | `clangd` | +| css | ✓ | | | | +| elixir | ✓ | | | `elixir-ls` | +| glsl | ✓ | | ✓ | | +| go | ✓ | ✓ | ✓ | `gopls` | +| html | ✓ | | | | +| java | ✓ | | | | +| javascript | ✓ | | ✓ | | +| json | ✓ | | ✓ | | +| julia | ✓ | | | `julia` | +| latex | ✓ | | | | +| ledger | ✓ | | | | +| llvm | ✓ | | | | +| lua | ✓ | | ✓ | | +| mint | | | | `mint` | +| nix | ✓ | | ✓ | `rnix-lsp` | +| ocaml | ✓ | | ✓ | | +| ocaml-interface | ✓ | | | | +| perl | ✓ | ✓ | | | +| php | ✓ | | ✓ | | +| prolog | | | | `swipl` | +| protobuf | ✓ | | ✓ | | +| python | ✓ | ✓ | ✓ | `pylsp` | +| racket | | | | `racket` | +| ruby | ✓ | | | `solargraph` | +| rust | ✓ | ✓ | ✓ | `rust-analyzer` | +| svelte | ✓ | | ✓ | `svelteserver` | +| toml | ✓ | | | | +| tsq | ✓ | | | | +| tsx | ✓ | | | `typescript-language-server` | +| typescript | ✓ | | ✓ | `typescript-language-server` | +| vue | ✓ | | | | +| wgsl | ✓ | | | | +| yaml | ✓ | | ✓ | | +| zig | ✓ | | ✓ | `zls` | diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 3ce3620a..b6f5081a 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -452,7 +452,6 @@ where file_types: vec!["rs".to_string()], shebangs: vec![], language_id: "Rust".to_string(), - display_name: "Rust".to_string(), highlight_config: OnceCell::new(), config: None, // diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 3c65ae33..ef35fc75 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -51,7 +51,6 @@ pub struct Configuration { pub struct LanguageConfiguration { #[serde(rename = "name")] pub language_id: String, // c-sharp, rust - pub display_name: String, // C#, Rust pub scope: String, // source.rust pub file_types: Vec, // filename ends_with? #[serde(default)] diff --git a/languages.toml b/languages.toml index ca339c98..428051a7 100644 --- a/languages.toml +++ b/languages.toml @@ -1,6 +1,5 @@ [[language]] name = "rust" -display-name = "Rust" scope = "source.rust" injection-regex = "rust" file-types = ["rs"] @@ -15,7 +14,6 @@ procMacro = { enable = false } [[language]] name = "toml" -display-name = "TOML" scope = "source.toml" injection-regex = "toml" file-types = ["toml"] @@ -26,7 +24,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "protobuf" -display-name = "Protobuf" scope = "source.proto" injection-regex = "protobuf" file-types = ["proto"] @@ -37,7 +34,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "elixir" -display-name = "Elixir" scope = "source.elixir" injection-regex = "elixir" file-types = ["ex", "exs"] @@ -50,7 +46,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "mint" -display-name = "Mint" scope = "source.mint" injection-regex = "mint" file-types = ["mint"] @@ -63,7 +58,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "json" -display-name = "JSON" scope = "source.json" injection-regex = "json" file-types = ["json"] @@ -73,7 +67,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "c" -display-name = "C" scope = "source.c" injection-regex = "c" file-types = ["c"] # TODO: ["h"] @@ -85,7 +78,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "cpp" -display-name = "C++" scope = "source.cpp" injection-regex = "cpp" file-types = ["cc", "hh", "cpp", "hpp", "h", "ipp", "tpp", "cxx", "hxx", "ixx", "txx", "ino"] @@ -97,7 +89,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "c-sharp" -display-name = "C#" scope = "source.csharp" injection-regex = "c-?sharp" file-types = ["cs"] @@ -108,7 +99,6 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "go" -display-name = "Go" scope = "source.go" injection-regex = "go" file-types = ["go"] @@ -122,7 +112,6 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "javascript" -display-name = "JavaScript" scope = "source.js" injection-regex = "^(js|javascript)$" file-types = ["js", "mjs"] @@ -135,7 +124,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "typescript" -display-name = "TypeScript" scope = "source.ts" injection-regex = "^(ts|typescript)$" file-types = ["ts"] @@ -148,7 +136,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "tsx" -display-name = "TSX" scope = "source.tsx" injection-regex = "^(tsx)$" # |typescript file-types = ["tsx"] @@ -160,7 +147,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "css" -display-name = "CSS" scope = "source.css" injection-regex = "css" file-types = ["css"] @@ -170,7 +156,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "html" -display-name = "HTML" scope = "text.html.basic" injection-regex = "html" file-types = ["html"] @@ -180,7 +165,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "python" -display-name = "Python" scope = "source.python" injection-regex = "python" file-types = ["py"] @@ -194,7 +178,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "nix" -display-name = "Nix" scope = "source.nix" injection-regex = "nix" file-types = ["nix"] @@ -207,7 +190,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "ruby" -display-name = "Ruby" scope = "source.ruby" injection-regex = "ruby" file-types = ["rb"] @@ -220,7 +202,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "bash" -display-name = "Bash" scope = "source.bash" injection-regex = "bash" file-types = ["sh", "bash"] @@ -233,7 +214,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "php" -display-name = "PHP" scope = "source.php" injection-regex = "php" file-types = ["php"] @@ -244,7 +224,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "latex" -display-name = "LaTeX" scope = "source.tex" injection-regex = "tex" file-types = ["tex"] @@ -255,7 +234,6 @@ indent = { tab-width = 4, unit = "\t" } [[language]] name = "julia" -display-name = "Julia" scope = "source.julia" injection-regex = "julia" file-types = ["jl"] @@ -271,7 +249,6 @@ language-server = { command = "julia", args = [ using Pkg; import StaticLint; env_path = dirname(Pkg.Types.Context().env.project_file); - server = LanguageServer.LanguageServerInstance(stdin, stdout, env_path, ""); server.runlinter = true; run(server); @@ -281,7 +258,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "java" -display-name = "Java" scope = "source.java" injection-regex = "java" file-types = ["java"] @@ -290,7 +266,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "ledger" -display-name = "Ledger" scope = "source.ledger" injection-regex = "ledger" file-types = ["ldg", "ledger", "journal"] @@ -300,7 +275,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "ocaml" -display-name = "OCaml" scope = "source.ocaml" injection-regex = "ocaml" file-types = ["ml"] @@ -311,7 +285,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "ocaml-interface" -display-name = "OCaml-Interface" scope = "source.ocaml.interface" file-types = ["mli"] shebangs = [] @@ -321,7 +294,6 @@ indent = { tab-width = 2, unit = " "} [[language]] name = "lua" -display-name = "Lua" scope = "source.lua" file-types = ["lua"] shebangs = ["lua"] @@ -331,7 +303,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "svelte" -display-name = "Svelte" scope = "source.svelte" injection-regex = "svelte" file-types = ["svelte"] @@ -342,7 +313,6 @@ language-server = { command = "svelteserver", args = ["--stdio"] } [[language]] name = "vue" -display-name = "Vue" scope = "source.vue" injection-regex = "vue" file-types = ["vue"] @@ -351,7 +321,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "yaml" -display-name = "YAML" scope = "source.yaml" file-types = ["yml", "yaml"] roots = [] @@ -360,7 +329,6 @@ indent = { tab-width = 2, unit = " " } # [[language]] # name = "haskell" -# display-name = "Haskell" # scope = "source.haskell" # injection-regex = "haskell" # file-types = ["hs"] @@ -371,7 +339,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "zig" -display-name = "Zig" scope = "source.zig" injection-regex = "zig" file-types = ["zig"] @@ -384,7 +351,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "prolog" -display-name = "Prolog" scope = "source.prolog" roots = [] file-types = ["pl", "prolog"] @@ -398,7 +364,6 @@ language-server = { command = "swipl", args = [ [[language]] name = "tsq" -display-name = "TSQ" scope = "source.tsq" file-types = ["scm"] roots = [] @@ -407,7 +372,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "cmake" -display-name = "CMake" scope = "source.cmake" file-types = ["cmake", "CMakeLists.txt"] roots = [] @@ -417,7 +381,6 @@ language-server = { command = "cmake-language-server" } [[language]] name = "glsl" -display-name = "GLSL" scope = "source.glsl" file-types = ["glsl", "vert", "tesc", "tese", "geom", "frag", "comp" ] roots = [] @@ -426,7 +389,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "perl" -display-name = "Perl" scope = "source.perl" file-types = ["pl", "pm"] shebangs = ["perl"] @@ -436,7 +398,6 @@ indent = { tab-width = 2, unit = " " } [[language]] name = "racket" -display-name = "Racket" scope = "source.rkt" roots = [] file-types = ["rkt"] @@ -446,7 +407,6 @@ language-server = { command = "racket", args = ["-l", "racket-langserver"] } [[language]] name = "wgsl" -display-name = "WGSL" scope = "source.wgsl" file-types = ["wgsl"] roots = [] @@ -455,7 +415,6 @@ indent = { tab-width = 4, unit = " " } [[language]] name = "llvm" -display-name = "LLVM" scope = "source.llvm" roots = [] file-types = ["ll"] diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 37e70592..7256653a 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -184,7 +184,7 @@ pub mod md_gen { .iter() .find(|l| l.language_id == lang) .unwrap(); // lang comes from config - row.push(lc.display_name.clone()); + row.push(lc.language_id.clone()); for (_feat, support_list) in &ts_features_to_langs { row.push( -- cgit v1.2.3-70-g09d2 From 3156577fbf1a97e07e90e11b51c66155f122c3b7 Mon Sep 17 00:00:00 2001 From: ath3 Date: Sun, 12 Dec 2021 13:13:33 +0100 Subject: Open files with spaces in filename, allow opening multiple files (#1231) --- helix-core/src/lib.rs | 1 + helix-core/src/shellwords.rs | 164 +++++++++++++++++++++++++++++++++++++++++++ helix-term/src/commands.rs | 145 +++++++++++++++++++------------------- 3 files changed, 239 insertions(+), 71 deletions(-) create mode 100644 helix-core/src/shellwords.rs (limited to 'helix-core/src') diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 4ae044cc..92a59f31 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -17,6 +17,7 @@ mod position; pub mod register; pub mod search; pub mod selection; +pub mod shellwords; mod state; pub mod surround; pub mod syntax; diff --git a/helix-core/src/shellwords.rs b/helix-core/src/shellwords.rs new file mode 100644 index 00000000..13f6f3e9 --- /dev/null +++ b/helix-core/src/shellwords.rs @@ -0,0 +1,164 @@ +use std::borrow::Cow; + +/// Get the vec of escaped / quoted / doublequoted filenames from the input str +pub fn shellwords(input: &str) -> Vec> { + enum State { + Normal, + NormalEscaped, + Quoted, + QuoteEscaped, + Dquoted, + DquoteEscaped, + } + + use State::*; + + let mut state = Normal; + let mut args: Vec> = Vec::new(); + let mut escaped = String::with_capacity(input.len()); + + let mut start = 0; + let mut end = 0; + + for (i, c) in input.char_indices() { + state = match state { + Normal => match c { + '\\' => { + escaped.push_str(&input[start..i]); + start = i + 1; + NormalEscaped + } + '"' => { + end = i; + Dquoted + } + '\'' => { + end = i; + Quoted + } + c if c.is_ascii_whitespace() => { + end = i; + Normal + } + _ => Normal, + }, + NormalEscaped => Normal, + Quoted => match c { + '\\' => { + escaped.push_str(&input[start..i]); + start = i + 1; + QuoteEscaped + } + '\'' => { + end = i; + Normal + } + _ => Quoted, + }, + QuoteEscaped => Quoted, + Dquoted => match c { + '\\' => { + escaped.push_str(&input[start..i]); + start = i + 1; + DquoteEscaped + } + '"' => { + end = i; + Normal + } + _ => Dquoted, + }, + DquoteEscaped => Dquoted, + }; + + if i >= input.len() - 1 && end == 0 { + end = i + 1; + } + + if end > 0 { + let esc_trim = escaped.trim(); + let inp = &input[start..end]; + + if !(esc_trim.is_empty() && inp.trim().is_empty()) { + if esc_trim.is_empty() { + args.push(inp.into()); + } else { + args.push([escaped, inp.into()].concat().into()); + escaped = "".to_string(); + } + } + start = i + 1; + end = 0; + } + } + args +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_normal() { + let input = r#":o single_word twó wörds \three\ \"with\ escaping\\"#; + let result = shellwords(input); + let expected = vec![ + Cow::from(":o"), + Cow::from("single_word"), + Cow::from("twó"), + Cow::from("wörds"), + Cow::from(r#"three "with escaping\"#), + ]; + // TODO test is_owned and is_borrowed, once they get stabilized. + assert_eq!(expected, result); + } + + #[test] + fn test_quoted() { + let quoted = + r#":o 'single_word' 'twó wörds' '' ' ''\three\' \"with\ escaping\\' 'quote incomplete"#; + let result = shellwords(quoted); + let expected = vec![ + Cow::from(":o"), + Cow::from("single_word"), + Cow::from("twó wörds"), + Cow::from(r#"three' "with escaping\"#), + Cow::from("quote incomplete"), + ]; + assert_eq!(expected, result); + } + + #[test] + fn test_dquoted() { + let dquoted = r#":o "single_word" "twó wörds" "" " ""\three\' \"with\ escaping\\" "dquote incomplete"#; + let result = shellwords(dquoted); + let expected = vec![ + Cow::from(":o"), + Cow::from("single_word"), + Cow::from("twó wörds"), + Cow::from(r#"three' "with escaping\"#), + Cow::from("dquote incomplete"), + ]; + assert_eq!(expected, result); + } + + #[test] + fn test_mixed() { + let dquoted = r#":o single_word 'twó wörds' "\three\' \"with\ escaping\\""no space before"'and after' $#%^@ "%^&(%^" ')(*&^%''a\\\\\b' '"#; + let result = shellwords(dquoted); + let expected = vec![ + Cow::from(":o"), + Cow::from("single_word"), + Cow::from("twó wörds"), + Cow::from("three' \"with escaping\\"), + Cow::from("no space before"), + Cow::from("and after"), + Cow::from("$#%^@"), + Cow::from("%^&(%^"), + Cow::from(")(*&^%"), + Cow::from(r#"a\\b"#), + //last ' just changes to quoted but since we dont have anything after it, it should be ignored + ]; + assert_eq!(expected, result); + } +} diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 87c5a63f..314cd11f 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -10,7 +10,7 @@ use helix_core::{ movement::{self, Direction}, object, pos_at_coords, regex::{self, Regex, RegexBuilder}, - search, selection, surround, textobject, + search, selection, shellwords, surround, textobject, unicode::width::UnicodeWidthChar, LineEnding, Position, Range, Rope, RopeGraphemes, RopeSlice, Selection, SmallVec, Tendril, Transaction, @@ -173,14 +173,14 @@ impl MappableCommand { pub fn execute(&self, cx: &mut Context) { match &self { MappableCommand::Typable { name, args, doc: _ } => { - let args: Vec<&str> = args.iter().map(|arg| arg.as_str()).collect(); + let args: Vec> = args.iter().map(Cow::from).collect(); if let Some(command) = cmd::TYPABLE_COMMAND_MAP.get(name.as_str()) { let mut cx = compositor::Context { editor: cx.editor, jobs: cx.jobs, scroll: None, }; - if let Err(e) = (command.fun)(&mut cx, &args, PromptEvent::Validate) { + if let Err(e) = (command.fun)(&mut cx, &args[..], PromptEvent::Validate) { cx.editor.set_error(format!("{}", e)); } } @@ -1963,13 +1963,13 @@ pub mod cmd { pub aliases: &'static [&'static str], pub doc: &'static str, // params, flags, helper, completer - pub fun: fn(&mut compositor::Context, &[&str], PromptEvent) -> anyhow::Result<()>, + pub fun: fn(&mut compositor::Context, &[Cow], PromptEvent) -> anyhow::Result<()>, pub completer: Option, } fn quit( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { // last view and we have unsaved changes @@ -1984,7 +1984,7 @@ pub mod cmd { fn force_quit( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { cx.editor.close(view!(cx.editor).id); @@ -1994,17 +1994,19 @@ pub mod cmd { fn open( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { - let path = args.get(0).context("wrong argument count")?; - let _ = cx.editor.open(path.into(), Action::Replace)?; + ensure!(!args.is_empty(), "wrong argument count"); + for arg in args { + let _ = cx.editor.open(arg.as_ref().into(), Action::Replace)?; + } Ok(()) } fn buffer_close( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let view = view!(cx.editor); @@ -2015,7 +2017,7 @@ pub mod cmd { fn force_buffer_close( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let view = view!(cx.editor); @@ -2024,15 +2026,12 @@ pub mod cmd { Ok(()) } - fn write_impl>( - cx: &mut compositor::Context, - path: Option

, - ) -> anyhow::Result<()> { + fn write_impl(cx: &mut compositor::Context, path: Option<&Cow>) -> anyhow::Result<()> { let jobs = &mut cx.jobs; let (_, doc) = current!(cx.editor); if let Some(ref path) = path { - doc.set_path(Some(path.as_ref())) + doc.set_path(Some(path.as_ref().as_ref())) .context("invalid filepath")?; } if doc.path().is_none() { @@ -2061,7 +2060,7 @@ pub mod cmd { fn write( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { write_impl(cx, args.first()) @@ -2069,7 +2068,7 @@ pub mod cmd { fn new_file( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { cx.editor.new_file(Action::Replace); @@ -2079,7 +2078,7 @@ pub mod cmd { fn format( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (_, doc) = current!(cx.editor); @@ -2094,7 +2093,7 @@ pub mod cmd { } fn set_indent_style( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { use IndentStyle::*; @@ -2114,7 +2113,7 @@ pub mod cmd { // Attempt to parse argument as an indent style. let style = match args.get(0) { Some(arg) if "tabs".starts_with(&arg.to_lowercase()) => Some(Tabs), - Some(&"0") => Some(Tabs), + Some(Cow::Borrowed("0")) => Some(Tabs), Some(arg) => arg .parse::() .ok() @@ -2133,7 +2132,7 @@ pub mod cmd { /// Sets or reports the current document's line ending setting. fn set_line_ending( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { use LineEnding::*; @@ -2177,7 +2176,7 @@ pub mod cmd { fn earlier( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let uk = args.join(" ").parse::().map_err(|s| anyhow!(s))?; @@ -2193,7 +2192,7 @@ pub mod cmd { fn later( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let uk = args.join(" ").parse::().map_err(|s| anyhow!(s))?; @@ -2208,7 +2207,7 @@ pub mod cmd { fn write_quit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { write_impl(cx, args.first())?; @@ -2217,7 +2216,7 @@ pub mod cmd { fn force_write_quit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { write_impl(cx, args.first())?; @@ -2248,7 +2247,7 @@ pub mod cmd { fn write_all_impl( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, quit: bool, force: bool, @@ -2284,7 +2283,7 @@ pub mod cmd { fn write_all( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { write_all_impl(cx, args, event, false, false) @@ -2292,7 +2291,7 @@ pub mod cmd { fn write_all_quit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { write_all_impl(cx, args, event, true, false) @@ -2300,7 +2299,7 @@ pub mod cmd { fn force_write_all_quit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { write_all_impl(cx, args, event, true, true) @@ -2308,7 +2307,7 @@ pub mod cmd { fn quit_all_impl( editor: &mut Editor, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, force: bool, ) -> anyhow::Result<()> { @@ -2327,7 +2326,7 @@ pub mod cmd { fn quit_all( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { quit_all_impl(cx.editor, args, event, false) @@ -2335,7 +2334,7 @@ pub mod cmd { fn force_quit_all( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { quit_all_impl(cx.editor, args, event, true) @@ -2343,7 +2342,7 @@ pub mod cmd { fn cquit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let exit_code = args @@ -2362,7 +2361,7 @@ pub mod cmd { fn theme( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let theme = args.first().context("theme not provided")?; @@ -2371,7 +2370,7 @@ pub mod cmd { fn yank_main_selection_to_clipboard( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { yank_main_selection_to_clipboard_impl(cx.editor, ClipboardType::Clipboard) @@ -2379,20 +2378,18 @@ pub mod cmd { fn yank_joined_to_clipboard( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (_, doc) = current!(cx.editor); - let separator = args - .first() - .copied() - .unwrap_or_else(|| doc.line_ending.as_str()); + let default_sep = Cow::Borrowed(doc.line_ending.as_str()); + let separator = args.first().unwrap_or(&default_sep); yank_joined_to_clipboard_impl(cx.editor, separator, ClipboardType::Clipboard) } fn yank_main_selection_to_primary_clipboard( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { yank_main_selection_to_clipboard_impl(cx.editor, ClipboardType::Selection) @@ -2400,20 +2397,18 @@ pub mod cmd { fn yank_joined_to_primary_clipboard( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (_, doc) = current!(cx.editor); - let separator = args - .first() - .copied() - .unwrap_or_else(|| doc.line_ending.as_str()); + let default_sep = Cow::Borrowed(doc.line_ending.as_str()); + let separator = args.first().unwrap_or(&default_sep); yank_joined_to_clipboard_impl(cx.editor, separator, ClipboardType::Selection) } fn paste_clipboard_after( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard) @@ -2421,7 +2416,7 @@ pub mod cmd { fn paste_clipboard_before( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Clipboard) @@ -2429,7 +2424,7 @@ pub mod cmd { fn paste_primary_clipboard_after( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection) @@ -2437,7 +2432,7 @@ pub mod cmd { fn paste_primary_clipboard_before( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { paste_clipboard_impl(cx.editor, Paste::After, ClipboardType::Selection) @@ -2467,7 +2462,7 @@ pub mod cmd { fn replace_selections_with_clipboard( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { replace_selections_with_clipboard_impl(cx, ClipboardType::Clipboard) @@ -2475,7 +2470,7 @@ pub mod cmd { fn replace_selections_with_primary_clipboard( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { replace_selections_with_clipboard_impl(cx, ClipboardType::Selection) @@ -2483,7 +2478,7 @@ pub mod cmd { fn show_clipboard_provider( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { cx.editor @@ -2493,12 +2488,13 @@ pub mod cmd { fn change_current_directory( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let dir = helix_core::path::expand_tilde( args.first() .context("target directory not provided")? + .as_ref() .as_ref(), ); @@ -2516,7 +2512,7 @@ pub mod cmd { fn show_current_directory( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let cwd = std::env::current_dir().context("Couldn't get the new working directory")?; @@ -2528,7 +2524,7 @@ pub mod cmd { /// Sets the [`Document`]'s encoding.. fn set_encoding( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (_, doc) = current!(cx.editor); @@ -2544,7 +2540,7 @@ pub mod cmd { /// Reload the [`Document`] from its source file. fn reload( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (view, doc) = current!(cx.editor); @@ -2553,7 +2549,7 @@ pub mod cmd { fn tree_sitter_scopes( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let (view, doc) = current!(cx.editor); @@ -2567,15 +2563,18 @@ pub mod cmd { fn vsplit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let id = view!(cx.editor).doc; - if let Some(path) = args.get(0) { - cx.editor.open(path.into(), Action::VerticalSplit)?; - } else { + if args.is_empty() { cx.editor.switch(id, Action::VerticalSplit); + } else { + for arg in args { + cx.editor + .open(PathBuf::from(arg.as_ref()), Action::VerticalSplit)?; + } } Ok(()) @@ -2583,15 +2582,18 @@ pub mod cmd { fn hsplit( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let id = view!(cx.editor).doc; - if let Some(path) = args.get(0) { - cx.editor.open(path.into(), Action::HorizontalSplit)?; - } else { + if args.is_empty() { cx.editor.switch(id, Action::HorizontalSplit); + } else { + for arg in args { + cx.editor + .open(PathBuf::from(arg.as_ref()), Action::HorizontalSplit)?; + } } Ok(()) @@ -2599,7 +2601,7 @@ pub mod cmd { fn tutor( cx: &mut compositor::Context, - _args: &[&str], + _args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { let path = helix_core::runtime_dir().join("tutor.txt"); @@ -2611,7 +2613,7 @@ pub mod cmd { pub(super) fn goto_line_number( cx: &mut compositor::Context, - args: &[&str], + args: &[Cow], _event: PromptEvent, ) -> anyhow::Result<()> { ensure!(!args.is_empty(), "Line number required"); @@ -2980,7 +2982,7 @@ fn command_mode(cx: &mut Context) { // If command is numeric, interpret as line number and go there. if parts.len() == 1 && parts[0].parse::().ok().is_some() { - if let Err(e) = cmd::goto_line_number(cx, &parts[0..], event) { + if let Err(e) = cmd::goto_line_number(cx, &[Cow::from(parts[0])], event) { cx.editor.set_error(format!("{}", e)); } return; @@ -2988,7 +2990,8 @@ fn command_mode(cx: &mut Context) { // Handle typable commands if let Some(cmd) = cmd::TYPABLE_COMMAND_MAP.get(parts[0]) { - if let Err(e) = (cmd.fun)(cx, &parts[1..], event) { + let args = shellwords::shellwords(input); + if let Err(e) = (cmd.fun)(cx, &args[1..], event) { cx.editor.set_error(format!("{}", e)); } } else { -- cgit v1.2.3-70-g09d2 From 94535fa013469abf6e5ab2fa52f3deced9d46de0 Mon Sep 17 00:00:00 2001 From: Skyler Hawthorne Date: Mon, 13 Dec 2021 10:58:58 -0500 Subject: Add auto pairs for same-char pairs (#1219) * Add auto pairs for same-char pairs * Add unit tests for all existing functionality * Add auto pairs for same-char pairs (quotes, etc). Account for apostrophe in prose by requiring both sides of the cursor to be non-pair chars or whitespace. This also incidentally will work for avoiding a double single quote in lifetime annotations, at least until <> is added * Slight factor of moving the cursor transform of the selection to inside the hooks. This will enable doing auto pairing with selections, and fixing the bug where auto pairs destroy the selection. Fixes #1014--- helix-core/src/auto_pairs.rs | 421 ++++++++++++++++++++++++++++++++++-------- helix-core/src/transaction.rs | 2 +- helix-term/src/commands.rs | 7 +- 3 files changed, 351 insertions(+), 79 deletions(-) (limited to 'helix-core/src') diff --git a/helix-core/src/auto_pairs.rs b/helix-core/src/auto_pairs.rs index cc966852..c037afef 100644 --- a/helix-core/src/auto_pairs.rs +++ b/helix-core/src/auto_pairs.rs @@ -2,6 +2,7 @@ //! this module provides the functionality to insert the paired closing character. use crate::{Range, Rope, Selection, Tendril, Transaction}; +use log::debug; use smallvec::SmallVec; // Heavily based on https://github.com/codemirror/closebrackets/ @@ -15,7 +16,9 @@ pub const PAIRS: &[(char, char)] = &[ ('`', '`'), ]; -const CLOSE_BEFORE: &str = ")]}'\":;> \n\r\u{000B}\u{000C}\u{0085}\u{2028}\u{2029}"; // includes space and newlines +// [TODO] build this dynamically in language config. see #992 +const OPEN_BEFORE: &str = "([{'\":;,> \n\r\u{000B}\u{000C}\u{0085}\u{2028}\u{2029}"; +const CLOSE_BEFORE: &str = ")]}'\":;,> \n\r\u{000B}\u{000C}\u{0085}\u{2028}\u{2029}"; // includes space and newlines // insert hook: // Fn(doc, selection, char) => Option @@ -25,40 +28,44 @@ const CLOSE_BEFORE: &str = ")]}'\":;> \n\r\u{000B}\u{000C}\u{0085}\u{2028}\u{202 // // to simplify, maybe return Option and just reimplement the default -// TODO: delete implementation where it erases the whole bracket (|) -> | +// [TODO] +// * delete implementation where it erases the whole bracket (|) -> | +// * do not reduce to cursors; use whole selections, and surround with pair +// * change to multi character pairs to handle cases like placing the cursor in the +// middle of triple quotes, and more exotic pairs like Jinja's {% %} #[must_use] pub fn hook(doc: &Rope, selection: &Selection, ch: char) -> Option { + debug!("autopairs hook selection: {:#?}", selection); + + let cursors = selection.clone().cursors(doc.slice(..)); + for &(open, close) in PAIRS { if open == ch { if open == close { - return handle_same(doc, selection, open); + return Some(handle_same(doc, &cursors, open, CLOSE_BEFORE, OPEN_BEFORE)); } else { - return Some(handle_open(doc, selection, open, close, CLOSE_BEFORE)); + return Some(handle_open(doc, &cursors, open, close, CLOSE_BEFORE)); } } if close == ch { // && char_at pos == close - return Some(handle_close(doc, selection, open, close)); + return Some(handle_close(doc, &cursors, open, close)); } } None } -// TODO: special handling for lifetimes in rust: if preceeded by & or < don't auto close ' -// for example "&'a mut", or "fn<'a>" - -fn next_char(doc: &Rope, pos: usize) -> Option { - if pos >= doc.len_chars() { +fn prev_char(doc: &Rope, pos: usize) -> Option { + if pos == 0 { return None; } - Some(doc.char(pos)) + + doc.get_char(pos - 1) } -// TODO: selections should be extended if range, moved if point. -// TODO: if not cursor but selection, wrap on both sides of selection (surround) fn handle_open( doc: &Rope, selection: &Selection, @@ -66,98 +73,362 @@ fn handle_open( close: char, close_before: &str, ) -> Transaction { - let mut ranges = SmallVec::with_capacity(selection.len()); + let mut end_ranges = SmallVec::with_capacity(selection.len()); let mut offs = 0; - let transaction = Transaction::change_by_selection(doc, selection, |range| { - let pos = range.head; - let next = next_char(doc, pos); + let transaction = Transaction::change_by_selection(doc, selection, |start_range| { + let start_head = start_range.head; - let head = pos + offs + open.len_utf8(); - // if selection, retain anchor, if cursor, move over - ranges.push(Range::new( - if range.is_empty() { - head - } else { - range.anchor + offs - }, - head, - )); + let next = doc.get_char(start_head); + let end_head = start_head + offs + open.len_utf8(); + + let end_anchor = if start_range.is_empty() { + end_head + } else { + start_range.anchor + offs + }; + + end_ranges.push(Range::new(end_anchor, end_head)); match next { Some(ch) if !close_before.contains(ch) => { - offs += 1; - // TODO: else return (use default handler that inserts open) - (pos, pos, Some(Tendril::from_char(open))) + offs += open.len_utf8(); + (start_head, start_head, Some(Tendril::from_char(open))) } // None | Some(ch) if close_before.contains(ch) => {} _ => { // insert open & close - let mut pair = Tendril::with_capacity(2); - pair.push_char(open); - pair.push_char(close); - - offs += 2; - - (pos, pos, Some(pair)) + let pair = Tendril::from_iter([open, close]); + offs += open.len_utf8() + close.len_utf8(); + (start_head, start_head, Some(pair)) } } }); - transaction.with_selection(Selection::new(ranges, selection.primary_index())) + let t = transaction.with_selection(Selection::new(end_ranges, selection.primary_index())); + debug!("auto pair transaction: {:#?}", t); + t } fn handle_close(doc: &Rope, selection: &Selection, _open: char, close: char) -> Transaction { - let mut ranges = SmallVec::with_capacity(selection.len()); + let mut end_ranges = SmallVec::with_capacity(selection.len()); let mut offs = 0; - let transaction = Transaction::change_by_selection(doc, selection, |range| { - let pos = range.head; - let next = next_char(doc, pos); + let transaction = Transaction::change_by_selection(doc, selection, |start_range| { + let start_head = start_range.head; + let next = doc.get_char(start_head); + let end_head = start_head + offs + close.len_utf8(); - let head = pos + offs + close.len_utf8(); - // if selection, retain anchor, if cursor, move over - ranges.push(Range::new( - if range.is_empty() { - head - } else { - range.anchor + offs - }, - head, - )); + let end_anchor = if start_range.is_empty() { + end_head + } else { + start_range.anchor + offs + }; + + end_ranges.push(Range::new(end_anchor, end_head)); if next == Some(close) { - // return transaction that moves past close - (pos, pos, None) // no-op + // return transaction that moves past close + (start_head, start_head, None) // no-op } else { offs += close.len_utf8(); + (start_head, start_head, Some(Tendril::from_char(close))) + } + }); + + transaction.with_selection(Selection::new(end_ranges, selection.primary_index())) +} - // TODO: else return (use default handler that inserts close) - (pos, pos, Some(Tendril::from_char(close))) +/// handle cases where open and close is the same, or in triples ("""docstring""") +fn handle_same( + doc: &Rope, + selection: &Selection, + token: char, + close_before: &str, + open_before: &str, +) -> Transaction { + let mut end_ranges = SmallVec::with_capacity(selection.len()); + + let mut offs = 0; + + let transaction = Transaction::change_by_selection(doc, selection, |start_range| { + let start_head = start_range.head; + let end_head = start_head + offs + token.len_utf8(); + + // if selection, retain anchor, if cursor, move over + let end_anchor = if start_range.is_empty() { + end_head + } else { + start_range.anchor + offs + }; + + end_ranges.push(Range::new(end_anchor, end_head)); + + let next = doc.get_char(start_head); + let prev = prev_char(doc, start_head); + + if next == Some(token) { + // return transaction that moves past close + (start_head, start_head, None) // no-op + } else { + let mut pair = Tendril::with_capacity(2 * token.len_utf8() as u32); + pair.push_char(token); + + // for equal pairs, don't insert both open and close if either + // side has a non-pair char + if (next.is_none() || close_before.contains(next.unwrap())) + && (prev.is_none() || open_before.contains(prev.unwrap())) + { + pair.push_char(token); + } + + offs += pair.len(); + (start_head, start_head, Some(pair)) } }); - transaction.with_selection(Selection::new(ranges, selection.primary_index())) + transaction.with_selection(Selection::new(end_ranges, selection.primary_index())) } -// handle cases where open and close is the same, or in triples ("""docstring""") -fn handle_same(_doc: &Rope, _selection: &Selection, _token: char) -> Option { - // if not cursor but selection, wrap - // let next = next char - - // if next == bracket { - // // if start of syntax node, insert token twice (new pair because node is complete) - // // elseif colsedBracketAt - // // is_triple == allow triple && next 3 is equal - // // cursor jump over - // } - //} else if allow_triple && followed by triple { - //} - //} else if next != word char && prev != bracket && prev != word char { - // // condition checks for cases like I' where you don't want I'' (or I'm) - // insert pair ("") - //} - None +#[cfg(test)] +mod test { + use super::*; + use smallvec::smallvec; + + fn differing_pairs() -> impl Iterator { + PAIRS.iter().filter(|(open, close)| open != close) + } + + fn matching_pairs() -> impl Iterator { + PAIRS.iter().filter(|(open, close)| open == close) + } + + fn test_hooks( + in_doc: &Rope, + in_sel: &Selection, + ch: char, + expected_doc: &Rope, + expected_sel: &Selection, + ) { + let trans = hook(&in_doc, &in_sel, ch).unwrap(); + let mut actual_doc = in_doc.clone(); + assert!(trans.apply(&mut actual_doc)); + assert_eq!(expected_doc, &actual_doc); + assert_eq!(expected_sel, trans.selection().unwrap()); + } + + fn test_hooks_with_pairs( + in_doc: &Rope, + in_sel: &Selection, + pairs: I, + get_expected_doc: F, + actual_sel: &Selection, + ) where + I: IntoIterator, + F: Fn(char, char) -> R, + R: Into, + Rope: From, + { + pairs.into_iter().for_each(|(open, close)| { + test_hooks( + in_doc, + in_sel, + *open, + &Rope::from(get_expected_doc(*open, *close)), + actual_sel, + ) + }); + } + + // [] indicates range + + /// [] -> insert ( -> ([]) + #[test] + fn test_insert_blank() { + test_hooks_with_pairs( + &Rope::new(), + &Selection::single(1, 0), + PAIRS, + |open, close| format!("{}{}", open, close), + &Selection::single(1, 1), + ); + } + + /// [] ([]) + /// [] -> insert -> ([]) + /// [] ([]) + #[test] + fn test_insert_blank_multi_cursor() { + test_hooks_with_pairs( + &Rope::from("\n\n\n"), + &Selection::new( + smallvec!(Range::new(1, 0), Range::new(2, 1), Range::new(3, 2),), + 0, + ), + PAIRS, + |open, close| { + format!( + "{open}{close}\n{open}{close}\n{open}{close}\n", + open = open, + close = close + ) + }, + &Selection::new( + smallvec!(Range::point(1), Range::point(4), Range::point(7),), + 0, + ), + ); + } + + // [TODO] broken until it works with selections + /// fo[o] -> append ( -> fo[o(]) + #[ignore] + #[test] + fn test_append() { + test_hooks_with_pairs( + &Rope::from("foo"), + &Selection::single(2, 4), + PAIRS, + |open, close| format!("foo{}{}", open, close), + &Selection::single(2, 5), + ); + } + + /// ([]) -> insert ) -> ()[] + #[test] + fn test_insert_close_inside_pair() { + for (open, close) in PAIRS { + let doc = Rope::from(format!("{}{}", open, close)); + + test_hooks( + &doc, + &Selection::single(2, 1), + *close, + &doc, + &Selection::point(2), + ); + } + } + + /// ([]) ()[] + /// ([]) -> insert ) -> ()[] + /// ([]) ()[] + #[test] + fn test_insert_close_inside_pair_multi_cursor() { + let sel = Selection::new( + smallvec!(Range::new(2, 1), Range::new(5, 4), Range::new(8, 7),), + 0, + ); + + let expected_sel = Selection::new( + // smallvec!(Range::new(3, 2), Range::new(6, 5), Range::new(9, 8),), + smallvec!(Range::point(2), Range::point(5), Range::point(8),), + 0, + ); + + for (open, close) in PAIRS { + let doc = Rope::from(format!( + "{open}{close}\n{open}{close}\n{open}{close}\n", + open = open, + close = close + )); + + test_hooks(&doc, &sel, *close, &doc, &expected_sel); + } + } + + /// ([]) -> insert ( -> (([])) + #[test] + fn test_insert_open_inside_pair() { + let sel = Selection::single(2, 1); + let expected_sel = Selection::point(2); + + for (open, close) in differing_pairs() { + let doc = Rope::from(format!("{}{}", open, close)); + let expected_doc = Rope::from(format!( + "{open}{open}{close}{close}", + open = open, + close = close + )); + + test_hooks(&doc, &sel, *open, &expected_doc, &expected_sel); + } + } + + /// ([]) -> insert " -> ("[]") + #[test] + fn test_insert_nested_open_inside_pair() { + let sel = Selection::single(2, 1); + let expected_sel = Selection::point(2); + + for (outer_open, outer_close) in differing_pairs() { + let doc = Rope::from(format!("{}{}", outer_open, outer_close,)); + + for (inner_open, inner_close) in matching_pairs() { + let expected_doc = Rope::from(format!( + "{}{}{}{}", + outer_open, inner_open, inner_close, outer_close + )); + + test_hooks(&doc, &sel, *inner_open, &expected_doc, &expected_sel); + } + } + } + + /// []word -> insert ( -> ([]word + #[test] + fn test_insert_open_before_non_pair() { + test_hooks_with_pairs( + &Rope::from("word"), + &Selection::single(1, 0), + PAIRS, + |open, _| format!("{}word", open), + &Selection::point(1), + ) + } + + // [TODO] broken until it works with selections + /// [wor]d -> insert ( -> ([wor]d + #[test] + #[ignore] + fn test_insert_open_with_selection() { + test_hooks_with_pairs( + &Rope::from("word"), + &Selection::single(0, 4), + PAIRS, + |open, _| format!("{}word", open), + &Selection::single(1, 5), + ) + } + + /// we want pairs that are *not* the same char to be inserted after + /// a non-pair char, for cases like functions, but for pairs that are + /// the same char, we want to *not* insert a pair to handle cases like "I'm" + /// + /// word[] -> insert ( -> word([]) + /// word[] -> insert ' -> word'[] + #[test] + fn test_insert_open_after_non_pair() { + let doc = Rope::from("word"); + let sel = Selection::single(5, 4); + let expected_sel = Selection::point(5); + + test_hooks_with_pairs( + &doc, + &sel, + differing_pairs(), + |open, close| format!("word{}{}", open, close), + &expected_sel, + ); + + test_hooks_with_pairs( + &doc, + &sel, + matching_pairs(), + |open, _| format!("word{}", open), + &expected_sel, + ); + } } diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index b62f4a9b..d8d389f3 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -409,7 +409,7 @@ impl ChangeSet { /// Transaction represents a single undoable unit of changes. Several changes can be grouped into /// a single transaction. -#[derive(Debug, Default, Clone)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct Transaction { changes: ChangeSet, selection: Option, diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 22c23043..8552f638 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4199,8 +4199,9 @@ pub mod insert { // The default insert hook: simply insert the character #[allow(clippy::unnecessary_wraps)] // need to use Option<> because of the Hook signature fn insert(doc: &Rope, selection: &Selection, ch: char) -> Option { + let cursors = selection.clone().cursors(doc.slice(..)); let t = Tendril::from_char(ch); - let transaction = Transaction::insert(doc, selection, t); + let transaction = Transaction::insert(doc, &cursors, t); Some(transaction) } @@ -4215,11 +4216,11 @@ pub mod insert { }; let text = doc.text(); - let selection = doc.selection(view.id).clone().cursors(text.slice(..)); + let selection = doc.selection(view.id); // run through insert hooks, stopping on the first one that returns Some(t) for hook in hooks { - if let Some(transaction) = hook(text, &selection, c) { + if let Some(transaction) = hook(text, selection, c) { doc.apply(&transaction, view.id); break; } -- cgit v1.2.3-70-g09d2