changed some hardcoded newlines, removed a else if in line_ending.rs

This commit is contained in:
Jan Hrastnik 2021-06-20 01:24:36 +02:00
parent 1e80fbb602
commit 701eb0dd68
4 changed files with 22 additions and 11 deletions

View File

@ -38,9 +38,8 @@ pub fn as_str(&self) -> &str {
pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
if let Some(text) = g.as_str() {
str_to_line_ending(text)
} else if g == "\u{000D}\u{000A}" {
Some(LineEnding::Crlf)
} else {
}
else {
// Not a line ending
None
}

View File

@ -5,7 +5,7 @@
use crate::{
coords_at_pos,
graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary},
pos_at_coords, Position, Range, RopeSlice,
pos_at_coords, Position, Range, RopeSlice, get_line_ending
};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@ -38,7 +38,9 @@ pub fn move_horizontally(
}
Direction::Forward => {
// Line end is pos at the start of next line - 1
let end = slice.line_to_char(line + 1).saturating_sub(1);
let end = slice.line_to_char(line + 1).saturating_sub(get_line_ending(&slice.line(line))
.map(|le| le.len_chars())
.unwrap_or(0));
nth_next_grapheme_boundary(slice, pos, count).min(end)
}
};

View File

@ -6,7 +6,7 @@
regex::{self, Regex},
register::{self, Register, Registers},
search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice, Selection,
SmallVec, Tendril, Transaction,
SmallVec, Tendril, Transaction, DEFAULT_LINE_ENDING,
};
use helix_view::{
@ -627,7 +627,7 @@ fn replace(cx: &mut Context) {
KeyEvent {
code: KeyCode::Enter,
..
} => Some('\n'),
} => Some('\n'), // TODO: replace this with DEFAULT_LINE_ENDING
_ => None,
};
@ -982,7 +982,13 @@ fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId
// then delete
let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| {
let max_to = doc.text().len_chars().saturating_sub(1);
let alltext = doc.text();
let line = alltext.char_to_line(range.head);
let max_to = doc.text().len_chars().saturating_sub(
get_line_ending(&alltext.line(line))
.map(|le| le.len_chars())
.unwrap_or(0),
);
let to = std::cmp::min(max_to, range.to() + 1);
(range.from(), to, None)
});
@ -1644,8 +1650,12 @@ fn append_to_line(cx: &mut Context) {
let selection = doc.selection(view.id).transform(|range| {
let text = doc.text();
let line = text.char_to_line(range.head);
// we can't use line_to_char(line + 1) - 2 because the last line might not contain \n
let pos = (text.line_to_char(line) + text.line(line).len_chars()).saturating_sub(1);
// we can't use line_to_char(line + 1) - 2 because the last line might not contain a newline
let pos = (text.line_to_char(line) + text.line(line).len_chars()).saturating_sub(
get_line_ending(&text.line(line))
.map(|le| le.len_chars())
.unwrap_or(0),
);
Range::new(pos, pos)
});
doc.set_selection(view.id, selection);

View File

@ -255,7 +255,7 @@ pub fn load(path: PathBuf) -> Result<Self, Error> {
use std::{fs::File, io::BufReader};
let doc = if !path.exists() {
Rope::from("\n")
Rope::from(DEFAULT_LINE_ENDING.as_str())
} else {
let file = File::open(&path).context(format!("unable to open {:?}", path))?;
let mut doc = Rope::from_reader(BufReader::new(file))?;