-
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 5 commits
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 |
---|---|---|
|
@@ -155,7 +155,7 @@ impl EditBuffer { | |
|
||
line.data.len() as u16 | ||
} | ||
|
||
/// 外部接口,本结构体内部方法不应该使用,因为涉及offset计算 | ||
pub fn remove_char(&self, x: u16, y: u16) { | ||
let mut buf = self.buf.write().unwrap(); | ||
|
@@ -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(); | ||
|
@@ -213,8 +223,12 @@ impl EditBuffer { | |
#[inline] | ||
pub fn insert_char(&self, ch: u8, x: u16, y: u16) { | ||
let mut buf = self.buf.write().unwrap(); | ||
let line = buf.get_mut(self.offset() + y as usize).unwrap(); | ||
line.insert(x as usize, ch); | ||
if buf.len() > 0 { | ||
let line = buf.get_mut(self.offset() + y as usize).unwrap(); | ||
line.insert(x as usize, ch); | ||
} else { | ||
buf.push(LineBuffer::new(vec![ch])); | ||
} | ||
} | ||
|
||
#[inline] | ||
|
@@ -353,6 +367,127 @@ impl EditBuffer { | |
|
||
count | ||
} | ||
|
||
pub fn delete_line(&self, y: usize) { | ||
let mut buffer = self.buf.write().unwrap(); | ||
let line = buffer.get(y).unwrap(); | ||
if line.data.is_empty() { | ||
return; | ||
} | ||
|
||
if !line.flags.contains(LineState::LOCKED) { | ||
buffer.remove(y); | ||
} | ||
} | ||
|
||
pub fn delete_until_line_beg(&self, x: usize, y: usize) -> Option<usize> { | ||
let mut buffer = self.buf.write().unwrap(); | ||
let line = buffer.get_mut(y).unwrap(); | ||
|
||
if line.data.len() < 2 { | ||
return None; | ||
} | ||
line.data.drain(0..x); | ||
return Some(x - 1); | ||
} | ||
|
||
pub fn delete_until_endl(&self, x: usize, y: usize) -> Option<usize> { | ||
let mut buffer = self.buf.write().unwrap(); | ||
let line = buffer.get_mut(y).unwrap(); | ||
let len = line.data.len(); | ||
if len < 2 { | ||
return None; | ||
} | ||
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
Oops, something went wrong.
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