Remove Editor::find_or_create

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-03-17 15:13:47 +01:00
parent 6f9c37851c
commit bff414cfbc
6 changed files with 63 additions and 28 deletions

View file

@ -847,27 +847,6 @@ impl Editor {
Self::new(EditorMode::Full, buffer, project, None, cx)
}
pub fn find_or_create(
workspace: &mut Workspace,
buffer: ModelHandle<Buffer>,
cx: &mut ViewContext<Workspace>,
) -> ViewHandle<Editor> {
let project = workspace.project().clone();
if let Some(item) = project::File::from_dyn(buffer.read(cx).file())
.and_then(|file| file.project_entry_id(cx))
.and_then(|entry_id| workspace.active_pane().read(cx).item_for_entry(entry_id))
.and_then(|item| item.downcast())
{
workspace.activate_item(&item, cx);
return item;
}
let editor = cx.add_view(|cx| Editor::for_buffer(buffer, Some(project.clone()), cx));
workspace.add_item(Box::new(editor.clone()), cx);
editor
}
pub fn clone(&self, cx: &mut ViewContext<Self>) -> Self {
let mut clone = Self::new(
self.mode,
@ -4324,8 +4303,7 @@ impl Editor {
for definition in definitions {
let range = definition.range.to_offset(definition.buffer.read(cx));
let target_editor_handle =
Self::find_or_create(workspace, definition.buffer, cx);
let target_editor_handle = workspace.open_project_item(definition.buffer, cx);
target_editor_handle.update(cx, |target_editor, cx| {
// When selecting a definition in a different buffer, disable the nav history
// to avoid creating a history entry at the previous cursor location.
@ -5597,7 +5575,7 @@ impl Editor {
workspace.activate_next_pane(cx);
for (buffer, ranges) in new_selections_by_buffer.into_iter() {
let editor = Self::find_or_create(workspace, buffer, cx);
let editor = workspace.open_project_item::<Self>(buffer, cx);
editor.update(cx, |editor, cx| {
editor.select_ranges(ranges, Some(Autoscroll::Newest), cx);
});

View file

@ -4,13 +4,13 @@ use gpui::{
elements::*, AppContext, Entity, ModelHandle, RenderContext, Subscription, Task, View,
ViewContext, ViewHandle,
};
use language::{Bias, Diagnostic, File as _};
use language::{Bias, Buffer, Diagnostic, File as _};
use project::{File, Project, ProjectPath};
use std::fmt::Write;
use std::path::PathBuf;
use text::{Point, Selection};
use util::ResultExt;
use workspace::{Item, ItemHandle, ItemNavHistory, Settings, StatusItemView};
use workspace::{Item, ItemHandle, ItemNavHistory, ProjectItem, Settings, StatusItemView};
impl Item for Editor {
fn navigate(&mut self, data: Box<dyn std::any::Any>, cx: &mut ViewContext<Self>) {
@ -132,6 +132,18 @@ impl Item for Editor {
}
}
impl ProjectItem for Editor {
type Item = Buffer;
fn for_project_item(
project: ModelHandle<Project>,
buffer: ModelHandle<Buffer>,
cx: &mut ViewContext<Self>,
) -> Self {
Self::for_buffer(buffer, Some(project), cx)
}
}
pub struct CursorPosition {
position: Option<Point>,
selected_count: usize,