items.rs

   1use anyhow::{anyhow, Context, Result};
   2use futures::FutureExt;
   3use gpui::{
   4    elements::*, geometry::vector::vec2f, AppContext, Entity, ModelHandle, MutableAppContext,
   5    RenderContext, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle,
   6};
   7use language::{Bias, Buffer, File as _, OffsetRangeExt, Point, SelectionGoal};
   8use project::{File, FormatTrigger, Project, ProjectEntryId, ProjectPath};
   9use rpc::proto::{self, update_view};
  10use settings::Settings;
  11use smallvec::SmallVec;
  12use std::{
  13    borrow::Cow,
  14    cmp::{self, Ordering},
  15    fmt::Write,
  16    ops::Range,
  17    path::{Path, PathBuf},
  18};
  19use text::Selection;
  20use util::{ResultExt, TryFutureExt};
  21use workspace::{
  22    item::{FollowableItem, Item, ItemEvent, ItemHandle, ProjectItem},
  23    searchable::{Direction, SearchEvent, SearchableItem, SearchableItemHandle},
  24    ItemId, ItemNavHistory, Pane, StatusItemView, ToolbarItemLocation, Workspace, WorkspaceId,
  25};
  26
  27use crate::{
  28    display_map::ToDisplayPoint, link_go_to_definition::hide_link_definition,
  29    movement::surrounding_word, persistence::DB, Anchor, Autoscroll, Editor, Event, ExcerptId,
  30    MultiBuffer, MultiBufferSnapshot, NavigationData, ToPoint as _, FORMAT_TIMEOUT,
  31};
  32
  33pub const MAX_TAB_TITLE_LEN: usize = 24;
  34
  35impl FollowableItem for Editor {
  36    fn from_state_proto(
  37        pane: ViewHandle<workspace::Pane>,
  38        project: ModelHandle<Project>,
  39        state: &mut Option<proto::view::Variant>,
  40        cx: &mut MutableAppContext,
  41    ) -> Option<Task<Result<ViewHandle<Self>>>> {
  42        let state = if matches!(state, Some(proto::view::Variant::Editor(_))) {
  43            if let Some(proto::view::Variant::Editor(state)) = state.take() {
  44                state
  45            } else {
  46                unreachable!()
  47            }
  48        } else {
  49            return None;
  50        };
  51
  52        let buffer = project.update(cx, |project, cx| {
  53            project.open_buffer_by_id(state.buffer_id, cx)
  54        });
  55        Some(cx.spawn(|mut cx| async move {
  56            let buffer = buffer.await?;
  57            let editor = pane
  58                .read_with(&cx, |pane, cx| {
  59                    pane.items_of_type::<Self>().find(|editor| {
  60                        editor.read(cx).buffer.read(cx).as_singleton().as_ref() == Some(&buffer)
  61                    })
  62                })
  63                .unwrap_or_else(|| {
  64                    pane.update(&mut cx, |_, cx| {
  65                        cx.add_view(|cx| Editor::for_buffer(buffer, Some(project), cx))
  66                    })
  67                });
  68            editor.update(&mut cx, |editor, cx| {
  69                let excerpt_id;
  70                let buffer_id;
  71                {
  72                    let buffer = editor.buffer.read(cx).read(cx);
  73                    let singleton = buffer.as_singleton().unwrap();
  74                    excerpt_id = singleton.0.clone();
  75                    buffer_id = singleton.1;
  76                }
  77                let selections = state
  78                    .selections
  79                    .into_iter()
  80                    .map(|selection| {
  81                        deserialize_selection(&excerpt_id, buffer_id, selection)
  82                            .ok_or_else(|| anyhow!("invalid selection"))
  83                    })
  84                    .collect::<Result<Vec<_>>>()?;
  85                if !selections.is_empty() {
  86                    editor.set_selections_from_remote(selections, cx);
  87                }
  88
  89                if let Some(anchor) = state.scroll_top_anchor {
  90                    editor.set_scroll_top_anchor(
  91                        Anchor {
  92                            buffer_id: Some(state.buffer_id as usize),
  93                            excerpt_id,
  94                            text_anchor: language::proto::deserialize_anchor(anchor)
  95                                .ok_or_else(|| anyhow!("invalid scroll top"))?,
  96                        },
  97                        vec2f(state.scroll_x, state.scroll_y),
  98                        cx,
  99                    );
 100                }
 101
 102                Ok::<_, anyhow::Error>(())
 103            })?;
 104            Ok(editor)
 105        }))
 106    }
 107
 108    fn set_leader_replica_id(
 109        &mut self,
 110        leader_replica_id: Option<u16>,
 111        cx: &mut ViewContext<Self>,
 112    ) {
 113        self.leader_replica_id = leader_replica_id;
 114        if self.leader_replica_id.is_some() {
 115            self.buffer.update(cx, |buffer, cx| {
 116                buffer.remove_active_selections(cx);
 117            });
 118        } else {
 119            self.buffer.update(cx, |buffer, cx| {
 120                if self.focused {
 121                    buffer.set_active_selections(
 122                        &self.selections.disjoint_anchors(),
 123                        self.selections.line_mode,
 124                        self.cursor_shape,
 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, _workspace_id: WorkspaceId, 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        self.report_event("save editor", cx);
 410
 411        let buffer = self.buffer().clone();
 412        let buffers = buffer.read(cx).all_buffers();
 413        let mut timeout = cx.background().timer(FORMAT_TIMEOUT).fuse();
 414        let format = project.update(cx, |project, cx| {
 415            project.format(buffers, true, FormatTrigger::Save, cx)
 416        });
 417        cx.spawn(|_, mut cx| async move {
 418            let transaction = futures::select_biased! {
 419                _ = timeout => {
 420                    log::warn!("timed out waiting for formatting");
 421                    None
 422                }
 423                transaction = format.log_err().fuse() => transaction,
 424            };
 425
 426            buffer
 427                .update(&mut cx, |buffer, cx| {
 428                    if let Some(transaction) = transaction {
 429                        if !buffer.is_singleton() {
 430                            buffer.push_transaction(&transaction.0);
 431                        }
 432                    }
 433
 434                    buffer.save(cx)
 435                })
 436                .await?;
 437            Ok(())
 438        })
 439    }
 440
 441    fn save_as(
 442        &mut self,
 443        project: ModelHandle<Project>,
 444        abs_path: PathBuf,
 445        cx: &mut ViewContext<Self>,
 446    ) -> Task<Result<()>> {
 447        let buffer = self
 448            .buffer()
 449            .read(cx)
 450            .as_singleton()
 451            .expect("cannot call save_as on an excerpt list");
 452
 453        project.update(cx, |project, cx| {
 454            project.save_buffer_as(buffer, abs_path, cx)
 455        })
 456    }
 457
 458    fn reload(
 459        &mut self,
 460        project: ModelHandle<Project>,
 461        cx: &mut ViewContext<Self>,
 462    ) -> Task<Result<()>> {
 463        let buffer = self.buffer().clone();
 464        let buffers = self.buffer.read(cx).all_buffers();
 465        let reload_buffers =
 466            project.update(cx, |project, cx| project.reload_buffers(buffers, true, cx));
 467        cx.spawn(|this, mut cx| async move {
 468            let transaction = reload_buffers.log_err().await;
 469            this.update(&mut cx, |editor, cx| {
 470                editor.request_autoscroll(Autoscroll::fit(), cx)
 471            });
 472            buffer.update(&mut cx, |buffer, _| {
 473                if let Some(transaction) = transaction {
 474                    if !buffer.is_singleton() {
 475                        buffer.push_transaction(&transaction.0);
 476                    }
 477                }
 478            });
 479            Ok(())
 480        })
 481    }
 482
 483    fn git_diff_recalc(
 484        &mut self,
 485        _project: ModelHandle<Project>,
 486        cx: &mut ViewContext<Self>,
 487    ) -> Task<Result<()>> {
 488        self.buffer().update(cx, |multibuffer, cx| {
 489            multibuffer.git_diff_recalc(cx);
 490        });
 491        Task::ready(Ok(()))
 492    }
 493
 494    fn to_item_events(event: &Self::Event) -> Vec<ItemEvent> {
 495        let mut result = Vec::new();
 496        match event {
 497            Event::Closed => result.push(ItemEvent::CloseItem),
 498            Event::Saved | Event::TitleChanged => {
 499                result.push(ItemEvent::UpdateTab);
 500                result.push(ItemEvent::UpdateBreadcrumbs);
 501            }
 502            Event::Reparsed => {
 503                result.push(ItemEvent::UpdateBreadcrumbs);
 504            }
 505            Event::SelectionsChanged { local } if *local => {
 506                result.push(ItemEvent::UpdateBreadcrumbs);
 507            }
 508            Event::DirtyChanged => {
 509                result.push(ItemEvent::UpdateTab);
 510            }
 511            Event::BufferEdited => {
 512                result.push(ItemEvent::Edit);
 513                result.push(ItemEvent::UpdateBreadcrumbs);
 514            }
 515            _ => {}
 516        }
 517        result
 518    }
 519
 520    fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
 521        Some(Box::new(handle.clone()))
 522    }
 523
 524    fn breadcrumb_location(&self) -> ToolbarItemLocation {
 525        ToolbarItemLocation::PrimaryLeft { flex: None }
 526    }
 527
 528    fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<ElementBox>> {
 529        let cursor = self.selections.newest_anchor().head();
 530        let multibuffer = &self.buffer().read(cx);
 531        let (buffer_id, symbols) =
 532            multibuffer.symbols_containing(cursor, Some(&theme.editor.syntax), cx)?;
 533        let buffer = multibuffer.buffer(buffer_id)?;
 534
 535        let buffer = buffer.read(cx);
 536        let filename = buffer
 537            .snapshot()
 538            .resolve_file_path(
 539                cx,
 540                self.project
 541                    .as_ref()
 542                    .map(|project| project.read(cx).visible_worktrees(cx).count() > 1)
 543                    .unwrap_or_default(),
 544            )
 545            .map(|path| path.to_string_lossy().to_string())
 546            .unwrap_or_else(|| "untitled".to_string());
 547
 548        let mut breadcrumbs = vec![Label::new(filename, theme.breadcrumbs.text.clone()).boxed()];
 549        breadcrumbs.extend(symbols.into_iter().map(|symbol| {
 550            Text::new(symbol.text, theme.breadcrumbs.text.clone())
 551                .with_highlights(symbol.highlight_ranges)
 552                .boxed()
 553        }));
 554        Some(breadcrumbs)
 555    }
 556
 557    fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
 558        let workspace_id = workspace.database_id();
 559        let item_id = cx.view_id();
 560
 561        fn serialize(
 562            buffer: ModelHandle<Buffer>,
 563            workspace_id: WorkspaceId,
 564            item_id: ItemId,
 565            cx: &mut MutableAppContext,
 566        ) {
 567            if let Some(file) = buffer.read(cx).file().and_then(|file| file.as_local()) {
 568                let path = file.abs_path(cx);
 569
 570                cx.background()
 571                    .spawn(async move {
 572                        DB.save_path(item_id, workspace_id, path.clone())
 573                            .await
 574                            .log_err()
 575                    })
 576                    .detach();
 577            }
 578        }
 579
 580        if let Some(buffer) = self.buffer().read(cx).as_singleton() {
 581            serialize(buffer.clone(), workspace_id, item_id, cx);
 582
 583            cx.subscribe(&buffer, |this, buffer, event, cx| {
 584                if let Some(workspace_id) = this.workspace_id {
 585                    if let language::Event::FileHandleChanged = event {
 586                        serialize(buffer, workspace_id, cx.view_id(), cx);
 587                    }
 588                }
 589            })
 590            .detach();
 591        }
 592    }
 593
 594    fn serialized_item_kind() -> Option<&'static str> {
 595        Some("Editor")
 596    }
 597
 598    fn deserialize(
 599        project: ModelHandle<Project>,
 600        _workspace: WeakViewHandle<Workspace>,
 601        workspace_id: workspace::WorkspaceId,
 602        item_id: ItemId,
 603        cx: &mut ViewContext<Pane>,
 604    ) -> Task<Result<ViewHandle<Self>>> {
 605        let project_item: Result<_> = project.update(cx, |project, cx| {
 606            // Look up the path with this key associated, create a self with that path
 607            let path = DB
 608                .get_path(item_id, workspace_id)?
 609                .context("No path stored for this editor")?;
 610
 611            let (worktree, path) = project
 612                .find_local_worktree(&path, cx)
 613                .with_context(|| format!("No worktree for path: {path:?}"))?;
 614            let project_path = ProjectPath {
 615                worktree_id: worktree.read(cx).id(),
 616                path: path.into(),
 617            };
 618
 619            Ok(project.open_path(project_path, cx))
 620        });
 621
 622        project_item
 623            .map(|project_item| {
 624                cx.spawn(|pane, mut cx| async move {
 625                    let (_, project_item) = project_item.await?;
 626                    let buffer = project_item
 627                        .downcast::<Buffer>()
 628                        .context("Project item at stored path was not a buffer")?;
 629
 630                    Ok(cx.update(|cx| {
 631                        cx.add_view(pane, |cx| Editor::for_buffer(buffer, Some(project), cx))
 632                    }))
 633                })
 634            })
 635            .unwrap_or_else(|error| Task::ready(Err(error)))
 636    }
 637}
 638
 639impl ProjectItem for Editor {
 640    type Item = Buffer;
 641
 642    fn for_project_item(
 643        project: ModelHandle<Project>,
 644        buffer: ModelHandle<Buffer>,
 645        cx: &mut ViewContext<Self>,
 646    ) -> Self {
 647        Self::for_buffer(buffer, Some(project), cx)
 648    }
 649}
 650
 651enum BufferSearchHighlights {}
 652impl SearchableItem for Editor {
 653    type Match = Range<Anchor>;
 654
 655    fn to_search_event(event: &Self::Event) -> Option<SearchEvent> {
 656        match event {
 657            Event::BufferEdited => Some(SearchEvent::MatchesInvalidated),
 658            Event::SelectionsChanged { .. } => Some(SearchEvent::ActiveMatchChanged),
 659            _ => None,
 660        }
 661    }
 662
 663    fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
 664        self.clear_background_highlights::<BufferSearchHighlights>(cx);
 665    }
 666
 667    fn update_matches(&mut self, matches: Vec<Range<Anchor>>, cx: &mut ViewContext<Self>) {
 668        self.highlight_background::<BufferSearchHighlights>(
 669            matches,
 670            |theme| theme.search.match_background,
 671            cx,
 672        );
 673    }
 674
 675    fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String {
 676        let display_map = self.snapshot(cx).display_snapshot;
 677        let selection = self.selections.newest::<usize>(cx);
 678        if selection.start == selection.end {
 679            let point = selection.start.to_display_point(&display_map);
 680            let range = surrounding_word(&display_map, point);
 681            let range = range.start.to_offset(&display_map, Bias::Left)
 682                ..range.end.to_offset(&display_map, Bias::Right);
 683            let text: String = display_map.buffer_snapshot.text_for_range(range).collect();
 684            if text.trim().is_empty() {
 685                String::new()
 686            } else {
 687                text
 688            }
 689        } else {
 690            display_map
 691                .buffer_snapshot
 692                .text_for_range(selection.start..selection.end)
 693                .collect()
 694        }
 695    }
 696
 697    fn activate_match(
 698        &mut self,
 699        index: usize,
 700        matches: Vec<Range<Anchor>>,
 701        cx: &mut ViewContext<Self>,
 702    ) {
 703        self.unfold_ranges([matches[index].clone()], false, cx);
 704        self.change_selections(Some(Autoscroll::fit()), cx, |s| {
 705            s.select_ranges([matches[index].clone()])
 706        });
 707    }
 708
 709    fn match_index_for_direction(
 710        &mut self,
 711        matches: &Vec<Range<Anchor>>,
 712        mut current_index: usize,
 713        direction: Direction,
 714        cx: &mut ViewContext<Self>,
 715    ) -> usize {
 716        let buffer = self.buffer().read(cx).snapshot(cx);
 717        let cursor = self.selections.newest_anchor().head();
 718        if matches[current_index].start.cmp(&cursor, &buffer).is_gt() {
 719            if direction == Direction::Prev {
 720                if current_index == 0 {
 721                    current_index = matches.len() - 1;
 722                } else {
 723                    current_index -= 1;
 724                }
 725            }
 726        } else if matches[current_index].end.cmp(&cursor, &buffer).is_lt() {
 727            if direction == Direction::Next {
 728                current_index = 0;
 729            }
 730        } else if direction == Direction::Prev {
 731            if current_index == 0 {
 732                current_index = matches.len() - 1;
 733            } else {
 734                current_index -= 1;
 735            }
 736        } else if direction == Direction::Next {
 737            if current_index == matches.len() - 1 {
 738                current_index = 0
 739            } else {
 740                current_index += 1;
 741            }
 742        };
 743        current_index
 744    }
 745
 746    fn find_matches(
 747        &mut self,
 748        query: project::search::SearchQuery,
 749        cx: &mut ViewContext<Self>,
 750    ) -> Task<Vec<Range<Anchor>>> {
 751        let buffer = self.buffer().read(cx).snapshot(cx);
 752        cx.background().spawn(async move {
 753            let mut ranges = Vec::new();
 754            if let Some((_, _, excerpt_buffer)) = buffer.as_singleton() {
 755                ranges.extend(
 756                    query
 757                        .search(excerpt_buffer.as_rope())
 758                        .await
 759                        .into_iter()
 760                        .map(|range| {
 761                            buffer.anchor_after(range.start)..buffer.anchor_before(range.end)
 762                        }),
 763                );
 764            } else {
 765                for excerpt in buffer.excerpt_boundaries_in_range(0..buffer.len()) {
 766                    let excerpt_range = excerpt.range.context.to_offset(&excerpt.buffer);
 767                    let rope = excerpt.buffer.as_rope().slice(excerpt_range.clone());
 768                    ranges.extend(query.search(&rope).await.into_iter().map(|range| {
 769                        let start = excerpt
 770                            .buffer
 771                            .anchor_after(excerpt_range.start + range.start);
 772                        let end = excerpt
 773                            .buffer
 774                            .anchor_before(excerpt_range.start + range.end);
 775                        buffer.anchor_in_excerpt(excerpt.id.clone(), start)
 776                            ..buffer.anchor_in_excerpt(excerpt.id.clone(), end)
 777                    }));
 778                }
 779            }
 780            ranges
 781        })
 782    }
 783
 784    fn active_match_index(
 785        &mut self,
 786        matches: Vec<Range<Anchor>>,
 787        cx: &mut ViewContext<Self>,
 788    ) -> Option<usize> {
 789        active_match_index(
 790            &matches,
 791            &self.selections.newest_anchor().head(),
 792            &self.buffer().read(cx).snapshot(cx),
 793        )
 794    }
 795}
 796
 797pub fn active_match_index(
 798    ranges: &[Range<Anchor>],
 799    cursor: &Anchor,
 800    buffer: &MultiBufferSnapshot,
 801) -> Option<usize> {
 802    if ranges.is_empty() {
 803        None
 804    } else {
 805        match ranges.binary_search_by(|probe| {
 806            if probe.end.cmp(cursor, &*buffer).is_lt() {
 807                Ordering::Less
 808            } else if probe.start.cmp(cursor, &*buffer).is_gt() {
 809                Ordering::Greater
 810            } else {
 811                Ordering::Equal
 812            }
 813        }) {
 814            Ok(i) | Err(i) => Some(cmp::min(i, ranges.len() - 1)),
 815        }
 816    }
 817}
 818
 819pub struct CursorPosition {
 820    position: Option<Point>,
 821    selected_count: usize,
 822    _observe_active_editor: Option<Subscription>,
 823}
 824
 825impl Default for CursorPosition {
 826    fn default() -> Self {
 827        Self::new()
 828    }
 829}
 830
 831impl CursorPosition {
 832    pub fn new() -> Self {
 833        Self {
 834            position: None,
 835            selected_count: 0,
 836            _observe_active_editor: None,
 837        }
 838    }
 839
 840    fn update_position(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
 841        let editor = editor.read(cx);
 842        let buffer = editor.buffer().read(cx).snapshot(cx);
 843
 844        self.selected_count = 0;
 845        let mut last_selection: Option<Selection<usize>> = None;
 846        for selection in editor.selections.all::<usize>(cx) {
 847            self.selected_count += selection.end - selection.start;
 848            if last_selection
 849                .as_ref()
 850                .map_or(true, |last_selection| selection.id > last_selection.id)
 851            {
 852                last_selection = Some(selection);
 853            }
 854        }
 855        self.position = last_selection.map(|s| s.head().to_point(&buffer));
 856
 857        cx.notify();
 858    }
 859}
 860
 861impl Entity for CursorPosition {
 862    type Event = ();
 863}
 864
 865impl View for CursorPosition {
 866    fn ui_name() -> &'static str {
 867        "CursorPosition"
 868    }
 869
 870    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
 871        if let Some(position) = self.position {
 872            let theme = &cx.global::<Settings>().theme.workspace.status_bar;
 873            let mut text = format!("{},{}", position.row + 1, position.column + 1);
 874            if self.selected_count > 0 {
 875                write!(text, " ({} selected)", self.selected_count).unwrap();
 876            }
 877            Label::new(text, theme.cursor_position.clone()).boxed()
 878        } else {
 879            Empty::new().boxed()
 880        }
 881    }
 882}
 883
 884impl StatusItemView for CursorPosition {
 885    fn set_active_pane_item(
 886        &mut self,
 887        active_pane_item: Option<&dyn ItemHandle>,
 888        cx: &mut ViewContext<Self>,
 889    ) {
 890        if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
 891            self._observe_active_editor = Some(cx.observe(&editor, Self::update_position));
 892            self.update_position(editor, cx);
 893        } else {
 894            self.position = None;
 895            self._observe_active_editor = None;
 896        }
 897
 898        cx.notify();
 899    }
 900}
 901
 902fn path_for_buffer<'a>(
 903    buffer: &ModelHandle<MultiBuffer>,
 904    height: usize,
 905    include_filename: bool,
 906    cx: &'a AppContext,
 907) -> Option<Cow<'a, Path>> {
 908    let file = buffer.read(cx).as_singleton()?.read(cx).file()?;
 909    path_for_file(file, height, include_filename, cx)
 910}
 911
 912fn path_for_file<'a>(
 913    file: &'a dyn language::File,
 914    mut height: usize,
 915    include_filename: bool,
 916    cx: &'a AppContext,
 917) -> Option<Cow<'a, Path>> {
 918    // Ensure we always render at least the filename.
 919    height += 1;
 920
 921    let mut prefix = file.path().as_ref();
 922    while height > 0 {
 923        if let Some(parent) = prefix.parent() {
 924            prefix = parent;
 925            height -= 1;
 926        } else {
 927            break;
 928        }
 929    }
 930
 931    // Here we could have just always used `full_path`, but that is very
 932    // allocation-heavy and so we try to use a `Cow<Path>` if we haven't
 933    // traversed all the way up to the worktree's root.
 934    if height > 0 {
 935        let full_path = file.full_path(cx);
 936        if include_filename {
 937            Some(full_path.into())
 938        } else {
 939            Some(full_path.parent()?.to_path_buf().into())
 940        }
 941    } else {
 942        let mut path = file.path().strip_prefix(prefix).ok()?;
 943        if !include_filename {
 944            path = path.parent()?;
 945        }
 946        Some(path.into())
 947    }
 948}
 949
 950#[cfg(test)]
 951mod tests {
 952    use super::*;
 953    use gpui::MutableAppContext;
 954    use std::{
 955        path::{Path, PathBuf},
 956        sync::Arc,
 957    };
 958
 959    #[gpui::test]
 960    fn test_path_for_file(cx: &mut MutableAppContext) {
 961        let file = TestFile {
 962            path: Path::new("").into(),
 963            full_path: PathBuf::from(""),
 964        };
 965        assert_eq!(path_for_file(&file, 0, false, cx), None);
 966    }
 967
 968    struct TestFile {
 969        path: Arc<Path>,
 970        full_path: PathBuf,
 971    }
 972
 973    impl language::File for TestFile {
 974        fn path(&self) -> &Arc<Path> {
 975            &self.path
 976        }
 977
 978        fn full_path(&self, _: &gpui::AppContext) -> PathBuf {
 979            self.full_path.clone()
 980        }
 981
 982        fn as_local(&self) -> Option<&dyn language::LocalFile> {
 983            todo!()
 984        }
 985
 986        fn mtime(&self) -> std::time::SystemTime {
 987            todo!()
 988        }
 989
 990        fn file_name<'a>(&'a self, _: &'a gpui::AppContext) -> &'a std::ffi::OsStr {
 991            todo!()
 992        }
 993
 994        fn is_deleted(&self) -> bool {
 995            todo!()
 996        }
 997
 998        fn save(
 999            &self,
1000            _: u64,
1001            _: language::Rope,
1002            _: clock::Global,
1003            _: project::LineEnding,
1004            _: &mut MutableAppContext,
1005        ) -> gpui::Task<anyhow::Result<(clock::Global, String, std::time::SystemTime)>> {
1006            todo!()
1007        }
1008
1009        fn as_any(&self) -> &dyn std::any::Any {
1010            todo!()
1011        }
1012
1013        fn to_proto(&self) -> rpc::proto::File {
1014            todo!()
1015        }
1016    }
1017}