on master help sign in
applied changes from pr review
8bccd6dJan Hrastnik
helix-core/src/line_ending.rs+23
@@ -13,6 +13,29 @@ pub enum LineEnding {
13 PS, // U+2029 -- ParagraphSeparator
13 PS, // U+2029 -- ParagraphSeparator
14}
14}
15
15
16+impl LineEnding {
17+ pub fn len(&self) -> usize {
18+ match self {
19+ Self::Crlf => 2,
20+ _ => 1,
21+ }
22+ }
23+
24+ pub fn as_str(&self) -> &str {
25+ match self {
26+ Self::Crlf => "\u{000D}\u{000A}",
27+ Self::LF => "\u{000A}",
28+ Self::Nel => "\u{0085}",
29+ Self::LS => "\u{2028}",
30+ Self::CR => "\u{000D}",
31+ _ => panic!(
32+ "Unexpected line ending: {:?}, expected Crlf, LF, CR, Nel, or LS.",
33+ self
34+ ),
35+ }
36+ }
37+}
38+
16pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
39pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
17 if let Some(text) = g.as_str() {
40 if let Some(text) = g.as_str() {
18 str_to_line_ending(text)
41 str_to_line_ending(text)
helix-term/src/commands.rs+13−21
@@ -184,14 +184,10 @@ pub fn move_line_end(cx: &mut Context) {
184 let line = text.char_to_line(range.head);
184 let line = text.char_to_line(range.head);
185
185
186 // Line end is pos at the start of next line - 1
186 // Line end is pos at the start of next line - 1
187− // subtract 3 if the line ending is \r\n, otherwise subtract 2 as I assume all others are just 1 char length
187+ // subtract another 1 because the line ends with \n
188− let pos =
188+ let pos = text
189− text.line_to_char(line + 1)
189+ .line_to_char(line + 1)
190− .saturating_sub(if doc.line_ending == LineEnding::Crlf {
190+ .saturating_sub(doc.line_ending().len() + 1);
191− 3
192− } else {
193− 2
194− });
195 Range::new(pos, pos)
191 Range::new(pos, pos)
196 });
192 });
197
193
@@ -612,14 +608,10 @@ pub fn extend_line_end(cx: &mut Context) {
612 let line = text.char_to_line(range.head);
608 let line = text.char_to_line(range.head);
613
609
614 // Line end is pos at the start of next line - 1
610 // Line end is pos at the start of next line - 1
615− // subtract 3 if the line ending is \r\n, otherwise subtract 2 as I assume all others are just 1 char length
611+ // subtract another 1 because the line ends with \n
616− let pos =
612+ let pos = text
617− text.line_to_char(line + 1)
613+ .line_to_char(line + 1)
618− .saturating_sub(if doc.line_ending == LineEnding::Crlf {
614+ .saturating_sub(doc.line_ending().len() + 1);
619− 3
620− } else {
621− 2
622− });
623 Range::new(range.anchor, pos)
615 Range::new(range.anchor, pos)
624 });
616 });
625
617
@@ -908,7 +900,7 @@ pub fn append_mode(cx: &mut Context) {
908 if selection.iter().any(|range| range.head == end) {
900 if selection.iter().any(|range| range.head == end) {
909 let transaction = Transaction::change(
901 let transaction = Transaction::change(
910 doc.text(),
902 doc.text(),
911− std::array::IntoIter::new([(end, end, Some(Tendril::from_char('\n')))]), // TODO: change \n to doc.line_ending()
903+ std::array::IntoIter::new([(end, end, Some(doc.line_ending().as_str().into()))]),
912 );
904 );
913 doc.apply(&transaction, view.id);
905 doc.apply(&transaction, view.id);
914 }
906 }
@@ -1535,7 +1527,7 @@ fn open(cx: &mut Context, open: Open) {
1535 );
1527 );
1536 let indent = doc.indent_unit().repeat(indent_level);
1528 let indent = doc.indent_unit().repeat(indent_level);
1537 let mut text = String::with_capacity(1 + indent.len());
1529 let mut text = String::with_capacity(1 + indent.len());
1538− text.push_str(doc.line_ending());
1530+ text.push_str(doc.line_ending().as_str());
1539 text.push_str(&indent);
1531 text.push_str(&indent);
1540 let text = text.repeat(count);
1532 let text = text.repeat(count);
1541
1533
@@ -2143,7 +2135,7 @@ pub mod insert {
2143 );
2135 );
2144 let indent = doc.indent_unit().repeat(indent_level);
2136 let indent = doc.indent_unit().repeat(indent_level);
2145 let mut text = String::with_capacity(1 + indent.len());
2137 let mut text = String::with_capacity(1 + indent.len());
2146− text.push_str(doc.line_ending());
2138+ text.push_str(doc.line_ending().as_str());
2147 text.push_str(&indent);
2139 text.push_str(&indent);
2148
2140
2149 let head = pos + offs + text.chars().count();
2141 let head = pos + offs + text.chars().count();
@@ -2164,7 +2156,7 @@ pub mod insert {
2164 if helix_core::auto_pairs::PAIRS.contains(&(prev, curr)) {
2156 if helix_core::auto_pairs::PAIRS.contains(&(prev, curr)) {
2165 // another newline, indent the end bracket one level less
2157 // another newline, indent the end bracket one level less
2166 let indent = doc.indent_unit().repeat(indent_level.saturating_sub(1));
2158 let indent = doc.indent_unit().repeat(indent_level.saturating_sub(1));
2167− text.push_str(doc.line_ending());
2159+ text.push_str(doc.line_ending().as_str());
2168 text.push_str(&indent);
2160 text.push_str(&indent);
2169 }
2161 }
2170
2162
@@ -2283,7 +2275,7 @@ fn paste_impl(
2283 // if any of values ends \n it's linewise paste
2275 // if any of values ends \n it's linewise paste
2284 let linewise = values
2276 let linewise = values
2285 .iter()
2277 .iter()
2286− .any(|value| value.ends_with(doc.line_ending()));
2278+ .any(|value| value.ends_with(doc.line_ending().as_str()));
2287
2279
2288 let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);
2280 let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);
2289
2281
helix-view/src/document.rs+3−13
@@ -63,7 +63,7 @@ pub struct Document {
63
63
64 diagnostics: Vec<Diagnostic>,
64 diagnostics: Vec<Diagnostic>,
65 language_server: Option<Arc<helix_lsp::Client>>,
65 language_server: Option<Arc<helix_lsp::Client>>,
66− pub line_ending: LineEnding,
66+ line_ending: LineEnding,
67}
67}
68
68
69use std::fmt;
69use std::fmt;
@@ -728,18 +728,8 @@ impl Document {
728 self.diagnostics = diagnostics;
728 self.diagnostics = diagnostics;
729 }
729 }
730
730
731− pub fn line_ending(&self) -> &'static str {
731+ pub fn line_ending(&self) -> LineEnding {
732− match self.line_ending {
733− LineEnding::Crlf => "\u{000D}\u{000A}",
734− LineEnding::LF => "\u{000A}",
735− LineEnding::Nel => "\u{0085}",
736− LineEnding::LS => "\u{2028}",
737− LineEnding::CR => "\u{000D}",
738− _ => panic!(
739− "Unexpected line ending: {:?}, expected Crlf, LF, CR, Nel, or LS.",
740− self.line_ending
732+ self.line_ending
741− ),
742− }
743 }
733 }
744}
734}
745
735