瀏覽代碼

Patch fix (#36)

* patch the error of command mode

* 进一步修复文件移动引起的错误

* 修改import, default.yaml

---------

Co-authored-by: zhou <2628735358@qq.com>
Z YS 4 月之前
父節點
當前提交
11749c19bf

+ 1 - 1
src/application/handler/mod.rs

@@ -9,8 +9,8 @@ mod cursor;
 mod delete;
 mod insert;
 mod monitor;
-mod search;
 mod normal;
+mod search;
 mod workspace;
 
 pub fn handle_map() -> HashMap<&'static str, fn(&mut Application) -> Result<()>> {

+ 6 - 7
src/application/handler/normal.rs

@@ -1,5 +1,4 @@
 use crossterm::event::KeyCode;
-use held_core::utils::distance::Distance;
 use held_core::utils::position::Position;
 use held_core::utils::range::Range;
 use unicode_segmentation::UnicodeSegmentation;
@@ -136,12 +135,12 @@ pub fn move_to_next_words(app: &mut Application) -> Result<()> {
 
 pub fn move_to_prev_words(app: &mut Application) -> Result<()> {
     if let Some(buffer) = &mut app.workspace.current_buffer {
-        let current_pos = buffer.cursor.position
-            + Distance {
-                lines: 0,
-                offset: 1,
-            }; // 由于是左闭右开区间,所以需要向后移动一个字符
-               // 从当前位置向前搜索
+        let current_pos = buffer.cursor.position;
+        // + Distance {
+        //     lines: 0,
+        //     offset: 1,
+        // }; // 由于是左闭右开区间,所以需要向后移动一个字符
+        // 从当前位置向前搜索
         let search_range =
             if let Some(str) = buffer.read(&Range::new(Position::new(0, 0), current_pos)) {
                 str

+ 1 - 1
src/application/handler/search.rs

@@ -1,8 +1,8 @@
 use crate::application::mode::ModeData;
 use crate::application::Application;
 use crate::errors::*;
-use crate::util::{position::Position, range::Range};
 use crossterm::event::KeyCode;
+use held_core::utils::{position::Position, range::Range};
 
 pub fn exec_search(app: &mut Application) -> Result<()> {
     if let ModeData::Search(ref mut search_data) = app.mode {

+ 6 - 5
src/application/mode/mod.rs

@@ -9,8 +9,8 @@ use error_chain::bail;
 use insert::InsertRenderer;
 use linked_hash_map::LinkedHashMap;
 use normal::NormalRenderer;
-use search::{SearchData, SearchRenderer};
 use replace::ReplaceRenderer;
+use search::{SearchData, SearchRenderer};
 use smallvec::SmallVec;
 use strum::EnumIter;
 use workspace::{WorkspaceModeData, WorkspaceRender};
@@ -20,14 +20,14 @@ use super::handler::handle_map;
 use super::Application;
 
 pub mod command;
-pub mod motion;
 pub mod delete;
 pub mod error;
 mod insert;
+pub mod motion;
 pub mod normal;
-pub mod workspace;
-pub mod search;
 mod replace;
+pub mod search;
+pub mod workspace;
 
 pub enum ModeData {
     Normal,
@@ -37,7 +37,8 @@ pub enum ModeData {
     Command(CommandData),
     Workspace(WorkspaceModeData),
     Search(SearchData),
-    Delete, Replace, // Other(OtherData)
+    Delete,
+    Replace, // Other(OtherData)
 }
 
 #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, EnumIter)]

+ 5 - 2
src/application/mode/motion.rs

@@ -36,6 +36,9 @@ pub fn locate_next_words_begin(count: usize, str: &str, current_pos: &Position)
 }
 
 pub fn locate_previous_words(count: usize, str: &str, current_pos: &Position) -> Position {
+    if str.len() == 0 {
+        return *current_pos;
+    }
     let s = str.as_bytes();
     let mut left = s.len() - 1;
     let mut right = left;
@@ -61,10 +64,10 @@ pub fn locate_previous_words(count: usize, str: &str, current_pos: &Position) ->
     let new_offset = if let Some(back_offset) = str[left..].find('\n') {
         // back_offset为目标位置到行尾的距离
         // new_line_len - back_offset为目标位置到行首的距离
-        new_line_len - back_offset
+        new_line_len - back_offset + 1
     } else {
         // 新旧行之间没有换行符
-        current_pos.offset - (s.len() - left)
+        current_pos.offset - (s.len() - 1 - left)
     };
     return Position::new(new_line, new_offset);
 }

+ 3 - 5
src/application/mode/replace.rs

@@ -1,8 +1,6 @@
-use crate::view::{
-    colors::colors::Colors,
-    status_data::{buffer_status_data, StatusLineData},
-    style::CharStyle,
-};
+use crate::view::status_data::{buffer_status_data, StatusLineData};
+use held_core::view::colors::Colors;
+use held_core::view::style::CharStyle;
 
 use super::ModeRenderer;
 

+ 17 - 11
src/application/mode/search.rs

@@ -1,12 +1,11 @@
 use super::ModeRenderer;
-use crate::util::range::Range;
 use crate::{
     errors::*,
-    view::{
-        colors::colors::Colors,
-        status_data::{buffer_status_data, StatusLineData},
-        style::CharStyle,
-    },
+    view::status_data::{buffer_status_data, StatusLineData},
+};
+use held_core::{
+    utils::range::Range,
+    view::{colors::Colors, style::CharStyle},
 };
 pub(super) struct SearchRenderer;
 
@@ -24,12 +23,19 @@ impl ModeRenderer for SearchRenderer {
             if let super::ModeData::Search(ref search_data) = _mode {
                 let highlight_search_string = search_data.search_result.clone();
 
-                let highlight_search_string_slice: Option<&[Range]> =
+                let collected_ranges: Vec<(Range, CharStyle, Colors)> =
                     if !highlight_search_string.is_empty() {
-                        Some(
-                            &highlight_search_string[search_data.search_result_index
-                                ..search_data.search_result_index + 1],
-                        )
+                        highlight_search_string
+                            .iter()
+                            .map(|range| (range.clone(), CharStyle::Bold, Colors::Inverted))
+                            .collect()
+                    } else {
+                        Vec::new()
+                    };
+
+                let highlight_search_string_slice: Option<&[(Range, CharStyle, Colors)]> =
+                    if !collected_ranges.is_empty() {
+                        Some(collected_ranges.as_slice())
                     } else {
                         None
                     };

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

@@ -104,8 +104,8 @@ replace:
   up: cursor::move_up
   down: cursor::move_down
   ctrl-c: app::exit
-  ctrl-s: workspace::save_file
-  ctrl-z: workspace::undo
+  ctrl-s: buffer::save_file
+  ctrl-z: buffer::undo
   enter: 
     - buffer::new_line
     - cursor::move_down

+ 1 - 4
src/modules/input/mod.rs

@@ -1,10 +1,7 @@
 use std::{collections::HashMap, ffi::OsStr, fs::read_to_string, path::PathBuf};
 
 use crate::{
-    application::{
-        mode::{ModeData, ModeKey},
-        Application,
-    },
+    application::{mode::ModeKey, Application},
     errors::*,
 };
 use crossterm::event::{Event, KeyEvent, KeyEventKind, KeyModifiers};