mirror of https://github.com/helix-editor/helix
rebase on branch line_ending_detection
parent
a364d6c383
commit
3756c21bae
|
@ -104,6 +104,7 @@ pub use position::{coords_at_pos, pos_at_coords, Position};
|
||||||
pub use selection::{Range, Selection};
|
pub use selection::{Range, Selection};
|
||||||
pub use smallvec::SmallVec;
|
pub use smallvec::SmallVec;
|
||||||
pub use syntax::Syntax;
|
pub use syntax::Syntax;
|
||||||
|
pub use graphemes::RopeGraphemes;
|
||||||
|
|
||||||
pub use diagnostic::Diagnostic;
|
pub use diagnostic::Diagnostic;
|
||||||
pub use state::State;
|
pub use state::State;
|
||||||
|
|
|
@ -179,7 +179,7 @@ impl EditorView {
|
||||||
|
|
||||||
// iterate over range char by char
|
// iterate over range char by char
|
||||||
for grapheme in RopeGraphemes::new(text) {
|
for grapheme in RopeGraphemes::new(text) {
|
||||||
if grapheme == "\n" {
|
if grapheme == "\n" || grapheme == "\r\n" {
|
||||||
visual_x = 0;
|
visual_x = 0;
|
||||||
line += 1;
|
line += 1;
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ use helix_core::{
|
||||||
chars::{char_is_linebreak, char_is_whitespace},
|
chars::{char_is_linebreak, char_is_whitespace},
|
||||||
history::History,
|
history::History,
|
||||||
syntax::{LanguageConfiguration, LOADER},
|
syntax::{LanguageConfiguration, LOADER},
|
||||||
ChangeSet, Diagnostic, Rope, Selection, State, Syntax, Transaction,
|
ChangeSet, Diagnostic, History, Rope, RopeSlice, RopeGraphemes, Selection, State, Syntax, Transaction,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{DocumentId, ViewId};
|
use crate::{DocumentId, ViewId};
|
||||||
|
@ -22,12 +22,28 @@ pub enum Mode {
|
||||||
Insert,
|
Insert,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum IndentStyle {
|
pub enum IndentStyle {
|
||||||
Tabs,
|
Tabs,
|
||||||
Spaces(u8),
|
Spaces(u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Represents one of the valid Unicode line endings.
|
||||||
|
/// Also acts as an index into `LINE_ENDINGS`.
|
||||||
|
#[derive(PartialEq, Copy, Clone, Debug)]
|
||||||
|
pub enum LineEnding {
|
||||||
|
None = 0, // No line ending
|
||||||
|
CRLF = 1, // CarriageReturn followed by LineFeed
|
||||||
|
LF = 2, // U+000A -- LineFeed
|
||||||
|
VT = 3, // U+000B -- VerticalTab
|
||||||
|
FF = 4, // U+000C -- FormFeed
|
||||||
|
CR = 5, // U+000D -- CarriageReturn
|
||||||
|
NEL = 6, // U+0085 -- NextLine
|
||||||
|
LS = 7, // U+2028 -- Line Separator
|
||||||
|
PS = 8, // U+2029 -- ParagraphSeparator
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Document {
|
pub struct Document {
|
||||||
// rope + selection
|
// rope + selection
|
||||||
pub(crate) id: DocumentId,
|
pub(crate) id: DocumentId,
|
||||||
|
@ -61,6 +77,7 @@ pub struct Document {
|
||||||
|
|
||||||
diagnostics: Vec<Diagnostic>,
|
diagnostics: Vec<Diagnostic>,
|
||||||
language_server: Option<Arc<helix_lsp::Client>>,
|
language_server: Option<Arc<helix_lsp::Client>>,
|
||||||
|
line_ending: LineEnding
|
||||||
}
|
}
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
@ -146,11 +163,61 @@ pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
|
||||||
std::env::current_dir().map(|current_dir| normalize_path(¤t_dir.join(path)))
|
std::env::current_dir().map(|current_dir| normalize_path(¤t_dir.join(path)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn auto_detect_line_ending(doc: &Rope) -> LineEnding {
|
||||||
|
// based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162
|
||||||
|
|
||||||
|
let mut ending = LineEnding::None;
|
||||||
|
for line in doc.lines().take(1) { // check first line only - unsure how sound this is
|
||||||
|
// Get the line ending
|
||||||
|
ending = if line.len_chars() == 1 {
|
||||||
|
let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..))
|
||||||
|
.last()
|
||||||
|
.unwrap();
|
||||||
|
rope_slice_to_line_ending(&g)
|
||||||
|
} else if line.len_chars() > 1 {
|
||||||
|
let g = RopeGraphemes::new(line.slice((line.len_chars() - 2)..))
|
||||||
|
.last()
|
||||||
|
.unwrap();
|
||||||
|
rope_slice_to_line_ending(&g)
|
||||||
|
} else {
|
||||||
|
LineEnding::None
|
||||||
|
};
|
||||||
|
}
|
||||||
|
ending
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rope_slice_to_line_ending(g: &RopeSlice) -> LineEnding {
|
||||||
|
if let Some(text) = g.as_str() {
|
||||||
|
str_to_line_ending(text)
|
||||||
|
} else if g == "\u{000D}\u{000A}" {
|
||||||
|
LineEnding::CRLF
|
||||||
|
} else {
|
||||||
|
// Not a line ending
|
||||||
|
LineEnding::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn str_to_line_ending(g: &str) -> LineEnding {
|
||||||
|
match g {
|
||||||
|
"\u{000D}\u{000A}" => LineEnding::CRLF,
|
||||||
|
"\u{000A}" => LineEnding::LF,
|
||||||
|
"\u{000B}" => LineEnding::VT,
|
||||||
|
"\u{000C}" => LineEnding::FF,
|
||||||
|
"\u{000D}" => LineEnding::CR,
|
||||||
|
"\u{0085}" => LineEnding::NEL,
|
||||||
|
"\u{2028}" => LineEnding::LS,
|
||||||
|
"\u{2029}" => LineEnding::PS,
|
||||||
|
|
||||||
|
// Not a line ending
|
||||||
|
_ => LineEnding::None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
use helix_lsp::lsp;
|
use helix_lsp::lsp;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
impl Document {
|
impl Document {
|
||||||
pub fn new(text: Rope) -> Self {
|
pub fn new(text: Rope, line_ending: LineEnding) -> Self {
|
||||||
let changes = ChangeSet::new(&text);
|
let changes = ChangeSet::new(&text);
|
||||||
let old_state = None;
|
let old_state = None;
|
||||||
|
|
||||||
|
@ -171,6 +238,7 @@ impl Document {
|
||||||
history: Cell::new(History::default()),
|
history: Cell::new(History::default()),
|
||||||
last_saved_revision: 0,
|
last_saved_revision: 0,
|
||||||
language_server: None,
|
language_server: None,
|
||||||
|
line_ending: line_ending
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +258,10 @@ impl Document {
|
||||||
doc
|
doc
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut doc = Self::new(doc);
|
// search for line endings
|
||||||
|
let line_ending = auto_detect_line_ending(&doc);
|
||||||
|
|
||||||
|
let mut doc = Self::new(doc, line_ending);
|
||||||
// set the path and try detecting the language
|
// set the path and try detecting the language
|
||||||
doc.set_path(&path)?;
|
doc.set_path(&path)?;
|
||||||
doc.detect_indent_style();
|
doc.detect_indent_style();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{theme::Theme, tree::Tree, Document, DocumentId, RegisterSelection, View, ViewId};
|
use crate::{theme::Theme, tree::Tree, Document, DocumentId, RegisterSelection, View, ViewId, LineEnding};
|
||||||
use tui::layout::Rect;
|
use tui::layout::Rect;
|
||||||
use tui::terminal::CursorKind;
|
use tui::terminal::CursorKind;
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ impl Editor {
|
||||||
|
|
||||||
pub fn new_file(&mut self, action: Action) -> DocumentId {
|
pub fn new_file(&mut self, action: Action) -> DocumentId {
|
||||||
use helix_core::Rope;
|
use helix_core::Rope;
|
||||||
let doc = Document::new(Rope::from("\n"));
|
let doc = Document::new(Rope::from("\n"), LineEnding::LF);
|
||||||
let id = self.documents.insert(doc);
|
let id = self.documents.insert(doc);
|
||||||
self.documents[id].id = id;
|
self.documents[id].id = id;
|
||||||
self.switch(id, action);
|
self.switch(id, action);
|
||||||
|
|
|
@ -10,6 +10,7 @@ new_key_type! { pub struct DocumentId; }
|
||||||
new_key_type! { pub struct ViewId; }
|
new_key_type! { pub struct ViewId; }
|
||||||
|
|
||||||
pub use document::Document;
|
pub use document::Document;
|
||||||
|
pub use document::LineEnding;
|
||||||
pub use editor::Editor;
|
pub use editor::Editor;
|
||||||
pub use register_selection::RegisterSelection;
|
pub use register_selection::RegisterSelection;
|
||||||
pub use theme::Theme;
|
pub use theme::Theme;
|
||||||
|
|
Loading…
Reference in New Issue