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

imgbot
Jan Hrastnik 3 years ago
parent 1e80fbb602
commit 701eb0dd68

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

@ -5,7 +5,7 @@ use ropey::iter::Chars;
use crate::{ use crate::{
coords_at_pos, coords_at_pos,
graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary}, 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)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
@ -38,7 +38,9 @@ pub fn move_horizontally(
} }
Direction::Forward => { Direction::Forward => {
// Line end is pos at the start of next line - 1 // 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) nth_next_grapheme_boundary(slice, pos, count).min(end)
} }
}; };

@ -6,7 +6,7 @@ use helix_core::{
regex::{self, Regex}, regex::{self, Regex},
register::{self, Register, Registers}, register::{self, Register, Registers},
search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice, Selection, search, selection, Change, ChangeSet, LineEnding, Position, Range, Rope, RopeSlice, Selection,
SmallVec, Tendril, Transaction, SmallVec, Tendril, Transaction, DEFAULT_LINE_ENDING,
}; };
use helix_view::{ use helix_view::{
@ -627,7 +627,7 @@ fn replace(cx: &mut Context) {
KeyEvent { KeyEvent {
code: KeyCode::Enter, code: KeyCode::Enter,
.. ..
} => Some('\n'), } => Some('\n'), // TODO: replace this with DEFAULT_LINE_ENDING
_ => None, _ => None,
}; };
@ -982,7 +982,13 @@ fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId
// then delete // then delete
let transaction = let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view_id), |range| { 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); let to = std::cmp::min(max_to, range.to() + 1);
(range.from(), to, None) (range.from(), to, None)
}); });
@ -1644,8 +1650,12 @@ fn append_to_line(cx: &mut Context) {
let selection = doc.selection(view.id).transform(|range| { let selection = doc.selection(view.id).transform(|range| {
let text = doc.text(); let text = doc.text();
let line = text.char_to_line(range.head); 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 // 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(1); 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) Range::new(pos, pos)
}); });
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);

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

Loading…
Cancel
Save