Remove 2 suffix for vim, diagnostics, go_to_line, theme_selector, command_palette, file_finder

Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
Max Brunsfeld 2024-01-03 10:42:49 -08:00
parent 37e6533b28
commit 252694390a
185 changed files with 1933 additions and 29192 deletions

View file

@ -9,15 +9,17 @@ path = "src/go_to_line.rs"
doctest = false
[dependencies]
editor = { path = "../editor" }
gpui = { path = "../gpui" }
menu = { path = "../menu" }
settings = { path = "../settings" }
text = { path = "../text" }
workspace = { path = "../workspace" }
editor = { package = "editor2", path = "../editor2" }
gpui = { package = "gpui2", path = "../gpui2" }
menu = { package = "menu2", path = "../menu2" }
serde.workspace = true
settings = { package = "settings2", path = "../settings2" }
text = { package = "text2", path = "../text2" }
workspace = { package = "workspace2", path = "../workspace2" }
postage.workspace = true
theme = { path = "../theme" }
theme = { package = "theme2", path = "../theme2" }
ui = { package = "ui2", path = "../ui2" }
util = { path = "../util" }
[dev-dependencies]
editor = { path = "../editor", features = ["test-support"] }
editor = { package = "editor2", path = "../editor2", features = ["test-support"] }

View file

@ -1,120 +1,120 @@
use std::sync::Arc;
use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Editor};
use gpui::{
actions, elements::*, geometry::vector::Vector2F, AnyViewHandle, AppContext, Axis, Entity,
View, ViewContext, ViewHandle,
actions, div, prelude::*, AnyWindowHandle, AppContext, DismissEvent, EventEmitter, FocusHandle,
FocusableView, Render, SharedString, Styled, Subscription, View, ViewContext, VisualContext,
};
use menu::{Cancel, Confirm};
use text::{Bias, Point};
use theme::ActiveTheme;
use ui::{h_stack, prelude::*, v_stack, Label};
use util::paths::FILE_ROW_COLUMN_DELIMITER;
use workspace::{Modal, Workspace};
use workspace::ModalView;
actions!(go_to_line, [Toggle]);
pub fn init(cx: &mut AppContext) {
cx.add_action(GoToLine::toggle);
cx.add_action(GoToLine::confirm);
cx.add_action(GoToLine::cancel);
cx.observe_new_views(GoToLine::register).detach();
}
pub struct GoToLine {
line_editor: ViewHandle<Editor>,
active_editor: ViewHandle<Editor>,
prev_scroll_position: Option<Vector2F>,
cursor_point: Point,
max_point: Point,
has_focus: bool,
line_editor: View<Editor>,
active_editor: View<Editor>,
current_text: SharedString,
prev_scroll_position: Option<gpui::Point<f32>>,
_subscriptions: Vec<Subscription>,
}
pub enum Event {
Dismissed,
impl ModalView for GoToLine {}
impl FocusableView for GoToLine {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
self.line_editor.focus_handle(cx)
}
}
impl EventEmitter<DismissEvent> for GoToLine {}
impl GoToLine {
pub fn new(active_editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) -> Self {
let line_editor = cx.add_view(|cx| {
Editor::single_line(
Some(Arc::new(|theme| theme.picker.input_editor.clone())),
cx,
)
fn register(editor: &mut Editor, cx: &mut ViewContext<Editor>) {
let handle = cx.view().downgrade();
editor.register_action(move |_: &Toggle, cx| {
let Some(editor) = handle.upgrade() else {
return;
};
let Some(workspace) = editor.read(cx).workspace() else {
return;
};
workspace.update(cx, |workspace, cx| {
workspace.toggle_modal(cx, move |cx| GoToLine::new(editor, cx));
})
});
cx.subscribe(&line_editor, Self::on_line_editor_event)
.detach();
}
let (scroll_position, cursor_point, max_point) = active_editor.update(cx, |editor, cx| {
let scroll_position = editor.scroll_position(cx);
let buffer = editor.buffer().read(cx).snapshot(cx);
(
Some(scroll_position),
editor.selections.newest(cx).head(),
buffer.max_point(),
)
});
pub fn new(active_editor: View<Editor>, cx: &mut ViewContext<Self>) -> Self {
let line_editor = cx.new_view(|cx| Editor::single_line(cx));
let line_editor_change = cx.subscribe(&line_editor, Self::on_line_editor_event);
let editor = active_editor.read(cx);
let cursor = editor.selections.last::<Point>(cx).head();
let last_line = editor.buffer().read(cx).snapshot(cx).max_point().row;
let scroll_position = active_editor.update(cx, |editor, cx| editor.scroll_position(cx));
let current_text = format!(
"line {} of {} (column {})",
cursor.row + 1,
last_line + 1,
cursor.column + 1,
);
Self {
line_editor,
active_editor,
prev_scroll_position: scroll_position,
cursor_point,
max_point,
has_focus: false,
current_text: current_text.into(),
prev_scroll_position: Some(scroll_position),
_subscriptions: vec![line_editor_change, cx.on_release(Self::release)],
}
}
fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
if let Some(editor) = workspace
.active_item(cx)
.and_then(|active_item| active_item.downcast::<Editor>())
{
workspace.toggle_modal(cx, |_, cx| cx.add_view(|cx| GoToLine::new(editor, cx)));
}
}
fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
cx.emit(Event::Dismissed);
}
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
self.prev_scroll_position.take();
if let Some(point) = self.point_from_query(cx) {
self.active_editor.update(cx, |active_editor, cx| {
let snapshot = active_editor.snapshot(cx).display_snapshot;
let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
s.select_ranges([point..point])
});
});
}
cx.emit(Event::Dismissed);
fn release(&mut self, window: AnyWindowHandle, cx: &mut AppContext) {
window
.update(cx, |_, cx| {
let scroll_position = self.prev_scroll_position.take();
self.active_editor.update(cx, |editor, cx| {
editor.highlight_rows(None);
if let Some(scroll_position) = scroll_position {
editor.set_scroll_position(scroll_position, cx);
}
cx.notify();
})
})
.ok();
}
fn on_line_editor_event(
&mut self,
_: ViewHandle<Editor>,
event: &editor::Event,
_: View<Editor>,
event: &editor::EditorEvent,
cx: &mut ViewContext<Self>,
) {
match event {
editor::Event::Blurred => cx.emit(Event::Dismissed),
editor::Event::BufferEdited { .. } => {
if let Some(point) = self.point_from_query(cx) {
self.active_editor.update(cx, |active_editor, cx| {
let snapshot = active_editor.snapshot(cx).display_snapshot;
let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
let display_point = point.to_display_point(&snapshot);
let row = display_point.row();
active_editor.highlight_rows(Some(row..row + 1));
active_editor.request_autoscroll(Autoscroll::center(), cx);
});
cx.notify();
}
}
editor::EditorEvent::Blurred => cx.emit(DismissEvent),
editor::EditorEvent::BufferEdited { .. } => self.highlight_current_line(cx),
_ => {}
}
}
fn highlight_current_line(&mut self, cx: &mut ViewContext<Self>) {
if let Some(point) = self.point_from_query(cx) {
self.active_editor.update(cx, |active_editor, cx| {
let snapshot = active_editor.snapshot(cx).display_snapshot;
let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
let display_point = point.to_display_point(&snapshot);
let row = display_point.row();
active_editor.highlight_rows(Some(row..row + 1));
active_editor.request_autoscroll(Autoscroll::center(), cx);
});
cx.notify();
}
}
fn point_from_query(&self, cx: &ViewContext<Self>) -> Option<Point> {
let line_editor = self.line_editor.read(cx).text(cx);
let mut components = line_editor
@ -128,73 +128,61 @@ impl GoToLine {
column.unwrap_or(0).saturating_sub(1),
))
}
}
impl Entity for GoToLine {
type Event = Event;
fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
cx.emit(DismissEvent);
}
fn release(&mut self, cx: &mut AppContext) {
let scroll_position = self.prev_scroll_position.take();
self.active_editor.window().update(cx, |cx| {
fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
if let Some(point) = self.point_from_query(cx) {
self.active_editor.update(cx, |editor, cx| {
editor.highlight_rows(None);
if let Some(scroll_position) = scroll_position {
editor.set_scroll_position(scroll_position, cx);
}
})
});
let snapshot = editor.snapshot(cx).display_snapshot;
let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
editor.change_selections(Some(Autoscroll::center()), cx, |s| {
s.select_ranges([point..point])
});
editor.focus(cx);
cx.notify();
});
self.prev_scroll_position.take();
}
cx.emit(DismissEvent);
}
}
impl View for GoToLine {
fn ui_name() -> &'static str {
"GoToLine"
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
let theme = &theme::current(cx).picker;
let label = format!(
"{}{FILE_ROW_COLUMN_DELIMITER}{} of {} lines",
self.cursor_point.row + 1,
self.cursor_point.column + 1,
self.max_point.row + 1
);
Flex::new(Axis::Vertical)
.with_child(
ChildView::new(&self.line_editor, cx)
.contained()
.with_style(theme.input_editor.container),
impl Render for GoToLine {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
div()
.elevation_2(cx)
.key_context("GoToLine")
.on_action(cx.listener(Self::cancel))
.on_action(cx.listener(Self::confirm))
.w_96()
.child(
v_stack()
.px_1()
.pt_0p5()
.gap_px()
.child(
v_stack()
.py_0p5()
.px_1()
.child(div().px_1().py_0p5().child(self.line_editor.clone())),
)
.child(
div()
.h_px()
.w_full()
.bg(cx.theme().colors().element_background),
)
.child(
h_stack()
.justify_between()
.px_2()
.py_1()
.child(Label::new(self.current_text.clone()).color(Color::Muted)),
),
)
.with_child(
Label::new(label, theme.no_matches.label.clone())
.contained()
.with_style(theme.no_matches.container),
)
.contained()
.with_style(theme.container)
.constrained()
.with_max_width(500.0)
.into_any_named("go to line")
}
fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
self.has_focus = true;
cx.focus(&self.line_editor);
}
fn focus_out(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {
self.has_focus = false;
}
}
impl Modal for GoToLine {
fn has_focus(&self) -> bool {
self.has_focus
}
fn dismiss_on_event(event: &Self::Event) -> bool {
matches!(event, Event::Dismissed)
}
}