diff --git a/helix-core/src/line_ending.rs b/helix-core/src/line_ending.rs index 2cc5b5d84..33f8d0783 100644 --- a/helix-core/src/line_ending.rs +++ b/helix-core/src/line_ending.rs @@ -38,9 +38,8 @@ pub fn as_str(&self) -> &str { pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option { if let Some(text) = g.as_str() { str_to_line_ending(text) - } else if g == "\u{000D}\u{000A}" { - Some(LineEnding::Crlf) - } else { + } + else { // Not a line ending None } diff --git a/helix-core/src/movement.rs b/helix-core/src/movement.rs index 8b1e802f4..cf7ea8548 100644 --- a/helix-core/src/movement.rs +++ b/helix-core/src/movement.rs @@ -5,7 +5,7 @@ use crate::{ coords_at_pos, graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary}, - pos_at_coords, Position, Range, RopeSlice, + pos_at_coords, Position, Range, RopeSlice, get_line_ending }; #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -38,7 +38,9 @@ pub fn move_horizontally( } Direction::Forward => { // Line end is pos at the start of next line - 1 - let end = slice.line_to_char(line + 1).saturating_sub(1); + let end = slice.line_to_char(line + 1).saturating_sub(get_line_ending(&slice.line(line)) + .map(|le| le.len_chars()) + .unwrap_or(0)); nth_next_grapheme_boundary(slice, pos, count).min(end) } }; diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index af2e8eeef..cee0e752f 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -6,7 +6,7 @@ regex::{self, Regex}, register::{self, Register, Registers}, search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice, Selection, - SmallVec, Tendril, Transaction, + SmallVec, Tendril, Transaction, DEFAULT_LINE_ENDING, }; use helix_view::{ @@ -627,7 +627,7 @@ fn replace(cx: &mut Context) { KeyEvent { code: KeyCode::Enter, .. - } => Some('\n'), + } => Some('\n'), // TODO: replace this with DEFAULT_LINE_ENDING _ => None, }; @@ -982,7 +982,13 @@ fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId // then delete let transaction = Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| { - let max_to = doc.text().len_chars().saturating_sub(1); + let alltext = doc.text(); + let line = alltext.char_to_line(range.head); + let max_to = doc.text().len_chars().saturating_sub( + get_line_ending(&alltext.line(line)) + .map(|le| le.len_chars()) + .unwrap_or(0), + ); let to = std::cmp::min(max_to, range.to() + 1); (range.from(), to, None) }); @@ -1644,8 +1650,12 @@ fn append_to_line(cx: &mut Context) { let selection = doc.selection(view.id).transform(|range| { let text = doc.text(); let line = text.char_to_line(range.head); - // we can't use line_to_char(line + 1) - 2 because the last line might not contain \n - let pos = (text.line_to_char(line) + text.line(line).len_chars()).saturating_sub(1); + // we can't use line_to_char(line + 1) - 2 because the last line might not contain a newline + let pos = (text.line_to_char(line) + text.line(line).len_chars()).saturating_sub( + get_line_ending(&text.line(line)) + .map(|le| le.len_chars()) + .unwrap_or(0), + ); Range::new(pos, pos) }); doc.set_selection(view.id, selection); diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 49d270e4d..44d505830 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -255,7 +255,7 @@ pub fn load(path: PathBuf) -> Result { use std::{fs::File, io::BufReader}; let doc = if !path.exists() { - Rope::from("\n") + Rope::from(DEFAULT_LINE_ENDING.as_str()) } else { let file = File::open(&path).context(format!("unable to open {:?}", path))?; let mut doc = Rope::from_reader(BufReader::new(file))?;