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