@@ -4,9 +4,14 @@ use helix_core::{
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},
8 register::{self, Register, Registers},
8 search, selection, Change, ChangeSet, Position, Range, Rope, RopeSlice, Selection, SmallVec,
9 search, selection, Change, ChangeSet, Position, Range, Rope, RopeSlice, Selection, SmallVec,
9 Tendril, Transaction,
10 Tendril, Transaction,
12+ register, search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice,
13+ Selection, SmallVec, Tendril, Transaction,
14+>>>>>>> 856fd95 (trying out line ending helper functions in commands.rs)
12use helix_view::{
17use helix_view::{
@@ -184,8 +189,14 @@ pub fn move_line_end(cx: &mut Context) {
184 let line = text.char_to_line(range.head);
189 let line = text.char_to_line(range.head);
186 // Line end is pos at the start of next line - 1
191 // Line end is pos at the start of next line - 1
187− // subtract another 1 because the line ends with \n
192+ // subtract 3 if the line ending is \r\n, otherwise subtract 2 as I assume all others are just 1 char length
188− let pos = text.line_to_char(line + 1).saturating_sub(2);
194+ text.line_to_char(line + 1)
195+ .saturating_sub(if doc.line_ending == LineEnding::Crlf {
189 Range::new(pos, pos)
200 Range::new(pos, pos)
@@ -337,7 +348,7 @@ where
337 KeyEvent {
348 KeyEvent {
338 code: KeyCode::Enter,
349 code: KeyCode::Enter,
340− } => '\n',
351+ } => '\n', // TODO: we should be calling doc.line_ending() here
341 KeyEvent {
352 KeyEvent {
342 code: KeyCode::Char(ch),
353 code: KeyCode::Char(ch),
@@ -465,7 +476,7 @@ pub fn replace(cx: &mut Context) {
465 KeyEvent {
476 KeyEvent {
466 code: KeyCode::Enter,
477 code: KeyCode::Enter,
468− } => Some('\n'),
479+ } => Some('\n'), // TODO: we should be calling doc.line_ending() here
469 _ => None,
480 _ => None,
@@ -606,8 +617,14 @@ pub fn extend_line_end(cx: &mut Context) {
606 let line = text.char_to_line(range.head);
617 let line = text.char_to_line(range.head);
608 // Line end is pos at the start of next line - 1
619 // Line end is pos at the start of next line - 1
609− // subtract another 1 because the line ends with \n
620+ // subtract 3 if the line ending is \r\n, otherwise subtract 2 as I assume all others are just 1 char length
610− let pos = text.line_to_char(line + 1).saturating_sub(2);
622+ text.line_to_char(line + 1)
623+ .saturating_sub(if doc.line_ending == LineEnding::Crlf {
611 Range::new(range.anchor, pos)
628 Range::new(range.anchor, pos)
@@ -896,7 +913,7 @@ pub fn append_mode(cx: &mut Context) {
896 if selection.iter().any(|range| range.head == end) {
913 if selection.iter().any(|range| range.head == end) {
897 let transaction = Transaction::change(
914 let transaction = Transaction::change(
898 doc.text(),
915 doc.text(),
899− std::array::IntoIter::new([(end, end, Some(Tendril::from_char('\n')))]),
916+ std::array::IntoIter::new([(end, end, Some(Tendril::from_char('\n')))]), // TODO: change \n to doc.line_ending()
901 doc.apply(&transaction, view.id);
918 doc.apply(&transaction, view.id);
@@ -1523,7 +1540,7 @@ fn open(cx: &mut Context, open: Open) {
1524 let indent = doc.indent_unit().repeat(indent_level);
1541 let indent = doc.indent_unit().repeat(indent_level);
1525 let mut text = String::with_capacity(1 + indent.len());
1542 let mut text = String::with_capacity(1 + indent.len());
1526− text.push('\n');
1543+ text.push_str(doc.line_ending());
1527 text.push_str(&indent);
1544 text.push_str(&indent);
1528 let text = text.repeat(count);
1545 let text = text.repeat(count);
@@ -2131,7 +2148,7 @@ pub mod insert {
2132 let indent = doc.indent_unit().repeat(indent_level);
2149 let indent = doc.indent_unit().repeat(indent_level);
2133 let mut text = String::with_capacity(1 + indent.len());
2150 let mut text = String::with_capacity(1 + indent.len());
2134− text.push('\n');
2151+ text.push_str(doc.line_ending());
2135 text.push_str(&indent);
2152 text.push_str(&indent);
2137 let head = pos + offs + text.chars().count();
2154 let head = pos + offs + text.chars().count();
@@ -2152,7 +2169,7 @@ pub mod insert {
2152 if helix_core::auto_pairs::PAIRS.contains(&(prev, curr)) {
2169 if helix_core::auto_pairs::PAIRS.contains(&(prev, curr)) {
2153 // another newline, indent the end bracket one level less
2170 // another newline, indent the end bracket one level less
2154 let indent = doc.indent_unit().repeat(indent_level.saturating_sub(1));
2171 let indent = doc.indent_unit().repeat(indent_level.saturating_sub(1));
2155− text.push('\n');
2172+ text.push_str(doc.line_ending());
2156 text.push_str(&indent);
2173 text.push_str(&indent);
@@ -2268,8 +2285,15 @@ fn paste_impl(
2268 .unwrap(),
2285 .unwrap(),
2271 // if any of values ends \n it's linewise paste
2289 // if any of values ends \n it's linewise paste
2272 let linewise = values.iter().any(|value| value.ends_with('\n'));
2290 let linewise = values.iter().any(|value| value.ends_with('\n'));
2292+ // if any of values ends \n it's linewise paste
2293+ let linewise = values
2295+ .any(|value| value.ends_with(doc.line_ending()));
2296+>>>>>>> 856fd95 (trying out line ending helper functions in commands.rs)
2274 let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);
2298 let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);