@setoelkahfi / svara / commits / 5ff3e09

fix: added error check for reading spaces new files

Signed-off-by: mellobacon <mellodev@outlook.com>

mellobacon committed Feb 10, 2024 at 16:12 UTC 5ff3e0955e79dfe77b5d145ab5c51a6f605c067f
2 files changed +17 -4
src-tauri/src/main.rs
+14 -4
@@ -172,27 +172,37 @@ fn read_file(path: &str) -> FileData {
172 if let Some(data) = encoding_rs::Encoding::for_bom(&bytes) {
173 let (text, encoding, _) = data.0.decode(&bytes);
174 let hay = text.to_string();
175 - let spaces = regex.captures(hay.as_str()).unwrap();
175 + let spaces = regex.captures(hay.as_str());
176 +
177 + let space_count = match spaces {
178 + Some(capture) => capture.len(),
179 + None => 0
180 + };
181
182 file_data = FileData {
183 text: text.to_string(),
184 encoding: encoding.name().to_string(),
185 extension: ext.to_string(),
186 bom: true,
182 - spaces: spaces[1].to_owned().len()
187 + spaces: space_count
188 };
189 info!("File BOM found. Encoding with {}...", encoding.name());
190 } else {
191 let (text, encoding, _) = encoding_rs::UTF_8.decode(&bytes);
192 let hay = text.to_string();
188 - let spaces = regex.captures(hay.as_str()).unwrap();
193 + let spaces = regex.captures(hay.as_str());
194 +
195 + let space_count = match spaces {
196 + Some(capture) => capture.len(),
197 + None => 0
198 + };
199
200 file_data = FileData {
201 text: text.to_string(),
202 encoding: encoding.name().to_string(),
203 extension: ext.to_string(),
204 bom: false,
195 - spaces: spaces[1].to_owned().len()
205 + spaces: space_count
206 };
207 info!(
208 "No file BOM found. Defaulting to {} encoding...",
src/lib/Tab/Tab.ts
+3
@@ -75,6 +75,9 @@ export class Tab {
75 let fileData = {text: "", encoding: "UTF-8", extension: "", bom: false, spaces: await appSettings.get("editor.tabSize")};
76 try {
77 fileData = await invoke("read_file", {path: path});
78 + if (fileData.spaces === 0) {
79 + fileData.spaces = await appSettings.get("editor.tabSize")
80 + }
81 } catch (error) {
82 warn(`Can't read file content in ${path}. Setting to empty string. Error: ${error}`, {file: "Tab.ts", line: 79});
83 }