From e09b438f6d55fc667d1c79b7174b6f86ea8f234e Mon Sep 17 00:00:00 2001 From: MinusGix Date: Mon, 25 Jul 2022 14:05:31 -0500 Subject: [PATCH] Read files lossily, in case they are not utf8 --- lapce-proxy/src/buffer.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lapce-proxy/src/buffer.rs b/lapce-proxy/src/buffer.rs index 68440ad0..7b9f3cd3 100644 --- a/lapce-proxy/src/buffer.rs +++ b/lapce-proxy/src/buffer.rs @@ -4,7 +4,7 @@ use std::ffi::OsString; use std::fs; use std::fs::File; -use std::io::Write; +use std::io::{Read, Write}; use std::path::PathBuf; use std::{borrow::Cow, path::Path, time::SystemTime}; use xi_rope::{interval::IntervalBounds, rope::Rope, RopeDelta}; @@ -127,11 +127,28 @@ pub fn is_empty(&self) -> bool { } pub fn load_file(path: &Path) -> Result { - Ok(fs::read_to_string(path)?) + Ok(read_path_to_string_lossy(path)?) } fn load_rope(path: &Path) -> Result { - Ok(Rope::from(fs::read_to_string(path)?)) + Ok(Rope::from(read_path_to_string_lossy(path)?)) +} + +pub fn read_path_to_string_lossy>( + path: P, +) -> Result { + let path = path.as_ref(); + + let mut file = File::open(path)?; + // Read the file in as bytes + let mut buffer = Vec::new(); + file.read_to_end(&mut buffer)?; + + // Parse the file contents as utf8, replacing non-utf8 data with the + // replacement character + let contents = String::from_utf8_lossy(&buffer); + + Ok(contents.to_string()) } fn language_id_from_path(path: &Path) -> Option<&str> {