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