From d4b3ff396934b901a7541b015c3ca30d5c570ade Mon Sep 17 00:00:00 2001 From: Shafkath Shuhan Date: Wed, 14 Jul 2021 12:52:51 -0400 Subject: [PATCH 1/3] fix #442 fix #442 fmt --- helix-view/src/document.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index a2bd1c415848..1c2489054ca4 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -456,14 +456,15 @@ impl Document { theme: Option<&Theme>, config_loader: Option<&syntax::Loader>, ) -> Result { - if !path.exists() { - return Ok(Self::default()); - } + let (mut rope, encoding) = if path.exists() { + let mut file = + std::fs::File::open(&path).context(format!("unable to open {:?}", path))?; + from_reader(&mut file, encoding)? + } else { + (Rope::default(), encoding_rs::UTF_8) + }; - let mut file = std::fs::File::open(&path).context(format!("unable to open {:?}", path))?; - let (mut rope, encoding) = from_reader(&mut file, encoding)?; let line_ending = with_line_ending(&mut rope); - let mut doc = Self::from(rope, Some(encoding)); // set the path and try detecting the language From 2a4b3d06c8d8d6d49845848205482ae6c2e570cd Mon Sep 17 00:00:00 2001 From: Shafkath Shuhan Date: Wed, 14 Jul 2021 12:59:20 -0400 Subject: [PATCH 2/3] create Rope from default line ending --- helix-view/src/document.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 1c2489054ca4..989850182ff2 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -461,7 +461,7 @@ impl Document { std::fs::File::open(&path).context(format!("unable to open {:?}", path))?; from_reader(&mut file, encoding)? } else { - (Rope::default(), encoding_rs::UTF_8) + (Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding_rs::UTF_8) }; let line_ending = with_line_ending(&mut rope); From d4f646786e71c518f6cdd83755b7a23fe70a77a2 Mon Sep 17 00:00:00 2001 From: Shafkath Shuhan Date: Wed, 14 Jul 2021 20:53:57 -0400 Subject: [PATCH 3/3] Fix use of encoding in Document::open() --- helix-view/src/document.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 989850182ff2..8fdf7d98966f 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -461,7 +461,8 @@ impl Document { std::fs::File::open(&path).context(format!("unable to open {:?}", path))?; from_reader(&mut file, encoding)? } else { - (Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding_rs::UTF_8) + let encoding = encoding.unwrap_or(encoding_rs::UTF_8); + (Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding) }; let line_ending = with_line_ending(&mut rope);