Skip to content
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 10 commits into from
Jul 25, 2024
141 changes: 138 additions & 3 deletions src/utils/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

命名写全会更好,这里的beg最好写全begin

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> {
Copy link
Member

Choose a reason for hiding this comment

The 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! {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ impl CursorCrtl {
let size = *WINSIZE.read().unwrap();
if self.y + lines >= size.rows {
// 向上滚动
todo!()
// todo!()
return Ok(());
}

CursorManager::move_to_nextline(lines)?;
Expand Down
Loading