items.rs

  1use crate::{
  2    display_map::ToDisplayPoint, link_go_to_definition::hide_link_definition,
  3    movement::surrounding_word, Anchor, Autoscroll, Editor, Event, ExcerptId, MultiBuffer,
  4    MultiBufferSnapshot, NavigationData, ToPoint as _,
  5};
  6use anyhow::{anyhow, Result};
  7use futures::FutureExt;
  8use gpui::{
  9    elements::*, geometry::vector::vec2f, AppContext, Entity, ModelHandle, MutableAppContext,
 10    RenderContext, Subscription, Task, View, ViewContext, ViewHandle,
 11};
 12use language::{Bias, Buffer, File as _, OffsetRangeExt, SelectionGoal};
 13use project::{File, Project, ProjectEntryId, ProjectPath};
 14use rpc::proto::{self, update_view};
 15use settings::Settings;
 16use smallvec::SmallVec;
 17use std::{
 18    any::Any,
 19    borrow::Cow,
 20    cmp::{self, Ordering},
 21    fmt::Write,
 22    ops::Range,
 23    path::{Path, PathBuf},
 24    time::Duration,
 25};
 26use text::{Point, Selection};
 27use util::TryFutureExt;
 28use workspace::{
 29    searchable::{Direction, SearchEvent, SearchableItem, SearchableItemHandle},
 30    FollowableItem, Item, ItemHandle, ItemNavHistory, ProjectItem, StatusItemView,
 31};
 32
 33pub const FORMAT_TIMEOUT: Duration = Duration::from_secs(2);
 34pub const MAX_TAB_TITLE_LEN: usize = 24;
 35
 36impl FollowableItem for Editor {
 37    fn from_state_proto(
 38        pane: ViewHandle<workspace::Pane>,
 39        project: ModelHandle<Project>,
 40        state: &mut Option<proto::view::Variant>,
 41        cx: &mut MutableAppContext,
 42    ) -> Option<Task<Result<ViewHandle<Self>>>> {
 43        let state = if matches!(state, Some(proto::view::Variant::Editor(_))) {
 44            if let Some(proto::view::Variant::Editor(state)) = state.take() {
 45                state
 46            } else {
 47                unreachable!()
 48            }
 49        } else {
 50            return None;
 51        };
 52
 53        let buffer = project.update(cx, |project, cx| {
 54            project.open_buffer_by_id(state.buffer_id, cx)
 55        });
 56        Some(cx.spawn(|mut cx| async move {
 57            let buffer = buffer.await?;
 58            let editor = pane
 59                .read_with(&cx, |pane, cx| {
 60                    pane.items_of_type::<Self>().find(|editor| {
 61                        editor.read(cx).buffer.read(cx).as_singleton().as_ref() == Some(&buffer)
 62                    })
 63                })
 64                .unwrap_or_else(|| {
 65                    pane.update(&mut cx, |_, cx| {
 66                        cx.add_view(|cx| Editor::for_buffer(buffer, Some(project), cx))
 67                    })
 68                });
 69            editor.update(&mut cx, |editor, cx| {
 70                let excerpt_id;
 71                let buffer_id;
 72                {
 73                    let buffer = editor.buffer.read(cx).read(cx);
 74                    let singleton = buffer.as_singleton().unwrap();
 75                    excerpt_id = singleton.0.clone();
 76                    buffer_id = singleton.1;
 77                }
 78                let selections = state
 79                    .selections
 80                    .into_iter()
 81                    .map(|selection| {
 82                        deserialize_selection(&excerpt_id, buffer_id, selection)
 83                            .ok_or_else(|| anyhow!("invalid selection"))
 84                    })
 85                    .collect::<Result<Vec<_>>>()?;
 86                if !selections.is_empty() {
 87                    editor.set_selections_from_remote(selections, cx);
 88                }
 89
 90                if let Some(anchor) = state.scroll_top_anchor {
 91                    editor.set_scroll_top_anchor(
 92                        Anchor {
 93                            buffer_id: Some(state.buffer_id as usize),
 94                            excerpt_id,
 95                            text_anchor: language::proto::deserialize_anchor(anchor)
 96                                .ok_or_else(|| anyhow!("invalid scroll top"))?,
 97                        },
 98                        vec2f(state.scroll_x, state.scroll_y),
 99                        cx,
100                    );
101                }
102
103                Ok::<_, anyhow::Error>(())
104            })?;
105            Ok(editor)
106        }))
107    }
108
109    fn set_leader_replica_id(
110        &mut self,
111        leader_replica_id: Option<u16>,
112        cx: &mut ViewContext<Self>,
113    ) {
114        self.leader_replica_id = leader_replica_id;
115        if self.leader_replica_id.is_some() {
116            self.buffer.update(cx, |buffer, cx| {
117                buffer.remove_active_selections(cx);
118            });
119        } else {
120            self.buffer.update(cx, |buffer, cx| {
121                if self.focused {
122                    buffer.set_active_selections(
123                        &self.selections.disjoint_anchors(),
124                        self.selections.line_mode,
125                        cx,
126                    );
127                }
128            });
129        }
130        cx.notify();
131    }
132
133    fn to_state_proto(&self, cx: &AppContext) -> Option<proto::view::Variant> {
134        let buffer_id = self.buffer.read(cx).as_singleton()?.read(cx).remote_id();
135        Some(proto::view::Variant::Editor(proto::view::Editor {
136            buffer_id,
137            scroll_top_anchor: Some(language::proto::serialize_anchor(
138                &self.scroll_top_anchor.text_anchor,
139            )),
140            scroll_x: self.scroll_position.x(),
141            scroll_y: self.scroll_position.y(),
142            selections: self
143                .selections
144                .disjoint_anchors()
145                .iter()
146                .map(serialize_selection)
147                .collect(),
148        }))
149    }
150
151    fn add_event_to_update_proto(
152        &self,
153        event: &Self::Event,
154        update: &mut Option<proto::update_view::Variant>,
155        _: &AppContext,
156    ) -> bool {
157        let update =
158            update.get_or_insert_with(|| proto::update_view::Variant::Editor(Default::default()));
159
160        match update {
161            proto::update_view::Variant::Editor(update) => match event {
162                Event::ScrollPositionChanged { .. } => {
163                    update.scroll_top_anchor = Some(language::proto::serialize_anchor(
164                        &self.scroll_top_anchor.text_anchor,
165                    ));
166                    update.scroll_x = self.scroll_position.x();
167                    update.scroll_y = self.scroll_position.y();
168                    true
169                }
170                Event::SelectionsChanged { .. } => {
171                    update.selections = self
172                        .selections
173                        .disjoint_anchors()
174                        .iter()
175                        .chain(self.selections.pending_anchor().as_ref())
176                        .map(serialize_selection)
177                        .collect();
178                    true
179                }
180                _ => false,
181            },
182        }
183    }
184
185    fn apply_update_proto(
186        &mut self,
187        message: update_view::Variant,
188        cx: &mut ViewContext<Self>,
189    ) -> Result<()> {
190        match message {
191            update_view::Variant::Editor(message) => {
192                let buffer = self.buffer.read(cx);
193                let buffer = buffer.read(cx);
194                let (excerpt_id, buffer_id, _) = buffer.as_singleton().unwrap();
195                let excerpt_id = excerpt_id.clone();
196                drop(buffer);
197
198                let selections = message
199                    .selections
200                    .into_iter()
201                    .filter_map(|selection| {
202                        deserialize_selection(&excerpt_id, buffer_id, selection)
203                    })
204                    .collect::<Vec<_>>();
205
206                if !selections.is_empty() {
207                    self.set_selections_from_remote(selections, cx);
208                    self.request_autoscroll_remotely(Autoscroll::Newest, cx);
209                } else if let Some(anchor) = message.scroll_top_anchor {
210                    self.set_scroll_top_anchor(
211                        Anchor {
212                            buffer_id: Some(buffer_id),
213                            excerpt_id,
214                            text_anchor: language::proto::deserialize_anchor(anchor)
215                                .ok_or_else(|| anyhow!("invalid scroll top"))?,
216                        },
217                        vec2f(message.scroll_x, message.scroll_y),
218                        cx,
219                    );
220                }
221            }
222        }
223        Ok(())
224    }
225
226    fn should_unfollow_on_event(event: &Self::Event, _: &AppContext) -> bool {
227        match event {
228            Event::Edited => true,
229            Event::SelectionsChanged { local } => *local,
230            Event::ScrollPositionChanged { local } => *local,
231            _ => false,
232        }
233    }
234}
235
236fn serialize_selection(selection: &Selection<Anchor>) -> proto::Selection {
237    proto::Selection {
238        id: selection.id as u64,
239        start: Some(language::proto::serialize_anchor(
240            &selection.start.text_anchor,
241        )),
242        end: Some(language::proto::serialize_anchor(
243            &selection.end.text_anchor,
244        )),
245        reversed: selection.reversed,
246    }
247}
248
249fn deserialize_selection(
250    excerpt_id: &ExcerptId,
251    buffer_id: usize,
252    selection: proto::Selection,
253) -> Option<Selection<Anchor>> {
254    Some(Selection {
255        id: selection.id as usize,
256        start: Anchor {
257            buffer_id: Some(buffer_id),
258            excerpt_id: excerpt_id.clone(),
259            text_anchor: language::proto::deserialize_anchor(selection.start?)?,
260        },
261        end: Anchor {
262            buffer_id: Some(buffer_id),
263            excerpt_id: excerpt_id.clone(),
264            text_anchor: language::proto::deserialize_anchor(selection.end?)?,
265        },
266        reversed: selection.reversed,
267        goal: SelectionGoal::None,
268    })
269}
270
271impl Item for Editor {
272    fn navigate(&mut self, data: Box<dyn std::any::Any>, cx: &mut ViewContext<Self>) -> bool {
273        if let Ok(data) = data.downcast::<NavigationData>() {
274            let newest_selection = self.selections.newest::<Point>(cx);
275            let buffer = self.buffer.read(cx).read(cx);
276            let offset = if buffer.can_resolve(&data.cursor_anchor) {
277                data.cursor_anchor.to_point(&buffer)
278            } else {
279                buffer.clip_point(data.cursor_position, Bias::Left)
280            };
281
282            let scroll_top_anchor = if buffer.can_resolve(&data.scroll_top_anchor) {
283                data.scroll_top_anchor
284            } else {
285                buffer.anchor_before(
286                    buffer.clip_point(Point::new(data.scroll_top_row, 0), Bias::Left),
287                )
288            };
289
290            drop(buffer);
291
292            if newest_selection.head() == offset {
293                false
294            } else {
295                let nav_history = self.nav_history.take();
296                self.scroll_position = data.scroll_position;
297                self.scroll_top_anchor = scroll_top_anchor;
298                self.change_selections(Some(Autoscroll::Fit), cx, |s| {
299                    s.select_ranges([offset..offset])
300                });
301                self.nav_history = nav_history;
302                true
303            }
304        } else {
305            false
306        }
307    }
308
309    fn tab_description<'a>(&'a self, detail: usize, cx: &'a AppContext) -> Option<Cow<'a, str>> {
310        match path_for_buffer(&self.buffer, detail, true, cx)? {
311            Cow::Borrowed(path) => Some(path.to_string_lossy()),
312            Cow::Owned(path) => Some(path.to_string_lossy().to_string().into()),
313        }
314    }
315
316    fn tab_content(
317        &self,
318        detail: Option<usize>,
319        style: &theme::Tab,
320        cx: &AppContext,
321    ) -> ElementBox {
322        Flex::row()
323            .with_child(
324                Label::new(self.title(cx).into(), style.label.clone())
325                    .aligned()
326                    .boxed(),
327            )
328            .with_children(detail.and_then(|detail| {
329                let path = path_for_buffer(&self.buffer, detail, false, cx)?;
330                let description = path.to_string_lossy();
331                Some(
332                    Label::new(
333                        if description.len() > MAX_TAB_TITLE_LEN {
334                            description[..MAX_TAB_TITLE_LEN].to_string() + ""
335                        } else {
336                            description.into()
337                        },
338                        style.description.text.clone(),
339                    )
340                    .contained()
341                    .with_style(style.description.container)
342                    .aligned()
343                    .boxed(),
344                )
345            }))
346            .boxed()
347    }
348
349    fn project_path(&self, cx: &AppContext) -> Option<ProjectPath> {
350        let buffer = self.buffer.read(cx).as_singleton()?;
351        let file = buffer.read(cx).file();
352        File::from_dyn(file).map(|file| ProjectPath {
353            worktree_id: file.worktree_id(cx),
354            path: file.path().clone(),
355        })
356    }
357
358    fn project_entry_ids(&self, cx: &AppContext) -> SmallVec<[ProjectEntryId; 3]> {
359        self.buffer
360            .read(cx)
361            .files(cx)
362            .into_iter()
363            .filter_map(|file| File::from_dyn(Some(file))?.project_entry_id(cx))
364            .collect()
365    }
366
367    fn is_singleton(&self, cx: &AppContext) -> bool {
368        self.buffer.read(cx).is_singleton()
369    }
370
371    fn clone_on_split(&self, cx: &mut ViewContext<Self>) -> Option<Self>
372    where
373        Self: Sized,
374    {
375        Some(self.clone(cx))
376    }
377
378    fn set_nav_history(&mut self, history: ItemNavHistory, _: &mut ViewContext<Self>) {
379        self.nav_history = Some(history);
380    }
381
382    fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
383        let selection = self.selections.newest_anchor();
384        self.push_to_nav_history(selection.head(), None, cx);
385    }
386
387    fn workspace_deactivated(&mut self, cx: &mut ViewContext<Self>) {
388        hide_link_definition(self, cx);
389        self.link_go_to_definition_state.last_mouse_location = None;
390    }
391
392    fn is_dirty(&self, cx: &AppContext) -> bool {
393        self.buffer().read(cx).read(cx).is_dirty()
394    }
395
396    fn has_conflict(&self, cx: &AppContext) -> bool {
397        self.buffer().read(cx).read(cx).has_conflict()
398    }
399
400    fn can_save(&self, cx: &AppContext) -> bool {
401        !self.buffer().read(cx).is_singleton() || self.project_path(cx).is_some()
402    }
403
404    fn save(
405        &mut self,
406        project: ModelHandle<Project>,
407        cx: &mut ViewContext<Self>,
408    ) -> Task<Result<()>> {
409        let buffer = self.buffer().clone();
410        let buffers = buffer.read(cx).all_buffers();
411        let mut timeout = cx.background().timer(FORMAT_TIMEOUT).fuse();
412        let format = project.update(cx, |project, cx| project.format(buffers, true, cx));
413        cx.spawn(|_, mut cx| async move {
414            let transaction = futures::select_biased! {
415                _ = timeout => {
416                    log::warn!("timed out waiting for formatting");
417                    None
418                }
419                transaction = format.log_err().fuse() => transaction,
420            };
421
422            buffer
423                .update(&mut cx, |buffer, cx| {
424                    if let Some(transaction) = transaction {
425                        if !buffer.is_singleton() {
426                            buffer.push_transaction(&transaction.0);
427                        }
428                    }
429
430                    buffer.save(cx)
431                })
432                .await?;
433            Ok(())
434        })
435    }
436
437    fn save_as(
438        &mut self,
439        project: ModelHandle<Project>,
440        abs_path: PathBuf,
441        cx: &mut ViewContext<Self>,
442    ) -> Task<Result<()>> {
443        let buffer = self
444            .buffer()
445            .read(cx)
446            .as_singleton()
447            .expect("cannot call save_as on an excerpt list");
448
449        project.update(cx, |project, cx| {
450            project.save_buffer_as(buffer, abs_path, cx)
451        })
452    }
453
454    fn reload(
455        &mut self,
456        project: ModelHandle<Project>,
457        cx: &mut ViewContext<Self>,
458    ) -> Task<Result<()>> {
459        let buffer = self.buffer().clone();
460        let buffers = self.buffer.read(cx).all_buffers();
461        let reload_buffers =
462            project.update(cx, |project, cx| project.reload_buffers(buffers, true, cx));
463        cx.spawn(|this, mut cx| async move {
464            let transaction = reload_buffers.log_err().await;
465            this.update(&mut cx, |editor, cx| {
466                editor.request_autoscroll(Autoscroll::Fit, cx)
467            });
468            buffer.update(&mut cx, |buffer, _| {
469                if let Some(transaction) = transaction {
470                    if !buffer.is_singleton() {
471                        buffer.push_transaction(&transaction.0);
472                    }
473                }
474            });
475            Ok(())
476        })
477    }
478
479    fn should_close_item_on_event(event: &Event) -> bool {
480        matches!(event, Event::Closed)
481    }
482
483    fn should_update_tab_on_event(event: &Event) -> bool {
484        matches!(
485            event,
486            Event::Saved | Event::DirtyChanged | Event::TitleChanged
487        )
488    }
489
490    fn is_edit_event(event: &Self::Event) -> bool {
491        matches!(event, Event::BufferEdited)
492    }
493
494    fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
495        Some(Box::new(handle.clone()))
496    }
497}
498
499impl ProjectItem for Editor {
500    type Item = Buffer;
501
502    fn for_project_item(
503        project: ModelHandle<Project>,
504        buffer: ModelHandle<Buffer>,
505        cx: &mut ViewContext<Self>,
506    ) -> Self {
507        Self::for_buffer(buffer, Some(project), cx)
508    }
509}
510
511enum BufferSearchHighlights {}
512impl SearchableItem for Editor {
513    fn to_search_event(event: &Self::Event) -> Option<SearchEvent> {
514        match event {
515            Event::BufferEdited => Some(SearchEvent::ContentsUpdated),
516            Event::SelectionsChanged { .. } => Some(SearchEvent::SelectionsChanged),
517            _ => None,
518        }
519    }
520
521    fn clear_highlights(&mut self, cx: &mut ViewContext<Self>) {
522        self.clear_background_highlights::<BufferSearchHighlights>(cx);
523    }
524
525    fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String {
526        let display_map = self.snapshot(cx).display_snapshot;
527        let selection = self.selections.newest::<usize>(cx);
528        if selection.start == selection.end {
529            let point = selection.start.to_display_point(&display_map);
530            let range = surrounding_word(&display_map, point);
531            let range = range.start.to_offset(&display_map, Bias::Left)
532                ..range.end.to_offset(&display_map, Bias::Right);
533            let text: String = display_map.buffer_snapshot.text_for_range(range).collect();
534            if text.trim().is_empty() {
535                String::new()
536            } else {
537                text
538            }
539        } else {
540            display_map
541                .buffer_snapshot
542                .text_for_range(selection.start..selection.end)
543                .collect()
544        }
545    }
546
547    fn select_next_match_in_direction(
548        &mut self,
549        index: usize,
550        direction: Direction,
551        matches: &Vec<Box<dyn Any + Send>>,
552        cx: &mut ViewContext<Self>,
553    ) {
554        if let Some(matches) = matches
555            .iter()
556            .map(|range| range.downcast_ref::<Range<Anchor>>().cloned())
557            .collect::<Option<Vec<_>>>()
558        {
559            let new_index: usize = match_index_for_direction(
560                matches.as_slice(),
561                &self.selections.newest_anchor().head(),
562                index,
563                direction,
564                &self.buffer().read(cx).snapshot(cx),
565            );
566
567            let range_to_select = matches[new_index].clone();
568            self.unfold_ranges([range_to_select.clone()], false, cx);
569            self.change_selections(Some(Autoscroll::Fit), cx, |s| {
570                s.select_ranges([range_to_select])
571            });
572        } else {
573            log::error!("Select next match in direction called with unexpected type matches");
574        }
575    }
576
577    fn select_match_by_index(
578        &mut self,
579        index: usize,
580        matches: &Vec<Box<dyn Any + Send>>,
581        cx: &mut ViewContext<Self>,
582    ) {
583        if let Some(matches) = matches
584            .iter()
585            .map(|range| range.downcast_ref::<Range<Anchor>>().cloned())
586            .collect::<Option<Vec<_>>>()
587        {
588            self.change_selections(Some(Autoscroll::Fit), cx, |s| {
589                s.select_ranges([matches[index].clone()])
590            });
591            self.highlight_background::<BufferSearchHighlights>(
592                matches,
593                |theme| theme.search.match_background,
594                cx,
595            );
596        } else {
597            log::error!("Select next match in direction called with unexpected type matches");
598        }
599    }
600
601    fn matches(
602        &mut self,
603        query: project::search::SearchQuery,
604        cx: &mut ViewContext<Self>,
605    ) -> Task<Vec<Box<dyn Any + Send>>> {
606        let buffer = self.buffer().read(cx).snapshot(cx);
607        cx.background().spawn(async move {
608            let mut ranges = Vec::new();
609            if let Some((_, _, excerpt_buffer)) = buffer.as_singleton() {
610                ranges.extend(
611                    query
612                        .search(excerpt_buffer.as_rope())
613                        .await
614                        .into_iter()
615                        .map(|range| {
616                            buffer.anchor_after(range.start)..buffer.anchor_before(range.end)
617                        }),
618                );
619            } else {
620                for excerpt in buffer.excerpt_boundaries_in_range(0..buffer.len()) {
621                    let excerpt_range = excerpt.range.context.to_offset(&excerpt.buffer);
622                    let rope = excerpt.buffer.as_rope().slice(excerpt_range.clone());
623                    ranges.extend(query.search(&rope).await.into_iter().map(|range| {
624                        let start = excerpt
625                            .buffer
626                            .anchor_after(excerpt_range.start + range.start);
627                        let end = excerpt
628                            .buffer
629                            .anchor_before(excerpt_range.start + range.end);
630                        buffer.anchor_in_excerpt(excerpt.id.clone(), start)
631                            ..buffer.anchor_in_excerpt(excerpt.id.clone(), end)
632                    }));
633                }
634            }
635            ranges
636                .into_iter()
637                .map::<Box<dyn Any + Send>, _>(|range| Box::new(range))
638                .collect()
639        })
640    }
641
642    fn active_match_index(
643        &mut self,
644        matches: &Vec<Box<dyn Any + Send>>,
645        cx: &mut ViewContext<Self>,
646    ) -> Option<usize> {
647        if let Some(matches) = matches
648            .iter()
649            .map(|range| range.downcast_ref::<Range<Anchor>>().cloned())
650            .collect::<Option<Vec<_>>>()
651        {
652            active_match_index(
653                &matches,
654                &self.selections.newest_anchor().head(),
655                &self.buffer().read(cx).snapshot(cx),
656            )
657        } else {
658            None
659        }
660    }
661}
662
663pub fn match_index_for_direction(
664    ranges: &[Range<Anchor>],
665    cursor: &Anchor,
666    mut index: usize,
667    direction: Direction,
668    buffer: &MultiBufferSnapshot,
669) -> usize {
670    if ranges[index].start.cmp(cursor, buffer).is_gt() {
671        if direction == Direction::Prev {
672            if index == 0 {
673                index = ranges.len() - 1;
674            } else {
675                index -= 1;
676            }
677        }
678    } else if ranges[index].end.cmp(cursor, buffer).is_lt() {
679        if direction == Direction::Next {
680            index = 0;
681        }
682    } else if direction == Direction::Prev {
683        if index == 0 {
684            index = ranges.len() - 1;
685        } else {
686            index -= 1;
687        }
688    } else if direction == Direction::Next {
689        if index == ranges.len() - 1 {
690            index = 0
691        } else {
692            index += 1;
693        }
694    };
695    index
696}
697
698pub fn active_match_index(
699    ranges: &[Range<Anchor>],
700    cursor: &Anchor,
701    buffer: &MultiBufferSnapshot,
702) -> Option<usize> {
703    if ranges.is_empty() {
704        None
705    } else {
706        match ranges.binary_search_by(|probe| {
707            if probe.end.cmp(cursor, &*buffer).is_lt() {
708                Ordering::Less
709            } else if probe.start.cmp(cursor, &*buffer).is_gt() {
710                Ordering::Greater
711            } else {
712                Ordering::Equal
713            }
714        }) {
715            Ok(i) | Err(i) => Some(cmp::min(i, ranges.len() - 1)),
716        }
717    }
718}
719
720pub struct CursorPosition {
721    position: Option<Point>,
722    selected_count: usize,
723    _observe_active_editor: Option<Subscription>,
724}
725
726impl Default for CursorPosition {
727    fn default() -> Self {
728        Self::new()
729    }
730}
731
732impl CursorPosition {
733    pub fn new() -> Self {
734        Self {
735            position: None,
736            selected_count: 0,
737            _observe_active_editor: None,
738        }
739    }
740
741    fn update_position(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
742        let editor = editor.read(cx);
743        let buffer = editor.buffer().read(cx).snapshot(cx);
744
745        self.selected_count = 0;
746        let mut last_selection: Option<Selection<usize>> = None;
747        for selection in editor.selections.all::<usize>(cx) {
748            self.selected_count += selection.end - selection.start;
749            if last_selection
750                .as_ref()
751                .map_or(true, |last_selection| selection.id > last_selection.id)
752            {
753                last_selection = Some(selection);
754            }
755        }
756        self.position = last_selection.map(|s| s.head().to_point(&buffer));
757
758        cx.notify();
759    }
760}
761
762impl Entity for CursorPosition {
763    type Event = ();
764}
765
766impl View for CursorPosition {
767    fn ui_name() -> &'static str {
768        "CursorPosition"
769    }
770
771    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
772        if let Some(position) = self.position {
773            let theme = &cx.global::<Settings>().theme.workspace.status_bar;
774            let mut text = format!("{},{}", position.row + 1, position.column + 1);
775            if self.selected_count > 0 {
776                write!(text, " ({} selected)", self.selected_count).unwrap();
777            }
778            Label::new(text, theme.cursor_position.clone()).boxed()
779        } else {
780            Empty::new().boxed()
781        }
782    }
783}
784
785impl StatusItemView for CursorPosition {
786    fn set_active_pane_item(
787        &mut self,
788        active_pane_item: Option<&dyn ItemHandle>,
789        cx: &mut ViewContext<Self>,
790    ) {
791        if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
792            self._observe_active_editor = Some(cx.observe(&editor, Self::update_position));
793            self.update_position(editor, cx);
794        } else {
795            self.position = None;
796            self._observe_active_editor = None;
797        }
798
799        cx.notify();
800    }
801}
802
803fn path_for_buffer<'a>(
804    buffer: &ModelHandle<MultiBuffer>,
805    mut height: usize,
806    include_filename: bool,
807    cx: &'a AppContext,
808) -> Option<Cow<'a, Path>> {
809    let file = buffer.read(cx).as_singleton()?.read(cx).file()?;
810    // Ensure we always render at least the filename.
811    height += 1;
812
813    let mut prefix = file.path().as_ref();
814    while height > 0 {
815        if let Some(parent) = prefix.parent() {
816            prefix = parent;
817            height -= 1;
818        } else {
819            break;
820        }
821    }
822
823    // Here we could have just always used `full_path`, but that is very
824    // allocation-heavy and so we try to use a `Cow<Path>` if we haven't
825    // traversed all the way up to the worktree's root.
826    if height > 0 {
827        let full_path = file.full_path(cx);
828        if include_filename {
829            Some(full_path.into())
830        } else {
831            Some(full_path.parent().unwrap().to_path_buf().into())
832        }
833    } else {
834        let mut path = file.path().strip_prefix(prefix).unwrap();
835        if !include_filename {
836            path = path.parent().unwrap();
837        }
838        Some(path.into())
839    }
840}