@@ -3,12 +3,13 @@ use std::{io, sync::MutexGuard};
3
3
use crate :: utils:: {
4
4
terminal:: TermManager ,
5
5
ui:: {
6
- event:: { KeyEventCallback , WarpUiCallBackType } ,
6
+ event:: KeyEventCallback ,
7
7
uicore:: { UiCore , CONTENT_WINSIZE } ,
8
8
} ,
9
9
} ;
10
10
11
11
pub trait CommonOp : KeyEventCallback {
12
+ /// 删除一行
12
13
fn remove_line ( & self , ui : & mut MutexGuard < UiCore > ) -> io:: Result < ( ) > {
13
14
TermManager :: clear_current_line ( ) ?;
14
15
TermManager :: clear_under_cursor ( ) ?;
@@ -33,6 +34,7 @@ pub trait CommonOp: KeyEventCallback {
33
34
Ok ( ( ) )
34
35
}
35
36
37
+ /// 删除数行
36
38
fn remove_n_line ( & self , ui : & mut MutexGuard < UiCore > , n : u16 ) -> io:: Result < ( ) > {
37
39
let linecount = ui. buffer . line_count ( ) as u16 ;
38
40
let y = ui. cursor . y ( ) ;
@@ -44,6 +46,7 @@ pub trait CommonOp: KeyEventCallback {
44
46
}
45
47
Ok ( ( ) )
46
48
}
49
+ /// 删除一个单词
47
50
fn remove_word ( & self , ui : & mut MutexGuard < UiCore > ) -> io:: Result < ( ) > {
48
51
let x = ui. cursor . x ( ) ;
49
52
let y = ui. cursor . y ( ) ;
@@ -62,30 +65,7 @@ pub trait CommonOp: KeyEventCallback {
62
65
ui. render_content ( y, 1 ) ?;
63
66
return Ok ( ( ) ) ;
64
67
}
65
- fn jump_to_next_word ( & self , ui : & mut MutexGuard < UiCore > ) -> io:: Result < WarpUiCallBackType > {
66
- let x = ui. cursor . x ( ) ;
67
- let y = ui. cursor . y ( ) ;
68
- let pos = ui. buffer . search_nextw_begin ( x, y) ;
69
- let linesize = ui. buffer . get_linesize ( y) ;
70
- let abs_y = y + ui. buffer . offset ( ) as u16 ;
71
-
72
- if pos < linesize as usize {
73
- // 如果下一个单词在当前行,则移动光标到该单词的起始位置
74
- ui. cursor . move_to_columu ( pos as u16 ) ?;
75
- } else if y as usize + ui. buffer . offset ( ) < ui. buffer . line_count ( ) - 1 {
76
- // 如果当前行不是最后一行,则移动到下一行的单词起始位置
77
- let next_word_pos = ui. buffer . search_nextw_begin ( 0 , y + 1 ) as u16 ;
78
- let next_linesize = ui. buffer . get_linesize_abs ( abs_y + 1 ) ;
79
- self . down ( ui) ?;
80
- ui. cursor
81
- . move_to_columu ( next_word_pos. min ( next_linesize - 1 ) ) ?;
82
- ui. cursor . highlight ( Some ( y) ) ?;
83
- } else {
84
- // 如果当前行是最后一行,则移动到当前行的末尾
85
- ui. cursor . move_to_columu ( linesize as u16 - 1 ) ?;
86
- }
87
- return Ok ( WarpUiCallBackType :: None ) ;
88
- }
68
+ /// 移动到指定行
89
69
fn move_to_line ( & self , ui : & mut MutexGuard < UiCore > , line : u16 ) -> io:: Result < ( ) > {
90
70
let x = ui. cursor . x ( ) ;
91
71
let y = ui. cursor . y ( ) ;
@@ -97,6 +77,7 @@ pub trait CommonOp: KeyEventCallback {
97
77
return Ok ( ( ) ) ;
98
78
}
99
79
80
+ /// 定位到上一个单词的首字母,返回绝对坐标
100
81
fn locate_prevw_begin ( & self , ui : & mut MutexGuard < UiCore > , x : u16 , abs_y : u16 ) -> ( u16 , u16 ) {
101
82
// 如果光标已在行首,则尝试移动到上一行的单词首字母
102
83
if x == 0 {
@@ -113,15 +94,16 @@ pub trait CommonOp: KeyEventCallback {
113
94
114
95
return ( prev_word_pos as u16 , abs_y) ;
115
96
}
116
- fn locate_nextw_ending ( & self , ui : & mut MutexGuard < UiCore > , x : u16 , y : u16 ) -> ( u16 , u16 ) {
117
- let linesize = ui. buffer . get_linesize ( y) as usize ;
118
97
119
- // y的绝对位置
120
- let abs_y = ui. buffer . offset ( ) as u16 + y;
98
+ /// 定位到下一个单词的末尾,返回绝对坐标
99
+ fn locate_nextw_ending ( & self , ui : & mut MutexGuard < UiCore > , x : u16 , abs_y : u16 ) -> ( u16 , u16 ) {
100
+ let linesize = ui. buffer . get_linesize_abs ( abs_y) as usize ;
101
+
121
102
// 如果光标已经在当前行的末尾或最后一个字符(x + 2),则尝试移动到下一行的末尾或单词末尾
122
103
if x as usize + 2 >= linesize {
123
104
if abs_y < ui. buffer . line_count ( ) as u16 - 1 {
124
- let next_end_pos = ui. buffer . search_nextw_end ( 0 , y + 1 ) as u16 ;
105
+ let offset = ui. buffer . offset ( ) as u16 ;
106
+ let next_end_pos = ui. buffer . search_nextw_end ( 0 , abs_y + 1 - offset) as u16 ;
125
107
return ( next_end_pos, abs_y + 1 ) ;
126
108
} else {
127
109
// 如果已经是最后一行,则保持光标在当前行的末尾
@@ -130,8 +112,39 @@ pub trait CommonOp: KeyEventCallback {
130
112
}
131
113
}
132
114
133
- let next_end_pos = ui. buffer . search_nextw_end ( x, y) as u16 ;
115
+ let offset = ui. buffer . offset ( ) as u16 ;
116
+ let next_end_pos = ui. buffer . search_nextw_end ( x, abs_y - offset) as u16 ;
134
117
// 如果下一个单词的末尾在当前行,则移动光标到该单词的末尾
135
118
return ( next_end_pos. min ( linesize as u16 - 1 ) , abs_y) ;
136
119
}
120
+
121
+ /// 定位到下一个单词的首字母,返回绝对坐标
122
+ fn locate_next_word ( & self , ui : & mut MutexGuard < UiCore > , abs_pos : ( u16 , u16 ) ) -> ( u16 , u16 ) {
123
+ let linesize = ui. buffer . get_linesize_abs ( abs_pos. 1 ) as usize ;
124
+ if abs_pos. 0 as usize + 2 >= linesize {
125
+ if abs_pos. 1 < ui. buffer . line_count ( ) as u16 - 1 {
126
+ let offset = ui. buffer . offset ( ) as u16 ;
127
+ let next_end_pos = ui. buffer . search_nextw_begin ( 0 , abs_pos. 1 + 1 - offset) as u16 ;
128
+ return ( next_end_pos, abs_pos. 1 + 1 ) ;
129
+ } else {
130
+ let x = if linesize > 0 { linesize - 1 } else { 0 } ;
131
+ return ( x as u16 , abs_pos. 1 ) ;
132
+ }
133
+ }
134
+ let offset = ui. buffer . offset ( ) as u16 ;
135
+ let next_end_pos = ui. buffer . search_nextw_begin ( abs_pos. 0 , abs_pos. 1 - offset) as u16 ;
136
+ return ( next_end_pos. min ( linesize as u16 - 1 ) , abs_pos. 1 ) ;
137
+ }
138
+ fn move_to_nlines_of_screen ( & self , ui : & mut MutexGuard < UiCore > , n : usize ) -> io:: Result < ( ) > {
139
+ let y = ui. cursor . y ( ) as usize ;
140
+
141
+ let offset = ui. buffer . offset ( ) ;
142
+
143
+ let new_y = ui. buffer . goto_line ( offset + n) ;
144
+ ui. render_content ( 0 , CONTENT_WINSIZE . read ( ) . unwrap ( ) . rows as usize ) ?;
145
+ ui. cursor . move_to_row ( new_y) ?;
146
+ ui. cursor . highlight ( Some ( y as u16 ) ) ?;
147
+
148
+ Ok ( ( ) )
149
+ }
137
150
}
0 commit comments