on master help sign in
trying out line ending helper functions in commands.rs
commita4f5a0134e308ffad318ab6920fe1ed0264a93cb authorJan Hrastnik <jan.hrastnik2@gmail.com> dateMon 14 Jun 2021, 15:09 (5 years ago) parenta9a718c
5 files changed+47−15
helix-core/src/lib.rs+3−1 helix-core/src/line_ending.rs+7−1 helix-term/src/commands.rs+34−10 helix-term/src/ui/editor.rs+2−2 helix-view/src/document.rs+1−1
helix-core/src/lib.rs+3−1
@@ -110,5 +110,7 @@ pub use syntax::Syntax;
110110pub use diagnostic::Diagnostic;
111111pub use state::State;
112112
113−pub use line_ending::{auto_detect_line_ending, LineEnding, DEFAULT_LINE_ENDING};
113+pub use line_ending::{
114+ auto_detect_line_ending, rope_slice_to_line_ending, LineEnding, DEFAULT_LINE_ENDING,
115+};
114116pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
helix-core/src/line_ending.rs+7−1
@@ -31,6 +31,9 @@ pub fn str_to_line_ending(g: &str) -> Option<LineEnding> {
3131 "\u{000D}" => Some(LineEnding::CR),
3232 "\u{0085}" => Some(LineEnding::Nel),
3333 "\u{2028}" => Some(LineEnding::LS),
34+ "\u{000B}" => Some(LineEnding::VT),
35+ "\u{000C}" => Some(LineEnding::FF),
36+ "\u{2029}" => Some(LineEnding::PS),
3437 // Not a line ending
3538 _ => None,
3639 }
@@ -58,7 +61,10 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
5861 _ => None,
5962 };
6063 if ending.is_some() {
61− return ending;
64+ match ending {
65+ Some(LineEnding::VT) | Some(LineEnding::FF) | Some(LineEnding::PS) => {}
66+ _ => return ending,
67+ }
6268 }
6369 }
6470 ending
helix-term/src/commands.rs+34−10
@@ -4,9 +4,14 @@ use helix_core::{
44 movement::{self, Direction},
55 object, pos_at_coords,
66 regex::{self, Regex},
7+<<<<<<< HEAD
78 register::{self, Register, Registers},
89 search, selection, Change, ChangeSet, Position, Range, Rope, RopeSlice, Selection, SmallVec,
910 Tendril, Transaction,
11+=======
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)
1015};
1116
1217use helix_view::{
@@ -184,8 +189,14 @@ pub fn move_line_end(cx: &mut Context) {
184189 let line = text.char_to_line(range.head);
185190
186191 // Line end is pos at the start of next line - 1
187− // subtract another 1 because the line ends with \n
188− let pos = text.line_to_char(line + 1).saturating_sub(2);
192+ // subtract 3 if the line ending is \r\n, otherwise subtract 2 as I assume all others are just 1 char length
193+ let pos =
194+ text.line_to_char(line + 1)
195+ .saturating_sub(if doc.line_ending == LineEnding::Crlf {
196+ 3
197+ } else {
198+ 2
199+ });
189200 Range::new(pos, pos)
190201 });
191202
@@ -337,7 +348,7 @@ where
337348 KeyEvent {
338349 code: KeyCode::Enter,
339350 ..
340− } => '\n',
351+ } => '\n', // TODO: we should be calling doc.line_ending() here
341352 KeyEvent {
342353 code: KeyCode::Char(ch),
343354 ..
@@ -465,7 +476,7 @@ pub fn replace(cx: &mut Context) {
465476 KeyEvent {
466477 code: KeyCode::Enter,
467478 ..
468− } => Some('\n'),
479+ } => Some('\n'), // TODO: we should be calling doc.line_ending() here
469480 _ => None,
470481 };
471482
@@ -606,8 +617,14 @@ pub fn extend_line_end(cx: &mut Context) {
606617 let line = text.char_to_line(range.head);
607618
608619 // Line end is pos at the start of next line - 1
609− // subtract another 1 because the line ends with \n
610− let pos = text.line_to_char(line + 1).saturating_sub(2);
620+ // subtract 3 if the line ending is \r\n, otherwise subtract 2 as I assume all others are just 1 char length
621+ let pos =
622+ text.line_to_char(line + 1)
623+ .saturating_sub(if doc.line_ending == LineEnding::Crlf {
624+ 3
625+ } else {
626+ 2
627+ });
611628 Range::new(range.anchor, pos)
612629 });
613630
@@ -896,7 +913,7 @@ pub fn append_mode(cx: &mut Context) {
896913 if selection.iter().any(|range| range.head == end) {
897914 let transaction = Transaction::change(
898915 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()
900917 );
901918 doc.apply(&transaction, view.id);
902919 }
@@ -1523,7 +1540,7 @@ fn open(cx: &mut Context, open: Open) {
15231540 );
15241541 let indent = doc.indent_unit().repeat(indent_level);
15251542 let mut text = String::with_capacity(1 + indent.len());
1526− text.push('\n');
1543+ text.push_str(doc.line_ending());
15271544 text.push_str(&indent);
15281545 let text = text.repeat(count);
15291546
@@ -2131,7 +2148,7 @@ pub mod insert {
21312148 );
21322149 let indent = doc.indent_unit().repeat(indent_level);
21332150 let mut text = String::with_capacity(1 + indent.len());
2134− text.push('\n');
2151+ text.push_str(doc.line_ending());
21352152 text.push_str(&indent);
21362153
21372154 let head = pos + offs + text.chars().count();
@@ -2152,7 +2169,7 @@ pub mod insert {
21522169 if helix_core::auto_pairs::PAIRS.contains(&(prev, curr)) {
21532170 // another newline, indent the end bracket one level less
21542171 let indent = doc.indent_unit().repeat(indent_level.saturating_sub(1));
2155− text.push('\n');
2172+ text.push_str(doc.line_ending());
21562173 text.push_str(&indent);
21572174 }
21582175
@@ -2268,8 +2285,15 @@ fn paste_impl(
22682285 .unwrap(),
22692286 );
22702287
2288+<<<<<<< HEAD
22712289 // if any of values ends \n it's linewise paste
22722290 let linewise = values.iter().any(|value| value.ends_with('\n'));
2291+=======
2292+ // if any of values ends \n it's linewise paste
2293+ let linewise = values
2294+ .iter()
2295+ .any(|value| value.ends_with(doc.line_ending()));
2296+>>>>>>> 856fd95 (trying out line ending helper functions in commands.rs)
22732297
22742298 let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);
22752299
helix-term/src/ui/editor.rs+2−2
@@ -7,7 +7,7 @@ use crate::{
77};
88
99use helix_core::{
10− coords_at_pos,
10+ coords_at_pos, rope_slice_to_line_ending,
1111 syntax::{self, HighlightEvent},
1212 Position, Range,
1313};
@@ -179,7 +179,7 @@ impl EditorView {
179179
180180 // iterate over range char by char
181181 for grapheme in RopeGraphemes::new(text) {
182− if grapheme == "\n" || grapheme == "\r\n" {
182+ if rope_slice_to_line_ending(&grapheme).is_some() {
183183 visual_x = 0;
184184 line += 1;
185185
helix-view/src/document.rs+1−1
@@ -76,7 +76,7 @@ pub struct Document {
7676
7777 diagnostics: Vec<Diagnostic>,
7878 language_server: Option<Arc<helix_lsp::Client>>,
79− line_ending: LineEnding,
79+ pub line_ending: LineEnding,
8080}
8181
8282use std::fmt;