Merge pull request #32 from zed-industries/editor-keybindings

Initial editor keybindings
This commit is contained in:
Antonio Scandurra
2021-04-30 09:31:32 +02:00
committed by GitHub
5 changed files with 858 additions and 89 deletions
+24 -1
View File
@@ -1,7 +1,7 @@
use crate::{
editor::{
buffer::{Anchor, Buffer, Point, ToPoint},
display_map::DisplayMap,
display_map::{Bias, DisplayMap},
DisplayPoint,
},
time,
@@ -72,4 +72,27 @@ impl Selection {
start..end
}
}
pub fn buffer_rows_for_display_rows(&self, map: &DisplayMap, ctx: &AppContext) -> Range<u32> {
let display_start = self.start.to_display_point(map, ctx).unwrap();
let buffer_start = DisplayPoint::new(display_start.row(), 0)
.to_buffer_point(map, Bias::Left, ctx)
.unwrap();
let mut display_end = self.end.to_display_point(map, ctx).unwrap();
if display_end != map.max_point(ctx)
&& display_start.row() != display_end.row()
&& display_end.column() == 0
{
*display_end.row_mut() -= 1;
}
let buffer_end = DisplayPoint::new(
display_end.row(),
map.line_len(display_end.row(), ctx).unwrap(),
)
.to_buffer_point(map, Bias::Left, ctx)
.unwrap();
buffer_start.row..buffer_end.row + 1
}
}
File diff suppressed because it is too large Load Diff
+14
View File
@@ -67,6 +67,20 @@ impl DisplayMap {
Ok(chars.take_while(|c| *c != '\n').collect())
}
pub fn line_indent(&self, display_row: u32, app: &AppContext) -> Result<(u32, bool)> {
let mut indent = 0;
let mut is_blank = true;
for c in self.chars_at(DisplayPoint::new(display_row, 0), app)? {
if c == ' ' {
indent += 1;
} else {
is_blank = c == '\n';
break;
}
}
Ok((indent, is_blank))
}
pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Result<Chars<'a>> {
let column = point.column() as usize;
let (point, to_next_stop) = point.collapse_tabs(self, Bias::Left, app)?;
+3 -6
View File
@@ -12,14 +12,11 @@ use display_map::*;
use std::{cmp, ops::Range};
trait RangeExt<T> {
fn sorted(&self) -> (T, T);
fn sorted(&self) -> Range<T>;
}
impl<T: Ord + Clone> RangeExt<T> for Range<T> {
fn sorted(&self) -> (T, T) {
(
cmp::min(&self.start, &self.end).clone(),
cmp::max(&self.start, &self.end).clone(),
)
fn sorted(&self) -> Self {
cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone()
}
}
+21
View File
@@ -58,3 +58,24 @@ pub fn down(
Ok((point, goal_column))
}
pub fn line_beginning(
map: &DisplayMap,
point: DisplayPoint,
toggle_indent: bool,
app: &AppContext,
) -> Result<DisplayPoint> {
let (indent, is_blank) = map.line_indent(point.row(), app)?;
if toggle_indent && !is_blank && point.column() != indent {
Ok(DisplayPoint::new(point.row(), indent))
} else {
Ok(DisplayPoint::new(point.row(), 0))
}
}
pub fn line_end(map: &DisplayMap, point: DisplayPoint, app: &AppContext) -> Result<DisplayPoint> {
Ok(DisplayPoint::new(
point.row(),
map.line_len(point.row(), app)?,
))
}