diff options
Diffstat (limited to 'helix-term/src')
-rw-r--r-- | helix-term/src/commands.rs | 50 | ||||
-rw-r--r-- | helix-term/src/ui/editor.rs | 4 |
2 files changed, 31 insertions, 23 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 1243a86f..07d2999b 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1,12 +1,12 @@ use helix_core::{ - comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent, - match_brackets, + comment, coords_at_pos, find_first_non_whitespace_char, find_root, get_line_ending, graphemes, + indent, match_brackets, movement::{self, Direction}, object, pos_at_coords, regex::{self, Regex}, register::{self, Register, Registers}, - search, selection, Change, ChangeSet, Position, Range, Rope, RopeSlice, Selection, SmallVec, - Tendril, Transaction, + search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice, Selection, + SmallVec, Tendril, Transaction, }; use helix_view::{ @@ -342,9 +342,12 @@ fn move_line_end(cx: &mut Context) { let text = doc.text(); let line = text.char_to_line(range.head); - // Line end is pos at the start of next line - 1 - // subtract another 1 because the line ends with \n - let pos = text.line_to_char(line + 1).saturating_sub(2); + let pos = text.line_to_char(line + 1).saturating_sub( + get_line_ending(&text.line(line)) + .map(|le| le.len_chars()) + .unwrap_or(0), + ); + Range::new(pos, pos) }); @@ -764,9 +767,12 @@ fn extend_line_end(cx: &mut Context) { let text = doc.text(); let line = text.char_to_line(range.head); - // Line end is pos at the start of next line - 1 - // subtract another 1 because the line ends with \n - let pos = text.line_to_char(line + 1).saturating_sub(2); + let pos = text.line_to_char(line + 1).saturating_sub( + get_line_ending(&text.line(line)) + .map(|le| le.len_chars()) + .unwrap_or(0), + ); + Range::new(range.anchor, pos) }); @@ -1057,7 +1063,7 @@ fn append_mode(cx: &mut Context) { if selection.iter().any(|range| range.head == end) { let transaction = Transaction::change( doc.text(), - std::array::IntoIter::new([(end, end, Some(Tendril::from_char('\n')))]), + std::array::IntoIter::new([(end, end, Some(doc.line_ending().as_str().into()))]), ); doc.apply(&transaction, view.id); } @@ -1662,16 +1668,16 @@ fn open(cx: &mut Context, open: Open) { let mut ranges = SmallVec::with_capacity(selection.len()); let mut offs = 0; + let line = match open { + // adjust position to the end of the line (next line - 1) + Open::Below => line + 1, + // adjust position to the end of the previous line (current line - 1) + Open::Above => line, + }; + let mut transaction = Transaction::change_by_selection(contents, selection, |range| { let line = text.char_to_line(range.head); - let line = match open { - // adjust position to the end of the line (next line - 1) - Open::Below => line + 1, - // adjust position to the end of the previous line (current line - 1) - Open::Above => line, - }; - // insert newlines after this index for both Above and Below variants let linend_index = doc.text().line_to_char(line).saturating_sub(1); @@ -2299,7 +2305,7 @@ pub mod insert { ); let indent = doc.indent_unit().repeat(indent_level); let mut text = String::with_capacity(1 + indent.len()); - text.push('\n'); + text.push_str(doc.line_ending().as_str()); text.push_str(&indent); let head = pos + offs + text.chars().count(); @@ -2320,7 +2326,7 @@ pub mod insert { if helix_core::auto_pairs::PAIRS.contains(&(prev, curr)) { // another newline, indent the end bracket one level less let indent = doc.indent_unit().repeat(indent_level.saturating_sub(1)); - text.push('\n'); + text.push_str(doc.line_ending().as_str()); text.push_str(&indent); } @@ -2439,7 +2445,9 @@ fn paste_impl( ); // if any of values ends \n it's linewise paste - let linewise = values.iter().any(|value| value.ends_with('\n')); + let linewise = values + .iter() + .any(|value| value.ends_with(doc.line_ending().as_str())); let mut values = values.iter().cloned().map(Tendril::from).chain(repeat); diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index d0eedad6..42bb3ba8 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -7,7 +7,7 @@ use crate::{ }; use helix_core::{ - coords_at_pos, + coords_at_pos, rope_slice_to_line_ending, syntax::{self, HighlightEvent}, Position, Range, }; @@ -177,7 +177,7 @@ impl EditorView { // iterate over range char by char for grapheme in RopeGraphemes::new(text) { - if grapheme == "\n" { + if rope_slice_to_line_ending(&grapheme).is_some() { visual_x = 0; line += 1; |