2023-11-09 00:09:38 +00:00
|
|
|
use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Editor};
|
2023-11-07 20:23:08 +00:00
|
|
|
use gpui::{
|
2023-11-27 05:27:33 +00:00
|
|
|
actions, div, prelude::*, AppContext, DismissEvent, Div, EventEmitter, FocusHandle,
|
|
|
|
FocusableView, Render, SharedString, Styled, Subscription, View, ViewContext, VisualContext,
|
|
|
|
WindowContext,
|
2023-11-07 20:42:33 +00:00
|
|
|
};
|
2023-11-09 00:09:38 +00:00
|
|
|
use text::{Bias, Point};
|
2023-11-07 22:27:08 +00:00
|
|
|
use theme::ActiveTheme;
|
2023-11-21 06:05:29 +00:00
|
|
|
use ui::{h_stack, v_stack, Color, Label, StyledExt};
|
2023-11-07 20:42:33 +00:00
|
|
|
use util::paths::FILE_ROW_COLUMN_DELIMITER;
|
2023-11-17 05:46:44 +00:00
|
|
|
use workspace::Workspace;
|
2023-11-06 22:35:49 +00:00
|
|
|
|
2023-11-07 19:07:04 +00:00
|
|
|
actions!(Toggle);
|
2023-11-06 22:35:49 +00:00
|
|
|
|
|
|
|
pub fn init(cx: &mut AppContext) {
|
2023-11-10 04:51:48 +00:00
|
|
|
cx.observe_new_views(GoToLine::register).detach();
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 20:42:33 +00:00
|
|
|
pub struct GoToLine {
|
|
|
|
line_editor: View<Editor>,
|
|
|
|
active_editor: View<Editor>,
|
2023-11-09 00:09:38 +00:00
|
|
|
current_text: SharedString,
|
|
|
|
prev_scroll_position: Option<gpui::Point<f32>>,
|
|
|
|
_subscriptions: Vec<Subscription>,
|
2023-11-07 20:42:33 +00:00
|
|
|
}
|
2023-11-06 22:35:49 +00:00
|
|
|
|
2023-11-17 17:51:11 +00:00
|
|
|
impl FocusableView for GoToLine {
|
2023-11-17 05:46:44 +00:00
|
|
|
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
|
2023-11-17 17:51:11 +00:00
|
|
|
self.active_editor.focus_handle(cx)
|
2023-11-10 03:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-27 05:27:33 +00:00
|
|
|
impl EventEmitter<DismissEvent> for GoToLine {}
|
2023-11-06 22:35:49 +00:00
|
|
|
|
2023-11-07 20:42:33 +00:00
|
|
|
impl GoToLine {
|
2023-11-10 04:51:48 +00:00
|
|
|
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
|
|
|
|
workspace.register_action(|workspace, _: &Toggle, cx| {
|
|
|
|
let Some(editor) = workspace
|
|
|
|
.active_item(cx)
|
|
|
|
.and_then(|active_item| active_item.downcast::<Editor>())
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
workspace.toggle_modal(cx, move |cx| GoToLine::new(editor, cx));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:42:33 +00:00
|
|
|
pub fn new(active_editor: View<Editor>, cx: &mut ViewContext<Self>) -> Self {
|
2023-11-10 03:58:35 +00:00
|
|
|
let line_editor = cx.build_view(|cx| Editor::single_line(cx));
|
2023-11-09 00:09:38 +00:00
|
|
|
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,
|
|
|
|
);
|
2023-11-07 20:42:33 +00:00
|
|
|
|
|
|
|
Self {
|
|
|
|
line_editor,
|
|
|
|
active_editor,
|
2023-11-09 00:09:38 +00:00
|
|
|
current_text: current_text.into(),
|
|
|
|
prev_scroll_position: Some(scroll_position),
|
|
|
|
_subscriptions: vec![line_editor_change, cx.on_release(Self::release)],
|
2023-11-07 20:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 00:09:38 +00:00
|
|
|
fn release(&mut self, cx: &mut WindowContext) {
|
|
|
|
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();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:42:33 +00:00
|
|
|
fn on_line_editor_event(
|
|
|
|
&mut self,
|
|
|
|
_: View<Editor>,
|
2023-11-15 00:25:55 +00:00
|
|
|
event: &editor::EditorEvent,
|
2023-11-07 20:42:33 +00:00
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
|
|
|
match event {
|
2023-11-09 00:09:38 +00:00
|
|
|
// todo!() this isn't working...
|
2023-11-27 05:27:33 +00:00
|
|
|
editor::EditorEvent::Blurred => cx.emit(DismissEvent::Dismiss),
|
2023-11-15 00:25:55 +00:00
|
|
|
editor::EditorEvent::BufferEdited { .. } => self.highlight_current_line(cx),
|
2023-11-07 20:42:33 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-09 00:09:38 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:42:33 +00:00
|
|
|
fn point_from_query(&self, cx: &ViewContext<Self>) -> Option<Point> {
|
2023-11-07 22:27:08 +00:00
|
|
|
let line_editor = self.line_editor.read(cx).text(cx);
|
2023-11-07 20:42:33 +00:00
|
|
|
let mut components = line_editor
|
|
|
|
.splitn(2, FILE_ROW_COLUMN_DELIMITER)
|
|
|
|
.map(str::trim)
|
|
|
|
.fuse();
|
|
|
|
let row = components.next().and_then(|row| row.parse::<u32>().ok())?;
|
|
|
|
let column = components.next().and_then(|col| col.parse::<u32>().ok());
|
|
|
|
Some(Point::new(
|
|
|
|
row.saturating_sub(1),
|
|
|
|
column.unwrap_or(0).saturating_sub(1),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-11-08 20:49:09 +00:00
|
|
|
fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
|
2023-11-27 05:27:33 +00:00
|
|
|
cx.emit(DismissEvent::Dismiss);
|
2023-11-08 20:49:09 +00:00
|
|
|
}
|
2023-11-07 22:27:08 +00:00
|
|
|
|
2023-11-09 00:09:38 +00:00
|
|
|
fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
|
|
|
|
if let Some(point) = self.point_from_query(cx) {
|
2023-11-09 15:25:21 +00:00
|
|
|
self.active_editor.update(cx, |editor, cx| {
|
|
|
|
let snapshot = editor.snapshot(cx).display_snapshot;
|
2023-11-09 00:09:38 +00:00
|
|
|
let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
|
2023-11-09 15:25:21 +00:00
|
|
|
editor.change_selections(Some(Autoscroll::center()), cx, |s| {
|
2023-11-09 00:09:38 +00:00
|
|
|
s.select_ranges([point..point])
|
|
|
|
});
|
2023-11-09 15:25:21 +00:00
|
|
|
editor.focus(cx);
|
|
|
|
cx.notify();
|
2023-11-09 00:09:38 +00:00
|
|
|
});
|
|
|
|
self.prev_scroll_position.take();
|
|
|
|
}
|
2023-11-07 22:27:08 +00:00
|
|
|
|
2023-11-27 05:27:33 +00:00
|
|
|
cx.emit(DismissEvent::Dismiss);
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:46:01 +00:00
|
|
|
impl Render for GoToLine {
|
|
|
|
type Element = Div;
|
2023-11-06 22:35:49 +00:00
|
|
|
|
2023-11-07 20:23:08 +00:00
|
|
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
2023-11-14 04:42:04 +00:00
|
|
|
div()
|
|
|
|
.elevation_2(cx)
|
2023-11-14 08:15:48 +00:00
|
|
|
.key_context("GoToLine")
|
2023-11-20 22:46:01 +00:00
|
|
|
.on_action(cx.listener(Self::cancel))
|
|
|
|
.on_action(cx.listener(Self::confirm))
|
2023-11-08 23:23:05 +00:00
|
|
|
.w_96()
|
2023-11-08 20:49:09 +00:00
|
|
|
.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()
|
2023-11-21 06:05:29 +00:00
|
|
|
.child(Label::new(self.current_text.clone()).color(Color::Muted)),
|
2023-11-08 20:49:09 +00:00
|
|
|
),
|
|
|
|
)
|
2023-11-06 22:35:49 +00:00
|
|
|
}
|
|
|
|
}
|