use crate::{Autoscroll, Editor, Event, NavigationData, ToOffset, ToPoint as _}; use anyhow::{anyhow, Result}; use gpui::{ elements::*, AppContext, Entity, ModelHandle, MutableAppContext, RenderContext, Subscription, Task, View, ViewContext, ViewHandle, }; use language::{Bias, Buffer, Diagnostic, File as _}; use project::{File, Project, ProjectEntryId, ProjectPath}; use rpc::proto::{self, update_followers::update_view}; use std::{fmt::Write, path::PathBuf}; use text::{Point, Selection}; use util::ResultExt; use workspace::{ FollowableItem, Item, ItemHandle, ItemNavHistory, ProjectItem, Settings, StatusItemView, }; impl FollowableItem for Editor { fn for_state_message( pane: ViewHandle, project: ModelHandle, state: &mut Option, cx: &mut MutableAppContext, ) -> Option>>> { let state = if matches!(state, Some(proto::view::Variant::Editor(_))) { if let Some(proto::view::Variant::Editor(state)) = state.take() { state } else { unreachable!() } } else { return None; }; let buffer = project.update(cx, |project, cx| { project.open_buffer_by_id(state.buffer_id, cx) }); Some(cx.spawn(|mut cx| async move { let buffer = buffer.await?; Ok(pane .read_with(&cx, |pane, cx| { pane.items_of_type::().find(|editor| { editor.read(cx).buffer.read(cx).as_singleton().as_ref() == Some(&buffer) }) }) .unwrap_or_else(|| { cx.add_view(pane.window_id(), |cx| { Editor::for_buffer(buffer, Some(project), cx) }) })) })) } fn to_state_message(&self, cx: &AppContext) -> proto::view::Variant { let buffer_id = self .buffer .read(cx) .as_singleton() .unwrap() .read(cx) .remote_id(); proto::view::Variant::Editor(proto::view::Editor { buffer_id, scroll_top: self .scroll_top_anchor .as_ref() .map(|anchor| language::proto::serialize_anchor(&anchor.text_anchor)), }) } fn to_update_message( &self, event: &Self::Event, cx: &AppContext, ) -> Option { match event { Event::ScrollPositionChanged => { Some(update_view::Variant::Editor(update_view::Editor { scroll_top: self .scroll_top_anchor .as_ref() .map(|anchor| language::proto::serialize_anchor(&anchor.text_anchor)), })) } _ => None, } } fn apply_update_message( &mut self, message: update_view::Variant, cx: &mut ViewContext, ) -> Result<()> { match message { update_view::Variant::Editor(message) => { if let Some(anchor) = message.scroll_top { let anchor = language::proto::deserialize_anchor(anchor) .ok_or_else(|| anyhow!("invalid scroll top"))?; let anchor = { let buffer = self.buffer.read(cx); let buffer = buffer.read(cx); let (excerpt_id, _, _) = buffer.as_singleton().unwrap(); buffer.anchor_in_excerpt(excerpt_id.clone(), anchor) }; self.set_scroll_top_anchor(anchor, cx); } } } Ok(()) } } impl Item for Editor { fn navigate(&mut self, data: Box, cx: &mut ViewContext) { if let Some(data) = data.downcast_ref::() { let buffer = self.buffer.read(cx).read(cx); let offset = if buffer.can_resolve(&data.anchor) { data.anchor.to_offset(&buffer) } else { buffer.clip_offset(data.offset, Bias::Left) }; drop(buffer); let nav_history = self.nav_history.take(); self.select_ranges([offset..offset], Some(Autoscroll::Fit), cx); self.nav_history = nav_history; } } fn tab_content(&self, style: &theme::Tab, cx: &AppContext) -> ElementBox { let title = self.title(cx); Label::new(title, style.label.clone()).boxed() } fn project_path(&self, cx: &AppContext) -> Option { File::from_dyn(self.buffer().read(cx).file(cx)).map(|file| ProjectPath { worktree_id: file.worktree_id(cx), path: file.path().clone(), }) } fn project_entry_id(&self, cx: &AppContext) -> Option { File::from_dyn(self.buffer().read(cx).file(cx)).and_then(|file| file.project_entry_id(cx)) } fn clone_on_split(&self, cx: &mut ViewContext) -> Option where Self: Sized, { Some(self.clone(cx)) } fn set_nav_history(&mut self, history: ItemNavHistory, _: &mut ViewContext) { self.nav_history = Some(history); } fn deactivated(&mut self, cx: &mut ViewContext) { let selection = self.newest_anchor_selection(); self.push_to_nav_history(selection.head(), None, cx); } fn is_dirty(&self, cx: &AppContext) -> bool { self.buffer().read(cx).read(cx).is_dirty() } fn has_conflict(&self, cx: &AppContext) -> bool { self.buffer().read(cx).read(cx).has_conflict() } fn can_save(&self, cx: &AppContext) -> bool { !self.buffer().read(cx).is_singleton() || self.project_path(cx).is_some() } fn save( &mut self, project: ModelHandle, cx: &mut ViewContext, ) -> Task> { let buffer = self.buffer().clone(); let buffers = buffer.read(cx).all_buffers(); let transaction = project.update(cx, |project, cx| project.format(buffers, true, cx)); cx.spawn(|this, mut cx| async move { let transaction = transaction.await.log_err(); this.update(&mut cx, |editor, cx| { editor.request_autoscroll(Autoscroll::Fit, cx) }); buffer .update(&mut cx, |buffer, cx| { if let Some(transaction) = transaction { if !buffer.is_singleton() { buffer.push_transaction(&transaction.0); } } buffer.save(cx) }) .await?; Ok(()) }) } fn can_save_as(&self, cx: &AppContext) -> bool { self.buffer().read(cx).is_singleton() } fn save_as( &mut self, project: ModelHandle, abs_path: PathBuf, cx: &mut ViewContext, ) -> Task> { let buffer = self .buffer() .read(cx) .as_singleton() .expect("cannot call save_as on an excerpt list") .clone(); project.update(cx, |project, cx| { project.save_buffer_as(buffer, abs_path, cx) }) } fn should_activate_item_on_event(event: &Event) -> bool { matches!(event, Event::Activate) } fn should_close_item_on_event(event: &Event) -> bool { matches!(event, Event::Closed) } fn should_update_tab_on_event(event: &Event) -> bool { matches!(event, Event::Saved | Event::Dirtied | Event::TitleChanged) } } impl ProjectItem for Editor { type Item = Buffer; fn for_project_item( project: ModelHandle, buffer: ModelHandle, cx: &mut ViewContext, ) -> Self { Self::for_buffer(buffer, Some(project), cx) } } pub struct CursorPosition { position: Option, selected_count: usize, _observe_active_editor: Option, } impl CursorPosition { pub fn new() -> Self { Self { position: None, selected_count: 0, _observe_active_editor: None, } } fn update_position(&mut self, editor: ViewHandle, cx: &mut ViewContext) { let editor = editor.read(cx); let buffer = editor.buffer().read(cx).snapshot(cx); self.selected_count = 0; let mut last_selection: Option> = None; for selection in editor.local_selections::(cx) { self.selected_count += selection.end - selection.start; if last_selection .as_ref() .map_or(true, |last_selection| selection.id > last_selection.id) { last_selection = Some(selection); } } self.position = last_selection.map(|s| s.head().to_point(&buffer)); cx.notify(); } } impl Entity for CursorPosition { type Event = (); } impl View for CursorPosition { fn ui_name() -> &'static str { "CursorPosition" } fn render(&mut self, cx: &mut RenderContext) -> ElementBox { if let Some(position) = self.position { let theme = &cx.global::().theme.workspace.status_bar; let mut text = format!("{},{}", position.row + 1, position.column + 1); if self.selected_count > 0 { write!(text, " ({} selected)", self.selected_count).unwrap(); } Label::new(text, theme.cursor_position.clone()).boxed() } else { Empty::new().boxed() } } } impl StatusItemView for CursorPosition { fn set_active_pane_item( &mut self, active_pane_item: Option<&dyn ItemHandle>, cx: &mut ViewContext, ) { if let Some(editor) = active_pane_item.and_then(|item| item.downcast::()) { self._observe_active_editor = Some(cx.observe(&editor, Self::update_position)); self.update_position(editor, cx); } else { self.position = None; self._observe_active_editor = None; } cx.notify(); } } pub struct DiagnosticMessage { diagnostic: Option, _observe_active_editor: Option, } impl DiagnosticMessage { pub fn new() -> Self { Self { diagnostic: None, _observe_active_editor: None, } } fn update(&mut self, editor: ViewHandle, cx: &mut ViewContext) { let editor = editor.read(cx); let buffer = editor.buffer().read(cx); let cursor_position = editor .newest_selection_with_snapshot::(&buffer.read(cx)) .head(); let new_diagnostic = buffer .read(cx) .diagnostics_in_range::<_, usize>(cursor_position..cursor_position, false) .filter(|entry| !entry.range.is_empty()) .min_by_key(|entry| (entry.diagnostic.severity, entry.range.len())) .map(|entry| entry.diagnostic); if new_diagnostic != self.diagnostic { self.diagnostic = new_diagnostic; cx.notify(); } } } impl Entity for DiagnosticMessage { type Event = (); } impl View for DiagnosticMessage { fn ui_name() -> &'static str { "DiagnosticMessage" } fn render(&mut self, cx: &mut RenderContext) -> ElementBox { if let Some(diagnostic) = &self.diagnostic { let theme = &cx.global::().theme.workspace.status_bar; Label::new( diagnostic.message.split('\n').next().unwrap().to_string(), theme.diagnostic_message.clone(), ) .boxed() } else { Empty::new().boxed() } } } impl StatusItemView for DiagnosticMessage { fn set_active_pane_item( &mut self, active_pane_item: Option<&dyn ItemHandle>, cx: &mut ViewContext, ) { if let Some(editor) = active_pane_item.and_then(|item| item.downcast::()) { self._observe_active_editor = Some(cx.observe(&editor, Self::update)); self.update(editor, cx); } else { self.diagnostic = Default::default(); self._observe_active_editor = None; } cx.notify(); } }