-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
vim: Add basic mark support #11507
Merged
Merged
vim: Add basic mark support #11507
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use std::{ops::Range, sync::Arc}; | ||
|
||
use collections::HashSet; | ||
use editor::{ | ||
display_map::{DisplaySnapshot, ToDisplayPoint}, | ||
movement, Anchor, Bias, DisplayPoint, | ||
}; | ||
use gpui::WindowContext; | ||
use language::SelectionGoal; | ||
|
||
use crate::{ | ||
motion::{self, Motion}, | ||
Vim, | ||
}; | ||
|
||
pub fn create_mark(vim: &mut Vim, text: Arc<str>, tail: bool, cx: &mut WindowContext) { | ||
let Some(anchors) = vim.update_active_editor(cx, |_, editor, _| { | ||
editor | ||
.selections | ||
.disjoint_anchors() | ||
.iter() | ||
.map(|s| if tail { s.tail() } else { s.head() }) | ||
.collect::<Vec<_>>() | ||
}) else { | ||
return; | ||
}; | ||
|
||
vim.update_state(|state| state.marks.insert(text.to_string(), anchors)); | ||
vim.clear_operator(cx); | ||
} | ||
|
||
pub fn create_mark_after(vim: &mut Vim, text: Arc<str>, cx: &mut WindowContext) { | ||
let Some(anchors) = vim.update_active_editor(cx, |_, editor, cx| { | ||
let (map, selections) = editor.selections.all_display(cx); | ||
selections | ||
.into_iter() | ||
.map(|selection| { | ||
let point = movement::saturating_right(&map, selection.tail()); | ||
map.buffer_snapshot | ||
.anchor_before(point.to_offset(&map, Bias::Left)) | ||
}) | ||
.collect::<Vec<_>>() | ||
}) else { | ||
return; | ||
}; | ||
|
||
vim.update_state(|state| state.marks.insert(text.to_string(), anchors)); | ||
vim.clear_operator(cx); | ||
} | ||
|
||
pub fn create_mark_before(vim: &mut Vim, text: Arc<str>, cx: &mut WindowContext) { | ||
let Some(anchors) = vim.update_active_editor(cx, |_, editor, cx| { | ||
let (map, selections) = editor.selections.all_display(cx); | ||
selections | ||
.into_iter() | ||
.map(|selection| { | ||
let point = movement::saturating_left(&map, selection.head()); | ||
map.buffer_snapshot | ||
.anchor_before(point.to_offset(&map, Bias::Left)) | ||
}) | ||
.collect::<Vec<_>>() | ||
}) else { | ||
return; | ||
}; | ||
|
||
vim.update_state(|state| state.marks.insert(text.to_string(), anchors)); | ||
vim.clear_operator(cx); | ||
} | ||
|
||
pub fn jump(text: Arc<str>, line: bool, cx: &mut WindowContext) { | ||
let anchors = match &*text { | ||
"{" | "}" => Vim::update(cx, |vim, cx| { | ||
vim.update_active_editor(cx, |_, editor, cx| { | ||
let (map, selections) = editor.selections.all_display(cx); | ||
selections | ||
.into_iter() | ||
.map(|selection| { | ||
let point = if &*text == "{" { | ||
movement::start_of_paragraph(&map, selection.head(), 1) | ||
} else { | ||
movement::end_of_paragraph(&map, selection.head(), 1) | ||
}; | ||
map.buffer_snapshot | ||
.anchor_before(point.to_offset(&map, Bias::Left)) | ||
}) | ||
.collect::<Vec<Anchor>>() | ||
}) | ||
}), | ||
_ => Vim::read(cx).state().marks.get(&*text).cloned(), | ||
}; | ||
|
||
Vim::update(cx, |vim, cx| { | ||
vim.pop_operator(cx); | ||
}); | ||
|
||
let Some(anchors) = anchors else { return }; | ||
|
||
let is_active_operator = Vim::read(cx).state().active_operator().is_some(); | ||
if is_active_operator { | ||
if let Some(anchor) = anchors.last() { | ||
motion::motion( | ||
Motion::Jump { | ||
anchor: *anchor, | ||
line, | ||
}, | ||
cx, | ||
) | ||
} | ||
return; | ||
} else { | ||
Vim::update(cx, |vim, cx| { | ||
vim.update_active_editor(cx, |_, editor, cx| { | ||
let map = editor.snapshot(cx); | ||
let mut ranges: HashSet<Range<Anchor>> = HashSet::default(); | ||
for mut anchor in anchors { | ||
if line { | ||
let mut point = anchor.to_display_point(&map.display_snapshot); | ||
point = motion::first_non_whitespace(&map.display_snapshot, false, point); | ||
anchor = map | ||
.display_snapshot | ||
.buffer_snapshot | ||
.anchor_before(point.to_point(&map.display_snapshot)); | ||
} | ||
ranges.insert(anchor..anchor); | ||
} | ||
editor.change_selections(None, cx, |s| s.select_anchor_ranges(ranges)) | ||
}); | ||
}) | ||
} | ||
} | ||
|
||
pub fn jump_motion( | ||
map: &DisplaySnapshot, | ||
anchor: Anchor, | ||
line: bool, | ||
) -> (DisplayPoint, SelectionGoal) { | ||
let mut point = anchor.to_display_point(map); | ||
if line { | ||
point = motion::first_non_whitespace(map, false, point) | ||
} | ||
|
||
(point, SelectionGoal::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
Oops, something went wrong.
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.
@Zachiah to fix the scrolling issue, this
None
needs to beSome(AutoScroll::fit())