Skip to content

Commit

Permalink
added more changes from pr review for line_ending_detection
Browse files Browse the repository at this point in the history
  • Loading branch information
janhrastnik committed Jun 16, 2021
1 parent 5eb6918 commit 9c419fe
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 37 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, DEFAULT_LINE_ENDING, LineEnding};
pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
19 changes: 5 additions & 14 deletions helix-core/src/line_ending.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
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
VT, // U+000B -- VerticalTab
FF, // U+000C -- FormFeed
CR, // U+000D -- CarriageReturn
Nel, // U+0085 -- NextLine
LS, // U+2028 -- Line Separator
PS, // U+2029 -- ParagraphSeparator
}

pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
Expand All @@ -28,13 +26,9 @@ pub fn str_to_line_ending(g: &str) -> Option<LineEnding> {
match g {
"\u{000D}\u{000A}" => Some(LineEnding::Crlf),
"\u{000A}" => Some(LineEnding::LF),
"\u{000B}" => Some(LineEnding::VT),
"\u{000C}" => Some(LineEnding::FF),
"\u{000D}" => Some(LineEnding::CR),
"\u{0085}" => Some(LineEnding::Nel),
"\u{2028}" => Some(LineEnding::LS),
"\u{2029}" => Some(LineEnding::PS),

// Not a line ending
_ => None,
}
Expand Down Expand Up @@ -65,10 +59,7 @@ pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
ending
}

pub fn default_line_ending() -> Option<LineEnding> {
if cfg!(windows) {
Some(LineEnding::Crlf)
} else {
Some(LineEnding::LF)
}
}
#[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;
24 changes: 6 additions & 18 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ use std::path::{Component, Path, PathBuf};
use std::sync::Arc;

use helix_core::{
<<<<<<< HEAD
chars::{char_is_linebreak, char_is_whitespace},
history::History,
=======
auto_detect_line_ending, default_line_ending,
>>>>>>> 491a8b3 (resolved conflict in rebase)
auto_detect_line_ending, DEFAULT_LINE_ENDING, history::History,
syntax::{LanguageConfiguration, LOADER},
ChangeSet, Diagnostic, History, LineEnding, Rope, Selection, State, Syntax, Transaction,
ChangeSet, Diagnostic, LineEnding, Rope, Selection, State, Syntax, Transaction,
};

use crate::{DocumentId, ViewId};
Expand All @@ -26,8 +22,6 @@ pub enum Mode {
Insert,
}

<<<<<<< HEAD

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum IndentStyle {
Tabs,
Expand All @@ -49,8 +43,6 @@ pub enum LineEnding {
PS = 8, // U+2029 -- ParagraphSeparator
}

=======
>>>>>>> 491a8b3 (resolved conflict in rebase)
pub struct Document {
// rope + selection
pub(crate) id: DocumentId,
Expand Down Expand Up @@ -84,7 +76,7 @@ pub struct Document {

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

use std::fmt;
Expand Down Expand Up @@ -177,7 +169,6 @@ impl Document {
pub fn new(text: Rope) -> Self {
let changes = ChangeSet::new(&text);
let old_state = None;
let line_ending = default_line_ending();

Self {
id: DocumentId::default(),
Expand All @@ -196,7 +187,7 @@ impl Document {
history: Cell::new(History::default()),
last_saved_revision: 0,
language_server: None,
line_ending,
line_ending: DEFAULT_LINE_ENDING,
}
}

Expand All @@ -217,16 +208,13 @@ impl Document {
};

// search for line endings
let line_ending = auto_detect_line_ending(&doc);
let line_ending = auto_detect_line_ending(&doc).unwrap_or(DEFAULT_LINE_ENDING);

let mut doc = Self::new(doc);
// set the path and try detecting the language
doc.set_path(&path)?;
<<<<<<< HEAD
doc.detect_indent_style();
=======
doc.set_line_ending(line_ending);
>>>>>>> 491a8b3 (resolved conflict in rebase)

Ok(doc)
}
Expand Down Expand Up @@ -485,7 +473,7 @@ impl Document {
self.selections.insert(view_id, selection);
}

pub fn set_line_ending(&mut self, line_ending: Option<LineEnding>) {
pub fn set_line_ending(&mut self, line_ending: LineEnding) {
self.line_ending = line_ending;
}

Expand Down
4 changes: 0 additions & 4 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ use slotmap::SlotMap;

use anyhow::Error;

<<<<<<< HEAD
pub use helix_core::diagnostic::Severity;
pub use helix_core::register::Registers;
use helix_core::Position;
=======
pub use helix_core::{diagnostic::Severity, LineEnding};
>>>>>>> 491a8b3 (resolved conflict in rebase)

#[derive(Debug)]
pub struct Editor {
Expand Down

0 comments on commit 9c419fe

Please sign in to comment.