on master help sign in
resolved conflict in rebase
commit5eb69183926ab2f781aa08abf587ba338027854b authorJan Hrastnik <jan.hrastnik2@gmail.com> dateWed 16 Jun 2021, 17:05 (5 years ago) parent17f69a0
5 files changed+103−59
helix-core/src/lib.rs+2 helix-core/src/line_ending.rs+74 helix-view/src/document.rs+21−54 helix-view/src/editor.rs+6−4 helix-view/src/lib.rs−1
helix-core/src/lib.rs+2
@@ -6,6 +6,7 @@ pub mod diagnostic;
66pub mod graphemes;
77pub mod history;
88pub mod indent;
9+pub mod line_ending;
910pub mod macros;
1011pub mod match_brackets;
1112pub mod movement;
@@ -109,4 +110,5 @@ pub use syntax::Syntax;
109110pub use diagnostic::Diagnostic;
110111pub use state::State;
111112
113+pub use line_ending::{auto_detect_line_ending, default_line_ending, LineEnding};
112114pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
helix-core/src/line_ending.rsnew file+74
@@ -0,0 +1,74 @@
1+use crate::{Rope, RopeGraphemes, RopeSlice};
2+
3+/// Represents one of the valid Unicode line endings.
4+#[derive(PartialEq, Copy, Clone, Debug)]
5+pub enum LineEnding {
6+ Crlf, // CarriageReturn followed by LineFeed
7+ LF, // U+000A -- LineFeed
8+ VT, // U+000B -- VerticalTab
9+ FF, // U+000C -- FormFeed
10+ CR, // U+000D -- CarriageReturn
11+ Nel, // U+0085 -- NextLine
12+ LS, // U+2028 -- Line Separator
13+ PS, // U+2029 -- ParagraphSeparator
14+}
15+
16+pub fn rope_slice_to_line_ending(g: &RopeSlice) -> Option<LineEnding> {
17+ if let Some(text) = g.as_str() {
18+ str_to_line_ending(text)
19+ } else if g == "\u{000D}\u{000A}" {
20+ Some(LineEnding::Crlf)
21+ } else {
22+ // Not a line ending
23+ None
24+ }
25+}
26+
27+pub fn str_to_line_ending(g: &str) -> Option<LineEnding> {
28+ match g {
29+ "\u{000D}\u{000A}" => Some(LineEnding::Crlf),
30+ "\u{000A}" => Some(LineEnding::LF),
31+ "\u{000B}" => Some(LineEnding::VT),
32+ "\u{000C}" => Some(LineEnding::FF),
33+ "\u{000D}" => Some(LineEnding::CR),
34+ "\u{0085}" => Some(LineEnding::Nel),
35+ "\u{2028}" => Some(LineEnding::LS),
36+ "\u{2029}" => Some(LineEnding::PS),
37+
38+ // Not a line ending
39+ _ => None,
40+ }
41+}
42+
43+pub fn auto_detect_line_ending(doc: &Rope) -> Option<LineEnding> {
44+ // based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162
45+
46+ let mut ending = None;
47+ for line in doc.lines().take(1) {
48+ // check first line only - unsure how sound this is
49+ ending = match line.len_chars() {
50+ 1 => {
51+ let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..))
52+ .last()
53+ .unwrap();
54+ rope_slice_to_line_ending(&g)
55+ }
56+ n if n > 1 => {
57+ let g = RopeGraphemes::new(line.slice((line.len_chars() - 2)..))
58+ .last()
59+ .unwrap();
60+ rope_slice_to_line_ending(&g)
61+ }
62+ _ => None,
63+ }
64+ }
65+ ending
66+}
67+
68+pub fn default_line_ending() -> Option<LineEnding> {
69+ if cfg!(windows) {
70+ Some(LineEnding::Crlf)
71+ } else {
72+ Some(LineEnding::LF)
73+ }
74+}
helix-view/src/document.rs+21−54
@@ -5,11 +5,14 @@ use std::path::{Component, Path, PathBuf};
55use std::sync::Arc;
66
77use helix_core::{
8+<<<<<<< HEAD
89 chars::{char_is_linebreak, char_is_whitespace},
910 history::History,
11+=======
12+ auto_detect_line_ending, default_line_ending,
13+>>>>>>> 491a8b3 (resolved conflict in rebase)
1014 syntax::{LanguageConfiguration, LOADER},
11− ChangeSet, Diagnostic, History, Rope, RopeGraphemes, RopeSlice, Selection, State, Syntax,
12− Transaction,
15+ ChangeSet, Diagnostic, History, LineEnding, Rope, Selection, State, Syntax, Transaction,
1316};
1417
1518use crate::{DocumentId, ViewId};
@@ -23,6 +26,7 @@ pub enum Mode {
2326 Insert,
2427}
2528
29+<<<<<<< HEAD
2630
2731#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
2832pub enum IndentStyle {
@@ -45,6 +49,8 @@ pub enum LineEnding {
4549 PS = 8, // U+2029 -- ParagraphSeparator
4650}
4751
52+=======
53+>>>>>>> 491a8b3 (resolved conflict in rebase)
4854pub struct Document {
4955 // rope + selection
5056 pub(crate) id: DocumentId,
@@ -78,7 +84,7 @@ pub struct Document {
7884
7985 diagnostics: Vec<Diagnostic>,
8086 language_server: Option<Arc<helix_lsp::Client>>,
81− _line_ending: LineEnding,
87+ line_ending: Option<LineEnding>,
8288}
8389
8490use std::fmt;
@@ -164,61 +170,14 @@ pub fn canonicalize_path(path: &Path) -> std::io::Result<PathBuf> {
164170 std::env::current_dir().map(|current_dir| normalize_path(&current_dir.join(path)))
165171}
166172
167−pub fn auto_detect_line_ending(doc: &Rope) -> LineEnding {
168− // based on https://github.com/cessen/led/blob/27572c8838a1c664ee378a19358604063881cc1d/src/editor/mod.rs#L88-L162
169−
170− let mut ending = LineEnding::None;
171− for line in doc.lines().take(1) { // check first line only - unsure how sound this is
172− ending = match line.len_chars() {
173− 1 => { let g = RopeGraphemes::new(line.slice((line.len_chars() - 1)..))
174− .last()
175− .unwrap();
176− rope_slice_to_line_ending(&g)}
177− n if n > 1 => { let g = RopeGraphemes::new(line.slice((line.len_chars() - 2)..))
178− .last()
179− .unwrap();
180− rope_slice_to_line_ending(&g) }
181− _ => LineEnding::None
182−
183− }
184− }
185− ending
186−}
187−
188−pub fn rope_slice_to_line_ending(g: &RopeSlice) -> LineEnding {
189− if let Some(text) = g.as_str() {
190− str_to_line_ending(text)
191− } else if g == "\u{000D}\u{000A}" {
192− LineEnding::Crlf
193− } else {
194− // Not a line ending
195− LineEnding::None
196− }
197−}
198−
199−pub fn str_to_line_ending(g: &str) -> LineEnding {
200− match g {
201− "\u{000D}\u{000A}" => LineEnding::Crlf,
202− "\u{000A}" => LineEnding::LF,
203− "\u{000B}" => LineEnding::VT,
204− "\u{000C}" => LineEnding::FF,
205− "\u{000D}" => LineEnding::CR,
206− "\u{0085}" => LineEnding::Nel,
207− "\u{2028}" => LineEnding::LS,
208− "\u{2029}" => LineEnding::PS,
209−
210− // Not a line ending
211− _ => LineEnding::None,
212− }
213−}
214−
215173use helix_lsp::lsp;
216174use url::Url;
217175
218176impl Document {
219− pub fn new(text: Rope, _line_ending: LineEnding) -> Self {
177+ pub fn new(text: Rope) -> Self {
220178 let changes = ChangeSet::new(&text);
221179 let old_state = None;
180+ let line_ending = default_line_ending();
222181
223182 Self {
224183 id: DocumentId::default(),
@@ -237,7 +196,7 @@ impl Document {
237196 history: Cell::new(History::default()),
238197 last_saved_revision: 0,
239198 language_server: None,
240− _line_ending
199+ line_ending,
241200 }
242201 }
243202
@@ -260,10 +219,14 @@ impl Document {
260219 // search for line endings
261220 let line_ending = auto_detect_line_ending(&doc);
262221
263− let mut doc = Self::new(doc, line_ending);
222+ let mut doc = Self::new(doc);
264223 // set the path and try detecting the language
265224 doc.set_path(&path)?;
225+<<<<<<< HEAD
266226 doc.detect_indent_style();
227+=======
228+ doc.set_line_ending(line_ending);
229+>>>>>>> 491a8b3 (resolved conflict in rebase)
267230
268231 Ok(doc)
269232 }
@@ -522,6 +485,10 @@ impl Document {
522485 self.selections.insert(view_id, selection);
523486 }
524487
488+ pub fn set_line_ending(&mut self, line_ending: Option<LineEnding>) {
489+ self.line_ending = line_ending;
490+ }
491+
525492 fn _apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
526493 let old_doc = self.text().clone();
527494
helix-view/src/editor.rs+6−4
@@ -1,6 +1,4 @@
1−use crate::{
2− theme::Theme, tree::Tree, Document, DocumentId, LineEnding, RegisterSelection, View, ViewId,
3−};
1+use crate::{theme::Theme, tree::Tree, Document, DocumentId, RegisterSelection, View, ViewId};
42use tui::layout::Rect;
53use tui::terminal::CursorKind;
64
@@ -10,9 +8,13 @@ use slotmap::SlotMap;
108
119use anyhow::Error;
1210
11+<<<<<<< HEAD
1312pub use helix_core::diagnostic::Severity;
1413pub use helix_core::register::Registers;
1514use helix_core::Position;
15+=======
16+pub use helix_core::{diagnostic::Severity, LineEnding};
17+>>>>>>> 491a8b3 (resolved conflict in rebase)
1618
1719#[derive(Debug)]
1820pub struct Editor {
@@ -150,7 +152,7 @@ impl Editor {
150152
151153 pub fn new_file(&mut self, action: Action) -> DocumentId {
152154 use helix_core::Rope;
153− let doc = Document::new(Rope::from("\n"), LineEnding::LF);
155+ let doc = Document::new(Rope::from("\n"));
154156 let id = self.documents.insert(doc);
155157 self.documents[id].id = id;
156158 self.switch(id, action);
helix-view/src/lib.rs−1
@@ -10,7 +10,6 @@ new_key_type! { pub struct DocumentId; }
1010new_key_type! { pub struct ViewId; }
1111
1212pub use document::Document;
13−pub use document::LineEnding;
1413pub use editor::Editor;
1514pub use register_selection::RegisterSelection;
1615pub use theme::Theme;