Skip to content

Commit

Permalink
trying out line ending helper functions in commands.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
janhrastnik committed Jun 14, 2021
1 parent 20722d9 commit 856fd95
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
4 changes: 3 additions & 1 deletion helix-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,7 @@ pub use syntax::Syntax;
pub use diagnostic::Diagnostic;
pub use state::State;

pub use line_ending::{auto_detect_line_ending, LineEnding, DEFAULT_LINE_ENDING};
pub use line_ending::{
auto_detect_line_ending, rope_slice_to_line_ending, LineEnding, DEFAULT_LINE_ENDING,
};
pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
8 changes: 7 additions & 1 deletion helix-core/src/line_ending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub fn str_to_line_ending(g: &str) -> Option<LineEnding> {
"\u{000D}" => Some(LineEnding::CR),
"\u{0085}" => Some(LineEnding::Nel),
"\u{2028}" => Some(LineEnding::LS),
"\u{000B}" => Some(LineEnding::VT),
"\u{000C}" => Some(LineEnding::FF),
"\u{2029}" => Some(LineEnding::PS),
// Not a line ending
_ => None,
}
Expand Down Expand Up @@ -58,7 +61,10 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
_ => None,
};
if ending.is_some() {
return ending;
match ending {
Some(LineEnding::VT) | Some(LineEnding::FF) | Some(LineEnding::PS) => {}
_ => return ending,
}
}
}
ending
Expand Down
40 changes: 27 additions & 13 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use helix_core::{
movement::{self, Direction},
object, pos_at_coords,
regex::{self, Regex},
register, search, selection, Change, ChangeSet, Position, Range, Rope, RopeSlice, Selection,
SmallVec, Tendril, Transaction,
register, search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice,
Selection, SmallVec, Tendril, Transaction,
};

use helix_view::{
Expand Down Expand Up @@ -178,8 +178,14 @@ pub fn move_line_end(cx: &mut Context) {
let line = text.char_to_line(range.head);

// Line end is pos at the start of next line - 1
// subtract another 1 because the line ends with \n
let pos = text.line_to_char(line + 1).saturating_sub(2);
// subtract 3 if the line ending is \r\n, otherwise subtract 2 as I assume all others are just 1 char length
let pos =
text.line_to_char(line + 1)
.saturating_sub(if doc.line_ending == LineEnding::Crlf {
3
} else {
2
});
Range::new(pos, pos)
});

Expand Down Expand Up @@ -331,7 +337,7 @@ where
KeyEvent {
code: KeyCode::Enter,
..
} => '\n',
} => '\n', // TODO: we should be calling doc.line_ending() here
KeyEvent {
code: KeyCode::Char(ch),
..
Expand Down Expand Up @@ -459,7 +465,7 @@ pub fn replace(cx: &mut Context) {
KeyEvent {
code: KeyCode::Enter,
..
} => Some('\n'),
} => Some('\n'), // TODO: we should be calling doc.line_ending() here
_ => None,
};

Expand Down Expand Up @@ -600,8 +606,14 @@ pub fn extend_line_end(cx: &mut Context) {
let line = text.char_to_line(range.head);

// Line end is pos at the start of next line - 1
// subtract another 1 because the line ends with \n
let pos = text.line_to_char(line + 1).saturating_sub(2);
// subtract 3 if the line ending is \r\n, otherwise subtract 2 as I assume all others are just 1 char length
let pos =
text.line_to_char(line + 1)
.saturating_sub(if doc.line_ending == LineEnding::Crlf {
3
} else {
2
});
Range::new(range.anchor, pos)
});

Expand Down Expand Up @@ -887,7 +899,7 @@ pub fn append_mode(cx: &mut Context) {
if selection.iter().any(|range| range.head == end) {
let transaction = Transaction::change(
doc.text(),
std::array::IntoIter::new([(end, end, Some(Tendril::from_char('\n')))]),
std::array::IntoIter::new([(end, end, Some(Tendril::from_char('\n')))]), // TODO: change \n to doc.line_ending()
);
doc.apply(&transaction, view.id);
}
Expand Down Expand Up @@ -1325,7 +1337,7 @@ fn open(cx: &mut Context, open: Open) {
);
let indent = doc.indent_unit().repeat(indent_level);
let mut text = String::with_capacity(1 + indent.len());
text.push('\n');
text.push_str(doc.line_ending());
text.push_str(&indent);
let text = text.repeat(count);

Expand Down Expand Up @@ -1933,7 +1945,7 @@ pub mod insert {
);
let indent = doc.indent_unit().repeat(indent_level);
let mut text = String::with_capacity(1 + indent.len());
text.push('\n');
text.push_str(doc.line_ending());
text.push_str(&indent);

let head = pos + offs + text.chars().count();
Expand All @@ -1954,7 +1966,7 @@ pub mod insert {
if helix_core::auto_pairs::PAIRS.contains(&(prev, curr)) {
// another newline, indent the end bracket one level less
let indent = doc.indent_unit().repeat(indent_level.saturating_sub(1));
text.push('\n');
text.push_str(doc.line_ending());
text.push_str(&indent);
}

Expand Down Expand Up @@ -2065,7 +2077,9 @@ fn _paste(reg: char, doc: &mut Document, view: &View, action: Paste) -> Option<T
);

// if any of values ends \n it's linewise paste
let linewise = values.iter().any(|value| value.ends_with('\n'));
let linewise = values
.iter()
.any(|value| value.ends_with(doc.line_ending()));

let mut values = values.into_iter().map(Tendril::from).chain(repeat);

Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};

use helix_core::{
coords_at_pos,
coords_at_pos, rope_slice_to_line_ending,
syntax::{self, HighlightEvent},
Position, Range,
};
Expand Down Expand Up @@ -175,7 +175,7 @@ impl EditorView {

// iterate over range char by char
for grapheme in RopeGraphemes::new(text) {
if grapheme == "\n" || grapheme == "\r\n" {
if rope_slice_to_line_ending(&grapheme).is_some() {
visual_x = 0;
line += 1;

Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct Document {

diagnostics: Vec<Diagnostic>,
language_server: Option<Arc<helix_lsp::Client>>,
line_ending: LineEnding,
pub line_ending: LineEnding,
}

use std::fmt;
Expand Down

0 comments on commit 856fd95

Please sign in to comment.