on master help sign in
changed some hardcoded newlines, removed a else if in line_ending.rs
commit701eb0dd6800e75116f36e503787dd0f50df709e authorJan Hrastnik <jan.hrastnik2@gmail.com> dateSun 20 Jun 2021, 01:24 (5 years ago) parent1e80fbb
4 files changed+22−11
helix-core/src/line_ending.rs+2−3 helix-core/src/movement.rs+4−2 helix-term/src/commands.rs+15−5 helix-view/src/document.rs+1−1
helix-core/src/line_ending.rs+2−3
@@ -38,9 +38,8 @@ impl LineEnding {
3838pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
3939 if let Some(text) = g.as_str() {
4040 str_to_line_ending(text)
41− } else if g == "\u{000D}\u{000A}" {
42− Some(LineEnding::Crlf)
43− } else {
41+ }
42+ else {
4443 // Not a line ending
4544 None
4645 }
helix-core/src/movement.rs+4−2
@@ -5,7 +5,7 @@ use ropey::iter::Chars;
55use crate::{
66 coords_at_pos,
77 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
8− pos_at_coords, Position, Range, RopeSlice,
8+ pos_at_coords, Position, Range, RopeSlice, get_line_ending
99};
1010
1111#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -38,7 +38,9 @@ pub fn move_horizontally(
3838 }
3939 Direction::Forward => {
4040 // Line end is pos at the start of next line - 1
41− let end = slice.line_to_char(line + 1).saturating_sub(1);
41+ let end = slice.line_to_char(line + 1).saturating_sub(get_line_ending(&slice.line(line))
42+ .map(|le| le.len_chars())
43+ .unwrap_or(0));
4244 nth_next_grapheme_boundary(slice, pos, count).min(end)
4345 }
4446 };
helix-term/src/commands.rs+15−5
@@ -6,7 +6,7 @@ use helix_core::{
66 regex::{self, Regex},
77 register::{self, Register, Registers},
88 search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice, Selection,
9− SmallVec, Tendril, Transaction,
9+ SmallVec, Tendril, Transaction, DEFAULT_LINE_ENDING,
1010};
1111
1212use helix_view::{
@@ -627,7 +627,7 @@ fn replace(cx: &mut Context) {
627627 KeyEvent {
628628 code: KeyCode::Enter,
629629 ..
630− } => Some('\n'),
630+ } => Some('\n'), // TODO: replace this with DEFAULT_LINE_ENDING
631631 _ => None,
632632 };
633633
@@ -982,7 +982,13 @@ fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId
982982 // then delete
983983 let transaction =
984984 Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
985− let max_to = doc.text().len_chars().saturating_sub(1);
985+ let alltext = doc.text();
986+ let line = alltext.char_to_line(range.head);
987+ let max_to = doc.text().len_chars().saturating_sub(
988+ get_line_ending(&alltext.line(line))
989+ .map(|le| le.len_chars())
990+ .unwrap_or(0),
991+ );
986992 let to = std::cmp::min(max_to, range.to() + 1);
987993 (range.from(), to, None)
988994 });
@@ -1644,8 +1650,12 @@ fn append_to_line(cx: &mut Context) {
16441650 let selection = doc.selection(view.id).transform(|range| {
16451651 let text = doc.text();
16461652 let line = text.char_to_line(range.head);
1647− // we can't use line_to_char(line + 1) - 2 because the last line might not contain \n
1648− let pos = (text.line_to_char(line) + text.line(line).len_chars()).saturating_sub(1);
1653+ // we can't use line_to_char(line + 1) - 2 because the last line might not contain a newline
1654+ let pos = (text.line_to_char(line) + text.line(line).len_chars()).saturating_sub(
1655+ get_line_ending(&text.line(line))
1656+ .map(|le| le.len_chars())
1657+ .unwrap_or(0),
1658+ );
16491659 Range::new(pos, pos)
16501660 });
16511661 doc.set_selection(view.id, selection);
helix-view/src/document.rs+1−1
@@ -255,7 +255,7 @@ impl Document {
255255 use std::{fs::File, io::BufReader};
256256
257257 let doc = if !path.exists() {
258− Rope::from("\n")
258+ Rope::from(DEFAULT_LINE_ENDING.as_str())
259259 } else {
260260 let file = File::open(&path).context(format!("unable to open {:?}", path))?;
261261 let mut doc = Rope::from_reader(BufReader::new(file))?;