on master help sign in
ran cargo fmt
97323dcJan Hrastnik
helix-core/src/lib.rs+2−1
@@ -111,6 +111,7 @@ pub use diagnostic::Diagnostic;
111pub use state::State;
111pub use state::State;
112
112
113pub use line_ending::{
113pub use line_ending::{
114− auto_detect_line_ending, rope_slice_to_line_ending, LineEnding, DEFAULT_LINE_ENDING, get_line_ending
114+ auto_detect_line_ending, get_line_ending, rope_slice_to_line_ending, LineEnding,
115+ DEFAULT_LINE_ENDING,
115};
116};
116pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
117pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
helix-core/src/line_ending.rs+8−2
@@ -95,12 +95,18 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
95/// Returns the passed line's line ending, if any.
95/// Returns the passed line's line ending, if any.
96pub fn get_line_ending(line: &RopeSlice) -> Option<LineEnding> {
96pub fn get_line_ending(line: &RopeSlice) -> Option<LineEnding> {
97 // Last character as str.
97 // Last character as str.
98+ let g1 = line
98− let g1 = line.slice(line.len_chars().saturating_sub(1)..).as_str().unwrap();
99+ .slice(line.len_chars().saturating_sub(1)..)
100+ .as_str()
101+ .unwrap();
99
102
100 // Last two characters as str, or empty str if they're not contiguous.
103 // Last two characters as str, or empty str if they're not contiguous.
101 // It's fine to punt on the non-contiguous case, because Ropey guarantees
104 // It's fine to punt on the non-contiguous case, because Ropey guarantees
102 // that CRLF is always contiguous.
105 // that CRLF is always contiguous.
106+ let g2 = line
103− let g2 = line.slice(line.len_chars().saturating_sub(2)..).as_str().unwrap_or("");
107+ .slice(line.len_chars().saturating_sub(2)..)
108+ .as_str()
109+ .unwrap_or("");
104
110
105 // First check the two-character case for CRLF, then check the single-character case.
111 // First check the two-character case for CRLF, then check the single-character case.
106 str_to_line_ending(g2).or_else(|| str_to_line_ending(g1))
112 str_to_line_ending(g2).or_else(|| str_to_line_ending(g1))
helix-term/src/commands.rs+13−9
@@ -1,11 +1,11 @@
1use helix_core::{
1use helix_core::{
2− comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent,
2+ comment, coords_at_pos, find_first_non_whitespace_char, find_root, get_line_ending, graphemes,
3− match_brackets,
3+ indent, match_brackets,
4 movement::{self, Direction},
4 movement::{self, Direction},
5 object, pos_at_coords,
5 object, pos_at_coords,
6 regex::{self, Regex},
6 regex::{self, Regex},
7 register::{self, Register, Registers},
7 register::{self, Register, Registers},
8− search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice, Selection, get_line_ending,
8+ search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice, Selection,
9 SmallVec, Tendril, Transaction,
9 SmallVec, Tendril, Transaction,
10};
10};
11
11
@@ -183,9 +183,11 @@ pub fn move_line_end(cx: &mut Context) {
183 let text = doc.text();
183 let text = doc.text();
184 let line = text.char_to_line(range.head);
184 let line = text.char_to_line(range.head);
185
185
186− let pos = text
187− .line_to_char(line + 1)
186+ let pos = text.line_to_char(line + 1).saturating_sub(
188− .saturating_sub(get_line_ending(&text.line(line)).map(|le| le.len_chars()).unwrap_or(0));
187+ get_line_ending(&text.line(line))
188+ .map(|le| le.len_chars())
189+ .unwrap_or(0),
190+ );
189
191
190 Range::new(pos, pos)
192 Range::new(pos, pos)
191 });
193 });
@@ -606,9 +608,11 @@ pub fn extend_line_end(cx: &mut Context) {
606 let text = doc.text();
608 let text = doc.text();
607 let line = text.char_to_line(range.head);
609 let line = text.char_to_line(range.head);
608
610
609− let pos = text
610− .line_to_char(line + 1)
611+ let pos = text.line_to_char(line + 1).saturating_sub(
611− .saturating_sub(get_line_ending(&text.line(line)).map(|le| le.len_chars()).unwrap_or(0));
612+ get_line_ending(&text.line(line))
613+ .map(|le| le.len_chars())
614+ .unwrap_or(0),
615+ );
612
616
613 Range::new(range.anchor, pos)
617 Range::new(range.anchor, pos)
614 });
618 });