Skip to content

Commit

Permalink
added some tests and a line_ending helper function in document.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
janhrastnik committed Jun 16, 2021
1 parent e4849f4 commit a9a718c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion helix-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,5 @@ pub use syntax::Syntax;
pub use diagnostic::Diagnostic;
pub use state::State;

pub use line_ending::{auto_detect_line_ending, DEFAULT_LINE_ENDING, LineEnding};
pub use line_ending::{auto_detect_line_ending, LineEnding, DEFAULT_LINE_ENDING};
pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
64 changes: 60 additions & 4 deletions helix-core/src/line_ending.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::{Rope, RopeGraphemes, RopeSlice};

/// Represents one of the valid Unicode line endings.
/// VT, FF and PS are excluded here, as we don't expect them to show up as a default line break
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum LineEnding {
Crlf, // CarriageReturn followed by LineFeed
LF, // U+000A -- LineFeed
CR, // U+000D -- CarriageReturn
Nel, // U+0085 -- NextLine
LS, // U+2028 -- Line Separator
VT, // U+000B -- VerticalTab
FF, // U+000C -- FormFeed
PS, // U+2029 -- ParagraphSeparator
}

pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
Expand Down Expand Up @@ -38,8 +40,8 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
// based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162

let mut ending = None;
for line in doc.lines().take(1) {
// check first line only - unsure how sound this is
// return first matched line ending. Not all possible line endings are being matched, as they might be special-use only
for line in doc.lines().take(100) {
ending = match line.len_chars() {
1 => {
let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..))
Expand All @@ -54,6 +56,9 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
rope_slice_to_line_ending(&g)
}
_ => None,
};
if ending.is_some() {
return ending;
}
}
ending
Expand All @@ -62,4 +67,55 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
#[cfg(target_os = "windows")]
pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::Crlf;
#[cfg(not(target_os = "windows"))]
pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::LF;
pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::LF;

#[cfg(test)]
mod line_ending_tests {
use super::*;

#[test]
fn test_autodetect() {
assert_eq!(
auto_detect_line_ending(&Rope::from_str("\n")),
Some(LineEnding::LF)
);
assert_eq!(
auto_detect_line_ending(&Rope::from_str("\r\n")),
Some(LineEnding::Crlf)
);
assert_eq!(auto_detect_line_ending(&Rope::from_str("hello")), None);
assert_eq!(auto_detect_line_ending(&Rope::from_str("")), None);
assert_eq!(
auto_detect_line_ending(&Rope::from_str("hello\nhelix\r\n")),
Some(LineEnding::LF)
);
assert_eq!(
auto_detect_line_ending(&Rope::from_str("a formfeed\u{000C}")),
None
);
assert_eq!(
auto_detect_line_ending(&Rope::from_str("\n\u{000A}\n \u{000A}")),
Some(LineEnding::LF)
);
assert_eq!(
auto_detect_line_ending(&Rope::from_str(
"a formfeed\u{000C} with a\u{000C} linefeed\u{000A}"
)),
Some(LineEnding::LF)
);
assert_eq!(auto_detect_line_ending(&Rope::from_str("a formfeed\u{000C} with a\u{000C} carriage return linefeed\u{000D}\u{000A} and a linefeed\u{000A}")), Some(LineEnding::Crlf));
}

#[test]
fn test_rope_slice_to_line_ending() {
let r = Rope::from_str("\r\n");
assert_eq!(
rope_slice_to_line_ending(&r.slice(1..2)),
Some(LineEnding::LF)
);
assert_eq!(
rope_slice_to_line_ending(&r.slice(0..2)),
Some(LineEnding::Crlf)
);
}
}
16 changes: 15 additions & 1 deletion helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::sync::Arc;

use helix_core::{
chars::{char_is_linebreak, char_is_whitespace},
auto_detect_line_ending, DEFAULT_LINE_ENDING, history::History,
syntax::{LanguageConfiguration, LOADER},
ChangeSet, Diagnostic, LineEnding, Rope, Selection, State, Syntax, Transaction,
DEFAULT_LINE_ENDING,
};

use crate::{DocumentId, ViewId};
Expand Down Expand Up @@ -740,6 +740,20 @@ impl Document {
pub fn set_diagnostics(&mut self, diagnostics: Vec<Diagnostic>) {
self.diagnostics = diagnostics;
}

pub fn line_ending(&self) -> &str {
match self.line_ending {
LineEnding::Crlf => "\u{000D}\u{000A}",
LineEnding::LF => "\u{000A}",
LineEnding::Nel => "\u{0085}",
LineEnding::LS => "\u{2028}",
LineEnding::CR => "\u{000D}",
_ => panic!(
"Unexpected line ending: {:?}, expected Crlf, LF, CR, Nel, or LS.",
self.line_ending
),
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit a9a718c

Please sign in to comment.