on master help sign in
changed some hardcoded newlines, removed a else if in line_ending.rs
701eb0dJan Hrastnik
helix-core/src/line_ending.rs+2−3
@@ -38,9 +38,8 @@ impl LineEnding {
38pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
38pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
39 if let Some(text) = g.as_str() {
39 if let Some(text) = g.as_str() {
40 str_to_line_ending(text)
40 str_to_line_ending(text)
41− } else if g == "\u{000D}\u{000A}" {
41+ }
42− Some(LineEnding::Crlf)
43− } else {
42+ else {
44 // Not a line ending
43 // Not a line ending
45 None
44 None
46 }
45 }
helix-core/src/movement.rs+4−2
@@ -5,7 +5,7 @@ use ropey::iter::Chars;
5use crate::{
5use crate::{
6 coords_at_pos,
6 coords_at_pos,
7 graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
7 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
9};
9};
10
10
11#[derive(Debug, Copy, Clone, PartialEq, Eq)]
11#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -38,7 +38,9 @@ pub fn move_horizontally(
38 }
38 }
39 Direction::Forward => {
39 Direction::Forward => {
40 // Line end is pos at the start of next line - 1
40 // 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));
42 nth_next_grapheme_boundary(slice, pos, count).min(end)
44 nth_next_grapheme_boundary(slice, pos, count).min(end)
43 }
45 }
44 };
46 };
helix-term/src/commands.rs+15−5
@@ -6,7 +6,7 @@ use helix_core::{
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,
8 search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice, Selection,
9− SmallVec, Tendril, Transaction,
9+ SmallVec, Tendril, Transaction, DEFAULT_LINE_ENDING,
10};
10};
11
11
12use helix_view::{
12use helix_view::{
@@ -627,7 +627,7 @@ fn replace(cx: &mut Context) {
627 KeyEvent {
627 KeyEvent {
628 code: KeyCode::Enter,
628 code: KeyCode::Enter,
629 ..
629 ..
630− } => Some('\n'),
630+ } => Some('\n'), // TODO: replace this with DEFAULT_LINE_ENDING
631 _ => None,
631 _ => None,
632 };
632 };
633
633
@@ -982,7 +982,13 @@ fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId
982 // then delete
982 // then delete
983 let transaction =
983 let transaction =
984 Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
984 Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
985+ let alltext = doc.text();
986+ let line = alltext.char_to_line(range.head);
985− let max_to = doc.text().len_chars().saturating_sub(1);
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+ );
986 let to = std::cmp::min(max_to, range.to() + 1);
992 let to = std::cmp::min(max_to, range.to() + 1);
987 (range.from(), to, None)
993 (range.from(), to, None)
988 });
994 });
@@ -1644,8 +1650,12 @@ fn append_to_line(cx: &mut Context) {
1644 let selection = doc.selection(view.id).transform(|range| {
1650 let selection = doc.selection(view.id).transform(|range| {
1645 let text = doc.text();
1651 let text = doc.text();
1646 let line = text.char_to_line(range.head);
1652 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
1653+ // we can't use line_to_char(line + 1) - 2 because the last line might not contain a newline
1648− let pos = (text.line_to_char(line) + text.line(line).len_chars()).saturating_sub(1);
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+ );
1649 Range::new(pos, pos)
1659 Range::new(pos, pos)
1650 });
1660 });
1651 doc.set_selection(view.id, selection);
1661 doc.set_selection(view.id, selection);
helix-view/src/document.rs+1−1
@@ -255,7 +255,7 @@ impl Document {
255 use std::{fs::File, io::BufReader};
255 use std::{fs::File, io::BufReader};
256
256
257 let doc = if !path.exists() {
257 let doc = if !path.exists() {
258− Rope::from("\n")
258+ Rope::from(DEFAULT_LINE_ENDING.as_str())
259 } else {
259 } else {
260 let file = File::open(&path).context(format!("unable to open {:?}", path))?;
260 let file = File::open(&path).context(format!("unable to open {:?}", path))?;
261 let mut doc = Rope::from_reader(BufReader::new(file))?;
261 let mut doc = Rope::from_reader(BufReader::new(file))?;