Browse Source

insertMode添加tab处理 (#25)

GnoCiYeH 6 months ago
parent
commit
910e8e543d

+ 27 - 2
src/application/handler/buffer.rs

@@ -1,12 +1,15 @@
-use crossterm::event::{KeyCode, KeyEvent};
+use crossterm::event::KeyCode;
 
-use crate::application::Application;
 use crate::errors::*;
+use crate::{application::Application, util::position::Position};
+
+use super::cursor;
 
 pub fn insert_char(app: &mut Application) -> Result<()> {
     if let Some(key) = app.monitor.last_key {
         if let KeyCode::Char(c) = key.code {
             app.workspace.current_buffer.as_mut().unwrap().insert(c);
+            cursor::move_right(app)?;
         }
     }
     Ok(())
@@ -18,3 +21,25 @@ pub fn new_line(app: &mut Application) -> Result<()> {
     }
     Ok(())
 }
+
+pub fn insert_tab(app: &mut Application) -> Result<()> {
+    if let Some(buffer) = app.workspace.current_buffer.as_mut() {
+        let tab_len = app.perferences.borrow().tab_width();
+        let width = tab_len - (buffer.cursor.offset) % tab_len;
+        if app.perferences.borrow().soft_tab() {
+            let tab_str = " ".repeat(width);
+            buffer.insert(tab_str);
+            buffer.cursor.move_to(Position {
+                line: buffer.cursor.line,
+                offset: buffer.cursor.offset + width,
+            });
+        } else {
+            buffer.insert("\t");
+            buffer.cursor.move_to(Position {
+                line: buffer.cursor.line,
+                offset: buffer.cursor.offset + width,
+            });
+        }
+    }
+    Ok(())
+}

+ 2 - 2
src/modules/input/default.yaml

@@ -20,6 +20,6 @@ insert:
     - cursor::move_down
     - cursor::move_to_start_of_line
   backspace: insert::backspace
+  tab: buffer::insert_tab
   _: 
-    - buffer::insert_char
-    - cursor::move_right
+    - buffer::insert_char

+ 1 - 1
src/modules/perferences/default.yaml

@@ -1,2 +1,2 @@
-soft_tabs: true
+soft_tab: true
 tab_width: 4