-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
增加按键功能 #6
Merged
Merged
增加按键功能 #6
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
20932a8
完善Command,实现行首行尾跳转
trv3wood 8e83ac6
实现了'w'跳转至下一个单词
trv3wood 4c2efbc
完善了'w'按键,实现'e'跳转下一单词末尾
trv3wood 2334e71
remove unused imports
trv3wood 6ded6c3
Refactor cursor movement logic for better performance; dw删除单词
trv3wood e51971f
重命名以提高可读性
trv3wood 97f99d7
格式化
trv3wood ff33d29
修复删除行的异常问题
trv3wood b20ec2b
修复删除行时渲染异常问题
trv3wood 6d33fdb
G移动到最后一行
trv3wood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,6 +167,16 @@ impl EditBuffer { | |
line.unwrap().remove(x as usize); | ||
} | ||
|
||
pub fn remove_str(&self, x: u16, y: u16, n: usize) { | ||
let mut buf = self.buf.write().unwrap(); | ||
let line = buf.get_mut(self.offset.load(Ordering::SeqCst) + y as usize); | ||
if line.is_none() { | ||
return; | ||
} | ||
let x = x as usize; | ||
line.unwrap().data.drain(x..x + n); | ||
} | ||
|
||
/// 获取一份对应行的拷贝 | ||
pub fn get_line(&self, line: u16) -> LineBuffer { | ||
let buf = self.buf.read().unwrap(); | ||
|
@@ -391,6 +401,93 @@ impl EditBuffer { | |
line.data.drain(x..len - 1); | ||
return Some(x); | ||
} | ||
|
||
/// 返回下一个单词的起始位置 | ||
/// 如果为该行最后一单词,返回该行长度 | ||
pub fn search_nextw_beg(&self, x: u16, y: u16) -> usize { | ||
let mut left = x as usize; | ||
let mut right = left; | ||
let linesize = self.get_linesize(y) as usize; | ||
let buf = self.buf.read().unwrap(); | ||
let line = buf.get(self.offset.load(Ordering::SeqCst) + y as usize).unwrap(); | ||
|
||
while left <= right && right < linesize { | ||
let lchar = line[left] as char; | ||
let rchar = line[right] as char; | ||
if !(lchar == ' ' || lchar == '\t') { | ||
left += 1; | ||
right += 1; | ||
continue; | ||
} | ||
if rchar != ' ' && rchar != '\t' { | ||
break; | ||
} | ||
right += 1; | ||
} | ||
|
||
return right; | ||
} | ||
|
||
/// 搜索下一个单词的末尾 | ||
/// 如果为该行最后一单词,返回该行长度 | ||
pub fn search_nextw_end(&self, x: u16, y: u16) -> usize { | ||
let mut left = x as usize; | ||
let mut right = left; | ||
let linesize = self.get_linesize(y) as usize; | ||
let buf = self.buf.read().unwrap(); | ||
let line = buf.get(self.offset.load(Ordering::SeqCst) + y as usize).unwrap(); | ||
|
||
while left <= right && right < linesize { | ||
let lchar = line[left] as char; | ||
let rchar = line[right] as char; | ||
if lchar == ' ' || lchar == '\t' { | ||
left += 1; | ||
right += 1; | ||
continue; | ||
} | ||
if rchar == ' ' || rchar == '\t' { | ||
if right == x as usize + 1 { | ||
left = right; | ||
continue; | ||
} | ||
right -= 1; | ||
break; | ||
} | ||
right += 1; | ||
} | ||
|
||
return right; | ||
} | ||
|
||
/// 返回前一单词首字母位置,如果是当前行首单词,返回 None | ||
pub fn search_prevw_beg(&self, x: u16, y: u16) -> Option<usize> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同理 |
||
let mut left = x as i32; | ||
let mut right = left; | ||
let buf = self.buf.read().unwrap(); | ||
let line = buf.get(self.offset.load(Ordering::SeqCst) + y as usize).unwrap(); | ||
|
||
while left <= right && left >= 0 { | ||
let lchar = line[left as usize] as char; | ||
let rchar = line[right as usize] as char; | ||
|
||
if rchar == ' ' || rchar == '\t' { | ||
left -= 1; | ||
right -= 1; | ||
continue; | ||
} | ||
|
||
if lchar == ' ' || lchar == '\t' { | ||
if left + 1 == x.into() { | ||
right = left; | ||
continue; | ||
} | ||
return Some(left as usize + 1); | ||
} | ||
|
||
left -= 1; | ||
} | ||
return None; | ||
} | ||
} | ||
|
||
bitflags! { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,6 +241,44 @@ impl Command { | |
None => {} | ||
} | ||
} | ||
|
||
b'w' | b'e' => { | ||
let x = ui.cursor.x(); | ||
let y = ui.cursor.y(); | ||
let next_word_pos = ui.buffer.search_nextw_beg(x, y); | ||
let linesize = ui.buffer.get_linesize(y); | ||
|
||
// 如果下一个单词在当前行,则删除当前单词 | ||
if next_word_pos < linesize.into() { | ||
ui.buffer.remove_str(x, y, next_word_pos - x as usize); | ||
} else { | ||
// 如果下一个单词在下一行,则删除当前行剩余部分 | ||
self.left(ui)?; | ||
ui.buffer.delete_until_endl(x.into(), y.into()); | ||
} | ||
ui.render_content(y, 1)?; | ||
} | ||
|
||
b'b' => { | ||
let old_x = ui.cursor.x(); | ||
let old_y = ui.cursor.y(); | ||
|
||
self.jump_to_prevw_beg(ui)?; | ||
|
||
let x = ui.cursor.x(); | ||
let y = ui.cursor.y(); | ||
if old_y == y { | ||
ui.buffer.remove_str(x, y, old_x as usize - x as usize); | ||
ui.render_content(y, 1)?; | ||
} else { | ||
ui.buffer.delete_until_endl(x as usize, y as usize); | ||
ui.buffer.delete_until_line_beg(old_x as usize, old_y as usize); | ||
ui.buffer.merge_line(old_y); | ||
let linecount = ui.buffer.line_count(); | ||
TermManager::clear_under_cursor()?; | ||
ui.render_content(y, linecount - y as usize - 1)?; | ||
} | ||
} | ||
_ => {} | ||
} | ||
return Ok(WarpUiCallBackType::None); | ||
|
@@ -249,29 +287,12 @@ impl Command { | |
fn jump_to_next_word(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> { | ||
let x = ui.cursor.x(); | ||
let y = ui.cursor.y(); | ||
let mut left = x as usize; | ||
let mut right = left; | ||
let line = ui.buffer.get_line(y); | ||
let linesize = ui.buffer.get_linesize(y) as usize; | ||
|
||
// 搜索下一个单词的起始位置 | ||
while left <= right && right < linesize { | ||
let lchar = line[left] as char; | ||
let rchar = line[right] as char; | ||
if !(lchar == ' ' || lchar == '\t') { | ||
left += 1; | ||
right += 1; | ||
continue; | ||
} | ||
if rchar != ' ' && rchar != '\t' { | ||
break; | ||
} | ||
right += 1; | ||
} | ||
let pos = ui.buffer.search_nextw_beg(x, y); | ||
let linesize = ui.buffer.get_linesize(y); | ||
|
||
if right < linesize { | ||
if pos < linesize as usize { | ||
// 如果下一个单词在当前行,则移动光标到该单词的起始位置 | ||
ui.cursor.move_to_columu(right as u16)?; | ||
ui.cursor.move_to_columu(pos as u16)?; | ||
} else if y + 1 < ui.buffer.line_count() as u16 { | ||
// 如果当前行不是最后一行,则移动到下一行的开头 | ||
self.down(ui)?; | ||
|
@@ -286,51 +307,55 @@ impl Command { | |
fn jump_to_nextw_ending(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> { | ||
let x = ui.cursor.x(); | ||
let y = ui.cursor.y(); | ||
let mut left = x as usize; | ||
let mut right = left; | ||
let line = ui.buffer.get_line(y); | ||
let linesize = ui.buffer.get_linesize(y) as usize; | ||
|
||
// 如果光标已经在当前行的末尾,则尝试移动到下一行的末尾或单词末尾 | ||
if x as usize == linesize - 1 { | ||
if x as usize >= linesize - 2 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这种减2的操作,这个2代表什么,应该尽量避免这种魔数 |
||
if y < ui.buffer.line_count() as u16 - 1 { | ||
self.down(ui)?; | ||
ui.cursor.move_to_columu(0)?; | ||
self.jump_to_nextw_ending(ui)?; | ||
return Ok(WarpUiCallBackType::None); | ||
let next_end_pos = ui.buffer.search_nextw_end(0, y + 1) as u16; | ||
ui.cursor.move_to(next_end_pos, y + 1)?; | ||
ui.cursor.highlight(Some(y))?; | ||
} else { | ||
// 如果已经是最后一行,则保持光标在当前行的末尾 | ||
ui.cursor.move_to_columu(linesize as u16 - 1)?; | ||
return Ok(WarpUiCallBackType::None); | ||
} | ||
return Ok(WarpUiCallBackType::None); | ||
} | ||
|
||
// 搜索下一个单词的末尾 | ||
while left <= right && right < linesize { | ||
let lchar = line[left] as char; | ||
let rchar = line[right] as char; | ||
if lchar == ' ' || lchar == '\t' { | ||
left += 1; | ||
right += 1; | ||
continue; | ||
} | ||
if rchar == ' ' || rchar == '\t' { | ||
if right == x as usize + 1 { | ||
left = right; | ||
continue; | ||
} | ||
right -= 1; | ||
break; | ||
} | ||
right += 1; | ||
} | ||
let next_end_pos = ui.buffer.search_nextw_end(x, y) as u16; | ||
// 如果下一个单词的末尾在当前行,则移动光标到该单词的末尾 | ||
ui.cursor.move_to_columu(next_end_pos.min(linesize as u16 - 2))?; | ||
return Ok(WarpUiCallBackType::None); | ||
} | ||
|
||
if right < linesize { | ||
ui.cursor.move_to_columu(right as u16)?; | ||
} else if right == linesize { | ||
ui.cursor.move_to_columu(linesize as u16 - 1)?; | ||
fn jump_to_prevw_beg(&self, ui: &mut MutexGuard<UiCore>) -> io::Result<WarpUiCallBackType> { | ||
let x = ui.cursor.x(); | ||
let y = ui.cursor.y(); | ||
|
||
// 如果光标已在行首,则尝试移动到上一行的单词首字母 | ||
if x == 0 { | ||
if y > 0 { | ||
let end_of_prev_line = ui.buffer.get_linesize(y - 1) - 1; | ||
let prev_word_pos = match ui.buffer.search_prevw_beg(end_of_prev_line, y - 1) { | ||
Some(pos) => pos, | ||
None => 0 | ||
}; | ||
ui.cursor.move_to(prev_word_pos as u16, y - 1)?; | ||
ui.cursor.highlight(Some(y))?; | ||
} else { | ||
// 如果已经是第一行,则保持光标在当前行的起始位置 | ||
ui.cursor.move_to_columu(0)?; | ||
} | ||
return Ok(WarpUiCallBackType::None); | ||
} | ||
return Ok(WarpUiCallBackType::None); | ||
|
||
let prev_word_pos = match ui.buffer.search_prevw_beg(x, y) { | ||
Some(pos) => pos, | ||
None => 0 | ||
}; | ||
|
||
ui.cursor.move_to(prev_word_pos as u16, y)?; | ||
return Ok(WarpUiCallBackType::None); | ||
} | ||
} | ||
|
||
|
@@ -421,6 +446,8 @@ impl KeyEventCallback for Command { | |
b"w" => self.jump_to_next_word(ui), | ||
|
||
b"e" => self.jump_to_nextw_ending(ui), | ||
|
||
b"b" => self.jump_to_prevw_beg(ui), | ||
|
||
b"W" => { | ||
// 跳转到下一个flag行 | ||
|
@@ -464,6 +491,16 @@ impl KeyEventCallback for Command { | |
} | ||
return Ok(WarpUiCallBackType::None); | ||
} | ||
|
||
b"G" => { | ||
// 移动到最后一行 | ||
let line_count = ui.buffer.line_count() as u16; | ||
let y = ui.cursor.y(); | ||
ui.scroll_down(line_count - y)?; | ||
ui.cursor.move_to_row(line_count - 1)?; | ||
ui.cursor.highlight(Some(y))?; | ||
return Ok(WarpUiCallBackType::None); | ||
} | ||
|
||
_ => { | ||
return Ok(WarpUiCallBackType::None); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
命名写全会更好,这里的beg最好写全begin