items.rs

   1use crate::{
   2    display_map::ToDisplayPoint, link_go_to_definition::hide_link_definition,
   3    movement::surrounding_word, persistence::DB, scroll::ScrollAnchor, Anchor, Autoscroll, Editor,
   4    Event, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, NavigationData, ToPoint as _,
   5    FORMAT_TIMEOUT,
   6};
   7use anyhow::{anyhow, Context, Result};
   8use collections::HashSet;
   9use futures::future::try_join_all;
  10use futures::FutureExt;
  11use gpui::{
  12    elements::*, geometry::vector::vec2f, AppContext, Entity, ModelHandle, MutableAppContext,
  13    RenderContext, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle,
  14};
  15use language::proto::serialize_anchor as serialize_text_anchor;
  16use language::{Bias, Buffer, File as _, OffsetRangeExt, Point, SelectionGoal};
  17use project::{File, FormatTrigger, Project, ProjectEntryId, ProjectPath};
  18use rpc::proto::{self, update_view};
  19use settings::Settings;
  20use smallvec::SmallVec;
  21use std::{
  22    borrow::Cow,
  23    cmp::{self, Ordering},
  24    fmt::Write,
  25    iter,
  26    ops::Range,
  27    path::{Path, PathBuf},
  28};
  29use text::Selection;
  30use util::{ResultExt, TryFutureExt};
  31use workspace::item::FollowableItemHandle;
  32use workspace::{
  33    item::{FollowableItem, Item, ItemEvent, ItemHandle, ProjectItem},
  34    searchable::{Direction, SearchEvent, SearchableItem, SearchableItemHandle},
  35    ItemId, ItemNavHistory, Pane, StatusItemView, ToolbarItemLocation, ViewId, Workspace,
  36    WorkspaceId,
  37};
  38
  39pub const MAX_TAB_TITLE_LEN: usize = 24;
  40
  41impl FollowableItem for Editor {
  42    fn remote_id(&self) -> Option<ViewId> {
  43        self.remote_id
  44    }
  45
  46    fn from_state_proto(
  47        pane: ViewHandle<workspace::Pane>,
  48        project: ModelHandle<Project>,
  49        remote_id: ViewId,
  50        state: &mut Option<proto::view::Variant>,
  51        cx: &mut MutableAppContext,
  52    ) -> Option<Task<Result<ViewHandle<Self>>>> {
  53        let Some(proto::view::Variant::Editor(_)) = state else { return None };
  54        let Some(proto::view::Variant::Editor(state)) = state.take() else { unreachable!() };
  55
  56        let client = project.read(cx).client();
  57        let replica_id = project.read(cx).replica_id();
  58        let buffer_ids = state
  59            .excerpts
  60            .iter()
  61            .map(|excerpt| excerpt.buffer_id)
  62            .collect::<HashSet<_>>();
  63        let buffers = project.update(cx, |project, cx| {
  64            buffer_ids
  65                .iter()
  66                .map(|id| project.open_buffer_by_id(*id, cx))
  67                .collect::<Vec<_>>()
  68        });
  69
  70        Some(cx.spawn(|mut cx| async move {
  71            let mut buffers = futures::future::try_join_all(buffers).await?;
  72            let editor = pane.read_with(&cx, |pane, cx| {
  73                let mut editors = pane.items_of_type::<Self>();
  74                editors.find(|editor| {
  75                    editor.remote_id(&client, cx) == Some(remote_id)
  76                        || state.singleton
  77                            && buffers.len() == 1
  78                            && editor.read(cx).buffer.read(cx).as_singleton().as_ref()
  79                                == Some(&buffers[0])
  80                })
  81            });
  82
  83            let editor = editor.unwrap_or_else(|| {
  84                pane.update(&mut cx, |_, cx| {
  85                    let multibuffer = cx.add_model(|cx| {
  86                        let mut multibuffer;
  87                        if state.singleton && buffers.len() == 1 {
  88                            multibuffer = MultiBuffer::singleton(buffers.pop().unwrap(), cx)
  89                        } else {
  90                            multibuffer = MultiBuffer::new(replica_id);
  91                            let mut excerpts = state.excerpts.into_iter().peekable();
  92                            while let Some(excerpt) = excerpts.peek() {
  93                                let buffer_id = excerpt.buffer_id;
  94                                let buffer_excerpts = iter::from_fn(|| {
  95                                    let excerpt = excerpts.peek()?;
  96                                    (excerpt.buffer_id == buffer_id)
  97                                        .then(|| excerpts.next().unwrap())
  98                                });
  99                                let buffer =
 100                                    buffers.iter().find(|b| b.read(cx).remote_id() == buffer_id);
 101                                if let Some(buffer) = buffer {
 102                                    multibuffer.push_excerpts(
 103                                        buffer.clone(),
 104                                        buffer_excerpts.filter_map(deserialize_excerpt_range),
 105                                        cx,
 106                                    );
 107                                }
 108                            }
 109                        };
 110
 111                        if let Some(title) = &state.title {
 112                            multibuffer = multibuffer.with_title(title.clone())
 113                        }
 114
 115                        multibuffer
 116                    });
 117
 118                    cx.add_view(|cx| Editor::for_multibuffer(multibuffer, Some(project), cx))
 119                })
 120            });
 121
 122            editor.update(&mut cx, |editor, cx| {
 123                editor.remote_id = Some(remote_id);
 124                let buffer = editor.buffer.read(cx).read(cx);
 125                let selections = state
 126                    .selections
 127                    .into_iter()
 128                    .map(|selection| {
 129                        deserialize_selection(&buffer, selection)
 130                            .ok_or_else(|| anyhow!("invalid selection"))
 131                    })
 132                    .collect::<Result<Vec<_>>>()?;
 133                let scroll_top_anchor = state
 134                    .scroll_top_anchor
 135                    .and_then(|anchor| deserialize_anchor(&buffer, anchor));
 136                drop(buffer);
 137
 138                if !selections.is_empty() {
 139                    editor.set_selections_from_remote(selections, cx);
 140                }
 141
 142                if let Some(scroll_top_anchor) = scroll_top_anchor {
 143                    editor.set_scroll_anchor_remote(
 144                        ScrollAnchor {
 145                            top_anchor: scroll_top_anchor,
 146                            offset: vec2f(state.scroll_x, state.scroll_y),
 147                        },
 148                        cx,
 149                    );
 150                }
 151
 152                anyhow::Ok(())
 153            })?;
 154
 155            Ok(editor)
 156        }))
 157    }
 158
 159    fn set_leader_replica_id(
 160        &mut self,
 161        leader_replica_id: Option<u16>,
 162        cx: &mut ViewContext<Self>,
 163    ) {
 164        self.leader_replica_id = leader_replica_id;
 165        if self.leader_replica_id.is_some() {
 166            self.buffer.update(cx, |buffer, cx| {
 167                buffer.remove_active_selections(cx);
 168            });
 169        } else {
 170            self.buffer.update(cx, |buffer, cx| {
 171                if self.focused {
 172                    buffer.set_active_selections(
 173                        &self.selections.disjoint_anchors(),
 174                        self.selections.line_mode,
 175                        self.cursor_shape,
 176                        cx,
 177                    );
 178                }
 179            });
 180        }
 181        cx.notify();
 182    }
 183
 184    fn to_state_proto(&self, cx: &AppContext) -> Option<proto::view::Variant> {
 185        let buffer = self.buffer.read(cx);
 186        let scroll_anchor = self.scroll_manager.anchor();
 187        let excerpts = buffer
 188            .read(cx)
 189            .excerpts()
 190            .map(|(id, buffer, range)| proto::Excerpt {
 191                id: id.to_proto(),
 192                buffer_id: buffer.remote_id(),
 193                context_start: Some(serialize_text_anchor(&range.context.start)),
 194                context_end: Some(serialize_text_anchor(&range.context.end)),
 195                primary_start: range
 196                    .primary
 197                    .as_ref()
 198                    .map(|range| serialize_text_anchor(&range.start)),
 199                primary_end: range
 200                    .primary
 201                    .as_ref()
 202                    .map(|range| serialize_text_anchor(&range.end)),
 203            })
 204            .collect();
 205
 206        Some(proto::view::Variant::Editor(proto::view::Editor {
 207            singleton: buffer.is_singleton(),
 208            title: (!buffer.is_singleton()).then(|| buffer.title(cx).into()),
 209            excerpts,
 210            scroll_top_anchor: Some(serialize_anchor(&scroll_anchor.top_anchor)),
 211            scroll_x: scroll_anchor.offset.x(),
 212            scroll_y: scroll_anchor.offset.y(),
 213            selections: self
 214                .selections
 215                .disjoint_anchors()
 216                .iter()
 217                .map(serialize_selection)
 218                .collect(),
 219        }))
 220    }
 221
 222    fn add_event_to_update_proto(
 223        &self,
 224        event: &Self::Event,
 225        update: &mut Option<proto::update_view::Variant>,
 226        cx: &AppContext,
 227    ) -> bool {
 228        let update =
 229            update.get_or_insert_with(|| proto::update_view::Variant::Editor(Default::default()));
 230
 231        match update {
 232            proto::update_view::Variant::Editor(update) => match event {
 233                Event::ExcerptsAdded {
 234                    buffer,
 235                    predecessor,
 236                    excerpts,
 237                } => {
 238                    let buffer_id = buffer.read(cx).remote_id();
 239                    let mut excerpts = excerpts.iter();
 240                    if let Some((id, range)) = excerpts.next() {
 241                        update.inserted_excerpts.push(proto::ExcerptInsertion {
 242                            previous_excerpt_id: Some(predecessor.to_proto()),
 243                            excerpt: serialize_excerpt(buffer_id, id, range),
 244                        });
 245                        update.inserted_excerpts.extend(excerpts.map(|(id, range)| {
 246                            proto::ExcerptInsertion {
 247                                previous_excerpt_id: None,
 248                                excerpt: serialize_excerpt(buffer_id, id, range),
 249                            }
 250                        }))
 251                    }
 252                    true
 253                }
 254                Event::ExcerptsRemoved { ids } => {
 255                    update
 256                        .deleted_excerpts
 257                        .extend(ids.iter().map(ExcerptId::to_proto));
 258                    true
 259                }
 260                Event::ScrollPositionChanged { .. } => {
 261                    let scroll_anchor = self.scroll_manager.anchor();
 262                    update.scroll_top_anchor = Some(serialize_anchor(&scroll_anchor.top_anchor));
 263                    update.scroll_x = scroll_anchor.offset.x();
 264                    update.scroll_y = scroll_anchor.offset.y();
 265                    true
 266                }
 267                Event::SelectionsChanged { .. } => {
 268                    update.selections = self
 269                        .selections
 270                        .disjoint_anchors()
 271                        .iter()
 272                        .chain(self.selections.pending_anchor().as_ref())
 273                        .map(serialize_selection)
 274                        .collect();
 275                    true
 276                }
 277                _ => false,
 278            },
 279        }
 280    }
 281
 282    fn apply_update_proto(
 283        &mut self,
 284        project: &ModelHandle<Project>,
 285        message: update_view::Variant,
 286        cx: &mut ViewContext<Self>,
 287    ) -> Task<Result<()>> {
 288        let update_view::Variant::Editor(message) = message;
 289        let multibuffer = self.buffer.read(cx);
 290        let multibuffer = multibuffer.read(cx);
 291
 292        let buffer_ids = message
 293            .inserted_excerpts
 294            .iter()
 295            .filter_map(|insertion| Some(insertion.excerpt.as_ref()?.buffer_id))
 296            .collect::<HashSet<_>>();
 297
 298        let mut removals = message
 299            .deleted_excerpts
 300            .into_iter()
 301            .map(ExcerptId::from_proto)
 302            .collect::<Vec<_>>();
 303        removals.sort_by(|a, b| a.cmp(&b, &multibuffer));
 304
 305        let selections = message
 306            .selections
 307            .into_iter()
 308            .filter_map(|selection| deserialize_selection(&multibuffer, selection))
 309            .collect::<Vec<_>>();
 310        let scroll_top_anchor = message
 311            .scroll_top_anchor
 312            .and_then(|anchor| deserialize_anchor(&multibuffer, anchor));
 313        drop(multibuffer);
 314
 315        let buffers = project.update(cx, |project, cx| {
 316            buffer_ids
 317                .into_iter()
 318                .map(|id| project.open_buffer_by_id(id, cx))
 319                .collect::<Vec<_>>()
 320        });
 321
 322        let project = project.clone();
 323        cx.spawn(|this, mut cx| async move {
 324            let _buffers = try_join_all(buffers).await?;
 325            this.update(&mut cx, |this, cx| {
 326                this.buffer.update(cx, |multibuffer, cx| {
 327                    let mut insertions = message.inserted_excerpts.into_iter().peekable();
 328                    while let Some(insertion) = insertions.next() {
 329                        let Some(excerpt) = insertion.excerpt else { continue };
 330                        let Some(previous_excerpt_id) = insertion.previous_excerpt_id else { continue };
 331                        let buffer_id = excerpt.buffer_id;
 332                        let Some(buffer) = project.read(cx).buffer_for_id(buffer_id, cx) else { continue };
 333
 334                        let adjacent_excerpts = iter::from_fn(|| {
 335                            let insertion = insertions.peek()?;
 336                            if insertion.previous_excerpt_id.is_none()
 337                                && insertion.excerpt.as_ref()?.buffer_id == buffer_id
 338                            {
 339                                insertions.next()?.excerpt
 340                            } else {
 341                                None
 342                            }
 343                        });
 344
 345                        multibuffer.insert_excerpts_with_ids_after(
 346                            ExcerptId::from_proto(previous_excerpt_id),
 347                            buffer,
 348                            [excerpt]
 349                                .into_iter()
 350                                .chain(adjacent_excerpts)
 351                                .filter_map(|excerpt| {
 352                                    Some((
 353                                        ExcerptId::from_proto(excerpt.id),
 354                                        deserialize_excerpt_range(excerpt)?,
 355                                    ))
 356                                }),
 357                            cx,
 358                        );
 359                    }
 360
 361                    multibuffer.remove_excerpts(removals, cx);
 362                });
 363
 364                if !selections.is_empty() {
 365                    this.set_selections_from_remote(selections, cx);
 366                    this.request_autoscroll_remotely(Autoscroll::newest(), cx);
 367                } else if let Some(anchor) = scroll_top_anchor {
 368                    this.set_scroll_anchor_remote(ScrollAnchor {
 369                        top_anchor: anchor,
 370                        offset: vec2f(message.scroll_x, message.scroll_y)
 371                    }, cx);
 372                }
 373            });
 374            Ok(())
 375        })
 376    }
 377
 378    fn should_unfollow_on_event(event: &Self::Event, _: &AppContext) -> bool {
 379        match event {
 380            Event::Edited => true,
 381            Event::SelectionsChanged { local } => *local,
 382            Event::ScrollPositionChanged { local } => *local,
 383            _ => false,
 384        }
 385    }
 386}
 387
 388fn serialize_excerpt(
 389    buffer_id: u64,
 390    id: &ExcerptId,
 391    range: &ExcerptRange<language::Anchor>,
 392) -> Option<proto::Excerpt> {
 393    Some(proto::Excerpt {
 394        id: id.to_proto(),
 395        buffer_id,
 396        context_start: Some(serialize_text_anchor(&range.context.start)),
 397        context_end: Some(serialize_text_anchor(&range.context.end)),
 398        primary_start: range
 399            .primary
 400            .as_ref()
 401            .map(|r| serialize_text_anchor(&r.start)),
 402        primary_end: range
 403            .primary
 404            .as_ref()
 405            .map(|r| serialize_text_anchor(&r.end)),
 406    })
 407}
 408
 409fn serialize_selection(selection: &Selection<Anchor>) -> proto::Selection {
 410    proto::Selection {
 411        id: selection.id as u64,
 412        start: Some(serialize_anchor(&selection.start)),
 413        end: Some(serialize_anchor(&selection.end)),
 414        reversed: selection.reversed,
 415    }
 416}
 417
 418fn serialize_anchor(anchor: &Anchor) -> proto::EditorAnchor {
 419    proto::EditorAnchor {
 420        excerpt_id: anchor.excerpt_id.to_proto(),
 421        anchor: Some(serialize_text_anchor(&anchor.text_anchor)),
 422    }
 423}
 424
 425fn deserialize_excerpt_range(excerpt: proto::Excerpt) -> Option<ExcerptRange<language::Anchor>> {
 426    let context = {
 427        let start = language::proto::deserialize_anchor(excerpt.context_start?)?;
 428        let end = language::proto::deserialize_anchor(excerpt.context_end?)?;
 429        start..end
 430    };
 431    let primary = excerpt
 432        .primary_start
 433        .zip(excerpt.primary_end)
 434        .and_then(|(start, end)| {
 435            let start = language::proto::deserialize_anchor(start)?;
 436            let end = language::proto::deserialize_anchor(end)?;
 437            Some(start..end)
 438        });
 439    Some(ExcerptRange { context, primary })
 440}
 441
 442fn deserialize_selection(
 443    buffer: &MultiBufferSnapshot,
 444    selection: proto::Selection,
 445) -> Option<Selection<Anchor>> {
 446    Some(Selection {
 447        id: selection.id as usize,
 448        start: deserialize_anchor(buffer, selection.start?)?,
 449        end: deserialize_anchor(buffer, selection.end?)?,
 450        reversed: selection.reversed,
 451        goal: SelectionGoal::None,
 452    })
 453}
 454
 455fn deserialize_anchor(buffer: &MultiBufferSnapshot, anchor: proto::EditorAnchor) -> Option<Anchor> {
 456    let excerpt_id = ExcerptId::from_proto(anchor.excerpt_id);
 457    Some(Anchor {
 458        excerpt_id,
 459        text_anchor: language::proto::deserialize_anchor(anchor.anchor?)?,
 460        buffer_id: buffer.buffer_id_for_excerpt(excerpt_id),
 461    })
 462}
 463
 464impl Item for Editor {
 465    fn navigate(&mut self, data: Box<dyn std::any::Any>, cx: &mut ViewContext<Self>) -> bool {
 466        if let Ok(data) = data.downcast::<NavigationData>() {
 467            let newest_selection = self.selections.newest::<Point>(cx);
 468            let buffer = self.buffer.read(cx).read(cx);
 469            let offset = if buffer.can_resolve(&data.cursor_anchor) {
 470                data.cursor_anchor.to_point(&buffer)
 471            } else {
 472                buffer.clip_point(data.cursor_position, Bias::Left)
 473            };
 474
 475            let mut scroll_anchor = data.scroll_anchor;
 476            if !buffer.can_resolve(&scroll_anchor.top_anchor) {
 477                scroll_anchor.top_anchor = buffer.anchor_before(
 478                    buffer.clip_point(Point::new(data.scroll_top_row, 0), Bias::Left),
 479                );
 480            }
 481
 482            drop(buffer);
 483
 484            if newest_selection.head() == offset {
 485                false
 486            } else {
 487                let nav_history = self.nav_history.take();
 488                self.set_scroll_anchor(scroll_anchor, cx);
 489                self.change_selections(Some(Autoscroll::fit()), cx, |s| {
 490                    s.select_ranges([offset..offset])
 491                });
 492                self.nav_history = nav_history;
 493                true
 494            }
 495        } else {
 496            false
 497        }
 498    }
 499
 500    fn tab_description<'a>(&'a self, detail: usize, cx: &'a AppContext) -> Option<Cow<'a, str>> {
 501        match path_for_buffer(&self.buffer, detail, true, cx)? {
 502            Cow::Borrowed(path) => Some(path.to_string_lossy()),
 503            Cow::Owned(path) => Some(path.to_string_lossy().to_string().into()),
 504        }
 505    }
 506
 507    fn tab_content(
 508        &self,
 509        detail: Option<usize>,
 510        style: &theme::Tab,
 511        cx: &AppContext,
 512    ) -> ElementBox {
 513        Flex::row()
 514            .with_child(
 515                Label::new(self.title(cx).into(), style.label.clone())
 516                    .aligned()
 517                    .boxed(),
 518            )
 519            .with_children(detail.and_then(|detail| {
 520                let path = path_for_buffer(&self.buffer, detail, false, cx)?;
 521                let description = path.to_string_lossy();
 522                Some(
 523                    Label::new(
 524                        if description.len() > MAX_TAB_TITLE_LEN {
 525                            description[..MAX_TAB_TITLE_LEN].to_string() + ""
 526                        } else {
 527                            description.into()
 528                        },
 529                        style.description.text.clone(),
 530                    )
 531                    .contained()
 532                    .with_style(style.description.container)
 533                    .aligned()
 534                    .boxed(),
 535                )
 536            }))
 537            .boxed()
 538    }
 539
 540    fn project_path(&self, cx: &AppContext) -> Option<ProjectPath> {
 541        let buffer = self.buffer.read(cx).as_singleton()?;
 542        let file = buffer.read(cx).file();
 543        File::from_dyn(file).map(|file| ProjectPath {
 544            worktree_id: file.worktree_id(cx),
 545            path: file.path().clone(),
 546        })
 547    }
 548
 549    fn project_entry_ids(&self, cx: &AppContext) -> SmallVec<[ProjectEntryId; 3]> {
 550        self.buffer
 551            .read(cx)
 552            .files(cx)
 553            .into_iter()
 554            .filter_map(|file| File::from_dyn(Some(file))?.project_entry_id(cx))
 555            .collect()
 556    }
 557
 558    fn is_singleton(&self, cx: &AppContext) -> bool {
 559        self.buffer.read(cx).is_singleton()
 560    }
 561
 562    fn clone_on_split(&self, _workspace_id: WorkspaceId, cx: &mut ViewContext<Self>) -> Option<Self>
 563    where
 564        Self: Sized,
 565    {
 566        Some(self.clone(cx))
 567    }
 568
 569    fn set_nav_history(&mut self, history: ItemNavHistory, _: &mut ViewContext<Self>) {
 570        self.nav_history = Some(history);
 571    }
 572
 573    fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
 574        let selection = self.selections.newest_anchor();
 575        self.push_to_nav_history(selection.head(), None, cx);
 576    }
 577
 578    fn workspace_deactivated(&mut self, cx: &mut ViewContext<Self>) {
 579        hide_link_definition(self, cx);
 580        self.link_go_to_definition_state.last_mouse_location = None;
 581    }
 582
 583    fn is_dirty(&self, cx: &AppContext) -> bool {
 584        self.buffer().read(cx).read(cx).is_dirty()
 585    }
 586
 587    fn has_conflict(&self, cx: &AppContext) -> bool {
 588        self.buffer().read(cx).read(cx).has_conflict()
 589    }
 590
 591    fn can_save(&self, cx: &AppContext) -> bool {
 592        !self.buffer().read(cx).is_singleton() || self.project_path(cx).is_some()
 593    }
 594
 595    fn save(
 596        &mut self,
 597        project: ModelHandle<Project>,
 598        cx: &mut ViewContext<Self>,
 599    ) -> Task<Result<()>> {
 600        self.report_event("save editor", cx);
 601
 602        let buffer = self.buffer().clone();
 603        let buffers = buffer.read(cx).all_buffers();
 604        let mut timeout = cx.background().timer(FORMAT_TIMEOUT).fuse();
 605        let format = project.update(cx, |project, cx| {
 606            project.format(buffers, true, FormatTrigger::Save, cx)
 607        });
 608        cx.spawn(|_, mut cx| async move {
 609            let transaction = futures::select_biased! {
 610                _ = timeout => {
 611                    log::warn!("timed out waiting for formatting");
 612                    None
 613                }
 614                transaction = format.log_err().fuse() => transaction,
 615            };
 616
 617            buffer
 618                .update(&mut cx, |buffer, cx| {
 619                    if let Some(transaction) = transaction {
 620                        if !buffer.is_singleton() {
 621                            buffer.push_transaction(&transaction.0);
 622                        }
 623                    }
 624
 625                    buffer.save(cx)
 626                })
 627                .await?;
 628            Ok(())
 629        })
 630    }
 631
 632    fn save_as(
 633        &mut self,
 634        project: ModelHandle<Project>,
 635        abs_path: PathBuf,
 636        cx: &mut ViewContext<Self>,
 637    ) -> Task<Result<()>> {
 638        let buffer = self
 639            .buffer()
 640            .read(cx)
 641            .as_singleton()
 642            .expect("cannot call save_as on an excerpt list");
 643
 644        project.update(cx, |project, cx| {
 645            project.save_buffer_as(buffer, abs_path, cx)
 646        })
 647    }
 648
 649    fn reload(
 650        &mut self,
 651        project: ModelHandle<Project>,
 652        cx: &mut ViewContext<Self>,
 653    ) -> Task<Result<()>> {
 654        let buffer = self.buffer().clone();
 655        let buffers = self.buffer.read(cx).all_buffers();
 656        let reload_buffers =
 657            project.update(cx, |project, cx| project.reload_buffers(buffers, true, cx));
 658        cx.spawn(|this, mut cx| async move {
 659            let transaction = reload_buffers.log_err().await;
 660            this.update(&mut cx, |editor, cx| {
 661                editor.request_autoscroll(Autoscroll::fit(), cx)
 662            });
 663            buffer.update(&mut cx, |buffer, _| {
 664                if let Some(transaction) = transaction {
 665                    if !buffer.is_singleton() {
 666                        buffer.push_transaction(&transaction.0);
 667                    }
 668                }
 669            });
 670            Ok(())
 671        })
 672    }
 673
 674    fn git_diff_recalc(
 675        &mut self,
 676        _project: ModelHandle<Project>,
 677        cx: &mut ViewContext<Self>,
 678    ) -> Task<Result<()>> {
 679        self.buffer().update(cx, |multibuffer, cx| {
 680            multibuffer.git_diff_recalc(cx);
 681        });
 682        Task::ready(Ok(()))
 683    }
 684
 685    fn to_item_events(event: &Self::Event) -> Vec<ItemEvent> {
 686        let mut result = Vec::new();
 687        match event {
 688            Event::Closed => result.push(ItemEvent::CloseItem),
 689            Event::Saved | Event::TitleChanged => {
 690                result.push(ItemEvent::UpdateTab);
 691                result.push(ItemEvent::UpdateBreadcrumbs);
 692            }
 693            Event::Reparsed => {
 694                result.push(ItemEvent::UpdateBreadcrumbs);
 695            }
 696            Event::SelectionsChanged { local } if *local => {
 697                result.push(ItemEvent::UpdateBreadcrumbs);
 698            }
 699            Event::DirtyChanged => {
 700                result.push(ItemEvent::UpdateTab);
 701            }
 702            Event::BufferEdited => {
 703                result.push(ItemEvent::Edit);
 704                result.push(ItemEvent::UpdateBreadcrumbs);
 705            }
 706            _ => {}
 707        }
 708        result
 709    }
 710
 711    fn as_searchable(&self, handle: &ViewHandle<Self>) -> Option<Box<dyn SearchableItemHandle>> {
 712        Some(Box::new(handle.clone()))
 713    }
 714
 715    fn breadcrumb_location(&self) -> ToolbarItemLocation {
 716        ToolbarItemLocation::PrimaryLeft { flex: None }
 717    }
 718
 719    fn breadcrumbs(&self, theme: &theme::Theme, cx: &AppContext) -> Option<Vec<ElementBox>> {
 720        let cursor = self.selections.newest_anchor().head();
 721        let multibuffer = &self.buffer().read(cx);
 722        let (buffer_id, symbols) =
 723            multibuffer.symbols_containing(cursor, Some(&theme.editor.syntax), cx)?;
 724        let buffer = multibuffer.buffer(buffer_id)?;
 725
 726        let buffer = buffer.read(cx);
 727        let filename = buffer
 728            .snapshot()
 729            .resolve_file_path(
 730                cx,
 731                self.project
 732                    .as_ref()
 733                    .map(|project| project.read(cx).visible_worktrees(cx).count() > 1)
 734                    .unwrap_or_default(),
 735            )
 736            .map(|path| path.to_string_lossy().to_string())
 737            .unwrap_or_else(|| "untitled".to_string());
 738
 739        let mut breadcrumbs = vec![Label::new(filename, theme.breadcrumbs.text.clone()).boxed()];
 740        breadcrumbs.extend(symbols.into_iter().map(|symbol| {
 741            Text::new(symbol.text, theme.breadcrumbs.text.clone())
 742                .with_highlights(symbol.highlight_ranges)
 743                .boxed()
 744        }));
 745        Some(breadcrumbs)
 746    }
 747
 748    fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext<Self>) {
 749        let workspace_id = workspace.database_id();
 750        let item_id = cx.view_id();
 751
 752        fn serialize(
 753            buffer: ModelHandle<Buffer>,
 754            workspace_id: WorkspaceId,
 755            item_id: ItemId,
 756            cx: &mut MutableAppContext,
 757        ) {
 758            if let Some(file) = buffer.read(cx).file().and_then(|file| file.as_local()) {
 759                let path = file.abs_path(cx);
 760
 761                cx.background()
 762                    .spawn(async move {
 763                        DB.save_path(item_id, workspace_id, path.clone())
 764                            .await
 765                            .log_err()
 766                    })
 767                    .detach();
 768            }
 769        }
 770
 771        if let Some(buffer) = self.buffer().read(cx).as_singleton() {
 772            serialize(buffer.clone(), workspace_id, item_id, cx);
 773
 774            cx.subscribe(&buffer, |this, buffer, event, cx| {
 775                if let Some(workspace_id) = this.workspace_id {
 776                    if let language::Event::FileHandleChanged = event {
 777                        serialize(buffer, workspace_id, cx.view_id(), cx);
 778                    }
 779                }
 780            })
 781            .detach();
 782        }
 783    }
 784
 785    fn serialized_item_kind() -> Option<&'static str> {
 786        Some("Editor")
 787    }
 788
 789    fn deserialize(
 790        project: ModelHandle<Project>,
 791        _workspace: WeakViewHandle<Workspace>,
 792        workspace_id: workspace::WorkspaceId,
 793        item_id: ItemId,
 794        cx: &mut ViewContext<Pane>,
 795    ) -> Task<Result<ViewHandle<Self>>> {
 796        let project_item: Result<_> = project.update(cx, |project, cx| {
 797            // Look up the path with this key associated, create a self with that path
 798            let path = DB
 799                .get_path(item_id, workspace_id)?
 800                .context("No path stored for this editor")?;
 801
 802            let (worktree, path) = project
 803                .find_local_worktree(&path, cx)
 804                .with_context(|| format!("No worktree for path: {path:?}"))?;
 805            let project_path = ProjectPath {
 806                worktree_id: worktree.read(cx).id(),
 807                path: path.into(),
 808            };
 809
 810            Ok(project.open_path(project_path, cx))
 811        });
 812
 813        project_item
 814            .map(|project_item| {
 815                cx.spawn(|pane, mut cx| async move {
 816                    let (_, project_item) = project_item.await?;
 817                    let buffer = project_item
 818                        .downcast::<Buffer>()
 819                        .context("Project item at stored path was not a buffer")?;
 820
 821                    Ok(cx.update(|cx| {
 822                        cx.add_view(pane, |cx| Editor::for_buffer(buffer, Some(project), cx))
 823                    }))
 824                })
 825            })
 826            .unwrap_or_else(|error| Task::ready(Err(error)))
 827    }
 828}
 829
 830impl ProjectItem for Editor {
 831    type Item = Buffer;
 832
 833    fn for_project_item(
 834        project: ModelHandle<Project>,
 835        buffer: ModelHandle<Buffer>,
 836        cx: &mut ViewContext<Self>,
 837    ) -> Self {
 838        Self::for_buffer(buffer, Some(project), cx)
 839    }
 840}
 841
 842enum BufferSearchHighlights {}
 843impl SearchableItem for Editor {
 844    type Match = Range<Anchor>;
 845
 846    fn to_search_event(event: &Self::Event) -> Option<SearchEvent> {
 847        match event {
 848            Event::BufferEdited => Some(SearchEvent::MatchesInvalidated),
 849            Event::SelectionsChanged { .. } => Some(SearchEvent::ActiveMatchChanged),
 850            _ => None,
 851        }
 852    }
 853
 854    fn clear_matches(&mut self, cx: &mut ViewContext<Self>) {
 855        self.clear_background_highlights::<BufferSearchHighlights>(cx);
 856    }
 857
 858    fn update_matches(&mut self, matches: Vec<Range<Anchor>>, cx: &mut ViewContext<Self>) {
 859        self.highlight_background::<BufferSearchHighlights>(
 860            matches,
 861            |theme| theme.search.match_background,
 862            cx,
 863        );
 864    }
 865
 866    fn query_suggestion(&mut self, cx: &mut ViewContext<Self>) -> String {
 867        let display_map = self.snapshot(cx).display_snapshot;
 868        let selection = self.selections.newest::<usize>(cx);
 869        if selection.start == selection.end {
 870            let point = selection.start.to_display_point(&display_map);
 871            let range = surrounding_word(&display_map, point);
 872            let range = range.start.to_offset(&display_map, Bias::Left)
 873                ..range.end.to_offset(&display_map, Bias::Right);
 874            let text: String = display_map.buffer_snapshot.text_for_range(range).collect();
 875            if text.trim().is_empty() {
 876                String::new()
 877            } else {
 878                text
 879            }
 880        } else {
 881            display_map
 882                .buffer_snapshot
 883                .text_for_range(selection.start..selection.end)
 884                .collect()
 885        }
 886    }
 887
 888    fn activate_match(
 889        &mut self,
 890        index: usize,
 891        matches: Vec<Range<Anchor>>,
 892        cx: &mut ViewContext<Self>,
 893    ) {
 894        self.unfold_ranges([matches[index].clone()], false, cx);
 895        self.change_selections(Some(Autoscroll::fit()), cx, |s| {
 896            s.select_ranges([matches[index].clone()])
 897        });
 898    }
 899
 900    fn match_index_for_direction(
 901        &mut self,
 902        matches: &Vec<Range<Anchor>>,
 903        mut current_index: usize,
 904        direction: Direction,
 905        cx: &mut ViewContext<Self>,
 906    ) -> usize {
 907        let buffer = self.buffer().read(cx).snapshot(cx);
 908        let cursor = self.selections.newest_anchor().head();
 909        if matches[current_index].start.cmp(&cursor, &buffer).is_gt() {
 910            if direction == Direction::Prev {
 911                if current_index == 0 {
 912                    current_index = matches.len() - 1;
 913                } else {
 914                    current_index -= 1;
 915                }
 916            }
 917        } else if matches[current_index].end.cmp(&cursor, &buffer).is_lt() {
 918            if direction == Direction::Next {
 919                current_index = 0;
 920            }
 921        } else if direction == Direction::Prev {
 922            if current_index == 0 {
 923                current_index = matches.len() - 1;
 924            } else {
 925                current_index -= 1;
 926            }
 927        } else if direction == Direction::Next {
 928            if current_index == matches.len() - 1 {
 929                current_index = 0
 930            } else {
 931                current_index += 1;
 932            }
 933        };
 934        current_index
 935    }
 936
 937    fn find_matches(
 938        &mut self,
 939        query: project::search::SearchQuery,
 940        cx: &mut ViewContext<Self>,
 941    ) -> Task<Vec<Range<Anchor>>> {
 942        let buffer = self.buffer().read(cx).snapshot(cx);
 943        cx.background().spawn(async move {
 944            let mut ranges = Vec::new();
 945            if let Some((_, _, excerpt_buffer)) = buffer.as_singleton() {
 946                ranges.extend(
 947                    query
 948                        .search(excerpt_buffer.as_rope())
 949                        .await
 950                        .into_iter()
 951                        .map(|range| {
 952                            buffer.anchor_after(range.start)..buffer.anchor_before(range.end)
 953                        }),
 954                );
 955            } else {
 956                for excerpt in buffer.excerpt_boundaries_in_range(0..buffer.len()) {
 957                    let excerpt_range = excerpt.range.context.to_offset(&excerpt.buffer);
 958                    let rope = excerpt.buffer.as_rope().slice(excerpt_range.clone());
 959                    ranges.extend(query.search(&rope).await.into_iter().map(|range| {
 960                        let start = excerpt
 961                            .buffer
 962                            .anchor_after(excerpt_range.start + range.start);
 963                        let end = excerpt
 964                            .buffer
 965                            .anchor_before(excerpt_range.start + range.end);
 966                        buffer.anchor_in_excerpt(excerpt.id.clone(), start)
 967                            ..buffer.anchor_in_excerpt(excerpt.id.clone(), end)
 968                    }));
 969                }
 970            }
 971            ranges
 972        })
 973    }
 974
 975    fn active_match_index(
 976        &mut self,
 977        matches: Vec<Range<Anchor>>,
 978        cx: &mut ViewContext<Self>,
 979    ) -> Option<usize> {
 980        active_match_index(
 981            &matches,
 982            &self.selections.newest_anchor().head(),
 983            &self.buffer().read(cx).snapshot(cx),
 984        )
 985    }
 986}
 987
 988pub fn active_match_index(
 989    ranges: &[Range<Anchor>],
 990    cursor: &Anchor,
 991    buffer: &MultiBufferSnapshot,
 992) -> Option<usize> {
 993    if ranges.is_empty() {
 994        None
 995    } else {
 996        match ranges.binary_search_by(|probe| {
 997            if probe.end.cmp(cursor, &*buffer).is_lt() {
 998                Ordering::Less
 999            } else if probe.start.cmp(cursor, &*buffer).is_gt() {
1000                Ordering::Greater
1001            } else {
1002                Ordering::Equal
1003            }
1004        }) {
1005            Ok(i) | Err(i) => Some(cmp::min(i, ranges.len() - 1)),
1006        }
1007    }
1008}
1009
1010pub struct CursorPosition {
1011    position: Option<Point>,
1012    selected_count: usize,
1013    _observe_active_editor: Option<Subscription>,
1014}
1015
1016impl Default for CursorPosition {
1017    fn default() -> Self {
1018        Self::new()
1019    }
1020}
1021
1022impl CursorPosition {
1023    pub fn new() -> Self {
1024        Self {
1025            position: None,
1026            selected_count: 0,
1027            _observe_active_editor: None,
1028        }
1029    }
1030
1031    fn update_position(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
1032        let editor = editor.read(cx);
1033        let buffer = editor.buffer().read(cx).snapshot(cx);
1034
1035        self.selected_count = 0;
1036        let mut last_selection: Option<Selection<usize>> = None;
1037        for selection in editor.selections.all::<usize>(cx) {
1038            self.selected_count += selection.end - selection.start;
1039            if last_selection
1040                .as_ref()
1041                .map_or(true, |last_selection| selection.id > last_selection.id)
1042            {
1043                last_selection = Some(selection);
1044            }
1045        }
1046        self.position = last_selection.map(|s| s.head().to_point(&buffer));
1047
1048        cx.notify();
1049    }
1050}
1051
1052impl Entity for CursorPosition {
1053    type Event = ();
1054}
1055
1056impl View for CursorPosition {
1057    fn ui_name() -> &'static str {
1058        "CursorPosition"
1059    }
1060
1061    fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
1062        if let Some(position) = self.position {
1063            let theme = &cx.global::<Settings>().theme.workspace.status_bar;
1064            let mut text = format!("{},{}", position.row + 1, position.column + 1);
1065            if self.selected_count > 0 {
1066                write!(text, " ({} selected)", self.selected_count).unwrap();
1067            }
1068            Label::new(text, theme.cursor_position.clone()).boxed()
1069        } else {
1070            Empty::new().boxed()
1071        }
1072    }
1073}
1074
1075impl StatusItemView for CursorPosition {
1076    fn set_active_pane_item(
1077        &mut self,
1078        active_pane_item: Option<&dyn ItemHandle>,
1079        cx: &mut ViewContext<Self>,
1080    ) {
1081        if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
1082            self._observe_active_editor = Some(cx.observe(&editor, Self::update_position));
1083            self.update_position(editor, cx);
1084        } else {
1085            self.position = None;
1086            self._observe_active_editor = None;
1087        }
1088
1089        cx.notify();
1090    }
1091}
1092
1093fn path_for_buffer<'a>(
1094    buffer: &ModelHandle<MultiBuffer>,
1095    height: usize,
1096    include_filename: bool,
1097    cx: &'a AppContext,
1098) -> Option<Cow<'a, Path>> {
1099    let file = buffer.read(cx).as_singleton()?.read(cx).file()?;
1100    path_for_file(file, height, include_filename, cx)
1101}
1102
1103fn path_for_file<'a>(
1104    file: &'a dyn language::File,
1105    mut height: usize,
1106    include_filename: bool,
1107    cx: &'a AppContext,
1108) -> Option<Cow<'a, Path>> {
1109    // Ensure we always render at least the filename.
1110    height += 1;
1111
1112    let mut prefix = file.path().as_ref();
1113    while height > 0 {
1114        if let Some(parent) = prefix.parent() {
1115            prefix = parent;
1116            height -= 1;
1117        } else {
1118            break;
1119        }
1120    }
1121
1122    // Here we could have just always used `full_path`, but that is very
1123    // allocation-heavy and so we try to use a `Cow<Path>` if we haven't
1124    // traversed all the way up to the worktree's root.
1125    if height > 0 {
1126        let full_path = file.full_path(cx);
1127        if include_filename {
1128            Some(full_path.into())
1129        } else {
1130            Some(full_path.parent()?.to_path_buf().into())
1131        }
1132    } else {
1133        let mut path = file.path().strip_prefix(prefix).ok()?;
1134        if !include_filename {
1135            path = path.parent()?;
1136        }
1137        Some(path.into())
1138    }
1139}
1140
1141#[cfg(test)]
1142mod tests {
1143    use super::*;
1144    use gpui::MutableAppContext;
1145    use std::{
1146        path::{Path, PathBuf},
1147        sync::Arc,
1148    };
1149
1150    #[gpui::test]
1151    fn test_path_for_file(cx: &mut MutableAppContext) {
1152        let file = TestFile {
1153            path: Path::new("").into(),
1154            full_path: PathBuf::from(""),
1155        };
1156        assert_eq!(path_for_file(&file, 0, false, cx), None);
1157    }
1158
1159    struct TestFile {
1160        path: Arc<Path>,
1161        full_path: PathBuf,
1162    }
1163
1164    impl language::File for TestFile {
1165        fn path(&self) -> &Arc<Path> {
1166            &self.path
1167        }
1168
1169        fn full_path(&self, _: &gpui::AppContext) -> PathBuf {
1170            self.full_path.clone()
1171        }
1172
1173        fn as_local(&self) -> Option<&dyn language::LocalFile> {
1174            todo!()
1175        }
1176
1177        fn mtime(&self) -> std::time::SystemTime {
1178            todo!()
1179        }
1180
1181        fn file_name<'a>(&'a self, _: &'a gpui::AppContext) -> &'a std::ffi::OsStr {
1182            todo!()
1183        }
1184
1185        fn is_deleted(&self) -> bool {
1186            todo!()
1187        }
1188
1189        fn save(
1190            &self,
1191            _: u64,
1192            _: language::Rope,
1193            _: clock::Global,
1194            _: project::LineEnding,
1195            _: &mut MutableAppContext,
1196        ) -> gpui::Task<anyhow::Result<(clock::Global, String, std::time::SystemTime)>> {
1197            todo!()
1198        }
1199
1200        fn as_any(&self) -> &dyn std::any::Any {
1201            todo!()
1202        }
1203
1204        fn to_proto(&self) -> rpc::proto::File {
1205            todo!()
1206        }
1207    }
1208}