@setoelkahfi / svara / commits / 2ba3a34

feat: detect indentation size on files

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

mellobacon committed Feb 7, 2024 at 10:38 UTC 2ba3a34d4b50489c500601d054a30a8e41b97645
4 files changed +26 -8
src-tauri/src/main.rs
+10 -3
@@ -15,16 +15,14 @@ use std::io::{Write, BufReader};
15 use std::path::{Path, PathBuf};
16 use std::process::Command;
17 use std::{env, fs, sync::Arc};
18 -
18 use tauri::{App, Manager, Wry, State, async_runtime::Mutex as AsyncMutex};
20 -
19 use encoding::BOM;
20 use log::{error, info, Level};
21 use tauri::plugin::TauriPlugin;
22 use tauri_plugin_log::{LogTarget, RotationStrategy};
23 use portable_pty::{native_pty_system, PtySize};
26 -
24 use crate::encoding::convert_to_u16;
25 +use tauri::regex::Regex;
26
27 mod encoding;
28 mod terminal;
@@ -142,6 +140,7 @@ struct FileData {
140 encoding: String,
141 extension: String,
142 bom: bool,
143 + spaces: usize
144 }
145
146 #[tauri::command]
@@ -167,25 +166,33 @@ fn read_file(path: &str) -> FileData {
166 };
167
168 let file_data: FileData;
169 + let regex = Regex::new(r"(?m)(^[ ]+)").unwrap();
170
171 // encode based on bom if present otherwise just default to utf8
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();
176
177 file_data = FileData {
178 text: text.to_string(),
179 encoding: encoding.name().to_string(),
180 extension: ext.to_string(),
181 bom: true,
182 + spaces: spaces[1].to_owned().len()
183 };
184 info!("File BOM found. Encoding with {}...", encoding.name());
185 } else {
186 let (text, encoding, _) = encoding_rs::UTF_8.decode(&bytes);
187 + let hay = text.to_string();
188 + let spaces = regex.captures(hay.as_str()).unwrap();
189 +
190 file_data = FileData {
191 text: text.to_string(),
192 encoding: encoding.name().to_string(),
193 extension: ext.to_string(),
194 bom: false,
195 + spaces: spaces[1].to_owned().len()
196 };
197 info!(
198 "No file BOM found. Defaulting to {} encoding...",
src/lib/Editor.svelte
+9 -2
@@ -27,6 +27,7 @@
27 "language": "",
28 "encoding": "",
29 "hasBom": false,
30 + "spaces": 0,
31 "readonly": false,
32 });
33 const lang = new Compartment();
@@ -72,10 +73,14 @@
73 export function hasBom() {
74 return $file_info.hasBom;
75 }
76 + export function getSpaces() {
77 + return $file_info.spaces;
78 + }
79 export function getView() {
80 return editorView;
81 }
82 export function setTabSize(size) {
83 + spaces.set(size);
84 editorView.dispatch({
85 effects: tabSize.reconfigure(EditorState.tabSize.of(size))
86 })
@@ -109,7 +114,7 @@
114 drawSelection(),
115 crosshairCursor(),
116 rectangularSelection(),
112 - indentUnit.of(" "),
117 + indentSize.of(indentUnit.of(" ")),
118 indentationMarkers({
119 thickness: 1,
120 colors: {
@@ -177,6 +182,7 @@
182 updateLineInfo();
183 language.set($file_info.language);
184 encoding.set({value: $file_info.encoding, hasBom: $file_info.hasBom});
185 + setTabSize($file_info.spaces);
186 }
187 export function updateLineInfo() {
188 let lineNumber = editorView.state.doc.lineAt(editorView.state.selection.main.head).number;
@@ -200,6 +206,7 @@
206 export const line_info = writable({line: "-", column: "-"});
207 export const language = writable("Unknown");
208 export const encoding = writable({value: "UTF-8", hasBom: false});
209 + export const spaces = writable(4);
210 const langmode = writable(null);
211
212 export function getLangFromExt(ext: string) {
@@ -305,4 +312,4 @@
312 :global(.cm-line) {
313 padding: 0 2px 0 1px !important;
314 }
308 -</style>
\ No newline at end of file
315 +</style>
src/lib/Statusbar.svelte
+4 -2
@@ -1,6 +1,6 @@
1 <script lang="ts">
2 import { isfile } from "./EditorTabList.svelte";
3 - import { line_info, language, encoding } from "./Editor.svelte";
3 + import { line_info, language, encoding, spaces } from "./Editor.svelte";
4 import { getVersion } from '@tauri-apps/api/app';
5 import { onMount } from "svelte";
6 import { Terminal as TerminalIcon } from "carbon-icons-svelte";
@@ -62,6 +62,8 @@
62 </div>
63 {#if $isfile}
64 <div class="editor-info">
65 + <span title="Indentation">Spaces: {$spaces}</span>
66 + <div class="divider"></div>
67 <span title="Ln: {$line_info.line}, Col: {$line_info.column}">{$line_info.line} : {$line_info.column}</span>
68 <div class="divider"></div>
69 <span>{$encoding.value} {$encoding.hasBom === true ? " with BOM" : ""}</span>
@@ -136,4 +138,4 @@
138 padding: 0 !important;
139 color: #6d6d6d;
140 }
139 -</style>
\ No newline at end of file
141 +</style>
src/lib/Tab/Tab.ts
+3 -1
@@ -3,6 +3,7 @@ import Editor from "../Editor.svelte";
3 import { dialog, invoke } from "@tauri-apps/api";
4 import { saveFile } from "../File";
5 import { warn } from "tauri-plugin-log-api";
6 +import { appSettings } from "../../config/config";
7
8 export class Tab {
9 id = 0;
@@ -71,7 +72,7 @@ export class Tab {
72 if (this.tabOpen(path)) {
73 return;
74 }
74 - let fileData = {text: "", encoding: "UTF-8", extension: "", bom: false};
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 } catch (error) {
@@ -89,6 +90,7 @@ export class Tab {
90 "language": await content.getLang(fileData.extension),
91 "encoding": fileData.encoding,
92 "hasBom": fileData.bom,
93 + "spaces": fileData.spaces,
94 "readonly": false,
95 });
96 this.tablist = [...this.tablist, tab];