WIP: Add keybinding to open buffers under cursors

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-01-07 16:12:50 +01:00
parent cf62d26ed8
commit 3cab32d201
7 changed files with 70 additions and 15 deletions

View file

@ -19,12 +19,21 @@ use util::TryFutureExt;
use workspace::Workspace;
action!(Deploy);
action!(OpenExcerpts);
const CONTEXT_LINE_COUNT: u32 = 1;
pub fn init(cx: &mut MutableAppContext) {
cx.add_bindings([Binding::new("alt-shift-D", Deploy, None)]);
cx.add_bindings([
Binding::new("alt-shift-D", Deploy, Some("Workspace")),
Binding::new(
"alt-shift-D",
OpenExcerpts,
Some("ProjectDiagnosticsEditor"),
),
]);
cx.add_action(ProjectDiagnosticsEditor::deploy);
cx.add_action(ProjectDiagnosticsEditor::open_excerpts);
}
type Event = editor::Event;
@ -155,6 +164,24 @@ impl ProjectDiagnosticsEditor {
workspace.add_item(diagnostics, cx);
}
fn open_excerpts(&mut self, _: &OpenExcerpts, cx: &mut ViewContext<Self>) {
let editor = self.editor.read(cx);
let excerpts = self.excerpts.read(cx);
let mut new_selections_by_buffer = HashMap::default();
for selection in editor.local_selections::<usize>(cx) {
for (buffer, range) in excerpts.excerpted_buffers(selection.start..selection.end, cx) {
new_selections_by_buffer
.entry(buffer)
.or_insert(Vec::new())
.push((range.start, range.end, selection.reversed))
}
}
for (buffer, selections) in new_selections_by_buffer {
// buffer.read(cx).
}
}
fn update_excerpts(&self, paths: HashSet<ProjectPath>, cx: &mut ViewContext<Self>) {
let project = self.project.clone();
cx.spawn(|this, mut cx| {

View file

@ -1105,7 +1105,7 @@ impl Editor {
T: ToOffset,
{
let buffer = self.buffer.read(cx).snapshot(cx);
let selections = ranges
let mut selections = ranges
.into_iter()
.map(|range| {
let mut start = range.start.to_offset(&buffer);
@ -1124,7 +1124,8 @@ impl Editor {
goal: SelectionGoal::None,
}
})
.collect();
.collect::<Vec<_>>();
selections.sort_unstable_by_key(|s| s.start);
self.update_selections(selections, autoscroll, cx);
}

View file

@ -689,6 +689,33 @@ impl MultiBuffer {
.map_or(Vec::new(), |state| state.excerpts.clone())
}
pub fn excerpted_buffers<'a, T: ToOffset>(
&'a self,
range: Range<T>,
cx: &AppContext,
) -> Vec<(ModelHandle<Buffer>, Range<usize>)> {
let snapshot = self.snapshot(cx);
let start = range.start.to_offset(&snapshot);
let end = range.end.to_offset(&snapshot);
let mut result = Vec::new();
let mut cursor = snapshot.excerpts.cursor::<usize>();
cursor.seek(&start, Bias::Right, &());
while let Some(excerpt) = cursor.item() {
if *cursor.start() > end {
break;
}
let excerpt_start = excerpt.range.start.to_offset(&excerpt.buffer);
let start = excerpt_start + (cmp::max(start, *cursor.start()) - *cursor.start());
let end = excerpt_start + (cmp::min(end, cursor.end(&())) - *cursor.start());
let buffer = self.buffers.borrow()[&excerpt.buffer_id].buffer.clone();
result.push((buffer, start..end));
}
result
}
pub fn remove_excerpts<'a>(
&mut self,
excerpt_ids: impl IntoIterator<Item = &'a ExcerptId>,

View file

@ -250,7 +250,7 @@ impl FileFinder {
match event {
Event::Selected(project_path) => {
workspace
.open_entry(project_path.clone(), cx)
.open_path(project_path.clone(), cx)
.map(|d| d.detach());
workspace.dismiss_modal(cx);
}

View file

@ -124,7 +124,7 @@ impl ProjectPanel {
if let Some(worktree) = project.read(cx).worktree_for_id(worktree_id, cx) {
if let Some(entry) = worktree.read(cx).entry_for_id(entry_id) {
workspace
.open_entry(
.open_path(
ProjectPath {
worktree_id,
path: entry.path.clone(),

View file

@ -561,7 +561,7 @@ impl Workspace {
let project_path = project_path.await.ok()?;
if fs.is_file(&abs_path).await {
if let Some(entry) =
this.update(&mut cx, |this, cx| this.open_entry(project_path, cx))
this.update(&mut cx, |this, cx| this.open_path(project_path, cx))
{
return Some(entry.await);
}
@ -665,7 +665,7 @@ impl Workspace {
}
#[must_use]
pub fn open_entry(
pub fn open_path(
&mut self,
project_path: ProjectPath,
cx: &mut ViewContext<Self>,

View file

@ -264,7 +264,7 @@ mod tests {
// Open the first entry
let entry_1 = workspace
.update(&mut cx, |w, cx| w.open_entry(file1.clone(), cx))
.update(&mut cx, |w, cx| w.open_path(file1.clone(), cx))
.unwrap()
.await
.unwrap();
@ -279,7 +279,7 @@ mod tests {
// Open the second entry
workspace
.update(&mut cx, |w, cx| w.open_entry(file2.clone(), cx))
.update(&mut cx, |w, cx| w.open_path(file2.clone(), cx))
.unwrap()
.await
.unwrap();
@ -294,7 +294,7 @@ mod tests {
// Open the first entry again. The existing pane item is activated.
let entry_1b = workspace
.update(&mut cx, |w, cx| w.open_entry(file1.clone(), cx).unwrap())
.update(&mut cx, |w, cx| w.open_path(file1.clone(), cx).unwrap())
.await
.unwrap();
assert_eq!(entry_1.id(), entry_1b.id());
@ -312,7 +312,7 @@ mod tests {
workspace
.update(&mut cx, |w, cx| {
w.split_pane(w.active_pane().clone(), SplitDirection::Right, cx);
w.open_entry(file2.clone(), cx).unwrap()
w.open_path(file2.clone(), cx).unwrap()
})
.await
.unwrap();
@ -331,8 +331,8 @@ mod tests {
// Open the third entry twice concurrently. Only one pane item is added.
let (t1, t2) = workspace.update(&mut cx, |w, cx| {
(
w.open_entry(file3.clone(), cx).unwrap(),
w.open_entry(file3.clone(), cx).unwrap(),
w.open_path(file3.clone(), cx).unwrap(),
w.open_path(file3.clone(), cx).unwrap(),
)
});
t1.await.unwrap();
@ -562,7 +562,7 @@ mod tests {
.update(&mut cx, |workspace, cx| {
workspace.split_pane(workspace.active_pane().clone(), SplitDirection::Right, cx);
workspace
.open_entry(
.open_path(
ProjectPath {
worktree_id: worktree.read(cx).id(),
path: Path::new("the-new-name.rs").into(),
@ -666,7 +666,7 @@ mod tests {
let pane_1 = cx.read(|cx| workspace.read(cx).active_pane().clone());
workspace
.update(&mut cx, |w, cx| w.open_entry(file1.clone(), cx))
.update(&mut cx, |w, cx| w.open_path(file1.clone(), cx))
.unwrap()
.await
.unwrap();