Merge pull request #1804 from zed-industries/vim-go-to-line

fix jump to line number in vim mode
This commit is contained in:
Kay Simmons 2022-10-26 11:43:27 -07:00 committed by GitHub
commit 88be4fe77e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 6 deletions

View File

@ -4,7 +4,7 @@ use editor::{
movement, Bias, CharKind, DisplayPoint, movement, Bias, CharKind, DisplayPoint,
}; };
use gpui::{actions, impl_actions, MutableAppContext}; use gpui::{actions, impl_actions, MutableAppContext};
use language::{Selection, SelectionGoal}; use language::{Point, Selection, SelectionGoal};
use serde::Deserialize; use serde::Deserialize;
use workspace::Workspace; use workspace::Workspace;
@ -189,7 +189,7 @@ impl Motion {
EndOfLine => (end_of_line(map, point), SelectionGoal::None), EndOfLine => (end_of_line(map, point), SelectionGoal::None),
CurrentLine => (end_of_line(map, point), SelectionGoal::None), CurrentLine => (end_of_line(map, point), SelectionGoal::None),
StartOfDocument => (start_of_document(map, point, times), SelectionGoal::None), StartOfDocument => (start_of_document(map, point, times), SelectionGoal::None),
EndOfDocument => (end_of_document(map, point), SelectionGoal::None), EndOfDocument => (end_of_document(map, point, times), SelectionGoal::None),
Matching => (matching(map, point), SelectionGoal::None), Matching => (matching(map, point), SelectionGoal::None),
} }
} }
@ -409,13 +409,17 @@ fn end_of_line(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint {
} }
fn start_of_document(map: &DisplaySnapshot, point: DisplayPoint, line: usize) -> DisplayPoint { fn start_of_document(map: &DisplaySnapshot, point: DisplayPoint, line: usize) -> DisplayPoint {
let mut new_point = (line - 1).to_display_point(map); let mut new_point = Point::new((line - 1) as u32, 0).to_display_point(map);
*new_point.column_mut() = point.column(); *new_point.column_mut() = point.column();
map.clip_point(new_point, Bias::Left) map.clip_point(new_point, Bias::Left)
} }
fn end_of_document(map: &DisplaySnapshot, point: DisplayPoint) -> DisplayPoint { fn end_of_document(map: &DisplaySnapshot, point: DisplayPoint, line: usize) -> DisplayPoint {
let mut new_point = map.max_point(); let mut new_point = if line == 1 {
map.max_point()
} else {
Point::new((line - 1) as u32, 0).to_display_point(map)
};
*new_point.column_mut() = point.column(); *new_point.column_mut() = point.column();
map.clip_point(new_point, Bias::Left) map.clip_point(new_point, Bias::Left)
} }

View File

@ -566,10 +566,42 @@ mod test {
.await; .await;
cx.assert_binding_matches( cx.assert_binding_matches(
["2", "g", "g"], ["2", "g", "g"],
indoc! {"
ˇ
brown fox jumps
over the lazydog"},
)
.await;
}
#[gpui::test]
async fn test_end_of_document(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.assert_binding_matches_all(
["shift-g"],
indoc! {"
The qˇuick
brown fox jumps
over ˇthe laˇzy dog"},
)
.await;
cx.assert_binding_matches(
["shift-g"],
indoc! {" indoc! {"
brown fox juˇmps brown fox jumps
over the laˇzy dog"},
)
.await;
cx.assert_binding_matches(
["2", "shift-g"],
indoc! {"
ˇ
brown fox jumps
over the lazydog"}, over the lazydog"},
) )
.await; .await;

View File

@ -0,0 +1 @@
[{"Text":"The quick\n\nbrown fox jumps\nover the lazy dog"},{"Mode":"Normal"},{"Selection":{"start":[3,5],"end":[3,5]}}]

View File

@ -0,0 +1 @@
[{"Text":"The quick\n\nbrown fox jumps\nover the lazy dog"},{"Mode":"Normal"},{"Selection":{"start":[3,5],"end":[3,5]}},{"Mode":"Normal"},{"Text":"The quick\n\nbrown fox jumps\nover the lazy dog"},{"Mode":"Normal"},{"Selection":{"start":[3,5],"end":[3,5]}},{"Mode":"Normal"},{"Text":"The quick\n\nbrown fox jumps\nover the lazy dog"},{"Mode":"Normal"},{"Selection":{"start":[3,11],"end":[3,11]}},{"Mode":"Normal"},{"Text":"\n\nbrown fox jumps\nover the lazy dog"},{"Mode":"Normal"},{"Selection":{"start":[3,11],"end":[3,11]}},{"Mode":"Normal"},{"Text":"\n\nbrown fox jumps\nover the lazydog"},{"Mode":"Normal"},{"Selection":{"start":[1,0],"end":[1,0]}},{"Mode":"Normal"}]