cursor_position.rs

  1use editor::{Editor, ToPoint};
  2use gpui::{Subscription, View, WeakView};
  3use std::fmt::Write;
  4use text::{Point, Selection};
  5use ui::{
  6    div, Button, ButtonCommon, Clickable, FluentBuilder, IntoElement, LabelSize, ParentElement,
  7    Render, Tooltip, ViewContext,
  8};
  9use util::paths::FILE_ROW_COLUMN_DELIMITER;
 10use workspace::{item::ItemHandle, StatusItemView, Workspace};
 11
 12pub struct CursorPosition {
 13    position: Option<Point>,
 14    selected_count: usize,
 15    workspace: WeakView<Workspace>,
 16    _observe_active_editor: Option<Subscription>,
 17}
 18
 19impl CursorPosition {
 20    pub fn new(workspace: &Workspace) -> Self {
 21        Self {
 22            position: None,
 23            selected_count: 0,
 24            workspace: workspace.weak_handle(),
 25            _observe_active_editor: None,
 26        }
 27    }
 28
 29    fn update_position(&mut self, editor: View<Editor>, cx: &mut ViewContext<Self>) {
 30        let editor = editor.read(cx);
 31        let buffer = editor.buffer().read(cx).snapshot(cx);
 32
 33        self.selected_count = 0;
 34        let mut last_selection: Option<Selection<usize>> = None;
 35        for selection in editor.selections.all::<usize>(cx) {
 36            self.selected_count += selection.end - selection.start;
 37            if last_selection
 38                .as_ref()
 39                .map_or(true, |last_selection| selection.id > last_selection.id)
 40            {
 41                last_selection = Some(selection);
 42            }
 43        }
 44        self.position = last_selection.map(|s| s.head().to_point(&buffer));
 45
 46        cx.notify();
 47    }
 48}
 49
 50impl Render for CursorPosition {
 51    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
 52        div().when_some(self.position, |el, position| {
 53            let mut text = format!(
 54                "{}{FILE_ROW_COLUMN_DELIMITER}{}",
 55                position.row + 1,
 56                position.column + 1
 57            );
 58            if self.selected_count > 0 {
 59                write!(text, " ({} selected)", self.selected_count).unwrap();
 60            }
 61
 62            el.child(
 63                Button::new("go-to-line-column", text)
 64                    .label_size(LabelSize::Small)
 65                    .on_click(cx.listener(|this, _, cx| {
 66                        if let Some(workspace) = this.workspace.upgrade() {
 67                            workspace.update(cx, |workspace, cx| {
 68                                if let Some(editor) = workspace
 69                                    .active_item(cx)
 70                                    .and_then(|item| item.act_as::<Editor>(cx))
 71                                {
 72                                    workspace
 73                                        .toggle_modal(cx, |cx| crate::GoToLine::new(editor, cx))
 74                                }
 75                            });
 76                        }
 77                    }))
 78                    .tooltip(|cx| Tooltip::for_action("Go to Line/Column", &crate::Toggle, cx)),
 79            )
 80        })
 81    }
 82}
 83
 84impl StatusItemView for CursorPosition {
 85    fn set_active_pane_item(
 86        &mut self,
 87        active_pane_item: Option<&dyn ItemHandle>,
 88        cx: &mut ViewContext<Self>,
 89    ) {
 90        if let Some(editor) = active_pane_item.and_then(|item| item.act_as::<Editor>(cx)) {
 91            self._observe_active_editor = Some(cx.observe(&editor, Self::update_position));
 92            self.update_position(editor, cx);
 93        } else {
 94            self.position = None;
 95            self._observe_active_editor = None;
 96        }
 97
 98        cx.notify();
 99    }
100}