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