1mod blink_manager;
2pub mod display_map;
3mod editor_settings;
4mod element;
5mod inlay_hint_cache;
6
7mod git;
8mod highlight_matching_bracket;
9mod hover_popover;
10pub mod items;
11mod link_go_to_definition;
12mod mouse_context_menu;
13pub mod movement;
14mod persistence;
15mod rust_analyzer_ext;
16pub mod scroll;
17pub mod selections_collection;
18
19#[cfg(test)]
20mod editor_tests;
21#[cfg(any(test, feature = "test-support"))]
22pub mod test;
23use ::git::diff::DiffHunk;
24use aho_corasick::AhoCorasick;
25use anyhow::{anyhow, Context as _, Result};
26use blink_manager::BlinkManager;
27use client::{Client, Collaborator, ParticipantIndex};
28use clock::ReplicaId;
29use collections::{BTreeMap, Bound, HashMap, HashSet, VecDeque};
30use convert_case::{Case, Casing};
31use copilot::Copilot;
32pub use display_map::DisplayPoint;
33use display_map::*;
34pub use editor_settings::EditorSettings;
35pub use element::{
36 Cursor, EditorElement, HighlightedRange, HighlightedRangeLine, LineWithInvisibles,
37};
38use futures::FutureExt;
39use fuzzy::{StringMatch, StringMatchCandidate};
40use git::diff_hunk_to_display;
41use gpui::{
42 actions, div, impl_actions, point, prelude::*, px, relative, rems, size, uniform_list, Action,
43 AnyElement, AppContext, AsyncWindowContext, BackgroundExecutor, Bounds, ClipboardItem, Context,
44 DispatchPhase, ElementId, EventEmitter, FocusHandle, FocusableView, FontStyle, FontWeight,
45 HighlightStyle, Hsla, InputHandler, InteractiveText, KeyContext, Model, MouseButton,
46 ParentElement, Pixels, Render, SharedString, Styled, StyledText, Subscription, Task, TextStyle,
47 UniformListScrollHandle, View, ViewContext, VisualContext, WeakView, WhiteSpace, WindowContext,
48};
49use highlight_matching_bracket::refresh_matching_bracket_highlights;
50use hover_popover::{hide_hover, HoverState};
51use inlay_hint_cache::{InlayHintCache, InlaySplice, InvalidationStrategy};
52pub use items::MAX_TAB_TITLE_LEN;
53use itertools::Itertools;
54pub use language::{char_kind, CharKind};
55use language::{
56 language_settings::{self, all_language_settings, InlayHintSettings},
57 markdown, point_from_lsp, AutoindentMode, BracketPair, Buffer, Capability, CodeAction,
58 CodeLabel, Completion, CursorShape, Diagnostic, Documentation, IndentKind, IndentSize,
59 Language, LanguageRegistry, LanguageServerName, OffsetRangeExt, Point, Selection,
60 SelectionGoal, TransactionId,
61};
62
63use link_go_to_definition::{GoToDefinitionLink, InlayHighlight, LinkGoToDefinitionState};
64use lsp::{DiagnosticSeverity, LanguageServerId};
65use mouse_context_menu::MouseContextMenu;
66use movement::TextLayoutDetails;
67use multi_buffer::ToOffsetUtf16;
68pub use multi_buffer::{
69 Anchor, AnchorRangeExt, ExcerptId, ExcerptRange, MultiBuffer, MultiBufferSnapshot, ToOffset,
70 ToPoint,
71};
72use ordered_float::OrderedFloat;
73use parking_lot::RwLock;
74use project::{FormatTrigger, Location, Project, ProjectPath, ProjectTransaction};
75use rand::prelude::*;
76use rpc::proto::{self, *};
77use scroll::{
78 autoscroll::Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, ScrollbarAutoHide,
79};
80use selections_collection::{resolve_multiple, MutableSelectionsCollection, SelectionsCollection};
81use serde::{Deserialize, Serialize};
82use settings::{Settings, SettingsStore};
83use smallvec::SmallVec;
84use snippet::Snippet;
85use std::{
86 any::TypeId,
87 borrow::Cow,
88 cmp::{self, Ordering, Reverse},
89 mem,
90 num::NonZeroU32,
91 ops::{ControlFlow, Deref, DerefMut, Range, RangeInclusive},
92 path::Path,
93 sync::Arc,
94 sync::Weak,
95 time::{Duration, Instant},
96};
97pub use sum_tree::Bias;
98use sum_tree::TreeMap;
99use text::{OffsetUtf16, Rope};
100use theme::{ActiveTheme, PlayerColor, StatusColors, SyntaxTheme, ThemeColors, ThemeSettings};
101use ui::{
102 h_flex, prelude::*, ButtonSize, ButtonStyle, IconButton, IconName, IconSize, ListItem, Popover,
103 Tooltip,
104};
105use util::{post_inc, RangeExt, ResultExt, TryFutureExt};
106use workspace::{searchable::SearchEvent, ItemNavHistory, Pane, SplitDirection, ViewId, Workspace};
107
108const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500);
109const MAX_LINE_LEN: usize = 1024;
110const MIN_NAVIGATION_HISTORY_ROW_DELTA: i64 = 10;
111const MAX_SELECTION_HISTORY_LEN: usize = 1024;
112const COPILOT_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(75);
113pub const CODE_ACTIONS_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(250);
114pub const DOCUMENT_HIGHLIGHTS_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(75);
115
116pub const FORMAT_TIMEOUT: Duration = Duration::from_secs(2);
117
118pub fn render_parsed_markdown(
119 element_id: impl Into<ElementId>,
120 parsed: &language::ParsedMarkdown,
121 editor_style: &EditorStyle,
122 workspace: Option<WeakView<Workspace>>,
123 cx: &mut ViewContext<Editor>,
124) -> InteractiveText {
125 let code_span_background_color = cx
126 .theme()
127 .colors()
128 .editor_document_highlight_read_background;
129
130 let highlights = gpui::combine_highlights(
131 parsed.highlights.iter().filter_map(|(range, highlight)| {
132 let highlight = highlight.to_highlight_style(&editor_style.syntax)?;
133 Some((range.clone(), highlight))
134 }),
135 parsed
136 .regions
137 .iter()
138 .zip(&parsed.region_ranges)
139 .filter_map(|(region, range)| {
140 if region.code {
141 Some((
142 range.clone(),
143 HighlightStyle {
144 background_color: Some(code_span_background_color),
145 ..Default::default()
146 },
147 ))
148 } else {
149 None
150 }
151 }),
152 );
153
154 let mut links = Vec::new();
155 let mut link_ranges = Vec::new();
156 for (range, region) in parsed.region_ranges.iter().zip(&parsed.regions) {
157 if let Some(link) = region.link.clone() {
158 links.push(link);
159 link_ranges.push(range.clone());
160 }
161 }
162
163 InteractiveText::new(
164 element_id,
165 StyledText::new(parsed.text.clone()).with_highlights(&editor_style.text, highlights),
166 )
167 .on_click(link_ranges, move |clicked_range_ix, cx| {
168 match &links[clicked_range_ix] {
169 markdown::Link::Web { url } => cx.open_url(url),
170 markdown::Link::Path { path } => {
171 if let Some(workspace) = &workspace {
172 _ = workspace.update(cx, |workspace, cx| {
173 workspace.open_abs_path(path.clone(), false, cx).detach();
174 });
175 }
176 }
177 }
178 })
179}
180
181#[derive(PartialEq, Clone, Deserialize, Default)]
182pub struct SelectNext {
183 #[serde(default)]
184 pub replace_newest: bool,
185}
186
187#[derive(PartialEq, Clone, Deserialize, Default)]
188pub struct SelectPrevious {
189 #[serde(default)]
190 pub replace_newest: bool,
191}
192
193#[derive(PartialEq, Clone, Deserialize, Default)]
194pub struct SelectAllMatches {
195 #[serde(default)]
196 pub replace_newest: bool,
197}
198
199#[derive(PartialEq, Clone, Deserialize, Default)]
200pub struct SelectToBeginningOfLine {
201 #[serde(default)]
202 stop_at_soft_wraps: bool,
203}
204
205#[derive(PartialEq, Clone, Deserialize, Default)]
206pub struct MovePageUp {
207 #[serde(default)]
208 center_cursor: bool,
209}
210
211#[derive(PartialEq, Clone, Deserialize, Default)]
212pub struct MovePageDown {
213 #[serde(default)]
214 center_cursor: bool,
215}
216
217#[derive(PartialEq, Clone, Deserialize, Default)]
218pub struct SelectToEndOfLine {
219 #[serde(default)]
220 stop_at_soft_wraps: bool,
221}
222
223#[derive(PartialEq, Clone, Deserialize, Default)]
224pub struct ToggleCodeActions {
225 #[serde(default)]
226 pub deployed_from_indicator: bool,
227}
228
229#[derive(PartialEq, Clone, Deserialize, Default)]
230pub struct ConfirmCompletion {
231 #[serde(default)]
232 pub item_ix: Option<usize>,
233}
234
235#[derive(PartialEq, Clone, Deserialize, Default)]
236pub struct ConfirmCodeAction {
237 #[serde(default)]
238 pub item_ix: Option<usize>,
239}
240
241#[derive(PartialEq, Clone, Deserialize, Default)]
242pub struct ToggleComments {
243 #[serde(default)]
244 pub advance_downwards: bool,
245}
246
247#[derive(PartialEq, Clone, Deserialize, Default)]
248pub struct FoldAt {
249 pub buffer_row: u32,
250}
251
252#[derive(PartialEq, Clone, Deserialize, Default)]
253pub struct UnfoldAt {
254 pub buffer_row: u32,
255}
256
257impl_actions!(
258 editor,
259 [
260 SelectNext,
261 SelectPrevious,
262 SelectAllMatches,
263 SelectToBeginningOfLine,
264 MovePageUp,
265 MovePageDown,
266 SelectToEndOfLine,
267 ToggleCodeActions,
268 ConfirmCompletion,
269 ConfirmCodeAction,
270 ToggleComments,
271 FoldAt,
272 UnfoldAt
273 ]
274);
275
276#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
277pub enum InlayId {
278 Suggestion(usize),
279 Hint(usize),
280}
281
282impl InlayId {
283 fn id(&self) -> usize {
284 match self {
285 Self::Suggestion(id) => *id,
286 Self::Hint(id) => *id,
287 }
288 }
289}
290
291actions!(
292 editor,
293 [
294 AddSelectionAbove,
295 AddSelectionBelow,
296 Backspace,
297 Cancel,
298 ConfirmRename,
299 ContextMenuFirst,
300 ContextMenuLast,
301 ContextMenuNext,
302 ContextMenuPrev,
303 ConvertToKebabCase,
304 ConvertToLowerCamelCase,
305 ConvertToLowerCase,
306 ConvertToSnakeCase,
307 ConvertToTitleCase,
308 ConvertToUpperCamelCase,
309 ConvertToUpperCase,
310 Copy,
311 CopyHighlightJson,
312 CopyPath,
313 CopyRelativePath,
314 Cut,
315 CutToEndOfLine,
316 Delete,
317 DeleteLine,
318 DeleteToBeginningOfLine,
319 DeleteToEndOfLine,
320 DeleteToNextSubwordEnd,
321 DeleteToNextWordEnd,
322 DeleteToPreviousSubwordStart,
323 DeleteToPreviousWordStart,
324 DuplicateLine,
325 ExpandMacroRecursively,
326 FindAllReferences,
327 Fold,
328 FoldSelectedRanges,
329 Format,
330 GoToDefinition,
331 GoToDefinitionSplit,
332 GoToDiagnostic,
333 GoToHunk,
334 GoToPrevDiagnostic,
335 GoToPrevHunk,
336 GoToTypeDefinition,
337 GoToTypeDefinitionSplit,
338 HalfPageDown,
339 HalfPageUp,
340 Hover,
341 Indent,
342 JoinLines,
343 LineDown,
344 LineUp,
345 MoveDown,
346 MoveLeft,
347 MoveLineDown,
348 MoveLineUp,
349 MoveRight,
350 MoveToBeginning,
351 MoveToBeginningOfLine,
352 MoveToEnclosingBracket,
353 MoveToEnd,
354 MoveToEndOfLine,
355 MoveToEndOfParagraph,
356 MoveToNextSubwordEnd,
357 MoveToNextWordEnd,
358 MoveToPreviousSubwordStart,
359 MoveToPreviousWordStart,
360 MoveToStartOfParagraph,
361 MoveUp,
362 Newline,
363 NewlineAbove,
364 NewlineBelow,
365 NextScreen,
366 OpenExcerpts,
367 Outdent,
368 PageDown,
369 PageUp,
370 Paste,
371 Redo,
372 RedoSelection,
373 Rename,
374 RestartLanguageServer,
375 RevealInFinder,
376 ReverseLines,
377 ScrollCursorBottom,
378 ScrollCursorCenter,
379 ScrollCursorTop,
380 SelectAll,
381 SelectDown,
382 SelectLargerSyntaxNode,
383 SelectLeft,
384 SelectLine,
385 SelectRight,
386 SelectSmallerSyntaxNode,
387 SelectToBeginning,
388 SelectToEnd,
389 SelectToEndOfParagraph,
390 SelectToNextSubwordEnd,
391 SelectToNextWordEnd,
392 SelectToPreviousSubwordStart,
393 SelectToPreviousWordStart,
394 SelectToStartOfParagraph,
395 SelectUp,
396 ShowCharacterPalette,
397 ShowCompletions,
398 ShuffleLines,
399 SortLinesCaseInsensitive,
400 SortLinesCaseSensitive,
401 SplitSelectionIntoLines,
402 Tab,
403 TabPrev,
404 ToggleInlayHints,
405 ToggleSoftWrap,
406 Transpose,
407 Undo,
408 UndoSelection,
409 UnfoldLines,
410 ]
411);
412
413enum DocumentHighlightRead {}
414enum DocumentHighlightWrite {}
415enum InputComposition {}
416
417#[derive(Copy, Clone, PartialEq, Eq)]
418pub enum Direction {
419 Prev,
420 Next,
421}
422
423pub fn init_settings(cx: &mut AppContext) {
424 EditorSettings::register(cx);
425}
426
427pub fn init(cx: &mut AppContext) {
428 init_settings(cx);
429
430 workspace::register_project_item::<Editor>(cx);
431 workspace::register_followable_item::<Editor>(cx);
432 workspace::register_deserializable_item::<Editor>(cx);
433 cx.observe_new_views(
434 |workspace: &mut Workspace, _cx: &mut ViewContext<Workspace>| {
435 workspace.register_action(Editor::new_file);
436 workspace.register_action(Editor::new_file_in_direction);
437 },
438 )
439 .detach();
440
441 cx.on_action(move |_: &workspace::NewFile, cx| {
442 let app_state = cx.global::<Weak<workspace::AppState>>();
443 if let Some(app_state) = app_state.upgrade() {
444 workspace::open_new(&app_state, cx, |workspace, cx| {
445 Editor::new_file(workspace, &Default::default(), cx)
446 })
447 .detach();
448 }
449 });
450 cx.on_action(move |_: &workspace::NewWindow, cx| {
451 let app_state = cx.global::<Weak<workspace::AppState>>();
452 if let Some(app_state) = app_state.upgrade() {
453 workspace::open_new(&app_state, cx, |workspace, cx| {
454 Editor::new_file(workspace, &Default::default(), cx)
455 })
456 .detach();
457 }
458 });
459}
460
461trait InvalidationRegion {
462 fn ranges(&self) -> &[Range<Anchor>];
463}
464
465#[derive(Clone, Debug, PartialEq)]
466pub enum SelectPhase {
467 Begin {
468 position: DisplayPoint,
469 add: bool,
470 click_count: usize,
471 },
472 BeginColumnar {
473 position: DisplayPoint,
474 goal_column: u32,
475 },
476 Extend {
477 position: DisplayPoint,
478 click_count: usize,
479 },
480 Update {
481 position: DisplayPoint,
482 goal_column: u32,
483 scroll_position: gpui::Point<f32>,
484 },
485 End,
486}
487
488#[derive(Clone, Debug)]
489pub enum SelectMode {
490 Character,
491 Word(Range<Anchor>),
492 Line(Range<Anchor>),
493 All,
494}
495
496#[derive(Copy, Clone, PartialEq, Eq, Debug)]
497pub enum EditorMode {
498 SingleLine,
499 AutoHeight { max_lines: usize },
500 Full,
501}
502
503#[derive(Clone, Debug)]
504pub enum SoftWrap {
505 None,
506 EditorWidth,
507 Column(u32),
508}
509
510#[derive(Clone)]
511pub struct EditorStyle {
512 pub background: Hsla,
513 pub local_player: PlayerColor,
514 pub text: TextStyle,
515 pub scrollbar_width: Pixels,
516 pub syntax: Arc<SyntaxTheme>,
517 pub status: StatusColors,
518 pub inlays_style: HighlightStyle,
519 pub suggestions_style: HighlightStyle,
520}
521
522impl Default for EditorStyle {
523 fn default() -> Self {
524 Self {
525 background: Hsla::default(),
526 local_player: PlayerColor::default(),
527 text: TextStyle::default(),
528 scrollbar_width: Pixels::default(),
529 syntax: Default::default(),
530 // HACK: Status colors don't have a real default.
531 // We should look into removing the status colors from the editor
532 // style and retrieve them directly from the theme.
533 status: StatusColors::dark(),
534 inlays_style: HighlightStyle::default(),
535 suggestions_style: HighlightStyle::default(),
536 }
537 }
538}
539
540type CompletionId = usize;
541
542// type GetFieldEditorTheme = dyn Fn(&theme::Theme) -> theme::FieldEditor;
543// type OverrideTextStyle = dyn Fn(&EditorStyle) -> Option<HighlightStyle>;
544
545type BackgroundHighlight = (fn(&ThemeColors) -> Hsla, Vec<Range<Anchor>>);
546type InlayBackgroundHighlight = (fn(&ThemeColors) -> Hsla, Vec<InlayHighlight>);
547
548pub struct Editor {
549 handle: WeakView<Self>,
550 focus_handle: FocusHandle,
551 buffer: Model<MultiBuffer>,
552 display_map: Model<DisplayMap>,
553 pub selections: SelectionsCollection,
554 pub scroll_manager: ScrollManager,
555 columnar_selection_tail: Option<Anchor>,
556 add_selections_state: Option<AddSelectionsState>,
557 select_next_state: Option<SelectNextState>,
558 select_prev_state: Option<SelectNextState>,
559 selection_history: SelectionHistory,
560 autoclose_regions: Vec<AutocloseRegion>,
561 snippet_stack: InvalidationStack<SnippetState>,
562 select_larger_syntax_node_stack: Vec<Box<[Selection<usize>]>>,
563 ime_transaction: Option<TransactionId>,
564 active_diagnostics: Option<ActiveDiagnosticGroup>,
565 soft_wrap_mode_override: Option<language_settings::SoftWrap>,
566 project: Option<Model<Project>>,
567 collaboration_hub: Option<Box<dyn CollaborationHub>>,
568 blink_manager: Model<BlinkManager>,
569 pub show_local_selections: bool,
570 mode: EditorMode,
571 show_gutter: bool,
572 show_wrap_guides: Option<bool>,
573 placeholder_text: Option<Arc<str>>,
574 highlighted_rows: Option<Range<u32>>,
575 background_highlights: BTreeMap<TypeId, BackgroundHighlight>,
576 inlay_background_highlights: TreeMap<Option<TypeId>, InlayBackgroundHighlight>,
577 nav_history: Option<ItemNavHistory>,
578 context_menu: RwLock<Option<ContextMenu>>,
579 mouse_context_menu: Option<MouseContextMenu>,
580 completion_tasks: Vec<(CompletionId, Task<Option<()>>)>,
581 next_completion_id: CompletionId,
582 available_code_actions: Option<(Model<Buffer>, Arc<[CodeAction]>)>,
583 code_actions_task: Option<Task<()>>,
584 document_highlights_task: Option<Task<()>>,
585 pending_rename: Option<RenameState>,
586 searchable: bool,
587 cursor_shape: CursorShape,
588 collapse_matches: bool,
589 autoindent_mode: Option<AutoindentMode>,
590 workspace: Option<(WeakView<Workspace>, i64)>,
591 keymap_context_layers: BTreeMap<TypeId, KeyContext>,
592 input_enabled: bool,
593 read_only: bool,
594 leader_peer_id: Option<PeerId>,
595 remote_id: Option<ViewId>,
596 hover_state: HoverState,
597 gutter_hovered: bool,
598 link_go_to_definition_state: LinkGoToDefinitionState,
599 copilot_state: CopilotState,
600 inlay_hint_cache: InlayHintCache,
601 next_inlay_id: usize,
602 _subscriptions: Vec<Subscription>,
603 pixel_position_of_newest_cursor: Option<gpui::Point<Pixels>>,
604 gutter_width: Pixels,
605 style: Option<EditorStyle>,
606 editor_actions: Vec<Box<dyn Fn(&mut ViewContext<Self>)>>,
607 show_copilot_suggestions: bool,
608}
609
610pub struct EditorSnapshot {
611 pub mode: EditorMode,
612 pub show_gutter: bool,
613 pub display_snapshot: DisplaySnapshot,
614 pub placeholder_text: Option<Arc<str>>,
615 is_focused: bool,
616 scroll_anchor: ScrollAnchor,
617 ongoing_scroll: OngoingScroll,
618}
619
620pub struct RemoteSelection {
621 pub replica_id: ReplicaId,
622 pub selection: Selection<Anchor>,
623 pub cursor_shape: CursorShape,
624 pub peer_id: PeerId,
625 pub line_mode: bool,
626 pub participant_index: Option<ParticipantIndex>,
627}
628
629#[derive(Clone, Debug)]
630struct SelectionHistoryEntry {
631 selections: Arc<[Selection<Anchor>]>,
632 select_next_state: Option<SelectNextState>,
633 select_prev_state: Option<SelectNextState>,
634 add_selections_state: Option<AddSelectionsState>,
635}
636
637enum SelectionHistoryMode {
638 Normal,
639 Undoing,
640 Redoing,
641}
642
643impl Default for SelectionHistoryMode {
644 fn default() -> Self {
645 Self::Normal
646 }
647}
648
649#[derive(Default)]
650struct SelectionHistory {
651 #[allow(clippy::type_complexity)]
652 selections_by_transaction:
653 HashMap<TransactionId, (Arc<[Selection<Anchor>]>, Option<Arc<[Selection<Anchor>]>>)>,
654 mode: SelectionHistoryMode,
655 undo_stack: VecDeque<SelectionHistoryEntry>,
656 redo_stack: VecDeque<SelectionHistoryEntry>,
657}
658
659impl SelectionHistory {
660 fn insert_transaction(
661 &mut self,
662 transaction_id: TransactionId,
663 selections: Arc<[Selection<Anchor>]>,
664 ) {
665 self.selections_by_transaction
666 .insert(transaction_id, (selections, None));
667 }
668
669 #[allow(clippy::type_complexity)]
670 fn transaction(
671 &self,
672 transaction_id: TransactionId,
673 ) -> Option<&(Arc<[Selection<Anchor>]>, Option<Arc<[Selection<Anchor>]>>)> {
674 self.selections_by_transaction.get(&transaction_id)
675 }
676
677 #[allow(clippy::type_complexity)]
678 fn transaction_mut(
679 &mut self,
680 transaction_id: TransactionId,
681 ) -> Option<&mut (Arc<[Selection<Anchor>]>, Option<Arc<[Selection<Anchor>]>>)> {
682 self.selections_by_transaction.get_mut(&transaction_id)
683 }
684
685 fn push(&mut self, entry: SelectionHistoryEntry) {
686 if !entry.selections.is_empty() {
687 match self.mode {
688 SelectionHistoryMode::Normal => {
689 self.push_undo(entry);
690 self.redo_stack.clear();
691 }
692 SelectionHistoryMode::Undoing => self.push_redo(entry),
693 SelectionHistoryMode::Redoing => self.push_undo(entry),
694 }
695 }
696 }
697
698 fn push_undo(&mut self, entry: SelectionHistoryEntry) {
699 if self
700 .undo_stack
701 .back()
702 .map_or(true, |e| e.selections != entry.selections)
703 {
704 self.undo_stack.push_back(entry);
705 if self.undo_stack.len() > MAX_SELECTION_HISTORY_LEN {
706 self.undo_stack.pop_front();
707 }
708 }
709 }
710
711 fn push_redo(&mut self, entry: SelectionHistoryEntry) {
712 if self
713 .redo_stack
714 .back()
715 .map_or(true, |e| e.selections != entry.selections)
716 {
717 self.redo_stack.push_back(entry);
718 if self.redo_stack.len() > MAX_SELECTION_HISTORY_LEN {
719 self.redo_stack.pop_front();
720 }
721 }
722 }
723}
724
725#[derive(Clone, Debug)]
726struct AddSelectionsState {
727 above: bool,
728 stack: Vec<usize>,
729}
730
731#[derive(Clone)]
732struct SelectNextState {
733 query: AhoCorasick,
734 wordwise: bool,
735 done: bool,
736}
737
738impl std::fmt::Debug for SelectNextState {
739 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
740 f.debug_struct(std::any::type_name::<Self>())
741 .field("wordwise", &self.wordwise)
742 .field("done", &self.done)
743 .finish()
744 }
745}
746
747#[derive(Debug)]
748struct AutocloseRegion {
749 selection_id: usize,
750 range: Range<Anchor>,
751 pair: BracketPair,
752}
753
754#[derive(Debug)]
755struct SnippetState {
756 ranges: Vec<Vec<Range<Anchor>>>,
757 active_index: usize,
758}
759
760pub struct RenameState {
761 pub range: Range<Anchor>,
762 pub old_name: Arc<str>,
763 pub editor: View<Editor>,
764 block_id: BlockId,
765}
766
767struct InvalidationStack<T>(Vec<T>);
768
769enum ContextMenu {
770 Completions(CompletionsMenu),
771 CodeActions(CodeActionsMenu),
772}
773
774impl ContextMenu {
775 fn select_first(
776 &mut self,
777 project: Option<&Model<Project>>,
778 cx: &mut ViewContext<Editor>,
779 ) -> bool {
780 if self.visible() {
781 match self {
782 ContextMenu::Completions(menu) => menu.select_first(project, cx),
783 ContextMenu::CodeActions(menu) => menu.select_first(cx),
784 }
785 true
786 } else {
787 false
788 }
789 }
790
791 fn select_prev(
792 &mut self,
793 project: Option<&Model<Project>>,
794 cx: &mut ViewContext<Editor>,
795 ) -> bool {
796 if self.visible() {
797 match self {
798 ContextMenu::Completions(menu) => menu.select_prev(project, cx),
799 ContextMenu::CodeActions(menu) => menu.select_prev(cx),
800 }
801 true
802 } else {
803 false
804 }
805 }
806
807 fn select_next(
808 &mut self,
809 project: Option<&Model<Project>>,
810 cx: &mut ViewContext<Editor>,
811 ) -> bool {
812 if self.visible() {
813 match self {
814 ContextMenu::Completions(menu) => menu.select_next(project, cx),
815 ContextMenu::CodeActions(menu) => menu.select_next(cx),
816 }
817 true
818 } else {
819 false
820 }
821 }
822
823 fn select_last(
824 &mut self,
825 project: Option<&Model<Project>>,
826 cx: &mut ViewContext<Editor>,
827 ) -> bool {
828 if self.visible() {
829 match self {
830 ContextMenu::Completions(menu) => menu.select_last(project, cx),
831 ContextMenu::CodeActions(menu) => menu.select_last(cx),
832 }
833 true
834 } else {
835 false
836 }
837 }
838
839 fn visible(&self) -> bool {
840 match self {
841 ContextMenu::Completions(menu) => menu.visible(),
842 ContextMenu::CodeActions(menu) => menu.visible(),
843 }
844 }
845
846 fn render(
847 &self,
848 cursor_position: DisplayPoint,
849 style: &EditorStyle,
850 max_height: Pixels,
851 workspace: Option<WeakView<Workspace>>,
852 cx: &mut ViewContext<Editor>,
853 ) -> (DisplayPoint, AnyElement) {
854 match self {
855 ContextMenu::Completions(menu) => (
856 cursor_position,
857 menu.render(style, max_height, workspace, cx),
858 ),
859 ContextMenu::CodeActions(menu) => menu.render(cursor_position, style, max_height, cx),
860 }
861 }
862}
863
864#[derive(Clone)]
865struct CompletionsMenu {
866 id: CompletionId,
867 initial_position: Anchor,
868 buffer: Model<Buffer>,
869 completions: Arc<RwLock<Box<[Completion]>>>,
870 match_candidates: Arc<[StringMatchCandidate]>,
871 matches: Arc<[StringMatch]>,
872 selected_item: usize,
873 scroll_handle: UniformListScrollHandle,
874}
875
876impl CompletionsMenu {
877 fn select_first(&mut self, project: Option<&Model<Project>>, cx: &mut ViewContext<Editor>) {
878 self.selected_item = 0;
879 self.scroll_handle.scroll_to_item(self.selected_item);
880 self.attempt_resolve_selected_completion_documentation(project, cx);
881 cx.notify();
882 }
883
884 fn select_prev(&mut self, project: Option<&Model<Project>>, cx: &mut ViewContext<Editor>) {
885 if self.selected_item > 0 {
886 self.selected_item -= 1;
887 } else {
888 self.selected_item = self.matches.len() - 1;
889 }
890 self.scroll_handle.scroll_to_item(self.selected_item);
891 self.attempt_resolve_selected_completion_documentation(project, cx);
892 cx.notify();
893 }
894
895 fn select_next(&mut self, project: Option<&Model<Project>>, cx: &mut ViewContext<Editor>) {
896 if self.selected_item + 1 < self.matches.len() {
897 self.selected_item += 1;
898 } else {
899 self.selected_item = 0;
900 }
901 self.scroll_handle.scroll_to_item(self.selected_item);
902 self.attempt_resolve_selected_completion_documentation(project, cx);
903 cx.notify();
904 }
905
906 fn select_last(&mut self, project: Option<&Model<Project>>, cx: &mut ViewContext<Editor>) {
907 self.selected_item = self.matches.len() - 1;
908 self.scroll_handle.scroll_to_item(self.selected_item);
909 self.attempt_resolve_selected_completion_documentation(project, cx);
910 cx.notify();
911 }
912
913 fn pre_resolve_completion_documentation(
914 &self,
915 editor: &Editor,
916 cx: &mut ViewContext<Editor>,
917 ) -> Option<Task<()>> {
918 let settings = EditorSettings::get_global(cx);
919 if !settings.show_completion_documentation {
920 return None;
921 }
922
923 let Some(project) = editor.project.clone() else {
924 return None;
925 };
926
927 let client = project.read(cx).client();
928 let language_registry = project.read(cx).languages().clone();
929
930 let is_remote = project.read(cx).is_remote();
931 let project_id = project.read(cx).remote_id();
932
933 let completions = self.completions.clone();
934 let completion_indices: Vec<_> = self.matches.iter().map(|m| m.candidate_id).collect();
935
936 Some(cx.spawn(move |this, mut cx| async move {
937 if is_remote {
938 let Some(project_id) = project_id else {
939 log::error!("Remote project without remote_id");
940 return;
941 };
942
943 for completion_index in completion_indices {
944 let completions_guard = completions.read();
945 let completion = &completions_guard[completion_index];
946 if completion.documentation.is_some() {
947 continue;
948 }
949
950 let server_id = completion.server_id;
951 let completion = completion.lsp_completion.clone();
952 drop(completions_guard);
953
954 Self::resolve_completion_documentation_remote(
955 project_id,
956 server_id,
957 completions.clone(),
958 completion_index,
959 completion,
960 client.clone(),
961 language_registry.clone(),
962 )
963 .await;
964
965 _ = this.update(&mut cx, |_, cx| cx.notify());
966 }
967 } else {
968 for completion_index in completion_indices {
969 let completions_guard = completions.read();
970 let completion = &completions_guard[completion_index];
971 if completion.documentation.is_some() {
972 continue;
973 }
974
975 let server_id = completion.server_id;
976 let completion = completion.lsp_completion.clone();
977 drop(completions_guard);
978
979 let server = project
980 .read_with(&mut cx, |project, _| {
981 project.language_server_for_id(server_id)
982 })
983 .ok()
984 .flatten();
985 let Some(server) = server else {
986 return;
987 };
988
989 Self::resolve_completion_documentation_local(
990 server,
991 completions.clone(),
992 completion_index,
993 completion,
994 language_registry.clone(),
995 )
996 .await;
997
998 _ = this.update(&mut cx, |_, cx| cx.notify());
999 }
1000 }
1001 }))
1002 }
1003
1004 fn attempt_resolve_selected_completion_documentation(
1005 &mut self,
1006 project: Option<&Model<Project>>,
1007 cx: &mut ViewContext<Editor>,
1008 ) {
1009 let settings = EditorSettings::get_global(cx);
1010 if !settings.show_completion_documentation {
1011 return;
1012 }
1013
1014 let completion_index = self.matches[self.selected_item].candidate_id;
1015 let Some(project) = project else {
1016 return;
1017 };
1018 let language_registry = project.read(cx).languages().clone();
1019
1020 let completions = self.completions.clone();
1021 let completions_guard = completions.read();
1022 let completion = &completions_guard[completion_index];
1023 if completion.documentation.is_some() {
1024 return;
1025 }
1026
1027 let server_id = completion.server_id;
1028 let completion = completion.lsp_completion.clone();
1029 drop(completions_guard);
1030
1031 if project.read(cx).is_remote() {
1032 let Some(project_id) = project.read(cx).remote_id() else {
1033 log::error!("Remote project without remote_id");
1034 return;
1035 };
1036
1037 let client = project.read(cx).client();
1038
1039 cx.spawn(move |this, mut cx| async move {
1040 Self::resolve_completion_documentation_remote(
1041 project_id,
1042 server_id,
1043 completions.clone(),
1044 completion_index,
1045 completion,
1046 client,
1047 language_registry.clone(),
1048 )
1049 .await;
1050
1051 _ = this.update(&mut cx, |_, cx| cx.notify());
1052 })
1053 .detach();
1054 } else {
1055 let Some(server) = project.read(cx).language_server_for_id(server_id) else {
1056 return;
1057 };
1058
1059 cx.spawn(move |this, mut cx| async move {
1060 Self::resolve_completion_documentation_local(
1061 server,
1062 completions,
1063 completion_index,
1064 completion,
1065 language_registry,
1066 )
1067 .await;
1068
1069 _ = this.update(&mut cx, |_, cx| cx.notify());
1070 })
1071 .detach();
1072 }
1073 }
1074
1075 async fn resolve_completion_documentation_remote(
1076 project_id: u64,
1077 server_id: LanguageServerId,
1078 completions: Arc<RwLock<Box<[Completion]>>>,
1079 completion_index: usize,
1080 completion: lsp::CompletionItem,
1081 client: Arc<Client>,
1082 language_registry: Arc<LanguageRegistry>,
1083 ) {
1084 let request = proto::ResolveCompletionDocumentation {
1085 project_id,
1086 language_server_id: server_id.0 as u64,
1087 lsp_completion: serde_json::to_string(&completion).unwrap().into_bytes(),
1088 };
1089
1090 let Some(response) = client
1091 .request(request)
1092 .await
1093 .context("completion documentation resolve proto request")
1094 .log_err()
1095 else {
1096 return;
1097 };
1098
1099 if response.text.is_empty() {
1100 let mut completions = completions.write();
1101 let completion = &mut completions[completion_index];
1102 completion.documentation = Some(Documentation::Undocumented);
1103 }
1104
1105 let documentation = if response.is_markdown {
1106 Documentation::MultiLineMarkdown(
1107 markdown::parse_markdown(&response.text, &language_registry, None).await,
1108 )
1109 } else if response.text.lines().count() <= 1 {
1110 Documentation::SingleLine(response.text)
1111 } else {
1112 Documentation::MultiLinePlainText(response.text)
1113 };
1114
1115 let mut completions = completions.write();
1116 let completion = &mut completions[completion_index];
1117 completion.documentation = Some(documentation);
1118 }
1119
1120 async fn resolve_completion_documentation_local(
1121 server: Arc<lsp::LanguageServer>,
1122 completions: Arc<RwLock<Box<[Completion]>>>,
1123 completion_index: usize,
1124 completion: lsp::CompletionItem,
1125 language_registry: Arc<LanguageRegistry>,
1126 ) {
1127 let can_resolve = server
1128 .capabilities()
1129 .completion_provider
1130 .as_ref()
1131 .and_then(|options| options.resolve_provider)
1132 .unwrap_or(false);
1133 if !can_resolve {
1134 return;
1135 }
1136
1137 let request = server.request::<lsp::request::ResolveCompletionItem>(completion);
1138 let Some(completion_item) = request.await.log_err() else {
1139 return;
1140 };
1141
1142 if let Some(lsp_documentation) = completion_item.documentation {
1143 let documentation = language::prepare_completion_documentation(
1144 &lsp_documentation,
1145 &language_registry,
1146 None, // TODO: Try to reasonably work out which language the completion is for
1147 )
1148 .await;
1149
1150 let mut completions = completions.write();
1151 let completion = &mut completions[completion_index];
1152 completion.documentation = Some(documentation);
1153 } else {
1154 let mut completions = completions.write();
1155 let completion = &mut completions[completion_index];
1156 completion.documentation = Some(Documentation::Undocumented);
1157 }
1158 }
1159
1160 fn visible(&self) -> bool {
1161 !self.matches.is_empty()
1162 }
1163
1164 fn render(
1165 &self,
1166 style: &EditorStyle,
1167 max_height: Pixels,
1168 workspace: Option<WeakView<Workspace>>,
1169 cx: &mut ViewContext<Editor>,
1170 ) -> AnyElement {
1171 let settings = EditorSettings::get_global(cx);
1172 let show_completion_documentation = settings.show_completion_documentation;
1173
1174 let widest_completion_ix = self
1175 .matches
1176 .iter()
1177 .enumerate()
1178 .max_by_key(|(_, mat)| {
1179 let completions = self.completions.read();
1180 let completion = &completions[mat.candidate_id];
1181 let documentation = &completion.documentation;
1182
1183 let mut len = completion.label.text.chars().count();
1184 if let Some(Documentation::SingleLine(text)) = documentation {
1185 if show_completion_documentation {
1186 len += text.chars().count();
1187 }
1188 }
1189
1190 len
1191 })
1192 .map(|(ix, _)| ix);
1193
1194 let completions = self.completions.clone();
1195 let matches = self.matches.clone();
1196 let selected_item = self.selected_item;
1197 let style = style.clone();
1198
1199 let multiline_docs = {
1200 let mat = &self.matches[selected_item];
1201 let multiline_docs = match &self.completions.read()[mat.candidate_id].documentation {
1202 Some(Documentation::MultiLinePlainText(text)) => {
1203 Some(div().child(SharedString::from(text.clone())))
1204 }
1205 Some(Documentation::MultiLineMarkdown(parsed)) => Some(div().child(
1206 render_parsed_markdown("completions_markdown", parsed, &style, workspace, cx),
1207 )),
1208 _ => None,
1209 };
1210 multiline_docs.map(|div| {
1211 div.id("multiline_docs")
1212 .max_h(max_height)
1213 .flex_1()
1214 .px_1p5()
1215 .py_1()
1216 .min_w(px(260.))
1217 .max_w(px(640.))
1218 .w(px(500.))
1219 .overflow_y_scroll()
1220 // Prevent a mouse down on documentation from being propagated to the editor,
1221 // because that would move the cursor.
1222 .on_mouse_down(MouseButton::Left, |_, cx| cx.stop_propagation())
1223 })
1224 };
1225
1226 let list = uniform_list(
1227 cx.view().clone(),
1228 "completions",
1229 matches.len(),
1230 move |_editor, range, cx| {
1231 let start_ix = range.start;
1232 let completions_guard = completions.read();
1233
1234 matches[range]
1235 .iter()
1236 .enumerate()
1237 .map(|(ix, mat)| {
1238 let item_ix = start_ix + ix;
1239 let candidate_id = mat.candidate_id;
1240 let completion = &completions_guard[candidate_id];
1241
1242 let documentation = if show_completion_documentation {
1243 &completion.documentation
1244 } else {
1245 &None
1246 };
1247
1248 let highlights = gpui::combine_highlights(
1249 mat.ranges().map(|range| (range, FontWeight::BOLD.into())),
1250 styled_runs_for_code_label(&completion.label, &style.syntax).map(
1251 |(range, mut highlight)| {
1252 // Ignore font weight for syntax highlighting, as we'll use it
1253 // for fuzzy matches.
1254 highlight.font_weight = None;
1255 (range, highlight)
1256 },
1257 ),
1258 );
1259 let completion_label = StyledText::new(completion.label.text.clone())
1260 .with_highlights(&style.text, highlights);
1261 let documentation_label =
1262 if let Some(Documentation::SingleLine(text)) = documentation {
1263 if text.trim().is_empty() {
1264 None
1265 } else {
1266 Some(
1267 h_flex().ml_4().child(
1268 Label::new(text.clone())
1269 .size(LabelSize::Small)
1270 .color(Color::Muted),
1271 ),
1272 )
1273 }
1274 } else {
1275 None
1276 };
1277
1278 div().min_w(px(220.)).max_w(px(540.)).child(
1279 ListItem::new(mat.candidate_id)
1280 .inset(true)
1281 .selected(item_ix == selected_item)
1282 .on_click(cx.listener(move |editor, _event, cx| {
1283 cx.stop_propagation();
1284 editor
1285 .confirm_completion(
1286 &ConfirmCompletion {
1287 item_ix: Some(item_ix),
1288 },
1289 cx,
1290 )
1291 .map(|task| task.detach_and_log_err(cx));
1292 }))
1293 .child(h_flex().overflow_hidden().child(completion_label))
1294 .end_slot::<Div>(documentation_label),
1295 )
1296 })
1297 .collect()
1298 },
1299 )
1300 .max_h(max_height)
1301 .track_scroll(self.scroll_handle.clone())
1302 .with_width_from_item(widest_completion_ix);
1303
1304 Popover::new()
1305 .child(list)
1306 .when_some(multiline_docs, |popover, multiline_docs| {
1307 popover.aside(multiline_docs)
1308 })
1309 .into_any_element()
1310 }
1311
1312 pub async fn filter(&mut self, query: Option<&str>, executor: BackgroundExecutor) {
1313 let mut matches = if let Some(query) = query {
1314 fuzzy::match_strings(
1315 &self.match_candidates,
1316 query,
1317 query.chars().any(|c| c.is_uppercase()),
1318 100,
1319 &Default::default(),
1320 executor,
1321 )
1322 .await
1323 } else {
1324 self.match_candidates
1325 .iter()
1326 .enumerate()
1327 .map(|(candidate_id, candidate)| StringMatch {
1328 candidate_id,
1329 score: Default::default(),
1330 positions: Default::default(),
1331 string: candidate.string.clone(),
1332 })
1333 .collect()
1334 };
1335
1336 // Remove all candidates where the query's start does not match the start of any word in the candidate
1337 if let Some(query) = query {
1338 if let Some(query_start) = query.chars().next() {
1339 matches.retain(|string_match| {
1340 split_words(&string_match.string).any(|word| {
1341 // Check that the first codepoint of the word as lowercase matches the first
1342 // codepoint of the query as lowercase
1343 word.chars()
1344 .flat_map(|codepoint| codepoint.to_lowercase())
1345 .zip(query_start.to_lowercase())
1346 .all(|(word_cp, query_cp)| word_cp == query_cp)
1347 })
1348 });
1349 }
1350 }
1351
1352 let completions = self.completions.read();
1353 matches.sort_unstable_by_key(|mat| {
1354 let completion = &completions[mat.candidate_id];
1355 (
1356 completion.lsp_completion.sort_text.as_ref(),
1357 Reverse(OrderedFloat(mat.score)),
1358 completion.sort_key(),
1359 )
1360 });
1361
1362 for mat in &mut matches {
1363 let completion = &completions[mat.candidate_id];
1364 mat.string = completion.label.text.clone();
1365 for position in &mut mat.positions {
1366 *position += completion.label.filter_range.start;
1367 }
1368 }
1369 drop(completions);
1370
1371 self.matches = matches.into();
1372 self.selected_item = 0;
1373 }
1374}
1375
1376#[derive(Clone)]
1377struct CodeActionsMenu {
1378 actions: Arc<[CodeAction]>,
1379 buffer: Model<Buffer>,
1380 selected_item: usize,
1381 scroll_handle: UniformListScrollHandle,
1382 deployed_from_indicator: bool,
1383}
1384
1385impl CodeActionsMenu {
1386 fn select_first(&mut self, cx: &mut ViewContext<Editor>) {
1387 self.selected_item = 0;
1388 self.scroll_handle.scroll_to_item(self.selected_item);
1389 cx.notify()
1390 }
1391
1392 fn select_prev(&mut self, cx: &mut ViewContext<Editor>) {
1393 if self.selected_item > 0 {
1394 self.selected_item -= 1;
1395 } else {
1396 self.selected_item = self.actions.len() - 1;
1397 }
1398 self.scroll_handle.scroll_to_item(self.selected_item);
1399 cx.notify();
1400 }
1401
1402 fn select_next(&mut self, cx: &mut ViewContext<Editor>) {
1403 if self.selected_item + 1 < self.actions.len() {
1404 self.selected_item += 1;
1405 } else {
1406 self.selected_item = 0;
1407 }
1408 self.scroll_handle.scroll_to_item(self.selected_item);
1409 cx.notify();
1410 }
1411
1412 fn select_last(&mut self, cx: &mut ViewContext<Editor>) {
1413 self.selected_item = self.actions.len() - 1;
1414 self.scroll_handle.scroll_to_item(self.selected_item);
1415 cx.notify()
1416 }
1417
1418 fn visible(&self) -> bool {
1419 !self.actions.is_empty()
1420 }
1421
1422 fn render(
1423 &self,
1424 mut cursor_position: DisplayPoint,
1425 _style: &EditorStyle,
1426 max_height: Pixels,
1427 cx: &mut ViewContext<Editor>,
1428 ) -> (DisplayPoint, AnyElement) {
1429 let actions = self.actions.clone();
1430 let selected_item = self.selected_item;
1431
1432 let element = uniform_list(
1433 cx.view().clone(),
1434 "code_actions_menu",
1435 self.actions.len(),
1436 move |_this, range, cx| {
1437 actions[range.clone()]
1438 .iter()
1439 .enumerate()
1440 .map(|(ix, action)| {
1441 let item_ix = range.start + ix;
1442 let selected = selected_item == item_ix;
1443 let colors = cx.theme().colors();
1444 div()
1445 .px_2()
1446 .text_color(colors.text)
1447 .when(selected, |style| {
1448 style
1449 .bg(colors.element_active)
1450 .text_color(colors.text_accent)
1451 })
1452 .hover(|style| {
1453 style
1454 .bg(colors.element_hover)
1455 .text_color(colors.text_accent)
1456 })
1457 .on_mouse_down(
1458 MouseButton::Left,
1459 cx.listener(move |editor, _, cx| {
1460 cx.stop_propagation();
1461 editor
1462 .confirm_code_action(
1463 &ConfirmCodeAction {
1464 item_ix: Some(item_ix),
1465 },
1466 cx,
1467 )
1468 .map(|task| task.detach_and_log_err(cx));
1469 }),
1470 )
1471 // TASK: It would be good to make lsp_action.title a SharedString to avoid allocating here.
1472 .child(SharedString::from(action.lsp_action.title.clone()))
1473 })
1474 .collect()
1475 },
1476 )
1477 .elevation_1(cx)
1478 .px_2()
1479 .py_1()
1480 .max_h(max_height)
1481 .track_scroll(self.scroll_handle.clone())
1482 .with_width_from_item(
1483 self.actions
1484 .iter()
1485 .enumerate()
1486 .max_by_key(|(_, action)| action.lsp_action.title.chars().count())
1487 .map(|(ix, _)| ix),
1488 )
1489 .into_any_element();
1490
1491 if self.deployed_from_indicator {
1492 *cursor_position.column_mut() = 0;
1493 }
1494
1495 (cursor_position, element)
1496 }
1497}
1498
1499pub struct CopilotState {
1500 excerpt_id: Option<ExcerptId>,
1501 pending_refresh: Task<Option<()>>,
1502 pending_cycling_refresh: Task<Option<()>>,
1503 cycled: bool,
1504 completions: Vec<copilot::Completion>,
1505 active_completion_index: usize,
1506 suggestion: Option<Inlay>,
1507}
1508
1509impl Default for CopilotState {
1510 fn default() -> Self {
1511 Self {
1512 excerpt_id: None,
1513 pending_cycling_refresh: Task::ready(Some(())),
1514 pending_refresh: Task::ready(Some(())),
1515 completions: Default::default(),
1516 active_completion_index: 0,
1517 cycled: false,
1518 suggestion: None,
1519 }
1520 }
1521}
1522
1523impl CopilotState {
1524 fn active_completion(&self) -> Option<&copilot::Completion> {
1525 self.completions.get(self.active_completion_index)
1526 }
1527
1528 fn text_for_active_completion(
1529 &self,
1530 cursor: Anchor,
1531 buffer: &MultiBufferSnapshot,
1532 ) -> Option<&str> {
1533 use language::ToOffset as _;
1534
1535 let completion = self.active_completion()?;
1536 let excerpt_id = self.excerpt_id?;
1537 let completion_buffer = buffer.buffer_for_excerpt(excerpt_id)?;
1538 if excerpt_id != cursor.excerpt_id
1539 || !completion.range.start.is_valid(completion_buffer)
1540 || !completion.range.end.is_valid(completion_buffer)
1541 {
1542 return None;
1543 }
1544
1545 let mut completion_range = completion.range.to_offset(&completion_buffer);
1546 let prefix_len = Self::common_prefix(
1547 completion_buffer.chars_for_range(completion_range.clone()),
1548 completion.text.chars(),
1549 );
1550 completion_range.start += prefix_len;
1551 let suffix_len = Self::common_prefix(
1552 completion_buffer.reversed_chars_for_range(completion_range.clone()),
1553 completion.text[prefix_len..].chars().rev(),
1554 );
1555 completion_range.end = completion_range.end.saturating_sub(suffix_len);
1556
1557 if completion_range.is_empty()
1558 && completion_range.start == cursor.text_anchor.to_offset(&completion_buffer)
1559 {
1560 Some(&completion.text[prefix_len..completion.text.len() - suffix_len])
1561 } else {
1562 None
1563 }
1564 }
1565
1566 fn cycle_completions(&mut self, direction: Direction) {
1567 match direction {
1568 Direction::Prev => {
1569 self.active_completion_index = if self.active_completion_index == 0 {
1570 self.completions.len().saturating_sub(1)
1571 } else {
1572 self.active_completion_index - 1
1573 };
1574 }
1575 Direction::Next => {
1576 if self.completions.len() == 0 {
1577 self.active_completion_index = 0
1578 } else {
1579 self.active_completion_index =
1580 (self.active_completion_index + 1) % self.completions.len();
1581 }
1582 }
1583 }
1584 }
1585
1586 fn push_completion(&mut self, new_completion: copilot::Completion) {
1587 for completion in &self.completions {
1588 if completion.text == new_completion.text && completion.range == new_completion.range {
1589 return;
1590 }
1591 }
1592 self.completions.push(new_completion);
1593 }
1594
1595 fn common_prefix<T1: Iterator<Item = char>, T2: Iterator<Item = char>>(a: T1, b: T2) -> usize {
1596 a.zip(b)
1597 .take_while(|(a, b)| a == b)
1598 .map(|(a, _)| a.len_utf8())
1599 .sum()
1600 }
1601}
1602
1603#[derive(Debug)]
1604struct ActiveDiagnosticGroup {
1605 primary_range: Range<Anchor>,
1606 primary_message: String,
1607 blocks: HashMap<BlockId, Diagnostic>,
1608 is_valid: bool,
1609}
1610
1611#[derive(Serialize, Deserialize)]
1612pub struct ClipboardSelection {
1613 pub len: usize,
1614 pub is_entire_line: bool,
1615 pub first_line_indent: u32,
1616}
1617
1618#[derive(Debug)]
1619pub struct NavigationData {
1620 cursor_anchor: Anchor,
1621 cursor_position: Point,
1622 scroll_anchor: ScrollAnchor,
1623 scroll_top_row: u32,
1624}
1625
1626pub struct EditorCreated(pub View<Editor>);
1627
1628enum GotoDefinitionKind {
1629 Symbol,
1630 Type,
1631}
1632
1633#[derive(Debug, Clone)]
1634enum InlayHintRefreshReason {
1635 Toggle(bool),
1636 SettingsChange(InlayHintSettings),
1637 NewLinesShown,
1638 BufferEdited(HashSet<Arc<Language>>),
1639 RefreshRequested,
1640 ExcerptsRemoved(Vec<ExcerptId>),
1641}
1642impl InlayHintRefreshReason {
1643 fn description(&self) -> &'static str {
1644 match self {
1645 Self::Toggle(_) => "toggle",
1646 Self::SettingsChange(_) => "settings change",
1647 Self::NewLinesShown => "new lines shown",
1648 Self::BufferEdited(_) => "buffer edited",
1649 Self::RefreshRequested => "refresh requested",
1650 Self::ExcerptsRemoved(_) => "excerpts removed",
1651 }
1652 }
1653}
1654
1655impl Editor {
1656 pub fn single_line(cx: &mut ViewContext<Self>) -> Self {
1657 let buffer = cx.new_model(|cx| Buffer::new(0, cx.entity_id().as_u64(), String::new()));
1658 let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
1659 Self::new(EditorMode::SingleLine, buffer, None, cx)
1660 }
1661
1662 pub fn multi_line(cx: &mut ViewContext<Self>) -> Self {
1663 let buffer = cx.new_model(|cx| Buffer::new(0, cx.entity_id().as_u64(), String::new()));
1664 let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
1665 Self::new(EditorMode::Full, buffer, None, cx)
1666 }
1667
1668 pub fn auto_height(max_lines: usize, cx: &mut ViewContext<Self>) -> Self {
1669 let buffer = cx.new_model(|cx| Buffer::new(0, cx.entity_id().as_u64(), String::new()));
1670 let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
1671 Self::new(EditorMode::AutoHeight { max_lines }, buffer, None, cx)
1672 }
1673
1674 pub fn for_buffer(
1675 buffer: Model<Buffer>,
1676 project: Option<Model<Project>>,
1677 cx: &mut ViewContext<Self>,
1678 ) -> Self {
1679 let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
1680 Self::new(EditorMode::Full, buffer, project, cx)
1681 }
1682
1683 pub fn for_multibuffer(
1684 buffer: Model<MultiBuffer>,
1685 project: Option<Model<Project>>,
1686 cx: &mut ViewContext<Self>,
1687 ) -> Self {
1688 Self::new(EditorMode::Full, buffer, project, cx)
1689 }
1690
1691 pub fn clone(&self, cx: &mut ViewContext<Self>) -> Self {
1692 let mut clone = Self::new(self.mode, self.buffer.clone(), self.project.clone(), cx);
1693 self.display_map.update(cx, |display_map, cx| {
1694 let snapshot = display_map.snapshot(cx);
1695 clone.display_map.update(cx, |display_map, cx| {
1696 display_map.set_state(&snapshot, cx);
1697 });
1698 });
1699 clone.selections.clone_state(&self.selections);
1700 clone.scroll_manager.clone_state(&self.scroll_manager);
1701 clone.searchable = self.searchable;
1702 clone
1703 }
1704
1705 fn new(
1706 mode: EditorMode,
1707 buffer: Model<MultiBuffer>,
1708 project: Option<Model<Project>>,
1709 cx: &mut ViewContext<Self>,
1710 ) -> Self {
1711 let style = cx.text_style();
1712 let font_size = style.font_size.to_pixels(cx.rem_size());
1713 let display_map = cx.new_model(|cx| {
1714 DisplayMap::new(buffer.clone(), style.font(), font_size, None, 2, 1, cx)
1715 });
1716
1717 let selections = SelectionsCollection::new(display_map.clone(), buffer.clone());
1718
1719 let blink_manager = cx.new_model(|cx| BlinkManager::new(CURSOR_BLINK_INTERVAL, cx));
1720
1721 let soft_wrap_mode_override =
1722 (mode == EditorMode::SingleLine).then(|| language_settings::SoftWrap::None);
1723
1724 let mut project_subscriptions = Vec::new();
1725 if mode == EditorMode::Full {
1726 if let Some(project) = project.as_ref() {
1727 if buffer.read(cx).is_singleton() {
1728 project_subscriptions.push(cx.observe(project, |_, _, cx| {
1729 cx.emit(EditorEvent::TitleChanged);
1730 }));
1731 }
1732 project_subscriptions.push(cx.subscribe(project, |editor, _, event, cx| {
1733 if let project::Event::RefreshInlayHints = event {
1734 editor.refresh_inlay_hints(InlayHintRefreshReason::RefreshRequested, cx);
1735 };
1736 }));
1737 }
1738 }
1739
1740 let inlay_hint_settings = inlay_hint_settings(
1741 selections.newest_anchor().head(),
1742 &buffer.read(cx).snapshot(cx),
1743 cx,
1744 );
1745
1746 let focus_handle = cx.focus_handle();
1747 cx.on_focus(&focus_handle, Self::handle_focus).detach();
1748 cx.on_blur(&focus_handle, Self::handle_blur).detach();
1749
1750 let mut this = Self {
1751 handle: cx.view().downgrade(),
1752 focus_handle,
1753 buffer: buffer.clone(),
1754 display_map: display_map.clone(),
1755 selections,
1756 scroll_manager: ScrollManager::new(),
1757 columnar_selection_tail: None,
1758 add_selections_state: None,
1759 select_next_state: None,
1760 select_prev_state: None,
1761 selection_history: Default::default(),
1762 autoclose_regions: Default::default(),
1763 snippet_stack: Default::default(),
1764 select_larger_syntax_node_stack: Vec::new(),
1765 ime_transaction: Default::default(),
1766 active_diagnostics: None,
1767 soft_wrap_mode_override,
1768 collaboration_hub: project.clone().map(|project| Box::new(project) as _),
1769 project,
1770 blink_manager: blink_manager.clone(),
1771 show_local_selections: true,
1772 mode,
1773 show_gutter: mode == EditorMode::Full,
1774 show_wrap_guides: None,
1775 placeholder_text: None,
1776 highlighted_rows: None,
1777 background_highlights: Default::default(),
1778 inlay_background_highlights: Default::default(),
1779 nav_history: None,
1780 context_menu: RwLock::new(None),
1781 mouse_context_menu: None,
1782 completion_tasks: Default::default(),
1783 next_completion_id: 0,
1784 next_inlay_id: 0,
1785 available_code_actions: Default::default(),
1786 code_actions_task: Default::default(),
1787 document_highlights_task: Default::default(),
1788 pending_rename: Default::default(),
1789 searchable: true,
1790 cursor_shape: Default::default(),
1791 autoindent_mode: Some(AutoindentMode::EachLine),
1792 collapse_matches: false,
1793 workspace: None,
1794 keymap_context_layers: Default::default(),
1795 input_enabled: true,
1796 read_only: false,
1797 leader_peer_id: None,
1798 remote_id: None,
1799 hover_state: Default::default(),
1800 link_go_to_definition_state: Default::default(),
1801 copilot_state: Default::default(),
1802 inlay_hint_cache: InlayHintCache::new(inlay_hint_settings),
1803 gutter_hovered: false,
1804 pixel_position_of_newest_cursor: None,
1805 gutter_width: Default::default(),
1806 style: None,
1807 editor_actions: Default::default(),
1808 show_copilot_suggestions: mode == EditorMode::Full,
1809 _subscriptions: vec![
1810 cx.observe(&buffer, Self::on_buffer_changed),
1811 cx.subscribe(&buffer, Self::on_buffer_event),
1812 cx.observe(&display_map, Self::on_display_map_changed),
1813 cx.observe(&blink_manager, |_, _, cx| cx.notify()),
1814 cx.observe_global::<SettingsStore>(Self::settings_changed),
1815 cx.observe_window_activation(|editor, cx| {
1816 let active = cx.is_window_active();
1817 editor.blink_manager.update(cx, |blink_manager, cx| {
1818 if active {
1819 blink_manager.enable(cx);
1820 } else {
1821 blink_manager.show_cursor(cx);
1822 blink_manager.disable(cx);
1823 }
1824 });
1825 }),
1826 ],
1827 };
1828
1829 this._subscriptions.extend(project_subscriptions);
1830
1831 this.end_selection(cx);
1832 this.scroll_manager.show_scrollbar(cx);
1833
1834 if mode == EditorMode::Full {
1835 let should_auto_hide_scrollbars = cx.should_auto_hide_scrollbars();
1836 cx.set_global(ScrollbarAutoHide(should_auto_hide_scrollbars));
1837 }
1838
1839 this.report_editor_event("open", None, cx);
1840 this
1841 }
1842
1843 fn key_context(&self, cx: &AppContext) -> KeyContext {
1844 let mut key_context = KeyContext::default();
1845 key_context.add("Editor");
1846 let mode = match self.mode {
1847 EditorMode::SingleLine => "single_line",
1848 EditorMode::AutoHeight { .. } => "auto_height",
1849 EditorMode::Full => "full",
1850 };
1851 key_context.set("mode", mode);
1852 if self.pending_rename.is_some() {
1853 key_context.add("renaming");
1854 }
1855 if self.context_menu_visible() {
1856 match self.context_menu.read().as_ref() {
1857 Some(ContextMenu::Completions(_)) => {
1858 key_context.add("menu");
1859 key_context.add("showing_completions")
1860 }
1861 Some(ContextMenu::CodeActions(_)) => {
1862 key_context.add("menu");
1863 key_context.add("showing_code_actions")
1864 }
1865 None => {}
1866 }
1867 }
1868
1869 for layer in self.keymap_context_layers.values() {
1870 key_context.extend(layer);
1871 }
1872
1873 if let Some(extension) = self
1874 .buffer
1875 .read(cx)
1876 .as_singleton()
1877 .and_then(|buffer| buffer.read(cx).file()?.path().extension()?.to_str())
1878 {
1879 key_context.set("extension", extension.to_string());
1880 }
1881
1882 key_context
1883 }
1884
1885 pub fn new_file(
1886 workspace: &mut Workspace,
1887 _: &workspace::NewFile,
1888 cx: &mut ViewContext<Workspace>,
1889 ) {
1890 let project = workspace.project().clone();
1891 if project.read(cx).is_remote() {
1892 cx.propagate();
1893 } else if let Some(buffer) = project
1894 .update(cx, |project, cx| project.create_buffer("", None, cx))
1895 .log_err()
1896 {
1897 workspace.add_item(
1898 Box::new(cx.new_view(|cx| Editor::for_buffer(buffer, Some(project.clone()), cx))),
1899 cx,
1900 );
1901 }
1902 }
1903
1904 pub fn new_file_in_direction(
1905 workspace: &mut Workspace,
1906 action: &workspace::NewFileInDirection,
1907 cx: &mut ViewContext<Workspace>,
1908 ) {
1909 let project = workspace.project().clone();
1910 if project.read(cx).is_remote() {
1911 cx.propagate();
1912 } else if let Some(buffer) = project
1913 .update(cx, |project, cx| project.create_buffer("", None, cx))
1914 .log_err()
1915 {
1916 workspace.split_item(
1917 action.0,
1918 Box::new(cx.new_view(|cx| Editor::for_buffer(buffer, Some(project.clone()), cx))),
1919 cx,
1920 );
1921 }
1922 }
1923
1924 pub fn replica_id(&self, cx: &AppContext) -> ReplicaId {
1925 self.buffer.read(cx).replica_id()
1926 }
1927
1928 pub fn leader_peer_id(&self) -> Option<PeerId> {
1929 self.leader_peer_id
1930 }
1931
1932 pub fn buffer(&self) -> &Model<MultiBuffer> {
1933 &self.buffer
1934 }
1935
1936 pub fn workspace(&self) -> Option<View<Workspace>> {
1937 self.workspace.as_ref()?.0.upgrade()
1938 }
1939
1940 pub fn pane(&self, cx: &AppContext) -> Option<View<Pane>> {
1941 self.workspace()?.read(cx).pane_for(&self.handle.upgrade()?)
1942 }
1943
1944 pub fn title<'a>(&self, cx: &'a AppContext) -> Cow<'a, str> {
1945 self.buffer().read(cx).title(cx)
1946 }
1947
1948 pub fn snapshot(&mut self, cx: &mut WindowContext) -> EditorSnapshot {
1949 EditorSnapshot {
1950 mode: self.mode,
1951 show_gutter: self.show_gutter,
1952 display_snapshot: self.display_map.update(cx, |map, cx| map.snapshot(cx)),
1953 scroll_anchor: self.scroll_manager.anchor(),
1954 ongoing_scroll: self.scroll_manager.ongoing_scroll(),
1955 placeholder_text: self.placeholder_text.clone(),
1956 is_focused: self.focus_handle.is_focused(cx),
1957 }
1958 }
1959
1960 pub fn language_at<'a, T: ToOffset>(
1961 &self,
1962 point: T,
1963 cx: &'a AppContext,
1964 ) -> Option<Arc<Language>> {
1965 self.buffer.read(cx).language_at(point, cx)
1966 }
1967
1968 pub fn file_at<'a, T: ToOffset>(
1969 &self,
1970 point: T,
1971 cx: &'a AppContext,
1972 ) -> Option<Arc<dyn language::File>> {
1973 self.buffer.read(cx).read(cx).file_at(point).cloned()
1974 }
1975
1976 pub fn active_excerpt(
1977 &self,
1978 cx: &AppContext,
1979 ) -> Option<(ExcerptId, Model<Buffer>, Range<text::Anchor>)> {
1980 self.buffer
1981 .read(cx)
1982 .excerpt_containing(self.selections.newest_anchor().head(), cx)
1983 }
1984
1985 pub fn mode(&self) -> EditorMode {
1986 self.mode
1987 }
1988
1989 pub fn collaboration_hub(&self) -> Option<&dyn CollaborationHub> {
1990 self.collaboration_hub.as_deref()
1991 }
1992
1993 pub fn set_collaboration_hub(&mut self, hub: Box<dyn CollaborationHub>) {
1994 self.collaboration_hub = Some(hub);
1995 }
1996
1997 pub fn placeholder_text(&self) -> Option<&str> {
1998 self.placeholder_text.as_deref()
1999 }
2000
2001 pub fn set_placeholder_text(
2002 &mut self,
2003 placeholder_text: impl Into<Arc<str>>,
2004 cx: &mut ViewContext<Self>,
2005 ) {
2006 let placeholder_text = Some(placeholder_text.into());
2007 if self.placeholder_text != placeholder_text {
2008 self.placeholder_text = placeholder_text;
2009 cx.notify();
2010 }
2011 }
2012
2013 pub fn set_cursor_shape(&mut self, cursor_shape: CursorShape, cx: &mut ViewContext<Self>) {
2014 self.cursor_shape = cursor_shape;
2015 cx.notify();
2016 }
2017
2018 pub fn set_collapse_matches(&mut self, collapse_matches: bool) {
2019 self.collapse_matches = collapse_matches;
2020 }
2021
2022 pub fn range_for_match<T: std::marker::Copy>(&self, range: &Range<T>) -> Range<T> {
2023 if self.collapse_matches {
2024 return range.start..range.start;
2025 }
2026 range.clone()
2027 }
2028
2029 pub fn set_clip_at_line_ends(&mut self, clip: bool, cx: &mut ViewContext<Self>) {
2030 if self.display_map.read(cx).clip_at_line_ends != clip {
2031 self.display_map
2032 .update(cx, |map, _| map.clip_at_line_ends = clip);
2033 }
2034 }
2035
2036 pub fn set_keymap_context_layer<Tag: 'static>(
2037 &mut self,
2038 context: KeyContext,
2039 cx: &mut ViewContext<Self>,
2040 ) {
2041 self.keymap_context_layers
2042 .insert(TypeId::of::<Tag>(), context);
2043 cx.notify();
2044 }
2045
2046 pub fn remove_keymap_context_layer<Tag: 'static>(&mut self, cx: &mut ViewContext<Self>) {
2047 self.keymap_context_layers.remove(&TypeId::of::<Tag>());
2048 cx.notify();
2049 }
2050
2051 pub fn set_input_enabled(&mut self, input_enabled: bool) {
2052 self.input_enabled = input_enabled;
2053 }
2054
2055 pub fn set_autoindent(&mut self, autoindent: bool) {
2056 if autoindent {
2057 self.autoindent_mode = Some(AutoindentMode::EachLine);
2058 } else {
2059 self.autoindent_mode = None;
2060 }
2061 }
2062
2063 pub fn read_only(&self, cx: &AppContext) -> bool {
2064 self.read_only || self.buffer.read(cx).read_only()
2065 }
2066
2067 pub fn set_read_only(&mut self, read_only: bool) {
2068 self.read_only = read_only;
2069 }
2070
2071 pub fn set_show_copilot_suggestions(&mut self, show_copilot_suggestions: bool) {
2072 self.show_copilot_suggestions = show_copilot_suggestions;
2073 }
2074
2075 fn selections_did_change(
2076 &mut self,
2077 local: bool,
2078 old_cursor_position: &Anchor,
2079 cx: &mut ViewContext<Self>,
2080 ) {
2081 if self.focus_handle.is_focused(cx) && self.leader_peer_id.is_none() {
2082 self.buffer.update(cx, |buffer, cx| {
2083 buffer.set_active_selections(
2084 &self.selections.disjoint_anchors(),
2085 self.selections.line_mode,
2086 self.cursor_shape,
2087 cx,
2088 )
2089 });
2090 }
2091
2092 let display_map = self
2093 .display_map
2094 .update(cx, |display_map, cx| display_map.snapshot(cx));
2095 let buffer = &display_map.buffer_snapshot;
2096 self.add_selections_state = None;
2097 self.select_next_state = None;
2098 self.select_prev_state = None;
2099 self.select_larger_syntax_node_stack.clear();
2100 self.invalidate_autoclose_regions(&self.selections.disjoint_anchors(), buffer);
2101 self.snippet_stack
2102 .invalidate(&self.selections.disjoint_anchors(), buffer);
2103 self.take_rename(false, cx);
2104
2105 let new_cursor_position = self.selections.newest_anchor().head();
2106
2107 self.push_to_nav_history(
2108 old_cursor_position.clone(),
2109 Some(new_cursor_position.to_point(buffer)),
2110 cx,
2111 );
2112
2113 if local {
2114 let new_cursor_position = self.selections.newest_anchor().head();
2115 let mut context_menu = self.context_menu.write();
2116 let completion_menu = match context_menu.as_ref() {
2117 Some(ContextMenu::Completions(menu)) => Some(menu),
2118
2119 _ => {
2120 *context_menu = None;
2121 None
2122 }
2123 };
2124
2125 if let Some(completion_menu) = completion_menu {
2126 let cursor_position = new_cursor_position.to_offset(buffer);
2127 let (word_range, kind) =
2128 buffer.surrounding_word(completion_menu.initial_position.clone());
2129 if kind == Some(CharKind::Word)
2130 && word_range.to_inclusive().contains(&cursor_position)
2131 {
2132 let mut completion_menu = completion_menu.clone();
2133 drop(context_menu);
2134
2135 let query = Self::completion_query(buffer, cursor_position);
2136 cx.spawn(move |this, mut cx| async move {
2137 completion_menu
2138 .filter(query.as_deref(), cx.background_executor().clone())
2139 .await;
2140
2141 this.update(&mut cx, |this, cx| {
2142 let mut context_menu = this.context_menu.write();
2143 let Some(ContextMenu::Completions(menu)) = context_menu.as_ref() else {
2144 return;
2145 };
2146
2147 if menu.id > completion_menu.id {
2148 return;
2149 }
2150
2151 *context_menu = Some(ContextMenu::Completions(completion_menu));
2152 drop(context_menu);
2153 cx.notify();
2154 })
2155 })
2156 .detach();
2157
2158 self.show_completions(&ShowCompletions, cx);
2159 } else {
2160 drop(context_menu);
2161 self.hide_context_menu(cx);
2162 }
2163 } else {
2164 drop(context_menu);
2165 }
2166
2167 hide_hover(self, cx);
2168
2169 if old_cursor_position.to_display_point(&display_map).row()
2170 != new_cursor_position.to_display_point(&display_map).row()
2171 {
2172 self.available_code_actions.take();
2173 }
2174 self.refresh_code_actions(cx);
2175 self.refresh_document_highlights(cx);
2176 refresh_matching_bracket_highlights(self, cx);
2177 self.discard_copilot_suggestion(cx);
2178 }
2179
2180 self.blink_manager.update(cx, BlinkManager::pause_blinking);
2181 cx.emit(EditorEvent::SelectionsChanged { local });
2182
2183 if self.selections.disjoint_anchors().len() == 1 {
2184 cx.emit(SearchEvent::ActiveMatchChanged)
2185 }
2186
2187 cx.notify();
2188 }
2189
2190 pub fn change_selections<R>(
2191 &mut self,
2192 autoscroll: Option<Autoscroll>,
2193 cx: &mut ViewContext<Self>,
2194 change: impl FnOnce(&mut MutableSelectionsCollection<'_>) -> R,
2195 ) -> R {
2196 let old_cursor_position = self.selections.newest_anchor().head();
2197 self.push_to_selection_history();
2198
2199 let (changed, result) = self.selections.change_with(cx, change);
2200
2201 if changed {
2202 if let Some(autoscroll) = autoscroll {
2203 self.request_autoscroll(autoscroll, cx);
2204 }
2205 self.selections_did_change(true, &old_cursor_position, cx);
2206 }
2207
2208 result
2209 }
2210
2211 pub fn edit<I, S, T>(&mut self, edits: I, cx: &mut ViewContext<Self>)
2212 where
2213 I: IntoIterator<Item = (Range<S>, T)>,
2214 S: ToOffset,
2215 T: Into<Arc<str>>,
2216 {
2217 if self.read_only(cx) {
2218 return;
2219 }
2220
2221 self.buffer
2222 .update(cx, |buffer, cx| buffer.edit(edits, None, cx));
2223 }
2224
2225 pub fn edit_with_autoindent<I, S, T>(&mut self, edits: I, cx: &mut ViewContext<Self>)
2226 where
2227 I: IntoIterator<Item = (Range<S>, T)>,
2228 S: ToOffset,
2229 T: Into<Arc<str>>,
2230 {
2231 if self.read_only(cx) {
2232 return;
2233 }
2234
2235 self.buffer.update(cx, |buffer, cx| {
2236 buffer.edit(edits, self.autoindent_mode.clone(), cx)
2237 });
2238 }
2239
2240 pub fn edit_with_block_indent<I, S, T>(
2241 &mut self,
2242 edits: I,
2243 original_indent_columns: Vec<u32>,
2244 cx: &mut ViewContext<Self>,
2245 ) where
2246 I: IntoIterator<Item = (Range<S>, T)>,
2247 S: ToOffset,
2248 T: Into<Arc<str>>,
2249 {
2250 if self.read_only(cx) {
2251 return;
2252 }
2253
2254 self.buffer.update(cx, |buffer, cx| {
2255 buffer.edit(
2256 edits,
2257 Some(AutoindentMode::Block {
2258 original_indent_columns,
2259 }),
2260 cx,
2261 )
2262 });
2263 }
2264
2265 fn select(&mut self, phase: SelectPhase, cx: &mut ViewContext<Self>) {
2266 self.hide_context_menu(cx);
2267
2268 match phase {
2269 SelectPhase::Begin {
2270 position,
2271 add,
2272 click_count,
2273 } => self.begin_selection(position, add, click_count, cx),
2274 SelectPhase::BeginColumnar {
2275 position,
2276 goal_column,
2277 } => self.begin_columnar_selection(position, goal_column, cx),
2278 SelectPhase::Extend {
2279 position,
2280 click_count,
2281 } => self.extend_selection(position, click_count, cx),
2282 SelectPhase::Update {
2283 position,
2284 goal_column,
2285 scroll_position,
2286 } => self.update_selection(position, goal_column, scroll_position, cx),
2287 SelectPhase::End => self.end_selection(cx),
2288 }
2289 }
2290
2291 fn extend_selection(
2292 &mut self,
2293 position: DisplayPoint,
2294 click_count: usize,
2295 cx: &mut ViewContext<Self>,
2296 ) {
2297 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
2298 let tail = self.selections.newest::<usize>(cx).tail();
2299 self.begin_selection(position, false, click_count, cx);
2300
2301 let position = position.to_offset(&display_map, Bias::Left);
2302 let tail_anchor = display_map.buffer_snapshot.anchor_before(tail);
2303
2304 let mut pending_selection = self
2305 .selections
2306 .pending_anchor()
2307 .expect("extend_selection not called with pending selection");
2308 if position >= tail {
2309 pending_selection.start = tail_anchor;
2310 } else {
2311 pending_selection.end = tail_anchor;
2312 pending_selection.reversed = true;
2313 }
2314
2315 let mut pending_mode = self.selections.pending_mode().unwrap();
2316 match &mut pending_mode {
2317 SelectMode::Word(range) | SelectMode::Line(range) => *range = tail_anchor..tail_anchor,
2318 _ => {}
2319 }
2320
2321 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
2322 s.set_pending(pending_selection, pending_mode)
2323 });
2324 }
2325
2326 fn begin_selection(
2327 &mut self,
2328 position: DisplayPoint,
2329 add: bool,
2330 click_count: usize,
2331 cx: &mut ViewContext<Self>,
2332 ) {
2333 if !self.focus_handle.is_focused(cx) {
2334 cx.focus(&self.focus_handle);
2335 }
2336
2337 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
2338 let buffer = &display_map.buffer_snapshot;
2339 let newest_selection = self.selections.newest_anchor().clone();
2340 let position = display_map.clip_point(position, Bias::Left);
2341
2342 let start;
2343 let end;
2344 let mode;
2345 let auto_scroll;
2346 match click_count {
2347 1 => {
2348 start = buffer.anchor_before(position.to_point(&display_map));
2349 end = start.clone();
2350 mode = SelectMode::Character;
2351 auto_scroll = true;
2352 }
2353 2 => {
2354 let range = movement::surrounding_word(&display_map, position);
2355 start = buffer.anchor_before(range.start.to_point(&display_map));
2356 end = buffer.anchor_before(range.end.to_point(&display_map));
2357 mode = SelectMode::Word(start.clone()..end.clone());
2358 auto_scroll = true;
2359 }
2360 3 => {
2361 let position = display_map
2362 .clip_point(position, Bias::Left)
2363 .to_point(&display_map);
2364 let line_start = display_map.prev_line_boundary(position).0;
2365 let next_line_start = buffer.clip_point(
2366 display_map.next_line_boundary(position).0 + Point::new(1, 0),
2367 Bias::Left,
2368 );
2369 start = buffer.anchor_before(line_start);
2370 end = buffer.anchor_before(next_line_start);
2371 mode = SelectMode::Line(start.clone()..end.clone());
2372 auto_scroll = true;
2373 }
2374 _ => {
2375 start = buffer.anchor_before(0);
2376 end = buffer.anchor_before(buffer.len());
2377 mode = SelectMode::All;
2378 auto_scroll = false;
2379 }
2380 }
2381
2382 self.change_selections(auto_scroll.then(|| Autoscroll::newest()), cx, |s| {
2383 if !add {
2384 s.clear_disjoint();
2385 } else if click_count > 1 {
2386 s.delete(newest_selection.id)
2387 }
2388
2389 s.set_pending_anchor_range(start..end, mode);
2390 });
2391 }
2392
2393 fn begin_columnar_selection(
2394 &mut self,
2395 position: DisplayPoint,
2396 goal_column: u32,
2397 cx: &mut ViewContext<Self>,
2398 ) {
2399 if !self.focus_handle.is_focused(cx) {
2400 cx.focus(&self.focus_handle);
2401 }
2402
2403 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
2404 let tail = self.selections.newest::<Point>(cx).tail();
2405 self.columnar_selection_tail = Some(display_map.buffer_snapshot.anchor_before(tail));
2406
2407 self.select_columns(
2408 tail.to_display_point(&display_map),
2409 position,
2410 goal_column,
2411 &display_map,
2412 cx,
2413 );
2414 }
2415
2416 fn update_selection(
2417 &mut self,
2418 position: DisplayPoint,
2419 goal_column: u32,
2420 scroll_position: gpui::Point<f32>,
2421 cx: &mut ViewContext<Self>,
2422 ) {
2423 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
2424
2425 if let Some(tail) = self.columnar_selection_tail.as_ref() {
2426 let tail = tail.to_display_point(&display_map);
2427 self.select_columns(tail, position, goal_column, &display_map, cx);
2428 } else if let Some(mut pending) = self.selections.pending_anchor() {
2429 let buffer = self.buffer.read(cx).snapshot(cx);
2430 let head;
2431 let tail;
2432 let mode = self.selections.pending_mode().unwrap();
2433 match &mode {
2434 SelectMode::Character => {
2435 head = position.to_point(&display_map);
2436 tail = pending.tail().to_point(&buffer);
2437 }
2438 SelectMode::Word(original_range) => {
2439 let original_display_range = original_range.start.to_display_point(&display_map)
2440 ..original_range.end.to_display_point(&display_map);
2441 let original_buffer_range = original_display_range.start.to_point(&display_map)
2442 ..original_display_range.end.to_point(&display_map);
2443 if movement::is_inside_word(&display_map, position)
2444 || original_display_range.contains(&position)
2445 {
2446 let word_range = movement::surrounding_word(&display_map, position);
2447 if word_range.start < original_display_range.start {
2448 head = word_range.start.to_point(&display_map);
2449 } else {
2450 head = word_range.end.to_point(&display_map);
2451 }
2452 } else {
2453 head = position.to_point(&display_map);
2454 }
2455
2456 if head <= original_buffer_range.start {
2457 tail = original_buffer_range.end;
2458 } else {
2459 tail = original_buffer_range.start;
2460 }
2461 }
2462 SelectMode::Line(original_range) => {
2463 let original_range = original_range.to_point(&display_map.buffer_snapshot);
2464
2465 let position = display_map
2466 .clip_point(position, Bias::Left)
2467 .to_point(&display_map);
2468 let line_start = display_map.prev_line_boundary(position).0;
2469 let next_line_start = buffer.clip_point(
2470 display_map.next_line_boundary(position).0 + Point::new(1, 0),
2471 Bias::Left,
2472 );
2473
2474 if line_start < original_range.start {
2475 head = line_start
2476 } else {
2477 head = next_line_start
2478 }
2479
2480 if head <= original_range.start {
2481 tail = original_range.end;
2482 } else {
2483 tail = original_range.start;
2484 }
2485 }
2486 SelectMode::All => {
2487 return;
2488 }
2489 };
2490
2491 if head < tail {
2492 pending.start = buffer.anchor_before(head);
2493 pending.end = buffer.anchor_before(tail);
2494 pending.reversed = true;
2495 } else {
2496 pending.start = buffer.anchor_before(tail);
2497 pending.end = buffer.anchor_before(head);
2498 pending.reversed = false;
2499 }
2500
2501 self.change_selections(None, cx, |s| {
2502 s.set_pending(pending, mode);
2503 });
2504 } else {
2505 log::error!("update_selection dispatched with no pending selection");
2506 return;
2507 }
2508
2509 self.set_scroll_position(scroll_position, cx);
2510 cx.notify();
2511 }
2512
2513 fn end_selection(&mut self, cx: &mut ViewContext<Self>) {
2514 self.columnar_selection_tail.take();
2515 if self.selections.pending_anchor().is_some() {
2516 let selections = self.selections.all::<usize>(cx);
2517 self.change_selections(None, cx, |s| {
2518 s.select(selections);
2519 s.clear_pending();
2520 });
2521 }
2522 }
2523
2524 fn select_columns(
2525 &mut self,
2526 tail: DisplayPoint,
2527 head: DisplayPoint,
2528 goal_column: u32,
2529 display_map: &DisplaySnapshot,
2530 cx: &mut ViewContext<Self>,
2531 ) {
2532 let start_row = cmp::min(tail.row(), head.row());
2533 let end_row = cmp::max(tail.row(), head.row());
2534 let start_column = cmp::min(tail.column(), goal_column);
2535 let end_column = cmp::max(tail.column(), goal_column);
2536 let reversed = start_column < tail.column();
2537
2538 let selection_ranges = (start_row..=end_row)
2539 .filter_map(|row| {
2540 if start_column <= display_map.line_len(row) && !display_map.is_block_line(row) {
2541 let start = display_map
2542 .clip_point(DisplayPoint::new(row, start_column), Bias::Left)
2543 .to_point(display_map);
2544 let end = display_map
2545 .clip_point(DisplayPoint::new(row, end_column), Bias::Right)
2546 .to_point(display_map);
2547 if reversed {
2548 Some(end..start)
2549 } else {
2550 Some(start..end)
2551 }
2552 } else {
2553 None
2554 }
2555 })
2556 .collect::<Vec<_>>();
2557
2558 self.change_selections(None, cx, |s| {
2559 s.select_ranges(selection_ranges);
2560 });
2561 cx.notify();
2562 }
2563
2564 pub fn has_pending_nonempty_selection(&self) -> bool {
2565 let pending_nonempty_selection = match self.selections.pending_anchor() {
2566 Some(Selection { start, end, .. }) => start != end,
2567 None => false,
2568 };
2569 pending_nonempty_selection || self.columnar_selection_tail.is_some()
2570 }
2571
2572 pub fn has_pending_selection(&self) -> bool {
2573 self.selections.pending_anchor().is_some() || self.columnar_selection_tail.is_some()
2574 }
2575
2576 pub fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
2577 if self.take_rename(false, cx).is_some() {
2578 return;
2579 }
2580
2581 if hide_hover(self, cx) {
2582 return;
2583 }
2584
2585 if self.hide_context_menu(cx).is_some() {
2586 return;
2587 }
2588
2589 if self.discard_copilot_suggestion(cx) {
2590 return;
2591 }
2592
2593 if self.snippet_stack.pop().is_some() {
2594 return;
2595 }
2596
2597 if self.mode == EditorMode::Full {
2598 if self.active_diagnostics.is_some() {
2599 self.dismiss_diagnostics(cx);
2600 return;
2601 }
2602
2603 if self.change_selections(Some(Autoscroll::fit()), cx, |s| s.try_cancel()) {
2604 return;
2605 }
2606 }
2607
2608 cx.propagate();
2609 }
2610
2611 pub fn handle_input(&mut self, text: &str, cx: &mut ViewContext<Self>) {
2612 let text: Arc<str> = text.into();
2613
2614 if self.read_only(cx) {
2615 return;
2616 }
2617
2618 let selections = self.selections.all_adjusted(cx);
2619 let mut brace_inserted = false;
2620 let mut edits = Vec::new();
2621 let mut new_selections = Vec::with_capacity(selections.len());
2622 let mut new_autoclose_regions = Vec::new();
2623 let snapshot = self.buffer.read(cx).read(cx);
2624
2625 for (selection, autoclose_region) in
2626 self.selections_with_autoclose_regions(selections, &snapshot)
2627 {
2628 if let Some(scope) = snapshot.language_scope_at(selection.head()) {
2629 // Determine if the inserted text matches the opening or closing
2630 // bracket of any of this language's bracket pairs.
2631 let mut bracket_pair = None;
2632 let mut is_bracket_pair_start = false;
2633 if !text.is_empty() {
2634 // `text` can be empty when an user is using IME (e.g. Chinese Wubi Simplified)
2635 // and they are removing the character that triggered IME popup.
2636 for (pair, enabled) in scope.brackets() {
2637 if enabled && pair.close && pair.start.ends_with(text.as_ref()) {
2638 bracket_pair = Some(pair.clone());
2639 is_bracket_pair_start = true;
2640 break;
2641 } else if pair.end.as_str() == text.as_ref() {
2642 bracket_pair = Some(pair.clone());
2643 break;
2644 }
2645 }
2646 }
2647
2648 if let Some(bracket_pair) = bracket_pair {
2649 if selection.is_empty() {
2650 if is_bracket_pair_start {
2651 let prefix_len = bracket_pair.start.len() - text.len();
2652
2653 // If the inserted text is a suffix of an opening bracket and the
2654 // selection is preceded by the rest of the opening bracket, then
2655 // insert the closing bracket.
2656 let following_text_allows_autoclose = snapshot
2657 .chars_at(selection.start)
2658 .next()
2659 .map_or(true, |c| scope.should_autoclose_before(c));
2660 let preceding_text_matches_prefix = prefix_len == 0
2661 || (selection.start.column >= (prefix_len as u32)
2662 && snapshot.contains_str_at(
2663 Point::new(
2664 selection.start.row,
2665 selection.start.column - (prefix_len as u32),
2666 ),
2667 &bracket_pair.start[..prefix_len],
2668 ));
2669 if following_text_allows_autoclose && preceding_text_matches_prefix {
2670 let anchor = snapshot.anchor_before(selection.end);
2671 new_selections.push((selection.map(|_| anchor), text.len()));
2672 new_autoclose_regions.push((
2673 anchor,
2674 text.len(),
2675 selection.id,
2676 bracket_pair.clone(),
2677 ));
2678 edits.push((
2679 selection.range(),
2680 format!("{}{}", text, bracket_pair.end).into(),
2681 ));
2682 brace_inserted = true;
2683 continue;
2684 }
2685 }
2686
2687 if let Some(region) = autoclose_region {
2688 // If the selection is followed by an auto-inserted closing bracket,
2689 // then don't insert that closing bracket again; just move the selection
2690 // past the closing bracket.
2691 let should_skip = selection.end == region.range.end.to_point(&snapshot)
2692 && text.as_ref() == region.pair.end.as_str();
2693 if should_skip {
2694 let anchor = snapshot.anchor_after(selection.end);
2695 new_selections
2696 .push((selection.map(|_| anchor), region.pair.end.len()));
2697 continue;
2698 }
2699 }
2700 }
2701 // If an opening bracket is 1 character long and is typed while
2702 // text is selected, then surround that text with the bracket pair.
2703 else if is_bracket_pair_start && bracket_pair.start.chars().count() == 1 {
2704 edits.push((selection.start..selection.start, text.clone()));
2705 edits.push((
2706 selection.end..selection.end,
2707 bracket_pair.end.as_str().into(),
2708 ));
2709 brace_inserted = true;
2710 new_selections.push((
2711 Selection {
2712 id: selection.id,
2713 start: snapshot.anchor_after(selection.start),
2714 end: snapshot.anchor_before(selection.end),
2715 reversed: selection.reversed,
2716 goal: selection.goal,
2717 },
2718 0,
2719 ));
2720 continue;
2721 }
2722 }
2723 }
2724
2725 // If not handling any auto-close operation, then just replace the selected
2726 // text with the given input and move the selection to the end of the
2727 // newly inserted text.
2728 let anchor = snapshot.anchor_after(selection.end);
2729 new_selections.push((selection.map(|_| anchor), 0));
2730 edits.push((selection.start..selection.end, text.clone()));
2731 }
2732
2733 drop(snapshot);
2734 self.transact(cx, |this, cx| {
2735 this.buffer.update(cx, |buffer, cx| {
2736 buffer.edit(edits, this.autoindent_mode.clone(), cx);
2737 });
2738
2739 let new_anchor_selections = new_selections.iter().map(|e| &e.0);
2740 let new_selection_deltas = new_selections.iter().map(|e| e.1);
2741 let snapshot = this.buffer.read(cx).read(cx);
2742 let new_selections = resolve_multiple::<usize, _>(new_anchor_selections, &snapshot)
2743 .zip(new_selection_deltas)
2744 .map(|(selection, delta)| Selection {
2745 id: selection.id,
2746 start: selection.start + delta,
2747 end: selection.end + delta,
2748 reversed: selection.reversed,
2749 goal: SelectionGoal::None,
2750 })
2751 .collect::<Vec<_>>();
2752
2753 let mut i = 0;
2754 for (position, delta, selection_id, pair) in new_autoclose_regions {
2755 let position = position.to_offset(&snapshot) + delta;
2756 let start = snapshot.anchor_before(position);
2757 let end = snapshot.anchor_after(position);
2758 while let Some(existing_state) = this.autoclose_regions.get(i) {
2759 match existing_state.range.start.cmp(&start, &snapshot) {
2760 Ordering::Less => i += 1,
2761 Ordering::Greater => break,
2762 Ordering::Equal => match end.cmp(&existing_state.range.end, &snapshot) {
2763 Ordering::Less => i += 1,
2764 Ordering::Equal => break,
2765 Ordering::Greater => break,
2766 },
2767 }
2768 }
2769 this.autoclose_regions.insert(
2770 i,
2771 AutocloseRegion {
2772 selection_id,
2773 range: start..end,
2774 pair,
2775 },
2776 );
2777 }
2778
2779 drop(snapshot);
2780 let had_active_copilot_suggestion = this.has_active_copilot_suggestion(cx);
2781 this.change_selections(Some(Autoscroll::fit()), cx, |s| s.select(new_selections));
2782
2783 if !brace_inserted && EditorSettings::get_global(cx).use_on_type_format {
2784 if let Some(on_type_format_task) =
2785 this.trigger_on_type_formatting(text.to_string(), cx)
2786 {
2787 on_type_format_task.detach_and_log_err(cx);
2788 }
2789 }
2790
2791 if had_active_copilot_suggestion {
2792 this.refresh_copilot_suggestions(true, cx);
2793 if !this.has_active_copilot_suggestion(cx) {
2794 this.trigger_completion_on_input(&text, cx);
2795 }
2796 } else {
2797 this.trigger_completion_on_input(&text, cx);
2798 this.refresh_copilot_suggestions(true, cx);
2799 }
2800 });
2801 }
2802
2803 pub fn newline(&mut self, _: &Newline, cx: &mut ViewContext<Self>) {
2804 self.transact(cx, |this, cx| {
2805 let (edits, selection_fixup_info): (Vec<_>, Vec<_>) = {
2806 let selections = this.selections.all::<usize>(cx);
2807 let multi_buffer = this.buffer.read(cx);
2808 let buffer = multi_buffer.snapshot(cx);
2809 selections
2810 .iter()
2811 .map(|selection| {
2812 let start_point = selection.start.to_point(&buffer);
2813 let mut indent = buffer.indent_size_for_line(start_point.row);
2814 indent.len = cmp::min(indent.len, start_point.column);
2815 let start = selection.start;
2816 let end = selection.end;
2817 let is_cursor = start == end;
2818 let language_scope = buffer.language_scope_at(start);
2819 let (comment_delimiter, insert_extra_newline) = if let Some(language) =
2820 &language_scope
2821 {
2822 let leading_whitespace_len = buffer
2823 .reversed_chars_at(start)
2824 .take_while(|c| c.is_whitespace() && *c != '\n')
2825 .map(|c| c.len_utf8())
2826 .sum::<usize>();
2827
2828 let trailing_whitespace_len = buffer
2829 .chars_at(end)
2830 .take_while(|c| c.is_whitespace() && *c != '\n')
2831 .map(|c| c.len_utf8())
2832 .sum::<usize>();
2833
2834 let insert_extra_newline =
2835 language.brackets().any(|(pair, enabled)| {
2836 let pair_start = pair.start.trim_end();
2837 let pair_end = pair.end.trim_start();
2838
2839 enabled
2840 && pair.newline
2841 && buffer.contains_str_at(
2842 end + trailing_whitespace_len,
2843 pair_end,
2844 )
2845 && buffer.contains_str_at(
2846 (start - leading_whitespace_len)
2847 .saturating_sub(pair_start.len()),
2848 pair_start,
2849 )
2850 });
2851 // Comment extension on newline is allowed only for cursor selections
2852 let comment_delimiter = language.line_comment_prefix().filter(|_| {
2853 let is_comment_extension_enabled =
2854 multi_buffer.settings_at(0, cx).extend_comment_on_newline;
2855 is_cursor && is_comment_extension_enabled
2856 });
2857 let comment_delimiter = if let Some(delimiter) = comment_delimiter {
2858 buffer
2859 .buffer_line_for_row(start_point.row)
2860 .is_some_and(|(snapshot, range)| {
2861 let mut index_of_first_non_whitespace = 0;
2862 let line_starts_with_comment = snapshot
2863 .chars_for_range(range)
2864 .skip_while(|c| {
2865 let should_skip = c.is_whitespace();
2866 if should_skip {
2867 index_of_first_non_whitespace += 1;
2868 }
2869 should_skip
2870 })
2871 .take(delimiter.len())
2872 .eq(delimiter.chars());
2873 let cursor_is_placed_after_comment_marker =
2874 index_of_first_non_whitespace + delimiter.len()
2875 <= start_point.column as usize;
2876 line_starts_with_comment
2877 && cursor_is_placed_after_comment_marker
2878 })
2879 .then(|| delimiter.clone())
2880 } else {
2881 None
2882 };
2883 (comment_delimiter, insert_extra_newline)
2884 } else {
2885 (None, false)
2886 };
2887
2888 let capacity_for_delimiter = comment_delimiter
2889 .as_deref()
2890 .map(str::len)
2891 .unwrap_or_default();
2892 let mut new_text =
2893 String::with_capacity(1 + capacity_for_delimiter + indent.len as usize);
2894 new_text.push_str("\n");
2895 new_text.extend(indent.chars());
2896 if let Some(delimiter) = &comment_delimiter {
2897 new_text.push_str(&delimiter);
2898 }
2899 if insert_extra_newline {
2900 new_text = new_text.repeat(2);
2901 }
2902
2903 let anchor = buffer.anchor_after(end);
2904 let new_selection = selection.map(|_| anchor);
2905 (
2906 (start..end, new_text),
2907 (insert_extra_newline, new_selection),
2908 )
2909 })
2910 .unzip()
2911 };
2912
2913 this.edit_with_autoindent(edits, cx);
2914 let buffer = this.buffer.read(cx).snapshot(cx);
2915 let new_selections = selection_fixup_info
2916 .into_iter()
2917 .map(|(extra_newline_inserted, new_selection)| {
2918 let mut cursor = new_selection.end.to_point(&buffer);
2919 if extra_newline_inserted {
2920 cursor.row -= 1;
2921 cursor.column = buffer.line_len(cursor.row);
2922 }
2923 new_selection.map(|_| cursor)
2924 })
2925 .collect();
2926
2927 this.change_selections(Some(Autoscroll::fit()), cx, |s| s.select(new_selections));
2928 this.refresh_copilot_suggestions(true, cx);
2929 });
2930 }
2931
2932 pub fn newline_above(&mut self, _: &NewlineAbove, cx: &mut ViewContext<Self>) {
2933 let buffer = self.buffer.read(cx);
2934 let snapshot = buffer.snapshot(cx);
2935
2936 let mut edits = Vec::new();
2937 let mut rows = Vec::new();
2938 let mut rows_inserted = 0;
2939
2940 for selection in self.selections.all_adjusted(cx) {
2941 let cursor = selection.head();
2942 let row = cursor.row;
2943
2944 let start_of_line = snapshot.clip_point(Point::new(row, 0), Bias::Left);
2945
2946 let newline = "\n".to_string();
2947 edits.push((start_of_line..start_of_line, newline));
2948
2949 rows.push(row + rows_inserted);
2950 rows_inserted += 1;
2951 }
2952
2953 self.transact(cx, |editor, cx| {
2954 editor.edit(edits, cx);
2955
2956 editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
2957 let mut index = 0;
2958 s.move_cursors_with(|map, _, _| {
2959 let row = rows[index];
2960 index += 1;
2961
2962 let point = Point::new(row, 0);
2963 let boundary = map.next_line_boundary(point).1;
2964 let clipped = map.clip_point(boundary, Bias::Left);
2965
2966 (clipped, SelectionGoal::None)
2967 });
2968 });
2969
2970 let mut indent_edits = Vec::new();
2971 let multibuffer_snapshot = editor.buffer.read(cx).snapshot(cx);
2972 for row in rows {
2973 let indents = multibuffer_snapshot.suggested_indents(row..row + 1, cx);
2974 for (row, indent) in indents {
2975 if indent.len == 0 {
2976 continue;
2977 }
2978
2979 let text = match indent.kind {
2980 IndentKind::Space => " ".repeat(indent.len as usize),
2981 IndentKind::Tab => "\t".repeat(indent.len as usize),
2982 };
2983 let point = Point::new(row, 0);
2984 indent_edits.push((point..point, text));
2985 }
2986 }
2987 editor.edit(indent_edits, cx);
2988 });
2989 }
2990
2991 pub fn newline_below(&mut self, _: &NewlineBelow, cx: &mut ViewContext<Self>) {
2992 let buffer = self.buffer.read(cx);
2993 let snapshot = buffer.snapshot(cx);
2994
2995 let mut edits = Vec::new();
2996 let mut rows = Vec::new();
2997 let mut rows_inserted = 0;
2998
2999 for selection in self.selections.all_adjusted(cx) {
3000 let cursor = selection.head();
3001 let row = cursor.row;
3002
3003 let point = Point::new(row + 1, 0);
3004 let start_of_line = snapshot.clip_point(point, Bias::Left);
3005
3006 let newline = "\n".to_string();
3007 edits.push((start_of_line..start_of_line, newline));
3008
3009 rows_inserted += 1;
3010 rows.push(row + rows_inserted);
3011 }
3012
3013 self.transact(cx, |editor, cx| {
3014 editor.edit(edits, cx);
3015
3016 editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
3017 let mut index = 0;
3018 s.move_cursors_with(|map, _, _| {
3019 let row = rows[index];
3020 index += 1;
3021
3022 let point = Point::new(row, 0);
3023 let boundary = map.next_line_boundary(point).1;
3024 let clipped = map.clip_point(boundary, Bias::Left);
3025
3026 (clipped, SelectionGoal::None)
3027 });
3028 });
3029
3030 let mut indent_edits = Vec::new();
3031 let multibuffer_snapshot = editor.buffer.read(cx).snapshot(cx);
3032 for row in rows {
3033 let indents = multibuffer_snapshot.suggested_indents(row..row + 1, cx);
3034 for (row, indent) in indents {
3035 if indent.len == 0 {
3036 continue;
3037 }
3038
3039 let text = match indent.kind {
3040 IndentKind::Space => " ".repeat(indent.len as usize),
3041 IndentKind::Tab => "\t".repeat(indent.len as usize),
3042 };
3043 let point = Point::new(row, 0);
3044 indent_edits.push((point..point, text));
3045 }
3046 }
3047 editor.edit(indent_edits, cx);
3048 });
3049 }
3050
3051 pub fn insert(&mut self, text: &str, cx: &mut ViewContext<Self>) {
3052 self.insert_with_autoindent_mode(
3053 text,
3054 Some(AutoindentMode::Block {
3055 original_indent_columns: Vec::new(),
3056 }),
3057 cx,
3058 );
3059 }
3060
3061 fn insert_with_autoindent_mode(
3062 &mut self,
3063 text: &str,
3064 autoindent_mode: Option<AutoindentMode>,
3065 cx: &mut ViewContext<Self>,
3066 ) {
3067 if self.read_only(cx) {
3068 return;
3069 }
3070
3071 let text: Arc<str> = text.into();
3072 self.transact(cx, |this, cx| {
3073 let old_selections = this.selections.all_adjusted(cx);
3074 let selection_anchors = this.buffer.update(cx, |buffer, cx| {
3075 let anchors = {
3076 let snapshot = buffer.read(cx);
3077 old_selections
3078 .iter()
3079 .map(|s| {
3080 let anchor = snapshot.anchor_after(s.head());
3081 s.map(|_| anchor)
3082 })
3083 .collect::<Vec<_>>()
3084 };
3085 buffer.edit(
3086 old_selections
3087 .iter()
3088 .map(|s| (s.start..s.end, text.clone())),
3089 autoindent_mode,
3090 cx,
3091 );
3092 anchors
3093 });
3094
3095 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
3096 s.select_anchors(selection_anchors);
3097 })
3098 });
3099 }
3100
3101 fn trigger_completion_on_input(&mut self, text: &str, cx: &mut ViewContext<Self>) {
3102 if !EditorSettings::get_global(cx).show_completions_on_input {
3103 return;
3104 }
3105
3106 let selection = self.selections.newest_anchor();
3107 if self
3108 .buffer
3109 .read(cx)
3110 .is_completion_trigger(selection.head(), text, cx)
3111 {
3112 self.show_completions(&ShowCompletions, cx);
3113 } else {
3114 self.hide_context_menu(cx);
3115 }
3116 }
3117
3118 /// If any empty selections is touching the start of its innermost containing autoclose
3119 /// region, expand it to select the brackets.
3120 fn select_autoclose_pair(&mut self, cx: &mut ViewContext<Self>) {
3121 let selections = self.selections.all::<usize>(cx);
3122 let buffer = self.buffer.read(cx).read(cx);
3123 let mut new_selections = Vec::new();
3124 for (mut selection, region) in self.selections_with_autoclose_regions(selections, &buffer) {
3125 if let (Some(region), true) = (region, selection.is_empty()) {
3126 let mut range = region.range.to_offset(&buffer);
3127 if selection.start == range.start {
3128 if range.start >= region.pair.start.len() {
3129 range.start -= region.pair.start.len();
3130 if buffer.contains_str_at(range.start, ®ion.pair.start) {
3131 if buffer.contains_str_at(range.end, ®ion.pair.end) {
3132 range.end += region.pair.end.len();
3133 selection.start = range.start;
3134 selection.end = range.end;
3135 }
3136 }
3137 }
3138 }
3139 }
3140 new_selections.push(selection);
3141 }
3142
3143 drop(buffer);
3144 self.change_selections(None, cx, |selections| selections.select(new_selections));
3145 }
3146
3147 /// Iterate the given selections, and for each one, find the smallest surrounding
3148 /// autoclose region. This uses the ordering of the selections and the autoclose
3149 /// regions to avoid repeated comparisons.
3150 fn selections_with_autoclose_regions<'a, D: ToOffset + Clone>(
3151 &'a self,
3152 selections: impl IntoIterator<Item = Selection<D>>,
3153 buffer: &'a MultiBufferSnapshot,
3154 ) -> impl Iterator<Item = (Selection<D>, Option<&'a AutocloseRegion>)> {
3155 let mut i = 0;
3156 let mut regions = self.autoclose_regions.as_slice();
3157 selections.into_iter().map(move |selection| {
3158 let range = selection.start.to_offset(buffer)..selection.end.to_offset(buffer);
3159
3160 let mut enclosing = None;
3161 while let Some(pair_state) = regions.get(i) {
3162 if pair_state.range.end.to_offset(buffer) < range.start {
3163 regions = ®ions[i + 1..];
3164 i = 0;
3165 } else if pair_state.range.start.to_offset(buffer) > range.end {
3166 break;
3167 } else {
3168 if pair_state.selection_id == selection.id {
3169 enclosing = Some(pair_state);
3170 }
3171 i += 1;
3172 }
3173 }
3174
3175 (selection.clone(), enclosing)
3176 })
3177 }
3178
3179 /// Remove any autoclose regions that no longer contain their selection.
3180 fn invalidate_autoclose_regions(
3181 &mut self,
3182 mut selections: &[Selection<Anchor>],
3183 buffer: &MultiBufferSnapshot,
3184 ) {
3185 self.autoclose_regions.retain(|state| {
3186 let mut i = 0;
3187 while let Some(selection) = selections.get(i) {
3188 if selection.end.cmp(&state.range.start, buffer).is_lt() {
3189 selections = &selections[1..];
3190 continue;
3191 }
3192 if selection.start.cmp(&state.range.end, buffer).is_gt() {
3193 break;
3194 }
3195 if selection.id == state.selection_id {
3196 return true;
3197 } else {
3198 i += 1;
3199 }
3200 }
3201 false
3202 });
3203 }
3204
3205 fn completion_query(buffer: &MultiBufferSnapshot, position: impl ToOffset) -> Option<String> {
3206 let offset = position.to_offset(buffer);
3207 let (word_range, kind) = buffer.surrounding_word(offset);
3208 if offset > word_range.start && kind == Some(CharKind::Word) {
3209 Some(
3210 buffer
3211 .text_for_range(word_range.start..offset)
3212 .collect::<String>(),
3213 )
3214 } else {
3215 None
3216 }
3217 }
3218
3219 pub fn toggle_inlay_hints(&mut self, _: &ToggleInlayHints, cx: &mut ViewContext<Self>) {
3220 self.refresh_inlay_hints(
3221 InlayHintRefreshReason::Toggle(!self.inlay_hint_cache.enabled),
3222 cx,
3223 );
3224 }
3225
3226 pub fn inlay_hints_enabled(&self) -> bool {
3227 self.inlay_hint_cache.enabled
3228 }
3229
3230 fn refresh_inlay_hints(&mut self, reason: InlayHintRefreshReason, cx: &mut ViewContext<Self>) {
3231 if self.project.is_none() || self.mode != EditorMode::Full {
3232 return;
3233 }
3234
3235 let reason_description = reason.description();
3236 let (invalidate_cache, required_languages) = match reason {
3237 InlayHintRefreshReason::Toggle(enabled) => {
3238 self.inlay_hint_cache.enabled = enabled;
3239 if enabled {
3240 (InvalidationStrategy::RefreshRequested, None)
3241 } else {
3242 self.inlay_hint_cache.clear();
3243 self.splice_inlay_hints(
3244 self.visible_inlay_hints(cx)
3245 .iter()
3246 .map(|inlay| inlay.id)
3247 .collect(),
3248 Vec::new(),
3249 cx,
3250 );
3251 return;
3252 }
3253 }
3254 InlayHintRefreshReason::SettingsChange(new_settings) => {
3255 match self.inlay_hint_cache.update_settings(
3256 &self.buffer,
3257 new_settings,
3258 self.visible_inlay_hints(cx),
3259 cx,
3260 ) {
3261 ControlFlow::Break(Some(InlaySplice {
3262 to_remove,
3263 to_insert,
3264 })) => {
3265 self.splice_inlay_hints(to_remove, to_insert, cx);
3266 return;
3267 }
3268 ControlFlow::Break(None) => return,
3269 ControlFlow::Continue(()) => (InvalidationStrategy::RefreshRequested, None),
3270 }
3271 }
3272 InlayHintRefreshReason::ExcerptsRemoved(excerpts_removed) => {
3273 if let Some(InlaySplice {
3274 to_remove,
3275 to_insert,
3276 }) = self.inlay_hint_cache.remove_excerpts(excerpts_removed)
3277 {
3278 self.splice_inlay_hints(to_remove, to_insert, cx);
3279 }
3280 return;
3281 }
3282 InlayHintRefreshReason::NewLinesShown => (InvalidationStrategy::None, None),
3283 InlayHintRefreshReason::BufferEdited(buffer_languages) => {
3284 (InvalidationStrategy::BufferEdited, Some(buffer_languages))
3285 }
3286 InlayHintRefreshReason::RefreshRequested => {
3287 (InvalidationStrategy::RefreshRequested, None)
3288 }
3289 };
3290
3291 if let Some(InlaySplice {
3292 to_remove,
3293 to_insert,
3294 }) = self.inlay_hint_cache.spawn_hint_refresh(
3295 reason_description,
3296 self.excerpts_for_inlay_hints_query(required_languages.as_ref(), cx),
3297 invalidate_cache,
3298 cx,
3299 ) {
3300 self.splice_inlay_hints(to_remove, to_insert, cx);
3301 }
3302 }
3303
3304 fn visible_inlay_hints(&self, cx: &ViewContext<'_, Editor>) -> Vec<Inlay> {
3305 self.display_map
3306 .read(cx)
3307 .current_inlays()
3308 .filter(move |inlay| {
3309 Some(inlay.id) != self.copilot_state.suggestion.as_ref().map(|h| h.id)
3310 })
3311 .cloned()
3312 .collect()
3313 }
3314
3315 pub fn excerpts_for_inlay_hints_query(
3316 &self,
3317 restrict_to_languages: Option<&HashSet<Arc<Language>>>,
3318 cx: &mut ViewContext<Editor>,
3319 ) -> HashMap<ExcerptId, (Model<Buffer>, clock::Global, Range<usize>)> {
3320 let Some(project) = self.project.as_ref() else {
3321 return HashMap::default();
3322 };
3323 let project = project.read(cx);
3324 let multi_buffer = self.buffer().read(cx);
3325 let multi_buffer_snapshot = multi_buffer.snapshot(cx);
3326 let multi_buffer_visible_start = self
3327 .scroll_manager
3328 .anchor()
3329 .anchor
3330 .to_point(&multi_buffer_snapshot);
3331 let multi_buffer_visible_end = multi_buffer_snapshot.clip_point(
3332 multi_buffer_visible_start
3333 + Point::new(self.visible_line_count().unwrap_or(0.).ceil() as u32, 0),
3334 Bias::Left,
3335 );
3336 let multi_buffer_visible_range = multi_buffer_visible_start..multi_buffer_visible_end;
3337 multi_buffer
3338 .range_to_buffer_ranges(multi_buffer_visible_range, cx)
3339 .into_iter()
3340 .filter(|(_, excerpt_visible_range, _)| !excerpt_visible_range.is_empty())
3341 .filter_map(|(buffer_handle, excerpt_visible_range, excerpt_id)| {
3342 let buffer = buffer_handle.read(cx);
3343 let buffer_file = project::worktree::File::from_dyn(buffer.file())?;
3344 let buffer_worktree = project.worktree_for_id(buffer_file.worktree_id(cx), cx)?;
3345 let worktree_entry = buffer_worktree
3346 .read(cx)
3347 .entry_for_id(buffer_file.project_entry_id(cx)?)?;
3348 if worktree_entry.is_ignored {
3349 return None;
3350 }
3351
3352 let language = buffer.language()?;
3353 if let Some(restrict_to_languages) = restrict_to_languages {
3354 if !restrict_to_languages.contains(language) {
3355 return None;
3356 }
3357 }
3358 Some((
3359 excerpt_id,
3360 (
3361 buffer_handle,
3362 buffer.version().clone(),
3363 excerpt_visible_range,
3364 ),
3365 ))
3366 })
3367 .collect()
3368 }
3369
3370 pub fn text_layout_details(&self, cx: &WindowContext) -> TextLayoutDetails {
3371 TextLayoutDetails {
3372 text_system: cx.text_system().clone(),
3373 editor_style: self.style.clone().unwrap(),
3374 rem_size: cx.rem_size(),
3375 }
3376 }
3377
3378 fn splice_inlay_hints(
3379 &self,
3380 to_remove: Vec<InlayId>,
3381 to_insert: Vec<Inlay>,
3382 cx: &mut ViewContext<Self>,
3383 ) {
3384 self.display_map.update(cx, |display_map, cx| {
3385 display_map.splice_inlays(to_remove, to_insert, cx);
3386 });
3387 cx.notify();
3388 }
3389
3390 fn trigger_on_type_formatting(
3391 &self,
3392 input: String,
3393 cx: &mut ViewContext<Self>,
3394 ) -> Option<Task<Result<()>>> {
3395 if input.len() != 1 {
3396 return None;
3397 }
3398
3399 let project = self.project.as_ref()?;
3400 let position = self.selections.newest_anchor().head();
3401 let (buffer, buffer_position) = self
3402 .buffer
3403 .read(cx)
3404 .text_anchor_for_position(position.clone(), cx)?;
3405
3406 // OnTypeFormatting returns a list of edits, no need to pass them between Zed instances,
3407 // hence we do LSP request & edit on host side only — add formats to host's history.
3408 let push_to_lsp_host_history = true;
3409 // If this is not the host, append its history with new edits.
3410 let push_to_client_history = project.read(cx).is_remote();
3411
3412 let on_type_formatting = project.update(cx, |project, cx| {
3413 project.on_type_format(
3414 buffer.clone(),
3415 buffer_position,
3416 input,
3417 push_to_lsp_host_history,
3418 cx,
3419 )
3420 });
3421 Some(cx.spawn(|editor, mut cx| async move {
3422 if let Some(transaction) = on_type_formatting.await? {
3423 if push_to_client_history {
3424 buffer
3425 .update(&mut cx, |buffer, _| {
3426 buffer.push_transaction(transaction, Instant::now());
3427 })
3428 .ok();
3429 }
3430 editor.update(&mut cx, |editor, cx| {
3431 editor.refresh_document_highlights(cx);
3432 })?;
3433 }
3434 Ok(())
3435 }))
3436 }
3437
3438 fn show_completions(&mut self, _: &ShowCompletions, cx: &mut ViewContext<Self>) {
3439 if self.pending_rename.is_some() {
3440 return;
3441 }
3442
3443 let project = if let Some(project) = self.project.clone() {
3444 project
3445 } else {
3446 return;
3447 };
3448
3449 let position = self.selections.newest_anchor().head();
3450 let (buffer, buffer_position) = if let Some(output) = self
3451 .buffer
3452 .read(cx)
3453 .text_anchor_for_position(position.clone(), cx)
3454 {
3455 output
3456 } else {
3457 return;
3458 };
3459
3460 let query = Self::completion_query(&self.buffer.read(cx).read(cx), position.clone());
3461 let completions = project.update(cx, |project, cx| {
3462 project.completions(&buffer, buffer_position, cx)
3463 });
3464
3465 let id = post_inc(&mut self.next_completion_id);
3466 let task = cx.spawn(|this, mut cx| {
3467 async move {
3468 let completions = completions.await.log_err();
3469 let (menu, pre_resolve_task) = if let Some(completions) = completions {
3470 let mut menu = CompletionsMenu {
3471 id,
3472 initial_position: position,
3473 match_candidates: completions
3474 .iter()
3475 .enumerate()
3476 .map(|(id, completion)| {
3477 StringMatchCandidate::new(
3478 id,
3479 completion.label.text[completion.label.filter_range.clone()]
3480 .into(),
3481 )
3482 })
3483 .collect(),
3484 buffer,
3485 completions: Arc::new(RwLock::new(completions.into())),
3486 matches: Vec::new().into(),
3487 selected_item: 0,
3488 scroll_handle: UniformListScrollHandle::new(),
3489 };
3490 menu.filter(query.as_deref(), cx.background_executor().clone())
3491 .await;
3492
3493 if menu.matches.is_empty() {
3494 (None, None)
3495 } else {
3496 let pre_resolve_task = this
3497 .update(&mut cx, |editor, cx| {
3498 menu.pre_resolve_completion_documentation(editor, cx)
3499 })
3500 .ok()
3501 .flatten();
3502 (Some(menu), pre_resolve_task)
3503 }
3504 } else {
3505 (None, None)
3506 };
3507
3508 this.update(&mut cx, |this, cx| {
3509 this.completion_tasks.retain(|(task_id, _)| *task_id >= id);
3510
3511 let mut context_menu = this.context_menu.write();
3512 match context_menu.as_ref() {
3513 None => {}
3514
3515 Some(ContextMenu::Completions(prev_menu)) => {
3516 if prev_menu.id > id {
3517 return;
3518 }
3519 }
3520
3521 _ => return,
3522 }
3523
3524 if this.focus_handle.is_focused(cx) && menu.is_some() {
3525 let menu = menu.unwrap();
3526 *context_menu = Some(ContextMenu::Completions(menu));
3527 drop(context_menu);
3528 this.discard_copilot_suggestion(cx);
3529 cx.notify();
3530 } else if this.completion_tasks.len() <= 1 {
3531 // If there are no more completion tasks and the last menu was
3532 // empty, we should hide it. If it was already hidden, we should
3533 // also show the copilot suggestion when available.
3534 drop(context_menu);
3535 if this.hide_context_menu(cx).is_none() {
3536 this.update_visible_copilot_suggestion(cx);
3537 }
3538 }
3539 })?;
3540
3541 if let Some(pre_resolve_task) = pre_resolve_task {
3542 pre_resolve_task.await;
3543 }
3544
3545 Ok::<_, anyhow::Error>(())
3546 }
3547 .log_err()
3548 });
3549
3550 self.completion_tasks.push((id, task));
3551 }
3552
3553 pub fn confirm_completion(
3554 &mut self,
3555 action: &ConfirmCompletion,
3556 cx: &mut ViewContext<Self>,
3557 ) -> Option<Task<Result<()>>> {
3558 use language::ToOffset as _;
3559
3560 let completions_menu = if let ContextMenu::Completions(menu) = self.hide_context_menu(cx)? {
3561 menu
3562 } else {
3563 return None;
3564 };
3565
3566 let mat = completions_menu
3567 .matches
3568 .get(action.item_ix.unwrap_or(completions_menu.selected_item))?;
3569 let buffer_handle = completions_menu.buffer;
3570 let completions = completions_menu.completions.read();
3571 let completion = completions.get(mat.candidate_id)?;
3572
3573 let snippet;
3574 let text;
3575 if completion.is_snippet() {
3576 snippet = Some(Snippet::parse(&completion.new_text).log_err()?);
3577 text = snippet.as_ref().unwrap().text.clone();
3578 } else {
3579 snippet = None;
3580 text = completion.new_text.clone();
3581 };
3582 let selections = self.selections.all::<usize>(cx);
3583 let buffer = buffer_handle.read(cx);
3584 let old_range = completion.old_range.to_offset(buffer);
3585 let old_text = buffer.text_for_range(old_range.clone()).collect::<String>();
3586
3587 let newest_selection = self.selections.newest_anchor();
3588 if newest_selection.start.buffer_id != Some(buffer_handle.read(cx).remote_id()) {
3589 return None;
3590 }
3591
3592 let lookbehind = newest_selection
3593 .start
3594 .text_anchor
3595 .to_offset(buffer)
3596 .saturating_sub(old_range.start);
3597 let lookahead = old_range
3598 .end
3599 .saturating_sub(newest_selection.end.text_anchor.to_offset(buffer));
3600 let mut common_prefix_len = old_text
3601 .bytes()
3602 .zip(text.bytes())
3603 .take_while(|(a, b)| a == b)
3604 .count();
3605
3606 let snapshot = self.buffer.read(cx).snapshot(cx);
3607 let mut range_to_replace: Option<Range<isize>> = None;
3608 let mut ranges = Vec::new();
3609 for selection in &selections {
3610 if snapshot.contains_str_at(selection.start.saturating_sub(lookbehind), &old_text) {
3611 let start = selection.start.saturating_sub(lookbehind);
3612 let end = selection.end + lookahead;
3613 if selection.id == newest_selection.id {
3614 range_to_replace = Some(
3615 ((start + common_prefix_len) as isize - selection.start as isize)
3616 ..(end as isize - selection.start as isize),
3617 );
3618 }
3619 ranges.push(start + common_prefix_len..end);
3620 } else {
3621 common_prefix_len = 0;
3622 ranges.clear();
3623 ranges.extend(selections.iter().map(|s| {
3624 if s.id == newest_selection.id {
3625 range_to_replace = Some(
3626 old_range.start.to_offset_utf16(&snapshot).0 as isize
3627 - selection.start as isize
3628 ..old_range.end.to_offset_utf16(&snapshot).0 as isize
3629 - selection.start as isize,
3630 );
3631 old_range.clone()
3632 } else {
3633 s.start..s.end
3634 }
3635 }));
3636 break;
3637 }
3638 }
3639 let text = &text[common_prefix_len..];
3640
3641 cx.emit(EditorEvent::InputHandled {
3642 utf16_range_to_replace: range_to_replace,
3643 text: text.into(),
3644 });
3645
3646 self.transact(cx, |this, cx| {
3647 if let Some(mut snippet) = snippet {
3648 snippet.text = text.to_string();
3649 for tabstop in snippet.tabstops.iter_mut().flatten() {
3650 tabstop.start -= common_prefix_len as isize;
3651 tabstop.end -= common_prefix_len as isize;
3652 }
3653
3654 this.insert_snippet(&ranges, snippet, cx).log_err();
3655 } else {
3656 this.buffer.update(cx, |buffer, cx| {
3657 buffer.edit(
3658 ranges.iter().map(|range| (range.clone(), text)),
3659 this.autoindent_mode.clone(),
3660 cx,
3661 );
3662 });
3663 }
3664
3665 this.refresh_copilot_suggestions(true, cx);
3666 });
3667
3668 let project = self.project.clone()?;
3669 let apply_edits = project.update(cx, |project, cx| {
3670 project.apply_additional_edits_for_completion(
3671 buffer_handle,
3672 completion.clone(),
3673 true,
3674 cx,
3675 )
3676 });
3677 Some(cx.foreground_executor().spawn(async move {
3678 apply_edits.await?;
3679 Ok(())
3680 }))
3681 }
3682
3683 pub fn toggle_code_actions(&mut self, action: &ToggleCodeActions, cx: &mut ViewContext<Self>) {
3684 let mut context_menu = self.context_menu.write();
3685 if matches!(context_menu.as_ref(), Some(ContextMenu::CodeActions(_))) {
3686 *context_menu = None;
3687 cx.notify();
3688 return;
3689 }
3690 drop(context_menu);
3691
3692 let deployed_from_indicator = action.deployed_from_indicator;
3693 let mut task = self.code_actions_task.take();
3694 cx.spawn(|this, mut cx| async move {
3695 while let Some(prev_task) = task {
3696 prev_task.await;
3697 task = this.update(&mut cx, |this, _| this.code_actions_task.take())?;
3698 }
3699
3700 this.update(&mut cx, |this, cx| {
3701 if this.focus_handle.is_focused(cx) {
3702 if let Some((buffer, actions)) = this.available_code_actions.clone() {
3703 this.completion_tasks.clear();
3704 this.discard_copilot_suggestion(cx);
3705 *this.context_menu.write() =
3706 Some(ContextMenu::CodeActions(CodeActionsMenu {
3707 buffer,
3708 actions,
3709 selected_item: Default::default(),
3710 scroll_handle: UniformListScrollHandle::default(),
3711 deployed_from_indicator,
3712 }));
3713 cx.notify();
3714 }
3715 }
3716 })?;
3717
3718 Ok::<_, anyhow::Error>(())
3719 })
3720 .detach_and_log_err(cx);
3721 }
3722
3723 pub fn confirm_code_action(
3724 &mut self,
3725 action: &ConfirmCodeAction,
3726 cx: &mut ViewContext<Self>,
3727 ) -> Option<Task<Result<()>>> {
3728 let actions_menu = if let ContextMenu::CodeActions(menu) = self.hide_context_menu(cx)? {
3729 menu
3730 } else {
3731 return None;
3732 };
3733 let action_ix = action.item_ix.unwrap_or(actions_menu.selected_item);
3734 let action = actions_menu.actions.get(action_ix)?.clone();
3735 let title = action.lsp_action.title.clone();
3736 let buffer = actions_menu.buffer;
3737 let workspace = self.workspace()?;
3738
3739 let apply_code_actions = workspace
3740 .read(cx)
3741 .project()
3742 .clone()
3743 .update(cx, |project, cx| {
3744 project.apply_code_action(buffer, action, true, cx)
3745 });
3746 let workspace = workspace.downgrade();
3747 Some(cx.spawn(|editor, cx| async move {
3748 let project_transaction = apply_code_actions.await?;
3749 Self::open_project_transaction(&editor, workspace, project_transaction, title, cx).await
3750 }))
3751 }
3752
3753 async fn open_project_transaction(
3754 this: &WeakView<Editor>,
3755 workspace: WeakView<Workspace>,
3756 transaction: ProjectTransaction,
3757 title: String,
3758 mut cx: AsyncWindowContext,
3759 ) -> Result<()> {
3760 let replica_id = this.update(&mut cx, |this, cx| this.replica_id(cx))?;
3761
3762 let mut entries = transaction.0.into_iter().collect::<Vec<_>>();
3763 cx.update(|_, cx| {
3764 entries.sort_unstable_by_key(|(buffer, _)| {
3765 buffer.read(cx).file().map(|f| f.path().clone())
3766 });
3767 })?;
3768
3769 // If the project transaction's edits are all contained within this editor, then
3770 // avoid opening a new editor to display them.
3771
3772 if let Some((buffer, transaction)) = entries.first() {
3773 if entries.len() == 1 {
3774 let excerpt = this.update(&mut cx, |editor, cx| {
3775 editor
3776 .buffer()
3777 .read(cx)
3778 .excerpt_containing(editor.selections.newest_anchor().head(), cx)
3779 })?;
3780 if let Some((_, excerpted_buffer, excerpt_range)) = excerpt {
3781 if excerpted_buffer == *buffer {
3782 let all_edits_within_excerpt = buffer.read_with(&cx, |buffer, _| {
3783 let excerpt_range = excerpt_range.to_offset(buffer);
3784 buffer
3785 .edited_ranges_for_transaction::<usize>(transaction)
3786 .all(|range| {
3787 excerpt_range.start <= range.start
3788 && excerpt_range.end >= range.end
3789 })
3790 })?;
3791
3792 if all_edits_within_excerpt {
3793 return Ok(());
3794 }
3795 }
3796 }
3797 }
3798 } else {
3799 return Ok(());
3800 }
3801
3802 let mut ranges_to_highlight = Vec::new();
3803 let excerpt_buffer = cx.new_model(|cx| {
3804 let mut multibuffer =
3805 MultiBuffer::new(replica_id, Capability::ReadWrite).with_title(title);
3806 for (buffer_handle, transaction) in &entries {
3807 let buffer = buffer_handle.read(cx);
3808 ranges_to_highlight.extend(
3809 multibuffer.push_excerpts_with_context_lines(
3810 buffer_handle.clone(),
3811 buffer
3812 .edited_ranges_for_transaction::<usize>(transaction)
3813 .collect(),
3814 1,
3815 cx,
3816 ),
3817 );
3818 }
3819 multibuffer.push_transaction(entries.iter().map(|(b, t)| (b, t)), cx);
3820 multibuffer
3821 })?;
3822
3823 workspace.update(&mut cx, |workspace, cx| {
3824 let project = workspace.project().clone();
3825 let editor =
3826 cx.new_view(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), cx));
3827 workspace.add_item(Box::new(editor.clone()), cx);
3828 editor.update(cx, |editor, cx| {
3829 editor.highlight_background::<Self>(
3830 ranges_to_highlight,
3831 |theme| theme.editor_highlighted_line_background,
3832 cx,
3833 );
3834 });
3835 })?;
3836
3837 Ok(())
3838 }
3839
3840 fn refresh_code_actions(&mut self, cx: &mut ViewContext<Self>) -> Option<()> {
3841 let project = self.project.clone()?;
3842 let buffer = self.buffer.read(cx);
3843 let newest_selection = self.selections.newest_anchor().clone();
3844 let (start_buffer, start) = buffer.text_anchor_for_position(newest_selection.start, cx)?;
3845 let (end_buffer, end) = buffer.text_anchor_for_position(newest_selection.end, cx)?;
3846 if start_buffer != end_buffer {
3847 return None;
3848 }
3849
3850 self.code_actions_task = Some(cx.spawn(|this, mut cx| async move {
3851 cx.background_executor()
3852 .timer(CODE_ACTIONS_DEBOUNCE_TIMEOUT)
3853 .await;
3854
3855 let actions = if let Ok(code_actions) = project.update(&mut cx, |project, cx| {
3856 project.code_actions(&start_buffer, start..end, cx)
3857 }) {
3858 code_actions.await.log_err()
3859 } else {
3860 None
3861 };
3862
3863 this.update(&mut cx, |this, cx| {
3864 this.available_code_actions = actions.and_then(|actions| {
3865 if actions.is_empty() {
3866 None
3867 } else {
3868 Some((start_buffer, actions.into()))
3869 }
3870 });
3871 cx.notify();
3872 })
3873 .log_err();
3874 }));
3875 None
3876 }
3877
3878 fn refresh_document_highlights(&mut self, cx: &mut ViewContext<Self>) -> Option<()> {
3879 if self.pending_rename.is_some() {
3880 return None;
3881 }
3882
3883 let project = self.project.clone()?;
3884 let buffer = self.buffer.read(cx);
3885 let newest_selection = self.selections.newest_anchor().clone();
3886 let cursor_position = newest_selection.head();
3887 let (cursor_buffer, cursor_buffer_position) =
3888 buffer.text_anchor_for_position(cursor_position.clone(), cx)?;
3889 let (tail_buffer, _) = buffer.text_anchor_for_position(newest_selection.tail(), cx)?;
3890 if cursor_buffer != tail_buffer {
3891 return None;
3892 }
3893
3894 self.document_highlights_task = Some(cx.spawn(|this, mut cx| async move {
3895 cx.background_executor()
3896 .timer(DOCUMENT_HIGHLIGHTS_DEBOUNCE_TIMEOUT)
3897 .await;
3898
3899 let highlights = if let Some(highlights) = project
3900 .update(&mut cx, |project, cx| {
3901 project.document_highlights(&cursor_buffer, cursor_buffer_position, cx)
3902 })
3903 .log_err()
3904 {
3905 highlights.await.log_err()
3906 } else {
3907 None
3908 };
3909
3910 if let Some(highlights) = highlights {
3911 this.update(&mut cx, |this, cx| {
3912 if this.pending_rename.is_some() {
3913 return;
3914 }
3915
3916 let buffer_id = cursor_position.buffer_id;
3917 let buffer = this.buffer.read(cx);
3918 if !buffer
3919 .text_anchor_for_position(cursor_position, cx)
3920 .map_or(false, |(buffer, _)| buffer == cursor_buffer)
3921 {
3922 return;
3923 }
3924
3925 let cursor_buffer_snapshot = cursor_buffer.read(cx);
3926 let mut write_ranges = Vec::new();
3927 let mut read_ranges = Vec::new();
3928 for highlight in highlights {
3929 for (excerpt_id, excerpt_range) in
3930 buffer.excerpts_for_buffer(&cursor_buffer, cx)
3931 {
3932 let start = highlight
3933 .range
3934 .start
3935 .max(&excerpt_range.context.start, cursor_buffer_snapshot);
3936 let end = highlight
3937 .range
3938 .end
3939 .min(&excerpt_range.context.end, cursor_buffer_snapshot);
3940 if start.cmp(&end, cursor_buffer_snapshot).is_ge() {
3941 continue;
3942 }
3943
3944 let range = Anchor {
3945 buffer_id,
3946 excerpt_id: excerpt_id.clone(),
3947 text_anchor: start,
3948 }..Anchor {
3949 buffer_id,
3950 excerpt_id,
3951 text_anchor: end,
3952 };
3953 if highlight.kind == lsp::DocumentHighlightKind::WRITE {
3954 write_ranges.push(range);
3955 } else {
3956 read_ranges.push(range);
3957 }
3958 }
3959 }
3960
3961 this.highlight_background::<DocumentHighlightRead>(
3962 read_ranges,
3963 |theme| theme.editor_document_highlight_read_background,
3964 cx,
3965 );
3966 this.highlight_background::<DocumentHighlightWrite>(
3967 write_ranges,
3968 |theme| theme.editor_document_highlight_write_background,
3969 cx,
3970 );
3971 cx.notify();
3972 })
3973 .log_err();
3974 }
3975 }));
3976 None
3977 }
3978
3979 fn refresh_copilot_suggestions(
3980 &mut self,
3981 debounce: bool,
3982 cx: &mut ViewContext<Self>,
3983 ) -> Option<()> {
3984 let copilot = Copilot::global(cx)?;
3985 if !self.show_copilot_suggestions || !copilot.read(cx).status().is_authorized() {
3986 self.clear_copilot_suggestions(cx);
3987 return None;
3988 }
3989 self.update_visible_copilot_suggestion(cx);
3990
3991 let snapshot = self.buffer.read(cx).snapshot(cx);
3992 let cursor = self.selections.newest_anchor().head();
3993 if !self.is_copilot_enabled_at(cursor, &snapshot, cx) {
3994 self.clear_copilot_suggestions(cx);
3995 return None;
3996 }
3997
3998 let (buffer, buffer_position) =
3999 self.buffer.read(cx).text_anchor_for_position(cursor, cx)?;
4000 self.copilot_state.pending_refresh = cx.spawn(|this, mut cx| async move {
4001 if debounce {
4002 cx.background_executor()
4003 .timer(COPILOT_DEBOUNCE_TIMEOUT)
4004 .await;
4005 }
4006
4007 let completions = copilot
4008 .update(&mut cx, |copilot, cx| {
4009 copilot.completions(&buffer, buffer_position, cx)
4010 })
4011 .log_err()
4012 .unwrap_or(Task::ready(Ok(Vec::new())))
4013 .await
4014 .log_err()
4015 .into_iter()
4016 .flatten()
4017 .collect_vec();
4018
4019 this.update(&mut cx, |this, cx| {
4020 if !completions.is_empty() {
4021 this.copilot_state.cycled = false;
4022 this.copilot_state.pending_cycling_refresh = Task::ready(None);
4023 this.copilot_state.completions.clear();
4024 this.copilot_state.active_completion_index = 0;
4025 this.copilot_state.excerpt_id = Some(cursor.excerpt_id);
4026 for completion in completions {
4027 this.copilot_state.push_completion(completion);
4028 }
4029 this.update_visible_copilot_suggestion(cx);
4030 }
4031 })
4032 .log_err()?;
4033 Some(())
4034 });
4035
4036 Some(())
4037 }
4038
4039 fn cycle_copilot_suggestions(
4040 &mut self,
4041 direction: Direction,
4042 cx: &mut ViewContext<Self>,
4043 ) -> Option<()> {
4044 let copilot = Copilot::global(cx)?;
4045 if !self.show_copilot_suggestions || !copilot.read(cx).status().is_authorized() {
4046 return None;
4047 }
4048
4049 if self.copilot_state.cycled {
4050 self.copilot_state.cycle_completions(direction);
4051 self.update_visible_copilot_suggestion(cx);
4052 } else {
4053 let cursor = self.selections.newest_anchor().head();
4054 let (buffer, buffer_position) =
4055 self.buffer.read(cx).text_anchor_for_position(cursor, cx)?;
4056 self.copilot_state.pending_cycling_refresh = cx.spawn(|this, mut cx| async move {
4057 let completions = copilot
4058 .update(&mut cx, |copilot, cx| {
4059 copilot.completions_cycling(&buffer, buffer_position, cx)
4060 })
4061 .log_err()?
4062 .await;
4063
4064 this.update(&mut cx, |this, cx| {
4065 this.copilot_state.cycled = true;
4066 for completion in completions.log_err().into_iter().flatten() {
4067 this.copilot_state.push_completion(completion);
4068 }
4069 this.copilot_state.cycle_completions(direction);
4070 this.update_visible_copilot_suggestion(cx);
4071 })
4072 .log_err()?;
4073
4074 Some(())
4075 });
4076 }
4077
4078 Some(())
4079 }
4080
4081 fn copilot_suggest(&mut self, _: &copilot::Suggest, cx: &mut ViewContext<Self>) {
4082 if !self.has_active_copilot_suggestion(cx) {
4083 self.refresh_copilot_suggestions(false, cx);
4084 return;
4085 }
4086
4087 self.update_visible_copilot_suggestion(cx);
4088 }
4089
4090 fn next_copilot_suggestion(&mut self, _: &copilot::NextSuggestion, cx: &mut ViewContext<Self>) {
4091 if self.has_active_copilot_suggestion(cx) {
4092 self.cycle_copilot_suggestions(Direction::Next, cx);
4093 } else {
4094 let is_copilot_disabled = self.refresh_copilot_suggestions(false, cx).is_none();
4095 if is_copilot_disabled {
4096 cx.propagate();
4097 }
4098 }
4099 }
4100
4101 fn previous_copilot_suggestion(
4102 &mut self,
4103 _: &copilot::PreviousSuggestion,
4104 cx: &mut ViewContext<Self>,
4105 ) {
4106 if self.has_active_copilot_suggestion(cx) {
4107 self.cycle_copilot_suggestions(Direction::Prev, cx);
4108 } else {
4109 let is_copilot_disabled = self.refresh_copilot_suggestions(false, cx).is_none();
4110 if is_copilot_disabled {
4111 cx.propagate();
4112 }
4113 }
4114 }
4115
4116 fn accept_copilot_suggestion(&mut self, cx: &mut ViewContext<Self>) -> bool {
4117 if let Some(suggestion) = self.take_active_copilot_suggestion(cx) {
4118 if let Some((copilot, completion)) =
4119 Copilot::global(cx).zip(self.copilot_state.active_completion())
4120 {
4121 copilot
4122 .update(cx, |copilot, cx| copilot.accept_completion(completion, cx))
4123 .detach_and_log_err(cx);
4124
4125 self.report_copilot_event(Some(completion.uuid.clone()), true, cx)
4126 }
4127 cx.emit(EditorEvent::InputHandled {
4128 utf16_range_to_replace: None,
4129 text: suggestion.text.to_string().into(),
4130 });
4131 self.insert_with_autoindent_mode(&suggestion.text.to_string(), None, cx);
4132 cx.notify();
4133 true
4134 } else {
4135 false
4136 }
4137 }
4138
4139 fn discard_copilot_suggestion(&mut self, cx: &mut ViewContext<Self>) -> bool {
4140 if let Some(suggestion) = self.take_active_copilot_suggestion(cx) {
4141 if let Some(copilot) = Copilot::global(cx) {
4142 copilot
4143 .update(cx, |copilot, cx| {
4144 copilot.discard_completions(&self.copilot_state.completions, cx)
4145 })
4146 .detach_and_log_err(cx);
4147
4148 self.report_copilot_event(None, false, cx)
4149 }
4150
4151 self.display_map.update(cx, |map, cx| {
4152 map.splice_inlays(vec![suggestion.id], Vec::new(), cx)
4153 });
4154 cx.notify();
4155 true
4156 } else {
4157 false
4158 }
4159 }
4160
4161 fn is_copilot_enabled_at(
4162 &self,
4163 location: Anchor,
4164 snapshot: &MultiBufferSnapshot,
4165 cx: &mut ViewContext<Self>,
4166 ) -> bool {
4167 let file = snapshot.file_at(location);
4168 let language = snapshot.language_at(location);
4169 let settings = all_language_settings(file, cx);
4170 self.show_copilot_suggestions
4171 && settings.copilot_enabled(language, file.map(|f| f.path().as_ref()))
4172 }
4173
4174 fn has_active_copilot_suggestion(&self, cx: &AppContext) -> bool {
4175 if let Some(suggestion) = self.copilot_state.suggestion.as_ref() {
4176 let buffer = self.buffer.read(cx).read(cx);
4177 suggestion.position.is_valid(&buffer)
4178 } else {
4179 false
4180 }
4181 }
4182
4183 fn take_active_copilot_suggestion(&mut self, cx: &mut ViewContext<Self>) -> Option<Inlay> {
4184 let suggestion = self.copilot_state.suggestion.take()?;
4185 self.display_map.update(cx, |map, cx| {
4186 map.splice_inlays(vec![suggestion.id], Default::default(), cx);
4187 });
4188 let buffer = self.buffer.read(cx).read(cx);
4189
4190 if suggestion.position.is_valid(&buffer) {
4191 Some(suggestion)
4192 } else {
4193 None
4194 }
4195 }
4196
4197 fn update_visible_copilot_suggestion(&mut self, cx: &mut ViewContext<Self>) {
4198 let snapshot = self.buffer.read(cx).snapshot(cx);
4199 let selection = self.selections.newest_anchor();
4200 let cursor = selection.head();
4201
4202 if self.context_menu.read().is_some()
4203 || !self.completion_tasks.is_empty()
4204 || selection.start != selection.end
4205 {
4206 self.discard_copilot_suggestion(cx);
4207 } else if let Some(text) = self
4208 .copilot_state
4209 .text_for_active_completion(cursor, &snapshot)
4210 {
4211 let text = Rope::from(text);
4212 let mut to_remove = Vec::new();
4213 if let Some(suggestion) = self.copilot_state.suggestion.take() {
4214 to_remove.push(suggestion.id);
4215 }
4216
4217 let suggestion_inlay =
4218 Inlay::suggestion(post_inc(&mut self.next_inlay_id), cursor, text);
4219 self.copilot_state.suggestion = Some(suggestion_inlay.clone());
4220 self.display_map.update(cx, move |map, cx| {
4221 map.splice_inlays(to_remove, vec![suggestion_inlay], cx)
4222 });
4223 cx.notify();
4224 } else {
4225 self.discard_copilot_suggestion(cx);
4226 }
4227 }
4228
4229 fn clear_copilot_suggestions(&mut self, cx: &mut ViewContext<Self>) {
4230 self.copilot_state = Default::default();
4231 self.discard_copilot_suggestion(cx);
4232 }
4233
4234 pub fn render_code_actions_indicator(
4235 &self,
4236 _style: &EditorStyle,
4237 is_active: bool,
4238 cx: &mut ViewContext<Self>,
4239 ) -> Option<IconButton> {
4240 if self.available_code_actions.is_some() {
4241 Some(
4242 IconButton::new("code_actions_indicator", ui::IconName::Bolt)
4243 .icon_size(IconSize::Small)
4244 .icon_color(Color::Muted)
4245 .selected(is_active)
4246 .on_click(cx.listener(|editor, _e, cx| {
4247 editor.toggle_code_actions(
4248 &ToggleCodeActions {
4249 deployed_from_indicator: true,
4250 },
4251 cx,
4252 );
4253 })),
4254 )
4255 } else {
4256 None
4257 }
4258 }
4259
4260 pub fn render_fold_indicators(
4261 &self,
4262 fold_data: Vec<Option<(FoldStatus, u32, bool)>>,
4263 _style: &EditorStyle,
4264 gutter_hovered: bool,
4265 _line_height: Pixels,
4266 _gutter_margin: Pixels,
4267 cx: &mut ViewContext<Self>,
4268 ) -> Vec<Option<IconButton>> {
4269 fold_data
4270 .iter()
4271 .enumerate()
4272 .map(|(ix, fold_data)| {
4273 fold_data
4274 .map(|(fold_status, buffer_row, active)| {
4275 (active || gutter_hovered || fold_status == FoldStatus::Folded).then(|| {
4276 IconButton::new(ix as usize, ui::IconName::ChevronDown)
4277 .on_click(cx.listener(move |editor, _e, cx| match fold_status {
4278 FoldStatus::Folded => {
4279 editor.unfold_at(&UnfoldAt { buffer_row }, cx);
4280 }
4281 FoldStatus::Foldable => {
4282 editor.fold_at(&FoldAt { buffer_row }, cx);
4283 }
4284 }))
4285 .icon_color(ui::Color::Muted)
4286 .icon_size(ui::IconSize::Small)
4287 .selected(fold_status == FoldStatus::Folded)
4288 .selected_icon(ui::IconName::ChevronRight)
4289 .size(ui::ButtonSize::None)
4290 })
4291 })
4292 .flatten()
4293 })
4294 .collect()
4295 }
4296
4297 pub fn context_menu_visible(&self) -> bool {
4298 self.context_menu
4299 .read()
4300 .as_ref()
4301 .map_or(false, |menu| menu.visible())
4302 }
4303
4304 pub fn render_context_menu(
4305 &self,
4306 cursor_position: DisplayPoint,
4307 style: &EditorStyle,
4308 max_height: Pixels,
4309 cx: &mut ViewContext<Editor>,
4310 ) -> Option<(DisplayPoint, AnyElement)> {
4311 self.context_menu.read().as_ref().map(|menu| {
4312 menu.render(
4313 cursor_position,
4314 style,
4315 max_height,
4316 self.workspace.as_ref().map(|(w, _)| w.clone()),
4317 cx,
4318 )
4319 })
4320 }
4321
4322 fn hide_context_menu(&mut self, cx: &mut ViewContext<Self>) -> Option<ContextMenu> {
4323 cx.notify();
4324 self.completion_tasks.clear();
4325 let context_menu = self.context_menu.write().take();
4326 if context_menu.is_some() {
4327 self.update_visible_copilot_suggestion(cx);
4328 }
4329 context_menu
4330 }
4331
4332 pub fn insert_snippet(
4333 &mut self,
4334 insertion_ranges: &[Range<usize>],
4335 snippet: Snippet,
4336 cx: &mut ViewContext<Self>,
4337 ) -> Result<()> {
4338 let tabstops = self.buffer.update(cx, |buffer, cx| {
4339 let snippet_text: Arc<str> = snippet.text.clone().into();
4340 buffer.edit(
4341 insertion_ranges
4342 .iter()
4343 .cloned()
4344 .map(|range| (range, snippet_text.clone())),
4345 Some(AutoindentMode::EachLine),
4346 cx,
4347 );
4348
4349 let snapshot = &*buffer.read(cx);
4350 let snippet = &snippet;
4351 snippet
4352 .tabstops
4353 .iter()
4354 .map(|tabstop| {
4355 let mut tabstop_ranges = tabstop
4356 .iter()
4357 .flat_map(|tabstop_range| {
4358 let mut delta = 0_isize;
4359 insertion_ranges.iter().map(move |insertion_range| {
4360 let insertion_start = insertion_range.start as isize + delta;
4361 delta +=
4362 snippet.text.len() as isize - insertion_range.len() as isize;
4363
4364 let start = snapshot.anchor_before(
4365 (insertion_start + tabstop_range.start) as usize,
4366 );
4367 let end = snapshot
4368 .anchor_after((insertion_start + tabstop_range.end) as usize);
4369 start..end
4370 })
4371 })
4372 .collect::<Vec<_>>();
4373 tabstop_ranges.sort_unstable_by(|a, b| a.start.cmp(&b.start, snapshot));
4374 tabstop_ranges
4375 })
4376 .collect::<Vec<_>>()
4377 });
4378
4379 if let Some(tabstop) = tabstops.first() {
4380 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
4381 s.select_ranges(tabstop.iter().cloned());
4382 });
4383 self.snippet_stack.push(SnippetState {
4384 active_index: 0,
4385 ranges: tabstops,
4386 });
4387 }
4388
4389 Ok(())
4390 }
4391
4392 pub fn move_to_next_snippet_tabstop(&mut self, cx: &mut ViewContext<Self>) -> bool {
4393 self.move_to_snippet_tabstop(Bias::Right, cx)
4394 }
4395
4396 pub fn move_to_prev_snippet_tabstop(&mut self, cx: &mut ViewContext<Self>) -> bool {
4397 self.move_to_snippet_tabstop(Bias::Left, cx)
4398 }
4399
4400 pub fn move_to_snippet_tabstop(&mut self, bias: Bias, cx: &mut ViewContext<Self>) -> bool {
4401 if let Some(mut snippet) = self.snippet_stack.pop() {
4402 match bias {
4403 Bias::Left => {
4404 if snippet.active_index > 0 {
4405 snippet.active_index -= 1;
4406 } else {
4407 self.snippet_stack.push(snippet);
4408 return false;
4409 }
4410 }
4411 Bias::Right => {
4412 if snippet.active_index + 1 < snippet.ranges.len() {
4413 snippet.active_index += 1;
4414 } else {
4415 self.snippet_stack.push(snippet);
4416 return false;
4417 }
4418 }
4419 }
4420 if let Some(current_ranges) = snippet.ranges.get(snippet.active_index) {
4421 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
4422 s.select_anchor_ranges(current_ranges.iter().cloned())
4423 });
4424 // If snippet state is not at the last tabstop, push it back on the stack
4425 if snippet.active_index + 1 < snippet.ranges.len() {
4426 self.snippet_stack.push(snippet);
4427 }
4428 return true;
4429 }
4430 }
4431
4432 false
4433 }
4434
4435 pub fn clear(&mut self, cx: &mut ViewContext<Self>) {
4436 self.transact(cx, |this, cx| {
4437 this.select_all(&SelectAll, cx);
4438 this.insert("", cx);
4439 });
4440 }
4441
4442 pub fn backspace(&mut self, _: &Backspace, cx: &mut ViewContext<Self>) {
4443 self.transact(cx, |this, cx| {
4444 this.select_autoclose_pair(cx);
4445 let mut selections = this.selections.all::<Point>(cx);
4446 if !this.selections.line_mode {
4447 let display_map = this.display_map.update(cx, |map, cx| map.snapshot(cx));
4448 for selection in &mut selections {
4449 if selection.is_empty() {
4450 let old_head = selection.head();
4451 let mut new_head =
4452 movement::left(&display_map, old_head.to_display_point(&display_map))
4453 .to_point(&display_map);
4454 if let Some((buffer, line_buffer_range)) = display_map
4455 .buffer_snapshot
4456 .buffer_line_for_row(old_head.row)
4457 {
4458 let indent_size =
4459 buffer.indent_size_for_line(line_buffer_range.start.row);
4460 let indent_len = match indent_size.kind {
4461 IndentKind::Space => {
4462 buffer.settings_at(line_buffer_range.start, cx).tab_size
4463 }
4464 IndentKind::Tab => NonZeroU32::new(1).unwrap(),
4465 };
4466 if old_head.column <= indent_size.len && old_head.column > 0 {
4467 let indent_len = indent_len.get();
4468 new_head = cmp::min(
4469 new_head,
4470 Point::new(
4471 old_head.row,
4472 ((old_head.column - 1) / indent_len) * indent_len,
4473 ),
4474 );
4475 }
4476 }
4477
4478 selection.set_head(new_head, SelectionGoal::None);
4479 }
4480 }
4481 }
4482
4483 this.change_selections(Some(Autoscroll::fit()), cx, |s| s.select(selections));
4484 this.insert("", cx);
4485 this.refresh_copilot_suggestions(true, cx);
4486 });
4487 }
4488
4489 pub fn delete(&mut self, _: &Delete, cx: &mut ViewContext<Self>) {
4490 self.transact(cx, |this, cx| {
4491 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
4492 let line_mode = s.line_mode;
4493 s.move_with(|map, selection| {
4494 if selection.is_empty() && !line_mode {
4495 let cursor = movement::right(map, selection.head());
4496 selection.end = cursor;
4497 selection.reversed = true;
4498 selection.goal = SelectionGoal::None;
4499 }
4500 })
4501 });
4502 this.insert("", cx);
4503 this.refresh_copilot_suggestions(true, cx);
4504 });
4505 }
4506
4507 pub fn tab_prev(&mut self, _: &TabPrev, cx: &mut ViewContext<Self>) {
4508 if self.move_to_prev_snippet_tabstop(cx) {
4509 return;
4510 }
4511
4512 self.outdent(&Outdent, cx);
4513 }
4514
4515 pub fn tab(&mut self, _: &Tab, cx: &mut ViewContext<Self>) {
4516 if self.move_to_next_snippet_tabstop(cx) || self.read_only(cx) {
4517 return;
4518 }
4519
4520 let mut selections = self.selections.all_adjusted(cx);
4521 let buffer = self.buffer.read(cx);
4522 let snapshot = buffer.snapshot(cx);
4523 let rows_iter = selections.iter().map(|s| s.head().row);
4524 let suggested_indents = snapshot.suggested_indents(rows_iter, cx);
4525
4526 let mut edits = Vec::new();
4527 let mut prev_edited_row = 0;
4528 let mut row_delta = 0;
4529 for selection in &mut selections {
4530 if selection.start.row != prev_edited_row {
4531 row_delta = 0;
4532 }
4533 prev_edited_row = selection.end.row;
4534
4535 // If the selection is non-empty, then increase the indentation of the selected lines.
4536 if !selection.is_empty() {
4537 row_delta =
4538 Self::indent_selection(buffer, &snapshot, selection, &mut edits, row_delta, cx);
4539 continue;
4540 }
4541
4542 // If the selection is empty and the cursor is in the leading whitespace before the
4543 // suggested indentation, then auto-indent the line.
4544 let cursor = selection.head();
4545 let current_indent = snapshot.indent_size_for_line(cursor.row);
4546 if let Some(suggested_indent) = suggested_indents.get(&cursor.row).copied() {
4547 if cursor.column < suggested_indent.len
4548 && cursor.column <= current_indent.len
4549 && current_indent.len <= suggested_indent.len
4550 {
4551 selection.start = Point::new(cursor.row, suggested_indent.len);
4552 selection.end = selection.start;
4553 if row_delta == 0 {
4554 edits.extend(Buffer::edit_for_indent_size_adjustment(
4555 cursor.row,
4556 current_indent,
4557 suggested_indent,
4558 ));
4559 row_delta = suggested_indent.len - current_indent.len;
4560 }
4561 continue;
4562 }
4563 }
4564
4565 // Accept copilot suggestion if there is only one selection and the cursor is not
4566 // in the leading whitespace.
4567 if self.selections.count() == 1
4568 && cursor.column >= current_indent.len
4569 && self.has_active_copilot_suggestion(cx)
4570 {
4571 self.accept_copilot_suggestion(cx);
4572 return;
4573 }
4574
4575 // Otherwise, insert a hard or soft tab.
4576 let settings = buffer.settings_at(cursor, cx);
4577 let tab_size = if settings.hard_tabs {
4578 IndentSize::tab()
4579 } else {
4580 let tab_size = settings.tab_size.get();
4581 let char_column = snapshot
4582 .text_for_range(Point::new(cursor.row, 0)..cursor)
4583 .flat_map(str::chars)
4584 .count()
4585 + row_delta as usize;
4586 let chars_to_next_tab_stop = tab_size - (char_column as u32 % tab_size);
4587 IndentSize::spaces(chars_to_next_tab_stop)
4588 };
4589 selection.start = Point::new(cursor.row, cursor.column + row_delta + tab_size.len);
4590 selection.end = selection.start;
4591 edits.push((cursor..cursor, tab_size.chars().collect::<String>()));
4592 row_delta += tab_size.len;
4593 }
4594
4595 self.transact(cx, |this, cx| {
4596 this.buffer.update(cx, |b, cx| b.edit(edits, None, cx));
4597 this.change_selections(Some(Autoscroll::fit()), cx, |s| s.select(selections));
4598 this.refresh_copilot_suggestions(true, cx);
4599 });
4600 }
4601
4602 pub fn indent(&mut self, _: &Indent, cx: &mut ViewContext<Self>) {
4603 let mut selections = self.selections.all::<Point>(cx);
4604 let mut prev_edited_row = 0;
4605 let mut row_delta = 0;
4606 let mut edits = Vec::new();
4607 let buffer = self.buffer.read(cx);
4608 let snapshot = buffer.snapshot(cx);
4609 for selection in &mut selections {
4610 if selection.start.row != prev_edited_row {
4611 row_delta = 0;
4612 }
4613 prev_edited_row = selection.end.row;
4614
4615 row_delta =
4616 Self::indent_selection(buffer, &snapshot, selection, &mut edits, row_delta, cx);
4617 }
4618
4619 self.transact(cx, |this, cx| {
4620 this.buffer.update(cx, |b, cx| b.edit(edits, None, cx));
4621 this.change_selections(Some(Autoscroll::fit()), cx, |s| s.select(selections));
4622 });
4623 }
4624
4625 fn indent_selection(
4626 buffer: &MultiBuffer,
4627 snapshot: &MultiBufferSnapshot,
4628 selection: &mut Selection<Point>,
4629 edits: &mut Vec<(Range<Point>, String)>,
4630 delta_for_start_row: u32,
4631 cx: &AppContext,
4632 ) -> u32 {
4633 let settings = buffer.settings_at(selection.start, cx);
4634 let tab_size = settings.tab_size.get();
4635 let indent_kind = if settings.hard_tabs {
4636 IndentKind::Tab
4637 } else {
4638 IndentKind::Space
4639 };
4640 let mut start_row = selection.start.row;
4641 let mut end_row = selection.end.row + 1;
4642
4643 // If a selection ends at the beginning of a line, don't indent
4644 // that last line.
4645 if selection.end.column == 0 {
4646 end_row -= 1;
4647 }
4648
4649 // Avoid re-indenting a row that has already been indented by a
4650 // previous selection, but still update this selection's column
4651 // to reflect that indentation.
4652 if delta_for_start_row > 0 {
4653 start_row += 1;
4654 selection.start.column += delta_for_start_row;
4655 if selection.end.row == selection.start.row {
4656 selection.end.column += delta_for_start_row;
4657 }
4658 }
4659
4660 let mut delta_for_end_row = 0;
4661 for row in start_row..end_row {
4662 let current_indent = snapshot.indent_size_for_line(row);
4663 let indent_delta = match (current_indent.kind, indent_kind) {
4664 (IndentKind::Space, IndentKind::Space) => {
4665 let columns_to_next_tab_stop = tab_size - (current_indent.len % tab_size);
4666 IndentSize::spaces(columns_to_next_tab_stop)
4667 }
4668 (IndentKind::Tab, IndentKind::Space) => IndentSize::spaces(tab_size),
4669 (_, IndentKind::Tab) => IndentSize::tab(),
4670 };
4671
4672 let row_start = Point::new(row, 0);
4673 edits.push((
4674 row_start..row_start,
4675 indent_delta.chars().collect::<String>(),
4676 ));
4677
4678 // Update this selection's endpoints to reflect the indentation.
4679 if row == selection.start.row {
4680 selection.start.column += indent_delta.len;
4681 }
4682 if row == selection.end.row {
4683 selection.end.column += indent_delta.len;
4684 delta_for_end_row = indent_delta.len;
4685 }
4686 }
4687
4688 if selection.start.row == selection.end.row {
4689 delta_for_start_row + delta_for_end_row
4690 } else {
4691 delta_for_end_row
4692 }
4693 }
4694
4695 pub fn outdent(&mut self, _: &Outdent, cx: &mut ViewContext<Self>) {
4696 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
4697 let selections = self.selections.all::<Point>(cx);
4698 let mut deletion_ranges = Vec::new();
4699 let mut last_outdent = None;
4700 {
4701 let buffer = self.buffer.read(cx);
4702 let snapshot = buffer.snapshot(cx);
4703 for selection in &selections {
4704 let settings = buffer.settings_at(selection.start, cx);
4705 let tab_size = settings.tab_size.get();
4706 let mut rows = selection.spanned_rows(false, &display_map);
4707
4708 // Avoid re-outdenting a row that has already been outdented by a
4709 // previous selection.
4710 if let Some(last_row) = last_outdent {
4711 if last_row == rows.start {
4712 rows.start += 1;
4713 }
4714 }
4715
4716 for row in rows {
4717 let indent_size = snapshot.indent_size_for_line(row);
4718 if indent_size.len > 0 {
4719 let deletion_len = match indent_size.kind {
4720 IndentKind::Space => {
4721 let columns_to_prev_tab_stop = indent_size.len % tab_size;
4722 if columns_to_prev_tab_stop == 0 {
4723 tab_size
4724 } else {
4725 columns_to_prev_tab_stop
4726 }
4727 }
4728 IndentKind::Tab => 1,
4729 };
4730 deletion_ranges.push(Point::new(row, 0)..Point::new(row, deletion_len));
4731 last_outdent = Some(row);
4732 }
4733 }
4734 }
4735 }
4736
4737 self.transact(cx, |this, cx| {
4738 this.buffer.update(cx, |buffer, cx| {
4739 let empty_str: Arc<str> = "".into();
4740 buffer.edit(
4741 deletion_ranges
4742 .into_iter()
4743 .map(|range| (range, empty_str.clone())),
4744 None,
4745 cx,
4746 );
4747 });
4748 let selections = this.selections.all::<usize>(cx);
4749 this.change_selections(Some(Autoscroll::fit()), cx, |s| s.select(selections));
4750 });
4751 }
4752
4753 pub fn delete_line(&mut self, _: &DeleteLine, cx: &mut ViewContext<Self>) {
4754 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
4755 let selections = self.selections.all::<Point>(cx);
4756
4757 let mut new_cursors = Vec::new();
4758 let mut edit_ranges = Vec::new();
4759 let mut selections = selections.iter().peekable();
4760 while let Some(selection) = selections.next() {
4761 let mut rows = selection.spanned_rows(false, &display_map);
4762 let goal_display_column = selection.head().to_display_point(&display_map).column();
4763
4764 // Accumulate contiguous regions of rows that we want to delete.
4765 while let Some(next_selection) = selections.peek() {
4766 let next_rows = next_selection.spanned_rows(false, &display_map);
4767 if next_rows.start <= rows.end {
4768 rows.end = next_rows.end;
4769 selections.next().unwrap();
4770 } else {
4771 break;
4772 }
4773 }
4774
4775 let buffer = &display_map.buffer_snapshot;
4776 let mut edit_start = Point::new(rows.start, 0).to_offset(buffer);
4777 let edit_end;
4778 let cursor_buffer_row;
4779 if buffer.max_point().row >= rows.end {
4780 // If there's a line after the range, delete the \n from the end of the row range
4781 // and position the cursor on the next line.
4782 edit_end = Point::new(rows.end, 0).to_offset(buffer);
4783 cursor_buffer_row = rows.end;
4784 } else {
4785 // If there isn't a line after the range, delete the \n from the line before the
4786 // start of the row range and position the cursor there.
4787 edit_start = edit_start.saturating_sub(1);
4788 edit_end = buffer.len();
4789 cursor_buffer_row = rows.start.saturating_sub(1);
4790 }
4791
4792 let mut cursor = Point::new(cursor_buffer_row, 0).to_display_point(&display_map);
4793 *cursor.column_mut() =
4794 cmp::min(goal_display_column, display_map.line_len(cursor.row()));
4795
4796 new_cursors.push((
4797 selection.id,
4798 buffer.anchor_after(cursor.to_point(&display_map)),
4799 ));
4800 edit_ranges.push(edit_start..edit_end);
4801 }
4802
4803 self.transact(cx, |this, cx| {
4804 let buffer = this.buffer.update(cx, |buffer, cx| {
4805 let empty_str: Arc<str> = "".into();
4806 buffer.edit(
4807 edit_ranges
4808 .into_iter()
4809 .map(|range| (range, empty_str.clone())),
4810 None,
4811 cx,
4812 );
4813 buffer.snapshot(cx)
4814 });
4815 let new_selections = new_cursors
4816 .into_iter()
4817 .map(|(id, cursor)| {
4818 let cursor = cursor.to_point(&buffer);
4819 Selection {
4820 id,
4821 start: cursor,
4822 end: cursor,
4823 reversed: false,
4824 goal: SelectionGoal::None,
4825 }
4826 })
4827 .collect();
4828
4829 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
4830 s.select(new_selections);
4831 });
4832 });
4833 }
4834
4835 pub fn join_lines(&mut self, _: &JoinLines, cx: &mut ViewContext<Self>) {
4836 let mut row_ranges = Vec::<Range<u32>>::new();
4837 for selection in self.selections.all::<Point>(cx) {
4838 let start = selection.start.row;
4839 let end = if selection.start.row == selection.end.row {
4840 selection.start.row + 1
4841 } else {
4842 selection.end.row
4843 };
4844
4845 if let Some(last_row_range) = row_ranges.last_mut() {
4846 if start <= last_row_range.end {
4847 last_row_range.end = end;
4848 continue;
4849 }
4850 }
4851 row_ranges.push(start..end);
4852 }
4853
4854 let snapshot = self.buffer.read(cx).snapshot(cx);
4855 let mut cursor_positions = Vec::new();
4856 for row_range in &row_ranges {
4857 let anchor = snapshot.anchor_before(Point::new(
4858 row_range.end - 1,
4859 snapshot.line_len(row_range.end - 1),
4860 ));
4861 cursor_positions.push(anchor.clone()..anchor);
4862 }
4863
4864 self.transact(cx, |this, cx| {
4865 for row_range in row_ranges.into_iter().rev() {
4866 for row in row_range.rev() {
4867 let end_of_line = Point::new(row, snapshot.line_len(row));
4868 let indent = snapshot.indent_size_for_line(row + 1);
4869 let start_of_next_line = Point::new(row + 1, indent.len);
4870
4871 let replace = if snapshot.line_len(row + 1) > indent.len {
4872 " "
4873 } else {
4874 ""
4875 };
4876
4877 this.buffer.update(cx, |buffer, cx| {
4878 buffer.edit([(end_of_line..start_of_next_line, replace)], None, cx)
4879 });
4880 }
4881 }
4882
4883 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
4884 s.select_anchor_ranges(cursor_positions)
4885 });
4886 });
4887 }
4888
4889 pub fn sort_lines_case_sensitive(
4890 &mut self,
4891 _: &SortLinesCaseSensitive,
4892 cx: &mut ViewContext<Self>,
4893 ) {
4894 self.manipulate_lines(cx, |lines| lines.sort())
4895 }
4896
4897 pub fn sort_lines_case_insensitive(
4898 &mut self,
4899 _: &SortLinesCaseInsensitive,
4900 cx: &mut ViewContext<Self>,
4901 ) {
4902 self.manipulate_lines(cx, |lines| lines.sort_by_key(|line| line.to_lowercase()))
4903 }
4904
4905 pub fn reverse_lines(&mut self, _: &ReverseLines, cx: &mut ViewContext<Self>) {
4906 self.manipulate_lines(cx, |lines| lines.reverse())
4907 }
4908
4909 pub fn shuffle_lines(&mut self, _: &ShuffleLines, cx: &mut ViewContext<Self>) {
4910 self.manipulate_lines(cx, |lines| lines.shuffle(&mut thread_rng()))
4911 }
4912
4913 fn manipulate_lines<Fn>(&mut self, cx: &mut ViewContext<Self>, mut callback: Fn)
4914 where
4915 Fn: FnMut(&mut [&str]),
4916 {
4917 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
4918 let buffer = self.buffer.read(cx).snapshot(cx);
4919
4920 let mut edits = Vec::new();
4921
4922 let selections = self.selections.all::<Point>(cx);
4923 let mut selections = selections.iter().peekable();
4924 let mut contiguous_row_selections = Vec::new();
4925 let mut new_selections = Vec::new();
4926
4927 while let Some(selection) = selections.next() {
4928 let (start_row, end_row) = consume_contiguous_rows(
4929 &mut contiguous_row_selections,
4930 selection,
4931 &display_map,
4932 &mut selections,
4933 );
4934
4935 let start_point = Point::new(start_row, 0);
4936 let end_point = Point::new(end_row - 1, buffer.line_len(end_row - 1));
4937 let text = buffer
4938 .text_for_range(start_point..end_point)
4939 .collect::<String>();
4940 let mut lines = text.split("\n").collect_vec();
4941
4942 let lines_len = lines.len();
4943 callback(&mut lines);
4944
4945 // This is a current limitation with selections.
4946 // If we wanted to support removing or adding lines, we'd need to fix the logic associated with selections.
4947 debug_assert!(
4948 lines.len() == lines_len,
4949 "callback should not change the number of lines"
4950 );
4951
4952 edits.push((start_point..end_point, lines.join("\n")));
4953 let start_anchor = buffer.anchor_after(start_point);
4954 let end_anchor = buffer.anchor_before(end_point);
4955
4956 // Make selection and push
4957 new_selections.push(Selection {
4958 id: selection.id,
4959 start: start_anchor.to_offset(&buffer),
4960 end: end_anchor.to_offset(&buffer),
4961 goal: SelectionGoal::None,
4962 reversed: selection.reversed,
4963 });
4964 }
4965
4966 self.transact(cx, |this, cx| {
4967 this.buffer.update(cx, |buffer, cx| {
4968 buffer.edit(edits, None, cx);
4969 });
4970
4971 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
4972 s.select(new_selections);
4973 });
4974
4975 this.request_autoscroll(Autoscroll::fit(), cx);
4976 });
4977 }
4978
4979 pub fn convert_to_upper_case(&mut self, _: &ConvertToUpperCase, cx: &mut ViewContext<Self>) {
4980 self.manipulate_text(cx, |text| text.to_uppercase())
4981 }
4982
4983 pub fn convert_to_lower_case(&mut self, _: &ConvertToLowerCase, cx: &mut ViewContext<Self>) {
4984 self.manipulate_text(cx, |text| text.to_lowercase())
4985 }
4986
4987 pub fn convert_to_title_case(&mut self, _: &ConvertToTitleCase, cx: &mut ViewContext<Self>) {
4988 self.manipulate_text(cx, |text| {
4989 // Hack to get around the fact that to_case crate doesn't support '\n' as a word boundary
4990 // https://github.com/rutrum/convert-case/issues/16
4991 text.split("\n")
4992 .map(|line| line.to_case(Case::Title))
4993 .join("\n")
4994 })
4995 }
4996
4997 pub fn convert_to_snake_case(&mut self, _: &ConvertToSnakeCase, cx: &mut ViewContext<Self>) {
4998 self.manipulate_text(cx, |text| text.to_case(Case::Snake))
4999 }
5000
5001 pub fn convert_to_kebab_case(&mut self, _: &ConvertToKebabCase, cx: &mut ViewContext<Self>) {
5002 self.manipulate_text(cx, |text| text.to_case(Case::Kebab))
5003 }
5004
5005 pub fn convert_to_upper_camel_case(
5006 &mut self,
5007 _: &ConvertToUpperCamelCase,
5008 cx: &mut ViewContext<Self>,
5009 ) {
5010 self.manipulate_text(cx, |text| {
5011 // Hack to get around the fact that to_case crate doesn't support '\n' as a word boundary
5012 // https://github.com/rutrum/convert-case/issues/16
5013 text.split("\n")
5014 .map(|line| line.to_case(Case::UpperCamel))
5015 .join("\n")
5016 })
5017 }
5018
5019 pub fn convert_to_lower_camel_case(
5020 &mut self,
5021 _: &ConvertToLowerCamelCase,
5022 cx: &mut ViewContext<Self>,
5023 ) {
5024 self.manipulate_text(cx, |text| text.to_case(Case::Camel))
5025 }
5026
5027 fn manipulate_text<Fn>(&mut self, cx: &mut ViewContext<Self>, mut callback: Fn)
5028 where
5029 Fn: FnMut(&str) -> String,
5030 {
5031 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
5032 let buffer = self.buffer.read(cx).snapshot(cx);
5033
5034 let mut new_selections = Vec::new();
5035 let mut edits = Vec::new();
5036 let mut selection_adjustment = 0i32;
5037
5038 for selection in self.selections.all::<usize>(cx) {
5039 let selection_is_empty = selection.is_empty();
5040
5041 let (start, end) = if selection_is_empty {
5042 let word_range = movement::surrounding_word(
5043 &display_map,
5044 selection.start.to_display_point(&display_map),
5045 );
5046 let start = word_range.start.to_offset(&display_map, Bias::Left);
5047 let end = word_range.end.to_offset(&display_map, Bias::Left);
5048 (start, end)
5049 } else {
5050 (selection.start, selection.end)
5051 };
5052
5053 let text = buffer.text_for_range(start..end).collect::<String>();
5054 let old_length = text.len() as i32;
5055 let text = callback(&text);
5056
5057 new_selections.push(Selection {
5058 start: (start as i32 - selection_adjustment) as usize,
5059 end: ((start + text.len()) as i32 - selection_adjustment) as usize,
5060 goal: SelectionGoal::None,
5061 ..selection
5062 });
5063
5064 selection_adjustment += old_length - text.len() as i32;
5065
5066 edits.push((start..end, text));
5067 }
5068
5069 self.transact(cx, |this, cx| {
5070 this.buffer.update(cx, |buffer, cx| {
5071 buffer.edit(edits, None, cx);
5072 });
5073
5074 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
5075 s.select(new_selections);
5076 });
5077
5078 this.request_autoscroll(Autoscroll::fit(), cx);
5079 });
5080 }
5081
5082 pub fn duplicate_line(&mut self, _: &DuplicateLine, cx: &mut ViewContext<Self>) {
5083 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
5084 let buffer = &display_map.buffer_snapshot;
5085 let selections = self.selections.all::<Point>(cx);
5086
5087 let mut edits = Vec::new();
5088 let mut selections_iter = selections.iter().peekable();
5089 while let Some(selection) = selections_iter.next() {
5090 // Avoid duplicating the same lines twice.
5091 let mut rows = selection.spanned_rows(false, &display_map);
5092
5093 while let Some(next_selection) = selections_iter.peek() {
5094 let next_rows = next_selection.spanned_rows(false, &display_map);
5095 if next_rows.start < rows.end {
5096 rows.end = next_rows.end;
5097 selections_iter.next().unwrap();
5098 } else {
5099 break;
5100 }
5101 }
5102
5103 // Copy the text from the selected row region and splice it at the start of the region.
5104 let start = Point::new(rows.start, 0);
5105 let end = Point::new(rows.end - 1, buffer.line_len(rows.end - 1));
5106 let text = buffer
5107 .text_for_range(start..end)
5108 .chain(Some("\n"))
5109 .collect::<String>();
5110 edits.push((start..start, text));
5111 }
5112
5113 self.transact(cx, |this, cx| {
5114 this.buffer.update(cx, |buffer, cx| {
5115 buffer.edit(edits, None, cx);
5116 });
5117
5118 this.request_autoscroll(Autoscroll::fit(), cx);
5119 });
5120 }
5121
5122 pub fn move_line_up(&mut self, _: &MoveLineUp, cx: &mut ViewContext<Self>) {
5123 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
5124 let buffer = self.buffer.read(cx).snapshot(cx);
5125
5126 let mut edits = Vec::new();
5127 let mut unfold_ranges = Vec::new();
5128 let mut refold_ranges = Vec::new();
5129
5130 let selections = self.selections.all::<Point>(cx);
5131 let mut selections = selections.iter().peekable();
5132 let mut contiguous_row_selections = Vec::new();
5133 let mut new_selections = Vec::new();
5134
5135 while let Some(selection) = selections.next() {
5136 // Find all the selections that span a contiguous row range
5137 let (start_row, end_row) = consume_contiguous_rows(
5138 &mut contiguous_row_selections,
5139 selection,
5140 &display_map,
5141 &mut selections,
5142 );
5143
5144 // Move the text spanned by the row range to be before the line preceding the row range
5145 if start_row > 0 {
5146 let range_to_move = Point::new(start_row - 1, buffer.line_len(start_row - 1))
5147 ..Point::new(end_row - 1, buffer.line_len(end_row - 1));
5148 let insertion_point = display_map
5149 .prev_line_boundary(Point::new(start_row - 1, 0))
5150 .0;
5151
5152 // Don't move lines across excerpts
5153 if buffer
5154 .excerpt_boundaries_in_range((
5155 Bound::Excluded(insertion_point),
5156 Bound::Included(range_to_move.end),
5157 ))
5158 .next()
5159 .is_none()
5160 {
5161 let text = buffer
5162 .text_for_range(range_to_move.clone())
5163 .flat_map(|s| s.chars())
5164 .skip(1)
5165 .chain(['\n'])
5166 .collect::<String>();
5167
5168 edits.push((
5169 buffer.anchor_after(range_to_move.start)
5170 ..buffer.anchor_before(range_to_move.end),
5171 String::new(),
5172 ));
5173 let insertion_anchor = buffer.anchor_after(insertion_point);
5174 edits.push((insertion_anchor..insertion_anchor, text));
5175
5176 let row_delta = range_to_move.start.row - insertion_point.row + 1;
5177
5178 // Move selections up
5179 new_selections.extend(contiguous_row_selections.drain(..).map(
5180 |mut selection| {
5181 selection.start.row -= row_delta;
5182 selection.end.row -= row_delta;
5183 selection
5184 },
5185 ));
5186
5187 // Move folds up
5188 unfold_ranges.push(range_to_move.clone());
5189 for fold in display_map.folds_in_range(
5190 buffer.anchor_before(range_to_move.start)
5191 ..buffer.anchor_after(range_to_move.end),
5192 ) {
5193 let mut start = fold.range.start.to_point(&buffer);
5194 let mut end = fold.range.end.to_point(&buffer);
5195 start.row -= row_delta;
5196 end.row -= row_delta;
5197 refold_ranges.push(start..end);
5198 }
5199 }
5200 }
5201
5202 // If we didn't move line(s), preserve the existing selections
5203 new_selections.append(&mut contiguous_row_selections);
5204 }
5205
5206 self.transact(cx, |this, cx| {
5207 this.unfold_ranges(unfold_ranges, true, true, cx);
5208 this.buffer.update(cx, |buffer, cx| {
5209 for (range, text) in edits {
5210 buffer.edit([(range, text)], None, cx);
5211 }
5212 });
5213 this.fold_ranges(refold_ranges, true, cx);
5214 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
5215 s.select(new_selections);
5216 })
5217 });
5218 }
5219
5220 pub fn move_line_down(&mut self, _: &MoveLineDown, cx: &mut ViewContext<Self>) {
5221 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
5222 let buffer = self.buffer.read(cx).snapshot(cx);
5223
5224 let mut edits = Vec::new();
5225 let mut unfold_ranges = Vec::new();
5226 let mut refold_ranges = Vec::new();
5227
5228 let selections = self.selections.all::<Point>(cx);
5229 let mut selections = selections.iter().peekable();
5230 let mut contiguous_row_selections = Vec::new();
5231 let mut new_selections = Vec::new();
5232
5233 while let Some(selection) = selections.next() {
5234 // Find all the selections that span a contiguous row range
5235 let (start_row, end_row) = consume_contiguous_rows(
5236 &mut contiguous_row_selections,
5237 selection,
5238 &display_map,
5239 &mut selections,
5240 );
5241
5242 // Move the text spanned by the row range to be after the last line of the row range
5243 if end_row <= buffer.max_point().row {
5244 let range_to_move = Point::new(start_row, 0)..Point::new(end_row, 0);
5245 let insertion_point = display_map.next_line_boundary(Point::new(end_row, 0)).0;
5246
5247 // Don't move lines across excerpt boundaries
5248 if buffer
5249 .excerpt_boundaries_in_range((
5250 Bound::Excluded(range_to_move.start),
5251 Bound::Included(insertion_point),
5252 ))
5253 .next()
5254 .is_none()
5255 {
5256 let mut text = String::from("\n");
5257 text.extend(buffer.text_for_range(range_to_move.clone()));
5258 text.pop(); // Drop trailing newline
5259 edits.push((
5260 buffer.anchor_after(range_to_move.start)
5261 ..buffer.anchor_before(range_to_move.end),
5262 String::new(),
5263 ));
5264 let insertion_anchor = buffer.anchor_after(insertion_point);
5265 edits.push((insertion_anchor..insertion_anchor, text));
5266
5267 let row_delta = insertion_point.row - range_to_move.end.row + 1;
5268
5269 // Move selections down
5270 new_selections.extend(contiguous_row_selections.drain(..).map(
5271 |mut selection| {
5272 selection.start.row += row_delta;
5273 selection.end.row += row_delta;
5274 selection
5275 },
5276 ));
5277
5278 // Move folds down
5279 unfold_ranges.push(range_to_move.clone());
5280 for fold in display_map.folds_in_range(
5281 buffer.anchor_before(range_to_move.start)
5282 ..buffer.anchor_after(range_to_move.end),
5283 ) {
5284 let mut start = fold.range.start.to_point(&buffer);
5285 let mut end = fold.range.end.to_point(&buffer);
5286 start.row += row_delta;
5287 end.row += row_delta;
5288 refold_ranges.push(start..end);
5289 }
5290 }
5291 }
5292
5293 // If we didn't move line(s), preserve the existing selections
5294 new_selections.append(&mut contiguous_row_selections);
5295 }
5296
5297 self.transact(cx, |this, cx| {
5298 this.unfold_ranges(unfold_ranges, true, true, cx);
5299 this.buffer.update(cx, |buffer, cx| {
5300 for (range, text) in edits {
5301 buffer.edit([(range, text)], None, cx);
5302 }
5303 });
5304 this.fold_ranges(refold_ranges, true, cx);
5305 this.change_selections(Some(Autoscroll::fit()), cx, |s| s.select(new_selections));
5306 });
5307 }
5308
5309 pub fn transpose(&mut self, _: &Transpose, cx: &mut ViewContext<Self>) {
5310 let text_layout_details = &self.text_layout_details(cx);
5311 self.transact(cx, |this, cx| {
5312 let edits = this.change_selections(Some(Autoscroll::fit()), cx, |s| {
5313 let mut edits: Vec<(Range<usize>, String)> = Default::default();
5314 let line_mode = s.line_mode;
5315 s.move_with(|display_map, selection| {
5316 if !selection.is_empty() || line_mode {
5317 return;
5318 }
5319
5320 let mut head = selection.head();
5321 let mut transpose_offset = head.to_offset(display_map, Bias::Right);
5322 if head.column() == display_map.line_len(head.row()) {
5323 transpose_offset = display_map
5324 .buffer_snapshot
5325 .clip_offset(transpose_offset.saturating_sub(1), Bias::Left);
5326 }
5327
5328 if transpose_offset == 0 {
5329 return;
5330 }
5331
5332 *head.column_mut() += 1;
5333 head = display_map.clip_point(head, Bias::Right);
5334 let goal = SelectionGoal::HorizontalPosition(
5335 display_map
5336 .x_for_display_point(head, &text_layout_details)
5337 .into(),
5338 );
5339 selection.collapse_to(head, goal);
5340
5341 let transpose_start = display_map
5342 .buffer_snapshot
5343 .clip_offset(transpose_offset.saturating_sub(1), Bias::Left);
5344 if edits.last().map_or(true, |e| e.0.end <= transpose_start) {
5345 let transpose_end = display_map
5346 .buffer_snapshot
5347 .clip_offset(transpose_offset + 1, Bias::Right);
5348 if let Some(ch) =
5349 display_map.buffer_snapshot.chars_at(transpose_start).next()
5350 {
5351 edits.push((transpose_start..transpose_offset, String::new()));
5352 edits.push((transpose_end..transpose_end, ch.to_string()));
5353 }
5354 }
5355 });
5356 edits
5357 });
5358 this.buffer
5359 .update(cx, |buffer, cx| buffer.edit(edits, None, cx));
5360 let selections = this.selections.all::<usize>(cx);
5361 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
5362 s.select(selections);
5363 });
5364 });
5365 }
5366
5367 pub fn cut(&mut self, _: &Cut, cx: &mut ViewContext<Self>) {
5368 let mut text = String::new();
5369 let buffer = self.buffer.read(cx).snapshot(cx);
5370 let mut selections = self.selections.all::<Point>(cx);
5371 let mut clipboard_selections = Vec::with_capacity(selections.len());
5372 {
5373 let max_point = buffer.max_point();
5374 let mut is_first = true;
5375 for selection in &mut selections {
5376 let is_entire_line = selection.is_empty() || self.selections.line_mode;
5377 if is_entire_line {
5378 selection.start = Point::new(selection.start.row, 0);
5379 selection.end = cmp::min(max_point, Point::new(selection.end.row + 1, 0));
5380 selection.goal = SelectionGoal::None;
5381 }
5382 if is_first {
5383 is_first = false;
5384 } else {
5385 text += "\n";
5386 }
5387 let mut len = 0;
5388 for chunk in buffer.text_for_range(selection.start..selection.end) {
5389 text.push_str(chunk);
5390 len += chunk.len();
5391 }
5392 clipboard_selections.push(ClipboardSelection {
5393 len,
5394 is_entire_line,
5395 first_line_indent: buffer.indent_size_for_line(selection.start.row).len,
5396 });
5397 }
5398 }
5399
5400 self.transact(cx, |this, cx| {
5401 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
5402 s.select(selections);
5403 });
5404 this.insert("", cx);
5405 cx.write_to_clipboard(ClipboardItem::new(text).with_metadata(clipboard_selections));
5406 });
5407 }
5408
5409 pub fn copy(&mut self, _: &Copy, cx: &mut ViewContext<Self>) {
5410 let selections = self.selections.all::<Point>(cx);
5411 let buffer = self.buffer.read(cx).read(cx);
5412 let mut text = String::new();
5413
5414 let mut clipboard_selections = Vec::with_capacity(selections.len());
5415 {
5416 let max_point = buffer.max_point();
5417 let mut is_first = true;
5418 for selection in selections.iter() {
5419 let mut start = selection.start;
5420 let mut end = selection.end;
5421 let is_entire_line = selection.is_empty() || self.selections.line_mode;
5422 if is_entire_line {
5423 start = Point::new(start.row, 0);
5424 end = cmp::min(max_point, Point::new(end.row + 1, 0));
5425 }
5426 if is_first {
5427 is_first = false;
5428 } else {
5429 text += "\n";
5430 }
5431 let mut len = 0;
5432 for chunk in buffer.text_for_range(start..end) {
5433 text.push_str(chunk);
5434 len += chunk.len();
5435 }
5436 clipboard_selections.push(ClipboardSelection {
5437 len,
5438 is_entire_line,
5439 first_line_indent: buffer.indent_size_for_line(start.row).len,
5440 });
5441 }
5442 }
5443
5444 cx.write_to_clipboard(ClipboardItem::new(text).with_metadata(clipboard_selections));
5445 }
5446
5447 pub fn paste(&mut self, _: &Paste, cx: &mut ViewContext<Self>) {
5448 if self.read_only(cx) {
5449 return;
5450 }
5451
5452 self.transact(cx, |this, cx| {
5453 if let Some(item) = cx.read_from_clipboard() {
5454 let clipboard_text = Cow::Borrowed(item.text());
5455 if let Some(mut clipboard_selections) = item.metadata::<Vec<ClipboardSelection>>() {
5456 let old_selections = this.selections.all::<usize>(cx);
5457 let all_selections_were_entire_line =
5458 clipboard_selections.iter().all(|s| s.is_entire_line);
5459 let first_selection_indent_column =
5460 clipboard_selections.first().map(|s| s.first_line_indent);
5461 if clipboard_selections.len() != old_selections.len() {
5462 clipboard_selections.drain(..);
5463 }
5464
5465 this.buffer.update(cx, |buffer, cx| {
5466 let snapshot = buffer.read(cx);
5467 let mut start_offset = 0;
5468 let mut edits = Vec::new();
5469 let mut original_indent_columns = Vec::new();
5470 let line_mode = this.selections.line_mode;
5471 for (ix, selection) in old_selections.iter().enumerate() {
5472 let to_insert;
5473 let entire_line;
5474 let original_indent_column;
5475 if let Some(clipboard_selection) = clipboard_selections.get(ix) {
5476 let end_offset = start_offset + clipboard_selection.len;
5477 to_insert = &clipboard_text[start_offset..end_offset];
5478 entire_line = clipboard_selection.is_entire_line;
5479 start_offset = end_offset + 1;
5480 original_indent_column =
5481 Some(clipboard_selection.first_line_indent);
5482 } else {
5483 to_insert = clipboard_text.as_str();
5484 entire_line = all_selections_were_entire_line;
5485 original_indent_column = first_selection_indent_column
5486 }
5487
5488 // If the corresponding selection was empty when this slice of the
5489 // clipboard text was written, then the entire line containing the
5490 // selection was copied. If this selection is also currently empty,
5491 // then paste the line before the current line of the buffer.
5492 let range = if selection.is_empty() && !line_mode && entire_line {
5493 let column = selection.start.to_point(&snapshot).column as usize;
5494 let line_start = selection.start - column;
5495 line_start..line_start
5496 } else {
5497 selection.range()
5498 };
5499
5500 edits.push((range, to_insert));
5501 original_indent_columns.extend(original_indent_column);
5502 }
5503 drop(snapshot);
5504
5505 buffer.edit(
5506 edits,
5507 Some(AutoindentMode::Block {
5508 original_indent_columns,
5509 }),
5510 cx,
5511 );
5512 });
5513
5514 let selections = this.selections.all::<usize>(cx);
5515 this.change_selections(Some(Autoscroll::fit()), cx, |s| s.select(selections));
5516 } else {
5517 this.insert(&clipboard_text, cx);
5518 }
5519 }
5520 });
5521 }
5522
5523 pub fn undo(&mut self, _: &Undo, cx: &mut ViewContext<Self>) {
5524 if self.read_only(cx) {
5525 return;
5526 }
5527
5528 if let Some(tx_id) = self.buffer.update(cx, |buffer, cx| buffer.undo(cx)) {
5529 if let Some((selections, _)) = self.selection_history.transaction(tx_id).cloned() {
5530 self.change_selections(None, cx, |s| {
5531 s.select_anchors(selections.to_vec());
5532 });
5533 }
5534 self.request_autoscroll(Autoscroll::fit(), cx);
5535 self.unmark_text(cx);
5536 self.refresh_copilot_suggestions(true, cx);
5537 cx.emit(EditorEvent::Edited);
5538 }
5539 }
5540
5541 pub fn redo(&mut self, _: &Redo, cx: &mut ViewContext<Self>) {
5542 if self.read_only(cx) {
5543 return;
5544 }
5545
5546 if let Some(tx_id) = self.buffer.update(cx, |buffer, cx| buffer.redo(cx)) {
5547 if let Some((_, Some(selections))) = self.selection_history.transaction(tx_id).cloned()
5548 {
5549 self.change_selections(None, cx, |s| {
5550 s.select_anchors(selections.to_vec());
5551 });
5552 }
5553 self.request_autoscroll(Autoscroll::fit(), cx);
5554 self.unmark_text(cx);
5555 self.refresh_copilot_suggestions(true, cx);
5556 cx.emit(EditorEvent::Edited);
5557 }
5558 }
5559
5560 pub fn finalize_last_transaction(&mut self, cx: &mut ViewContext<Self>) {
5561 self.buffer
5562 .update(cx, |buffer, cx| buffer.finalize_last_transaction(cx));
5563 }
5564
5565 pub fn move_left(&mut self, _: &MoveLeft, cx: &mut ViewContext<Self>) {
5566 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5567 let line_mode = s.line_mode;
5568 s.move_with(|map, selection| {
5569 let cursor = if selection.is_empty() && !line_mode {
5570 movement::left(map, selection.start)
5571 } else {
5572 selection.start
5573 };
5574 selection.collapse_to(cursor, SelectionGoal::None);
5575 });
5576 })
5577 }
5578
5579 pub fn select_left(&mut self, _: &SelectLeft, cx: &mut ViewContext<Self>) {
5580 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5581 s.move_heads_with(|map, head, _| (movement::left(map, head), SelectionGoal::None));
5582 })
5583 }
5584
5585 pub fn move_right(&mut self, _: &MoveRight, cx: &mut ViewContext<Self>) {
5586 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5587 let line_mode = s.line_mode;
5588 s.move_with(|map, selection| {
5589 let cursor = if selection.is_empty() && !line_mode {
5590 movement::right(map, selection.end)
5591 } else {
5592 selection.end
5593 };
5594 selection.collapse_to(cursor, SelectionGoal::None)
5595 });
5596 })
5597 }
5598
5599 pub fn select_right(&mut self, _: &SelectRight, cx: &mut ViewContext<Self>) {
5600 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5601 s.move_heads_with(|map, head, _| (movement::right(map, head), SelectionGoal::None));
5602 })
5603 }
5604
5605 pub fn move_up(&mut self, _: &MoveUp, cx: &mut ViewContext<Self>) {
5606 if self.take_rename(true, cx).is_some() {
5607 return;
5608 }
5609
5610 if matches!(self.mode, EditorMode::SingleLine) {
5611 cx.propagate();
5612 return;
5613 }
5614
5615 let text_layout_details = &self.text_layout_details(cx);
5616
5617 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5618 let line_mode = s.line_mode;
5619 s.move_with(|map, selection| {
5620 if !selection.is_empty() && !line_mode {
5621 selection.goal = SelectionGoal::None;
5622 }
5623 let (cursor, goal) = movement::up(
5624 map,
5625 selection.start,
5626 selection.goal,
5627 false,
5628 &text_layout_details,
5629 );
5630 selection.collapse_to(cursor, goal);
5631 });
5632 })
5633 }
5634
5635 pub fn move_page_up(&mut self, action: &MovePageUp, cx: &mut ViewContext<Self>) {
5636 if self.take_rename(true, cx).is_some() {
5637 return;
5638 }
5639
5640 if matches!(self.mode, EditorMode::SingleLine) {
5641 cx.propagate();
5642 return;
5643 }
5644
5645 let row_count = if let Some(row_count) = self.visible_line_count() {
5646 row_count as u32 - 1
5647 } else {
5648 return;
5649 };
5650
5651 let autoscroll = if action.center_cursor {
5652 Autoscroll::center()
5653 } else {
5654 Autoscroll::fit()
5655 };
5656
5657 let text_layout_details = &self.text_layout_details(cx);
5658
5659 self.change_selections(Some(autoscroll), cx, |s| {
5660 let line_mode = s.line_mode;
5661 s.move_with(|map, selection| {
5662 if !selection.is_empty() && !line_mode {
5663 selection.goal = SelectionGoal::None;
5664 }
5665 let (cursor, goal) = movement::up_by_rows(
5666 map,
5667 selection.end,
5668 row_count,
5669 selection.goal,
5670 false,
5671 &text_layout_details,
5672 );
5673 selection.collapse_to(cursor, goal);
5674 });
5675 });
5676 }
5677
5678 pub fn select_up(&mut self, _: &SelectUp, cx: &mut ViewContext<Self>) {
5679 let text_layout_details = &self.text_layout_details(cx);
5680 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5681 s.move_heads_with(|map, head, goal| {
5682 movement::up(map, head, goal, false, &text_layout_details)
5683 })
5684 })
5685 }
5686
5687 pub fn move_down(&mut self, _: &MoveDown, cx: &mut ViewContext<Self>) {
5688 self.take_rename(true, cx);
5689
5690 if self.mode == EditorMode::SingleLine {
5691 cx.propagate();
5692 return;
5693 }
5694
5695 let text_layout_details = &self.text_layout_details(cx);
5696 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5697 let line_mode = s.line_mode;
5698 s.move_with(|map, selection| {
5699 if !selection.is_empty() && !line_mode {
5700 selection.goal = SelectionGoal::None;
5701 }
5702 let (cursor, goal) = movement::down(
5703 map,
5704 selection.end,
5705 selection.goal,
5706 false,
5707 &text_layout_details,
5708 );
5709 selection.collapse_to(cursor, goal);
5710 });
5711 });
5712 }
5713
5714 pub fn move_page_down(&mut self, action: &MovePageDown, cx: &mut ViewContext<Self>) {
5715 if self.take_rename(true, cx).is_some() {
5716 return;
5717 }
5718
5719 if self
5720 .context_menu
5721 .write()
5722 .as_mut()
5723 .map(|menu| menu.select_last(self.project.as_ref(), cx))
5724 .unwrap_or(false)
5725 {
5726 return;
5727 }
5728
5729 if matches!(self.mode, EditorMode::SingleLine) {
5730 cx.propagate();
5731 return;
5732 }
5733
5734 let row_count = if let Some(row_count) = self.visible_line_count() {
5735 row_count as u32 - 1
5736 } else {
5737 return;
5738 };
5739
5740 let autoscroll = if action.center_cursor {
5741 Autoscroll::center()
5742 } else {
5743 Autoscroll::fit()
5744 };
5745
5746 let text_layout_details = &self.text_layout_details(cx);
5747 self.change_selections(Some(autoscroll), cx, |s| {
5748 let line_mode = s.line_mode;
5749 s.move_with(|map, selection| {
5750 if !selection.is_empty() && !line_mode {
5751 selection.goal = SelectionGoal::None;
5752 }
5753 let (cursor, goal) = movement::down_by_rows(
5754 map,
5755 selection.end,
5756 row_count,
5757 selection.goal,
5758 false,
5759 &text_layout_details,
5760 );
5761 selection.collapse_to(cursor, goal);
5762 });
5763 });
5764 }
5765
5766 pub fn select_down(&mut self, _: &SelectDown, cx: &mut ViewContext<Self>) {
5767 let text_layout_details = &self.text_layout_details(cx);
5768 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5769 s.move_heads_with(|map, head, goal| {
5770 movement::down(map, head, goal, false, &text_layout_details)
5771 })
5772 });
5773 }
5774
5775 pub fn context_menu_first(&mut self, _: &ContextMenuFirst, cx: &mut ViewContext<Self>) {
5776 if let Some(context_menu) = self.context_menu.write().as_mut() {
5777 context_menu.select_first(self.project.as_ref(), cx);
5778 }
5779 }
5780
5781 pub fn context_menu_prev(&mut self, _: &ContextMenuPrev, cx: &mut ViewContext<Self>) {
5782 if let Some(context_menu) = self.context_menu.write().as_mut() {
5783 context_menu.select_prev(self.project.as_ref(), cx);
5784 }
5785 }
5786
5787 pub fn context_menu_next(&mut self, _: &ContextMenuNext, cx: &mut ViewContext<Self>) {
5788 if let Some(context_menu) = self.context_menu.write().as_mut() {
5789 context_menu.select_next(self.project.as_ref(), cx);
5790 }
5791 }
5792
5793 pub fn context_menu_last(&mut self, _: &ContextMenuLast, cx: &mut ViewContext<Self>) {
5794 if let Some(context_menu) = self.context_menu.write().as_mut() {
5795 context_menu.select_last(self.project.as_ref(), cx);
5796 }
5797 }
5798
5799 pub fn move_to_previous_word_start(
5800 &mut self,
5801 _: &MoveToPreviousWordStart,
5802 cx: &mut ViewContext<Self>,
5803 ) {
5804 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5805 s.move_cursors_with(|map, head, _| {
5806 (
5807 movement::previous_word_start(map, head),
5808 SelectionGoal::None,
5809 )
5810 });
5811 })
5812 }
5813
5814 pub fn move_to_previous_subword_start(
5815 &mut self,
5816 _: &MoveToPreviousSubwordStart,
5817 cx: &mut ViewContext<Self>,
5818 ) {
5819 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5820 s.move_cursors_with(|map, head, _| {
5821 (
5822 movement::previous_subword_start(map, head),
5823 SelectionGoal::None,
5824 )
5825 });
5826 })
5827 }
5828
5829 pub fn select_to_previous_word_start(
5830 &mut self,
5831 _: &SelectToPreviousWordStart,
5832 cx: &mut ViewContext<Self>,
5833 ) {
5834 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5835 s.move_heads_with(|map, head, _| {
5836 (
5837 movement::previous_word_start(map, head),
5838 SelectionGoal::None,
5839 )
5840 });
5841 })
5842 }
5843
5844 pub fn select_to_previous_subword_start(
5845 &mut self,
5846 _: &SelectToPreviousSubwordStart,
5847 cx: &mut ViewContext<Self>,
5848 ) {
5849 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5850 s.move_heads_with(|map, head, _| {
5851 (
5852 movement::previous_subword_start(map, head),
5853 SelectionGoal::None,
5854 )
5855 });
5856 })
5857 }
5858
5859 pub fn delete_to_previous_word_start(
5860 &mut self,
5861 _: &DeleteToPreviousWordStart,
5862 cx: &mut ViewContext<Self>,
5863 ) {
5864 self.transact(cx, |this, cx| {
5865 this.select_autoclose_pair(cx);
5866 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
5867 let line_mode = s.line_mode;
5868 s.move_with(|map, selection| {
5869 if selection.is_empty() && !line_mode {
5870 let cursor = movement::previous_word_start(map, selection.head());
5871 selection.set_head(cursor, SelectionGoal::None);
5872 }
5873 });
5874 });
5875 this.insert("", cx);
5876 });
5877 }
5878
5879 pub fn delete_to_previous_subword_start(
5880 &mut self,
5881 _: &DeleteToPreviousSubwordStart,
5882 cx: &mut ViewContext<Self>,
5883 ) {
5884 self.transact(cx, |this, cx| {
5885 this.select_autoclose_pair(cx);
5886 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
5887 let line_mode = s.line_mode;
5888 s.move_with(|map, selection| {
5889 if selection.is_empty() && !line_mode {
5890 let cursor = movement::previous_subword_start(map, selection.head());
5891 selection.set_head(cursor, SelectionGoal::None);
5892 }
5893 });
5894 });
5895 this.insert("", cx);
5896 });
5897 }
5898
5899 pub fn move_to_next_word_end(&mut self, _: &MoveToNextWordEnd, cx: &mut ViewContext<Self>) {
5900 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5901 s.move_cursors_with(|map, head, _| {
5902 (movement::next_word_end(map, head), SelectionGoal::None)
5903 });
5904 })
5905 }
5906
5907 pub fn move_to_next_subword_end(
5908 &mut self,
5909 _: &MoveToNextSubwordEnd,
5910 cx: &mut ViewContext<Self>,
5911 ) {
5912 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5913 s.move_cursors_with(|map, head, _| {
5914 (movement::next_subword_end(map, head), SelectionGoal::None)
5915 });
5916 })
5917 }
5918
5919 pub fn select_to_next_word_end(&mut self, _: &SelectToNextWordEnd, cx: &mut ViewContext<Self>) {
5920 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5921 s.move_heads_with(|map, head, _| {
5922 (movement::next_word_end(map, head), SelectionGoal::None)
5923 });
5924 })
5925 }
5926
5927 pub fn select_to_next_subword_end(
5928 &mut self,
5929 _: &SelectToNextSubwordEnd,
5930 cx: &mut ViewContext<Self>,
5931 ) {
5932 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5933 s.move_heads_with(|map, head, _| {
5934 (movement::next_subword_end(map, head), SelectionGoal::None)
5935 });
5936 })
5937 }
5938
5939 pub fn delete_to_next_word_end(&mut self, _: &DeleteToNextWordEnd, cx: &mut ViewContext<Self>) {
5940 self.transact(cx, |this, cx| {
5941 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
5942 let line_mode = s.line_mode;
5943 s.move_with(|map, selection| {
5944 if selection.is_empty() && !line_mode {
5945 let cursor = movement::next_word_end(map, selection.head());
5946 selection.set_head(cursor, SelectionGoal::None);
5947 }
5948 });
5949 });
5950 this.insert("", cx);
5951 });
5952 }
5953
5954 pub fn delete_to_next_subword_end(
5955 &mut self,
5956 _: &DeleteToNextSubwordEnd,
5957 cx: &mut ViewContext<Self>,
5958 ) {
5959 self.transact(cx, |this, cx| {
5960 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
5961 s.move_with(|map, selection| {
5962 if selection.is_empty() {
5963 let cursor = movement::next_subword_end(map, selection.head());
5964 selection.set_head(cursor, SelectionGoal::None);
5965 }
5966 });
5967 });
5968 this.insert("", cx);
5969 });
5970 }
5971
5972 pub fn move_to_beginning_of_line(
5973 &mut self,
5974 _: &MoveToBeginningOfLine,
5975 cx: &mut ViewContext<Self>,
5976 ) {
5977 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5978 s.move_cursors_with(|map, head, _| {
5979 (
5980 movement::indented_line_beginning(map, head, true),
5981 SelectionGoal::None,
5982 )
5983 });
5984 })
5985 }
5986
5987 pub fn select_to_beginning_of_line(
5988 &mut self,
5989 action: &SelectToBeginningOfLine,
5990 cx: &mut ViewContext<Self>,
5991 ) {
5992 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
5993 s.move_heads_with(|map, head, _| {
5994 (
5995 movement::indented_line_beginning(map, head, action.stop_at_soft_wraps),
5996 SelectionGoal::None,
5997 )
5998 });
5999 });
6000 }
6001
6002 pub fn delete_to_beginning_of_line(
6003 &mut self,
6004 _: &DeleteToBeginningOfLine,
6005 cx: &mut ViewContext<Self>,
6006 ) {
6007 self.transact(cx, |this, cx| {
6008 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
6009 s.move_with(|_, selection| {
6010 selection.reversed = true;
6011 });
6012 });
6013
6014 this.select_to_beginning_of_line(
6015 &SelectToBeginningOfLine {
6016 stop_at_soft_wraps: false,
6017 },
6018 cx,
6019 );
6020 this.backspace(&Backspace, cx);
6021 });
6022 }
6023
6024 pub fn move_to_end_of_line(&mut self, _: &MoveToEndOfLine, cx: &mut ViewContext<Self>) {
6025 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6026 s.move_cursors_with(|map, head, _| {
6027 (movement::line_end(map, head, true), SelectionGoal::None)
6028 });
6029 })
6030 }
6031
6032 pub fn select_to_end_of_line(
6033 &mut self,
6034 action: &SelectToEndOfLine,
6035 cx: &mut ViewContext<Self>,
6036 ) {
6037 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6038 s.move_heads_with(|map, head, _| {
6039 (
6040 movement::line_end(map, head, action.stop_at_soft_wraps),
6041 SelectionGoal::None,
6042 )
6043 });
6044 })
6045 }
6046
6047 pub fn delete_to_end_of_line(&mut self, _: &DeleteToEndOfLine, cx: &mut ViewContext<Self>) {
6048 self.transact(cx, |this, cx| {
6049 this.select_to_end_of_line(
6050 &SelectToEndOfLine {
6051 stop_at_soft_wraps: false,
6052 },
6053 cx,
6054 );
6055 this.delete(&Delete, cx);
6056 });
6057 }
6058
6059 pub fn cut_to_end_of_line(&mut self, _: &CutToEndOfLine, cx: &mut ViewContext<Self>) {
6060 self.transact(cx, |this, cx| {
6061 this.select_to_end_of_line(
6062 &SelectToEndOfLine {
6063 stop_at_soft_wraps: false,
6064 },
6065 cx,
6066 );
6067 this.cut(&Cut, cx);
6068 });
6069 }
6070
6071 pub fn move_to_start_of_paragraph(
6072 &mut self,
6073 _: &MoveToStartOfParagraph,
6074 cx: &mut ViewContext<Self>,
6075 ) {
6076 if matches!(self.mode, EditorMode::SingleLine) {
6077 cx.propagate();
6078 return;
6079 }
6080
6081 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6082 s.move_with(|map, selection| {
6083 selection.collapse_to(
6084 movement::start_of_paragraph(map, selection.head(), 1),
6085 SelectionGoal::None,
6086 )
6087 });
6088 })
6089 }
6090
6091 pub fn move_to_end_of_paragraph(
6092 &mut self,
6093 _: &MoveToEndOfParagraph,
6094 cx: &mut ViewContext<Self>,
6095 ) {
6096 if matches!(self.mode, EditorMode::SingleLine) {
6097 cx.propagate();
6098 return;
6099 }
6100
6101 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6102 s.move_with(|map, selection| {
6103 selection.collapse_to(
6104 movement::end_of_paragraph(map, selection.head(), 1),
6105 SelectionGoal::None,
6106 )
6107 });
6108 })
6109 }
6110
6111 pub fn select_to_start_of_paragraph(
6112 &mut self,
6113 _: &SelectToStartOfParagraph,
6114 cx: &mut ViewContext<Self>,
6115 ) {
6116 if matches!(self.mode, EditorMode::SingleLine) {
6117 cx.propagate();
6118 return;
6119 }
6120
6121 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6122 s.move_heads_with(|map, head, _| {
6123 (
6124 movement::start_of_paragraph(map, head, 1),
6125 SelectionGoal::None,
6126 )
6127 });
6128 })
6129 }
6130
6131 pub fn select_to_end_of_paragraph(
6132 &mut self,
6133 _: &SelectToEndOfParagraph,
6134 cx: &mut ViewContext<Self>,
6135 ) {
6136 if matches!(self.mode, EditorMode::SingleLine) {
6137 cx.propagate();
6138 return;
6139 }
6140
6141 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6142 s.move_heads_with(|map, head, _| {
6143 (
6144 movement::end_of_paragraph(map, head, 1),
6145 SelectionGoal::None,
6146 )
6147 });
6148 })
6149 }
6150
6151 pub fn move_to_beginning(&mut self, _: &MoveToBeginning, cx: &mut ViewContext<Self>) {
6152 if matches!(self.mode, EditorMode::SingleLine) {
6153 cx.propagate();
6154 return;
6155 }
6156
6157 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6158 s.select_ranges(vec![0..0]);
6159 });
6160 }
6161
6162 pub fn select_to_beginning(&mut self, _: &SelectToBeginning, cx: &mut ViewContext<Self>) {
6163 let mut selection = self.selections.last::<Point>(cx);
6164 selection.set_head(Point::zero(), SelectionGoal::None);
6165
6166 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6167 s.select(vec![selection]);
6168 });
6169 }
6170
6171 pub fn move_to_end(&mut self, _: &MoveToEnd, cx: &mut ViewContext<Self>) {
6172 if matches!(self.mode, EditorMode::SingleLine) {
6173 cx.propagate();
6174 return;
6175 }
6176
6177 let cursor = self.buffer.read(cx).read(cx).len();
6178 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6179 s.select_ranges(vec![cursor..cursor])
6180 });
6181 }
6182
6183 pub fn set_nav_history(&mut self, nav_history: Option<ItemNavHistory>) {
6184 self.nav_history = nav_history;
6185 }
6186
6187 pub fn nav_history(&self) -> Option<&ItemNavHistory> {
6188 self.nav_history.as_ref()
6189 }
6190
6191 fn push_to_nav_history(
6192 &mut self,
6193 cursor_anchor: Anchor,
6194 new_position: Option<Point>,
6195 cx: &mut ViewContext<Self>,
6196 ) {
6197 if let Some(nav_history) = self.nav_history.as_mut() {
6198 let buffer = self.buffer.read(cx).read(cx);
6199 let cursor_position = cursor_anchor.to_point(&buffer);
6200 let scroll_state = self.scroll_manager.anchor();
6201 let scroll_top_row = scroll_state.top_row(&buffer);
6202 drop(buffer);
6203
6204 if let Some(new_position) = new_position {
6205 let row_delta = (new_position.row as i64 - cursor_position.row as i64).abs();
6206 if row_delta < MIN_NAVIGATION_HISTORY_ROW_DELTA {
6207 return;
6208 }
6209 }
6210
6211 nav_history.push(
6212 Some(NavigationData {
6213 cursor_anchor,
6214 cursor_position,
6215 scroll_anchor: scroll_state,
6216 scroll_top_row,
6217 }),
6218 cx,
6219 );
6220 }
6221 }
6222
6223 pub fn select_to_end(&mut self, _: &SelectToEnd, cx: &mut ViewContext<Self>) {
6224 let buffer = self.buffer.read(cx).snapshot(cx);
6225 let mut selection = self.selections.first::<usize>(cx);
6226 selection.set_head(buffer.len(), SelectionGoal::None);
6227 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6228 s.select(vec![selection]);
6229 });
6230 }
6231
6232 pub fn select_all(&mut self, _: &SelectAll, cx: &mut ViewContext<Self>) {
6233 let end = self.buffer.read(cx).read(cx).len();
6234 self.change_selections(None, cx, |s| {
6235 s.select_ranges(vec![0..end]);
6236 });
6237 }
6238
6239 pub fn select_line(&mut self, _: &SelectLine, cx: &mut ViewContext<Self>) {
6240 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
6241 let mut selections = self.selections.all::<Point>(cx);
6242 let max_point = display_map.buffer_snapshot.max_point();
6243 for selection in &mut selections {
6244 let rows = selection.spanned_rows(true, &display_map);
6245 selection.start = Point::new(rows.start, 0);
6246 selection.end = cmp::min(max_point, Point::new(rows.end, 0));
6247 selection.reversed = false;
6248 }
6249 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6250 s.select(selections);
6251 });
6252 }
6253
6254 pub fn split_selection_into_lines(
6255 &mut self,
6256 _: &SplitSelectionIntoLines,
6257 cx: &mut ViewContext<Self>,
6258 ) {
6259 let mut to_unfold = Vec::new();
6260 let mut new_selection_ranges = Vec::new();
6261 {
6262 let selections = self.selections.all::<Point>(cx);
6263 let buffer = self.buffer.read(cx).read(cx);
6264 for selection in selections {
6265 for row in selection.start.row..selection.end.row {
6266 let cursor = Point::new(row, buffer.line_len(row));
6267 new_selection_ranges.push(cursor..cursor);
6268 }
6269 new_selection_ranges.push(selection.end..selection.end);
6270 to_unfold.push(selection.start..selection.end);
6271 }
6272 }
6273 self.unfold_ranges(to_unfold, true, true, cx);
6274 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6275 s.select_ranges(new_selection_ranges);
6276 });
6277 }
6278
6279 pub fn add_selection_above(&mut self, _: &AddSelectionAbove, cx: &mut ViewContext<Self>) {
6280 self.add_selection(true, cx);
6281 }
6282
6283 pub fn add_selection_below(&mut self, _: &AddSelectionBelow, cx: &mut ViewContext<Self>) {
6284 self.add_selection(false, cx);
6285 }
6286
6287 fn add_selection(&mut self, above: bool, cx: &mut ViewContext<Self>) {
6288 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
6289 let mut selections = self.selections.all::<Point>(cx);
6290 let text_layout_details = self.text_layout_details(cx);
6291 let mut state = self.add_selections_state.take().unwrap_or_else(|| {
6292 let oldest_selection = selections.iter().min_by_key(|s| s.id).unwrap().clone();
6293 let range = oldest_selection.display_range(&display_map).sorted();
6294
6295 let start_x = display_map.x_for_display_point(range.start, &text_layout_details);
6296 let end_x = display_map.x_for_display_point(range.end, &text_layout_details);
6297 let positions = start_x.min(end_x)..start_x.max(end_x);
6298
6299 selections.clear();
6300 let mut stack = Vec::new();
6301 for row in range.start.row()..=range.end.row() {
6302 if let Some(selection) = self.selections.build_columnar_selection(
6303 &display_map,
6304 row,
6305 &positions,
6306 oldest_selection.reversed,
6307 &text_layout_details,
6308 ) {
6309 stack.push(selection.id);
6310 selections.push(selection);
6311 }
6312 }
6313
6314 if above {
6315 stack.reverse();
6316 }
6317
6318 AddSelectionsState { above, stack }
6319 });
6320
6321 let last_added_selection = *state.stack.last().unwrap();
6322 let mut new_selections = Vec::new();
6323 if above == state.above {
6324 let end_row = if above {
6325 0
6326 } else {
6327 display_map.max_point().row()
6328 };
6329
6330 'outer: for selection in selections {
6331 if selection.id == last_added_selection {
6332 let range = selection.display_range(&display_map).sorted();
6333 debug_assert_eq!(range.start.row(), range.end.row());
6334 let mut row = range.start.row();
6335 let positions =
6336 if let SelectionGoal::HorizontalRange { start, end } = selection.goal {
6337 px(start)..px(end)
6338 } else {
6339 let start_x =
6340 display_map.x_for_display_point(range.start, &text_layout_details);
6341 let end_x =
6342 display_map.x_for_display_point(range.end, &text_layout_details);
6343 start_x.min(end_x)..start_x.max(end_x)
6344 };
6345
6346 while row != end_row {
6347 if above {
6348 row -= 1;
6349 } else {
6350 row += 1;
6351 }
6352
6353 if let Some(new_selection) = self.selections.build_columnar_selection(
6354 &display_map,
6355 row,
6356 &positions,
6357 selection.reversed,
6358 &text_layout_details,
6359 ) {
6360 state.stack.push(new_selection.id);
6361 if above {
6362 new_selections.push(new_selection);
6363 new_selections.push(selection);
6364 } else {
6365 new_selections.push(selection);
6366 new_selections.push(new_selection);
6367 }
6368
6369 continue 'outer;
6370 }
6371 }
6372 }
6373
6374 new_selections.push(selection);
6375 }
6376 } else {
6377 new_selections = selections;
6378 new_selections.retain(|s| s.id != last_added_selection);
6379 state.stack.pop();
6380 }
6381
6382 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6383 s.select(new_selections);
6384 });
6385 if state.stack.len() > 1 {
6386 self.add_selections_state = Some(state);
6387 }
6388 }
6389
6390 pub fn select_next_match_internal(
6391 &mut self,
6392 display_map: &DisplaySnapshot,
6393 replace_newest: bool,
6394 autoscroll: Option<Autoscroll>,
6395 cx: &mut ViewContext<Self>,
6396 ) -> Result<()> {
6397 fn select_next_match_ranges(
6398 this: &mut Editor,
6399 range: Range<usize>,
6400 replace_newest: bool,
6401 auto_scroll: Option<Autoscroll>,
6402 cx: &mut ViewContext<Editor>,
6403 ) {
6404 this.unfold_ranges([range.clone()], false, true, cx);
6405 this.change_selections(auto_scroll, cx, |s| {
6406 if replace_newest {
6407 s.delete(s.newest_anchor().id);
6408 }
6409 s.insert_range(range.clone());
6410 });
6411 }
6412
6413 let buffer = &display_map.buffer_snapshot;
6414 let mut selections = self.selections.all::<usize>(cx);
6415 if let Some(mut select_next_state) = self.select_next_state.take() {
6416 let query = &select_next_state.query;
6417 if !select_next_state.done {
6418 let first_selection = selections.iter().min_by_key(|s| s.id).unwrap();
6419 let last_selection = selections.iter().max_by_key(|s| s.id).unwrap();
6420 let mut next_selected_range = None;
6421
6422 let bytes_after_last_selection =
6423 buffer.bytes_in_range(last_selection.end..buffer.len());
6424 let bytes_before_first_selection = buffer.bytes_in_range(0..first_selection.start);
6425 let query_matches = query
6426 .stream_find_iter(bytes_after_last_selection)
6427 .map(|result| (last_selection.end, result))
6428 .chain(
6429 query
6430 .stream_find_iter(bytes_before_first_selection)
6431 .map(|result| (0, result)),
6432 );
6433
6434 for (start_offset, query_match) in query_matches {
6435 let query_match = query_match.unwrap(); // can only fail due to I/O
6436 let offset_range =
6437 start_offset + query_match.start()..start_offset + query_match.end();
6438 let display_range = offset_range.start.to_display_point(&display_map)
6439 ..offset_range.end.to_display_point(&display_map);
6440
6441 if !select_next_state.wordwise
6442 || (!movement::is_inside_word(&display_map, display_range.start)
6443 && !movement::is_inside_word(&display_map, display_range.end))
6444 {
6445 if selections
6446 .iter()
6447 .find(|selection| selection.range().overlaps(&offset_range))
6448 .is_none()
6449 {
6450 next_selected_range = Some(offset_range);
6451 break;
6452 }
6453 }
6454 }
6455
6456 if let Some(next_selected_range) = next_selected_range {
6457 select_next_match_ranges(
6458 self,
6459 next_selected_range,
6460 replace_newest,
6461 autoscroll,
6462 cx,
6463 );
6464 } else {
6465 select_next_state.done = true;
6466 }
6467 }
6468
6469 self.select_next_state = Some(select_next_state);
6470 } else if selections.len() == 1 {
6471 let selection = selections.last_mut().unwrap();
6472 if selection.start == selection.end {
6473 let word_range = movement::surrounding_word(
6474 &display_map,
6475 selection.start.to_display_point(&display_map),
6476 );
6477 selection.start = word_range.start.to_offset(&display_map, Bias::Left);
6478 selection.end = word_range.end.to_offset(&display_map, Bias::Left);
6479 selection.goal = SelectionGoal::None;
6480 selection.reversed = false;
6481
6482 let query = buffer
6483 .text_for_range(selection.start..selection.end)
6484 .collect::<String>();
6485
6486 let is_empty = query.is_empty();
6487 let select_state = SelectNextState {
6488 query: AhoCorasick::new(&[query])?,
6489 wordwise: true,
6490 done: is_empty,
6491 };
6492 select_next_match_ranges(
6493 self,
6494 selection.start..selection.end,
6495 replace_newest,
6496 autoscroll,
6497 cx,
6498 );
6499 self.select_next_state = Some(select_state);
6500 } else {
6501 let query = buffer
6502 .text_for_range(selection.start..selection.end)
6503 .collect::<String>();
6504 self.select_next_state = Some(SelectNextState {
6505 query: AhoCorasick::new(&[query])?,
6506 wordwise: false,
6507 done: false,
6508 });
6509 self.select_next_match_internal(display_map, replace_newest, autoscroll, cx)?;
6510 }
6511 }
6512 Ok(())
6513 }
6514
6515 pub fn select_all_matches(
6516 &mut self,
6517 action: &SelectAllMatches,
6518 cx: &mut ViewContext<Self>,
6519 ) -> Result<()> {
6520 self.push_to_selection_history();
6521 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
6522
6523 loop {
6524 self.select_next_match_internal(&display_map, action.replace_newest, None, cx)?;
6525
6526 if self
6527 .select_next_state
6528 .as_ref()
6529 .map(|selection_state| selection_state.done)
6530 .unwrap_or(true)
6531 {
6532 break;
6533 }
6534 }
6535
6536 Ok(())
6537 }
6538
6539 pub fn select_next(&mut self, action: &SelectNext, cx: &mut ViewContext<Self>) -> Result<()> {
6540 self.push_to_selection_history();
6541 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
6542 self.select_next_match_internal(
6543 &display_map,
6544 action.replace_newest,
6545 Some(Autoscroll::newest()),
6546 cx,
6547 )?;
6548 Ok(())
6549 }
6550
6551 pub fn select_previous(
6552 &mut self,
6553 action: &SelectPrevious,
6554 cx: &mut ViewContext<Self>,
6555 ) -> Result<()> {
6556 self.push_to_selection_history();
6557 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
6558 let buffer = &display_map.buffer_snapshot;
6559 let mut selections = self.selections.all::<usize>(cx);
6560 if let Some(mut select_prev_state) = self.select_prev_state.take() {
6561 let query = &select_prev_state.query;
6562 if !select_prev_state.done {
6563 let first_selection = selections.iter().min_by_key(|s| s.id).unwrap();
6564 let last_selection = selections.iter().max_by_key(|s| s.id).unwrap();
6565 let mut next_selected_range = None;
6566 // When we're iterating matches backwards, the oldest match will actually be the furthest one in the buffer.
6567 let bytes_before_last_selection =
6568 buffer.reversed_bytes_in_range(0..last_selection.start);
6569 let bytes_after_first_selection =
6570 buffer.reversed_bytes_in_range(first_selection.end..buffer.len());
6571 let query_matches = query
6572 .stream_find_iter(bytes_before_last_selection)
6573 .map(|result| (last_selection.start, result))
6574 .chain(
6575 query
6576 .stream_find_iter(bytes_after_first_selection)
6577 .map(|result| (buffer.len(), result)),
6578 );
6579 for (end_offset, query_match) in query_matches {
6580 let query_match = query_match.unwrap(); // can only fail due to I/O
6581 let offset_range =
6582 end_offset - query_match.end()..end_offset - query_match.start();
6583 let display_range = offset_range.start.to_display_point(&display_map)
6584 ..offset_range.end.to_display_point(&display_map);
6585
6586 if !select_prev_state.wordwise
6587 || (!movement::is_inside_word(&display_map, display_range.start)
6588 && !movement::is_inside_word(&display_map, display_range.end))
6589 {
6590 next_selected_range = Some(offset_range);
6591 break;
6592 }
6593 }
6594
6595 if let Some(next_selected_range) = next_selected_range {
6596 self.unfold_ranges([next_selected_range.clone()], false, true, cx);
6597 self.change_selections(Some(Autoscroll::newest()), cx, |s| {
6598 if action.replace_newest {
6599 s.delete(s.newest_anchor().id);
6600 }
6601 s.insert_range(next_selected_range);
6602 });
6603 } else {
6604 select_prev_state.done = true;
6605 }
6606 }
6607
6608 self.select_prev_state = Some(select_prev_state);
6609 } else if selections.len() == 1 {
6610 let selection = selections.last_mut().unwrap();
6611 if selection.start == selection.end {
6612 let word_range = movement::surrounding_word(
6613 &display_map,
6614 selection.start.to_display_point(&display_map),
6615 );
6616 selection.start = word_range.start.to_offset(&display_map, Bias::Left);
6617 selection.end = word_range.end.to_offset(&display_map, Bias::Left);
6618 selection.goal = SelectionGoal::None;
6619 selection.reversed = false;
6620
6621 let query = buffer
6622 .text_for_range(selection.start..selection.end)
6623 .collect::<String>();
6624 let query = query.chars().rev().collect::<String>();
6625 let select_state = SelectNextState {
6626 query: AhoCorasick::new(&[query])?,
6627 wordwise: true,
6628 done: false,
6629 };
6630 self.unfold_ranges([selection.start..selection.end], false, true, cx);
6631 self.change_selections(Some(Autoscroll::newest()), cx, |s| {
6632 s.select(selections);
6633 });
6634 self.select_prev_state = Some(select_state);
6635 } else {
6636 let query = buffer
6637 .text_for_range(selection.start..selection.end)
6638 .collect::<String>();
6639 let query = query.chars().rev().collect::<String>();
6640 self.select_prev_state = Some(SelectNextState {
6641 query: AhoCorasick::new(&[query])?,
6642 wordwise: false,
6643 done: false,
6644 });
6645 self.select_previous(action, cx)?;
6646 }
6647 }
6648 Ok(())
6649 }
6650
6651 pub fn toggle_comments(&mut self, action: &ToggleComments, cx: &mut ViewContext<Self>) {
6652 let text_layout_details = &self.text_layout_details(cx);
6653 self.transact(cx, |this, cx| {
6654 let mut selections = this.selections.all::<Point>(cx);
6655 let mut edits = Vec::new();
6656 let mut selection_edit_ranges = Vec::new();
6657 let mut last_toggled_row = None;
6658 let snapshot = this.buffer.read(cx).read(cx);
6659 let empty_str: Arc<str> = "".into();
6660 let mut suffixes_inserted = Vec::new();
6661
6662 fn comment_prefix_range(
6663 snapshot: &MultiBufferSnapshot,
6664 row: u32,
6665 comment_prefix: &str,
6666 comment_prefix_whitespace: &str,
6667 ) -> Range<Point> {
6668 let start = Point::new(row, snapshot.indent_size_for_line(row).len);
6669
6670 let mut line_bytes = snapshot
6671 .bytes_in_range(start..snapshot.max_point())
6672 .flatten()
6673 .copied();
6674
6675 // If this line currently begins with the line comment prefix, then record
6676 // the range containing the prefix.
6677 if line_bytes
6678 .by_ref()
6679 .take(comment_prefix.len())
6680 .eq(comment_prefix.bytes())
6681 {
6682 // Include any whitespace that matches the comment prefix.
6683 let matching_whitespace_len = line_bytes
6684 .zip(comment_prefix_whitespace.bytes())
6685 .take_while(|(a, b)| a == b)
6686 .count() as u32;
6687 let end = Point::new(
6688 start.row,
6689 start.column + comment_prefix.len() as u32 + matching_whitespace_len,
6690 );
6691 start..end
6692 } else {
6693 start..start
6694 }
6695 }
6696
6697 fn comment_suffix_range(
6698 snapshot: &MultiBufferSnapshot,
6699 row: u32,
6700 comment_suffix: &str,
6701 comment_suffix_has_leading_space: bool,
6702 ) -> Range<Point> {
6703 let end = Point::new(row, snapshot.line_len(row));
6704 let suffix_start_column = end.column.saturating_sub(comment_suffix.len() as u32);
6705
6706 let mut line_end_bytes = snapshot
6707 .bytes_in_range(Point::new(end.row, suffix_start_column.saturating_sub(1))..end)
6708 .flatten()
6709 .copied();
6710
6711 let leading_space_len = if suffix_start_column > 0
6712 && line_end_bytes.next() == Some(b' ')
6713 && comment_suffix_has_leading_space
6714 {
6715 1
6716 } else {
6717 0
6718 };
6719
6720 // If this line currently begins with the line comment prefix, then record
6721 // the range containing the prefix.
6722 if line_end_bytes.by_ref().eq(comment_suffix.bytes()) {
6723 let start = Point::new(end.row, suffix_start_column - leading_space_len);
6724 start..end
6725 } else {
6726 end..end
6727 }
6728 }
6729
6730 // TODO: Handle selections that cross excerpts
6731 for selection in &mut selections {
6732 let start_column = snapshot.indent_size_for_line(selection.start.row).len;
6733 let language = if let Some(language) =
6734 snapshot.language_scope_at(Point::new(selection.start.row, start_column))
6735 {
6736 language
6737 } else {
6738 continue;
6739 };
6740
6741 selection_edit_ranges.clear();
6742
6743 // If multiple selections contain a given row, avoid processing that
6744 // row more than once.
6745 let mut start_row = selection.start.row;
6746 if last_toggled_row == Some(start_row) {
6747 start_row += 1;
6748 }
6749 let end_row =
6750 if selection.end.row > selection.start.row && selection.end.column == 0 {
6751 selection.end.row - 1
6752 } else {
6753 selection.end.row
6754 };
6755 last_toggled_row = Some(end_row);
6756
6757 if start_row > end_row {
6758 continue;
6759 }
6760
6761 // If the language has line comments, toggle those.
6762 if let Some(full_comment_prefix) = language.line_comment_prefix() {
6763 // Split the comment prefix's trailing whitespace into a separate string,
6764 // as that portion won't be used for detecting if a line is a comment.
6765 let comment_prefix = full_comment_prefix.trim_end_matches(' ');
6766 let comment_prefix_whitespace = &full_comment_prefix[comment_prefix.len()..];
6767 let mut all_selection_lines_are_comments = true;
6768
6769 for row in start_row..=end_row {
6770 if snapshot.is_line_blank(row) && start_row < end_row {
6771 continue;
6772 }
6773
6774 let prefix_range = comment_prefix_range(
6775 snapshot.deref(),
6776 row,
6777 comment_prefix,
6778 comment_prefix_whitespace,
6779 );
6780 if prefix_range.is_empty() {
6781 all_selection_lines_are_comments = false;
6782 }
6783 selection_edit_ranges.push(prefix_range);
6784 }
6785
6786 if all_selection_lines_are_comments {
6787 edits.extend(
6788 selection_edit_ranges
6789 .iter()
6790 .cloned()
6791 .map(|range| (range, empty_str.clone())),
6792 );
6793 } else {
6794 let min_column = selection_edit_ranges
6795 .iter()
6796 .map(|r| r.start.column)
6797 .min()
6798 .unwrap_or(0);
6799 edits.extend(selection_edit_ranges.iter().map(|range| {
6800 let position = Point::new(range.start.row, min_column);
6801 (position..position, full_comment_prefix.clone())
6802 }));
6803 }
6804 } else if let Some((full_comment_prefix, comment_suffix)) =
6805 language.block_comment_delimiters()
6806 {
6807 let comment_prefix = full_comment_prefix.trim_end_matches(' ');
6808 let comment_prefix_whitespace = &full_comment_prefix[comment_prefix.len()..];
6809 let prefix_range = comment_prefix_range(
6810 snapshot.deref(),
6811 start_row,
6812 comment_prefix,
6813 comment_prefix_whitespace,
6814 );
6815 let suffix_range = comment_suffix_range(
6816 snapshot.deref(),
6817 end_row,
6818 comment_suffix.trim_start_matches(' '),
6819 comment_suffix.starts_with(' '),
6820 );
6821
6822 if prefix_range.is_empty() || suffix_range.is_empty() {
6823 edits.push((
6824 prefix_range.start..prefix_range.start,
6825 full_comment_prefix.clone(),
6826 ));
6827 edits.push((suffix_range.end..suffix_range.end, comment_suffix.clone()));
6828 suffixes_inserted.push((end_row, comment_suffix.len()));
6829 } else {
6830 edits.push((prefix_range, empty_str.clone()));
6831 edits.push((suffix_range, empty_str.clone()));
6832 }
6833 } else {
6834 continue;
6835 }
6836 }
6837
6838 drop(snapshot);
6839 this.buffer.update(cx, |buffer, cx| {
6840 buffer.edit(edits, None, cx);
6841 });
6842
6843 // Adjust selections so that they end before any comment suffixes that
6844 // were inserted.
6845 let mut suffixes_inserted = suffixes_inserted.into_iter().peekable();
6846 let mut selections = this.selections.all::<Point>(cx);
6847 let snapshot = this.buffer.read(cx).read(cx);
6848 for selection in &mut selections {
6849 while let Some((row, suffix_len)) = suffixes_inserted.peek().copied() {
6850 match row.cmp(&selection.end.row) {
6851 Ordering::Less => {
6852 suffixes_inserted.next();
6853 continue;
6854 }
6855 Ordering::Greater => break,
6856 Ordering::Equal => {
6857 if selection.end.column == snapshot.line_len(row) {
6858 if selection.is_empty() {
6859 selection.start.column -= suffix_len as u32;
6860 }
6861 selection.end.column -= suffix_len as u32;
6862 }
6863 break;
6864 }
6865 }
6866 }
6867 }
6868
6869 drop(snapshot);
6870 this.change_selections(Some(Autoscroll::fit()), cx, |s| s.select(selections));
6871
6872 let selections = this.selections.all::<Point>(cx);
6873 let selections_on_single_row = selections.windows(2).all(|selections| {
6874 selections[0].start.row == selections[1].start.row
6875 && selections[0].end.row == selections[1].end.row
6876 && selections[0].start.row == selections[0].end.row
6877 });
6878 let selections_selecting = selections
6879 .iter()
6880 .any(|selection| selection.start != selection.end);
6881 let advance_downwards = action.advance_downwards
6882 && selections_on_single_row
6883 && !selections_selecting
6884 && this.mode != EditorMode::SingleLine;
6885
6886 if advance_downwards {
6887 let snapshot = this.buffer.read(cx).snapshot(cx);
6888
6889 this.change_selections(Some(Autoscroll::fit()), cx, |s| {
6890 s.move_cursors_with(|display_snapshot, display_point, _| {
6891 let mut point = display_point.to_point(display_snapshot);
6892 point.row += 1;
6893 point = snapshot.clip_point(point, Bias::Left);
6894 let display_point = point.to_display_point(display_snapshot);
6895 let goal = SelectionGoal::HorizontalPosition(
6896 display_snapshot
6897 .x_for_display_point(display_point, &text_layout_details)
6898 .into(),
6899 );
6900 (display_point, goal)
6901 })
6902 });
6903 }
6904 });
6905 }
6906
6907 pub fn select_larger_syntax_node(
6908 &mut self,
6909 _: &SelectLargerSyntaxNode,
6910 cx: &mut ViewContext<Self>,
6911 ) {
6912 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
6913 let buffer = self.buffer.read(cx).snapshot(cx);
6914 let old_selections = self.selections.all::<usize>(cx).into_boxed_slice();
6915
6916 let mut stack = mem::take(&mut self.select_larger_syntax_node_stack);
6917 let mut selected_larger_node = false;
6918 let new_selections = old_selections
6919 .iter()
6920 .map(|selection| {
6921 let old_range = selection.start..selection.end;
6922 let mut new_range = old_range.clone();
6923 while let Some(containing_range) =
6924 buffer.range_for_syntax_ancestor(new_range.clone())
6925 {
6926 new_range = containing_range;
6927 if !display_map.intersects_fold(new_range.start)
6928 && !display_map.intersects_fold(new_range.end)
6929 {
6930 break;
6931 }
6932 }
6933
6934 selected_larger_node |= new_range != old_range;
6935 Selection {
6936 id: selection.id,
6937 start: new_range.start,
6938 end: new_range.end,
6939 goal: SelectionGoal::None,
6940 reversed: selection.reversed,
6941 }
6942 })
6943 .collect::<Vec<_>>();
6944
6945 if selected_larger_node {
6946 stack.push(old_selections);
6947 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6948 s.select(new_selections);
6949 });
6950 }
6951 self.select_larger_syntax_node_stack = stack;
6952 }
6953
6954 pub fn select_smaller_syntax_node(
6955 &mut self,
6956 _: &SelectSmallerSyntaxNode,
6957 cx: &mut ViewContext<Self>,
6958 ) {
6959 let mut stack = mem::take(&mut self.select_larger_syntax_node_stack);
6960 if let Some(selections) = stack.pop() {
6961 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6962 s.select(selections.to_vec());
6963 });
6964 }
6965 self.select_larger_syntax_node_stack = stack;
6966 }
6967
6968 pub fn move_to_enclosing_bracket(
6969 &mut self,
6970 _: &MoveToEnclosingBracket,
6971 cx: &mut ViewContext<Self>,
6972 ) {
6973 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
6974 s.move_offsets_with(|snapshot, selection| {
6975 let Some(enclosing_bracket_ranges) =
6976 snapshot.enclosing_bracket_ranges(selection.start..selection.end)
6977 else {
6978 return;
6979 };
6980
6981 let mut best_length = usize::MAX;
6982 let mut best_inside = false;
6983 let mut best_in_bracket_range = false;
6984 let mut best_destination = None;
6985 for (open, close) in enclosing_bracket_ranges {
6986 let close = close.to_inclusive();
6987 let length = close.end() - open.start;
6988 let inside = selection.start >= open.end && selection.end <= *close.start();
6989 let in_bracket_range = open.to_inclusive().contains(&selection.head())
6990 || close.contains(&selection.head());
6991
6992 // If best is next to a bracket and current isn't, skip
6993 if !in_bracket_range && best_in_bracket_range {
6994 continue;
6995 }
6996
6997 // Prefer smaller lengths unless best is inside and current isn't
6998 if length > best_length && (best_inside || !inside) {
6999 continue;
7000 }
7001
7002 best_length = length;
7003 best_inside = inside;
7004 best_in_bracket_range = in_bracket_range;
7005 best_destination = Some(
7006 if close.contains(&selection.start) && close.contains(&selection.end) {
7007 if inside {
7008 open.end
7009 } else {
7010 open.start
7011 }
7012 } else {
7013 if inside {
7014 *close.start()
7015 } else {
7016 *close.end()
7017 }
7018 },
7019 );
7020 }
7021
7022 if let Some(destination) = best_destination {
7023 selection.collapse_to(destination, SelectionGoal::None);
7024 }
7025 })
7026 });
7027 }
7028
7029 pub fn undo_selection(&mut self, _: &UndoSelection, cx: &mut ViewContext<Self>) {
7030 self.end_selection(cx);
7031 self.selection_history.mode = SelectionHistoryMode::Undoing;
7032 if let Some(entry) = self.selection_history.undo_stack.pop_back() {
7033 self.change_selections(None, cx, |s| s.select_anchors(entry.selections.to_vec()));
7034 self.select_next_state = entry.select_next_state;
7035 self.select_prev_state = entry.select_prev_state;
7036 self.add_selections_state = entry.add_selections_state;
7037 self.request_autoscroll(Autoscroll::newest(), cx);
7038 }
7039 self.selection_history.mode = SelectionHistoryMode::Normal;
7040 }
7041
7042 pub fn redo_selection(&mut self, _: &RedoSelection, cx: &mut ViewContext<Self>) {
7043 self.end_selection(cx);
7044 self.selection_history.mode = SelectionHistoryMode::Redoing;
7045 if let Some(entry) = self.selection_history.redo_stack.pop_back() {
7046 self.change_selections(None, cx, |s| s.select_anchors(entry.selections.to_vec()));
7047 self.select_next_state = entry.select_next_state;
7048 self.select_prev_state = entry.select_prev_state;
7049 self.add_selections_state = entry.add_selections_state;
7050 self.request_autoscroll(Autoscroll::newest(), cx);
7051 }
7052 self.selection_history.mode = SelectionHistoryMode::Normal;
7053 }
7054
7055 fn go_to_diagnostic(&mut self, _: &GoToDiagnostic, cx: &mut ViewContext<Self>) {
7056 self.go_to_diagnostic_impl(Direction::Next, cx)
7057 }
7058
7059 fn go_to_prev_diagnostic(&mut self, _: &GoToPrevDiagnostic, cx: &mut ViewContext<Self>) {
7060 self.go_to_diagnostic_impl(Direction::Prev, cx)
7061 }
7062
7063 pub fn go_to_diagnostic_impl(&mut self, direction: Direction, cx: &mut ViewContext<Self>) {
7064 let buffer = self.buffer.read(cx).snapshot(cx);
7065 let selection = self.selections.newest::<usize>(cx);
7066
7067 // If there is an active Diagnostic Popover jump to its diagnostic instead.
7068 if direction == Direction::Next {
7069 if let Some(popover) = self.hover_state.diagnostic_popover.as_ref() {
7070 let (group_id, jump_to) = popover.activation_info();
7071 if self.activate_diagnostics(group_id, cx) {
7072 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
7073 let mut new_selection = s.newest_anchor().clone();
7074 new_selection.collapse_to(jump_to, SelectionGoal::None);
7075 s.select_anchors(vec![new_selection.clone()]);
7076 });
7077 }
7078 return;
7079 }
7080 }
7081
7082 let mut active_primary_range = self.active_diagnostics.as_ref().map(|active_diagnostics| {
7083 active_diagnostics
7084 .primary_range
7085 .to_offset(&buffer)
7086 .to_inclusive()
7087 });
7088 let mut search_start = if let Some(active_primary_range) = active_primary_range.as_ref() {
7089 if active_primary_range.contains(&selection.head()) {
7090 *active_primary_range.end()
7091 } else {
7092 selection.head()
7093 }
7094 } else {
7095 selection.head()
7096 };
7097
7098 loop {
7099 let mut diagnostics = if direction == Direction::Prev {
7100 buffer.diagnostics_in_range::<_, usize>(0..search_start, true)
7101 } else {
7102 buffer.diagnostics_in_range::<_, usize>(search_start..buffer.len(), false)
7103 };
7104 let group = diagnostics.find_map(|entry| {
7105 if entry.diagnostic.is_primary
7106 && entry.diagnostic.severity <= DiagnosticSeverity::WARNING
7107 && !entry.range.is_empty()
7108 && Some(entry.range.end) != active_primary_range.as_ref().map(|r| *r.end())
7109 && !entry.range.contains(&search_start)
7110 {
7111 Some((entry.range, entry.diagnostic.group_id))
7112 } else {
7113 None
7114 }
7115 });
7116
7117 if let Some((primary_range, group_id)) = group {
7118 if self.activate_diagnostics(group_id, cx) {
7119 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
7120 s.select(vec![Selection {
7121 id: selection.id,
7122 start: primary_range.start,
7123 end: primary_range.start,
7124 reversed: false,
7125 goal: SelectionGoal::None,
7126 }]);
7127 });
7128 }
7129 break;
7130 } else {
7131 // Cycle around to the start of the buffer, potentially moving back to the start of
7132 // the currently active diagnostic.
7133 active_primary_range.take();
7134 if direction == Direction::Prev {
7135 if search_start == buffer.len() {
7136 break;
7137 } else {
7138 search_start = buffer.len();
7139 }
7140 } else if search_start == 0 {
7141 break;
7142 } else {
7143 search_start = 0;
7144 }
7145 }
7146 }
7147 }
7148
7149 fn go_to_hunk(&mut self, _: &GoToHunk, cx: &mut ViewContext<Self>) {
7150 let snapshot = self
7151 .display_map
7152 .update(cx, |display_map, cx| display_map.snapshot(cx));
7153 let selection = self.selections.newest::<Point>(cx);
7154
7155 if !self.seek_in_direction(
7156 &snapshot,
7157 selection.head(),
7158 false,
7159 snapshot
7160 .buffer_snapshot
7161 .git_diff_hunks_in_range((selection.head().row + 1)..u32::MAX),
7162 cx,
7163 ) {
7164 let wrapped_point = Point::zero();
7165 self.seek_in_direction(
7166 &snapshot,
7167 wrapped_point,
7168 true,
7169 snapshot
7170 .buffer_snapshot
7171 .git_diff_hunks_in_range((wrapped_point.row + 1)..u32::MAX),
7172 cx,
7173 );
7174 }
7175 }
7176
7177 fn go_to_prev_hunk(&mut self, _: &GoToPrevHunk, cx: &mut ViewContext<Self>) {
7178 let snapshot = self
7179 .display_map
7180 .update(cx, |display_map, cx| display_map.snapshot(cx));
7181 let selection = self.selections.newest::<Point>(cx);
7182
7183 if !self.seek_in_direction(
7184 &snapshot,
7185 selection.head(),
7186 false,
7187 snapshot
7188 .buffer_snapshot
7189 .git_diff_hunks_in_range_rev(0..selection.head().row),
7190 cx,
7191 ) {
7192 let wrapped_point = snapshot.buffer_snapshot.max_point();
7193 self.seek_in_direction(
7194 &snapshot,
7195 wrapped_point,
7196 true,
7197 snapshot
7198 .buffer_snapshot
7199 .git_diff_hunks_in_range_rev(0..wrapped_point.row),
7200 cx,
7201 );
7202 }
7203 }
7204
7205 fn seek_in_direction(
7206 &mut self,
7207 snapshot: &DisplaySnapshot,
7208 initial_point: Point,
7209 is_wrapped: bool,
7210 hunks: impl Iterator<Item = DiffHunk<u32>>,
7211 cx: &mut ViewContext<Editor>,
7212 ) -> bool {
7213 let display_point = initial_point.to_display_point(snapshot);
7214 let mut hunks = hunks
7215 .map(|hunk| diff_hunk_to_display(hunk, &snapshot))
7216 .filter(|hunk| {
7217 if is_wrapped {
7218 true
7219 } else {
7220 !hunk.contains_display_row(display_point.row())
7221 }
7222 })
7223 .dedup();
7224
7225 if let Some(hunk) = hunks.next() {
7226 self.change_selections(Some(Autoscroll::fit()), cx, |s| {
7227 let row = hunk.start_display_row();
7228 let point = DisplayPoint::new(row, 0);
7229 s.select_display_ranges([point..point]);
7230 });
7231
7232 true
7233 } else {
7234 false
7235 }
7236 }
7237
7238 pub fn go_to_definition(&mut self, _: &GoToDefinition, cx: &mut ViewContext<Self>) {
7239 self.go_to_definition_of_kind(GotoDefinitionKind::Symbol, false, cx);
7240 }
7241
7242 pub fn go_to_type_definition(&mut self, _: &GoToTypeDefinition, cx: &mut ViewContext<Self>) {
7243 self.go_to_definition_of_kind(GotoDefinitionKind::Type, false, cx);
7244 }
7245
7246 pub fn go_to_definition_split(&mut self, _: &GoToDefinitionSplit, cx: &mut ViewContext<Self>) {
7247 self.go_to_definition_of_kind(GotoDefinitionKind::Symbol, true, cx);
7248 }
7249
7250 pub fn go_to_type_definition_split(
7251 &mut self,
7252 _: &GoToTypeDefinitionSplit,
7253 cx: &mut ViewContext<Self>,
7254 ) {
7255 self.go_to_definition_of_kind(GotoDefinitionKind::Type, true, cx);
7256 }
7257
7258 fn go_to_definition_of_kind(
7259 &mut self,
7260 kind: GotoDefinitionKind,
7261 split: bool,
7262 cx: &mut ViewContext<Self>,
7263 ) {
7264 let Some(workspace) = self.workspace() else {
7265 return;
7266 };
7267 let buffer = self.buffer.read(cx);
7268 let head = self.selections.newest::<usize>(cx).head();
7269 let (buffer, head) = if let Some(text_anchor) = buffer.text_anchor_for_position(head, cx) {
7270 text_anchor
7271 } else {
7272 return;
7273 };
7274
7275 let project = workspace.read(cx).project().clone();
7276 let definitions = project.update(cx, |project, cx| match kind {
7277 GotoDefinitionKind::Symbol => project.definition(&buffer, head, cx),
7278 GotoDefinitionKind::Type => project.type_definition(&buffer, head, cx),
7279 });
7280
7281 cx.spawn(|editor, mut cx| async move {
7282 let definitions = definitions.await?;
7283 editor.update(&mut cx, |editor, cx| {
7284 editor.navigate_to_definitions(
7285 definitions
7286 .into_iter()
7287 .map(GoToDefinitionLink::Text)
7288 .collect(),
7289 split,
7290 cx,
7291 );
7292 })?;
7293 Ok::<(), anyhow::Error>(())
7294 })
7295 .detach_and_log_err(cx);
7296 }
7297
7298 pub fn navigate_to_definitions(
7299 &mut self,
7300 mut definitions: Vec<GoToDefinitionLink>,
7301 split: bool,
7302 cx: &mut ViewContext<Editor>,
7303 ) {
7304 let Some(workspace) = self.workspace() else {
7305 return;
7306 };
7307 let pane = workspace.read(cx).active_pane().clone();
7308 // If there is one definition, just open it directly
7309 if definitions.len() == 1 {
7310 let definition = definitions.pop().unwrap();
7311 let target_task = match definition {
7312 GoToDefinitionLink::Text(link) => Task::Ready(Some(Ok(Some(link.target)))),
7313 GoToDefinitionLink::InlayHint(lsp_location, server_id) => {
7314 self.compute_target_location(lsp_location, server_id, cx)
7315 }
7316 };
7317 cx.spawn(|editor, mut cx| async move {
7318 let target = target_task.await.context("target resolution task")?;
7319 if let Some(target) = target {
7320 editor.update(&mut cx, |editor, cx| {
7321 let range = target.range.to_offset(target.buffer.read(cx));
7322 let range = editor.range_for_match(&range);
7323 if Some(&target.buffer) == editor.buffer.read(cx).as_singleton().as_ref() {
7324 editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
7325 s.select_ranges([range]);
7326 });
7327 } else {
7328 cx.window_context().defer(move |cx| {
7329 let target_editor: View<Self> =
7330 workspace.update(cx, |workspace, cx| {
7331 if split {
7332 workspace.split_project_item(target.buffer.clone(), cx)
7333 } else {
7334 workspace.open_project_item(target.buffer.clone(), cx)
7335 }
7336 });
7337 target_editor.update(cx, |target_editor, cx| {
7338 // When selecting a definition in a different buffer, disable the nav history
7339 // to avoid creating a history entry at the previous cursor location.
7340 pane.update(cx, |pane, _| pane.disable_history());
7341 target_editor.change_selections(
7342 Some(Autoscroll::fit()),
7343 cx,
7344 |s| {
7345 s.select_ranges([range]);
7346 },
7347 );
7348 pane.update(cx, |pane, _| pane.enable_history());
7349 });
7350 });
7351 }
7352 })
7353 } else {
7354 Ok(())
7355 }
7356 })
7357 .detach_and_log_err(cx);
7358 } else if !definitions.is_empty() {
7359 let replica_id = self.replica_id(cx);
7360 cx.spawn(|editor, mut cx| async move {
7361 let (title, location_tasks) = editor
7362 .update(&mut cx, |editor, cx| {
7363 let title = definitions
7364 .iter()
7365 .find_map(|definition| match definition {
7366 GoToDefinitionLink::Text(link) => {
7367 link.origin.as_ref().map(|origin| {
7368 let buffer = origin.buffer.read(cx);
7369 format!(
7370 "Definitions for {}",
7371 buffer
7372 .text_for_range(origin.range.clone())
7373 .collect::<String>()
7374 )
7375 })
7376 }
7377 GoToDefinitionLink::InlayHint(_, _) => None,
7378 })
7379 .unwrap_or("Definitions".to_string());
7380 let location_tasks = definitions
7381 .into_iter()
7382 .map(|definition| match definition {
7383 GoToDefinitionLink::Text(link) => {
7384 Task::Ready(Some(Ok(Some(link.target))))
7385 }
7386 GoToDefinitionLink::InlayHint(lsp_location, server_id) => {
7387 editor.compute_target_location(lsp_location, server_id, cx)
7388 }
7389 })
7390 .collect::<Vec<_>>();
7391 (title, location_tasks)
7392 })
7393 .context("location tasks preparation")?;
7394
7395 let locations = futures::future::join_all(location_tasks)
7396 .await
7397 .into_iter()
7398 .filter_map(|location| location.transpose())
7399 .collect::<Result<_>>()
7400 .context("location tasks")?;
7401 workspace
7402 .update(&mut cx, |workspace, cx| {
7403 Self::open_locations_in_multibuffer(
7404 workspace, locations, replica_id, title, split, cx,
7405 )
7406 })
7407 .ok();
7408
7409 anyhow::Ok(())
7410 })
7411 .detach_and_log_err(cx);
7412 }
7413 }
7414
7415 fn compute_target_location(
7416 &self,
7417 lsp_location: lsp::Location,
7418 server_id: LanguageServerId,
7419 cx: &mut ViewContext<Editor>,
7420 ) -> Task<anyhow::Result<Option<Location>>> {
7421 let Some(project) = self.project.clone() else {
7422 return Task::Ready(Some(Ok(None)));
7423 };
7424
7425 cx.spawn(move |editor, mut cx| async move {
7426 let location_task = editor.update(&mut cx, |editor, cx| {
7427 project.update(cx, |project, cx| {
7428 let language_server_name =
7429 editor.buffer.read(cx).as_singleton().and_then(|buffer| {
7430 project
7431 .language_server_for_buffer(buffer.read(cx), server_id, cx)
7432 .map(|(_, lsp_adapter)| {
7433 LanguageServerName(Arc::from(lsp_adapter.name()))
7434 })
7435 });
7436 language_server_name.map(|language_server_name| {
7437 project.open_local_buffer_via_lsp(
7438 lsp_location.uri.clone(),
7439 server_id,
7440 language_server_name,
7441 cx,
7442 )
7443 })
7444 })
7445 })?;
7446 let location = match location_task {
7447 Some(task) => Some({
7448 let target_buffer_handle = task.await.context("open local buffer")?;
7449 let range = target_buffer_handle.update(&mut cx, |target_buffer, _| {
7450 let target_start = target_buffer
7451 .clip_point_utf16(point_from_lsp(lsp_location.range.start), Bias::Left);
7452 let target_end = target_buffer
7453 .clip_point_utf16(point_from_lsp(lsp_location.range.end), Bias::Left);
7454 target_buffer.anchor_after(target_start)
7455 ..target_buffer.anchor_before(target_end)
7456 })?;
7457 Location {
7458 buffer: target_buffer_handle,
7459 range,
7460 }
7461 }),
7462 None => None,
7463 };
7464 Ok(location)
7465 })
7466 }
7467
7468 pub fn find_all_references(
7469 &mut self,
7470 _: &FindAllReferences,
7471 cx: &mut ViewContext<Self>,
7472 ) -> Option<Task<Result<()>>> {
7473 let buffer = self.buffer.read(cx);
7474 let head = self.selections.newest::<usize>(cx).head();
7475 let (buffer, head) = buffer.text_anchor_for_position(head, cx)?;
7476 let replica_id = self.replica_id(cx);
7477
7478 let workspace = self.workspace()?;
7479 let project = workspace.read(cx).project().clone();
7480 let references = project.update(cx, |project, cx| project.references(&buffer, head, cx));
7481 Some(cx.spawn(|_, mut cx| async move {
7482 let locations = references.await?;
7483 if locations.is_empty() {
7484 return Ok(());
7485 }
7486
7487 workspace.update(&mut cx, |workspace, cx| {
7488 let title = locations
7489 .first()
7490 .as_ref()
7491 .map(|location| {
7492 let buffer = location.buffer.read(cx);
7493 format!(
7494 "References to `{}`",
7495 buffer
7496 .text_for_range(location.range.clone())
7497 .collect::<String>()
7498 )
7499 })
7500 .unwrap();
7501 Self::open_locations_in_multibuffer(
7502 workspace, locations, replica_id, title, false, cx,
7503 );
7504 })?;
7505
7506 Ok(())
7507 }))
7508 }
7509
7510 /// Opens a multibuffer with the given project locations in it
7511 pub fn open_locations_in_multibuffer(
7512 workspace: &mut Workspace,
7513 mut locations: Vec<Location>,
7514 replica_id: ReplicaId,
7515 title: String,
7516 split: bool,
7517 cx: &mut ViewContext<Workspace>,
7518 ) {
7519 // If there are multiple definitions, open them in a multibuffer
7520 locations.sort_by_key(|location| location.buffer.read(cx).remote_id());
7521 let mut locations = locations.into_iter().peekable();
7522 let mut ranges_to_highlight = Vec::new();
7523 let capability = workspace.project().read(cx).capability();
7524
7525 let excerpt_buffer = cx.new_model(|cx| {
7526 let mut multibuffer = MultiBuffer::new(replica_id, capability);
7527 while let Some(location) = locations.next() {
7528 let buffer = location.buffer.read(cx);
7529 let mut ranges_for_buffer = Vec::new();
7530 let range = location.range.to_offset(buffer);
7531 ranges_for_buffer.push(range.clone());
7532
7533 while let Some(next_location) = locations.peek() {
7534 if next_location.buffer == location.buffer {
7535 ranges_for_buffer.push(next_location.range.to_offset(buffer));
7536 locations.next();
7537 } else {
7538 break;
7539 }
7540 }
7541
7542 ranges_for_buffer.sort_by_key(|range| (range.start, Reverse(range.end)));
7543 ranges_to_highlight.extend(multibuffer.push_excerpts_with_context_lines(
7544 location.buffer.clone(),
7545 ranges_for_buffer,
7546 1,
7547 cx,
7548 ))
7549 }
7550
7551 multibuffer.with_title(title)
7552 });
7553
7554 let editor = cx.new_view(|cx| {
7555 Editor::for_multibuffer(excerpt_buffer, Some(workspace.project().clone()), cx)
7556 });
7557 editor.update(cx, |editor, cx| {
7558 editor.highlight_background::<Self>(
7559 ranges_to_highlight,
7560 |theme| theme.editor_highlighted_line_background,
7561 cx,
7562 );
7563 });
7564 if split {
7565 workspace.split_item(SplitDirection::Right, Box::new(editor), cx);
7566 } else {
7567 workspace.add_item(Box::new(editor), cx);
7568 }
7569 }
7570
7571 pub fn rename(&mut self, _: &Rename, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> {
7572 use language::ToOffset as _;
7573
7574 let project = self.project.clone()?;
7575 let selection = self.selections.newest_anchor().clone();
7576 let (cursor_buffer, cursor_buffer_position) = self
7577 .buffer
7578 .read(cx)
7579 .text_anchor_for_position(selection.head(), cx)?;
7580 let (tail_buffer, _) = self
7581 .buffer
7582 .read(cx)
7583 .text_anchor_for_position(selection.tail(), cx)?;
7584 if tail_buffer != cursor_buffer {
7585 return None;
7586 }
7587
7588 let snapshot = cursor_buffer.read(cx).snapshot();
7589 let cursor_buffer_offset = cursor_buffer_position.to_offset(&snapshot);
7590 let prepare_rename = project.update(cx, |project, cx| {
7591 project.prepare_rename(cursor_buffer, cursor_buffer_offset, cx)
7592 });
7593
7594 Some(cx.spawn(|this, mut cx| async move {
7595 let rename_range = if let Some(range) = prepare_rename.await? {
7596 Some(range)
7597 } else {
7598 this.update(&mut cx, |this, cx| {
7599 let buffer = this.buffer.read(cx).snapshot(cx);
7600 let mut buffer_highlights = this
7601 .document_highlights_for_position(selection.head(), &buffer)
7602 .filter(|highlight| {
7603 highlight.start.excerpt_id == selection.head().excerpt_id
7604 && highlight.end.excerpt_id == selection.head().excerpt_id
7605 });
7606 buffer_highlights
7607 .next()
7608 .map(|highlight| highlight.start.text_anchor..highlight.end.text_anchor)
7609 })?
7610 };
7611 if let Some(rename_range) = rename_range {
7612 let rename_buffer_range = rename_range.to_offset(&snapshot);
7613 let cursor_offset_in_rename_range =
7614 cursor_buffer_offset.saturating_sub(rename_buffer_range.start);
7615
7616 this.update(&mut cx, |this, cx| {
7617 this.take_rename(false, cx);
7618 let buffer = this.buffer.read(cx).read(cx);
7619 let cursor_offset = selection.head().to_offset(&buffer);
7620 let rename_start = cursor_offset.saturating_sub(cursor_offset_in_rename_range);
7621 let rename_end = rename_start + rename_buffer_range.len();
7622 let range = buffer.anchor_before(rename_start)..buffer.anchor_after(rename_end);
7623 let mut old_highlight_id = None;
7624 let old_name: Arc<str> = buffer
7625 .chunks(rename_start..rename_end, true)
7626 .map(|chunk| {
7627 if old_highlight_id.is_none() {
7628 old_highlight_id = chunk.syntax_highlight_id;
7629 }
7630 chunk.text
7631 })
7632 .collect::<String>()
7633 .into();
7634
7635 drop(buffer);
7636
7637 // Position the selection in the rename editor so that it matches the current selection.
7638 this.show_local_selections = false;
7639 let rename_editor = cx.new_view(|cx| {
7640 let mut editor = Editor::single_line(cx);
7641 editor.buffer.update(cx, |buffer, cx| {
7642 buffer.edit([(0..0, old_name.clone())], None, cx)
7643 });
7644 editor.select_all(&SelectAll, cx);
7645 editor
7646 });
7647
7648 let ranges = this
7649 .clear_background_highlights::<DocumentHighlightWrite>(cx)
7650 .into_iter()
7651 .flat_map(|(_, ranges)| ranges.into_iter())
7652 .chain(
7653 this.clear_background_highlights::<DocumentHighlightRead>(cx)
7654 .into_iter()
7655 .flat_map(|(_, ranges)| ranges.into_iter()),
7656 )
7657 .collect();
7658
7659 this.highlight_text::<Rename>(
7660 ranges,
7661 HighlightStyle {
7662 fade_out: Some(0.6),
7663 ..Default::default()
7664 },
7665 cx,
7666 );
7667 let rename_focus_handle = rename_editor.focus_handle(cx);
7668 cx.focus(&rename_focus_handle);
7669 let block_id = this.insert_blocks(
7670 [BlockProperties {
7671 style: BlockStyle::Flex,
7672 position: range.start.clone(),
7673 height: 1,
7674 render: Arc::new({
7675 let rename_editor = rename_editor.clone();
7676 move |cx: &mut BlockContext| {
7677 let mut text_style = cx.editor_style.text.clone();
7678 if let Some(highlight_style) = old_highlight_id
7679 .and_then(|h| h.style(&cx.editor_style.syntax))
7680 {
7681 text_style = text_style.highlight(highlight_style);
7682 }
7683 div()
7684 .pl(cx.anchor_x)
7685 .child(EditorElement::new(
7686 &rename_editor,
7687 EditorStyle {
7688 background: cx.theme().system().transparent,
7689 local_player: cx.editor_style.local_player,
7690 text: text_style,
7691 scrollbar_width: cx.editor_style.scrollbar_width,
7692 syntax: cx.editor_style.syntax.clone(),
7693 status: cx.editor_style.status.clone(),
7694 inlays_style: HighlightStyle {
7695 color: Some(cx.theme().status().hint),
7696 font_weight: Some(FontWeight::BOLD),
7697 ..HighlightStyle::default()
7698 },
7699 suggestions_style: HighlightStyle {
7700 color: Some(cx.theme().status().predictive),
7701 ..HighlightStyle::default()
7702 },
7703 },
7704 ))
7705 .into_any_element()
7706 }
7707 }),
7708 disposition: BlockDisposition::Below,
7709 }],
7710 Some(Autoscroll::fit()),
7711 cx,
7712 )[0];
7713 this.pending_rename = Some(RenameState {
7714 range,
7715 old_name,
7716 editor: rename_editor,
7717 block_id,
7718 });
7719 })?;
7720 }
7721
7722 Ok(())
7723 }))
7724 }
7725
7726 pub fn confirm_rename(
7727 &mut self,
7728 _: &ConfirmRename,
7729 cx: &mut ViewContext<Self>,
7730 ) -> Option<Task<Result<()>>> {
7731 let rename = self.take_rename(false, cx)?;
7732 let workspace = self.workspace()?;
7733 let (start_buffer, start) = self
7734 .buffer
7735 .read(cx)
7736 .text_anchor_for_position(rename.range.start.clone(), cx)?;
7737 let (end_buffer, end) = self
7738 .buffer
7739 .read(cx)
7740 .text_anchor_for_position(rename.range.end.clone(), cx)?;
7741 if start_buffer != end_buffer {
7742 return None;
7743 }
7744
7745 let buffer = start_buffer;
7746 let range = start..end;
7747 let old_name = rename.old_name;
7748 let new_name = rename.editor.read(cx).text(cx);
7749
7750 let rename = workspace
7751 .read(cx)
7752 .project()
7753 .clone()
7754 .update(cx, |project, cx| {
7755 project.perform_rename(buffer.clone(), range.start, new_name.clone(), true, cx)
7756 });
7757 let workspace = workspace.downgrade();
7758
7759 Some(cx.spawn(|editor, mut cx| async move {
7760 let project_transaction = rename.await?;
7761 Self::open_project_transaction(
7762 &editor,
7763 workspace,
7764 project_transaction,
7765 format!("Rename: {} → {}", old_name, new_name),
7766 cx.clone(),
7767 )
7768 .await?;
7769
7770 editor.update(&mut cx, |editor, cx| {
7771 editor.refresh_document_highlights(cx);
7772 })?;
7773 Ok(())
7774 }))
7775 }
7776
7777 fn take_rename(
7778 &mut self,
7779 moving_cursor: bool,
7780 cx: &mut ViewContext<Self>,
7781 ) -> Option<RenameState> {
7782 let rename = self.pending_rename.take()?;
7783 if rename.editor.focus_handle(cx).is_focused(cx) {
7784 cx.focus(&self.focus_handle);
7785 }
7786
7787 self.remove_blocks(
7788 [rename.block_id].into_iter().collect(),
7789 Some(Autoscroll::fit()),
7790 cx,
7791 );
7792 self.clear_highlights::<Rename>(cx);
7793 self.show_local_selections = true;
7794
7795 if moving_cursor {
7796 let rename_editor = rename.editor.read(cx);
7797 let cursor_in_rename_editor = rename_editor.selections.newest::<usize>(cx).head();
7798
7799 // Update the selection to match the position of the selection inside
7800 // the rename editor.
7801 let snapshot = self.buffer.read(cx).read(cx);
7802 let rename_range = rename.range.to_offset(&snapshot);
7803 let cursor_in_editor = snapshot
7804 .clip_offset(rename_range.start + cursor_in_rename_editor, Bias::Left)
7805 .min(rename_range.end);
7806 drop(snapshot);
7807
7808 self.change_selections(None, cx, |s| {
7809 s.select_ranges(vec![cursor_in_editor..cursor_in_editor])
7810 });
7811 } else {
7812 self.refresh_document_highlights(cx);
7813 }
7814
7815 Some(rename)
7816 }
7817
7818 #[cfg(any(test, feature = "test-support"))]
7819 pub fn pending_rename(&self) -> Option<&RenameState> {
7820 self.pending_rename.as_ref()
7821 }
7822
7823 fn format(&mut self, _: &Format, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> {
7824 let project = match &self.project {
7825 Some(project) => project.clone(),
7826 None => return None,
7827 };
7828
7829 Some(self.perform_format(project, FormatTrigger::Manual, cx))
7830 }
7831
7832 fn perform_format(
7833 &mut self,
7834 project: Model<Project>,
7835 trigger: FormatTrigger,
7836 cx: &mut ViewContext<Self>,
7837 ) -> Task<Result<()>> {
7838 let buffer = self.buffer().clone();
7839 let buffers = buffer.read(cx).all_buffers();
7840
7841 let mut timeout = cx.background_executor().timer(FORMAT_TIMEOUT).fuse();
7842 let format = project.update(cx, |project, cx| project.format(buffers, true, trigger, cx));
7843
7844 cx.spawn(|_, mut cx| async move {
7845 let transaction = futures::select_biased! {
7846 _ = timeout => {
7847 log::warn!("timed out waiting for formatting");
7848 None
7849 }
7850 transaction = format.log_err().fuse() => transaction,
7851 };
7852
7853 buffer
7854 .update(&mut cx, |buffer, cx| {
7855 if let Some(transaction) = transaction {
7856 if !buffer.is_singleton() {
7857 buffer.push_transaction(&transaction.0, cx);
7858 }
7859 }
7860
7861 cx.notify();
7862 })
7863 .ok();
7864
7865 Ok(())
7866 })
7867 }
7868
7869 fn restart_language_server(&mut self, _: &RestartLanguageServer, cx: &mut ViewContext<Self>) {
7870 if let Some(project) = self.project.clone() {
7871 self.buffer.update(cx, |multi_buffer, cx| {
7872 project.update(cx, |project, cx| {
7873 project.restart_language_servers_for_buffers(multi_buffer.all_buffers(), cx);
7874 });
7875 })
7876 }
7877 }
7878
7879 fn show_character_palette(&mut self, _: &ShowCharacterPalette, cx: &mut ViewContext<Self>) {
7880 cx.show_character_palette();
7881 }
7882
7883 fn refresh_active_diagnostics(&mut self, cx: &mut ViewContext<Editor>) {
7884 if let Some(active_diagnostics) = self.active_diagnostics.as_mut() {
7885 let buffer = self.buffer.read(cx).snapshot(cx);
7886 let primary_range_start = active_diagnostics.primary_range.start.to_offset(&buffer);
7887 let is_valid = buffer
7888 .diagnostics_in_range::<_, usize>(active_diagnostics.primary_range.clone(), false)
7889 .any(|entry| {
7890 entry.diagnostic.is_primary
7891 && !entry.range.is_empty()
7892 && entry.range.start == primary_range_start
7893 && entry.diagnostic.message == active_diagnostics.primary_message
7894 });
7895
7896 if is_valid != active_diagnostics.is_valid {
7897 active_diagnostics.is_valid = is_valid;
7898 let mut new_styles = HashMap::default();
7899 for (block_id, diagnostic) in &active_diagnostics.blocks {
7900 new_styles.insert(
7901 *block_id,
7902 diagnostic_block_renderer(diagnostic.clone(), is_valid),
7903 );
7904 }
7905 self.display_map
7906 .update(cx, |display_map, _| display_map.replace_blocks(new_styles));
7907 }
7908 }
7909 }
7910
7911 fn activate_diagnostics(&mut self, group_id: usize, cx: &mut ViewContext<Self>) -> bool {
7912 self.dismiss_diagnostics(cx);
7913 self.active_diagnostics = self.display_map.update(cx, |display_map, cx| {
7914 let buffer = self.buffer.read(cx).snapshot(cx);
7915
7916 let mut primary_range = None;
7917 let mut primary_message = None;
7918 let mut group_end = Point::zero();
7919 let diagnostic_group = buffer
7920 .diagnostic_group::<Point>(group_id)
7921 .map(|entry| {
7922 if entry.range.end > group_end {
7923 group_end = entry.range.end;
7924 }
7925 if entry.diagnostic.is_primary {
7926 primary_range = Some(entry.range.clone());
7927 primary_message = Some(entry.diagnostic.message.clone());
7928 }
7929 entry
7930 })
7931 .collect::<Vec<_>>();
7932 let primary_range = primary_range?;
7933 let primary_message = primary_message?;
7934 let primary_range =
7935 buffer.anchor_after(primary_range.start)..buffer.anchor_before(primary_range.end);
7936
7937 let blocks = display_map
7938 .insert_blocks(
7939 diagnostic_group.iter().map(|entry| {
7940 let diagnostic = entry.diagnostic.clone();
7941 let message_height = diagnostic.message.lines().count() as u8;
7942 BlockProperties {
7943 style: BlockStyle::Fixed,
7944 position: buffer.anchor_after(entry.range.start),
7945 height: message_height,
7946 render: diagnostic_block_renderer(diagnostic, true),
7947 disposition: BlockDisposition::Below,
7948 }
7949 }),
7950 cx,
7951 )
7952 .into_iter()
7953 .zip(diagnostic_group.into_iter().map(|entry| entry.diagnostic))
7954 .collect();
7955
7956 Some(ActiveDiagnosticGroup {
7957 primary_range,
7958 primary_message,
7959 blocks,
7960 is_valid: true,
7961 })
7962 });
7963 self.active_diagnostics.is_some()
7964 }
7965
7966 fn dismiss_diagnostics(&mut self, cx: &mut ViewContext<Self>) {
7967 if let Some(active_diagnostic_group) = self.active_diagnostics.take() {
7968 self.display_map.update(cx, |display_map, cx| {
7969 display_map.remove_blocks(active_diagnostic_group.blocks.into_keys().collect(), cx);
7970 });
7971 cx.notify();
7972 }
7973 }
7974
7975 pub fn set_selections_from_remote(
7976 &mut self,
7977 selections: Vec<Selection<Anchor>>,
7978 pending_selection: Option<Selection<Anchor>>,
7979 cx: &mut ViewContext<Self>,
7980 ) {
7981 let old_cursor_position = self.selections.newest_anchor().head();
7982 self.selections.change_with(cx, |s| {
7983 s.select_anchors(selections);
7984 if let Some(pending_selection) = pending_selection {
7985 s.set_pending(pending_selection, SelectMode::Character);
7986 } else {
7987 s.clear_pending();
7988 }
7989 });
7990 self.selections_did_change(false, &old_cursor_position, cx);
7991 }
7992
7993 fn push_to_selection_history(&mut self) {
7994 self.selection_history.push(SelectionHistoryEntry {
7995 selections: self.selections.disjoint_anchors(),
7996 select_next_state: self.select_next_state.clone(),
7997 select_prev_state: self.select_prev_state.clone(),
7998 add_selections_state: self.add_selections_state.clone(),
7999 });
8000 }
8001
8002 pub fn transact(
8003 &mut self,
8004 cx: &mut ViewContext<Self>,
8005 update: impl FnOnce(&mut Self, &mut ViewContext<Self>),
8006 ) -> Option<TransactionId> {
8007 self.start_transaction_at(Instant::now(), cx);
8008 update(self, cx);
8009 self.end_transaction_at(Instant::now(), cx)
8010 }
8011
8012 fn start_transaction_at(&mut self, now: Instant, cx: &mut ViewContext<Self>) {
8013 self.end_selection(cx);
8014 if let Some(tx_id) = self
8015 .buffer
8016 .update(cx, |buffer, cx| buffer.start_transaction_at(now, cx))
8017 {
8018 self.selection_history
8019 .insert_transaction(tx_id, self.selections.disjoint_anchors());
8020 }
8021 }
8022
8023 fn end_transaction_at(
8024 &mut self,
8025 now: Instant,
8026 cx: &mut ViewContext<Self>,
8027 ) -> Option<TransactionId> {
8028 if let Some(tx_id) = self
8029 .buffer
8030 .update(cx, |buffer, cx| buffer.end_transaction_at(now, cx))
8031 {
8032 if let Some((_, end_selections)) = self.selection_history.transaction_mut(tx_id) {
8033 *end_selections = Some(self.selections.disjoint_anchors());
8034 } else {
8035 log::error!("unexpectedly ended a transaction that wasn't started by this editor");
8036 }
8037
8038 cx.emit(EditorEvent::Edited);
8039 Some(tx_id)
8040 } else {
8041 None
8042 }
8043 }
8044
8045 pub fn fold(&mut self, _: &Fold, cx: &mut ViewContext<Self>) {
8046 let mut fold_ranges = Vec::new();
8047
8048 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
8049
8050 let selections = self.selections.all_adjusted(cx);
8051 for selection in selections {
8052 let range = selection.range().sorted();
8053 let buffer_start_row = range.start.row;
8054
8055 for row in (0..=range.end.row).rev() {
8056 let fold_range = display_map.foldable_range(row);
8057
8058 if let Some(fold_range) = fold_range {
8059 if fold_range.end.row >= buffer_start_row {
8060 fold_ranges.push(fold_range);
8061 if row <= range.start.row {
8062 break;
8063 }
8064 }
8065 }
8066 }
8067 }
8068
8069 self.fold_ranges(fold_ranges, true, cx);
8070 }
8071
8072 pub fn fold_at(&mut self, fold_at: &FoldAt, cx: &mut ViewContext<Self>) {
8073 let buffer_row = fold_at.buffer_row;
8074 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
8075
8076 if let Some(fold_range) = display_map.foldable_range(buffer_row) {
8077 let autoscroll = self
8078 .selections
8079 .all::<Point>(cx)
8080 .iter()
8081 .any(|selection| fold_range.overlaps(&selection.range()));
8082
8083 self.fold_ranges(std::iter::once(fold_range), autoscroll, cx);
8084 }
8085 }
8086
8087 pub fn unfold_lines(&mut self, _: &UnfoldLines, cx: &mut ViewContext<Self>) {
8088 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
8089 let buffer = &display_map.buffer_snapshot;
8090 let selections = self.selections.all::<Point>(cx);
8091 let ranges = selections
8092 .iter()
8093 .map(|s| {
8094 let range = s.display_range(&display_map).sorted();
8095 let mut start = range.start.to_point(&display_map);
8096 let mut end = range.end.to_point(&display_map);
8097 start.column = 0;
8098 end.column = buffer.line_len(end.row);
8099 start..end
8100 })
8101 .collect::<Vec<_>>();
8102
8103 self.unfold_ranges(ranges, true, true, cx);
8104 }
8105
8106 pub fn unfold_at(&mut self, unfold_at: &UnfoldAt, cx: &mut ViewContext<Self>) {
8107 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
8108
8109 let intersection_range = Point::new(unfold_at.buffer_row, 0)
8110 ..Point::new(
8111 unfold_at.buffer_row,
8112 display_map.buffer_snapshot.line_len(unfold_at.buffer_row),
8113 );
8114
8115 let autoscroll = self
8116 .selections
8117 .all::<Point>(cx)
8118 .iter()
8119 .any(|selection| selection.range().overlaps(&intersection_range));
8120
8121 self.unfold_ranges(std::iter::once(intersection_range), true, autoscroll, cx)
8122 }
8123
8124 pub fn fold_selected_ranges(&mut self, _: &FoldSelectedRanges, cx: &mut ViewContext<Self>) {
8125 let selections = self.selections.all::<Point>(cx);
8126 let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
8127 let line_mode = self.selections.line_mode;
8128 let ranges = selections.into_iter().map(|s| {
8129 if line_mode {
8130 let start = Point::new(s.start.row, 0);
8131 let end = Point::new(s.end.row, display_map.buffer_snapshot.line_len(s.end.row));
8132 start..end
8133 } else {
8134 s.start..s.end
8135 }
8136 });
8137 self.fold_ranges(ranges, true, cx);
8138 }
8139
8140 pub fn fold_ranges<T: ToOffset + Clone>(
8141 &mut self,
8142 ranges: impl IntoIterator<Item = Range<T>>,
8143 auto_scroll: bool,
8144 cx: &mut ViewContext<Self>,
8145 ) {
8146 let mut ranges = ranges.into_iter().peekable();
8147 if ranges.peek().is_some() {
8148 self.display_map.update(cx, |map, cx| map.fold(ranges, cx));
8149
8150 if auto_scroll {
8151 self.request_autoscroll(Autoscroll::fit(), cx);
8152 }
8153
8154 cx.notify();
8155 }
8156 }
8157
8158 pub fn unfold_ranges<T: ToOffset + Clone>(
8159 &mut self,
8160 ranges: impl IntoIterator<Item = Range<T>>,
8161 inclusive: bool,
8162 auto_scroll: bool,
8163 cx: &mut ViewContext<Self>,
8164 ) {
8165 let mut ranges = ranges.into_iter().peekable();
8166 if ranges.peek().is_some() {
8167 self.display_map
8168 .update(cx, |map, cx| map.unfold(ranges, inclusive, cx));
8169 if auto_scroll {
8170 self.request_autoscroll(Autoscroll::fit(), cx);
8171 }
8172
8173 cx.notify();
8174 }
8175 }
8176
8177 pub fn set_gutter_hovered(&mut self, hovered: bool, cx: &mut ViewContext<Self>) {
8178 if hovered != self.gutter_hovered {
8179 self.gutter_hovered = hovered;
8180 cx.notify();
8181 }
8182 }
8183
8184 pub fn insert_blocks(
8185 &mut self,
8186 blocks: impl IntoIterator<Item = BlockProperties<Anchor>>,
8187 autoscroll: Option<Autoscroll>,
8188 cx: &mut ViewContext<Self>,
8189 ) -> Vec<BlockId> {
8190 let blocks = self
8191 .display_map
8192 .update(cx, |display_map, cx| display_map.insert_blocks(blocks, cx));
8193 if let Some(autoscroll) = autoscroll {
8194 self.request_autoscroll(autoscroll, cx);
8195 }
8196 blocks
8197 }
8198
8199 pub fn replace_blocks(
8200 &mut self,
8201 blocks: HashMap<BlockId, RenderBlock>,
8202 autoscroll: Option<Autoscroll>,
8203 cx: &mut ViewContext<Self>,
8204 ) {
8205 self.display_map
8206 .update(cx, |display_map, _| display_map.replace_blocks(blocks));
8207 if let Some(autoscroll) = autoscroll {
8208 self.request_autoscroll(autoscroll, cx);
8209 }
8210 }
8211
8212 pub fn remove_blocks(
8213 &mut self,
8214 block_ids: HashSet<BlockId>,
8215 autoscroll: Option<Autoscroll>,
8216 cx: &mut ViewContext<Self>,
8217 ) {
8218 self.display_map.update(cx, |display_map, cx| {
8219 display_map.remove_blocks(block_ids, cx)
8220 });
8221 if let Some(autoscroll) = autoscroll {
8222 self.request_autoscroll(autoscroll, cx);
8223 }
8224 }
8225
8226 pub fn longest_row(&self, cx: &mut AppContext) -> u32 {
8227 self.display_map
8228 .update(cx, |map, cx| map.snapshot(cx))
8229 .longest_row()
8230 }
8231
8232 pub fn max_point(&self, cx: &mut AppContext) -> DisplayPoint {
8233 self.display_map
8234 .update(cx, |map, cx| map.snapshot(cx))
8235 .max_point()
8236 }
8237
8238 pub fn text(&self, cx: &AppContext) -> String {
8239 self.buffer.read(cx).read(cx).text()
8240 }
8241
8242 pub fn text_option(&self, cx: &AppContext) -> Option<String> {
8243 let text = self.text(cx);
8244 let text = text.trim();
8245
8246 if text.is_empty() {
8247 return None;
8248 }
8249
8250 Some(text.to_string())
8251 }
8252
8253 pub fn set_text(&mut self, text: impl Into<Arc<str>>, cx: &mut ViewContext<Self>) {
8254 self.transact(cx, |this, cx| {
8255 this.buffer
8256 .read(cx)
8257 .as_singleton()
8258 .expect("you can only call set_text on editors for singleton buffers")
8259 .update(cx, |buffer, cx| buffer.set_text(text, cx));
8260 });
8261 }
8262
8263 pub fn display_text(&self, cx: &mut AppContext) -> String {
8264 self.display_map
8265 .update(cx, |map, cx| map.snapshot(cx))
8266 .text()
8267 }
8268
8269 pub fn wrap_guides(&self, cx: &AppContext) -> SmallVec<[(usize, bool); 2]> {
8270 let mut wrap_guides = smallvec::smallvec![];
8271
8272 if self.show_wrap_guides == Some(false) {
8273 return wrap_guides;
8274 }
8275
8276 let settings = self.buffer.read(cx).settings_at(0, cx);
8277 if settings.show_wrap_guides {
8278 if let SoftWrap::Column(soft_wrap) = self.soft_wrap_mode(cx) {
8279 wrap_guides.push((soft_wrap as usize, true));
8280 }
8281 wrap_guides.extend(settings.wrap_guides.iter().map(|guide| (*guide, false)))
8282 }
8283
8284 wrap_guides
8285 }
8286
8287 pub fn soft_wrap_mode(&self, cx: &AppContext) -> SoftWrap {
8288 let settings = self.buffer.read(cx).settings_at(0, cx);
8289 let mode = self
8290 .soft_wrap_mode_override
8291 .unwrap_or_else(|| settings.soft_wrap);
8292 match mode {
8293 language_settings::SoftWrap::None => SoftWrap::None,
8294 language_settings::SoftWrap::EditorWidth => SoftWrap::EditorWidth,
8295 language_settings::SoftWrap::PreferredLineLength => {
8296 SoftWrap::Column(settings.preferred_line_length)
8297 }
8298 }
8299 }
8300
8301 pub fn set_soft_wrap_mode(
8302 &mut self,
8303 mode: language_settings::SoftWrap,
8304 cx: &mut ViewContext<Self>,
8305 ) {
8306 self.soft_wrap_mode_override = Some(mode);
8307 cx.notify();
8308 }
8309
8310 pub fn set_style(&mut self, style: EditorStyle, cx: &mut ViewContext<Self>) {
8311 let rem_size = cx.rem_size();
8312 self.display_map.update(cx, |map, cx| {
8313 map.set_font(
8314 style.text.font(),
8315 style.text.font_size.to_pixels(rem_size),
8316 cx,
8317 )
8318 });
8319 self.style = Some(style);
8320 }
8321
8322 #[cfg(any(test, feature = "test-support"))]
8323 pub fn style(&self) -> Option<&EditorStyle> {
8324 self.style.as_ref()
8325 }
8326
8327 // Called by the element. This method is not designed to be called outside of the editor
8328 // element's layout code because it does not notify when rewrapping is computed synchronously.
8329 pub(crate) fn set_wrap_width(&self, width: Option<Pixels>, cx: &mut AppContext) -> bool {
8330 self.display_map
8331 .update(cx, |map, cx| map.set_wrap_width(width, cx))
8332 }
8333
8334 pub fn toggle_soft_wrap(&mut self, _: &ToggleSoftWrap, cx: &mut ViewContext<Self>) {
8335 if self.soft_wrap_mode_override.is_some() {
8336 self.soft_wrap_mode_override.take();
8337 } else {
8338 let soft_wrap = match self.soft_wrap_mode(cx) {
8339 SoftWrap::None => language_settings::SoftWrap::EditorWidth,
8340 SoftWrap::EditorWidth | SoftWrap::Column(_) => language_settings::SoftWrap::None,
8341 };
8342 self.soft_wrap_mode_override = Some(soft_wrap);
8343 }
8344 cx.notify();
8345 }
8346
8347 pub fn set_show_gutter(&mut self, show_gutter: bool, cx: &mut ViewContext<Self>) {
8348 self.show_gutter = show_gutter;
8349 cx.notify();
8350 }
8351
8352 pub fn set_show_wrap_guides(&mut self, show_gutter: bool, cx: &mut ViewContext<Self>) {
8353 self.show_wrap_guides = Some(show_gutter);
8354 cx.notify();
8355 }
8356
8357 pub fn reveal_in_finder(&mut self, _: &RevealInFinder, cx: &mut ViewContext<Self>) {
8358 if let Some(buffer) = self.buffer().read(cx).as_singleton() {
8359 if let Some(file) = buffer.read(cx).file().and_then(|f| f.as_local()) {
8360 cx.reveal_path(&file.abs_path(cx));
8361 }
8362 }
8363 }
8364
8365 pub fn copy_path(&mut self, _: &CopyPath, cx: &mut ViewContext<Self>) {
8366 if let Some(buffer) = self.buffer().read(cx).as_singleton() {
8367 if let Some(file) = buffer.read(cx).file().and_then(|f| f.as_local()) {
8368 if let Some(path) = file.abs_path(cx).to_str() {
8369 cx.write_to_clipboard(ClipboardItem::new(path.to_string()));
8370 }
8371 }
8372 }
8373 }
8374
8375 pub fn copy_relative_path(&mut self, _: &CopyRelativePath, cx: &mut ViewContext<Self>) {
8376 if let Some(buffer) = self.buffer().read(cx).as_singleton() {
8377 if let Some(file) = buffer.read(cx).file().and_then(|f| f.as_local()) {
8378 if let Some(path) = file.path().to_str() {
8379 cx.write_to_clipboard(ClipboardItem::new(path.to_string()));
8380 }
8381 }
8382 }
8383 }
8384
8385 pub fn highlight_rows(&mut self, rows: Option<Range<u32>>) {
8386 self.highlighted_rows = rows;
8387 }
8388
8389 pub fn highlighted_rows(&self) -> Option<Range<u32>> {
8390 self.highlighted_rows.clone()
8391 }
8392
8393 pub fn highlight_background<T: 'static>(
8394 &mut self,
8395 ranges: Vec<Range<Anchor>>,
8396 color_fetcher: fn(&ThemeColors) -> Hsla,
8397 cx: &mut ViewContext<Self>,
8398 ) {
8399 self.background_highlights
8400 .insert(TypeId::of::<T>(), (color_fetcher, ranges));
8401 cx.notify();
8402 }
8403
8404 pub fn highlight_inlay_background<T: 'static>(
8405 &mut self,
8406 ranges: Vec<InlayHighlight>,
8407 color_fetcher: fn(&ThemeColors) -> Hsla,
8408 cx: &mut ViewContext<Self>,
8409 ) {
8410 // TODO: no actual highlights happen for inlays currently, find a way to do that
8411 self.inlay_background_highlights
8412 .insert(Some(TypeId::of::<T>()), (color_fetcher, ranges));
8413 cx.notify();
8414 }
8415
8416 pub fn clear_background_highlights<T: 'static>(
8417 &mut self,
8418 cx: &mut ViewContext<Self>,
8419 ) -> Option<BackgroundHighlight> {
8420 let text_highlights = self.background_highlights.remove(&TypeId::of::<T>());
8421 let inlay_highlights = self
8422 .inlay_background_highlights
8423 .remove(&Some(TypeId::of::<T>()));
8424 if text_highlights.is_some() || inlay_highlights.is_some() {
8425 cx.notify();
8426 }
8427 text_highlights
8428 }
8429
8430 #[cfg(feature = "test-support")]
8431 pub fn all_text_background_highlights(
8432 &mut self,
8433 cx: &mut ViewContext<Self>,
8434 ) -> Vec<(Range<DisplayPoint>, Hsla)> {
8435 let snapshot = self.snapshot(cx);
8436 let buffer = &snapshot.buffer_snapshot;
8437 let start = buffer.anchor_before(0);
8438 let end = buffer.anchor_after(buffer.len());
8439 let theme = cx.theme().colors();
8440 self.background_highlights_in_range(start..end, &snapshot, theme)
8441 }
8442
8443 fn document_highlights_for_position<'a>(
8444 &'a self,
8445 position: Anchor,
8446 buffer: &'a MultiBufferSnapshot,
8447 ) -> impl 'a + Iterator<Item = &Range<Anchor>> {
8448 let read_highlights = self
8449 .background_highlights
8450 .get(&TypeId::of::<DocumentHighlightRead>())
8451 .map(|h| &h.1);
8452 let write_highlights = self
8453 .background_highlights
8454 .get(&TypeId::of::<DocumentHighlightWrite>())
8455 .map(|h| &h.1);
8456 let left_position = position.bias_left(buffer);
8457 let right_position = position.bias_right(buffer);
8458 read_highlights
8459 .into_iter()
8460 .chain(write_highlights)
8461 .flat_map(move |ranges| {
8462 let start_ix = match ranges.binary_search_by(|probe| {
8463 let cmp = probe.end.cmp(&left_position, buffer);
8464 if cmp.is_ge() {
8465 Ordering::Greater
8466 } else {
8467 Ordering::Less
8468 }
8469 }) {
8470 Ok(i) | Err(i) => i,
8471 };
8472
8473 let right_position = right_position.clone();
8474 ranges[start_ix..]
8475 .iter()
8476 .take_while(move |range| range.start.cmp(&right_position, buffer).is_le())
8477 })
8478 }
8479
8480 pub fn has_background_highlights<T: 'static>(&self) -> bool {
8481 self.background_highlights
8482 .get(&TypeId::of::<T>())
8483 .map_or(false, |(_, highlights)| !highlights.is_empty())
8484 }
8485
8486 pub fn background_highlights_in_range(
8487 &self,
8488 search_range: Range<Anchor>,
8489 display_snapshot: &DisplaySnapshot,
8490 theme: &ThemeColors,
8491 ) -> Vec<(Range<DisplayPoint>, Hsla)> {
8492 let mut results = Vec::new();
8493 for (color_fetcher, ranges) in self.background_highlights.values() {
8494 let color = color_fetcher(theme);
8495 let start_ix = match ranges.binary_search_by(|probe| {
8496 let cmp = probe
8497 .end
8498 .cmp(&search_range.start, &display_snapshot.buffer_snapshot);
8499 if cmp.is_gt() {
8500 Ordering::Greater
8501 } else {
8502 Ordering::Less
8503 }
8504 }) {
8505 Ok(i) | Err(i) => i,
8506 };
8507 for range in &ranges[start_ix..] {
8508 if range
8509 .start
8510 .cmp(&search_range.end, &display_snapshot.buffer_snapshot)
8511 .is_ge()
8512 {
8513 break;
8514 }
8515
8516 let start = range.start.to_display_point(&display_snapshot);
8517 let end = range.end.to_display_point(&display_snapshot);
8518 results.push((start..end, color))
8519 }
8520 }
8521 results
8522 }
8523
8524 pub fn background_highlight_row_ranges<T: 'static>(
8525 &self,
8526 search_range: Range<Anchor>,
8527 display_snapshot: &DisplaySnapshot,
8528 count: usize,
8529 ) -> Vec<RangeInclusive<DisplayPoint>> {
8530 let mut results = Vec::new();
8531 let Some((_, ranges)) = self.background_highlights.get(&TypeId::of::<T>()) else {
8532 return vec![];
8533 };
8534
8535 let start_ix = match ranges.binary_search_by(|probe| {
8536 let cmp = probe
8537 .end
8538 .cmp(&search_range.start, &display_snapshot.buffer_snapshot);
8539 if cmp.is_gt() {
8540 Ordering::Greater
8541 } else {
8542 Ordering::Less
8543 }
8544 }) {
8545 Ok(i) | Err(i) => i,
8546 };
8547 let mut push_region = |start: Option<Point>, end: Option<Point>| {
8548 if let (Some(start_display), Some(end_display)) = (start, end) {
8549 results.push(
8550 start_display.to_display_point(display_snapshot)
8551 ..=end_display.to_display_point(display_snapshot),
8552 );
8553 }
8554 };
8555 let mut start_row: Option<Point> = None;
8556 let mut end_row: Option<Point> = None;
8557 if ranges.len() > count {
8558 return Vec::new();
8559 }
8560 for range in &ranges[start_ix..] {
8561 if range
8562 .start
8563 .cmp(&search_range.end, &display_snapshot.buffer_snapshot)
8564 .is_ge()
8565 {
8566 break;
8567 }
8568 let end = range.end.to_point(&display_snapshot.buffer_snapshot);
8569 if let Some(current_row) = &end_row {
8570 if end.row == current_row.row {
8571 continue;
8572 }
8573 }
8574 let start = range.start.to_point(&display_snapshot.buffer_snapshot);
8575 if start_row.is_none() {
8576 assert_eq!(end_row, None);
8577 start_row = Some(start);
8578 end_row = Some(end);
8579 continue;
8580 }
8581 if let Some(current_end) = end_row.as_mut() {
8582 if start.row > current_end.row + 1 {
8583 push_region(start_row, end_row);
8584 start_row = Some(start);
8585 end_row = Some(end);
8586 } else {
8587 // Merge two hunks.
8588 *current_end = end;
8589 }
8590 } else {
8591 unreachable!();
8592 }
8593 }
8594 // We might still have a hunk that was not rendered (if there was a search hit on the last line)
8595 push_region(start_row, end_row);
8596 results
8597 }
8598
8599 pub fn highlight_text<T: 'static>(
8600 &mut self,
8601 ranges: Vec<Range<Anchor>>,
8602 style: HighlightStyle,
8603 cx: &mut ViewContext<Self>,
8604 ) {
8605 self.display_map.update(cx, |map, _| {
8606 map.highlight_text(TypeId::of::<T>(), ranges, style)
8607 });
8608 cx.notify();
8609 }
8610
8611 pub fn highlight_inlays<T: 'static>(
8612 &mut self,
8613 highlights: Vec<InlayHighlight>,
8614 style: HighlightStyle,
8615 cx: &mut ViewContext<Self>,
8616 ) {
8617 self.display_map.update(cx, |map, _| {
8618 map.highlight_inlays(TypeId::of::<T>(), highlights, style)
8619 });
8620 cx.notify();
8621 }
8622
8623 pub fn text_highlights<'a, T: 'static>(
8624 &'a self,
8625 cx: &'a AppContext,
8626 ) -> Option<(HighlightStyle, &'a [Range<Anchor>])> {
8627 self.display_map.read(cx).text_highlights(TypeId::of::<T>())
8628 }
8629
8630 pub fn clear_highlights<T: 'static>(&mut self, cx: &mut ViewContext<Self>) {
8631 let cleared = self
8632 .display_map
8633 .update(cx, |map, _| map.clear_highlights(TypeId::of::<T>()));
8634 if cleared {
8635 cx.notify();
8636 }
8637 }
8638
8639 pub fn show_local_cursors(&self, cx: &WindowContext) -> bool {
8640 (self.read_only(cx) || self.blink_manager.read(cx).visible())
8641 && self.focus_handle.is_focused(cx)
8642 }
8643
8644 fn on_buffer_changed(&mut self, _: Model<MultiBuffer>, cx: &mut ViewContext<Self>) {
8645 cx.notify();
8646 }
8647
8648 fn on_buffer_event(
8649 &mut self,
8650 multibuffer: Model<MultiBuffer>,
8651 event: &multi_buffer::Event,
8652 cx: &mut ViewContext<Self>,
8653 ) {
8654 match event {
8655 multi_buffer::Event::Edited {
8656 sigleton_buffer_edited,
8657 } => {
8658 self.refresh_active_diagnostics(cx);
8659 self.refresh_code_actions(cx);
8660 if self.has_active_copilot_suggestion(cx) {
8661 self.update_visible_copilot_suggestion(cx);
8662 }
8663 cx.emit(EditorEvent::BufferEdited);
8664 cx.emit(SearchEvent::MatchesInvalidated);
8665
8666 if *sigleton_buffer_edited {
8667 if let Some(project) = &self.project {
8668 let project = project.read(cx);
8669 let languages_affected = multibuffer
8670 .read(cx)
8671 .all_buffers()
8672 .into_iter()
8673 .filter_map(|buffer| {
8674 let buffer = buffer.read(cx);
8675 let language = buffer.language()?;
8676 if project.is_local()
8677 && project.language_servers_for_buffer(buffer, cx).count() == 0
8678 {
8679 None
8680 } else {
8681 Some(language)
8682 }
8683 })
8684 .cloned()
8685 .collect::<HashSet<_>>();
8686 if !languages_affected.is_empty() {
8687 self.refresh_inlay_hints(
8688 InlayHintRefreshReason::BufferEdited(languages_affected),
8689 cx,
8690 );
8691 }
8692 }
8693 }
8694
8695 let Some(project) = &self.project else { return };
8696 let telemetry = project.read(cx).client().telemetry().clone();
8697 telemetry.log_edit_event("editor");
8698 }
8699 multi_buffer::Event::ExcerptsAdded {
8700 buffer,
8701 predecessor,
8702 excerpts,
8703 } => {
8704 cx.emit(EditorEvent::ExcerptsAdded {
8705 buffer: buffer.clone(),
8706 predecessor: *predecessor,
8707 excerpts: excerpts.clone(),
8708 });
8709 self.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx);
8710 }
8711 multi_buffer::Event::ExcerptsRemoved { ids } => {
8712 self.refresh_inlay_hints(InlayHintRefreshReason::ExcerptsRemoved(ids.clone()), cx);
8713 cx.emit(EditorEvent::ExcerptsRemoved { ids: ids.clone() })
8714 }
8715 multi_buffer::Event::Reparsed => cx.emit(EditorEvent::Reparsed),
8716 multi_buffer::Event::DirtyChanged => cx.emit(EditorEvent::DirtyChanged),
8717 multi_buffer::Event::Saved => cx.emit(EditorEvent::Saved),
8718 multi_buffer::Event::FileHandleChanged | multi_buffer::Event::Reloaded => {
8719 cx.emit(EditorEvent::TitleChanged)
8720 }
8721 multi_buffer::Event::DiffBaseChanged => cx.emit(EditorEvent::DiffBaseChanged),
8722 multi_buffer::Event::Closed => cx.emit(EditorEvent::Closed),
8723 multi_buffer::Event::DiagnosticsUpdated => {
8724 self.refresh_active_diagnostics(cx);
8725 }
8726 _ => {}
8727 };
8728 }
8729
8730 fn on_display_map_changed(&mut self, _: Model<DisplayMap>, cx: &mut ViewContext<Self>) {
8731 cx.notify();
8732 }
8733
8734 fn settings_changed(&mut self, cx: &mut ViewContext<Self>) {
8735 self.refresh_copilot_suggestions(true, cx);
8736 self.refresh_inlay_hints(
8737 InlayHintRefreshReason::SettingsChange(inlay_hint_settings(
8738 self.selections.newest_anchor().head(),
8739 &self.buffer.read(cx).snapshot(cx),
8740 cx,
8741 )),
8742 cx,
8743 );
8744 }
8745
8746 pub fn set_searchable(&mut self, searchable: bool) {
8747 self.searchable = searchable;
8748 }
8749
8750 pub fn searchable(&self) -> bool {
8751 self.searchable
8752 }
8753
8754 fn open_excerpts(&mut self, _: &OpenExcerpts, cx: &mut ViewContext<Self>) {
8755 let buffer = self.buffer.read(cx);
8756 if buffer.is_singleton() {
8757 cx.propagate();
8758 return;
8759 }
8760
8761 let Some(workspace) = self.workspace() else {
8762 cx.propagate();
8763 return;
8764 };
8765
8766 let mut new_selections_by_buffer = HashMap::default();
8767 for selection in self.selections.all::<usize>(cx) {
8768 for (buffer, mut range, _) in
8769 buffer.range_to_buffer_ranges(selection.start..selection.end, cx)
8770 {
8771 if selection.reversed {
8772 mem::swap(&mut range.start, &mut range.end);
8773 }
8774 new_selections_by_buffer
8775 .entry(buffer)
8776 .or_insert(Vec::new())
8777 .push(range)
8778 }
8779 }
8780
8781 self.push_to_nav_history(self.selections.newest_anchor().head(), None, cx);
8782
8783 // We defer the pane interaction because we ourselves are a workspace item
8784 // and activating a new item causes the pane to call a method on us reentrantly,
8785 // which panics if we're on the stack.
8786 cx.window_context().defer(move |cx| {
8787 workspace.update(cx, |workspace, cx| {
8788 let pane = workspace.active_pane().clone();
8789 pane.update(cx, |pane, _| pane.disable_history());
8790
8791 for (buffer, ranges) in new_selections_by_buffer.into_iter() {
8792 let editor = workspace.open_project_item::<Self>(buffer, cx);
8793 editor.update(cx, |editor, cx| {
8794 editor.change_selections(Some(Autoscroll::newest()), cx, |s| {
8795 s.select_ranges(ranges);
8796 });
8797 });
8798 }
8799
8800 pane.update(cx, |pane, _| pane.enable_history());
8801 })
8802 });
8803 }
8804
8805 fn jump(
8806 &mut self,
8807 path: ProjectPath,
8808 position: Point,
8809 anchor: language::Anchor,
8810 cx: &mut ViewContext<Self>,
8811 ) {
8812 let workspace = self.workspace();
8813 cx.spawn(|_, mut cx| async move {
8814 let workspace = workspace.ok_or_else(|| anyhow!("cannot jump without workspace"))?;
8815 let editor = workspace.update(&mut cx, |workspace, cx| {
8816 workspace.open_path(path, None, true, cx)
8817 })?;
8818 let editor = editor
8819 .await?
8820 .downcast::<Editor>()
8821 .ok_or_else(|| anyhow!("opened item was not an editor"))?
8822 .downgrade();
8823 editor.update(&mut cx, |editor, cx| {
8824 let buffer = editor
8825 .buffer()
8826 .read(cx)
8827 .as_singleton()
8828 .ok_or_else(|| anyhow!("cannot jump in a multi-buffer"))?;
8829 let buffer = buffer.read(cx);
8830 let cursor = if buffer.can_resolve(&anchor) {
8831 language::ToPoint::to_point(&anchor, buffer)
8832 } else {
8833 buffer.clip_point(position, Bias::Left)
8834 };
8835
8836 let nav_history = editor.nav_history.take();
8837 editor.change_selections(Some(Autoscroll::newest()), cx, |s| {
8838 s.select_ranges([cursor..cursor]);
8839 });
8840 editor.nav_history = nav_history;
8841
8842 anyhow::Ok(())
8843 })??;
8844
8845 anyhow::Ok(())
8846 })
8847 .detach_and_log_err(cx);
8848 }
8849
8850 fn marked_text_ranges(&self, cx: &AppContext) -> Option<Vec<Range<OffsetUtf16>>> {
8851 let snapshot = self.buffer.read(cx).read(cx);
8852 let (_, ranges) = self.text_highlights::<InputComposition>(cx)?;
8853 Some(
8854 ranges
8855 .iter()
8856 .map(move |range| {
8857 range.start.to_offset_utf16(&snapshot)..range.end.to_offset_utf16(&snapshot)
8858 })
8859 .collect(),
8860 )
8861 }
8862
8863 fn selection_replacement_ranges(
8864 &self,
8865 range: Range<OffsetUtf16>,
8866 cx: &AppContext,
8867 ) -> Vec<Range<OffsetUtf16>> {
8868 let selections = self.selections.all::<OffsetUtf16>(cx);
8869 let newest_selection = selections
8870 .iter()
8871 .max_by_key(|selection| selection.id)
8872 .unwrap();
8873 let start_delta = range.start.0 as isize - newest_selection.start.0 as isize;
8874 let end_delta = range.end.0 as isize - newest_selection.end.0 as isize;
8875 let snapshot = self.buffer.read(cx).read(cx);
8876 selections
8877 .into_iter()
8878 .map(|mut selection| {
8879 selection.start.0 =
8880 (selection.start.0 as isize).saturating_add(start_delta) as usize;
8881 selection.end.0 = (selection.end.0 as isize).saturating_add(end_delta) as usize;
8882 snapshot.clip_offset_utf16(selection.start, Bias::Left)
8883 ..snapshot.clip_offset_utf16(selection.end, Bias::Right)
8884 })
8885 .collect()
8886 }
8887
8888 fn report_copilot_event(
8889 &self,
8890 suggestion_id: Option<String>,
8891 suggestion_accepted: bool,
8892 cx: &AppContext,
8893 ) {
8894 let Some(project) = &self.project else { return };
8895
8896 // If None, we are either getting suggestions in a new, unsaved file, or in a file without an extension
8897 let file_extension = self
8898 .buffer
8899 .read(cx)
8900 .as_singleton()
8901 .and_then(|b| b.read(cx).file())
8902 .and_then(|file| Path::new(file.file_name(cx)).extension())
8903 .and_then(|e| e.to_str())
8904 .map(|a| a.to_string());
8905
8906 let telemetry = project.read(cx).client().telemetry().clone();
8907
8908 telemetry.report_copilot_event(suggestion_id, suggestion_accepted, file_extension)
8909 }
8910
8911 #[cfg(any(test, feature = "test-support"))]
8912 fn report_editor_event(
8913 &self,
8914 _operation: &'static str,
8915 _file_extension: Option<String>,
8916 _cx: &AppContext,
8917 ) {
8918 }
8919
8920 #[cfg(not(any(test, feature = "test-support")))]
8921 fn report_editor_event(
8922 &self,
8923 operation: &'static str,
8924 file_extension: Option<String>,
8925 cx: &AppContext,
8926 ) {
8927 let Some(project) = &self.project else { return };
8928
8929 // If None, we are in a file without an extension
8930 let file = self
8931 .buffer
8932 .read(cx)
8933 .as_singleton()
8934 .and_then(|b| b.read(cx).file());
8935 let file_extension = file_extension.or(file
8936 .as_ref()
8937 .and_then(|file| Path::new(file.file_name(cx)).extension())
8938 .and_then(|e| e.to_str())
8939 .map(|a| a.to_string()));
8940
8941 let vim_mode = cx
8942 .global::<SettingsStore>()
8943 .raw_user_settings()
8944 .get("vim_mode")
8945 == Some(&serde_json::Value::Bool(true));
8946 let copilot_enabled = all_language_settings(file, cx).copilot_enabled(None, None);
8947 let copilot_enabled_for_language = self
8948 .buffer
8949 .read(cx)
8950 .settings_at(0, cx)
8951 .show_copilot_suggestions;
8952
8953 let telemetry = project.read(cx).client().telemetry().clone();
8954 telemetry.report_editor_event(
8955 file_extension,
8956 vim_mode,
8957 operation,
8958 copilot_enabled,
8959 copilot_enabled_for_language,
8960 )
8961 }
8962
8963 /// Copy the highlighted chunks to the clipboard as JSON. The format is an array of lines,
8964 /// with each line being an array of {text, highlight} objects.
8965 fn copy_highlight_json(&mut self, _: &CopyHighlightJson, cx: &mut ViewContext<Self>) {
8966 let Some(buffer) = self.buffer.read(cx).as_singleton() else {
8967 return;
8968 };
8969
8970 #[derive(Serialize)]
8971 struct Chunk<'a> {
8972 text: String,
8973 highlight: Option<&'a str>,
8974 }
8975
8976 let snapshot = buffer.read(cx).snapshot();
8977 let range = self
8978 .selected_text_range(cx)
8979 .and_then(|selected_range| {
8980 if selected_range.is_empty() {
8981 None
8982 } else {
8983 Some(selected_range)
8984 }
8985 })
8986 .unwrap_or_else(|| 0..snapshot.len());
8987
8988 let chunks = snapshot.chunks(range, true);
8989 let mut lines = Vec::new();
8990 let mut line: VecDeque<Chunk> = VecDeque::new();
8991
8992 let Some(style) = self.style.as_ref() else {
8993 return;
8994 };
8995
8996 for chunk in chunks {
8997 let highlight = chunk
8998 .syntax_highlight_id
8999 .and_then(|id| id.name(&style.syntax));
9000 let mut chunk_lines = chunk.text.split("\n").peekable();
9001 while let Some(text) = chunk_lines.next() {
9002 let mut merged_with_last_token = false;
9003 if let Some(last_token) = line.back_mut() {
9004 if last_token.highlight == highlight {
9005 last_token.text.push_str(text);
9006 merged_with_last_token = true;
9007 }
9008 }
9009
9010 if !merged_with_last_token {
9011 line.push_back(Chunk {
9012 text: text.into(),
9013 highlight,
9014 });
9015 }
9016
9017 if chunk_lines.peek().is_some() {
9018 if line.len() > 1 && line.front().unwrap().text.is_empty() {
9019 line.pop_front();
9020 }
9021 if line.len() > 1 && line.back().unwrap().text.is_empty() {
9022 line.pop_back();
9023 }
9024
9025 lines.push(mem::take(&mut line));
9026 }
9027 }
9028 }
9029
9030 let Some(lines) = serde_json::to_string_pretty(&lines).log_err() else {
9031 return;
9032 };
9033 cx.write_to_clipboard(ClipboardItem::new(lines));
9034 }
9035
9036 pub fn inlay_hint_cache(&self) -> &InlayHintCache {
9037 &self.inlay_hint_cache
9038 }
9039
9040 pub fn replay_insert_event(
9041 &mut self,
9042 text: &str,
9043 relative_utf16_range: Option<Range<isize>>,
9044 cx: &mut ViewContext<Self>,
9045 ) {
9046 if !self.input_enabled {
9047 cx.emit(EditorEvent::InputIgnored { text: text.into() });
9048 return;
9049 }
9050 if let Some(relative_utf16_range) = relative_utf16_range {
9051 let selections = self.selections.all::<OffsetUtf16>(cx);
9052 self.change_selections(None, cx, |s| {
9053 let new_ranges = selections.into_iter().map(|range| {
9054 let start = OffsetUtf16(
9055 range
9056 .head()
9057 .0
9058 .saturating_add_signed(relative_utf16_range.start),
9059 );
9060 let end = OffsetUtf16(
9061 range
9062 .head()
9063 .0
9064 .saturating_add_signed(relative_utf16_range.end),
9065 );
9066 start..end
9067 });
9068 s.select_ranges(new_ranges);
9069 });
9070 }
9071
9072 self.handle_input(text, cx);
9073 }
9074
9075 pub fn supports_inlay_hints(&self, cx: &AppContext) -> bool {
9076 let Some(project) = self.project.as_ref() else {
9077 return false;
9078 };
9079 let project = project.read(cx);
9080
9081 let mut supports = false;
9082 self.buffer().read(cx).for_each_buffer(|buffer| {
9083 if !supports {
9084 supports = project
9085 .language_servers_for_buffer(buffer.read(cx), cx)
9086 .any(
9087 |(_, server)| match server.capabilities().inlay_hint_provider {
9088 Some(lsp::OneOf::Left(enabled)) => enabled,
9089 Some(lsp::OneOf::Right(_)) => true,
9090 None => false,
9091 },
9092 )
9093 }
9094 });
9095 supports
9096 }
9097
9098 pub fn focus(&self, cx: &mut WindowContext) {
9099 cx.focus(&self.focus_handle)
9100 }
9101
9102 pub fn is_focused(&self, cx: &WindowContext) -> bool {
9103 self.focus_handle.is_focused(cx)
9104 }
9105
9106 fn handle_focus(&mut self, cx: &mut ViewContext<Self>) {
9107 cx.emit(EditorEvent::Focused);
9108
9109 if let Some(rename) = self.pending_rename.as_ref() {
9110 let rename_editor_focus_handle = rename.editor.read(cx).focus_handle.clone();
9111 cx.focus(&rename_editor_focus_handle);
9112 } else {
9113 self.blink_manager.update(cx, BlinkManager::enable);
9114 self.buffer.update(cx, |buffer, cx| {
9115 buffer.finalize_last_transaction(cx);
9116 if self.leader_peer_id.is_none() {
9117 buffer.set_active_selections(
9118 &self.selections.disjoint_anchors(),
9119 self.selections.line_mode,
9120 self.cursor_shape,
9121 cx,
9122 );
9123 }
9124 });
9125 }
9126 }
9127
9128 pub fn handle_blur(&mut self, cx: &mut ViewContext<Self>) {
9129 self.blink_manager.update(cx, BlinkManager::disable);
9130 self.buffer
9131 .update(cx, |buffer, cx| buffer.remove_active_selections(cx));
9132 self.hide_context_menu(cx);
9133 hide_hover(self, cx);
9134 cx.emit(EditorEvent::Blurred);
9135 cx.notify();
9136 }
9137
9138 pub fn register_action<A: Action>(
9139 &mut self,
9140 listener: impl Fn(&A, &mut WindowContext) + 'static,
9141 ) -> &mut Self {
9142 let listener = Arc::new(listener);
9143
9144 self.editor_actions.push(Box::new(move |cx| {
9145 let _view = cx.view().clone();
9146 let cx = cx.window_context();
9147 let listener = listener.clone();
9148 cx.on_action(TypeId::of::<A>(), move |action, phase, cx| {
9149 let action = action.downcast_ref().unwrap();
9150 if phase == DispatchPhase::Bubble {
9151 listener(action, cx)
9152 }
9153 })
9154 }));
9155 self
9156 }
9157}
9158
9159pub trait CollaborationHub {
9160 fn collaborators<'a>(&self, cx: &'a AppContext) -> &'a HashMap<PeerId, Collaborator>;
9161 fn user_participant_indices<'a>(
9162 &self,
9163 cx: &'a AppContext,
9164 ) -> &'a HashMap<u64, ParticipantIndex>;
9165}
9166
9167impl CollaborationHub for Model<Project> {
9168 fn collaborators<'a>(&self, cx: &'a AppContext) -> &'a HashMap<PeerId, Collaborator> {
9169 self.read(cx).collaborators()
9170 }
9171
9172 fn user_participant_indices<'a>(
9173 &self,
9174 cx: &'a AppContext,
9175 ) -> &'a HashMap<u64, ParticipantIndex> {
9176 self.read(cx).user_store().read(cx).participant_indices()
9177 }
9178}
9179
9180fn inlay_hint_settings(
9181 location: Anchor,
9182 snapshot: &MultiBufferSnapshot,
9183 cx: &mut ViewContext<'_, Editor>,
9184) -> InlayHintSettings {
9185 let file = snapshot.file_at(location);
9186 let language = snapshot.language_at(location);
9187 let settings = all_language_settings(file, cx);
9188 settings
9189 .language(language.map(|l| l.name()).as_deref())
9190 .inlay_hints
9191}
9192
9193fn consume_contiguous_rows(
9194 contiguous_row_selections: &mut Vec<Selection<Point>>,
9195 selection: &Selection<Point>,
9196 display_map: &DisplaySnapshot,
9197 selections: &mut std::iter::Peekable<std::slice::Iter<Selection<Point>>>,
9198) -> (u32, u32) {
9199 contiguous_row_selections.push(selection.clone());
9200 let start_row = selection.start.row;
9201 let mut end_row = ending_row(selection, display_map);
9202
9203 while let Some(next_selection) = selections.peek() {
9204 if next_selection.start.row <= end_row {
9205 end_row = ending_row(next_selection, display_map);
9206 contiguous_row_selections.push(selections.next().unwrap().clone());
9207 } else {
9208 break;
9209 }
9210 }
9211 (start_row, end_row)
9212}
9213
9214fn ending_row(next_selection: &Selection<Point>, display_map: &DisplaySnapshot) -> u32 {
9215 if next_selection.end.column > 0 || next_selection.is_empty() {
9216 display_map.next_line_boundary(next_selection.end).0.row + 1
9217 } else {
9218 next_selection.end.row
9219 }
9220}
9221
9222impl EditorSnapshot {
9223 pub fn remote_selections_in_range<'a>(
9224 &'a self,
9225 range: &'a Range<Anchor>,
9226 collaboration_hub: &dyn CollaborationHub,
9227 cx: &'a AppContext,
9228 ) -> impl 'a + Iterator<Item = RemoteSelection> {
9229 let participant_indices = collaboration_hub.user_participant_indices(cx);
9230 let collaborators_by_peer_id = collaboration_hub.collaborators(cx);
9231 let collaborators_by_replica_id = collaborators_by_peer_id
9232 .iter()
9233 .map(|(_, collaborator)| (collaborator.replica_id, collaborator))
9234 .collect::<HashMap<_, _>>();
9235 self.buffer_snapshot
9236 .remote_selections_in_range(range)
9237 .filter_map(move |(replica_id, line_mode, cursor_shape, selection)| {
9238 let collaborator = collaborators_by_replica_id.get(&replica_id)?;
9239 let participant_index = participant_indices.get(&collaborator.user_id).copied();
9240 Some(RemoteSelection {
9241 replica_id,
9242 selection,
9243 cursor_shape,
9244 line_mode,
9245 participant_index,
9246 peer_id: collaborator.peer_id,
9247 })
9248 })
9249 }
9250
9251 pub fn language_at<T: ToOffset>(&self, position: T) -> Option<&Arc<Language>> {
9252 self.display_snapshot.buffer_snapshot.language_at(position)
9253 }
9254
9255 pub fn is_focused(&self) -> bool {
9256 self.is_focused
9257 }
9258
9259 pub fn placeholder_text(&self) -> Option<&Arc<str>> {
9260 self.placeholder_text.as_ref()
9261 }
9262
9263 pub fn scroll_position(&self) -> gpui::Point<f32> {
9264 self.scroll_anchor.scroll_position(&self.display_snapshot)
9265 }
9266}
9267
9268impl Deref for EditorSnapshot {
9269 type Target = DisplaySnapshot;
9270
9271 fn deref(&self) -> &Self::Target {
9272 &self.display_snapshot
9273 }
9274}
9275
9276#[derive(Clone, Debug, PartialEq, Eq)]
9277pub enum EditorEvent {
9278 InputIgnored {
9279 text: Arc<str>,
9280 },
9281 InputHandled {
9282 utf16_range_to_replace: Option<Range<isize>>,
9283 text: Arc<str>,
9284 },
9285 ExcerptsAdded {
9286 buffer: Model<Buffer>,
9287 predecessor: ExcerptId,
9288 excerpts: Vec<(ExcerptId, ExcerptRange<language::Anchor>)>,
9289 },
9290 ExcerptsRemoved {
9291 ids: Vec<ExcerptId>,
9292 },
9293 BufferEdited,
9294 Edited,
9295 Reparsed,
9296 Focused,
9297 Blurred,
9298 DirtyChanged,
9299 Saved,
9300 TitleChanged,
9301 DiffBaseChanged,
9302 SelectionsChanged {
9303 local: bool,
9304 },
9305 ScrollPositionChanged {
9306 local: bool,
9307 autoscroll: bool,
9308 },
9309 Closed,
9310}
9311
9312impl EventEmitter<EditorEvent> for Editor {}
9313
9314impl FocusableView for Editor {
9315 fn focus_handle(&self, _cx: &AppContext) -> FocusHandle {
9316 self.focus_handle.clone()
9317 }
9318}
9319
9320impl Render for Editor {
9321 fn render<'a>(&mut self, cx: &mut ViewContext<'a, Self>) -> impl IntoElement {
9322 let settings = ThemeSettings::get_global(cx);
9323 let text_style = match self.mode {
9324 EditorMode::SingleLine | EditorMode::AutoHeight { .. } => TextStyle {
9325 color: cx.theme().colors().editor_foreground,
9326 font_family: settings.ui_font.family.clone(),
9327 font_features: settings.ui_font.features,
9328 font_size: rems(0.875).into(),
9329 font_weight: FontWeight::NORMAL,
9330 font_style: FontStyle::Normal,
9331 line_height: relative(settings.buffer_line_height.value()),
9332 background_color: None,
9333 underline: None,
9334 white_space: WhiteSpace::Normal,
9335 },
9336
9337 EditorMode::Full => TextStyle {
9338 color: cx.theme().colors().editor_foreground,
9339 font_family: settings.buffer_font.family.clone(),
9340 font_features: settings.buffer_font.features,
9341 font_size: settings.buffer_font_size(cx).into(),
9342 font_weight: FontWeight::NORMAL,
9343 font_style: FontStyle::Normal,
9344 line_height: relative(settings.buffer_line_height.value()),
9345 background_color: None,
9346 underline: None,
9347 white_space: WhiteSpace::Normal,
9348 },
9349 };
9350
9351 let background = match self.mode {
9352 EditorMode::SingleLine => cx.theme().system().transparent,
9353 EditorMode::AutoHeight { max_lines: _ } => cx.theme().system().transparent,
9354 EditorMode::Full => cx.theme().colors().editor_background,
9355 };
9356
9357 EditorElement::new(
9358 cx.view(),
9359 EditorStyle {
9360 background,
9361 local_player: cx.theme().players().local(),
9362 text: text_style,
9363 scrollbar_width: px(12.),
9364 syntax: cx.theme().syntax().clone(),
9365 status: cx.theme().status().clone(),
9366 inlays_style: HighlightStyle {
9367 color: Some(cx.theme().status().hint),
9368 font_weight: Some(FontWeight::BOLD),
9369 ..HighlightStyle::default()
9370 },
9371 suggestions_style: HighlightStyle {
9372 color: Some(cx.theme().status().predictive),
9373 ..HighlightStyle::default()
9374 },
9375 },
9376 )
9377 }
9378}
9379
9380impl InputHandler for Editor {
9381 fn text_for_range(
9382 &mut self,
9383 range_utf16: Range<usize>,
9384 cx: &mut ViewContext<Self>,
9385 ) -> Option<String> {
9386 Some(
9387 self.buffer
9388 .read(cx)
9389 .read(cx)
9390 .text_for_range(OffsetUtf16(range_utf16.start)..OffsetUtf16(range_utf16.end))
9391 .collect(),
9392 )
9393 }
9394
9395 fn selected_text_range(&mut self, cx: &mut ViewContext<Self>) -> Option<Range<usize>> {
9396 // Prevent the IME menu from appearing when holding down an alphabetic key
9397 // while input is disabled.
9398 if !self.input_enabled {
9399 return None;
9400 }
9401
9402 let range = self.selections.newest::<OffsetUtf16>(cx).range();
9403 Some(range.start.0..range.end.0)
9404 }
9405
9406 fn marked_text_range(&self, cx: &mut ViewContext<Self>) -> Option<Range<usize>> {
9407 let snapshot = self.buffer.read(cx).read(cx);
9408 let range = self.text_highlights::<InputComposition>(cx)?.1.get(0)?;
9409 Some(range.start.to_offset_utf16(&snapshot).0..range.end.to_offset_utf16(&snapshot).0)
9410 }
9411
9412 fn unmark_text(&mut self, cx: &mut ViewContext<Self>) {
9413 self.clear_highlights::<InputComposition>(cx);
9414 self.ime_transaction.take();
9415 }
9416
9417 fn replace_text_in_range(
9418 &mut self,
9419 range_utf16: Option<Range<usize>>,
9420 text: &str,
9421 cx: &mut ViewContext<Self>,
9422 ) {
9423 if !self.input_enabled {
9424 cx.emit(EditorEvent::InputIgnored { text: text.into() });
9425 return;
9426 }
9427
9428 self.transact(cx, |this, cx| {
9429 let new_selected_ranges = if let Some(range_utf16) = range_utf16 {
9430 let range_utf16 = OffsetUtf16(range_utf16.start)..OffsetUtf16(range_utf16.end);
9431 Some(this.selection_replacement_ranges(range_utf16, cx))
9432 } else {
9433 this.marked_text_ranges(cx)
9434 };
9435
9436 let range_to_replace = new_selected_ranges.as_ref().and_then(|ranges_to_replace| {
9437 let newest_selection_id = this.selections.newest_anchor().id;
9438 this.selections
9439 .all::<OffsetUtf16>(cx)
9440 .iter()
9441 .zip(ranges_to_replace.iter())
9442 .find_map(|(selection, range)| {
9443 if selection.id == newest_selection_id {
9444 Some(
9445 (range.start.0 as isize - selection.head().0 as isize)
9446 ..(range.end.0 as isize - selection.head().0 as isize),
9447 )
9448 } else {
9449 None
9450 }
9451 })
9452 });
9453
9454 cx.emit(EditorEvent::InputHandled {
9455 utf16_range_to_replace: range_to_replace,
9456 text: text.into(),
9457 });
9458
9459 if let Some(new_selected_ranges) = new_selected_ranges {
9460 this.change_selections(None, cx, |selections| {
9461 selections.select_ranges(new_selected_ranges)
9462 });
9463 }
9464
9465 this.handle_input(text, cx);
9466 });
9467
9468 if let Some(transaction) = self.ime_transaction {
9469 self.buffer.update(cx, |buffer, cx| {
9470 buffer.group_until_transaction(transaction, cx);
9471 });
9472 }
9473
9474 self.unmark_text(cx);
9475 }
9476
9477 fn replace_and_mark_text_in_range(
9478 &mut self,
9479 range_utf16: Option<Range<usize>>,
9480 text: &str,
9481 new_selected_range_utf16: Option<Range<usize>>,
9482 cx: &mut ViewContext<Self>,
9483 ) {
9484 if !self.input_enabled {
9485 cx.emit(EditorEvent::InputIgnored { text: text.into() });
9486 return;
9487 }
9488
9489 let transaction = self.transact(cx, |this, cx| {
9490 let ranges_to_replace = if let Some(mut marked_ranges) = this.marked_text_ranges(cx) {
9491 let snapshot = this.buffer.read(cx).read(cx);
9492 if let Some(relative_range_utf16) = range_utf16.as_ref() {
9493 for marked_range in &mut marked_ranges {
9494 marked_range.end.0 = marked_range.start.0 + relative_range_utf16.end;
9495 marked_range.start.0 += relative_range_utf16.start;
9496 marked_range.start =
9497 snapshot.clip_offset_utf16(marked_range.start, Bias::Left);
9498 marked_range.end =
9499 snapshot.clip_offset_utf16(marked_range.end, Bias::Right);
9500 }
9501 }
9502 Some(marked_ranges)
9503 } else if let Some(range_utf16) = range_utf16 {
9504 let range_utf16 = OffsetUtf16(range_utf16.start)..OffsetUtf16(range_utf16.end);
9505 Some(this.selection_replacement_ranges(range_utf16, cx))
9506 } else {
9507 None
9508 };
9509
9510 let range_to_replace = ranges_to_replace.as_ref().and_then(|ranges_to_replace| {
9511 let newest_selection_id = this.selections.newest_anchor().id;
9512 this.selections
9513 .all::<OffsetUtf16>(cx)
9514 .iter()
9515 .zip(ranges_to_replace.iter())
9516 .find_map(|(selection, range)| {
9517 if selection.id == newest_selection_id {
9518 Some(
9519 (range.start.0 as isize - selection.head().0 as isize)
9520 ..(range.end.0 as isize - selection.head().0 as isize),
9521 )
9522 } else {
9523 None
9524 }
9525 })
9526 });
9527
9528 cx.emit(EditorEvent::InputHandled {
9529 utf16_range_to_replace: range_to_replace,
9530 text: text.into(),
9531 });
9532
9533 if let Some(ranges) = ranges_to_replace {
9534 this.change_selections(None, cx, |s| s.select_ranges(ranges));
9535 }
9536
9537 let marked_ranges = {
9538 let snapshot = this.buffer.read(cx).read(cx);
9539 this.selections
9540 .disjoint_anchors()
9541 .iter()
9542 .map(|selection| {
9543 selection.start.bias_left(&*snapshot)..selection.end.bias_right(&*snapshot)
9544 })
9545 .collect::<Vec<_>>()
9546 };
9547
9548 if text.is_empty() {
9549 this.unmark_text(cx);
9550 } else {
9551 this.highlight_text::<InputComposition>(
9552 marked_ranges.clone(),
9553 HighlightStyle::default(), // todo!() this.style(cx).composition_mark,
9554 cx,
9555 );
9556 }
9557
9558 this.handle_input(text, cx);
9559
9560 if let Some(new_selected_range) = new_selected_range_utf16 {
9561 let snapshot = this.buffer.read(cx).read(cx);
9562 let new_selected_ranges = marked_ranges
9563 .into_iter()
9564 .map(|marked_range| {
9565 let insertion_start = marked_range.start.to_offset_utf16(&snapshot).0;
9566 let new_start = OffsetUtf16(new_selected_range.start + insertion_start);
9567 let new_end = OffsetUtf16(new_selected_range.end + insertion_start);
9568 snapshot.clip_offset_utf16(new_start, Bias::Left)
9569 ..snapshot.clip_offset_utf16(new_end, Bias::Right)
9570 })
9571 .collect::<Vec<_>>();
9572
9573 drop(snapshot);
9574 this.change_selections(None, cx, |selections| {
9575 selections.select_ranges(new_selected_ranges)
9576 });
9577 }
9578 });
9579
9580 self.ime_transaction = self.ime_transaction.or(transaction);
9581 if let Some(transaction) = self.ime_transaction {
9582 self.buffer.update(cx, |buffer, cx| {
9583 buffer.group_until_transaction(transaction, cx);
9584 });
9585 }
9586
9587 if self.text_highlights::<InputComposition>(cx).is_none() {
9588 self.ime_transaction.take();
9589 }
9590 }
9591
9592 fn bounds_for_range(
9593 &mut self,
9594 range_utf16: Range<usize>,
9595 element_bounds: gpui::Bounds<Pixels>,
9596 cx: &mut ViewContext<Self>,
9597 ) -> Option<gpui::Bounds<Pixels>> {
9598 let text_layout_details = self.text_layout_details(cx);
9599 let style = &text_layout_details.editor_style;
9600 let font_id = cx.text_system().resolve_font(&style.text.font());
9601 let font_size = style.text.font_size.to_pixels(cx.rem_size());
9602 let line_height = style.text.line_height_in_pixels(cx.rem_size());
9603 let em_width = cx
9604 .text_system()
9605 .typographic_bounds(font_id, font_size, 'm')
9606 .unwrap()
9607 .size
9608 .width;
9609
9610 let snapshot = self.snapshot(cx);
9611 let scroll_position = snapshot.scroll_position();
9612 let scroll_left = scroll_position.x * em_width;
9613
9614 let start = OffsetUtf16(range_utf16.start).to_display_point(&snapshot);
9615 let x = snapshot.x_for_display_point(start, &text_layout_details) - scroll_left
9616 + self.gutter_width;
9617 let y = line_height * (start.row() as f32 - scroll_position.y);
9618
9619 Some(Bounds {
9620 origin: element_bounds.origin + point(x, y),
9621 size: size(em_width, line_height),
9622 })
9623 }
9624}
9625
9626trait SelectionExt {
9627 fn offset_range(&self, buffer: &MultiBufferSnapshot) -> Range<usize>;
9628 fn point_range(&self, buffer: &MultiBufferSnapshot) -> Range<Point>;
9629 fn display_range(&self, map: &DisplaySnapshot) -> Range<DisplayPoint>;
9630 fn spanned_rows(&self, include_end_if_at_line_start: bool, map: &DisplaySnapshot)
9631 -> Range<u32>;
9632}
9633
9634impl<T: ToPoint + ToOffset> SelectionExt for Selection<T> {
9635 fn point_range(&self, buffer: &MultiBufferSnapshot) -> Range<Point> {
9636 let start = self.start.to_point(buffer);
9637 let end = self.end.to_point(buffer);
9638 if self.reversed {
9639 end..start
9640 } else {
9641 start..end
9642 }
9643 }
9644
9645 fn offset_range(&self, buffer: &MultiBufferSnapshot) -> Range<usize> {
9646 let start = self.start.to_offset(buffer);
9647 let end = self.end.to_offset(buffer);
9648 if self.reversed {
9649 end..start
9650 } else {
9651 start..end
9652 }
9653 }
9654
9655 fn display_range(&self, map: &DisplaySnapshot) -> Range<DisplayPoint> {
9656 let start = self
9657 .start
9658 .to_point(&map.buffer_snapshot)
9659 .to_display_point(map);
9660 let end = self
9661 .end
9662 .to_point(&map.buffer_snapshot)
9663 .to_display_point(map);
9664 if self.reversed {
9665 end..start
9666 } else {
9667 start..end
9668 }
9669 }
9670
9671 fn spanned_rows(
9672 &self,
9673 include_end_if_at_line_start: bool,
9674 map: &DisplaySnapshot,
9675 ) -> Range<u32> {
9676 let start = self.start.to_point(&map.buffer_snapshot);
9677 let mut end = self.end.to_point(&map.buffer_snapshot);
9678 if !include_end_if_at_line_start && start.row != end.row && end.column == 0 {
9679 end.row -= 1;
9680 }
9681
9682 let buffer_start = map.prev_line_boundary(start).0;
9683 let buffer_end = map.next_line_boundary(end).0;
9684 buffer_start.row..buffer_end.row + 1
9685 }
9686}
9687
9688impl<T: InvalidationRegion> InvalidationStack<T> {
9689 fn invalidate<S>(&mut self, selections: &[Selection<S>], buffer: &MultiBufferSnapshot)
9690 where
9691 S: Clone + ToOffset,
9692 {
9693 while let Some(region) = self.last() {
9694 let all_selections_inside_invalidation_ranges =
9695 if selections.len() == region.ranges().len() {
9696 selections
9697 .iter()
9698 .zip(region.ranges().iter().map(|r| r.to_offset(buffer)))
9699 .all(|(selection, invalidation_range)| {
9700 let head = selection.head().to_offset(buffer);
9701 invalidation_range.start <= head && invalidation_range.end >= head
9702 })
9703 } else {
9704 false
9705 };
9706
9707 if all_selections_inside_invalidation_ranges {
9708 break;
9709 } else {
9710 self.pop();
9711 }
9712 }
9713 }
9714}
9715
9716impl<T> Default for InvalidationStack<T> {
9717 fn default() -> Self {
9718 Self(Default::default())
9719 }
9720}
9721
9722impl<T> Deref for InvalidationStack<T> {
9723 type Target = Vec<T>;
9724
9725 fn deref(&self) -> &Self::Target {
9726 &self.0
9727 }
9728}
9729
9730impl<T> DerefMut for InvalidationStack<T> {
9731 fn deref_mut(&mut self) -> &mut Self::Target {
9732 &mut self.0
9733 }
9734}
9735
9736impl InvalidationRegion for SnippetState {
9737 fn ranges(&self) -> &[Range<Anchor>] {
9738 &self.ranges[self.active_index]
9739 }
9740}
9741
9742pub fn diagnostic_block_renderer(diagnostic: Diagnostic, _is_valid: bool) -> RenderBlock {
9743 let (text_without_backticks, code_ranges) = highlight_diagnostic_message(&diagnostic);
9744
9745 Arc::new(move |cx: &mut BlockContext| {
9746 let color = Some(cx.theme().colors().text_accent);
9747 let group_id: SharedString = cx.block_id.to_string().into();
9748 // TODO: Nate: We should tint the background of the block with the severity color
9749 // We need to extend the theme before we can do this
9750 h_flex()
9751 .id(cx.block_id)
9752 .group(group_id.clone())
9753 .relative()
9754 .pl(cx.anchor_x)
9755 .size_full()
9756 .gap_2()
9757 .child(
9758 StyledText::new(text_without_backticks.clone()).with_highlights(
9759 &cx.text_style(),
9760 code_ranges.iter().map(|range| {
9761 (
9762 range.clone(),
9763 HighlightStyle {
9764 color,
9765 ..Default::default()
9766 },
9767 )
9768 }),
9769 ),
9770 )
9771 .child(
9772 IconButton::new(("copy-block", cx.block_id), IconName::Copy)
9773 .icon_color(Color::Muted)
9774 .size(ButtonSize::Compact)
9775 .style(ButtonStyle::Transparent)
9776 .visible_on_hover(group_id)
9777 .on_click(cx.listener({
9778 let message = diagnostic.message.clone();
9779 move |_, _, cx| cx.write_to_clipboard(ClipboardItem::new(message.clone()))
9780 }))
9781 .tooltip(|cx| Tooltip::text("Copy diagnostic message", cx)),
9782 )
9783 .into_any_element()
9784 })
9785}
9786
9787pub fn highlight_diagnostic_message(diagnostic: &Diagnostic) -> (SharedString, Vec<Range<usize>>) {
9788 let mut text_without_backticks = String::new();
9789 let mut code_ranges = Vec::new();
9790
9791 if let Some(source) = &diagnostic.source {
9792 text_without_backticks.push_str(&source);
9793 code_ranges.push(0..source.len());
9794 text_without_backticks.push_str(": ");
9795 }
9796
9797 let mut prev_offset = 0;
9798 let mut in_code_block = false;
9799 for (ix, _) in diagnostic
9800 .message
9801 .match_indices('`')
9802 .chain([(diagnostic.message.len(), "")])
9803 {
9804 let prev_len = text_without_backticks.len();
9805 text_without_backticks.push_str(&diagnostic.message[prev_offset..ix]);
9806 prev_offset = ix + 1;
9807 if in_code_block {
9808 code_ranges.push(prev_len..text_without_backticks.len());
9809 in_code_block = false;
9810 } else {
9811 in_code_block = true;
9812 }
9813 }
9814
9815 (text_without_backticks.into(), code_ranges)
9816}
9817
9818pub fn diagnostic_style(severity: DiagnosticSeverity, valid: bool, colors: &StatusColors) -> Hsla {
9819 match (severity, valid) {
9820 (DiagnosticSeverity::ERROR, true) => colors.error,
9821 (DiagnosticSeverity::ERROR, false) => colors.error,
9822 (DiagnosticSeverity::WARNING, true) => colors.warning,
9823 (DiagnosticSeverity::WARNING, false) => colors.warning,
9824 (DiagnosticSeverity::INFORMATION, true) => colors.info,
9825 (DiagnosticSeverity::INFORMATION, false) => colors.info,
9826 (DiagnosticSeverity::HINT, true) => colors.info,
9827 (DiagnosticSeverity::HINT, false) => colors.info,
9828 _ => colors.ignored,
9829 }
9830}
9831
9832pub fn styled_runs_for_code_label<'a>(
9833 label: &'a CodeLabel,
9834 syntax_theme: &'a theme::SyntaxTheme,
9835) -> impl 'a + Iterator<Item = (Range<usize>, HighlightStyle)> {
9836 let fade_out = HighlightStyle {
9837 fade_out: Some(0.35),
9838 ..Default::default()
9839 };
9840
9841 let mut prev_end = label.filter_range.end;
9842 label
9843 .runs
9844 .iter()
9845 .enumerate()
9846 .flat_map(move |(ix, (range, highlight_id))| {
9847 let style = if let Some(style) = highlight_id.style(syntax_theme) {
9848 style
9849 } else {
9850 return Default::default();
9851 };
9852 let mut muted_style = style;
9853 muted_style.highlight(fade_out);
9854
9855 let mut runs = SmallVec::<[(Range<usize>, HighlightStyle); 3]>::new();
9856 if range.start >= label.filter_range.end {
9857 if range.start > prev_end {
9858 runs.push((prev_end..range.start, fade_out));
9859 }
9860 runs.push((range.clone(), muted_style));
9861 } else if range.end <= label.filter_range.end {
9862 runs.push((range.clone(), style));
9863 } else {
9864 runs.push((range.start..label.filter_range.end, style));
9865 runs.push((label.filter_range.end..range.end, muted_style));
9866 }
9867 prev_end = cmp::max(prev_end, range.end);
9868
9869 if ix + 1 == label.runs.len() && label.text.len() > prev_end {
9870 runs.push((prev_end..label.text.len(), fade_out));
9871 }
9872
9873 runs
9874 })
9875}
9876
9877pub fn split_words<'a>(text: &'a str) -> impl std::iter::Iterator<Item = &'a str> + 'a {
9878 let mut index = 0;
9879 let mut codepoints = text.char_indices().peekable();
9880
9881 std::iter::from_fn(move || {
9882 let start_index = index;
9883 while let Some((new_index, codepoint)) = codepoints.next() {
9884 index = new_index + codepoint.len_utf8();
9885 let current_upper = codepoint.is_uppercase();
9886 let next_upper = codepoints
9887 .peek()
9888 .map(|(_, c)| c.is_uppercase())
9889 .unwrap_or(false);
9890
9891 if !current_upper && next_upper {
9892 return Some(&text[start_index..index]);
9893 }
9894 }
9895
9896 index = text.len();
9897 if start_index < text.len() {
9898 return Some(&text[start_index..]);
9899 }
9900 None
9901 })
9902 .flat_map(|word| word.split_inclusive('_'))
9903 .flat_map(|word| word.split_inclusive('-'))
9904}
9905
9906trait RangeToAnchorExt {
9907 fn to_anchors(self, snapshot: &MultiBufferSnapshot) -> Range<Anchor>;
9908}
9909
9910impl<T: ToOffset> RangeToAnchorExt for Range<T> {
9911 fn to_anchors(self, snapshot: &MultiBufferSnapshot) -> Range<Anchor> {
9912 snapshot.anchor_after(self.start)..snapshot.anchor_before(self.end)
9913 }
9914}