on master help sign in
applied changes from pr review
commit8bccd6df3054143baf128157d8dcecb10a911956 authorJan Hrastnik <jan.hrastnik2@gmail.com> dateThu 17 Jun 2021, 13:49 (5 years ago) parent9c3eadb
3 files changed+39−34
helix-core/src/line_ending.rs+23 helix-term/src/commands.rs+13−21 helix-view/src/document.rs+3−13
helix-core/src/line_ending.rs+23
@@ -13,6 +13,29 @@ pub enum LineEnding {
1313 PS, // U+2029 -- ParagraphSeparator
1414}
1515
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+
1639pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
1740 if let Some(text) = g.as_str() {
1841 str_to_line_ending(text)
helix-term/src/commands.rs+13−21
@@ -184,14 +184,10 @@ pub fn move_line_end(cx: &mut Context) {
184184 let line = text.char_to_line(range.head);
185185
186186 // 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
188− let pos =
189− text.line_to_char(line + 1)
190− .saturating_sub(if doc.line_ending == LineEnding::Crlf {
191− 3
192− } else {
193− 2
194− });
187+ // subtract another 1 because the line ends with \n
188+ let pos = text
189+ .line_to_char(line + 1)
190+ .saturating_sub(doc.line_ending().len() + 1);
195191 Range::new(pos, pos)
196192 });
197193
@@ -612,14 +608,10 @@ pub fn extend_line_end(cx: &mut Context) {
612608 let line = text.char_to_line(range.head);
613609
614610 // 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
616− let pos =
617− text.line_to_char(line + 1)
618− .saturating_sub(if doc.line_ending == LineEnding::Crlf {
619− 3
620− } else {
621− 2
622− });
611+ // subtract another 1 because the line ends with \n
612+ let pos = text
613+ .line_to_char(line + 1)
614+ .saturating_sub(doc.line_ending().len() + 1);
623615 Range::new(range.anchor, pos)
624616 });
625617
@@ -908,7 +900,7 @@ pub fn append_mode(cx: &mut Context) {
908900 if selection.iter().any(|range| range.head == end) {
909901 let transaction = Transaction::change(
910902 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()))]),
912904 );
913905 doc.apply(&transaction, view.id);
914906 }
@@ -1535,7 +1527,7 @@ fn open(cx: &mut Context, open: Open) {
15351527 );
15361528 let indent = doc.indent_unit().repeat(indent_level);
15371529 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());
15391531 text.push_str(&indent);
15401532 let text = text.repeat(count);
15411533
@@ -2143,7 +2135,7 @@ pub mod insert {
21432135 );
21442136 let indent = doc.indent_unit().repeat(indent_level);
21452137 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());
21472139 text.push_str(&indent);
21482140
21492141 let head = pos + offs + text.chars().count();
@@ -2164,7 +2156,7 @@ pub mod insert {
21642156 if helix_core::auto_pairs::PAIRS.contains(&(prev, curr)) {
21652157 // another newline, indent the end bracket one level less
21662158 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());
21682160 text.push_str(&indent);
21692161 }
21702162
@@ -2283,7 +2275,7 @@ fn paste_impl(
22832275 // if any of values ends \n it's linewise paste
22842276 let linewise = values
22852277 .iter()
2286− .any(|value| value.ends_with(doc.line_ending()));
2278+ .any(|value| value.ends_with(doc.line_ending().as_str()));
22872279
22882280 let mut values = values.iter().cloned().map(Tendril::from).chain(repeat);
22892281
helix-view/src/document.rs+3−13
@@ -63,7 +63,7 @@ pub struct Document {
6363
6464 diagnostics: Vec<Diagnostic>,
6565 language_server: Option<Arc<helix_lsp::Client>>,
66− pub line_ending: LineEnding,
66+ line_ending: LineEnding,
6767}
6868
6969use std::fmt;
@@ -728,18 +728,8 @@ impl Document {
728728 self.diagnostics = diagnostics;
729729 }
730730
731− pub fn line_ending(&self) -> &'static str {
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
741− ),
742− }
731+ pub fn line_ending(&self) -> LineEnding {
732+ self.line_ending
743733 }
744734}
745735