Add Editor::for_multibuffer
and repurpose Editor::for_buffer
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
44d997c00c
commit
6f9c37851c
5 changed files with 27 additions and 26 deletions
|
@ -113,7 +113,8 @@ impl ProjectDiagnosticsEditor {
|
||||||
|
|
||||||
let excerpts = cx.add_model(|cx| MultiBuffer::new(project_handle.read(cx).replica_id()));
|
let excerpts = cx.add_model(|cx| MultiBuffer::new(project_handle.read(cx).replica_id()));
|
||||||
let editor = cx.add_view(|cx| {
|
let editor = cx.add_view(|cx| {
|
||||||
let mut editor = Editor::for_buffer(excerpts.clone(), Some(project_handle.clone()), cx);
|
let mut editor =
|
||||||
|
Editor::for_multibuffer(excerpts.clone(), Some(project_handle.clone()), cx);
|
||||||
editor.set_vertical_scroll_margin(5, cx);
|
editor.set_vertical_scroll_margin(5, cx);
|
||||||
editor
|
editor
|
||||||
});
|
});
|
||||||
|
|
|
@ -341,7 +341,6 @@ pub fn init(cx: &mut MutableAppContext) {
|
||||||
cx.add_async_action(Editor::find_all_references);
|
cx.add_async_action(Editor::find_all_references);
|
||||||
|
|
||||||
workspace::register_editor_builder(cx, |project, buffer, cx| {
|
workspace::register_editor_builder(cx, |project, buffer, cx| {
|
||||||
let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
|
|
||||||
Editor::for_buffer(buffer, Some(project), cx)
|
Editor::for_buffer(buffer, Some(project), cx)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -832,6 +831,15 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn for_buffer(
|
pub fn for_buffer(
|
||||||
|
buffer: ModelHandle<Buffer>,
|
||||||
|
project: Option<ModelHandle<Project>>,
|
||||||
|
cx: &mut ViewContext<Self>,
|
||||||
|
) -> Self {
|
||||||
|
let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
|
||||||
|
Self::new(EditorMode::Full, buffer, project, None, cx)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn for_multibuffer(
|
||||||
buffer: ModelHandle<MultiBuffer>,
|
buffer: ModelHandle<MultiBuffer>,
|
||||||
project: Option<ModelHandle<Project>>,
|
project: Option<ModelHandle<Project>>,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
|
@ -855,8 +863,7 @@ impl Editor {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
|
let editor = cx.add_view(|cx| Editor::for_buffer(buffer, Some(project.clone()), cx));
|
||||||
let editor = cx.add_view(|cx| Editor::for_buffer(multibuffer, Some(project.clone()), cx));
|
|
||||||
workspace.add_item(Box::new(editor.clone()), cx);
|
workspace.add_item(Box::new(editor.clone()), cx);
|
||||||
editor
|
editor
|
||||||
}
|
}
|
||||||
|
@ -969,11 +976,8 @@ impl Editor {
|
||||||
.update(cx, |project, cx| project.create_buffer(cx))
|
.update(cx, |project, cx| project.create_buffer(cx))
|
||||||
.log_err()
|
.log_err()
|
||||||
{
|
{
|
||||||
let multibuffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx));
|
|
||||||
workspace.add_item(
|
workspace.add_item(
|
||||||
Box::new(
|
Box::new(cx.add_view(|cx| Editor::for_buffer(buffer, Some(project.clone()), cx))),
|
||||||
cx.add_view(|cx| Editor::for_buffer(multibuffer, Some(project.clone()), cx)),
|
|
||||||
),
|
|
||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -2380,7 +2384,8 @@ impl Editor {
|
||||||
|
|
||||||
workspace.update(&mut cx, |workspace, cx| {
|
workspace.update(&mut cx, |workspace, cx| {
|
||||||
let project = workspace.project().clone();
|
let project = workspace.project().clone();
|
||||||
let editor = cx.add_view(|cx| Editor::for_buffer(excerpt_buffer, Some(project), cx));
|
let editor =
|
||||||
|
cx.add_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx));
|
||||||
workspace.add_item(Box::new(editor.clone()), cx);
|
workspace.add_item(Box::new(editor.clone()), cx);
|
||||||
editor.update(cx, |editor, cx| {
|
editor.update(cx, |editor, cx| {
|
||||||
let color = editor.style(cx).highlighted_line_background;
|
let color = editor.style(cx).highlighted_line_background;
|
||||||
|
@ -4397,7 +4402,7 @@ impl Editor {
|
||||||
|
|
||||||
workspace.update(&mut cx, |workspace, cx| {
|
workspace.update(&mut cx, |workspace, cx| {
|
||||||
let editor =
|
let editor =
|
||||||
cx.add_view(|cx| Editor::for_buffer(excerpt_buffer, Some(project), cx));
|
cx.add_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx));
|
||||||
editor.update(cx, |editor, cx| {
|
editor.update(cx, |editor, cx| {
|
||||||
let color = editor.style(cx).highlighted_line_background;
|
let color = editor.style(cx).highlighted_line_background;
|
||||||
editor.highlight_background::<Self>(ranges_to_highlight, color, cx);
|
editor.highlight_background::<Self>(ranges_to_highlight, color, cx);
|
||||||
|
|
|
@ -510,8 +510,9 @@ impl SearchBar {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use editor::{DisplayPoint, Editor, MultiBuffer};
|
use editor::{DisplayPoint, Editor};
|
||||||
use gpui::{color::Color, TestAppContext};
|
use gpui::{color::Color, TestAppContext};
|
||||||
|
use language::Buffer;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use unindent::Unindent as _;
|
use unindent::Unindent as _;
|
||||||
|
|
||||||
|
@ -523,9 +524,10 @@ mod tests {
|
||||||
let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
|
let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap();
|
||||||
cx.update(|cx| cx.set_global(settings));
|
cx.update(|cx| cx.set_global(settings));
|
||||||
|
|
||||||
let buffer = cx.update(|cx| {
|
let buffer = cx.add_model(|cx| {
|
||||||
MultiBuffer::build_simple(
|
Buffer::new(
|
||||||
&r#"
|
0,
|
||||||
|
r#"
|
||||||
A regular expression (shortened as regex or regexp;[1] also referred to as
|
A regular expression (shortened as regex or regexp;[1] also referred to as
|
||||||
rational expression[2][3]) is a sequence of characters that specifies a search
|
rational expression[2][3]) is a sequence of characters that specifies a search
|
||||||
pattern in text. Usually such patterns are used by string-searching algorithms
|
pattern in text. Usually such patterns are used by string-searching algorithms
|
||||||
|
|
|
@ -339,7 +339,7 @@ impl ProjectSearchView {
|
||||||
});
|
});
|
||||||
|
|
||||||
let results_editor = cx.add_view(|cx| {
|
let results_editor = cx.add_view(|cx| {
|
||||||
let mut editor = Editor::for_buffer(excerpts, Some(project), cx);
|
let mut editor = Editor::for_multibuffer(excerpts, Some(project), cx);
|
||||||
editor.set_searchable(false);
|
editor.set_searchable(false);
|
||||||
editor
|
editor
|
||||||
});
|
});
|
||||||
|
|
|
@ -1013,8 +1013,8 @@ mod tests {
|
||||||
};
|
};
|
||||||
use collections::BTreeMap;
|
use collections::BTreeMap;
|
||||||
use editor::{
|
use editor::{
|
||||||
self, ConfirmCodeAction, ConfirmCompletion, ConfirmRename, Editor, Input, MultiBuffer,
|
self, ConfirmCodeAction, ConfirmCompletion, ConfirmRename, Editor, Input, Redo, Rename,
|
||||||
Redo, Rename, ToOffset, ToggleCodeActions, Undo,
|
ToOffset, ToggleCodeActions, Undo,
|
||||||
};
|
};
|
||||||
use gpui::{executor, ModelHandle, TestAppContext};
|
use gpui::{executor, ModelHandle, TestAppContext};
|
||||||
use language::{
|
use language::{
|
||||||
|
@ -1140,10 +1140,7 @@ mod tests {
|
||||||
.update(cx_b, |p, cx| p.open_buffer((worktree_id, "b.txt"), cx))
|
.update(cx_b, |p, cx| p.open_buffer((worktree_id, "b.txt"), cx))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let buffer_b = cx_b.add_model(|cx| MultiBuffer::singleton(buffer_b, cx));
|
buffer_b.read_with(cx_b, |buf, _| assert_eq!(buf.text(), "b-contents"));
|
||||||
buffer_b.read_with(cx_b, |buf, cx| {
|
|
||||||
assert_eq!(buf.read(cx).text(), "b-contents")
|
|
||||||
});
|
|
||||||
project_a.read_with(cx_a, |project, cx| {
|
project_a.read_with(cx_a, |project, cx| {
|
||||||
assert!(project.has_open_buffer((worktree_id, "b.txt"), cx))
|
assert!(project.has_open_buffer((worktree_id, "b.txt"), cx))
|
||||||
});
|
});
|
||||||
|
@ -2176,11 +2173,7 @@ mod tests {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let (window_b, _) = cx_b.add_window(|_| EmptyView);
|
let (window_b, _) = cx_b.add_window(|_| EmptyView);
|
||||||
let editor_b = cx_b.add_view(window_b, |cx| {
|
let editor_b = cx_b.add_view(window_b, |cx| {
|
||||||
Editor::for_buffer(
|
Editor::for_buffer(buffer_b.clone(), Some(project_b.clone()), cx)
|
||||||
cx.add_model(|cx| MultiBuffer::singleton(buffer_b.clone(), cx)),
|
|
||||||
Some(project_b.clone()),
|
|
||||||
cx,
|
|
||||||
)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut fake_language_server = fake_language_servers.next().await.unwrap();
|
let mut fake_language_server = fake_language_servers.next().await.unwrap();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue