@setoelkahfi / svara / commits / 702e4f2

chore: refactored indent detection

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

mellobacon committed Apr 15, 2024 at 10:25 UTC 702e4f2e7887870ed4c455b00dba4e6434e7ba41
1 file changed +52 -13
src-tauri/src/main.rs
+52 -13
@@ -9,7 +9,7 @@ const LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::Webview];
9 #[cfg(not(debug_assertions))]
10 const LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::LogDir];
11
12 -use std::collections::HashMap;
12 +use std::collections::{self, HashMap};
13 use std::fs::File;
14 use std::io::{Write, BufReader};
15 use std::path::{Path, PathBuf};
@@ -22,7 +22,6 @@ use tauri::plugin::TauriPlugin;
22 use tauri_plugin_log::{LogTarget, RotationStrategy};
23 use portable_pty::{native_pty_system, PtySize};
24 use crate::encoding::convert_to_u16;
25 -use tauri::regex::Regex;
25
26 mod encoding;
27 mod terminal;
@@ -92,12 +91,24 @@ fn open_terminal(path: &str) {
91
92 #[tauri::command]
93 fn is_file(path: &str) -> bool {
95 - fs::metadata(path).unwrap().is_file()
94 + match fs::metadata(path) {
95 + Ok(r) => r.is_file(),
96 + Err(e) => {
97 + error!("{}", e);
98 + false
99 + }
100 + }
101 }
102
103 #[tauri::command]
104 fn is_folder(path: &str) -> bool {
100 - fs::metadata(path).unwrap().is_dir()
105 + match fs::metadata(path) {
106 + Ok(r) => r.is_dir(),
107 + Err(e) => {
108 + error!("{}", e);
109 + false
110 + }
111 + }
112 }
113
114 #[tauri::command]
@@ -166,16 +177,16 @@ fn read_file(path: &str) -> FileData {
177 };
178
179 let file_data: FileData;
169 - let regex = Regex::new(r"(?m)(^[ ]+)").unwrap();
180
181 // encode based on bom if present otherwise just default to utf8
182 if let Some(data) = encoding_rs::Encoding::for_bom(&bytes) {
183 let (text, encoding, _) = data.0.decode(&bytes);
174 - let hay = text.to_string();
175 - let spaces = regex.captures(hay.as_str());
176 -
184 + let t = text.to_string();
185 + let x = t.as_str();
186 + let lines: Vec<&str> = x.lines().collect();
187 + let spaces = detect_indent(&lines);
188 let space_count = match spaces {
178 - Some(capture) => capture.len(),
189 + Some(capture) => capture,
190 None => 0
191 };
192
@@ -189,11 +200,12 @@ fn read_file(path: &str) -> FileData {
200 info!("File BOM found. Encoding with {}...", encoding.name());
201 } else {
202 let (text, encoding, _) = encoding_rs::UTF_8.decode(&bytes);
192 - let hay = text.to_string();
193 - let spaces = regex.captures(hay.as_str());
194 -
203 + let t = text.to_string();
204 + let x = t.as_str();
205 + let lines: Vec<&str> = x.lines().collect();
206 + let spaces = detect_indent(&lines);
207 let space_count = match spaces {
196 - Some(capture) => capture.len(),
208 + Some(capture) => capture,
209 None => 0
210 };
211
@@ -270,6 +282,33 @@ fn is_supported(path: &str) -> bool {
282 }
283 }
284
285 +fn detect_indent(lines: &[&str]) -> Option<usize> {
286 + let mut indents: collections::HashMap<usize, usize> = collections::HashMap::new(); // # spaces indent -> # times seen
287 + let mut last = 0; // # leading spaces in the last line we saw
288 +
289 + for &text in lines.iter() {
290 + let width = text.find(|c: char| c != ' ').unwrap_or_else(|| text.len());
291 +
292 + let indent = (width as isize - last as isize).abs() as usize;
293 + if indent > 1 {
294 + *indents.entry(indent).or_insert(0) += 1;
295 + }
296 + last = width;
297 + }
298 +
299 + // find most frequent non-zero width difference
300 + let mut max = 0;
301 + let mut indent = None;
302 + for (&width, &tally) in &indents {
303 + if tally > max {
304 + max = tally;
305 + indent = Some(width);
306 + }
307 + }
308 +
309 + indent
310 +}
311 +
312 fn configure_log() -> TauriPlugin<Wry> {
313 tauri_plugin_log::Builder::default()
314 .format(move |out, message, record| {