#![allow(rustdoc::private_intra_doc_links)]
//! This is the place where everything editor-related is stored (data-wise) and displayed (ui-wise).
//! The main point of interest in this crate is [`Editor`] type, which is used in every other Zed part as a user input element.
//! It comes in different flavors: single line, multiline and a fixed height one.
//!
//! Editor contains of multiple large submodules:
//! * [`element`] — the place where all rendering happens
//! * [`display_map`] - chunks up text in the editor into the logical blocks, establishes coordinates and mapping between each of them.
//!   Contains all metadata related to text transformations (folds, fake inlay text insertions, soft wraps, tab markup, etc.).
//!
//! All other submodules and structs are mostly concerned with holding editor data about the way it displays current buffer region(s).
//!
//! If you're looking to improve Vim mode, you should check out Vim crate that wraps Editor and overrides its behavior.
pub mod actions;
pub mod blink_manager;
mod bracket_colorization;
mod clangd_ext;
pub mod code_context_menus;
pub mod display_map;
mod document_colors;
mod document_symbols;
mod editor_settings;
mod element;
mod folding_ranges;
mod git;
mod highlight_matching_bracket;
mod hover_links;
pub mod hover_popover;
mod indent_guides;
mod inlays;
pub mod items;
mod jsx_tag_auto_close;
mod linked_editing_ranges;
mod lsp_ext;
mod mouse_context_menu;
pub mod movement;
mod persistence;
mod runnables;
mod rust_analyzer_ext;
pub mod scroll;
mod selections_collection;
pub mod semantic_tokens;
mod split;
pub mod split_editor_view;

#[cfg(test)]
mod code_completion_tests;
#[cfg(test)]
mod edit_prediction_tests;
#[cfg(test)]
mod editor_tests;
mod signature_help;
#[cfg(any(test, feature = "test-support"))]
pub mod test;

pub(crate) use actions::*;
pub use display_map::{
    ChunkRenderer, ChunkRendererContext, DisplayPoint, FoldPlaceholder, HighlightKey,
    SemanticTokenHighlight,
};
pub use edit_prediction_types::Direction;
pub use editor_settings::{
    CompletionDetailAlignment, CurrentLineHighlight, DiffViewStyle, DocumentColorsRenderMode,
    EditorSettings, EditorSettingsScrollbarProxy, HideMouseMode, ScrollBeyondLastLine,
    ScrollbarAxes, SearchSettings, ShowMinimap, ui_scrollbar_settings_from_raw,
};
pub use element::{
    CursorLayout, EditorElement, HighlightedRange, HighlightedRangeLine, PointForPosition,
    render_breadcrumb_text,
};
pub use git::blame::BlameRenderer;
pub use hover_popover::hover_markdown_style;
pub use inlays::Inlay;
pub use items::MAX_TAB_TITLE_LEN;
pub use linked_editing_ranges::LinkedEdits;
pub use lsp::CompletionContext;
pub use lsp_ext::lsp_tasks;
pub use multi_buffer::{
    Anchor, AnchorRangeExt, BufferOffset, ExcerptRange, MBTextSummary, MultiBuffer,
    MultiBufferOffset, MultiBufferOffsetUtf16, MultiBufferSnapshot, PathKey, RowInfo, ToOffset,
    ToPoint,
};
pub use split::{SplittableEditor, ToggleSplitDiff};
pub use split_editor_view::SplitEditorView;
pub use text::Bias;

use ::git::{Restore, blame::BlameEntry, commit::ParsedCommitMessage, status::FileStatus};
use aho_corasick::{AhoCorasick, AhoCorasickBuilder, BuildError};
use anyhow::{Context as _, Result, anyhow, bail};
use blink_manager::BlinkManager;
use buffer_diff::DiffHunkStatus;
use client::{Collaborator, ParticipantIndex, parse_zed_link};
use clock::ReplicaId;
use code_context_menus::{
    AvailableCodeAction, CodeActionContents, CodeActionsItem, CodeActionsMenu, CodeContextMenu,
    CompletionsMenu, ContextMenuOrigin,
};
use collections::{BTreeMap, HashMap, HashSet, VecDeque};
use convert_case::{Case, Casing};
use dap::TelemetrySpawnLocation;
use display_map::*;
use document_colors::LspColorData;
use edit_prediction_types::{
    EditPredictionDelegate, EditPredictionDelegateHandle, EditPredictionDiscardReason,
    EditPredictionGranularity, SuggestionDisplayType,
};
use editor_settings::{GoToDefinitionFallback, Minimap as MinimapSettings};
use element::{LineWithInvisibles, PositionMap, layout_line};
use futures::{
    FutureExt,
    future::{self, Shared, join},
};
use fuzzy::{StringMatch, StringMatchCandidate};
use git::blame::{GitBlame, GlobalBlameRenderer};
use gpui::{
    Action, Animation, AnimationExt, AnyElement, App, AppContext, AsyncWindowContext,
    AvailableSpace, Background, Bounds, ClickEvent, ClipboardEntry, ClipboardItem, Context,
    DispatchPhase, Edges, Entity, EntityId, EntityInputHandler, EventEmitter, FocusHandle,
    FocusOutEvent, Focusable, FontId, FontStyle, FontWeight, Global, HighlightStyle, Hsla,
    KeyContext, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, PaintQuad, ParentElement,
    Pixels, PressureStage, Render, ScrollHandle, SharedString, SharedUri, Size, Stateful, Styled,
    Subscription, Task, TextRun, TextStyle, TextStyleRefinement, UTF16Selection, UnderlineStyle,
    UniformListScrollHandle, WeakEntity, WeakFocusHandle, Window, div, point, prelude::*,
    pulsating_between, px, relative, size,
};
use hover_links::{HoverLink, HoveredLinkState, find_file};
use hover_popover::{HoverState, hide_hover};
use indent_guides::ActiveIndentGuidesState;
use inlays::{InlaySplice, inlay_hints::InlayHintRefreshReason};
use itertools::{Either, Itertools};
use language::{
    AutoindentMode, BlockCommentConfig, BracketMatch, BracketPair, Buffer, BufferRow,
    BufferSnapshot, Capability, CharClassifier, CharKind, CharScopeContext, CodeLabel, CursorShape,
    DiagnosticEntryRef, DiffOptions, EditPredictionsMode, EditPreview, HighlightedText, IndentKind,
    IndentSize, Language, LanguageAwareStyling, LanguageName, LanguageRegistry, LanguageScope,
    LocalFile, OffsetRangeExt, OutlineItem, Point, Selection, SelectionGoal, TextObject,
    TransactionId, TreeSitterOptions, WordsQuery,
    language_settings::{
        self, AllLanguageSettings, LanguageSettings, LspInsertMode, RewrapBehavior,
        WordsCompletionMode, all_language_settings,
    },
    point_from_lsp, point_to_lsp, text_diff_with_options,
};
use linked_editing_ranges::refresh_linked_ranges;
use lsp::{
    CodeActionKind, CompletionItemKind, CompletionTriggerKind, InsertTextFormat, InsertTextMode,
    LanguageServerId,
};
use markdown::Markdown;
use mouse_context_menu::MouseContextMenu;
use movement::TextLayoutDetails;
use multi_buffer::{
    ExcerptBoundaryInfo, ExpandExcerptDirection, MultiBufferDiffHunk, MultiBufferPoint,
    MultiBufferRow,
};
use parking_lot::Mutex;
use persistence::EditorDb;
use project::{
    BreakpointWithPosition, CodeAction, Completion, CompletionDisplayOptions, CompletionIntent,
    CompletionResponse, CompletionSource, DisableAiSettings, DocumentHighlight, InlayHint, InlayId,
    InvalidationStrategy, Location, LocationLink, LspAction, PrepareRenameResponse, Project,
    ProjectItem, ProjectPath, ProjectTransaction,
    debugger::{
        breakpoint_store::{
            Breakpoint, BreakpointEditAction, BreakpointSessionState, BreakpointState,
            BreakpointStore, BreakpointStoreEvent,
        },
        session::{Session, SessionEvent},
    },
    git_store::GitStoreEvent,
    lsp_store::{
        BufferSemanticTokens, CacheInlayHints, CompletionDocumentation, FormatTrigger,
        LspFormatTarget, OpenLspBufferHandle, RefreshForServer,
    },
    project_settings::{DiagnosticSeverity, GoToDiagnosticSeverityFilter, ProjectSettings},
};
use rand::seq::SliceRandom;
use regex::Regex;
use rpc::{ErrorCode, ErrorExt, proto::PeerId};
use scroll::{Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, SharedScrollAnchor};
use selections_collection::{MutableSelectionsCollection, SelectionsCollection};
use serde::{Deserialize, Serialize};
use settings::{
    GitGutterSetting, RelativeLineNumbers, Settings, SettingsLocation, SettingsStore,
    update_settings_file,
};
use smallvec::{SmallVec, smallvec};
use snippet::Snippet;
use std::{
    any::{Any, TypeId},
    borrow::Cow,
    cell::{OnceCell, RefCell},
    cmp::{self, Ordering, Reverse},
    collections::hash_map,
    iter::{self, Peekable},
    mem,
    num::NonZeroU32,
    ops::{ControlFlow, Deref, DerefMut, Not, Range, RangeInclusive},
    path::{Path, PathBuf},
    rc::Rc,
    sync::Arc,
    time::{Duration, Instant},
};
use task::TaskVariables;
use text::{BufferId, FromAnchor, OffsetUtf16, Rope, ToOffset as _, ToPoint as _};
use theme::{
    AccentColors, ActiveTheme, GlobalTheme, PlayerColor, StatusColors, SyntaxTheme, Theme,
};
use theme_settings::{ThemeSettings, observe_buffer_font_size_adjustment};
use ui::{
    Avatar, ButtonSize, ButtonStyle, ContextMenu, Disclosure, IconButton, IconButtonShape,
    IconName, IconSize, Indicator, Key, Tooltip, h_flex, prelude::*, scrollbars::ScrollbarAutoHide,
    utils::WithRemSize,
};
use ui_input::ErasedEditor;
use util::{RangeExt, ResultExt, TryFutureExt, maybe, post_inc};
use workspace::{
    CollaboratorId, Item as WorkspaceItem, ItemId, ItemNavHistory, NavigationEntry, OpenInTerminal,
    OpenTerminal, Pane, RestoreOnStartupBehavior, SERIALIZATION_THROTTLE_TIME, SplitDirection,
    TabBarSettings, Toast, ViewId, Workspace, WorkspaceId, WorkspaceSettings,
    item::{ItemBufferKind, ItemHandle, PreviewTabsSettings, SaveOptions},
    notifications::{DetachAndPromptErr, NotificationId, NotifyTaskExt},
    searchable::SearchEvent,
};
pub use zed_actions::editor::RevealInFileManager;
use zed_actions::editor::{MoveDown, MoveUp};

use crate::{
    code_context_menus::CompletionsMenuSource,
    editor_settings::MultiCursorModifier,
    hover_links::{find_url, find_url_from_range},
    inlays::{
        InlineValueCache,
        inlay_hints::{LspInlayHintData, inlay_hint_settings},
    },
    runnables::{ResolvedTasks, RunnableData, RunnableTasks},
    scroll::{ScrollOffset, ScrollPixelOffset},
    selections_collection::resolve_selections_wrapping_blocks,
    semantic_tokens::SemanticTokenState,
    signature_help::{SignatureHelpHiddenBy, SignatureHelpState},
};

pub const FILE_HEADER_HEIGHT: u32 = 2;
pub const BUFFER_HEADER_PADDING: Rems = rems(0.25);
pub const MULTI_BUFFER_EXCERPT_HEADER_HEIGHT: u32 = 1;
const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500);
const MAX_LINE_LEN: usize = 1024;
const MIN_NAVIGATION_HISTORY_ROW_DELTA: i64 = 10;
const MAX_SELECTION_HISTORY_LEN: usize = 1024;
pub(crate) const CURSORS_VISIBLE_FOR: Duration = Duration::from_millis(2000);
#[doc(hidden)]
pub const CODE_ACTIONS_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(250);
pub const SELECTION_HIGHLIGHT_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(100);

pub(crate) const CODE_ACTION_TIMEOUT: Duration = Duration::from_secs(5);
pub(crate) const FORMAT_TIMEOUT: Duration = Duration::from_secs(5);
pub(crate) const SCROLL_CENTER_TOP_BOTTOM_DEBOUNCE_TIMEOUT: Duration = Duration::from_secs(1);
pub const LSP_REQUEST_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(50);

pub(crate) const EDIT_PREDICTION_KEY_CONTEXT: &str = "edit_prediction";
pub(crate) const MINIMAP_FONT_SIZE: AbsoluteLength = AbsoluteLength::Pixels(px(2.));

pub type RenderDiffHunkControlsFn = Arc<
    dyn Fn(
        u32,
        &DiffHunkStatus,
        Range<Anchor>,
        bool,
        Pixels,
        &Entity<Editor>,
        &mut Window,
        &mut App,
    ) -> AnyElement,
>;

enum ReportEditorEvent {
    Saved { auto_saved: bool },
    EditorOpened,
    Closed,
}

impl ReportEditorEvent {
    pub fn event_type(&self) -> &'static str {
        match self {
            Self::Saved { .. } => "Editor Saved",
            Self::EditorOpened => "Editor Opened",
            Self::Closed => "Editor Closed",
        }
    }
}

pub enum ActiveDebugLine {}
pub enum DebugStackFrameLine {}

pub enum ConflictsOuter {}
pub enum ConflictsOurs {}
pub enum ConflictsTheirs {}
pub enum ConflictsOursMarker {}
pub enum ConflictsTheirsMarker {}

pub struct HunkAddedColor;
pub struct HunkRemovedColor;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Navigated {
    Yes,
    No,
}

impl Navigated {
    pub fn from_bool(yes: bool) -> Navigated {
        if yes { Navigated::Yes } else { Navigated::No }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum DisplayDiffHunk {
    Folded {
        display_row: DisplayRow,
    },
    Unfolded {
        is_created_file: bool,
        diff_base_byte_range: Range<usize>,
        display_row_range: Range<DisplayRow>,
        multi_buffer_range: Range<Anchor>,
        status: DiffHunkStatus,
        word_diffs: Vec<Range<MultiBufferOffset>>,
    },
}

pub enum HideMouseCursorOrigin {
    TypingAction,
    MovementAction,
}

pub fn init(cx: &mut App) {
    cx.set_global(GlobalBlameRenderer(Arc::new(())));
    cx.set_global(breadcrumbs::RenderBreadcrumbText(render_breadcrumb_text));

    workspace::register_project_item::<Editor>(cx);
    workspace::FollowableViewRegistry::register::<Editor>(cx);
    workspace::register_serializable_item::<Editor>(cx);

    cx.observe_new(
        |workspace: &mut Workspace, _: Option<&mut Window>, _cx: &mut Context<Workspace>| {
            workspace.register_action(Editor::new_file);
            workspace.register_action(Editor::new_file_split);
            workspace.register_action(Editor::new_file_vertical);
            workspace.register_action(Editor::new_file_horizontal);
            workspace.register_action(Editor::cancel_language_server_work);
            workspace.register_action(Editor::toggle_focus);
        },
    )
    .detach();

    cx.on_action(move |_: &workspace::NewFile, cx| {
        let app_state = workspace::AppState::global(cx);
        workspace::open_new(
            Default::default(),
            app_state,
            cx,
            |workspace, window, cx| Editor::new_file(workspace, &Default::default(), window, cx),
        )
        .detach_and_log_err(cx);
    })
    .on_action(move |_: &workspace::NewWindow, cx| {
        let app_state = workspace::AppState::global(cx);
        workspace::open_new(
            Default::default(),
            app_state,
            cx,
            |workspace, window, cx| {
                cx.activate(true);
                Editor::new_file(workspace, &Default::default(), window, cx)
            },
        )
        .detach_and_log_err(cx);
    });
    _ = ui_input::ERASED_EDITOR_FACTORY.set(|window, cx| {
        Arc::new(ErasedEditorImpl(
            cx.new(|cx| Editor::single_line(window, cx)),
        )) as Arc<dyn ErasedEditor>
    });
    _ = multi_buffer::EXCERPT_CONTEXT_LINES.set(multibuffer_context_lines);
}

pub fn set_blame_renderer(renderer: impl BlameRenderer + 'static, cx: &mut App) {
    cx.set_global(GlobalBlameRenderer(Arc::new(renderer)));
}

pub trait DiagnosticRenderer {
    fn render_group(
        &self,
        diagnostic_group: Vec<DiagnosticEntryRef<'_, Point>>,
        buffer_id: BufferId,
        snapshot: EditorSnapshot,
        editor: WeakEntity<Editor>,
        language_registry: Option<Arc<LanguageRegistry>>,
        cx: &mut App,
    ) -> Vec<BlockProperties<Anchor>>;

    fn render_hover(
        &self,
        diagnostic_group: Vec<DiagnosticEntryRef<'_, Point>>,
        range: Range<Point>,
        buffer_id: BufferId,
        language_registry: Option<Arc<LanguageRegistry>>,
        cx: &mut App,
    ) -> Option<Entity<markdown::Markdown>>;

    fn open_link(
        &self,
        editor: &mut Editor,
        link: SharedString,
        window: &mut Window,
        cx: &mut Context<Editor>,
    );
}

pub(crate) struct GlobalDiagnosticRenderer(pub Arc<dyn DiagnosticRenderer>);

impl GlobalDiagnosticRenderer {
    fn global(cx: &App) -> Option<Arc<dyn DiagnosticRenderer>> {
        cx.try_global::<Self>().map(|g| g.0.clone())
    }
}

impl gpui::Global for GlobalDiagnosticRenderer {}
pub fn set_diagnostic_renderer(renderer: impl DiagnosticRenderer + 'static, cx: &mut App) {
    cx.set_global(GlobalDiagnosticRenderer(Arc::new(renderer)));
}

pub struct SearchWithinRange;

trait InvalidationRegion {
    fn ranges(&self) -> &[Range<Anchor>];
}

#[derive(Clone, Debug, PartialEq)]
pub enum SelectPhase {
    Begin {
        position: DisplayPoint,
        add: bool,
        click_count: usize,
    },
    BeginColumnar {
        position: DisplayPoint,
        reset: bool,
        mode: ColumnarMode,
        goal_column: u32,
    },
    Extend {
        position: DisplayPoint,
        click_count: usize,
    },
    Update {
        position: DisplayPoint,
        goal_column: u32,
        scroll_delta: gpui::Point<f32>,
    },
    End,
}

#[derive(Clone, Debug, PartialEq)]
pub enum ColumnarMode {
    FromMouse,
    FromSelection,
}

#[derive(Clone, Debug)]
pub enum SelectMode {
    Character,
    Word(Range<Anchor>),
    Line(Range<Anchor>),
    All,
}

#[derive(Copy, Clone, Default, PartialEq, Eq, Debug)]
pub enum SizingBehavior {
    /// The editor will layout itself using `size_full` and will include the vertical
    /// scroll margin as requested by user settings.
    #[default]
    Default,
    /// The editor will layout itself using `size_full`, but will not have any
    /// vertical overscroll.
    ExcludeOverscrollMargin,
    /// The editor will request a vertical size according to its content and will be
    /// layouted without a vertical scroll margin.
    SizeByContent,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum EditorMode {
    SingleLine,
    AutoHeight {
        min_lines: usize,
        max_lines: Option<usize>,
    },
    Full {
        /// When set to `true`, the editor will scale its UI elements with the buffer font size.
        scale_ui_elements_with_buffer_font_size: bool,
        /// When set to `true`, the editor will render a background for the active line.
        show_active_line_background: bool,
        /// Determines the sizing behavior for this editor
        sizing_behavior: SizingBehavior,
    },
    Minimap {
        parent: WeakEntity<Editor>,
    },
}

impl EditorMode {
    pub fn full() -> Self {
        Self::Full {
            scale_ui_elements_with_buffer_font_size: true,
            show_active_line_background: true,
            sizing_behavior: SizingBehavior::Default,
        }
    }

    #[inline]
    pub fn is_full(&self) -> bool {
        matches!(self, Self::Full { .. })
    }

    #[inline]
    pub fn is_single_line(&self) -> bool {
        matches!(self, Self::SingleLine { .. })
    }

    #[inline]
    fn is_minimap(&self) -> bool {
        matches!(self, Self::Minimap { .. })
    }
}

#[derive(Copy, Clone, Debug)]
pub enum SoftWrap {
    /// Prefer not to wrap at all.
    ///
    /// Note: this is currently internal, as actually limited by [`crate::MAX_LINE_LEN`] until it wraps.
    /// The mode is used inside git diff hunks, where it's seems currently more useful to not wrap as much as possible.
    GitDiff,
    /// Prefer a single line generally, unless an overly long line is encountered.
    None,
    /// Soft wrap lines that exceed the editor width.
    EditorWidth,
    /// Soft wrap lines at the preferred line length.
    Column(u32),
    /// Soft wrap line at the preferred line length or the editor width (whichever is smaller).
    Bounded(u32),
}

#[derive(Clone)]
pub struct EditorStyle {
    pub background: Hsla,
    pub border: Hsla,
    pub local_player: PlayerColor,
    pub text: TextStyle,
    pub scrollbar_width: Pixels,
    pub syntax: Arc<SyntaxTheme>,
    pub status: StatusColors,
    pub inlay_hints_style: HighlightStyle,
    pub edit_prediction_styles: EditPredictionStyles,
    pub unnecessary_code_fade: f32,
    pub show_underlines: bool,
}

impl Default for EditorStyle {
    fn default() -> Self {
        Self {
            background: Hsla::default(),
            border: Hsla::default(),
            local_player: PlayerColor::default(),
            text: TextStyle::default(),
            scrollbar_width: Pixels::default(),
            syntax: Default::default(),
            // HACK: Status colors don't have a real default.
            // We should look into removing the status colors from the editor
            // style and retrieve them directly from the theme.
            status: StatusColors::dark(),
            inlay_hints_style: HighlightStyle::default(),
            edit_prediction_styles: EditPredictionStyles {
                insertion: HighlightStyle::default(),
                whitespace: HighlightStyle::default(),
            },
            unnecessary_code_fade: Default::default(),
            show_underlines: true,
        }
    }
}

pub fn make_inlay_hints_style(cx: &App) -> HighlightStyle {
    let show_background = AllLanguageSettings::get_global(cx)
        .defaults
        .inlay_hints
        .show_background;

    let mut style = cx
        .theme()
        .syntax()
        .style_for_name("hint")
        .unwrap_or_default();

    if style.color.is_none() {
        style.color = Some(cx.theme().status().hint);
    }

    if !show_background {
        style.background_color = None;
        return style;
    }

    if style.background_color.is_none() {
        style.background_color = Some(cx.theme().status().hint_background);
    }

    style
}

pub fn make_suggestion_styles(cx: &App) -> EditPredictionStyles {
    EditPredictionStyles {
        insertion: HighlightStyle {
            color: Some(cx.theme().status().predictive),
            ..HighlightStyle::default()
        },
        whitespace: HighlightStyle {
            background_color: Some(cx.theme().status().created_background),
            ..HighlightStyle::default()
        },
    }
}

type CompletionId = usize;

pub(crate) enum EditDisplayMode {
    TabAccept,
    DiffPopover,
    Inline,
}

enum EditPrediction {
    Edit {
        // TODO could be a language::Anchor?
        edits: Vec<(Range<Anchor>, Arc<str>)>,
        /// Predicted cursor position as (anchor, offset_from_anchor).
        /// The anchor is in multibuffer coordinates; after applying edits,
        /// resolve the anchor and add the offset to get the final cursor position.
        cursor_position: Option<(Anchor, usize)>,
        edit_preview: Option<EditPreview>,
        display_mode: EditDisplayMode,
        snapshot: BufferSnapshot,
    },
    /// Move to a specific location in the active editor
    MoveWithin {
        target: Anchor,
        snapshot: BufferSnapshot,
    },
    /// Move to a specific location in a different editor (not the active one)
    MoveOutside {
        target: language::Anchor,
        snapshot: BufferSnapshot,
    },
}

struct EditPredictionState {
    inlay_ids: Vec<InlayId>,
    completion: EditPrediction,
    completion_id: Option<SharedString>,
    invalidation_range: Option<Range<Anchor>>,
}

enum EditPredictionSettings {
    Disabled,
    Enabled {
        show_in_menu: bool,
        preview_requires_modifier: bool,
    },
}

#[derive(Debug, Clone)]
struct InlineDiagnostic {
    message: SharedString,
    group_id: usize,
    is_primary: bool,
    start: Point,
    severity: lsp::DiagnosticSeverity,
}

pub enum MenuEditPredictionsPolicy {
    Never,
    ByProvider,
}

pub enum EditPredictionPreview {
    /// Modifier is not pressed
    Inactive { released_too_fast: bool },
    /// Modifier pressed
    Active {
        since: Instant,
        previous_scroll_position: Option<SharedScrollAnchor>,
    },
}

#[derive(Copy, Clone, Eq, PartialEq)]
enum EditPredictionKeybindSurface {
    Inline,
    CursorPopoverCompact,
    CursorPopoverExpanded,
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum EditPredictionKeybindAction {
    Accept,
    Preview,
}

struct EditPredictionKeybindDisplay {
    #[cfg(test)]
    accept_keystroke: Option<gpui::KeybindingKeystroke>,
    #[cfg(test)]
    preview_keystroke: Option<gpui::KeybindingKeystroke>,
    displayed_keystroke: Option<gpui::KeybindingKeystroke>,
    action: EditPredictionKeybindAction,
    missing_accept_keystroke: bool,
    show_hold_label: bool,
}

impl EditPredictionPreview {
    pub fn released_too_fast(&self) -> bool {
        match self {
            EditPredictionPreview::Inactive { released_too_fast } => *released_too_fast,
            EditPredictionPreview::Active { .. } => false,
        }
    }

    pub fn set_previous_scroll_position(&mut self, scroll_position: Option<SharedScrollAnchor>) {
        if let EditPredictionPreview::Active {
            previous_scroll_position,
            ..
        } = self
        {
            *previous_scroll_position = scroll_position;
        }
    }
}

pub struct ContextMenuOptions {
    pub min_entries_visible: usize,
    pub max_entries_visible: usize,
    pub placement: Option<ContextMenuPlacement>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContextMenuPlacement {
    Above,
    Below,
}

#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug, Default)]
struct EditorActionId(usize);

impl EditorActionId {
    pub fn post_inc(&mut self) -> Self {
        let answer = self.0;

        *self = Self(answer + 1);

        Self(answer)
    }
}

// type GetFieldEditorTheme = dyn Fn(&theme::Theme) -> theme::FieldEditor;
// type OverrideTextStyle = dyn Fn(&EditorStyle) -> Option<HighlightStyle>;

type BackgroundHighlight = (
    Arc<dyn Fn(&usize, &Theme) -> Hsla + Send + Sync>,
    Arc<[Range<Anchor>]>,
);
type GutterHighlight = (fn(&App) -> Hsla, Vec<Range<Anchor>>);

#[derive(Default)]
struct ScrollbarMarkerState {
    scrollbar_size: Size<Pixels>,
    dirty: bool,
    markers: Arc<[PaintQuad]>,
    pending_refresh: Option<Task<Result<()>>>,
}

impl ScrollbarMarkerState {
    fn should_refresh(&self, scrollbar_size: Size<Pixels>) -> bool {
        self.pending_refresh.is_none() && (self.scrollbar_size != scrollbar_size || self.dirty)
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum MinimapVisibility {
    Disabled,
    Enabled {
        /// The configuration currently present in the users settings.
        setting_configuration: bool,
        /// Whether to override the currently set visibility from the users setting.
        toggle_override: bool,
    },
}

impl MinimapVisibility {
    fn for_mode(mode: &EditorMode, cx: &App) -> Self {
        if mode.is_full() {
            Self::Enabled {
                setting_configuration: EditorSettings::get_global(cx).minimap.minimap_enabled(),
                toggle_override: false,
            }
        } else {
            Self::Disabled
        }
    }

    fn hidden(&self) -> Self {
        match *self {
            Self::Enabled {
                setting_configuration,
                ..
            } => Self::Enabled {
                setting_configuration,
                toggle_override: setting_configuration,
            },
            Self::Disabled => Self::Disabled,
        }
    }

    fn disabled(&self) -> bool {
        matches!(*self, Self::Disabled)
    }

    fn settings_visibility(&self) -> bool {
        match *self {
            Self::Enabled {
                setting_configuration,
                ..
            } => setting_configuration,
            _ => false,
        }
    }

    fn visible(&self) -> bool {
        match *self {
            Self::Enabled {
                setting_configuration,
                toggle_override,
            } => setting_configuration ^ toggle_override,
            _ => false,
        }
    }

    fn toggle_visibility(&self) -> Self {
        match *self {
            Self::Enabled {
                toggle_override,
                setting_configuration,
            } => Self::Enabled {
                setting_configuration,
                toggle_override: !toggle_override,
            },
            Self::Disabled => Self::Disabled,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BufferSerialization {
    All,
    NonDirtyBuffers,
}

impl BufferSerialization {
    fn new(restore_unsaved_buffers: bool) -> Self {
        if restore_unsaved_buffers {
            Self::All
        } else {
            Self::NonDirtyBuffers
        }
    }
}

/// Addons allow storing per-editor state in other crates (e.g. Vim)
pub trait Addon: 'static {
    fn extend_key_context(&self, _: &mut KeyContext, _: &App) {}

    fn render_buffer_header_controls(
        &self,
        _: &ExcerptBoundaryInfo,
        _: &language::BufferSnapshot,
        _: &Window,
        _: &App,
    ) -> Option<AnyElement> {
        None
    }

    fn override_status_for_buffer_id(&self, _: BufferId, _: &App) -> Option<FileStatus> {
        None
    }

    fn to_any(&self) -> &dyn std::any::Any;

    fn to_any_mut(&mut self) -> Option<&mut dyn std::any::Any> {
        None
    }
}

struct ChangeLocation {
    current: Option<Vec<Anchor>>,
    original: Vec<Anchor>,
}
impl ChangeLocation {
    fn locations(&self) -> &[Anchor] {
        self.current.as_ref().unwrap_or(&self.original)
    }
}

/// A set of caret positions, registered when the editor was edited.
pub struct ChangeList {
    changes: Vec<ChangeLocation>,
    /// Currently "selected" change.
    position: Option<usize>,
}

impl ChangeList {
    pub fn new() -> Self {
        Self {
            changes: Vec::new(),
            position: None,
        }
    }

    /// Moves to the next change in the list (based on the direction given) and returns the caret positions for the next change.
    /// If reaches the end of the list in the direction, returns the corresponding change until called for a different direction.
    pub fn next_change(&mut self, count: usize, direction: Direction) -> Option<&[Anchor]> {
        if self.changes.is_empty() {
            return None;
        }

        let prev = self.position.unwrap_or(self.changes.len());
        let next = if direction == Direction::Prev {
            prev.saturating_sub(count)
        } else {
            (prev + count).min(self.changes.len() - 1)
        };
        self.position = Some(next);
        self.changes.get(next).map(|change| change.locations())
    }

    /// Adds a new change to the list, resetting the change list position.
    pub fn push_to_change_list(&mut self, group: bool, new_positions: Vec<Anchor>) {
        self.position.take();
        if let Some(last) = self.changes.last_mut()
            && group
        {
            last.current = Some(new_positions)
        } else {
            self.changes.push(ChangeLocation {
                original: new_positions,
                current: None,
            });
        }
    }

    pub fn last(&self) -> Option<&[Anchor]> {
        self.changes.last().map(|change| change.locations())
    }

    pub fn last_before_grouping(&self) -> Option<&[Anchor]> {
        self.changes.last().map(|change| change.original.as_slice())
    }

    pub fn invert_last_group(&mut self) {
        if let Some(last) = self.changes.last_mut()
            && let Some(current) = last.current.as_mut()
        {
            mem::swap(&mut last.original, current);
        }
    }
}

#[derive(Clone)]
struct InlineBlamePopoverState {
    scroll_handle: ScrollHandle,
    commit_message: Option<ParsedCommitMessage>,
    markdown: Entity<Markdown>,
}

struct InlineBlamePopover {
    position: gpui::Point<Pixels>,
    hide_task: Option<Task<()>>,
    popover_bounds: Option<Bounds<Pixels>>,
    popover_state: InlineBlamePopoverState,
    keyboard_grace: bool,
}

enum SelectionDragState {
    /// State when no drag related activity is detected.
    None,
    /// State when the mouse is down on a selection that is about to be dragged.
    ReadyToDrag {
        selection: Selection<Anchor>,
        click_position: gpui::Point<Pixels>,
        mouse_down_time: Instant,
    },
    /// State when the mouse is dragging the selection in the editor.
    Dragging {
        selection: Selection<Anchor>,
        drop_cursor: Selection<Anchor>,
        hide_drop_cursor: bool,
    },
}

enum ColumnarSelectionState {
    FromMouse {
        selection_tail: Anchor,
        display_point: Option<DisplayPoint>,
    },
    FromSelection {
        selection_tail: Anchor,
    },
}

/// Represents a breakpoint indicator that shows up when hovering over lines in the gutter that don't have
/// a breakpoint on them.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct PhantomBreakpointIndicator {
    display_row: DisplayRow,
    /// There's a small debounce between hovering over the line and showing the indicator.
    /// We don't want to show the indicator when moving the mouse from editor to e.g. project panel.
    is_active: bool,
    collides_with_existing_breakpoint: bool,
}

/// Represents a diff review button indicator that shows up when hovering over lines in the gutter
/// in diff view mode.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PhantomDiffReviewIndicator {
    /// The starting anchor of the selection (or the only row if not dragging).
    pub start: Anchor,
    /// The ending anchor of the selection. Equal to start_anchor for single-line selection.
    pub end: Anchor,
    /// There's a small debounce between hovering over the line and showing the indicator.
    /// We don't want to show the indicator when moving the mouse from editor to e.g. project panel.
    pub is_active: bool,
}

#[derive(Clone, Debug)]
pub(crate) struct DiffReviewDragState {
    pub start_anchor: Anchor,
    pub current_anchor: Anchor,
}

impl DiffReviewDragState {
    pub fn row_range(&self, snapshot: &DisplaySnapshot) -> std::ops::RangeInclusive<DisplayRow> {
        let start = self.start_anchor.to_display_point(snapshot).row();
        let current = self.current_anchor.to_display_point(snapshot).row();

        (start..=current).sorted()
    }
}

/// Identifies a specific hunk in the diff buffer.
/// Used as a key to group comments by their location.
#[derive(Clone, Debug)]
pub struct DiffHunkKey {
    /// The file path (relative to worktree) this hunk belongs to.
    pub file_path: Arc<util::rel_path::RelPath>,
    /// An anchor at the start of the hunk. This tracks position as the buffer changes.
    pub hunk_start_anchor: Anchor,
}

/// A review comment stored locally before being sent to the Agent panel.
#[derive(Clone)]
pub struct StoredReviewComment {
    /// Unique identifier for this comment (for edit/delete operations).
    pub id: usize,
    /// The comment text entered by the user.
    pub comment: String,
    /// Anchors for the code range being reviewed.
    pub range: Range<Anchor>,
    /// Timestamp when the comment was created (for chronological ordering).
    pub created_at: Instant,
    /// Whether this comment is currently being edited inline.
    pub is_editing: bool,
}

impl StoredReviewComment {
    pub fn new(id: usize, comment: String, anchor_range: Range<Anchor>) -> Self {
        Self {
            id,
            comment,
            range: anchor_range,
            created_at: Instant::now(),
            is_editing: false,
        }
    }
}

/// Represents an active diff review overlay that appears when clicking the "Add Review" button.
pub(crate) struct DiffReviewOverlay {
    pub anchor_range: Range<Anchor>,
    /// The block ID for the overlay.
    pub block_id: CustomBlockId,
    /// The editor entity for the review input.
    pub prompt_editor: Entity<Editor>,
    /// The hunk key this overlay belongs to.
    pub hunk_key: DiffHunkKey,
    /// Whether the comments section is expanded.
    pub comments_expanded: bool,
    /// Editors for comments currently being edited inline.
    /// Key: comment ID, Value: Editor entity for inline editing.
    pub inline_edit_editors: HashMap<usize, Entity<Editor>>,
    /// Subscriptions for inline edit editors' action handlers.
    /// Key: comment ID, Value: Subscription keeping the Newline action handler alive.
    pub inline_edit_subscriptions: HashMap<usize, Subscription>,
    /// The current user's avatar URI for display in comment rows.
    pub user_avatar_uri: Option<SharedUri>,
    /// Subscription to keep the action handler alive.
    _subscription: Subscription,
}

/// Zed's primary implementation of text input, allowing users to edit a [`MultiBuffer`].
///
/// See the [module level documentation](self) for more information.
pub struct Editor {
    focus_handle: FocusHandle,
    last_focused_descendant: Option<WeakFocusHandle>,
    /// The text buffer being edited
    buffer: Entity<MultiBuffer>,
    /// Map of how text in the buffer should be displayed.
    /// Handles soft wraps, folds, fake inlay text insertions, etc.
    pub display_map: Entity<DisplayMap>,
    placeholder_display_map: Option<Entity<DisplayMap>>,
    pub selections: SelectionsCollection,
    pub scroll_manager: ScrollManager,
    /// When inline assist editors are linked, they all render cursors because
    /// typing enters text into each of them, even the ones that aren't focused.
    pub(crate) show_cursor_when_unfocused: bool,
    columnar_selection_state: Option<ColumnarSelectionState>,
    add_selections_state: Option<AddSelectionsState>,
    select_next_state: Option<SelectNextState>,
    select_prev_state: Option<SelectNextState>,
    selection_history: SelectionHistory,
    defer_selection_effects: bool,
    deferred_selection_effects_state: Option<DeferredSelectionEffectsState>,
    autoclose_regions: Vec<AutocloseRegion>,
    snippet_stack: InvalidationStack<SnippetState>,
    select_syntax_node_history: SelectSyntaxNodeHistory,
    ime_transaction: Option<TransactionId>,
    pub diagnostics_max_severity: DiagnosticSeverity,
    active_diagnostics: ActiveDiagnostic,
    show_inline_diagnostics: bool,
    inline_diagnostics_update: Task<()>,
    inline_diagnostics_enabled: bool,
    diagnostics_enabled: bool,
    word_completions_enabled: bool,
    inline_diagnostics: Vec<(Anchor, InlineDiagnostic)>,
    soft_wrap_mode_override: Option<language_settings::SoftWrap>,
    hard_wrap: Option<usize>,
    project: Option<Entity<Project>>,
    semantics_provider: Option<Rc<dyn SemanticsProvider>>,
    completion_provider: Option<Rc<dyn CompletionProvider>>,
    collaboration_hub: Option<Box<dyn CollaborationHub>>,
    blink_manager: Entity<BlinkManager>,
    show_cursor_names: bool,
    hovered_cursors: HashMap<HoveredCursor, Task<()>>,
    pub show_local_selections: bool,
    mode: EditorMode,
    show_breadcrumbs: bool,
    show_gutter: bool,
    show_scrollbars: ScrollbarAxes,
    minimap_visibility: MinimapVisibility,
    offset_content: bool,
    disable_expand_excerpt_buttons: bool,
    delegate_expand_excerpts: bool,
    delegate_stage_and_restore: bool,
    delegate_open_excerpts: bool,
    enable_lsp_data: bool,
    enable_runnables: bool,
    show_line_numbers: Option<bool>,
    use_relative_line_numbers: Option<bool>,
    show_git_diff_gutter: Option<bool>,
    show_code_actions: Option<bool>,
    show_runnables: Option<bool>,
    show_breakpoints: Option<bool>,
    show_diff_review_button: bool,
    show_wrap_guides: Option<bool>,
    show_indent_guides: Option<bool>,
    buffers_with_disabled_indent_guides: HashSet<BufferId>,
    highlight_order: usize,
    highlighted_rows: HashMap<TypeId, Vec<RowHighlight>>,
    background_highlights: HashMap<HighlightKey, BackgroundHighlight>,
    gutter_highlights: HashMap<TypeId, GutterHighlight>,
    scrollbar_marker_state: ScrollbarMarkerState,
    active_indent_guides_state: ActiveIndentGuidesState,
    nav_history: Option<ItemNavHistory>,
    context_menu: RefCell<Option<CodeContextMenu>>,
    context_menu_options: Option<ContextMenuOptions>,
    mouse_context_menu: Option<MouseContextMenu>,
    completion_tasks: Vec<(CompletionId, Task<()>)>,
    inline_blame_popover: Option<InlineBlamePopover>,
    inline_blame_popover_show_task: Option<Task<()>>,
    signature_help_state: SignatureHelpState,
    auto_signature_help: Option<bool>,
    find_all_references_task_sources: Vec<Anchor>,
    next_completion_id: CompletionId,
    available_code_actions: Option<(Location, Rc<[AvailableCodeAction]>)>,
    code_actions_task: Option<Task<Result<()>>>,
    quick_selection_highlight_task: Option<(Range<Anchor>, Task<()>)>,
    debounced_selection_highlight_task: Option<(Range<Anchor>, Task<()>)>,
    debounced_selection_highlight_complete: bool,
    document_highlights_task: Option<Task<()>>,
    linked_editing_range_task: Option<Task<Option<()>>>,
    linked_edit_ranges: linked_editing_ranges::LinkedEditingRanges,
    pending_rename: Option<RenameState>,
    searchable: bool,
    cursor_shape: CursorShape,
    /// Whether the cursor is offset one character to the left when something is
    /// selected (needed for vim visual mode)
    cursor_offset_on_selection: bool,
    current_line_highlight: Option<CurrentLineHighlight>,
    /// Whether to collapse search match ranges to just their start position.
    /// When true, navigating to a match positions the cursor at the match
    /// without selecting the matched text.
    collapse_matches: bool,
    autoindent_mode: Option<AutoindentMode>,
    workspace: Option<(WeakEntity<Workspace>, Option<WorkspaceId>)>,
    input_enabled: bool,
    expects_character_input: bool,
    use_modal_editing: bool,
    read_only: bool,
    leader_id: Option<CollaboratorId>,
    remote_id: Option<ViewId>,
    pub hover_state: HoverState,
    pending_mouse_down: Option<Rc<RefCell<Option<MouseDownEvent>>>>,
    prev_pressure_stage: Option<PressureStage>,
    gutter_hovered: bool,
    hovered_link_state: Option<HoveredLinkState>,
    edit_prediction_provider: Option<RegisteredEditPredictionDelegate>,
    code_action_providers: Vec<Rc<dyn CodeActionProvider>>,
    active_edit_prediction: Option<EditPredictionState>,
    /// Used to prevent flickering as the user types while the menu is open
    stale_edit_prediction_in_menu: Option<EditPredictionState>,
    edit_prediction_settings: EditPredictionSettings,
    edit_predictions_hidden_for_vim_mode: bool,
    show_edit_predictions_override: Option<bool>,
    show_completions_on_input_override: Option<bool>,
    menu_edit_predictions_policy: MenuEditPredictionsPolicy,
    edit_prediction_preview: EditPredictionPreview,
    in_leading_whitespace: bool,
    next_inlay_id: usize,
    next_color_inlay_id: usize,
    _subscriptions: Vec<Subscription>,
    pixel_position_of_newest_cursor: Option<gpui::Point<Pixels>>,
    gutter_dimensions: GutterDimensions,
    style: Option<EditorStyle>,
    text_style_refinement: Option<TextStyleRefinement>,
    next_editor_action_id: EditorActionId,
    editor_actions: Rc<
        RefCell<BTreeMap<EditorActionId, Box<dyn Fn(&Editor, &mut Window, &mut Context<Self>)>>>,
    >,
    use_autoclose: bool,
    use_auto_surround: bool,
    use_selection_highlight: bool,
    auto_replace_emoji_shortcode: bool,
    jsx_tag_auto_close_enabled_in_any_buffer: bool,
    show_git_blame_gutter: bool,
    show_git_blame_inline: bool,
    show_git_blame_inline_delay_task: Option<Task<()>>,
    git_blame_inline_enabled: bool,
    render_diff_hunk_controls: RenderDiffHunkControlsFn,
    buffer_serialization: Option<BufferSerialization>,
    show_selection_menu: Option<bool>,
    blame: Option<Entity<GitBlame>>,
    blame_subscription: Option<Subscription>,
    custom_context_menu: Option<
        Box<
            dyn 'static
                + Fn(
                    &mut Self,
                    DisplayPoint,
                    &mut Window,
                    &mut Context<Self>,
                ) -> Option<Entity<ui::ContextMenu>>,
        >,
    >,
    last_bounds: Option<Bounds<Pixels>>,
    last_position_map: Option<Rc<PositionMap>>,
    expect_bounds_change: Option<Bounds<Pixels>>,
    runnables: RunnableData,
    breakpoint_store: Option<Entity<BreakpointStore>>,
    gutter_breakpoint_indicator: (Option<PhantomBreakpointIndicator>, Option<Task<()>>),
    pub(crate) gutter_diff_review_indicator: (Option<PhantomDiffReviewIndicator>, Option<Task<()>>),
    pub(crate) diff_review_drag_state: Option<DiffReviewDragState>,
    /// Active diff review overlays. Multiple overlays can be open simultaneously
    /// when hunks have comments stored.
    pub(crate) diff_review_overlays: Vec<DiffReviewOverlay>,
    /// Stored review comments grouped by hunk.
    /// Uses a Vec instead of HashMap because DiffHunkKey contains an Anchor
    /// which doesn't implement Hash/Eq in a way suitable for HashMap keys.
    stored_review_comments: Vec<(DiffHunkKey, Vec<StoredReviewComment>)>,
    /// Counter for generating unique comment IDs.
    next_review_comment_id: usize,
    hovered_diff_hunk_row: Option<DisplayRow>,
    pull_diagnostics_task: Task<()>,
    in_project_search: bool,
    previous_search_ranges: Option<Arc<[Range<Anchor>]>>,
    breadcrumb_header: Option<String>,
    focused_block: Option<FocusedBlock>,
    next_scroll_position: NextScrollCursorCenterTopBottom,
    addons: HashMap<TypeId, Box<dyn Addon>>,
    registered_buffers: HashMap<BufferId, OpenLspBufferHandle>,
    load_diff_task: Option<Shared<Task<()>>>,
    /// Whether we are temporarily displaying a diff other than git's
    temporary_diff_override: bool,
    selection_mark_mode: bool,
    toggle_fold_multiple_buffers: Task<()>,
    _scroll_cursor_center_top_bottom_task: Task<()>,
    serialize_selections: Task<()>,
    serialize_folds: Task<()>,
    mouse_cursor_hidden: bool,
    minimap: Option<Entity<Self>>,
    hide_mouse_mode: HideMouseMode,
    pub change_list: ChangeList,
    inline_value_cache: InlineValueCache,
    number_deleted_lines: bool,

    selection_drag_state: SelectionDragState,
    colors: Option<LspColorData>,
    post_scroll_update: Task<()>,
    refresh_colors_task: Task<()>,
    use_document_folding_ranges: bool,
    refresh_folding_ranges_task: Task<()>,
    inlay_hints: Option<LspInlayHintData>,
    folding_newlines: Task<()>,
    select_next_is_case_sensitive: Option<bool>,
    pub lookup_key: Option<Box<dyn Any + Send + Sync>>,
    on_local_selections_changed:
        Option<Box<dyn Fn(Point, &mut Window, &mut Context<Self>) + 'static>>,
    suppress_selection_callback: bool,
    applicable_language_settings: HashMap<Option<LanguageName>, LanguageSettings>,
    accent_data: Option<AccentData>,
    bracket_fetched_tree_sitter_chunks: HashMap<Range<text::Anchor>, HashSet<Range<BufferRow>>>,
    semantic_token_state: SemanticTokenState,
    pub(crate) refresh_matching_bracket_highlights_task: Task<()>,
    refresh_document_symbols_task: Shared<Task<()>>,
    lsp_document_symbols: HashMap<BufferId, Vec<OutlineItem<text::Anchor>>>,
    refresh_outline_symbols_at_cursor_at_cursor_task: Task<()>,
    outline_symbols_at_cursor: Option<(BufferId, Vec<OutlineItem<Anchor>>)>,
    sticky_headers_task: Task<()>,
    sticky_headers: Option<Vec<OutlineItem<Anchor>>>,
    pub(crate) colorize_brackets_task: Task<()>,
}

#[derive(Debug, PartialEq)]
struct AccentData {
    colors: AccentColors,
    overrides: Vec<SharedString>,
}

fn debounce_value(debounce_ms: u64) -> Option<Duration> {
    if debounce_ms > 0 {
        Some(Duration::from_millis(debounce_ms))
    } else {
        None
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
enum NextScrollCursorCenterTopBottom {
    #[default]
    Center,
    Top,
    Bottom,
}

impl NextScrollCursorCenterTopBottom {
    fn next(&self) -> Self {
        match self {
            Self::Center => Self::Top,
            Self::Top => Self::Bottom,
            Self::Bottom => Self::Center,
        }
    }
}

#[derive(Clone)]
pub struct EditorSnapshot {
    pub mode: EditorMode,
    show_gutter: bool,
    offset_content: bool,
    show_line_numbers: Option<bool>,
    number_deleted_lines: bool,
    show_git_diff_gutter: Option<bool>,
    show_code_actions: Option<bool>,
    show_runnables: Option<bool>,
    show_breakpoints: Option<bool>,
    git_blame_gutter_max_author_length: Option<usize>,
    pub display_snapshot: DisplaySnapshot,
    pub placeholder_display_snapshot: Option<DisplaySnapshot>,
    is_focused: bool,
    scroll_anchor: SharedScrollAnchor,
    ongoing_scroll: OngoingScroll,
    current_line_highlight: CurrentLineHighlight,
    gutter_hovered: bool,
    semantic_tokens_enabled: bool,
}

#[derive(Default, Debug, Clone, Copy)]
pub struct GutterDimensions {
    pub left_padding: Pixels,
    pub right_padding: Pixels,
    pub width: Pixels,
    pub margin: Pixels,
    pub git_blame_entries_width: Option<Pixels>,
}

impl GutterDimensions {
    fn default_with_margin(font_id: FontId, font_size: Pixels, cx: &App) -> Self {
        Self {
            margin: Self::default_gutter_margin(font_id, font_size, cx),
            ..Default::default()
        }
    }

    fn default_gutter_margin(font_id: FontId, font_size: Pixels, cx: &App) -> Pixels {
        -cx.text_system().descent(font_id, font_size)
    }
    /// The full width of the space taken up by the gutter.
    pub fn full_width(&self) -> Pixels {
        self.margin + self.width
    }

    /// The width of the space reserved for the fold indicators,
    /// use alongside 'justify_end' and `gutter_width` to
    /// right align content with the line numbers
    pub fn fold_area_width(&self) -> Pixels {
        self.margin + self.right_padding
    }
}

struct CharacterDimensions {
    em_width: Pixels,
    em_advance: Pixels,
    line_height: Pixels,
}

#[derive(Debug)]
pub struct RemoteSelection {
    pub replica_id: ReplicaId,
    pub selection: Selection<Anchor>,
    pub cursor_shape: CursorShape,
    pub collaborator_id: CollaboratorId,
    pub line_mode: bool,
    pub user_name: Option<SharedString>,
    pub color: PlayerColor,
}

#[derive(Clone, Debug)]
struct SelectionHistoryEntry {
    selections: Arc<[Selection<Anchor>]>,
    select_next_state: Option<SelectNextState>,
    select_prev_state: Option<SelectNextState>,
    add_selections_state: Option<AddSelectionsState>,
}

#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
enum SelectionHistoryMode {
    #[default]
    Normal,
    Undoing,
    Redoing,
    Skipping,
}

#[derive(Clone, PartialEq, Eq, Hash)]
struct HoveredCursor {
    replica_id: ReplicaId,
    selection_id: usize,
}

#[derive(Debug)]
/// SelectionEffects controls the side-effects of updating the selection.
///
/// The default behaviour does "what you mostly want":
/// - it pushes to the nav history if the cursor moved by >10 lines
/// - it re-triggers completion requests
/// - it scrolls to fit
///
/// You might want to modify these behaviours. For example when doing a "jump"
/// like go to definition, we always want to add to nav history; but when scrolling
/// in vim mode we never do.
///
/// Similarly, you might want to disable scrolling if you don't want the viewport to
/// move.
#[derive(Clone)]
pub struct SelectionEffects {
    nav_history: Option<bool>,
    completions: bool,
    scroll: Option<Autoscroll>,
}

impl Default for SelectionEffects {
    fn default() -> Self {
        Self {
            nav_history: None,
            completions: true,
            scroll: Some(Autoscroll::fit()),
        }
    }
}
impl SelectionEffects {
    pub fn scroll(scroll: Autoscroll) -> Self {
        Self {
            scroll: Some(scroll),
            ..Default::default()
        }
    }

    pub fn no_scroll() -> Self {
        Self {
            scroll: None,
            ..Default::default()
        }
    }

    pub fn completions(self, completions: bool) -> Self {
        Self {
            completions,
            ..self
        }
    }

    pub fn nav_history(self, nav_history: bool) -> Self {
        Self {
            nav_history: Some(nav_history),
            ..self
        }
    }
}

struct DeferredSelectionEffectsState {
    changed: bool,
    effects: SelectionEffects,
    old_cursor_position: Anchor,
    history_entry: SelectionHistoryEntry,
}

#[derive(Default)]
struct SelectionHistory {
    #[allow(clippy::type_complexity)]
    selections_by_transaction:
        HashMap<TransactionId, (Arc<[Selection<Anchor>]>, Option<Arc<[Selection<Anchor>]>>)>,
    mode: SelectionHistoryMode,
    undo_stack: VecDeque<SelectionHistoryEntry>,
    redo_stack: VecDeque<SelectionHistoryEntry>,
}

impl SelectionHistory {
    #[track_caller]
    fn insert_transaction(
        &mut self,
        transaction_id: TransactionId,
        selections: Arc<[Selection<Anchor>]>,
    ) {
        if selections.is_empty() {
            log::error!(
                "SelectionHistory::insert_transaction called with empty selections. Caller: {}",
                std::panic::Location::caller()
            );
            return;
        }
        self.selections_by_transaction
            .insert(transaction_id, (selections, None));
    }

    #[allow(clippy::type_complexity)]
    fn transaction(
        &self,
        transaction_id: TransactionId,
    ) -> Option<&(Arc<[Selection<Anchor>]>, Option<Arc<[Selection<Anchor>]>>)> {
        self.selections_by_transaction.get(&transaction_id)
    }

    #[allow(clippy::type_complexity)]
    fn transaction_mut(
        &mut self,
        transaction_id: TransactionId,
    ) -> Option<&mut (Arc<[Selection<Anchor>]>, Option<Arc<[Selection<Anchor>]>>)> {
        self.selections_by_transaction.get_mut(&transaction_id)
    }

    fn push(&mut self, entry: SelectionHistoryEntry) {
        if !entry.selections.is_empty() {
            match self.mode {
                SelectionHistoryMode::Normal => {
                    self.push_undo(entry);
                    self.redo_stack.clear();
                }
                SelectionHistoryMode::Undoing => self.push_redo(entry),
                SelectionHistoryMode::Redoing => self.push_undo(entry),
                SelectionHistoryMode::Skipping => {}
            }
        }
    }

    fn push_undo(&mut self, entry: SelectionHistoryEntry) {
        if self
            .undo_stack
            .back()
            .is_none_or(|e| e.selections != entry.selections)
        {
            self.undo_stack.push_back(entry);
            if self.undo_stack.len() > MAX_SELECTION_HISTORY_LEN {
                self.undo_stack.pop_front();
            }
        }
    }

    fn push_redo(&mut self, entry: SelectionHistoryEntry) {
        if self
            .redo_stack
            .back()
            .is_none_or(|e| e.selections != entry.selections)
        {
            self.redo_stack.push_back(entry);
            if self.redo_stack.len() > MAX_SELECTION_HISTORY_LEN {
                self.redo_stack.pop_front();
            }
        }
    }
}

#[derive(Clone, Copy)]
pub struct RowHighlightOptions {
    pub autoscroll: bool,
    pub include_gutter: bool,
}

impl Default for RowHighlightOptions {
    fn default() -> Self {
        Self {
            autoscroll: Default::default(),
            include_gutter: true,
        }
    }
}

struct RowHighlight {
    index: usize,
    range: Range<Anchor>,
    color: Hsla,
    options: RowHighlightOptions,
    type_id: TypeId,
}

#[derive(Clone, Debug)]
struct AddSelectionsState {
    groups: Vec<AddSelectionsGroup>,
}

#[derive(Clone, Debug)]
struct AddSelectionsGroup {
    above: bool,
    stack: Vec<usize>,
}

#[derive(Clone)]
struct SelectNextState {
    query: AhoCorasick,
    wordwise: bool,
    done: bool,
}

impl std::fmt::Debug for SelectNextState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(std::any::type_name::<Self>())
            .field("wordwise", &self.wordwise)
            .field("done", &self.done)
            .finish()
    }
}

#[derive(Debug)]
struct AutocloseRegion {
    selection_id: usize,
    range: Range<Anchor>,
    pair: BracketPair,
}

#[derive(Debug)]
struct SnippetState {
    ranges: Vec<Vec<Range<Anchor>>>,
    active_index: usize,
    choices: Vec<Option<Vec<String>>>,
}

#[doc(hidden)]
pub struct RenameState {
    pub range: Range<Anchor>,
    pub old_name: Arc<str>,
    pub editor: Entity<Editor>,
    block_id: CustomBlockId,
}

struct InvalidationStack<T>(Vec<T>);

struct RegisteredEditPredictionDelegate {
    provider: Arc<dyn EditPredictionDelegateHandle>,
    _subscription: Subscription,
}

#[derive(Debug, PartialEq, Eq)]
pub struct ActiveDiagnosticGroup {
    pub active_range: Range<Anchor>,
    pub active_message: String,
    pub group_id: usize,
    pub blocks: HashSet<CustomBlockId>,
}

#[derive(Debug, PartialEq, Eq)]

pub(crate) enum ActiveDiagnostic {
    None,
    All,
    Group(ActiveDiagnosticGroup),
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ClipboardSelection {
    /// The number of bytes in this selection.
    pub len: usize,
    /// Whether this was a full-line selection.
    pub is_entire_line: bool,
    /// The indentation of the first line when this content was originally copied.
    pub first_line_indent: u32,
    #[serde(default)]
    pub file_path: Option<PathBuf>,
    #[serde(default)]
    pub line_range: Option<RangeInclusive<u32>>,
}

impl ClipboardSelection {
    pub fn for_buffer(
        len: usize,
        is_entire_line: bool,
        range: Range<Point>,
        buffer: &MultiBufferSnapshot,
        project: Option<&Entity<Project>>,
        cx: &App,
    ) -> Self {
        let first_line_indent = buffer
            .indent_size_for_line(MultiBufferRow(range.start.row))
            .len;

        let file_path = util::maybe!({
            let project = project?.read(cx);
            let file = buffer.file_at(range.start)?;
            let project_path = ProjectPath {
                worktree_id: file.worktree_id(cx),
                path: file.path().clone(),
            };
            project.absolute_path(&project_path, cx)
        });

        let line_range = if file_path.is_some() {
            buffer
                .range_to_buffer_range(range)
                .map(|(_, buffer_range)| buffer_range.start.row..=buffer_range.end.row)
        } else {
            None
        };

        Self {
            len,
            is_entire_line,
            first_line_indent,
            file_path,
            line_range,
        }
    }
}

// selections, scroll behavior, was newest selection reversed
type SelectSyntaxNodeHistoryState = (
    Box<[Selection<Anchor>]>,
    SelectSyntaxNodeScrollBehavior,
    bool,
);

#[derive(Default)]
struct SelectSyntaxNodeHistory {
    stack: Vec<SelectSyntaxNodeHistoryState>,
    // disable temporarily to allow changing selections without losing the stack
    pub disable_clearing: bool,
}

impl SelectSyntaxNodeHistory {
    pub fn try_clear(&mut self) {
        if !self.disable_clearing {
            self.stack.clear();
        }
    }

    pub fn push(&mut self, selection: SelectSyntaxNodeHistoryState) {
        self.stack.push(selection);
    }

    pub fn pop(&mut self) -> Option<SelectSyntaxNodeHistoryState> {
        self.stack.pop()
    }
}

enum SelectSyntaxNodeScrollBehavior {
    CursorTop,
    FitSelection,
    CursorBottom,
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct NavigationData {
    cursor_anchor: Anchor,
    cursor_position: Point,
    scroll_anchor: ScrollAnchor,
    scroll_top_row: u32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GotoDefinitionKind {
    Symbol,
    Declaration,
    Type,
    Implementation,
}

pub enum FormatTarget {
    Buffers(HashSet<Entity<Buffer>>),
    Ranges(Vec<Range<MultiBufferPoint>>),
}

pub(crate) struct FocusedBlock {
    id: BlockId,
    focus_handle: WeakFocusHandle,
}

#[derive(Clone, Debug)]
pub enum JumpData {
    MultiBufferRow {
        row: MultiBufferRow,
        line_offset_from_top: u32,
    },
    MultiBufferPoint {
        anchor: language::Anchor,
        position: Point,
        line_offset_from_top: u32,
    },
}

pub enum MultibufferSelectionMode {
    First,
    All,
}

#[derive(Clone, Copy, Debug, Default)]
pub struct RewrapOptions {
    pub override_language_settings: bool,
    pub preserve_existing_whitespace: bool,
    pub line_length: Option<usize>,
}

impl Editor {
    pub fn single_line(window: &mut Window, cx: &mut Context<Self>) -> Self {
        let buffer = cx.new(|cx| Buffer::local("", cx));
        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
        Self::new(EditorMode::SingleLine, buffer, None, window, cx)
    }

    pub fn multi_line(window: &mut Window, cx: &mut Context<Self>) -> Self {
        let buffer = cx.new(|cx| Buffer::local("", cx));
        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
        Self::new(EditorMode::full(), buffer, None, window, cx)
    }

    pub fn auto_height(
        min_lines: usize,
        max_lines: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        let buffer = cx.new(|cx| Buffer::local("", cx));
        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
        Self::new(
            EditorMode::AutoHeight {
                min_lines,
                max_lines: Some(max_lines),
            },
            buffer,
            None,
            window,
            cx,
        )
    }

    /// Creates a new auto-height editor with a minimum number of lines but no maximum.
    /// The editor grows as tall as needed to fit its content.
    pub fn auto_height_unbounded(
        min_lines: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        let buffer = cx.new(|cx| Buffer::local("", cx));
        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
        Self::new(
            EditorMode::AutoHeight {
                min_lines,
                max_lines: None,
            },
            buffer,
            None,
            window,
            cx,
        )
    }

    pub fn for_buffer(
        buffer: Entity<Buffer>,
        project: Option<Entity<Project>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));
        Self::new(EditorMode::full(), buffer, project, window, cx)
    }

    pub fn for_multibuffer(
        buffer: Entity<MultiBuffer>,
        project: Option<Entity<Project>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        Self::new(EditorMode::full(), buffer, project, window, cx)
    }

    pub fn clone(&self, window: &mut Window, cx: &mut Context<Self>) -> Self {
        let mut clone = Self::new(
            self.mode.clone(),
            self.buffer.clone(),
            self.project.clone(),
            window,
            cx,
        );
        let my_snapshot = self.display_map.update(cx, |display_map, cx| {
            let snapshot = display_map.snapshot(cx);
            clone.display_map.update(cx, |display_map, cx| {
                display_map.set_state(&snapshot, cx);
            });
            snapshot
        });
        let clone_snapshot = clone.display_map.update(cx, |map, cx| map.snapshot(cx));
        clone.folds_did_change(cx);
        clone.selections.clone_state(&self.selections);
        clone
            .scroll_manager
            .clone_state(&self.scroll_manager, &my_snapshot, &clone_snapshot, cx);
        clone.searchable = self.searchable;
        clone.read_only = self.read_only;
        clone.buffers_with_disabled_indent_guides =
            self.buffers_with_disabled_indent_guides.clone();
        clone
    }

    pub fn new(
        mode: EditorMode,
        buffer: Entity<MultiBuffer>,
        project: Option<Entity<Project>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        Editor::new_internal(mode, buffer, project, None, window, cx)
    }

    pub fn refresh_sticky_headers(
        &mut self,
        display_snapshot: &DisplaySnapshot,
        cx: &mut Context<Editor>,
    ) {
        if !self.mode.is_full() {
            return;
        }
        let multi_buffer = display_snapshot.buffer_snapshot().clone();
        let scroll_anchor = self
            .scroll_manager
            .native_anchor(display_snapshot, cx)
            .anchor;
        let Some(buffer_snapshot) = multi_buffer.as_singleton() else {
            return;
        };

        let buffer = buffer_snapshot.clone();
        let Some((buffer_visible_start, _)) = multi_buffer.anchor_to_buffer_anchor(scroll_anchor)
        else {
            return;
        };
        let buffer_visible_start = buffer_visible_start.to_point(&buffer);
        let max_row = buffer.max_point().row;
        let start_row = buffer_visible_start.row.min(max_row);
        let end_row = (buffer_visible_start.row + 10).min(max_row);

        let syntax = self.style(cx).syntax.clone();
        let background_task = cx.background_spawn(async move {
            buffer
                .outline_items_containing(
                    Point::new(start_row, 0)..Point::new(end_row, 0),
                    true,
                    Some(syntax.as_ref()),
                )
                .into_iter()
                .filter_map(|outline_item| {
                    Some(OutlineItem {
                        depth: outline_item.depth,
                        range: multi_buffer
                            .buffer_anchor_range_to_anchor_range(outline_item.range)?,
                        source_range_for_text: multi_buffer.buffer_anchor_range_to_anchor_range(
                            outline_item.source_range_for_text,
                        )?,
                        text: outline_item.text,
                        highlight_ranges: outline_item.highlight_ranges,
                        name_ranges: outline_item.name_ranges,
                        body_range: outline_item.body_range.and_then(|range| {
                            multi_buffer.buffer_anchor_range_to_anchor_range(range)
                        }),
                        annotation_range: outline_item.annotation_range.and_then(|range| {
                            multi_buffer.buffer_anchor_range_to_anchor_range(range)
                        }),
                    })
                })
                .collect()
        });
        self.sticky_headers_task = cx.spawn(async move |this, cx| {
            let sticky_headers = background_task.await;
            this.update(cx, |this, cx| {
                this.sticky_headers = Some(sticky_headers);
                cx.notify();
            })
            .ok();
        });
    }

    fn new_internal(
        mode: EditorMode,
        multi_buffer: Entity<MultiBuffer>,
        project: Option<Entity<Project>>,
        display_map: Option<Entity<DisplayMap>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        debug_assert!(
            display_map.is_none() || mode.is_minimap(),
            "Providing a display map for a new editor is only intended for the minimap and might have unintended side effects otherwise!"
        );

        let full_mode = mode.is_full();
        let is_minimap = mode.is_minimap();
        let diagnostics_max_severity = if full_mode {
            EditorSettings::get_global(cx)
                .diagnostics_max_severity
                .unwrap_or(DiagnosticSeverity::Hint)
        } else {
            DiagnosticSeverity::Off
        };
        let style = window.text_style();
        let font_size = style.font_size.to_pixels(window.rem_size());
        let editor = cx.entity().downgrade();
        let fold_placeholder = FoldPlaceholder {
            constrain_width: false,
            render: Arc::new(move |fold_id, fold_range, cx| {
                let editor = editor.clone();
                FoldPlaceholder::fold_element(fold_id, cx)
                    .cursor_pointer()
                    .child("⋯")
                    .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
                    .on_click(move |_, _window, cx| {
                        editor
                            .update(cx, |editor, cx| {
                                editor.unfold_ranges(
                                    &[fold_range.start..fold_range.end],
                                    true,
                                    false,
                                    cx,
                                );
                                cx.stop_propagation();
                            })
                            .ok();
                    })
                    .into_any()
            }),
            merge_adjacent: true,
            ..FoldPlaceholder::default()
        };
        let display_map = display_map.unwrap_or_else(|| {
            cx.new(|cx| {
                DisplayMap::new(
                    multi_buffer.clone(),
                    style.font(),
                    font_size,
                    None,
                    FILE_HEADER_HEIGHT,
                    MULTI_BUFFER_EXCERPT_HEADER_HEIGHT,
                    fold_placeholder,
                    diagnostics_max_severity,
                    cx,
                )
            })
        });

        let selections = SelectionsCollection::new();

        let blink_manager = cx.new(|cx| {
            let mut blink_manager = BlinkManager::new(
                CURSOR_BLINK_INTERVAL,
                |cx| EditorSettings::get_global(cx).cursor_blink,
                cx,
            );
            if is_minimap {
                blink_manager.disable(cx);
            }
            blink_manager
        });

        let soft_wrap_mode_override =
            matches!(mode, EditorMode::SingleLine).then(|| language_settings::SoftWrap::None);

        let mut project_subscriptions = Vec::new();
        if full_mode && let Some(project) = project.as_ref() {
            project_subscriptions.push(cx.subscribe_in(
                project,
                window,
                |editor, _, event, window, cx| match event {
                    project::Event::RefreshCodeLens => {
                        // we always query lens with actions, without storing them, always refreshing them
                    }
                    project::Event::RefreshInlayHints {
                        server_id,
                        request_id,
                    } => {
                        editor.refresh_inlay_hints(
                            InlayHintRefreshReason::RefreshRequested {
                                server_id: *server_id,
                                request_id: *request_id,
                            },
                            cx,
                        );
                    }
                    project::Event::RefreshSemanticTokens {
                        server_id,
                        request_id,
                    } => {
                        editor.refresh_semantic_tokens(
                            None,
                            Some(RefreshForServer {
                                server_id: *server_id,
                                request_id: *request_id,
                            }),
                            cx,
                        );
                    }
                    project::Event::LanguageServerRemoved(_) => {
                        editor.registered_buffers.clear();
                        editor.register_visible_buffers(cx);
                        editor.invalidate_semantic_tokens(None);
                        editor.refresh_runnables(None, window, cx);
                        editor.update_lsp_data(None, window, cx);
                        editor.refresh_inlay_hints(InlayHintRefreshReason::ServerRemoved, cx);
                    }
                    project::Event::SnippetEdit(id, snippet_edits) => {
                        // todo(lw): Non singletons
                        if let Some(buffer) = editor.buffer.read(cx).as_singleton() {
                            let snapshot = buffer.read(cx).snapshot();
                            let focus_handle = editor.focus_handle(cx);
                            if snapshot.remote_id() == *id && focus_handle.is_focused(window) {
                                for (range, snippet) in snippet_edits {
                                    let buffer_range =
                                        language::range_from_lsp(*range).to_offset(&snapshot);
                                    editor
                                        .insert_snippet(
                                            &[MultiBufferOffset(buffer_range.start)
                                                ..MultiBufferOffset(buffer_range.end)],
                                            snippet.clone(),
                                            window,
                                            cx,
                                        )
                                        .ok();
                                }
                            }
                        }
                    }
                    project::Event::LanguageServerBufferRegistered { buffer_id, .. } => {
                        let buffer_id = *buffer_id;
                        if editor.buffer().read(cx).buffer(buffer_id).is_some() {
                            editor.register_buffer(buffer_id, cx);
                            editor.refresh_runnables(Some(buffer_id), window, cx);
                            editor.update_lsp_data(Some(buffer_id), window, cx);
                            editor.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx);
                            refresh_linked_ranges(editor, window, cx);
                            editor.refresh_code_actions(window, cx);
                            editor.refresh_document_highlights(cx);
                        }
                    }

                    project::Event::EntryRenamed(transaction, project_path, abs_path) => {
                        let Some(workspace) = editor.workspace() else {
                            return;
                        };
                        let Some(active_editor) = workspace.read(cx).active_item_as::<Self>(cx)
                        else {
                            return;
                        };

                        if active_editor.entity_id() == cx.entity_id() {
                            let entity_id = cx.entity_id();
                            workspace.update(cx, |this, cx| {
                                this.panes_mut()
                                    .iter_mut()
                                    .filter(|pane| pane.entity_id() != entity_id)
                                    .for_each(|p| {
                                        p.update(cx, |pane, _| {
                                            pane.nav_history_mut().rename_item(
                                                entity_id,
                                                project_path.clone(),
                                                abs_path.clone().into(),
                                            );
                                        })
                                    });
                            });

                            Self::open_transaction_for_hidden_buffers(
                                workspace,
                                transaction.clone(),
                                "Rename".to_string(),
                                window,
                                cx,
                            );
                        }
                    }

                    project::Event::WorkspaceEditApplied(transaction) => {
                        let Some(workspace) = editor.workspace() else {
                            return;
                        };
                        let Some(active_editor) = workspace.read(cx).active_item_as::<Self>(cx)
                        else {
                            return;
                        };

                        if active_editor.entity_id() == cx.entity_id() {
                            Self::open_transaction_for_hidden_buffers(
                                workspace,
                                transaction.clone(),
                                "LSP Edit".to_string(),
                                window,
                                cx,
                            );
                        }
                    }

                    _ => {}
                },
            ));
            if let Some(task_inventory) = project
                .read(cx)
                .task_store()
                .read(cx)
                .task_inventory()
                .cloned()
            {
                project_subscriptions.push(cx.observe_in(
                    &task_inventory,
                    window,
                    |editor, _, window, cx| {
                        editor.refresh_runnables(None, window, cx);
                    },
                ));
            };

            project_subscriptions.push(cx.subscribe_in(
                &project.read(cx).breakpoint_store(),
                window,
                |editor, _, event, window, cx| match event {
                    BreakpointStoreEvent::ClearDebugLines => {
                        editor.clear_row_highlights::<ActiveDebugLine>();
                        editor.refresh_inline_values(cx);
                    }
                    BreakpointStoreEvent::SetDebugLine => {
                        if editor.go_to_active_debug_line(window, cx) {
                            cx.stop_propagation();
                        }

                        editor.refresh_inline_values(cx);
                    }
                    _ => {}
                },
            ));
            let git_store = project.read(cx).git_store().clone();
            let project = project.clone();
            project_subscriptions.push(cx.subscribe(&git_store, move |this, _, event, cx| {
                if let GitStoreEvent::RepositoryAdded = event {
                    this.load_diff_task = Some(
                        update_uncommitted_diff_for_buffer(
                            cx.entity(),
                            &project,
                            this.buffer.read(cx).all_buffers(),
                            this.buffer.clone(),
                            cx,
                        )
                        .shared(),
                    );
                }
            }));
        }

        let buffer_snapshot = multi_buffer.read(cx).snapshot(cx);

        let inlay_hint_settings =
            inlay_hint_settings(selections.newest_anchor().head(), &buffer_snapshot, cx);
        let focus_handle = cx.focus_handle();
        if !is_minimap {
            cx.on_focus(&focus_handle, window, Self::handle_focus)
                .detach();
            cx.on_focus_in(&focus_handle, window, Self::handle_focus_in)
                .detach();
            cx.on_focus_out(&focus_handle, window, Self::handle_focus_out)
                .detach();
            cx.on_blur(&focus_handle, window, Self::handle_blur)
                .detach();
            cx.observe_pending_input(window, Self::observe_pending_input)
                .detach();
        }

        let show_indent_guides =
            if matches!(mode, EditorMode::SingleLine | EditorMode::Minimap { .. }) {
                Some(false)
            } else {
                None
            };

        let breakpoint_store = match (&mode, project.as_ref()) {
            (EditorMode::Full { .. }, Some(project)) => Some(project.read(cx).breakpoint_store()),
            _ => None,
        };

        let mut code_action_providers = Vec::new();
        let mut load_uncommitted_diff = None;
        if let Some(project) = project.clone() {
            load_uncommitted_diff = Some(
                update_uncommitted_diff_for_buffer(
                    cx.entity(),
                    &project,
                    multi_buffer.read(cx).all_buffers(),
                    multi_buffer.clone(),
                    cx,
                )
                .shared(),
            );
            code_action_providers.push(Rc::new(project) as Rc<_>);
        }

        let mut editor = Self {
            focus_handle,
            show_cursor_when_unfocused: false,
            last_focused_descendant: None,
            buffer: multi_buffer.clone(),
            display_map: display_map.clone(),
            placeholder_display_map: None,
            selections,
            scroll_manager: ScrollManager::new(cx),
            columnar_selection_state: None,
            add_selections_state: None,
            select_next_state: None,
            select_prev_state: None,
            selection_history: SelectionHistory::default(),
            defer_selection_effects: false,
            deferred_selection_effects_state: None,
            autoclose_regions: Vec::new(),
            snippet_stack: InvalidationStack::default(),
            select_syntax_node_history: SelectSyntaxNodeHistory::default(),
            ime_transaction: None,
            active_diagnostics: ActiveDiagnostic::None,
            show_inline_diagnostics: ProjectSettings::get_global(cx).diagnostics.inline.enabled,
            inline_diagnostics_update: Task::ready(()),
            inline_diagnostics: Vec::new(),
            soft_wrap_mode_override,
            diagnostics_max_severity,
            hard_wrap: None,
            completion_provider: project.clone().map(|project| Rc::new(project) as _),
            semantics_provider: project
                .as_ref()
                .map(|project| Rc::new(project.downgrade()) as _),
            collaboration_hub: project.clone().map(|project| Box::new(project) as _),
            project,
            blink_manager: blink_manager.clone(),
            show_local_selections: true,
            show_scrollbars: ScrollbarAxes {
                horizontal: full_mode,
                vertical: full_mode,
            },
            minimap_visibility: MinimapVisibility::for_mode(&mode, cx),
            offset_content: !matches!(mode, EditorMode::SingleLine),
            show_breadcrumbs: EditorSettings::get_global(cx).toolbar.breadcrumbs,
            show_gutter: full_mode,
            show_line_numbers: (!full_mode).then_some(false),
            use_relative_line_numbers: None,
            disable_expand_excerpt_buttons: !full_mode,
            delegate_expand_excerpts: false,
            delegate_stage_and_restore: false,
            delegate_open_excerpts: false,
            enable_lsp_data: true,
            enable_runnables: true,
            show_git_diff_gutter: None,
            show_code_actions: None,
            show_runnables: None,
            show_breakpoints: None,
            show_diff_review_button: false,
            show_wrap_guides: None,
            show_indent_guides,
            buffers_with_disabled_indent_guides: HashSet::default(),
            highlight_order: 0,
            highlighted_rows: HashMap::default(),
            background_highlights: HashMap::default(),
            gutter_highlights: HashMap::default(),
            scrollbar_marker_state: ScrollbarMarkerState::default(),
            active_indent_guides_state: ActiveIndentGuidesState::default(),
            nav_history: None,
            context_menu: RefCell::new(None),
            context_menu_options: None,
            mouse_context_menu: None,
            completion_tasks: Vec::new(),
            inline_blame_popover: None,
            inline_blame_popover_show_task: None,
            signature_help_state: SignatureHelpState::default(),
            auto_signature_help: None,
            find_all_references_task_sources: Vec::new(),
            next_completion_id: 0,
            next_inlay_id: 0,
            code_action_providers,
            available_code_actions: None,
            code_actions_task: None,
            quick_selection_highlight_task: None,
            debounced_selection_highlight_task: None,
            debounced_selection_highlight_complete: false,
            document_highlights_task: None,
            linked_editing_range_task: None,
            pending_rename: None,
            searchable: !is_minimap,
            cursor_shape: EditorSettings::get_global(cx)
                .cursor_shape
                .unwrap_or_default(),
            cursor_offset_on_selection: false,
            current_line_highlight: None,
            autoindent_mode: Some(AutoindentMode::EachLine),
            collapse_matches: false,
            workspace: None,
            input_enabled: !is_minimap,
            expects_character_input: !is_minimap,
            use_modal_editing: full_mode,
            read_only: is_minimap,
            use_autoclose: true,
            use_auto_surround: true,
            use_selection_highlight: true,
            auto_replace_emoji_shortcode: false,
            jsx_tag_auto_close_enabled_in_any_buffer: false,
            leader_id: None,
            remote_id: None,
            hover_state: HoverState::default(),
            pending_mouse_down: None,
            prev_pressure_stage: None,
            hovered_link_state: None,
            edit_prediction_provider: None,
            active_edit_prediction: None,
            stale_edit_prediction_in_menu: None,
            edit_prediction_preview: EditPredictionPreview::Inactive {
                released_too_fast: false,
            },
            inline_diagnostics_enabled: full_mode,
            diagnostics_enabled: full_mode,
            word_completions_enabled: full_mode,
            inline_value_cache: InlineValueCache::new(inlay_hint_settings.show_value_hints),
            gutter_hovered: false,
            pixel_position_of_newest_cursor: None,
            last_bounds: None,
            last_position_map: None,
            expect_bounds_change: None,
            gutter_dimensions: GutterDimensions::default(),
            style: None,
            show_cursor_names: false,
            hovered_cursors: HashMap::default(),
            next_editor_action_id: EditorActionId::default(),
            editor_actions: Rc::default(),
            edit_predictions_hidden_for_vim_mode: false,
            show_edit_predictions_override: None,
            show_completions_on_input_override: None,
            menu_edit_predictions_policy: MenuEditPredictionsPolicy::ByProvider,
            edit_prediction_settings: EditPredictionSettings::Disabled,
            in_leading_whitespace: false,
            custom_context_menu: None,
            show_git_blame_gutter: false,
            show_git_blame_inline: false,
            show_selection_menu: None,
            show_git_blame_inline_delay_task: None,
            git_blame_inline_enabled: full_mode
                && ProjectSettings::get_global(cx).git.inline_blame.enabled,
            render_diff_hunk_controls: Arc::new(render_diff_hunk_controls),
            buffer_serialization: is_minimap.not().then(|| {
                BufferSerialization::new(
                    ProjectSettings::get_global(cx)
                        .session
                        .restore_unsaved_buffers,
                )
            }),
            blame: None,
            blame_subscription: None,

            breakpoint_store,
            gutter_breakpoint_indicator: (None, None),
            gutter_diff_review_indicator: (None, None),
            diff_review_drag_state: None,
            diff_review_overlays: Vec::new(),
            stored_review_comments: Vec::new(),
            next_review_comment_id: 0,
            hovered_diff_hunk_row: None,
            _subscriptions: (!is_minimap)
                .then(|| {
                    vec![
                        cx.observe(&multi_buffer, Self::on_buffer_changed),
                        cx.subscribe_in(&multi_buffer, window, Self::on_buffer_event),
                        cx.observe_in(&display_map, window, Self::on_display_map_changed),
                        cx.observe(&blink_manager, |_, _, cx| cx.notify()),
                        cx.observe_global_in::<SettingsStore>(window, Self::settings_changed),
                        cx.observe_global_in::<GlobalTheme>(window, Self::theme_changed),
                        observe_buffer_font_size_adjustment(cx, |_, cx| cx.notify()),
                        cx.observe_window_activation(window, |editor, window, cx| {
                            let active = window.is_window_active();
                            editor.blink_manager.update(cx, |blink_manager, cx| {
                                if active {
                                    blink_manager.enable(cx);
                                } else {
                                    blink_manager.disable(cx);
                                }
                            });
                            if active {
                                editor.show_mouse_cursor(cx);
                            }
                        }),
                    ]
                })
                .unwrap_or_default(),
            runnables: RunnableData::new(),
            pull_diagnostics_task: Task::ready(()),
            colors: None,
            refresh_colors_task: Task::ready(()),
            use_document_folding_ranges: false,
            refresh_folding_ranges_task: Task::ready(()),
            inlay_hints: None,
            next_color_inlay_id: 0,
            post_scroll_update: Task::ready(()),
            linked_edit_ranges: Default::default(),
            in_project_search: false,
            previous_search_ranges: None,
            breadcrumb_header: None,
            focused_block: None,
            next_scroll_position: NextScrollCursorCenterTopBottom::default(),
            addons: HashMap::default(),
            registered_buffers: HashMap::default(),
            _scroll_cursor_center_top_bottom_task: Task::ready(()),
            selection_mark_mode: false,
            toggle_fold_multiple_buffers: Task::ready(()),
            serialize_selections: Task::ready(()),
            serialize_folds: Task::ready(()),
            text_style_refinement: None,
            load_diff_task: load_uncommitted_diff,
            temporary_diff_override: false,
            mouse_cursor_hidden: false,
            minimap: None,
            hide_mouse_mode: EditorSettings::get_global(cx)
                .hide_mouse
                .unwrap_or_default(),
            change_list: ChangeList::new(),
            mode,
            selection_drag_state: SelectionDragState::None,
            folding_newlines: Task::ready(()),
            lookup_key: None,
            select_next_is_case_sensitive: None,
            on_local_selections_changed: None,
            suppress_selection_callback: false,
            applicable_language_settings: HashMap::default(),
            semantic_token_state: SemanticTokenState::new(cx, full_mode),
            accent_data: None,
            bracket_fetched_tree_sitter_chunks: HashMap::default(),
            number_deleted_lines: false,
            refresh_matching_bracket_highlights_task: Task::ready(()),
            refresh_document_symbols_task: Task::ready(()).shared(),
            lsp_document_symbols: HashMap::default(),
            refresh_outline_symbols_at_cursor_at_cursor_task: Task::ready(()),
            outline_symbols_at_cursor: None,
            sticky_headers_task: Task::ready(()),
            sticky_headers: None,
            colorize_brackets_task: Task::ready(()),
        };

        if is_minimap {
            return editor;
        }

        editor.applicable_language_settings = editor.fetch_applicable_language_settings(cx);
        editor.accent_data = editor.fetch_accent_data(cx);

        if let Some(breakpoints) = editor.breakpoint_store.as_ref() {
            editor
                ._subscriptions
                .push(cx.observe(breakpoints, |_, _, cx| {
                    cx.notify();
                }));
        }
        editor._subscriptions.extend(project_subscriptions);

        editor._subscriptions.push(cx.subscribe_in(
            &cx.entity(),
            window,
            |editor, _, e: &EditorEvent, window, cx| match e {
                EditorEvent::ScrollPositionChanged { local, .. } => {
                    if *local {
                        editor.hide_signature_help(cx, SignatureHelpHiddenBy::Escape);
                        editor.inline_blame_popover.take();
                        let snapshot = editor.snapshot(window, cx);
                        let new_anchor = editor
                            .scroll_manager
                            .native_anchor(&snapshot.display_snapshot, cx);
                        editor.update_restoration_data(cx, move |data| {
                            data.scroll_position = (
                                new_anchor.top_row(snapshot.buffer_snapshot()),
                                new_anchor.offset,
                            );
                        });

                        editor.post_scroll_update = cx.spawn_in(window, async move |editor, cx| {
                            cx.background_executor()
                                .timer(Duration::from_millis(50))
                                .await;
                            editor
                                .update_in(cx, |editor, window, cx| {
                                    editor.update_data_on_scroll(window, cx)
                                })
                                .ok();
                        });
                    }
                    editor.refresh_sticky_headers(&editor.snapshot(window, cx), cx);
                }
                EditorEvent::Edited { .. } => {
                    let vim_mode = vim_mode_setting::VimModeSetting::try_get(cx)
                        .map(|vim_mode| vim_mode.0)
                        .unwrap_or(false);
                    if !vim_mode {
                        let display_map = editor.display_snapshot(cx);
                        let selections = editor.selections.all_adjusted_display(&display_map);
                        let pop_state = editor
                            .change_list
                            .last()
                            .map(|previous| {
                                previous.len() == selections.len()
                                    && previous.iter().enumerate().all(|(ix, p)| {
                                        p.to_display_point(&display_map).row()
                                            == selections[ix].head().row()
                                    })
                            })
                            .unwrap_or(false);
                        let new_positions = selections
                            .into_iter()
                            .map(|s| display_map.display_point_to_anchor(s.head(), Bias::Left))
                            .collect();
                        editor
                            .change_list
                            .push_to_change_list(pop_state, new_positions);
                    }
                }
                _ => (),
            },
        ));

        if let Some(dap_store) = editor
            .project
            .as_ref()
            .map(|project| project.read(cx).dap_store())
        {
            let weak_editor = cx.weak_entity();

            editor
                ._subscriptions
                .push(
                    cx.observe_new::<project::debugger::session::Session>(move |_, _, cx| {
                        let session_entity = cx.entity();
                        weak_editor
                            .update(cx, |editor, cx| {
                                editor._subscriptions.push(
                                    cx.subscribe(&session_entity, Self::on_debug_session_event),
                                );
                            })
                            .ok();
                    }),
                );

            for session in dap_store.read(cx).sessions().cloned().collect::<Vec<_>>() {
                editor
                    ._subscriptions
                    .push(cx.subscribe(&session, Self::on_debug_session_event));
            }
        }

        // skip adding the initial selection to selection history
        editor.selection_history.mode = SelectionHistoryMode::Skipping;
        editor.end_selection(window, cx);
        editor.selection_history.mode = SelectionHistoryMode::Normal;

        editor.scroll_manager.show_scrollbars(window, cx);
        jsx_tag_auto_close::refresh_enabled_in_any_buffer(&mut editor, &multi_buffer, cx);

        if full_mode {
            let should_auto_hide_scrollbars = cx.should_auto_hide_scrollbars();
            cx.set_global(ScrollbarAutoHide(should_auto_hide_scrollbars));

            if editor.git_blame_inline_enabled {
                editor.start_git_blame_inline(false, window, cx);
            }

            editor.go_to_active_debug_line(window, cx);

            editor.minimap =
                editor.create_minimap(EditorSettings::get_global(cx).minimap, window, cx);
            editor.colors = Some(LspColorData::new(cx));
            editor.use_document_folding_ranges = true;
            editor.inlay_hints = Some(LspInlayHintData::new(inlay_hint_settings));

            if let Some(buffer) = multi_buffer.read(cx).as_singleton() {
                editor.register_buffer(buffer.read(cx).remote_id(), cx);
            }
            editor.report_editor_event(ReportEditorEvent::EditorOpened, None, cx);
        }

        editor
    }

    pub fn display_snapshot(&self, cx: &mut App) -> DisplaySnapshot {
        self.display_map.update(cx, |map, cx| map.snapshot(cx))
    }

    pub fn deploy_mouse_context_menu(
        &mut self,
        position: gpui::Point<Pixels>,
        context_menu: Entity<ContextMenu>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.mouse_context_menu = Some(MouseContextMenu::new(
            self,
            crate::mouse_context_menu::MenuPosition::PinnedToScreen(position),
            context_menu,
            window,
            cx,
        ));
    }

    pub fn mouse_menu_is_focused(&self, window: &Window, cx: &App) -> bool {
        self.mouse_context_menu
            .as_ref()
            .is_some_and(|menu| menu.context_menu.focus_handle(cx).is_focused(window))
    }

    pub fn is_range_selected(&mut self, range: &Range<Anchor>, cx: &mut Context<Self>) -> bool {
        if self
            .selections
            .pending_anchor()
            .is_some_and(|pending_selection| {
                let snapshot = self.buffer().read(cx).snapshot(cx);
                pending_selection.range().includes(range, &snapshot)
            })
        {
            return true;
        }

        self.selections
            .disjoint_in_range::<MultiBufferOffset>(range.clone(), &self.display_snapshot(cx))
            .into_iter()
            .any(|selection| {
                // This is needed to cover a corner case, if we just check for an existing
                // selection in the fold range, having a cursor at the start of the fold
                // marks it as selected. Non-empty selections don't cause this.
                let length = selection.end - selection.start;
                length > 0
            })
    }

    pub fn key_context(&self, window: &mut Window, cx: &mut App) -> KeyContext {
        self.key_context_internal(self.has_active_edit_prediction(), window, cx)
    }

    fn key_context_internal(
        &self,
        has_active_edit_prediction: bool,
        window: &mut Window,
        cx: &mut App,
    ) -> KeyContext {
        let mut key_context = KeyContext::new_with_defaults();
        key_context.add("Editor");
        let mode = match self.mode {
            EditorMode::SingleLine => "single_line",
            EditorMode::AutoHeight { .. } => "auto_height",
            EditorMode::Minimap { .. } => "minimap",
            EditorMode::Full { .. } => "full",
        };

        if EditorSettings::jupyter_enabled(cx) {
            key_context.add("jupyter");
        }

        key_context.set("mode", mode);
        if self.pending_rename.is_some() {
            key_context.add("renaming");
        }

        if let Some(snippet_stack) = self.snippet_stack.last() {
            key_context.add("in_snippet");

            if snippet_stack.active_index > 0 {
                key_context.add("has_previous_tabstop");
            }

            if snippet_stack.active_index < snippet_stack.ranges.len().saturating_sub(1) {
                key_context.add("has_next_tabstop");
            }
        }

        match self.context_menu.borrow().as_ref() {
            Some(CodeContextMenu::Completions(menu)) => {
                if menu.visible() {
                    key_context.add("menu");
                    key_context.add("showing_completions");
                }
            }
            Some(CodeContextMenu::CodeActions(menu)) => {
                if menu.visible() {
                    key_context.add("menu");
                    key_context.add("showing_code_actions")
                }
            }
            None => {}
        }

        if self.signature_help_state.has_multiple_signatures() {
            key_context.add("showing_signature_help");
        }

        // Disable vim contexts when a sub-editor (e.g. rename/inline assistant) is focused.
        if !self.focus_handle(cx).contains_focused(window, cx)
            || (self.is_focused(window) || self.mouse_menu_is_focused(window, cx))
        {
            for addon in self.addons.values() {
                addon.extend_key_context(&mut key_context, cx)
            }
        }

        if let Some(singleton_buffer) = self.buffer.read(cx).as_singleton() {
            if let Some(extension) = singleton_buffer.read(cx).file().and_then(|file| {
                Some(
                    file.full_path(cx)
                        .extension()?
                        .to_string_lossy()
                        .to_lowercase(),
                )
            }) {
                key_context.set("extension", extension);
            }
        } else {
            key_context.add("multibuffer");
        }

        if has_active_edit_prediction {
            key_context.add(EDIT_PREDICTION_KEY_CONTEXT);
            key_context.add("copilot_suggestion");
        }

        if self.in_leading_whitespace {
            key_context.add("in_leading_whitespace");
        }
        if self.edit_prediction_requires_modifier() {
            key_context.set("edit_prediction_mode", "subtle")
        } else {
            key_context.set("edit_prediction_mode", "eager");
        }

        if self.selection_mark_mode {
            key_context.add("selection_mode");
        }

        let disjoint = self.selections.disjoint_anchors();
        if matches!(
            &self.mode,
            EditorMode::SingleLine | EditorMode::AutoHeight { .. }
        ) && let [selection] = disjoint
            && selection.start == selection.end
        {
            let snapshot = self.snapshot(window, cx);
            let snapshot = snapshot.buffer_snapshot();
            let caret_offset = selection.end.to_offset(snapshot);

            if caret_offset == MultiBufferOffset(0) {
                key_context.add("start_of_input");
            }

            if caret_offset == snapshot.len() {
                key_context.add("end_of_input");
            }
        }

        if self.has_any_expanded_diff_hunks(cx) {
            key_context.add("diffs_expanded");
        }

        key_context
    }

    pub fn last_bounds(&self) -> Option<&Bounds<Pixels>> {
        self.last_bounds.as_ref()
    }

    fn show_mouse_cursor(&mut self, cx: &mut Context<Self>) {
        if self.mouse_cursor_hidden {
            self.mouse_cursor_hidden = false;
            cx.notify();
        }
    }

    pub fn hide_mouse_cursor(&mut self, origin: HideMouseCursorOrigin, cx: &mut Context<Self>) {
        let hide_mouse_cursor = match origin {
            HideMouseCursorOrigin::TypingAction => {
                matches!(
                    self.hide_mouse_mode,
                    HideMouseMode::OnTyping | HideMouseMode::OnTypingAndMovement
                )
            }
            HideMouseCursorOrigin::MovementAction => {
                matches!(self.hide_mouse_mode, HideMouseMode::OnTypingAndMovement)
            }
        };
        if self.mouse_cursor_hidden != hide_mouse_cursor {
            self.mouse_cursor_hidden = hide_mouse_cursor;
            cx.notify();
        }
    }

    fn accept_edit_prediction_keystroke(
        &self,
        granularity: EditPredictionGranularity,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<gpui::KeybindingKeystroke> {
        let key_context = self.key_context_internal(true, window, cx);

        let bindings =
            match granularity {
                EditPredictionGranularity::Word => window
                    .bindings_for_action_in_context(&AcceptNextWordEditPrediction, key_context),
                EditPredictionGranularity::Line => window
                    .bindings_for_action_in_context(&AcceptNextLineEditPrediction, key_context),
                EditPredictionGranularity::Full => {
                    window.bindings_for_action_in_context(&AcceptEditPrediction, key_context)
                }
            };

        bindings
            .into_iter()
            .rev()
            .find_map(|binding| match binding.keystrokes() {
                [keystroke, ..] => Some(keystroke.clone()),
                _ => None,
            })
    }

    fn preview_edit_prediction_keystroke(
        &self,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<gpui::KeybindingKeystroke> {
        let key_context = self.key_context_internal(true, window, cx);
        let bindings = window.bindings_for_action_in_context(&AcceptEditPrediction, key_context);
        bindings
            .into_iter()
            .rev()
            .find_map(|binding| match binding.keystrokes() {
                [keystroke, ..] if keystroke.modifiers().modified() => Some(keystroke.clone()),
                _ => None,
            })
    }

    fn edit_prediction_preview_modifiers_held(
        &self,
        modifiers: &Modifiers,
        window: &mut Window,
        cx: &mut App,
    ) -> bool {
        let key_context = self.key_context_internal(true, window, cx);
        let actions: [&dyn Action; 3] = [
            &AcceptEditPrediction,
            &AcceptNextWordEditPrediction,
            &AcceptNextLineEditPrediction,
        ];

        actions.into_iter().any(|action| {
            window
                .bindings_for_action_in_context(action, key_context.clone())
                .into_iter()
                .rev()
                .any(|binding| {
                    binding.keystrokes().first().is_some_and(|keystroke| {
                        keystroke.modifiers().modified() && keystroke.modifiers() == modifiers
                    })
                })
        })
    }

    fn edit_prediction_cursor_popover_prefers_preview(
        &self,
        completion: &EditPredictionState,
        cx: &App,
    ) -> bool {
        let multibuffer_snapshot = self.buffer.read(cx).snapshot(cx);

        match &completion.completion {
            EditPrediction::Edit {
                edits, snapshot, ..
            } => {
                let mut start_row: Option<u32> = None;
                let mut end_row: Option<u32> = None;

                for (range, text) in edits {
                    let Some((_, range)) =
                        multibuffer_snapshot.anchor_range_to_buffer_anchor_range(range.clone())
                    else {
                        continue;
                    };
                    let edit_start_row = range.start.to_point(snapshot).row;
                    let old_end_row = range.end.to_point(snapshot).row;
                    let inserted_newline_count = text
                        .as_ref()
                        .chars()
                        .filter(|character| *character == '\n')
                        .count() as u32;
                    let deleted_newline_count = old_end_row - edit_start_row;
                    let preview_end_row = edit_start_row + inserted_newline_count;

                    start_row =
                        Some(start_row.map_or(edit_start_row, |row| row.min(edit_start_row)));
                    end_row = Some(end_row.map_or(preview_end_row, |row| row.max(preview_end_row)));

                    if deleted_newline_count > 1 {
                        end_row = Some(end_row.map_or(old_end_row, |row| row.max(old_end_row)));
                    }
                }

                start_row
                    .zip(end_row)
                    .is_some_and(|(start_row, end_row)| end_row > start_row)
            }
            EditPrediction::MoveWithin { .. } | EditPrediction::MoveOutside { .. } => false,
        }
    }

    fn edit_prediction_keybind_display(
        &self,
        surface: EditPredictionKeybindSurface,
        window: &mut Window,
        cx: &mut App,
    ) -> EditPredictionKeybindDisplay {
        let accept_keystroke =
            self.accept_edit_prediction_keystroke(EditPredictionGranularity::Full, window, cx);
        let preview_keystroke = self.preview_edit_prediction_keystroke(window, cx);

        let action = match surface {
            EditPredictionKeybindSurface::Inline
            | EditPredictionKeybindSurface::CursorPopoverCompact => {
                if self.edit_prediction_requires_modifier() {
                    EditPredictionKeybindAction::Preview
                } else {
                    EditPredictionKeybindAction::Accept
                }
            }
            EditPredictionKeybindSurface::CursorPopoverExpanded => self
                .active_edit_prediction
                .as_ref()
                .filter(|completion| {
                    self.edit_prediction_cursor_popover_prefers_preview(completion, cx)
                })
                .map_or(EditPredictionKeybindAction::Accept, |_| {
                    EditPredictionKeybindAction::Preview
                }),
        };
        #[cfg(test)]
        let preview_copy = preview_keystroke.clone();
        #[cfg(test)]
        let accept_copy = accept_keystroke.clone();

        let displayed_keystroke = match surface {
            EditPredictionKeybindSurface::Inline => match action {
                EditPredictionKeybindAction::Accept => accept_keystroke,
                EditPredictionKeybindAction::Preview => preview_keystroke,
            },
            EditPredictionKeybindSurface::CursorPopoverCompact
            | EditPredictionKeybindSurface::CursorPopoverExpanded => match action {
                EditPredictionKeybindAction::Accept => accept_keystroke,
                EditPredictionKeybindAction::Preview => {
                    preview_keystroke.or_else(|| accept_keystroke.clone())
                }
            },
        };

        let missing_accept_keystroke = displayed_keystroke.is_none();

        EditPredictionKeybindDisplay {
            #[cfg(test)]
            accept_keystroke: accept_copy,
            #[cfg(test)]
            preview_keystroke: preview_copy,
            displayed_keystroke,
            action,
            missing_accept_keystroke,
            show_hold_label: matches!(surface, EditPredictionKeybindSurface::CursorPopoverCompact)
                && self.edit_prediction_preview.released_too_fast(),
        }
    }

    pub fn new_file(
        workspace: &mut Workspace,
        _: &workspace::NewFile,
        window: &mut Window,
        cx: &mut Context<Workspace>,
    ) {
        Self::new_in_workspace(workspace, window, cx).detach_and_prompt_err(
            "Failed to create buffer",
            window,
            cx,
            |e, _, _| match e.error_code() {
                ErrorCode::RemoteUpgradeRequired => Some(format!(
                "The remote instance of Zed does not support this yet. It must be upgraded to {}",
                e.error_tag("required").unwrap_or("the latest version")
            )),
                _ => None,
            },
        );
    }

    pub fn new_in_workspace(
        workspace: &mut Workspace,
        window: &mut Window,
        cx: &mut Context<Workspace>,
    ) -> Task<Result<Entity<Editor>>> {
        let project = workspace.project().clone();
        let create = project.update(cx, |project, cx| project.create_buffer(None, true, cx));

        cx.spawn_in(window, async move |workspace, cx| {
            let buffer = create.await?;
            workspace.update_in(cx, |workspace, window, cx| {
                let editor =
                    cx.new(|cx| Editor::for_buffer(buffer, Some(project.clone()), window, cx));
                workspace.add_item_to_active_pane(Box::new(editor.clone()), None, true, window, cx);
                editor
            })
        })
    }

    fn new_file_vertical(
        workspace: &mut Workspace,
        _: &workspace::NewFileSplitVertical,
        window: &mut Window,
        cx: &mut Context<Workspace>,
    ) {
        Self::new_file_in_direction(workspace, SplitDirection::vertical(cx), window, cx)
    }

    fn new_file_horizontal(
        workspace: &mut Workspace,
        _: &workspace::NewFileSplitHorizontal,
        window: &mut Window,
        cx: &mut Context<Workspace>,
    ) {
        Self::new_file_in_direction(workspace, SplitDirection::horizontal(cx), window, cx)
    }

    fn new_file_split(
        workspace: &mut Workspace,
        action: &workspace::NewFileSplit,
        window: &mut Window,
        cx: &mut Context<Workspace>,
    ) {
        Self::new_file_in_direction(workspace, action.0, window, cx)
    }

    fn new_file_in_direction(
        workspace: &mut Workspace,
        direction: SplitDirection,
        window: &mut Window,
        cx: &mut Context<Workspace>,
    ) {
        let project = workspace.project().clone();
        let create = project.update(cx, |project, cx| project.create_buffer(None, true, cx));

        cx.spawn_in(window, async move |workspace, cx| {
            let buffer = create.await?;
            workspace.update_in(cx, move |workspace, window, cx| {
                workspace.split_item(
                    direction,
                    Box::new(
                        cx.new(|cx| Editor::for_buffer(buffer, Some(project.clone()), window, cx)),
                    ),
                    window,
                    cx,
                )
            })?;
            anyhow::Ok(())
        })
        .detach_and_prompt_err("Failed to create buffer", window, cx, |e, _, _| {
            match e.error_code() {
                ErrorCode::RemoteUpgradeRequired => Some(format!(
                "The remote instance of Zed does not support this yet. It must be upgraded to {}",
                e.error_tag("required").unwrap_or("the latest version")
            )),
                _ => None,
            }
        });
    }

    pub fn leader_id(&self) -> Option<CollaboratorId> {
        self.leader_id
    }

    pub fn buffer(&self) -> &Entity<MultiBuffer> {
        &self.buffer
    }

    pub fn project(&self) -> Option<&Entity<Project>> {
        self.project.as_ref()
    }

    pub fn workspace(&self) -> Option<Entity<Workspace>> {
        self.workspace.as_ref()?.0.upgrade()
    }

    /// Detaches a task and shows an error notification in the workspace if available,
    /// otherwise just logs the error.
    pub fn detach_and_notify_err<R, E>(
        &self,
        task: Task<Result<R, E>>,
        window: &mut Window,
        cx: &mut App,
    ) where
        E: std::fmt::Debug + std::fmt::Display + 'static,
        R: 'static,
    {
        if let Some(workspace) = self.workspace() {
            task.detach_and_notify_err(workspace.downgrade(), window, cx);
        } else {
            task.detach_and_log_err(cx);
        }
    }

    /// Returns the workspace serialization ID if this editor should be serialized.
    fn workspace_serialization_id(&self, _cx: &App) -> Option<WorkspaceId> {
        self.workspace
            .as_ref()
            .filter(|_| self.should_serialize_buffer())
            .and_then(|workspace| workspace.1)
    }

    pub fn title<'a>(&self, cx: &'a App) -> Cow<'a, str> {
        self.buffer().read(cx).title(cx)
    }

    pub fn snapshot(&self, window: &Window, cx: &mut App) -> EditorSnapshot {
        let git_blame_gutter_max_author_length = self
            .render_git_blame_gutter(cx)
            .then(|| {
                if let Some(blame) = self.blame.as_ref() {
                    let max_author_length =
                        blame.update(cx, |blame, cx| blame.max_author_length(cx));
                    Some(max_author_length)
                } else {
                    None
                }
            })
            .flatten();

        let display_snapshot = self.display_map.update(cx, |map, cx| map.snapshot(cx));

        EditorSnapshot {
            mode: self.mode.clone(),
            show_gutter: self.show_gutter,
            offset_content: self.offset_content,
            show_line_numbers: self.show_line_numbers,
            number_deleted_lines: self.number_deleted_lines,
            show_git_diff_gutter: self.show_git_diff_gutter,
            semantic_tokens_enabled: self.semantic_token_state.enabled(),
            show_code_actions: self.show_code_actions,
            show_runnables: self.show_runnables,
            show_breakpoints: self.show_breakpoints,
            git_blame_gutter_max_author_length,
            scroll_anchor: self.scroll_manager.shared_scroll_anchor(cx),
            display_snapshot,
            placeholder_display_snapshot: self
                .placeholder_display_map
                .as_ref()
                .map(|display_map| display_map.update(cx, |map, cx| map.snapshot(cx))),
            ongoing_scroll: self.scroll_manager.ongoing_scroll(),
            is_focused: self.focus_handle.is_focused(window),
            current_line_highlight: self
                .current_line_highlight
                .unwrap_or_else(|| EditorSettings::get_global(cx).current_line_highlight),
            gutter_hovered: self.gutter_hovered,
        }
    }

    pub fn language_at<T: ToOffset>(&self, point: T, cx: &App) -> Option<Arc<Language>> {
        self.buffer.read(cx).language_at(point, cx)
    }

    pub fn file_at<T: ToOffset>(&self, point: T, cx: &App) -> Option<Arc<dyn language::File>> {
        self.buffer.read(cx).read(cx).file_at(point).cloned()
    }

    pub fn active_buffer(&self, cx: &App) -> Option<Entity<Buffer>> {
        let multibuffer = self.buffer.read(cx);
        let snapshot = multibuffer.snapshot(cx);
        let (anchor, _) =
            snapshot.anchor_to_buffer_anchor(self.selections.newest_anchor().head())?;
        multibuffer.buffer(anchor.buffer_id)
    }

    pub fn mode(&self) -> &EditorMode {
        &self.mode
    }

    pub fn set_mode(&mut self, mode: EditorMode) {
        self.mode = mode;
    }

    pub fn collaboration_hub(&self) -> Option<&dyn CollaborationHub> {
        self.collaboration_hub.as_deref()
    }

    pub fn set_collaboration_hub(&mut self, hub: Box<dyn CollaborationHub>) {
        self.collaboration_hub = Some(hub);
    }

    pub fn set_in_project_search(&mut self, in_project_search: bool) {
        self.in_project_search = in_project_search;
    }

    pub fn set_custom_context_menu(
        &mut self,
        f: impl 'static
        + Fn(
            &mut Self,
            DisplayPoint,
            &mut Window,
            &mut Context<Self>,
        ) -> Option<Entity<ui::ContextMenu>>,
    ) {
        self.custom_context_menu = Some(Box::new(f))
    }

    pub fn set_completion_provider(&mut self, provider: Option<Rc<dyn CompletionProvider>>) {
        self.completion_provider = provider;
    }

    #[cfg(any(test, feature = "test-support"))]
    pub fn completion_provider(&self) -> Option<Rc<dyn CompletionProvider>> {
        self.completion_provider.clone()
    }

    pub fn semantics_provider(&self) -> Option<Rc<dyn SemanticsProvider>> {
        self.semantics_provider.clone()
    }

    pub fn set_semantics_provider(&mut self, provider: Option<Rc<dyn SemanticsProvider>>) {
        self.semantics_provider = provider;
    }

    pub fn set_edit_prediction_provider<T>(
        &mut self,
        provider: Option<Entity<T>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) where
        T: EditPredictionDelegate,
    {
        self.edit_prediction_provider = provider.map(|provider| RegisteredEditPredictionDelegate {
            _subscription: cx.observe_in(&provider, window, |this, _, window, cx| {
                if this.focus_handle.is_focused(window) {
                    this.update_visible_edit_prediction(window, cx);
                }
            }),
            provider: Arc::new(provider),
        });
        self.update_edit_prediction_settings(cx);
        self.refresh_edit_prediction(false, false, window, cx);
    }

    pub fn placeholder_text(&self, cx: &mut App) -> Option<String> {
        self.placeholder_display_map
            .as_ref()
            .map(|display_map| display_map.update(cx, |map, cx| map.snapshot(cx)).text())
    }

    pub fn set_placeholder_text(
        &mut self,
        placeholder_text: &str,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let multibuffer = cx
            .new(|cx| MultiBuffer::singleton(cx.new(|cx| Buffer::local(placeholder_text, cx)), cx));

        let style = window.text_style();

        self.placeholder_display_map = Some(cx.new(|cx| {
            DisplayMap::new(
                multibuffer,
                style.font(),
                style.font_size.to_pixels(window.rem_size()),
                None,
                FILE_HEADER_HEIGHT,
                MULTI_BUFFER_EXCERPT_HEADER_HEIGHT,
                Default::default(),
                DiagnosticSeverity::Off,
                cx,
            )
        }));
        cx.notify();
    }

    pub fn set_cursor_shape(&mut self, cursor_shape: CursorShape, cx: &mut Context<Self>) {
        self.cursor_shape = cursor_shape;

        // Disrupt blink for immediate user feedback that the cursor shape has changed
        self.blink_manager.update(cx, BlinkManager::show_cursor);

        cx.notify();
    }

    pub fn cursor_shape(&self) -> CursorShape {
        self.cursor_shape
    }

    pub fn set_cursor_offset_on_selection(&mut self, set_cursor_offset_on_selection: bool) {
        self.cursor_offset_on_selection = set_cursor_offset_on_selection;
    }

    pub fn set_current_line_highlight(
        &mut self,
        current_line_highlight: Option<CurrentLineHighlight>,
    ) {
        self.current_line_highlight = current_line_highlight;
    }

    pub fn set_collapse_matches(&mut self, collapse_matches: bool) {
        self.collapse_matches = collapse_matches;
    }

    pub fn range_for_match<T: std::marker::Copy>(&self, range: &Range<T>) -> Range<T> {
        if self.collapse_matches {
            return range.start..range.start;
        }
        range.clone()
    }

    pub fn clip_at_line_ends(&mut self, cx: &mut Context<Self>) -> bool {
        self.display_map.read(cx).clip_at_line_ends
    }

    pub fn set_clip_at_line_ends(&mut self, clip: bool, cx: &mut Context<Self>) {
        if self.display_map.read(cx).clip_at_line_ends != clip {
            self.display_map
                .update(cx, |map, _| map.clip_at_line_ends = clip);
        }
    }

    pub fn set_input_enabled(&mut self, input_enabled: bool) {
        self.input_enabled = input_enabled;
    }

    pub fn set_expects_character_input(&mut self, expects_character_input: bool) {
        self.expects_character_input = expects_character_input;
    }

    pub fn set_edit_predictions_hidden_for_vim_mode(
        &mut self,
        hidden: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if hidden != self.edit_predictions_hidden_for_vim_mode {
            self.edit_predictions_hidden_for_vim_mode = hidden;
            if hidden {
                self.update_visible_edit_prediction(window, cx);
            } else {
                self.refresh_edit_prediction(true, false, window, cx);
            }
        }
    }

    pub fn set_menu_edit_predictions_policy(&mut self, value: MenuEditPredictionsPolicy) {
        self.menu_edit_predictions_policy = value;
    }

    pub fn set_autoindent(&mut self, autoindent: bool) {
        if autoindent {
            self.autoindent_mode = Some(AutoindentMode::EachLine);
        } else {
            self.autoindent_mode = None;
        }
    }

    pub fn capability(&self, cx: &App) -> Capability {
        if self.read_only {
            Capability::ReadOnly
        } else {
            self.buffer.read(cx).capability()
        }
    }

    pub fn read_only(&self, cx: &App) -> bool {
        self.read_only || self.buffer.read(cx).read_only()
    }

    pub fn set_read_only(&mut self, read_only: bool) {
        self.read_only = read_only;
    }

    pub fn set_use_autoclose(&mut self, autoclose: bool) {
        self.use_autoclose = autoclose;
    }

    pub fn set_use_selection_highlight(&mut self, highlight: bool) {
        self.use_selection_highlight = highlight;
    }

    pub fn set_use_auto_surround(&mut self, auto_surround: bool) {
        self.use_auto_surround = auto_surround;
    }

    pub fn set_auto_replace_emoji_shortcode(&mut self, auto_replace: bool) {
        self.auto_replace_emoji_shortcode = auto_replace;
    }

    pub fn set_should_serialize(&mut self, should_serialize: bool, cx: &App) {
        self.buffer_serialization = should_serialize.then(|| {
            BufferSerialization::new(
                ProjectSettings::get_global(cx)
                    .session
                    .restore_unsaved_buffers,
            )
        })
    }

    fn should_serialize_buffer(&self) -> bool {
        self.buffer_serialization.is_some()
    }

    pub fn toggle_edit_predictions(
        &mut self,
        _: &ToggleEditPrediction,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.show_edit_predictions_override.is_some() {
            self.set_show_edit_predictions(None, window, cx);
        } else {
            let show_edit_predictions = !self.edit_predictions_enabled();
            self.set_show_edit_predictions(Some(show_edit_predictions), window, cx);
        }
    }

    pub fn set_show_completions_on_input(&mut self, show_completions_on_input: Option<bool>) {
        self.show_completions_on_input_override = show_completions_on_input;
    }

    pub fn set_show_edit_predictions(
        &mut self,
        show_edit_predictions: Option<bool>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.show_edit_predictions_override = show_edit_predictions;
        self.update_edit_prediction_settings(cx);

        if let Some(false) = show_edit_predictions {
            self.discard_edit_prediction(EditPredictionDiscardReason::Ignored, cx);
        } else {
            self.refresh_edit_prediction(false, true, window, cx);
        }
    }

    fn edit_predictions_disabled_in_scope(
        &self,
        buffer: &Entity<Buffer>,
        buffer_position: language::Anchor,
        cx: &App,
    ) -> bool {
        let snapshot = buffer.read(cx).snapshot();
        let settings = snapshot.settings_at(buffer_position, cx);

        let Some(scope) = snapshot.language_scope_at(buffer_position) else {
            return false;
        };

        scope.override_name().is_some_and(|scope_name| {
            settings
                .edit_predictions_disabled_in
                .iter()
                .any(|s| s == scope_name)
        })
    }

    pub fn set_use_modal_editing(&mut self, to: bool) {
        self.use_modal_editing = to;
    }

    pub fn use_modal_editing(&self) -> bool {
        self.use_modal_editing
    }

    fn selections_did_change(
        &mut self,
        local: bool,
        old_cursor_position: &Anchor,
        effects: SelectionEffects,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        window.invalidate_character_coordinates();

        // Copy selections to primary selection buffer
        #[cfg(any(target_os = "linux", target_os = "freebsd"))]
        if local {
            let selections = self
                .selections
                .all::<MultiBufferOffset>(&self.display_snapshot(cx));
            let buffer_handle = self.buffer.read(cx).read(cx);

            let mut text = String::new();
            for (index, selection) in selections.iter().enumerate() {
                let text_for_selection = buffer_handle
                    .text_for_range(selection.start..selection.end)
                    .collect::<String>();

                text.push_str(&text_for_selection);
                if index != selections.len() - 1 {
                    text.push('\n');
                }
            }

            if !text.is_empty() {
                cx.write_to_primary(ClipboardItem::new_string(text));
            }
        }

        let selection_anchors = self.selections.disjoint_anchors_arc();

        if self.focus_handle.is_focused(window) && self.leader_id.is_none() {
            self.buffer.update(cx, |buffer, cx| {
                buffer.set_active_selections(
                    &selection_anchors,
                    self.selections.line_mode(),
                    self.cursor_shape,
                    cx,
                )
            });
        }
        let display_map = self
            .display_map
            .update(cx, |display_map, cx| display_map.snapshot(cx));
        let buffer = display_map.buffer_snapshot();
        if self.selections.count() == 1 {
            self.add_selections_state = None;
        }
        self.select_next_state = None;
        self.select_prev_state = None;
        self.select_syntax_node_history.try_clear();
        self.invalidate_autoclose_regions(&selection_anchors, buffer);
        self.snippet_stack.invalidate(&selection_anchors, buffer);
        self.take_rename(false, window, cx);

        let newest_selection = self.selections.newest_anchor();
        let new_cursor_position = newest_selection.head();
        let selection_start = newest_selection.start;

        if effects.nav_history.is_none() || effects.nav_history == Some(true) {
            self.push_to_nav_history(
                *old_cursor_position,
                Some(new_cursor_position.to_point(buffer)),
                false,
                effects.nav_history == Some(true),
                cx,
            );
        }

        if local {
            if let Some((anchor, _)) = buffer.anchor_to_buffer_anchor(new_cursor_position) {
                self.register_buffer(anchor.buffer_id, cx);
            }

            let mut context_menu = self.context_menu.borrow_mut();
            let completion_menu = match context_menu.as_ref() {
                Some(CodeContextMenu::Completions(menu)) => Some(menu),
                Some(CodeContextMenu::CodeActions(_)) => {
                    *context_menu = None;
                    None
                }
                None => None,
            };
            let completion_position = completion_menu.map(|menu| menu.initial_position);
            drop(context_menu);

            if effects.completions
                && let Some(completion_position) = completion_position
            {
                let start_offset = selection_start.to_offset(buffer);
                let position_matches = start_offset == completion_position.to_offset(buffer);
                let continue_showing = if let Some((snap, ..)) =
                    buffer.point_to_buffer_offset(completion_position)
                    && !snap.capability.editable()
                {
                    false
                } else if position_matches {
                    if self.snippet_stack.is_empty() {
                        buffer.char_kind_before(start_offset, Some(CharScopeContext::Completion))
                            == Some(CharKind::Word)
                    } else {
                        // Snippet choices can be shown even when the cursor is in whitespace.
                        // Dismissing the menu with actions like backspace is handled by
                        // invalidation regions.
                        true
                    }
                } else {
                    false
                };

                if continue_showing {
                    self.open_or_update_completions_menu(None, None, false, window, cx);
                } else {
                    self.hide_context_menu(window, cx);
                }
            }

            hide_hover(self, cx);

            if old_cursor_position.to_display_point(&display_map).row()
                != new_cursor_position.to_display_point(&display_map).row()
            {
                self.available_code_actions.take();
            }
            self.refresh_code_actions(window, cx);
            self.refresh_document_highlights(cx);
            refresh_linked_ranges(self, window, cx);

            self.refresh_selected_text_highlights(&display_map, false, window, cx);
            self.refresh_matching_bracket_highlights(&display_map, cx);
            self.refresh_outline_symbols_at_cursor(cx);
            self.update_visible_edit_prediction(window, cx);
            self.inline_blame_popover.take();
            if self.git_blame_inline_enabled {
                self.start_inline_blame_timer(window, cx);
            }
        }

        self.blink_manager.update(cx, BlinkManager::pause_blinking);

        if local && !self.suppress_selection_callback {
            if let Some(callback) = self.on_local_selections_changed.as_ref() {
                let cursor_position = self.selections.newest::<Point>(&display_map).head();
                callback(cursor_position, window, cx);
            }
        }

        cx.emit(EditorEvent::SelectionsChanged { local });

        let selections = &self.selections.disjoint_anchors_arc();
        if selections.len() == 1 {
            cx.emit(SearchEvent::ActiveMatchChanged)
        }
        if local && let Some(buffer_snapshot) = buffer.as_singleton() {
            let inmemory_selections = selections
                .iter()
                .map(|s| {
                    let start = s.range().start.text_anchor_in(buffer_snapshot);
                    let end = s.range().end.text_anchor_in(buffer_snapshot);
                    (start..end).to_point(buffer_snapshot)
                })
                .collect();
            self.update_restoration_data(cx, |data| {
                data.selections = inmemory_selections;
            });

            if WorkspaceSettings::get(None, cx).restore_on_startup
                != RestoreOnStartupBehavior::EmptyTab
                && let Some(workspace_id) = self.workspace_serialization_id(cx)
            {
                let snapshot = self.buffer().read(cx).snapshot(cx);
                let selections = selections.clone();
                let background_executor = cx.background_executor().clone();
                let editor_id = cx.entity().entity_id().as_u64() as ItemId;
                let db = EditorDb::global(cx);
                self.serialize_selections = cx.background_spawn(async move {
                    background_executor.timer(SERIALIZATION_THROTTLE_TIME).await;
                    let db_selections = selections
                        .iter()
                        .map(|selection| {
                            (
                                selection.start.to_offset(&snapshot).0,
                                selection.end.to_offset(&snapshot).0,
                            )
                        })
                        .collect();

                    db.save_editor_selections(editor_id, workspace_id, db_selections)
                        .await
                        .with_context(|| {
                            format!(
                                "persisting editor selections for editor {editor_id}, \
                                workspace {workspace_id:?}"
                            )
                        })
                        .log_err();
                });
            }
        }

        cx.notify();
    }

    fn folds_did_change(&mut self, cx: &mut Context<Self>) {
        use text::ToOffset as _;

        if self.mode.is_minimap()
            || WorkspaceSettings::get(None, cx).restore_on_startup
                == RestoreOnStartupBehavior::EmptyTab
        {
            return;
        }

        let display_snapshot = self
            .display_map
            .update(cx, |display_map, cx| display_map.snapshot(cx));
        let Some(buffer_snapshot) = display_snapshot.buffer_snapshot().as_singleton() else {
            return;
        };
        let inmemory_folds = display_snapshot
            .folds_in_range(MultiBufferOffset(0)..display_snapshot.buffer_snapshot().len())
            .map(|fold| {
                let start = fold.range.start.text_anchor_in(buffer_snapshot);
                let end = fold.range.end.text_anchor_in(buffer_snapshot);
                (start..end).to_point(buffer_snapshot)
            })
            .collect();
        self.update_restoration_data(cx, |data| {
            data.folds = inmemory_folds;
        });

        let Some(workspace_id) = self.workspace_serialization_id(cx) else {
            return;
        };

        // Get file path for path-based fold storage (survives tab close)
        let Some(file_path) = self.buffer().read(cx).as_singleton().and_then(|buffer| {
            project::File::from_dyn(buffer.read(cx).file())
                .map(|file| Arc::<Path>::from(file.abs_path(cx)))
        }) else {
            return;
        };

        let background_executor = cx.background_executor().clone();
        const FINGERPRINT_LEN: usize = 32;
        let db_folds = display_snapshot
            .folds_in_range(MultiBufferOffset(0)..display_snapshot.buffer_snapshot().len())
            .map(|fold| {
                let start = fold
                    .range
                    .start
                    .text_anchor_in(buffer_snapshot)
                    .to_offset(buffer_snapshot);
                let end = fold
                    .range
                    .end
                    .text_anchor_in(buffer_snapshot)
                    .to_offset(buffer_snapshot);

                // Extract fingerprints - content at fold boundaries for validation on restore
                // Both fingerprints must be INSIDE the fold to avoid capturing surrounding
                // content that might change independently.
                // start_fp: first min(32, fold_len) bytes of fold content
                // end_fp: last min(32, fold_len) bytes of fold content
                // Clip to character boundaries to handle multibyte UTF-8 characters.
                let fold_len = end - start;
                let start_fp_end = buffer_snapshot
                    .clip_offset(start + std::cmp::min(FINGERPRINT_LEN, fold_len), Bias::Left);
                let start_fp: String = buffer_snapshot
                    .text_for_range(start..start_fp_end)
                    .collect();
                let end_fp_start = buffer_snapshot
                    .clip_offset(end.saturating_sub(FINGERPRINT_LEN).max(start), Bias::Right);
                let end_fp: String = buffer_snapshot.text_for_range(end_fp_start..end).collect();

                (start, end, start_fp, end_fp)
            })
            .collect::<Vec<_>>();
        let db = EditorDb::global(cx);
        self.serialize_folds = cx.background_spawn(async move {
            background_executor.timer(SERIALIZATION_THROTTLE_TIME).await;
            if db_folds.is_empty() {
                // No folds - delete any persisted folds for this file
                db.delete_file_folds(workspace_id, file_path)
                    .await
                    .with_context(|| format!("deleting file folds for workspace {workspace_id:?}"))
                    .log_err();
            } else {
                db.save_file_folds(workspace_id, file_path, db_folds)
                    .await
                    .with_context(|| {
                        format!("persisting file folds for workspace {workspace_id:?}")
                    })
                    .log_err();
            }
        });
    }

    pub fn sync_selections(
        &mut self,
        other: Entity<Editor>,
        cx: &mut Context<Self>,
    ) -> gpui::Subscription {
        let other_selections = other.read(cx).selections.disjoint_anchors().to_vec();
        if !other_selections.is_empty() {
            self.selections
                .change_with(&self.display_snapshot(cx), |selections| {
                    selections.select_anchors(other_selections);
                });
        }

        let other_subscription = cx.subscribe(&other, |this, other, other_evt, cx| {
            if let EditorEvent::SelectionsChanged { local: true } = other_evt {
                let other_selections = other.read(cx).selections.disjoint_anchors().to_vec();
                if other_selections.is_empty() {
                    return;
                }
                let snapshot = this.display_snapshot(cx);
                this.selections.change_with(&snapshot, |selections| {
                    selections.select_anchors(other_selections);
                });
            }
        });

        let this_subscription = cx.subscribe_self::<EditorEvent>(move |this, this_evt, cx| {
            if let EditorEvent::SelectionsChanged { local: true } = this_evt {
                let these_selections = this.selections.disjoint_anchors().to_vec();
                if these_selections.is_empty() {
                    return;
                }
                other.update(cx, |other_editor, cx| {
                    let snapshot = other_editor.display_snapshot(cx);
                    other_editor
                        .selections
                        .change_with(&snapshot, |selections| {
                            selections.select_anchors(these_selections);
                        })
                });
            }
        });

        Subscription::join(other_subscription, this_subscription)
    }

    fn unfold_buffers_with_selections(&mut self, cx: &mut Context<Self>) {
        if self.buffer().read(cx).is_singleton() {
            return;
        }
        let snapshot = self.buffer.read(cx).snapshot(cx);
        let buffer_ids: HashSet<BufferId> = self
            .selections
            .disjoint_anchor_ranges()
            .flat_map(|range| snapshot.buffer_ids_for_range(range))
            .collect();
        for buffer_id in buffer_ids {
            self.unfold_buffer(buffer_id, cx);
        }
    }

    /// Changes selections using the provided mutation function. Changes to `self.selections` occur
    /// immediately, but when run within `transact` or `with_selection_effects_deferred` other
    /// effects of selection change occur at the end of the transaction.
    pub fn change_selections<R>(
        &mut self,
        effects: SelectionEffects,
        window: &mut Window,
        cx: &mut Context<Self>,
        change: impl FnOnce(&mut MutableSelectionsCollection<'_, '_>) -> R,
    ) -> R {
        let snapshot = self.display_snapshot(cx);
        if let Some(state) = &mut self.deferred_selection_effects_state {
            state.effects.scroll = effects.scroll.or(state.effects.scroll);
            state.effects.completions = effects.completions;
            state.effects.nav_history = effects.nav_history.or(state.effects.nav_history);
            let (changed, result) = self.selections.change_with(&snapshot, change);
            state.changed |= changed;
            return result;
        }
        let mut state = DeferredSelectionEffectsState {
            changed: false,
            effects,
            old_cursor_position: self.selections.newest_anchor().head(),
            history_entry: SelectionHistoryEntry {
                selections: self.selections.disjoint_anchors_arc(),
                select_next_state: self.select_next_state.clone(),
                select_prev_state: self.select_prev_state.clone(),
                add_selections_state: self.add_selections_state.clone(),
            },
        };
        let (changed, result) = self.selections.change_with(&snapshot, change);
        state.changed = state.changed || changed;
        if self.defer_selection_effects {
            self.deferred_selection_effects_state = Some(state);
        } else {
            self.apply_selection_effects(state, window, cx);
        }
        result
    }

    /// Defers the effects of selection change, so that the effects of multiple calls to
    /// `change_selections` are applied at the end. This way these intermediate states aren't added
    /// to selection history and the state of popovers based on selection position aren't
    /// erroneously updated.
    pub fn with_selection_effects_deferred<R>(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
        update: impl FnOnce(&mut Self, &mut Window, &mut Context<Self>) -> R,
    ) -> R {
        let already_deferred = self.defer_selection_effects;
        self.defer_selection_effects = true;
        let result = update(self, window, cx);
        if !already_deferred {
            self.defer_selection_effects = false;
            if let Some(state) = self.deferred_selection_effects_state.take() {
                self.apply_selection_effects(state, window, cx);
            }
        }
        result
    }

    fn apply_selection_effects(
        &mut self,
        state: DeferredSelectionEffectsState,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if state.changed {
            self.selection_history.push(state.history_entry);

            if let Some(autoscroll) = state.effects.scroll {
                self.request_autoscroll(autoscroll, cx);
            }

            let old_cursor_position = &state.old_cursor_position;

            self.selections_did_change(true, old_cursor_position, state.effects, window, cx);

            if self.should_open_signature_help_automatically(old_cursor_position, cx) {
                self.show_signature_help_auto(window, cx);
            }
        }
    }

    pub fn edit<I, S, T>(&mut self, edits: I, cx: &mut Context<Self>)
    where
        I: IntoIterator<Item = (Range<S>, T)>,
        S: ToOffset,
        T: Into<Arc<str>>,
    {
        if self.read_only(cx) {
            return;
        }

        self.buffer
            .update(cx, |buffer, cx| buffer.edit(edits, None, cx));
    }

    pub fn edit_with_autoindent<I, S, T>(&mut self, edits: I, cx: &mut Context<Self>)
    where
        I: IntoIterator<Item = (Range<S>, T)>,
        S: ToOffset,
        T: Into<Arc<str>>,
    {
        if self.read_only(cx) {
            return;
        }

        self.buffer.update(cx, |buffer, cx| {
            buffer.edit(edits, self.autoindent_mode.clone(), cx)
        });
    }

    pub fn edit_with_block_indent<I, S, T>(
        &mut self,
        edits: I,
        original_indent_columns: Vec<Option<u32>>,
        cx: &mut Context<Self>,
    ) where
        I: IntoIterator<Item = (Range<S>, T)>,
        S: ToOffset,
        T: Into<Arc<str>>,
    {
        if self.read_only(cx) {
            return;
        }

        self.buffer.update(cx, |buffer, cx| {
            buffer.edit(
                edits,
                Some(AutoindentMode::Block {
                    original_indent_columns,
                }),
                cx,
            )
        });
    }

    fn select(&mut self, phase: SelectPhase, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_context_menu(window, cx);

        match phase {
            SelectPhase::Begin {
                position,
                add,
                click_count,
            } => self.begin_selection(position, add, click_count, window, cx),
            SelectPhase::BeginColumnar {
                position,
                goal_column,
                reset,
                mode,
            } => self.begin_columnar_selection(position, goal_column, reset, mode, window, cx),
            SelectPhase::Extend {
                position,
                click_count,
            } => self.extend_selection(position, click_count, window, cx),
            SelectPhase::Update {
                position,
                goal_column,
                scroll_delta,
            } => self.update_selection(position, goal_column, scroll_delta, window, cx),
            SelectPhase::End => self.end_selection(window, cx),
        }
    }

    fn extend_selection(
        &mut self,
        position: DisplayPoint,
        click_count: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let tail = self
            .selections
            .newest::<MultiBufferOffset>(&display_map)
            .tail();
        let click_count = click_count.max(match self.selections.select_mode() {
            SelectMode::Character => 1,
            SelectMode::Word(_) => 2,
            SelectMode::Line(_) => 3,
            SelectMode::All => 4,
        });
        self.begin_selection(position, false, click_count, window, cx);

        let tail_anchor = display_map.buffer_snapshot().anchor_before(tail);

        let current_selection = match self.selections.select_mode() {
            SelectMode::Character | SelectMode::All => tail_anchor..tail_anchor,
            SelectMode::Word(range) | SelectMode::Line(range) => range.clone(),
        };

        let mut pending_selection = self
            .selections
            .pending_anchor()
            .cloned()
            .expect("extend_selection not called with pending selection");

        if pending_selection
            .start
            .cmp(&current_selection.start, display_map.buffer_snapshot())
            == Ordering::Greater
        {
            pending_selection.start = current_selection.start;
        }
        if pending_selection
            .end
            .cmp(&current_selection.end, display_map.buffer_snapshot())
            == Ordering::Less
        {
            pending_selection.end = current_selection.end;
            pending_selection.reversed = true;
        }

        let mut pending_mode = self.selections.pending_mode().unwrap();
        match &mut pending_mode {
            SelectMode::Word(range) | SelectMode::Line(range) => *range = current_selection,
            _ => {}
        }

        let effects = if EditorSettings::get_global(cx).autoscroll_on_clicks {
            SelectionEffects::scroll(Autoscroll::fit())
        } else {
            SelectionEffects::no_scroll()
        };

        self.change_selections(effects, window, cx, |s| {
            s.set_pending(pending_selection.clone(), pending_mode);
            s.set_is_extending(true);
        });
    }

    fn begin_selection(
        &mut self,
        position: DisplayPoint,
        add: bool,
        click_count: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.focus_handle.is_focused(window) {
            self.last_focused_descendant = None;
            window.focus(&self.focus_handle, cx);
        }

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = display_map.buffer_snapshot();
        let position = display_map.clip_point(position, Bias::Left);

        let start;
        let end;
        let mode;
        let mut auto_scroll;
        match click_count {
            1 => {
                start = buffer.anchor_before(position.to_point(&display_map));
                end = start;
                mode = SelectMode::Character;
                auto_scroll = true;
            }
            2 => {
                let position = display_map
                    .clip_point(position, Bias::Left)
                    .to_offset(&display_map, Bias::Left);
                let (range, _) = buffer.surrounding_word(position, None);
                start = buffer.anchor_before(range.start);
                end = buffer.anchor_before(range.end);
                mode = SelectMode::Word(start..end);
                auto_scroll = true;
            }
            3 => {
                let position = display_map
                    .clip_point(position, Bias::Left)
                    .to_point(&display_map);
                let line_start = display_map.prev_line_boundary(position).0;
                let next_line_start = buffer.clip_point(
                    display_map.next_line_boundary(position).0 + Point::new(1, 0),
                    Bias::Left,
                );
                start = buffer.anchor_before(line_start);
                end = buffer.anchor_before(next_line_start);
                mode = SelectMode::Line(start..end);
                auto_scroll = true;
            }
            _ => {
                start = buffer.anchor_before(MultiBufferOffset(0));
                end = buffer.anchor_before(buffer.len());
                mode = SelectMode::All;
                auto_scroll = false;
            }
        }
        auto_scroll &= EditorSettings::get_global(cx).autoscroll_on_clicks;

        let point_to_delete: Option<usize> = {
            let selected_points: Vec<Selection<Point>> =
                self.selections.disjoint_in_range(start..end, &display_map);

            if !add || click_count > 1 {
                None
            } else if !selected_points.is_empty() {
                Some(selected_points[0].id)
            } else {
                let clicked_point_already_selected =
                    self.selections.disjoint_anchors().iter().find(|selection| {
                        selection.start.to_point(buffer) == start.to_point(buffer)
                            || selection.end.to_point(buffer) == end.to_point(buffer)
                    });

                clicked_point_already_selected.map(|selection| selection.id)
            }
        };

        let selections_count = self.selections.count();
        let effects = if auto_scroll {
            SelectionEffects::default()
        } else {
            SelectionEffects::no_scroll()
        };

        self.change_selections(effects, window, cx, |s| {
            if let Some(point_to_delete) = point_to_delete {
                s.delete(point_to_delete);

                if selections_count == 1 {
                    s.set_pending_anchor_range(start..end, mode);
                }
            } else {
                if !add {
                    s.clear_disjoint();
                }

                s.set_pending_anchor_range(start..end, mode);
            }
        });
    }

    fn begin_columnar_selection(
        &mut self,
        position: DisplayPoint,
        goal_column: u32,
        reset: bool,
        mode: ColumnarMode,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.focus_handle.is_focused(window) {
            self.last_focused_descendant = None;
            window.focus(&self.focus_handle, cx);
        }

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));

        if reset {
            let pointer_position = display_map
                .buffer_snapshot()
                .anchor_before(position.to_point(&display_map));

            self.change_selections(
                SelectionEffects::scroll(Autoscroll::newest()),
                window,
                cx,
                |s| {
                    s.clear_disjoint();
                    s.set_pending_anchor_range(
                        pointer_position..pointer_position,
                        SelectMode::Character,
                    );
                },
            );
        };

        let tail = self.selections.newest::<Point>(&display_map).tail();
        let selection_anchor = display_map.buffer_snapshot().anchor_before(tail);
        self.columnar_selection_state = match mode {
            ColumnarMode::FromMouse => Some(ColumnarSelectionState::FromMouse {
                selection_tail: selection_anchor,
                display_point: if reset {
                    if position.column() != goal_column {
                        Some(DisplayPoint::new(position.row(), goal_column))
                    } else {
                        None
                    }
                } else {
                    None
                },
            }),
            ColumnarMode::FromSelection => Some(ColumnarSelectionState::FromSelection {
                selection_tail: selection_anchor,
            }),
        };

        if !reset {
            self.select_columns(position, goal_column, &display_map, window, cx);
        }
    }

    fn update_selection(
        &mut self,
        position: DisplayPoint,
        goal_column: u32,
        scroll_delta: gpui::Point<f32>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));

        if self.columnar_selection_state.is_some() {
            self.select_columns(position, goal_column, &display_map, window, cx);
        } else if let Some(mut pending) = self.selections.pending_anchor().cloned() {
            let buffer = display_map.buffer_snapshot();
            let head;
            let tail;
            let mode = self.selections.pending_mode().unwrap();
            match &mode {
                SelectMode::Character => {
                    head = position.to_point(&display_map);
                    tail = pending.tail().to_point(buffer);
                }
                SelectMode::Word(original_range) => {
                    let offset = display_map
                        .clip_point(position, Bias::Left)
                        .to_offset(&display_map, Bias::Left);
                    let original_range = original_range.to_offset(buffer);

                    let head_offset = if buffer.is_inside_word(offset, None)
                        || original_range.contains(&offset)
                    {
                        let (word_range, _) = buffer.surrounding_word(offset, None);
                        if word_range.start < original_range.start {
                            word_range.start
                        } else {
                            word_range.end
                        }
                    } else {
                        offset
                    };

                    head = head_offset.to_point(buffer);
                    if head_offset <= original_range.start {
                        tail = original_range.end.to_point(buffer);
                    } else {
                        tail = original_range.start.to_point(buffer);
                    }
                }
                SelectMode::Line(original_range) => {
                    let original_range = original_range.to_point(display_map.buffer_snapshot());

                    let position = display_map
                        .clip_point(position, Bias::Left)
                        .to_point(&display_map);
                    let line_start = display_map.prev_line_boundary(position).0;
                    let next_line_start = buffer.clip_point(
                        display_map.next_line_boundary(position).0 + Point::new(1, 0),
                        Bias::Left,
                    );

                    if line_start < original_range.start {
                        head = line_start
                    } else {
                        head = next_line_start
                    }

                    if head <= original_range.start {
                        tail = original_range.end;
                    } else {
                        tail = original_range.start;
                    }
                }
                SelectMode::All => {
                    return;
                }
            };

            if head < tail {
                pending.start = buffer.anchor_before(head);
                pending.end = buffer.anchor_before(tail);
                pending.reversed = true;
            } else {
                pending.start = buffer.anchor_before(tail);
                pending.end = buffer.anchor_before(head);
                pending.reversed = false;
            }

            self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                s.set_pending(pending.clone(), mode);
            });
        } else {
            log::error!("update_selection dispatched with no pending selection");
            return;
        }

        self.apply_scroll_delta(scroll_delta, window, cx);
        cx.notify();
    }

    fn end_selection(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.columnar_selection_state.take();
        if let Some(pending_mode) = self.selections.pending_mode() {
            let selections = self
                .selections
                .all::<MultiBufferOffset>(&self.display_snapshot(cx));
            self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                s.select(selections);
                s.clear_pending();
                if s.is_extending() {
                    s.set_is_extending(false);
                } else {
                    s.set_select_mode(pending_mode);
                }
            });
        }
    }

    fn select_columns(
        &mut self,
        head: DisplayPoint,
        goal_column: u32,
        display_map: &DisplaySnapshot,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(columnar_state) = self.columnar_selection_state.as_ref() else {
            return;
        };

        let tail = match columnar_state {
            ColumnarSelectionState::FromMouse {
                selection_tail,
                display_point,
            } => display_point.unwrap_or_else(|| selection_tail.to_display_point(display_map)),
            ColumnarSelectionState::FromSelection { selection_tail } => {
                selection_tail.to_display_point(display_map)
            }
        };

        let start_row = cmp::min(tail.row(), head.row());
        let end_row = cmp::max(tail.row(), head.row());
        let start_column = cmp::min(tail.column(), goal_column);
        let end_column = cmp::max(tail.column(), goal_column);
        let reversed = start_column < tail.column();

        let selection_ranges = (start_row.0..=end_row.0)
            .map(DisplayRow)
            .filter_map(|row| {
                if (matches!(columnar_state, ColumnarSelectionState::FromMouse { .. })
                    || start_column <= display_map.line_len(row))
                    && !display_map.is_block_line(row)
                {
                    let start = display_map
                        .clip_point(DisplayPoint::new(row, start_column), Bias::Left)
                        .to_point(display_map);
                    let end = display_map
                        .clip_point(DisplayPoint::new(row, end_column), Bias::Right)
                        .to_point(display_map);
                    if reversed {
                        Some(end..start)
                    } else {
                        Some(start..end)
                    }
                } else {
                    None
                }
            })
            .collect::<Vec<_>>();
        if selection_ranges.is_empty() {
            return;
        }

        let ranges = match columnar_state {
            ColumnarSelectionState::FromMouse { .. } => {
                let mut non_empty_ranges = selection_ranges
                    .iter()
                    .filter(|selection_range| selection_range.start != selection_range.end)
                    .peekable();
                if non_empty_ranges.peek().is_some() {
                    non_empty_ranges.cloned().collect()
                } else {
                    selection_ranges
                }
            }
            _ => selection_ranges,
        };

        self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
            s.select_ranges(ranges);
        });
        cx.notify();
    }

    pub fn has_non_empty_selection(&self, snapshot: &DisplaySnapshot) -> bool {
        self.selections
            .all_adjusted(snapshot)
            .iter()
            .any(|selection| !selection.is_empty())
    }

    pub fn has_pending_nonempty_selection(&self) -> bool {
        let pending_nonempty_selection = match self.selections.pending_anchor() {
            Some(Selection { start, end, .. }) => start != end,
            None => false,
        };

        pending_nonempty_selection
            || (self.columnar_selection_state.is_some()
                && self.selections.disjoint_anchors().len() > 1)
    }

    pub fn has_pending_selection(&self) -> bool {
        self.selections.pending_anchor().is_some() || self.columnar_selection_state.is_some()
    }

    pub fn cancel(&mut self, _: &Cancel, window: &mut Window, cx: &mut Context<Self>) {
        self.selection_mark_mode = false;
        self.selection_drag_state = SelectionDragState::None;

        if self.dismiss_menus_and_popups(true, window, cx) {
            cx.notify();
            return;
        }
        if self.clear_expanded_diff_hunks(cx) {
            cx.notify();
            return;
        }
        if self.show_git_blame_gutter {
            self.show_git_blame_gutter = false;
            cx.notify();
            return;
        }

        if self.mode.is_full()
            && self.change_selections(Default::default(), window, cx, |s| s.try_cancel())
        {
            cx.notify();
            return;
        }

        cx.propagate();
    }

    pub fn dismiss_menus_and_popups(
        &mut self,
        is_user_requested: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> bool {
        let mut dismissed = false;

        dismissed |= self.take_rename(false, window, cx).is_some();
        dismissed |= self.hide_blame_popover(true, cx);
        dismissed |= hide_hover(self, cx);
        dismissed |= self.hide_signature_help(cx, SignatureHelpHiddenBy::Escape);
        dismissed |= self.hide_context_menu(window, cx).is_some();
        dismissed |= self.mouse_context_menu.take().is_some();
        dismissed |= is_user_requested
            && self.discard_edit_prediction(EditPredictionDiscardReason::Rejected, cx);
        dismissed |= self.snippet_stack.pop().is_some();
        if self.diff_review_drag_state.is_some() {
            self.cancel_diff_review_drag(cx);
            dismissed = true;
        }
        if !self.diff_review_overlays.is_empty() {
            self.dismiss_all_diff_review_overlays(cx);
            dismissed = true;
        }

        if self.mode.is_full() && matches!(self.active_diagnostics, ActiveDiagnostic::Group(_)) {
            self.dismiss_diagnostics(cx);
            dismissed = true;
        }

        dismissed
    }

    fn linked_editing_ranges_for(
        &self,
        query_range: Range<text::Anchor>,
        cx: &App,
    ) -> Option<HashMap<Entity<Buffer>, Vec<Range<text::Anchor>>>> {
        use text::ToOffset as TO;

        if self.linked_edit_ranges.is_empty() {
            return None;
        }
        if query_range.start.buffer_id != query_range.end.buffer_id {
            return None;
        };
        let multibuffer_snapshot = self.buffer.read(cx).snapshot(cx);
        let buffer = self.buffer.read(cx).buffer(query_range.end.buffer_id)?;
        let buffer_snapshot = buffer.read(cx).snapshot();
        let (base_range, linked_ranges) = self.linked_edit_ranges.get(
            buffer_snapshot.remote_id(),
            query_range.clone(),
            &buffer_snapshot,
        )?;
        // find offset from the start of current range to current cursor position
        let start_byte_offset = TO::to_offset(&base_range.start, &buffer_snapshot);

        let start_offset = TO::to_offset(&query_range.start, &buffer_snapshot);
        let start_difference = start_offset - start_byte_offset;
        let end_offset = TO::to_offset(&query_range.end, &buffer_snapshot);
        let end_difference = end_offset - start_byte_offset;

        // Current range has associated linked ranges.
        let mut linked_edits = HashMap::<_, Vec<_>>::default();
        for range in linked_ranges.iter() {
            let start_offset = TO::to_offset(&range.start, &buffer_snapshot);
            let end_offset = start_offset + end_difference;
            let start_offset = start_offset + start_difference;
            if start_offset > buffer_snapshot.len() || end_offset > buffer_snapshot.len() {
                continue;
            }
            if self.selections.disjoint_anchor_ranges().any(|s| {
                let Some((selection_start, _)) =
                    multibuffer_snapshot.anchor_to_buffer_anchor(s.start)
                else {
                    return false;
                };
                let Some((selection_end, _)) = multibuffer_snapshot.anchor_to_buffer_anchor(s.end)
                else {
                    return false;
                };
                if selection_start.buffer_id != query_range.start.buffer_id
                    || selection_end.buffer_id != query_range.end.buffer_id
                {
                    return false;
                }
                TO::to_offset(&selection_start, &buffer_snapshot) <= end_offset
                    && TO::to_offset(&selection_end, &buffer_snapshot) >= start_offset
            }) {
                continue;
            }
            let start = buffer_snapshot.anchor_after(start_offset);
            let end = buffer_snapshot.anchor_after(end_offset);
            linked_edits
                .entry(buffer.clone())
                .or_default()
                .push(start..end);
        }
        Some(linked_edits)
    }

    pub fn handle_input(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) {
        let text: Arc<str> = text.into();

        if self.read_only(cx) {
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        self.unfold_buffers_with_selections(cx);

        let selections = self.selections.all_adjusted(&self.display_snapshot(cx));
        let mut bracket_inserted = false;
        let mut edits = Vec::new();
        let mut linked_edits = LinkedEdits::new();
        let mut new_selections = Vec::with_capacity(selections.len());
        let mut new_autoclose_regions = Vec::new();
        let snapshot = self.buffer.read(cx).read(cx);
        let mut clear_linked_edit_ranges = false;
        let mut all_selections_read_only = true;
        let mut has_adjacent_edits = false;
        let mut in_adjacent_group = false;

        let mut regions = self
            .selections_with_autoclose_regions(selections, &snapshot)
            .peekable();

        while let Some((selection, autoclose_region)) = regions.next() {
            if snapshot
                .point_to_buffer_point(selection.head())
                .is_none_or(|(snapshot, ..)| !snapshot.capability.editable())
            {
                continue;
            }
            if snapshot
                .point_to_buffer_point(selection.tail())
                .is_none_or(|(snapshot, ..)| !snapshot.capability.editable())
            {
                // note, ideally we'd clip the tail to the closest writeable region towards the head
                continue;
            }
            all_selections_read_only = false;

            if let Some(scope) = snapshot.language_scope_at(selection.head()) {
                // Determine if the inserted text matches the opening or closing
                // bracket of any of this language's bracket pairs.
                let mut bracket_pair = None;
                let mut is_bracket_pair_start = false;
                let mut is_bracket_pair_end = false;
                if !text.is_empty() {
                    let mut bracket_pair_matching_end = None;
                    // `text` can be empty when a user is using IME (e.g. Chinese Wubi Simplified)
                    //  and they are removing the character that triggered IME popup.
                    for (pair, enabled) in scope.brackets() {
                        if !pair.close && !pair.surround {
                            continue;
                        }

                        if enabled && pair.start.ends_with(text.as_ref()) {
                            let prefix_len = pair.start.len() - text.len();
                            let preceding_text_matches_prefix = prefix_len == 0
                                || (selection.start.column >= (prefix_len as u32)
                                    && snapshot.contains_str_at(
                                        Point::new(
                                            selection.start.row,
                                            selection.start.column - (prefix_len as u32),
                                        ),
                                        &pair.start[..prefix_len],
                                    ));
                            if preceding_text_matches_prefix {
                                bracket_pair = Some(pair.clone());
                                is_bracket_pair_start = true;
                                break;
                            }
                        }
                        if pair.end.as_str() == text.as_ref() && bracket_pair_matching_end.is_none()
                        {
                            // take first bracket pair matching end, but don't break in case a later bracket
                            // pair matches start
                            bracket_pair_matching_end = Some(pair.clone());
                        }
                    }
                    if let Some(end) = bracket_pair_matching_end
                        && bracket_pair.is_none()
                    {
                        bracket_pair = Some(end);
                        is_bracket_pair_end = true;
                    }
                }

                if let Some(bracket_pair) = bracket_pair {
                    let snapshot_settings = snapshot.language_settings_at(selection.start, cx);
                    let autoclose = self.use_autoclose && snapshot_settings.use_autoclose;
                    let auto_surround =
                        self.use_auto_surround && snapshot_settings.use_auto_surround;
                    if selection.is_empty() {
                        if is_bracket_pair_start {
                            // If the inserted text is a suffix of an opening bracket and the
                            // selection is preceded by the rest of the opening bracket, then
                            // insert the closing bracket.
                            let following_text_allows_autoclose = snapshot
                                .chars_at(selection.start)
                                .next()
                                .is_none_or(|c| scope.should_autoclose_before(c));

                            let preceding_text_allows_autoclose = selection.start.column == 0
                                || snapshot
                                    .reversed_chars_at(selection.start)
                                    .next()
                                    .is_none_or(|c| {
                                        bracket_pair.start != bracket_pair.end
                                            || !snapshot
                                                .char_classifier_at(selection.start)
                                                .is_word(c)
                                    });

                            let is_closing_quote = if bracket_pair.end == bracket_pair.start
                                && bracket_pair.start.len() == 1
                            {
                                let target = bracket_pair.start.chars().next().unwrap();
                                let mut byte_offset = 0u32;
                                let current_line_count = snapshot
                                    .reversed_chars_at(selection.start)
                                    .take_while(|&c| c != '\n')
                                    .filter(|c| {
                                        byte_offset += c.len_utf8() as u32;
                                        if *c != target {
                                            return false;
                                        }

                                        let point = Point::new(
                                            selection.start.row,
                                            selection.start.column.saturating_sub(byte_offset),
                                        );

                                        let is_enabled = snapshot
                                            .language_scope_at(point)
                                            .and_then(|scope| {
                                                scope
                                                    .brackets()
                                                    .find(|(pair, _)| {
                                                        pair.start == bracket_pair.start
                                                    })
                                                    .map(|(_, enabled)| enabled)
                                            })
                                            .unwrap_or(true);

                                        let is_delimiter = snapshot
                                            .language_scope_at(Point::new(
                                                point.row,
                                                point.column + 1,
                                            ))
                                            .and_then(|scope| {
                                                scope
                                                    .brackets()
                                                    .find(|(pair, _)| {
                                                        pair.start == bracket_pair.start
                                                    })
                                                    .map(|(_, enabled)| !enabled)
                                            })
                                            .unwrap_or(false);

                                        is_enabled && !is_delimiter
                                    })
                                    .count();
                                current_line_count % 2 == 1
                            } else {
                                false
                            };

                            if autoclose
                                && bracket_pair.close
                                && following_text_allows_autoclose
                                && preceding_text_allows_autoclose
                                && !is_closing_quote
                            {
                                let anchor = snapshot.anchor_before(selection.end);
                                new_selections.push((selection.map(|_| anchor), text.len()));
                                new_autoclose_regions.push((
                                    anchor,
                                    text.len(),
                                    selection.id,
                                    bracket_pair.clone(),
                                ));
                                edits.push((
                                    selection.range(),
                                    format!("{}{}", text, bracket_pair.end).into(),
                                ));
                                bracket_inserted = true;
                                continue;
                            }
                        }

                        if let Some(region) = autoclose_region {
                            // If the selection is followed by an auto-inserted closing bracket,
                            // then don't insert that closing bracket again; just move the selection
                            // past the closing bracket.
                            let should_skip = selection.end == region.range.end.to_point(&snapshot)
                                && text.as_ref() == region.pair.end.as_str()
                                && snapshot.contains_str_at(region.range.end, text.as_ref());
                            if should_skip {
                                let anchor = snapshot.anchor_after(selection.end);
                                new_selections
                                    .push((selection.map(|_| anchor), region.pair.end.len()));
                                continue;
                            }
                        }

                        let always_treat_brackets_as_autoclosed = snapshot
                            .language_settings_at(selection.start, cx)
                            .always_treat_brackets_as_autoclosed;
                        if always_treat_brackets_as_autoclosed
                            && is_bracket_pair_end
                            && snapshot.contains_str_at(selection.end, text.as_ref())
                        {
                            // Otherwise, when `always_treat_brackets_as_autoclosed` is set to `true
                            // and the inserted text is a closing bracket and the selection is followed
                            // by the closing bracket then move the selection past the closing bracket.
                            let anchor = snapshot.anchor_after(selection.end);
                            new_selections.push((selection.map(|_| anchor), text.len()));
                            continue;
                        }
                    }
                    // If an opening bracket is 1 character long and is typed while
                    // text is selected, then surround that text with the bracket pair.
                    else if auto_surround
                        && bracket_pair.surround
                        && is_bracket_pair_start
                        && bracket_pair.start.chars().count() == 1
                    {
                        edits.push((selection.start..selection.start, text.clone()));
                        edits.push((
                            selection.end..selection.end,
                            bracket_pair.end.as_str().into(),
                        ));
                        bracket_inserted = true;
                        new_selections.push((
                            Selection {
                                id: selection.id,
                                start: snapshot.anchor_after(selection.start),
                                end: snapshot.anchor_before(selection.end),
                                reversed: selection.reversed,
                                goal: selection.goal,
                            },
                            0,
                        ));
                        continue;
                    }
                }
            }

            if self.auto_replace_emoji_shortcode
                && selection.is_empty()
                && text.as_ref().ends_with(':')
                && let Some(possible_emoji_short_code) =
                    Self::find_possible_emoji_shortcode_at_position(&snapshot, selection.start)
                && !possible_emoji_short_code.is_empty()
                && let Some(emoji) = emojis::get_by_shortcode(&possible_emoji_short_code)
            {
                let emoji_shortcode_start = Point::new(
                    selection.start.row,
                    selection.start.column - possible_emoji_short_code.len() as u32 - 1,
                );

                // Remove shortcode from buffer
                edits.push((
                    emoji_shortcode_start..selection.start,
                    "".to_string().into(),
                ));
                new_selections.push((
                    Selection {
                        id: selection.id,
                        start: snapshot.anchor_after(emoji_shortcode_start),
                        end: snapshot.anchor_before(selection.start),
                        reversed: selection.reversed,
                        goal: selection.goal,
                    },
                    0,
                ));

                // Insert emoji
                let selection_start_anchor = snapshot.anchor_after(selection.start);
                new_selections.push((selection.map(|_| selection_start_anchor), 0));
                edits.push((selection.start..selection.end, emoji.to_string().into()));

                continue;
            }

            let next_is_adjacent = regions
                .peek()
                .is_some_and(|(next, _)| selection.end == next.start);

            // If not handling any auto-close operation, then just replace the selected
            // text with the given input and move the selection to the end of the
            // newly inserted text.
            let anchor = if in_adjacent_group || next_is_adjacent {
                // After edits the right bias would shift those anchor to the next visible fragment
                // but we want to resolve to the previous one
                snapshot.anchor_before(selection.end)
            } else {
                snapshot.anchor_after(selection.end)
            };

            if !self.linked_edit_ranges.is_empty() {
                let start_anchor = snapshot.anchor_before(selection.start);
                let classifier = snapshot
                    .char_classifier_at(start_anchor)
                    .scope_context(Some(CharScopeContext::LinkedEdit));

                if let Some((_, anchor_range)) =
                    snapshot.anchor_range_to_buffer_anchor_range(start_anchor..anchor)
                {
                    let is_word_char = text
                        .chars()
                        .next()
                        .is_none_or(|char| classifier.is_word(char));

                    let is_dot = text.as_ref() == ".";
                    let should_apply_linked_edit = is_word_char || is_dot;

                    if should_apply_linked_edit {
                        linked_edits.push(&self, anchor_range, text.clone(), cx);
                    } else {
                        clear_linked_edit_ranges = true;
                    }
                }
            }

            new_selections.push((selection.map(|_| anchor), 0));
            edits.push((selection.start..selection.end, text.clone()));

            has_adjacent_edits |= next_is_adjacent;
            in_adjacent_group = next_is_adjacent;
        }

        if all_selections_read_only {
            return;
        }

        drop(regions);
        drop(snapshot);

        self.transact(window, cx, |this, window, cx| {
            if clear_linked_edit_ranges {
                this.linked_edit_ranges.clear();
            }
            let initial_buffer_versions =
                jsx_tag_auto_close::construct_initial_buffer_versions_map(this, &edits, cx);

            this.buffer.update(cx, |buffer, cx| {
                if has_adjacent_edits {
                    buffer.edit_non_coalesce(edits, this.autoindent_mode.clone(), cx);
                } else {
                    buffer.edit(edits, this.autoindent_mode.clone(), cx);
                }
            });
            linked_edits.apply(cx);
            let new_anchor_selections = new_selections.iter().map(|e| &e.0);
            let new_selection_deltas = new_selections.iter().map(|e| e.1);
            let map = this.display_map.update(cx, |map, cx| map.snapshot(cx));
            let new_selections = resolve_selections_wrapping_blocks::<MultiBufferOffset, _>(
                new_anchor_selections,
                &map,
            )
            .zip(new_selection_deltas)
            .map(|(selection, delta)| Selection {
                id: selection.id,
                start: selection.start + delta,
                end: selection.end + delta,
                reversed: selection.reversed,
                goal: SelectionGoal::None,
            })
            .collect::<Vec<_>>();

            let mut i = 0;
            for (position, delta, selection_id, pair) in new_autoclose_regions {
                let position = position.to_offset(map.buffer_snapshot()) + delta;
                let start = map.buffer_snapshot().anchor_before(position);
                let end = map.buffer_snapshot().anchor_after(position);
                while let Some(existing_state) = this.autoclose_regions.get(i) {
                    match existing_state
                        .range
                        .start
                        .cmp(&start, map.buffer_snapshot())
                    {
                        Ordering::Less => i += 1,
                        Ordering::Greater => break,
                        Ordering::Equal => {
                            match end.cmp(&existing_state.range.end, map.buffer_snapshot()) {
                                Ordering::Less => i += 1,
                                Ordering::Equal => break,
                                Ordering::Greater => break,
                            }
                        }
                    }
                }
                this.autoclose_regions.insert(
                    i,
                    AutocloseRegion {
                        selection_id,
                        range: start..end,
                        pair,
                    },
                );
            }

            let had_active_edit_prediction = this.has_active_edit_prediction();
            this.change_selections(
                SelectionEffects::scroll(Autoscroll::fit()).completions(false),
                window,
                cx,
                |s| s.select(new_selections),
            );

            if !bracket_inserted
                && let Some(on_type_format_task) =
                    this.trigger_on_type_formatting(text.to_string(), window, cx)
            {
                on_type_format_task.detach_and_log_err(cx);
            }

            let editor_settings = EditorSettings::get_global(cx);
            if bracket_inserted
                && (editor_settings.auto_signature_help
                    || editor_settings.show_signature_help_after_edits)
            {
                this.show_signature_help(&ShowSignatureHelp, window, cx);
            }

            let trigger_in_words =
                this.show_edit_predictions_in_menu() || !had_active_edit_prediction;
            if this.hard_wrap.is_some() {
                let latest: Range<Point> = this.selections.newest(&map).range();
                if latest.is_empty()
                    && this
                        .buffer()
                        .read(cx)
                        .snapshot(cx)
                        .line_len(MultiBufferRow(latest.start.row))
                        == latest.start.column
                {
                    this.rewrap_impl(
                        RewrapOptions {
                            override_language_settings: true,
                            preserve_existing_whitespace: true,
                            line_length: None,
                        },
                        cx,
                    )
                }
            }
            this.trigger_completion_on_input(&text, trigger_in_words, window, cx);
            refresh_linked_ranges(this, window, cx);
            this.refresh_edit_prediction(true, false, window, cx);
            jsx_tag_auto_close::handle_from(this, initial_buffer_versions, window, cx);
        });
    }

    fn find_possible_emoji_shortcode_at_position(
        snapshot: &MultiBufferSnapshot,
        position: Point,
    ) -> Option<String> {
        let mut chars = Vec::new();
        let mut found_colon = false;
        for char in snapshot.reversed_chars_at(position).take(100) {
            // Found a possible emoji shortcode in the middle of the buffer
            if found_colon {
                if char.is_whitespace() {
                    chars.reverse();
                    return Some(chars.iter().collect());
                }
                // If the previous character is not a whitespace, we are in the middle of a word
                // and we only want to complete the shortcode if the word is made up of other emojis
                let mut containing_word = String::new();
                for ch in snapshot
                    .reversed_chars_at(position)
                    .skip(chars.len() + 1)
                    .take(100)
                {
                    if ch.is_whitespace() {
                        break;
                    }
                    containing_word.push(ch);
                }
                let containing_word = containing_word.chars().rev().collect::<String>();
                if util::word_consists_of_emojis(containing_word.as_str()) {
                    chars.reverse();
                    return Some(chars.iter().collect());
                }
            }

            if char.is_whitespace() || !char.is_ascii() {
                return None;
            }
            if char == ':' {
                found_colon = true;
            } else {
                chars.push(char);
            }
        }
        // Found a possible emoji shortcode at the beginning of the buffer
        chars.reverse();
        Some(chars.iter().collect())
    }

    pub fn newline(&mut self, _: &Newline, window: &mut Window, cx: &mut Context<Self>) {
        if self.read_only(cx) {
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            let (edits_with_flags, selection_info): (Vec<_>, Vec<_>) = {
                let selections = this
                    .selections
                    .all::<MultiBufferOffset>(&this.display_snapshot(cx));
                let multi_buffer = this.buffer.read(cx);
                let buffer = multi_buffer.snapshot(cx);
                selections
                    .iter()
                    .map(|selection| {
                        let start_point = selection.start.to_point(&buffer);
                        let mut existing_indent =
                            buffer.indent_size_for_line(MultiBufferRow(start_point.row));
                        existing_indent.len = cmp::min(existing_indent.len, start_point.column);
                        let start = selection.start;
                        let end = selection.end;
                        let selection_is_empty = start == end;
                        let language_scope = buffer.language_scope_at(start);
                        let (delimiter, newline_config) = if let Some(language) = &language_scope {
                            let needs_extra_newline = NewlineConfig::insert_extra_newline_brackets(
                                &buffer,
                                start..end,
                                language,
                            )
                                || NewlineConfig::insert_extra_newline_tree_sitter(
                                    &buffer,
                                    start..end,
                                );

                            let mut newline_config = NewlineConfig::Newline {
                                additional_indent: IndentSize::spaces(0),
                                extra_line_additional_indent: if needs_extra_newline {
                                    Some(IndentSize::spaces(0))
                                } else {
                                    None
                                },
                                prevent_auto_indent: false,
                            };

                            let comment_delimiter = maybe!({
                                if !selection_is_empty {
                                    return None;
                                }

                                if !multi_buffer.language_settings(cx).extend_comment_on_newline {
                                    return None;
                                }

                                return comment_delimiter_for_newline(
                                    &start_point,
                                    &buffer,
                                    language,
                                );
                            });

                            let doc_delimiter = maybe!({
                                if !selection_is_empty {
                                    return None;
                                }

                                if !multi_buffer.language_settings(cx).extend_comment_on_newline {
                                    return None;
                                }

                                return documentation_delimiter_for_newline(
                                    &start_point,
                                    &buffer,
                                    language,
                                    &mut newline_config,
                                );
                            });

                            let list_delimiter = maybe!({
                                if !selection_is_empty {
                                    return None;
                                }

                                if !multi_buffer.language_settings(cx).extend_list_on_newline {
                                    return None;
                                }

                                return list_delimiter_for_newline(
                                    &start_point,
                                    &buffer,
                                    language,
                                    &mut newline_config,
                                );
                            });

                            (
                                comment_delimiter.or(doc_delimiter).or(list_delimiter),
                                newline_config,
                            )
                        } else {
                            (
                                None,
                                NewlineConfig::Newline {
                                    additional_indent: IndentSize::spaces(0),
                                    extra_line_additional_indent: None,
                                    prevent_auto_indent: false,
                                },
                            )
                        };

                        let (edit_start, new_text, prevent_auto_indent) = match &newline_config {
                            NewlineConfig::ClearCurrentLine => {
                                let row_start =
                                    buffer.point_to_offset(Point::new(start_point.row, 0));
                                (row_start, String::new(), false)
                            }
                            NewlineConfig::UnindentCurrentLine { continuation } => {
                                let row_start =
                                    buffer.point_to_offset(Point::new(start_point.row, 0));
                                let tab_size = buffer.language_settings_at(start, cx).tab_size;
                                let tab_size_indent = IndentSize::spaces(tab_size.get());
                                let reduced_indent =
                                    existing_indent.with_delta(Ordering::Less, tab_size_indent);
                                let mut new_text = String::new();
                                new_text.extend(reduced_indent.chars());
                                new_text.push_str(continuation);
                                (row_start, new_text, true)
                            }
                            NewlineConfig::Newline {
                                additional_indent,
                                extra_line_additional_indent,
                                prevent_auto_indent,
                            } => {
                                let auto_indent_mode =
                                    buffer.language_settings_at(start, cx).auto_indent;
                                let preserve_indent =
                                    auto_indent_mode != language::AutoIndentMode::None;
                                let apply_syntax_indent =
                                    auto_indent_mode == language::AutoIndentMode::SyntaxAware;
                                let capacity_for_delimiter =
                                    delimiter.as_deref().map(str::len).unwrap_or_default();
                                let existing_indent_len = if preserve_indent {
                                    existing_indent.len as usize
                                } else {
                                    0
                                };
                                let extra_line_len = extra_line_additional_indent
                                    .map(|i| 1 + existing_indent_len + i.len as usize)
                                    .unwrap_or(0);
                                let mut new_text = String::with_capacity(
                                    1 + capacity_for_delimiter
                                        + existing_indent_len
                                        + additional_indent.len as usize
                                        + extra_line_len,
                                );
                                new_text.push('\n');
                                if preserve_indent {
                                    new_text.extend(existing_indent.chars());
                                }
                                new_text.extend(additional_indent.chars());
                                if let Some(delimiter) = &delimiter {
                                    new_text.push_str(delimiter);
                                }
                                if let Some(extra_indent) = extra_line_additional_indent {
                                    new_text.push('\n');
                                    if preserve_indent {
                                        new_text.extend(existing_indent.chars());
                                    }
                                    new_text.extend(extra_indent.chars());
                                }
                                (
                                    start,
                                    new_text,
                                    *prevent_auto_indent || !apply_syntax_indent,
                                )
                            }
                        };

                        let anchor = buffer.anchor_after(end);
                        let new_selection = selection.map(|_| anchor);
                        (
                            ((edit_start..end, new_text), prevent_auto_indent),
                            (newline_config.has_extra_line(), new_selection),
                        )
                    })
                    .unzip()
            };

            let mut auto_indent_edits = Vec::new();
            let mut edits = Vec::new();
            for (edit, prevent_auto_indent) in edits_with_flags {
                if prevent_auto_indent {
                    edits.push(edit);
                } else {
                    auto_indent_edits.push(edit);
                }
            }
            if !edits.is_empty() {
                this.edit(edits, cx);
            }
            if !auto_indent_edits.is_empty() {
                this.edit_with_autoindent(auto_indent_edits, cx);
            }

            let buffer = this.buffer.read(cx).snapshot(cx);
            let new_selections = selection_info
                .into_iter()
                .map(|(extra_newline_inserted, new_selection)| {
                    let mut cursor = new_selection.end.to_point(&buffer);
                    if extra_newline_inserted {
                        cursor.row -= 1;
                        cursor.column = buffer.line_len(MultiBufferRow(cursor.row));
                    }
                    new_selection.map(|_| cursor)
                })
                .collect();

            this.change_selections(Default::default(), window, cx, |s| s.select(new_selections));
            this.refresh_edit_prediction(true, false, window, cx);
            if let Some(task) = this.trigger_on_type_formatting("\n".to_owned(), window, cx) {
                task.detach_and_log_err(cx);
            }
        });
    }

    pub fn newline_above(&mut self, _: &NewlineAbove, window: &mut Window, cx: &mut Context<Self>) {
        if self.read_only(cx) {
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        let buffer = self.buffer.read(cx);
        let snapshot = buffer.snapshot(cx);

        let mut edits = Vec::new();
        let mut rows = Vec::new();

        for (rows_inserted, selection) in self
            .selections
            .all_adjusted(&self.display_snapshot(cx))
            .into_iter()
            .enumerate()
        {
            let cursor = selection.head();
            let row = cursor.row;

            let start_of_line = snapshot.clip_point(Point::new(row, 0), Bias::Left);

            let newline = "\n".to_string();
            edits.push((start_of_line..start_of_line, newline));

            rows.push(row + rows_inserted as u32);
        }

        self.transact(window, cx, |editor, window, cx| {
            editor.edit(edits, cx);

            editor.change_selections(Default::default(), window, cx, |s| {
                let mut index = 0;
                s.move_cursors_with(&mut |map, _, _| {
                    let row = rows[index];
                    index += 1;

                    let point = Point::new(row, 0);
                    let boundary = map.next_line_boundary(point).1;
                    let clipped = map.clip_point(boundary, Bias::Left);

                    (clipped, SelectionGoal::None)
                });
            });

            let mut indent_edits = Vec::new();
            let multibuffer_snapshot = editor.buffer.read(cx).snapshot(cx);
            for row in rows {
                let indents = multibuffer_snapshot.suggested_indents(row..row + 1, cx);
                for (row, indent) in indents {
                    if indent.len == 0 {
                        continue;
                    }

                    let text = match indent.kind {
                        IndentKind::Space => " ".repeat(indent.len as usize),
                        IndentKind::Tab => "\t".repeat(indent.len as usize),
                    };
                    let point = Point::new(row.0, 0);
                    indent_edits.push((point..point, text));
                }
            }
            editor.edit(indent_edits, cx);
            if let Some(format) = editor.trigger_on_type_formatting("\n".to_owned(), window, cx) {
                format.detach_and_log_err(cx);
            }
        });
    }

    pub fn newline_below(&mut self, _: &NewlineBelow, window: &mut Window, cx: &mut Context<Self>) {
        if self.read_only(cx) {
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        let mut buffer_edits: HashMap<EntityId, (Entity<Buffer>, Vec<Point>)> = HashMap::default();
        let mut rows = Vec::new();
        let mut rows_inserted = 0;

        for selection in self.selections.all_adjusted(&self.display_snapshot(cx)) {
            let cursor = selection.head();
            let row = cursor.row;

            let point = Point::new(row, 0);
            let Some((buffer_handle, buffer_point)) =
                self.buffer.read(cx).point_to_buffer_point(point, cx)
            else {
                continue;
            };

            buffer_edits
                .entry(buffer_handle.entity_id())
                .or_insert_with(|| (buffer_handle, Vec::new()))
                .1
                .push(buffer_point);

            rows_inserted += 1;
            rows.push(row + rows_inserted);
        }

        self.transact(window, cx, |editor, window, cx| {
            for (_, (buffer_handle, points)) in &buffer_edits {
                buffer_handle.update(cx, |buffer, cx| {
                    let edits: Vec<_> = points
                        .iter()
                        .map(|point| {
                            let target = Point::new(point.row + 1, 0);
                            let start_of_line = buffer.point_to_offset(target).min(buffer.len());
                            (start_of_line..start_of_line, "\n")
                        })
                        .collect();
                    buffer.edit(edits, None, cx);
                });
            }

            editor.change_selections(Default::default(), window, cx, |s| {
                let mut index = 0;
                s.move_cursors_with(&mut |map, _, _| {
                    let row = rows[index];
                    index += 1;

                    let point = Point::new(row, 0);
                    let boundary = map.next_line_boundary(point).1;
                    let clipped = map.clip_point(boundary, Bias::Left);

                    (clipped, SelectionGoal::None)
                });
            });

            let mut indent_edits = Vec::new();
            let multibuffer_snapshot = editor.buffer.read(cx).snapshot(cx);
            for row in rows {
                let indents = multibuffer_snapshot.suggested_indents(row..row + 1, cx);
                for (row, indent) in indents {
                    if indent.len == 0 {
                        continue;
                    }

                    let text = match indent.kind {
                        IndentKind::Space => " ".repeat(indent.len as usize),
                        IndentKind::Tab => "\t".repeat(indent.len as usize),
                    };
                    let point = Point::new(row.0, 0);
                    indent_edits.push((point..point, text));
                }
            }
            editor.edit(indent_edits, cx);
            if let Some(format) = editor.trigger_on_type_formatting("\n".to_owned(), window, cx) {
                format.detach_and_log_err(cx);
            }
        });
    }

    pub fn insert(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) {
        let autoindent = text.is_empty().not().then(|| AutoindentMode::Block {
            original_indent_columns: Vec::new(),
        });
        self.replace_selections(text, autoindent, window, cx, false);
    }

    /// Replaces the editor's selections with the provided `text`, applying the
    /// given `autoindent_mode` (`None` will skip autoindentation).
    ///
    /// Early returns if the editor is in read-only mode, without applying any
    /// edits.
    fn replace_selections(
        &mut self,
        text: &str,
        autoindent_mode: Option<AutoindentMode>,
        window: &mut Window,
        cx: &mut Context<Self>,
        apply_linked_edits: bool,
    ) {
        if self.read_only(cx) {
            return;
        }

        let text: Arc<str> = text.into();
        self.transact(window, cx, |this, window, cx| {
            let old_selections = this.selections.all_adjusted(&this.display_snapshot(cx));
            let linked_edits = if apply_linked_edits {
                this.linked_edits_for_selections(text.clone(), cx)
            } else {
                LinkedEdits::new()
            };

            let selection_anchors = this.buffer.update(cx, |buffer, cx| {
                let anchors = {
                    let snapshot = buffer.read(cx);
                    old_selections
                        .iter()
                        .map(|s| {
                            let anchor = snapshot.anchor_after(s.head());
                            s.map(|_| anchor)
                        })
                        .collect::<Vec<_>>()
                };
                buffer.edit(
                    old_selections
                        .iter()
                        .map(|s| (s.start..s.end, text.clone())),
                    autoindent_mode,
                    cx,
                );
                anchors
            });

            linked_edits.apply(cx);

            this.change_selections(Default::default(), window, cx, |s| {
                s.select_anchors(selection_anchors);
            });

            if apply_linked_edits {
                refresh_linked_ranges(this, window, cx);
            }

            cx.notify();
        });
    }

    /// Collects linked edits for the current selections, pairing each linked
    /// range with `text`.
    pub fn linked_edits_for_selections(&self, text: Arc<str>, cx: &App) -> LinkedEdits {
        let multibuffer_snapshot = self.buffer().read(cx).snapshot(cx);
        let mut linked_edits = LinkedEdits::new();
        if !self.linked_edit_ranges.is_empty() {
            for selection in self.selections.disjoint_anchors() {
                let Some((_, range)) =
                    multibuffer_snapshot.anchor_range_to_buffer_anchor_range(selection.range())
                else {
                    continue;
                };
                linked_edits.push(self, range, text.clone(), cx);
            }
        }
        linked_edits
    }

    /// Deletes the content covered by the current selections and applies
    /// linked edits.
    pub fn delete_selections_with_linked_edits(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.replace_selections("", None, window, cx, true);
    }

    #[cfg(any(test, feature = "test-support"))]
    pub fn set_linked_edit_ranges_for_testing(
        &mut self,
        ranges: Vec<(Range<Point>, Vec<Range<Point>>)>,
        cx: &mut Context<Self>,
    ) -> Option<()> {
        let Some((buffer, _)) = self
            .buffer
            .read(cx)
            .text_anchor_for_position(self.selections.newest_anchor().start, cx)
        else {
            return None;
        };
        let buffer = buffer.read(cx);
        let buffer_id = buffer.remote_id();
        let mut linked_ranges = Vec::with_capacity(ranges.len());
        for (base_range, linked_ranges_points) in ranges {
            let base_anchor =
                buffer.anchor_before(base_range.start)..buffer.anchor_after(base_range.end);
            let linked_anchors = linked_ranges_points
                .into_iter()
                .map(|range| buffer.anchor_before(range.start)..buffer.anchor_after(range.end))
                .collect();
            linked_ranges.push((base_anchor, linked_anchors));
        }
        let mut map = HashMap::default();
        map.insert(buffer_id, linked_ranges);
        self.linked_edit_ranges = linked_editing_ranges::LinkedEditingRanges(map);
        Some(())
    }

    fn trigger_completion_on_input(
        &mut self,
        text: &str,
        trigger_in_words: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let completions_source = self
            .context_menu
            .borrow()
            .as_ref()
            .and_then(|menu| match menu {
                CodeContextMenu::Completions(completions_menu) => Some(completions_menu.source),
                CodeContextMenu::CodeActions(_) => None,
            });

        match completions_source {
            Some(CompletionsMenuSource::Words { .. }) => {
                self.open_or_update_completions_menu(
                    Some(CompletionsMenuSource::Words {
                        ignore_threshold: false,
                    }),
                    None,
                    trigger_in_words,
                    window,
                    cx,
                );
            }
            _ => self.open_or_update_completions_menu(
                None,
                Some(text.to_owned()).filter(|x| !x.is_empty()),
                true,
                window,
                cx,
            ),
        }
    }

    /// If any empty selections is touching the start of its innermost containing autoclose
    /// region, expand it to select the brackets.
    fn select_autoclose_pair(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let selections = self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx));
        let buffer = self.buffer.read(cx).read(cx);
        let new_selections = self
            .selections_with_autoclose_regions(selections, &buffer)
            .map(|(mut selection, region)| {
                if !selection.is_empty() {
                    return selection;
                }

                if let Some(region) = region {
                    let mut range = region.range.to_offset(&buffer);
                    if selection.start == range.start && range.start.0 >= region.pair.start.len() {
                        range.start -= region.pair.start.len();
                        if buffer.contains_str_at(range.start, &region.pair.start)
                            && buffer.contains_str_at(range.end, &region.pair.end)
                        {
                            range.end += region.pair.end.len();
                            selection.start = range.start;
                            selection.end = range.end;

                            return selection;
                        }
                    }
                }

                let always_treat_brackets_as_autoclosed = buffer
                    .language_settings_at(selection.start, cx)
                    .always_treat_brackets_as_autoclosed;

                if !always_treat_brackets_as_autoclosed {
                    return selection;
                }

                if let Some(scope) = buffer.language_scope_at(selection.start) {
                    for (pair, enabled) in scope.brackets() {
                        if !enabled || !pair.close {
                            continue;
                        }

                        if buffer.contains_str_at(selection.start, &pair.end) {
                            let pair_start_len = pair.start.len();
                            if buffer.contains_str_at(
                                selection.start.saturating_sub_usize(pair_start_len),
                                &pair.start,
                            ) {
                                selection.start -= pair_start_len;
                                selection.end += pair.end.len();

                                return selection;
                            }
                        }
                    }
                }

                selection
            })
            .collect();

        drop(buffer);
        self.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
            selections.select(new_selections)
        });
    }

    /// Iterate the given selections, and for each one, find the smallest surrounding
    /// autoclose region. This uses the ordering of the selections and the autoclose
    /// regions to avoid repeated comparisons.
    fn selections_with_autoclose_regions<'a, D: ToOffset + Clone>(
        &'a self,
        selections: impl IntoIterator<Item = Selection<D>>,
        buffer: &'a MultiBufferSnapshot,
    ) -> impl Iterator<Item = (Selection<D>, Option<&'a AutocloseRegion>)> {
        let mut i = 0;
        let mut regions = self.autoclose_regions.as_slice();
        selections.into_iter().map(move |selection| {
            let range = selection.start.to_offset(buffer)..selection.end.to_offset(buffer);

            let mut enclosing = None;
            while let Some(pair_state) = regions.get(i) {
                if pair_state.range.end.to_offset(buffer) < range.start {
                    regions = &regions[i + 1..];
                    i = 0;
                } else if pair_state.range.start.to_offset(buffer) > range.end {
                    break;
                } else {
                    if pair_state.selection_id == selection.id {
                        enclosing = Some(pair_state);
                    }
                    i += 1;
                }
            }

            (selection, enclosing)
        })
    }

    /// Remove any autoclose regions that no longer contain their selection or have invalid anchors in ranges.
    fn invalidate_autoclose_regions(
        &mut self,
        mut selections: &[Selection<Anchor>],
        buffer: &MultiBufferSnapshot,
    ) {
        self.autoclose_regions.retain(|state| {
            if !state.range.start.is_valid(buffer) || !state.range.end.is_valid(buffer) {
                return false;
            }

            let mut i = 0;
            while let Some(selection) = selections.get(i) {
                if selection.end.cmp(&state.range.start, buffer).is_lt() {
                    selections = &selections[1..];
                    continue;
                }
                if selection.start.cmp(&state.range.end, buffer).is_gt() {
                    break;
                }
                if selection.id == state.selection_id {
                    return true;
                } else {
                    i += 1;
                }
            }
            false
        });
    }

    fn completion_query(buffer: &MultiBufferSnapshot, position: impl ToOffset) -> Option<String> {
        let offset = position.to_offset(buffer);
        let (word_range, kind) =
            buffer.surrounding_word(offset, Some(CharScopeContext::Completion));
        if offset > word_range.start && kind == Some(CharKind::Word) {
            Some(
                buffer
                    .text_for_range(word_range.start..offset)
                    .collect::<String>(),
            )
        } else {
            None
        }
    }

    pub fn is_lsp_relevant(&self, file: Option<&Arc<dyn language::File>>, cx: &App) -> bool {
        let Some(project) = self.project() else {
            return false;
        };
        let Some(buffer_file) = project::File::from_dyn(file) else {
            return false;
        };
        let Some(entry_id) = buffer_file.project_entry_id() else {
            return false;
        };
        let project = project.read(cx);
        let Some(buffer_worktree) = project.worktree_for_id(buffer_file.worktree_id(cx), cx) else {
            return false;
        };
        let Some(worktree_entry) = buffer_worktree.read(cx).entry_for_id(entry_id) else {
            return false;
        };
        !worktree_entry.is_ignored
    }

    pub fn visible_buffers(&self, cx: &mut Context<Editor>) -> Vec<Entity<Buffer>> {
        let display_snapshot = self.display_snapshot(cx);
        let visible_range = self.multi_buffer_visible_range(&display_snapshot, cx);
        let multi_buffer = self.buffer().read(cx);
        display_snapshot
            .buffer_snapshot()
            .range_to_buffer_ranges(visible_range)
            .into_iter()
            .filter(|(_, excerpt_visible_range, _)| !excerpt_visible_range.is_empty())
            .filter_map(|(buffer_snapshot, _, _)| multi_buffer.buffer(buffer_snapshot.remote_id()))
            .collect()
    }

    pub fn visible_buffer_ranges(
        &self,
        cx: &mut Context<Editor>,
    ) -> Vec<(
        BufferSnapshot,
        Range<BufferOffset>,
        ExcerptRange<text::Anchor>,
    )> {
        let display_snapshot = self.display_snapshot(cx);
        let visible_range = self.multi_buffer_visible_range(&display_snapshot, cx);
        display_snapshot
            .buffer_snapshot()
            .range_to_buffer_ranges(visible_range)
            .into_iter()
            .filter(|(_, excerpt_visible_range, _)| !excerpt_visible_range.is_empty())
            .collect()
    }

    pub fn text_layout_details(&self, window: &mut Window, cx: &mut App) -> TextLayoutDetails {
        TextLayoutDetails {
            text_system: window.text_system().clone(),
            editor_style: self.style.clone().unwrap(),
            rem_size: window.rem_size(),
            scroll_anchor: self.scroll_manager.shared_scroll_anchor(cx),
            visible_rows: self.visible_line_count(),
            vertical_scroll_margin: self.scroll_manager.vertical_scroll_margin,
        }
    }

    fn trigger_on_type_formatting(
        &self,
        input: String,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        if input.chars().count() != 1 {
            return None;
        }

        let project = self.project()?;
        let position = self.selections.newest_anchor().head();
        let (buffer, buffer_position) = self
            .buffer
            .read(cx)
            .text_anchor_for_position(position, cx)?;

        let settings = LanguageSettings::for_buffer_at(&buffer.read(cx), buffer_position, cx);
        if !settings.use_on_type_format {
            return None;
        }

        // OnTypeFormatting returns a list of edits, no need to pass them between Zed instances,
        // hence we do LSP request & edit on host side only — add formats to host's history.
        let push_to_lsp_host_history = true;
        // If this is not the host, append its history with new edits.
        let push_to_client_history = project.read(cx).is_via_collab();

        let on_type_formatting = project.update(cx, |project, cx| {
            project.on_type_format(
                buffer.clone(),
                buffer_position,
                input,
                push_to_lsp_host_history,
                cx,
            )
        });
        Some(cx.spawn_in(window, async move |editor, cx| {
            if let Some(transaction) = on_type_formatting.await? {
                if push_to_client_history {
                    buffer.update(cx, |buffer, _| {
                        buffer.push_transaction(transaction, Instant::now());
                        buffer.finalize_last_transaction();
                    });
                }
                editor.update(cx, |editor, cx| {
                    editor.refresh_document_highlights(cx);
                })?;
            }
            Ok(())
        }))
    }

    pub fn show_word_completions(
        &mut self,
        _: &ShowWordCompletions,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.open_or_update_completions_menu(
            Some(CompletionsMenuSource::Words {
                ignore_threshold: true,
            }),
            None,
            false,
            window,
            cx,
        );
    }

    pub fn show_completions(
        &mut self,
        _: &ShowCompletions,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.open_or_update_completions_menu(None, None, false, window, cx);
    }

    fn open_or_update_completions_menu(
        &mut self,
        requested_source: Option<CompletionsMenuSource>,
        trigger: Option<String>,
        trigger_in_words: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.pending_rename.is_some() {
            return;
        }

        let completions_source = self
            .context_menu
            .borrow()
            .as_ref()
            .and_then(|menu| match menu {
                CodeContextMenu::Completions(completions_menu) => Some(completions_menu.source),
                CodeContextMenu::CodeActions(_) => None,
            });

        let multibuffer_snapshot = self.buffer.read(cx).read(cx);

        // Typically `start` == `end`, but with snippet tabstop choices the default choice is
        // inserted and selected. To handle that case, the start of the selection is used so that
        // the menu starts with all choices.
        let position = self
            .selections
            .newest_anchor()
            .start
            .bias_right(&multibuffer_snapshot);

        if position.diff_base_anchor().is_some() {
            return;
        }
        let multibuffer_position = multibuffer_snapshot.anchor_before(position);
        let Some((buffer_position, _)) =
            multibuffer_snapshot.anchor_to_buffer_anchor(multibuffer_position)
        else {
            return;
        };
        let Some(buffer) = self.buffer.read(cx).buffer(buffer_position.buffer_id) else {
            return;
        };
        let buffer_snapshot = buffer.read(cx).snapshot();

        let menu_is_open = matches!(
            self.context_menu.borrow().as_ref(),
            Some(CodeContextMenu::Completions(_))
        );

        let language = buffer_snapshot
            .language_at(buffer_position)
            .map(|language| language.name());
        let language_settings = multibuffer_snapshot.language_settings_at(multibuffer_position, cx);
        let completion_settings = language_settings.completions.clone();

        let show_completions_on_input = self
            .show_completions_on_input_override
            .unwrap_or(language_settings.show_completions_on_input);
        if !menu_is_open && trigger.is_some() && !show_completions_on_input {
            return;
        }

        let query: Option<Arc<String>> =
            Self::completion_query(&multibuffer_snapshot, multibuffer_position)
                .map(|query| query.into());

        drop(multibuffer_snapshot);

        // Hide the current completions menu when query is empty. Without this, cached
        // completions from before the trigger char may be reused (#32774).
        if query.is_none() && menu_is_open {
            self.hide_context_menu(window, cx);
        }

        let mut ignore_word_threshold = false;
        let provider = match requested_source {
            Some(CompletionsMenuSource::Normal) | None => self.completion_provider.clone(),
            Some(CompletionsMenuSource::Words { ignore_threshold }) => {
                ignore_word_threshold = ignore_threshold;
                None
            }
            Some(CompletionsMenuSource::SnippetChoices)
            | Some(CompletionsMenuSource::SnippetsOnly) => {
                log::error!("bug: SnippetChoices requested_source is not handled");
                None
            }
        };

        let sort_completions = provider
            .as_ref()
            .is_some_and(|provider| provider.sort_completions());

        let filter_completions = provider
            .as_ref()
            .is_none_or(|provider| provider.filter_completions());

        let was_snippets_only = matches!(
            completions_source,
            Some(CompletionsMenuSource::SnippetsOnly)
        );

        if let Some(CodeContextMenu::Completions(menu)) = self.context_menu.borrow_mut().as_mut() {
            if filter_completions {
                menu.filter(
                    query.clone().unwrap_or_default(),
                    buffer_position,
                    &buffer,
                    provider.clone(),
                    window,
                    cx,
                );
            }
            // When `is_incomplete` is false, no need to re-query completions when the current query
            // is a suffix of the initial query.
            let was_complete = !menu.is_incomplete;
            if was_complete && !was_snippets_only {
                // If the new query is a suffix of the old query (typing more characters) and
                // the previous result was complete, the existing completions can be filtered.
                //
                // Note that snippet completions are always complete.
                let query_matches = match (&menu.initial_query, &query) {
                    (Some(initial_query), Some(query)) => query.starts_with(initial_query.as_ref()),
                    (None, _) => true,
                    _ => false,
                };
                if query_matches {
                    let position_matches = if menu.initial_position == position {
                        true
                    } else {
                        let snapshot = self.buffer.read(cx).read(cx);
                        menu.initial_position.to_offset(&snapshot) == position.to_offset(&snapshot)
                    };
                    if position_matches {
                        return;
                    }
                }
            }
        };

        let (word_replace_range, word_to_exclude) = if let (word_range, Some(CharKind::Word)) =
            buffer_snapshot.surrounding_word(buffer_position, None)
        {
            let word_to_exclude = buffer_snapshot
                .text_for_range(word_range.clone())
                .collect::<String>();
            (
                buffer_snapshot.anchor_before(word_range.start)
                    ..buffer_snapshot.anchor_after(buffer_position),
                Some(word_to_exclude),
            )
        } else {
            (buffer_position..buffer_position, None)
        };

        let show_completion_documentation = buffer_snapshot
            .settings_at(buffer_position, cx)
            .show_completion_documentation;

        // The document can be large, so stay in reasonable bounds when searching for words,
        // otherwise completion pop-up might be slow to appear.
        const WORD_LOOKUP_ROWS: u32 = 5_000;
        let buffer_row = text::ToPoint::to_point(&buffer_position, &buffer_snapshot).row;
        let min_word_search = buffer_snapshot.clip_point(
            Point::new(buffer_row.saturating_sub(WORD_LOOKUP_ROWS), 0),
            Bias::Left,
        );
        let max_word_search = buffer_snapshot.clip_point(
            Point::new(buffer_row + WORD_LOOKUP_ROWS, 0).min(buffer_snapshot.max_point()),
            Bias::Right,
        );
        let word_search_range = buffer_snapshot.point_to_offset(min_word_search)
            ..buffer_snapshot.point_to_offset(max_word_search);

        let skip_digits = query
            .as_ref()
            .is_none_or(|query| !query.chars().any(|c| c.is_digit(10)));

        let load_provider_completions = provider.as_ref().is_some_and(|provider| {
            trigger.as_ref().is_none_or(|trigger| {
                provider.is_completion_trigger(
                    &buffer,
                    buffer_position,
                    trigger,
                    trigger_in_words,
                    cx,
                )
            })
        });

        let provider_responses = if let Some(provider) = &provider
            && load_provider_completions
        {
            let trigger_character =
                trigger.filter(|trigger| buffer.read(cx).completion_triggers().contains(trigger));
            let completion_context = CompletionContext {
                trigger_kind: match &trigger_character {
                    Some(_) => CompletionTriggerKind::TRIGGER_CHARACTER,
                    None => CompletionTriggerKind::INVOKED,
                },
                trigger_character,
            };

            provider.completions(&buffer, buffer_position, completion_context, window, cx)
        } else {
            Task::ready(Ok(Vec::new()))
        };

        let load_word_completions = if !self.word_completions_enabled {
            false
        } else if requested_source
            == Some(CompletionsMenuSource::Words {
                ignore_threshold: true,
            })
        {
            true
        } else {
            load_provider_completions
                && completion_settings.words != WordsCompletionMode::Disabled
                && (ignore_word_threshold || {
                    let words_min_length = completion_settings.words_min_length;
                    // check whether word has at least `words_min_length` characters
                    let query_chars = query.iter().flat_map(|q| q.chars());
                    query_chars.take(words_min_length).count() == words_min_length
                })
        };

        let mut words = if load_word_completions {
            cx.background_spawn({
                let buffer_snapshot = buffer_snapshot.clone();
                async move {
                    buffer_snapshot.words_in_range(WordsQuery {
                        fuzzy_contents: None,
                        range: word_search_range,
                        skip_digits,
                    })
                }
            })
        } else {
            Task::ready(BTreeMap::default())
        };

        let snippets = if let Some(provider) = &provider
            && provider.show_snippets()
            && let Some(project) = self.project()
        {
            let char_classifier = buffer_snapshot
                .char_classifier_at(buffer_position)
                .scope_context(Some(CharScopeContext::Completion));
            project.update(cx, |project, cx| {
                snippet_completions(project, &buffer, buffer_position, char_classifier, cx)
            })
        } else {
            Task::ready(Ok(CompletionResponse {
                completions: Vec::new(),
                display_options: Default::default(),
                is_incomplete: false,
            }))
        };

        let snippet_sort_order = EditorSettings::get_global(cx).snippet_sort_order;

        let id = post_inc(&mut self.next_completion_id);
        let task = cx.spawn_in(window, async move |editor, cx| {
            let Ok(()) = editor.update(cx, |this, _| {
                this.completion_tasks.retain(|(task_id, _)| *task_id >= id);
            }) else {
                return;
            };

            // TODO: Ideally completions from different sources would be selectively re-queried, so
            // that having one source with `is_incomplete: true` doesn't cause all to be re-queried.
            let mut completions = Vec::new();
            let mut is_incomplete = false;
            let mut display_options: Option<CompletionDisplayOptions> = None;
            if let Some(provider_responses) = provider_responses.await.log_err()
                && !provider_responses.is_empty()
            {
                for response in provider_responses {
                    completions.extend(response.completions);
                    is_incomplete = is_incomplete || response.is_incomplete;
                    match display_options.as_mut() {
                        None => {
                            display_options = Some(response.display_options);
                        }
                        Some(options) => options.merge(&response.display_options),
                    }
                }
                if completion_settings.words == WordsCompletionMode::Fallback {
                    words = Task::ready(BTreeMap::default());
                }
            }
            let display_options = display_options.unwrap_or_default();

            let mut words = words.await;
            if let Some(word_to_exclude) = &word_to_exclude {
                words.remove(word_to_exclude);
            }
            for lsp_completion in &completions {
                words.remove(&lsp_completion.new_text);
            }
            completions.extend(words.into_iter().map(|(word, word_range)| Completion {
                replace_range: word_replace_range.clone(),
                new_text: word.clone(),
                label: CodeLabel::plain(word, None),
                match_start: None,
                snippet_deduplication_key: None,
                icon_path: None,
                documentation: None,
                source: CompletionSource::BufferWord {
                    word_range,
                    resolved: false,
                },
                insert_text_mode: Some(InsertTextMode::AS_IS),
                confirm: None,
            }));

            completions.extend(
                snippets
                    .await
                    .into_iter()
                    .flat_map(|response| response.completions),
            );

            let menu = if completions.is_empty() {
                None
            } else {
                let Ok((mut menu, matches_task)) = editor.update(cx, |editor, cx| {
                    let languages = editor
                        .workspace
                        .as_ref()
                        .and_then(|(workspace, _)| workspace.upgrade())
                        .map(|workspace| workspace.read(cx).app_state().languages.clone());
                    let menu = CompletionsMenu::new(
                        id,
                        requested_source.unwrap_or(if load_provider_completions {
                            CompletionsMenuSource::Normal
                        } else {
                            CompletionsMenuSource::SnippetsOnly
                        }),
                        sort_completions,
                        show_completion_documentation,
                        position,
                        query.clone(),
                        is_incomplete,
                        buffer.clone(),
                        completions.into(),
                        editor
                            .context_menu()
                            .borrow_mut()
                            .as_ref()
                            .map(|menu| menu.primary_scroll_handle()),
                        display_options,
                        snippet_sort_order,
                        languages,
                        language,
                        cx,
                    );

                    let query = if filter_completions { query } else { None };
                    let matches_task = menu.do_async_filtering(
                        query.unwrap_or_default(),
                        buffer_position,
                        &buffer,
                        cx,
                    );
                    (menu, matches_task)
                }) else {
                    return;
                };

                let matches = matches_task.await;

                let Ok(()) = editor.update_in(cx, |editor, window, cx| {
                    // Newer menu already set, so exit.
                    if let Some(CodeContextMenu::Completions(prev_menu)) =
                        editor.context_menu.borrow().as_ref()
                        && prev_menu.id > id
                    {
                        return;
                    };

                    // Only valid to take prev_menu because either the new menu is immediately set
                    // below, or the menu is hidden.
                    if let Some(CodeContextMenu::Completions(prev_menu)) =
                        editor.context_menu.borrow_mut().take()
                    {
                        let position_matches =
                            if prev_menu.initial_position == menu.initial_position {
                                true
                            } else {
                                let snapshot = editor.buffer.read(cx).read(cx);
                                prev_menu.initial_position.to_offset(&snapshot)
                                    == menu.initial_position.to_offset(&snapshot)
                            };
                        if position_matches {
                            // Preserve markdown cache before `set_filter_results` because it will
                            // try to populate the documentation cache.
                            menu.preserve_markdown_cache(prev_menu);
                        }
                    };

                    menu.set_filter_results(matches, provider, window, cx);
                }) else {
                    return;
                };

                menu.visible().then_some(menu)
            };

            editor
                .update_in(cx, |editor, window, cx| {
                    if editor.focus_handle.is_focused(window)
                        && let Some(menu) = menu
                    {
                        *editor.context_menu.borrow_mut() =
                            Some(CodeContextMenu::Completions(menu));

                        crate::hover_popover::hide_hover(editor, cx);
                        if editor.show_edit_predictions_in_menu() {
                            editor.update_visible_edit_prediction(window, cx);
                        } else {
                            editor
                                .discard_edit_prediction(EditPredictionDiscardReason::Ignored, cx);
                        }

                        cx.notify();
                        return;
                    }

                    if editor.completion_tasks.len() <= 1 {
                        // If there are no more completion tasks and the last menu was empty, we should hide it.
                        let was_hidden = editor.hide_context_menu(window, cx).is_none();
                        // If it was already hidden and we don't show edit predictions in the menu,
                        // we should also show the edit prediction when available.
                        if was_hidden && editor.show_edit_predictions_in_menu() {
                            editor.update_visible_edit_prediction(window, cx);
                        }
                    }
                })
                .ok();
        });

        self.completion_tasks.push((id, task));
    }

    #[cfg(any(test, feature = "test-support"))]
    pub fn current_completions(&self) -> Option<Vec<project::Completion>> {
        let menu = self.context_menu.borrow();
        if let CodeContextMenu::Completions(menu) = menu.as_ref()? {
            let completions = menu.completions.borrow();
            Some(completions.to_vec())
        } else {
            None
        }
    }

    pub fn with_completions_menu_matching_id<R>(
        &self,
        id: CompletionId,
        f: impl FnOnce(Option<&mut CompletionsMenu>) -> R,
    ) -> R {
        let mut context_menu = self.context_menu.borrow_mut();
        let Some(CodeContextMenu::Completions(completions_menu)) = &mut *context_menu else {
            return f(None);
        };
        if completions_menu.id != id {
            return f(None);
        }
        f(Some(completions_menu))
    }

    pub fn confirm_completion(
        &mut self,
        action: &ConfirmCompletion,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.do_completion(action.item_ix, CompletionIntent::Complete, window, cx)
    }

    pub fn confirm_completion_insert(
        &mut self,
        _: &ConfirmCompletionInsert,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.do_completion(None, CompletionIntent::CompleteWithInsert, window, cx)
    }

    pub fn confirm_completion_replace(
        &mut self,
        _: &ConfirmCompletionReplace,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.do_completion(None, CompletionIntent::CompleteWithReplace, window, cx)
    }

    pub fn compose_completion(
        &mut self,
        action: &ComposeCompletion,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.do_completion(action.item_ix, CompletionIntent::Compose, window, cx)
    }

    fn do_completion(
        &mut self,
        item_ix: Option<usize>,
        intent: CompletionIntent,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) -> Option<Task<Result<()>>> {
        use language::ToOffset as _;

        let CodeContextMenu::Completions(completions_menu) = self.hide_context_menu(window, cx)?
        else {
            return None;
        };

        let candidate_id = {
            let entries = completions_menu.entries.borrow();
            let mat = entries.get(item_ix.unwrap_or(completions_menu.selected_item))?;
            if self.show_edit_predictions_in_menu() {
                self.discard_edit_prediction(EditPredictionDiscardReason::Rejected, cx);
            }
            mat.candidate_id
        };

        let completion = completions_menu
            .completions
            .borrow()
            .get(candidate_id)?
            .clone();
        cx.stop_propagation();

        let buffer_handle = completions_menu.buffer.clone();
        let multibuffer_snapshot = self.buffer.read(cx).snapshot(cx);
        let (initial_position, _) =
            multibuffer_snapshot.anchor_to_buffer_anchor(completions_menu.initial_position)?;

        let CompletionEdit {
            new_text,
            snippet,
            replace_range,
        } = process_completion_for_edit(&completion, intent, &buffer_handle, &initial_position, cx);

        let buffer = buffer_handle.read(cx).snapshot();
        let newest_selection = self.selections.newest_anchor();

        let Some(replace_range_multibuffer) =
            multibuffer_snapshot.buffer_anchor_range_to_anchor_range(replace_range.clone())
        else {
            return None;
        };

        let Some((buffer_snapshot, newest_range_buffer)) =
            multibuffer_snapshot.anchor_range_to_buffer_anchor_range(newest_selection.range())
        else {
            return None;
        };

        let old_text = buffer
            .text_for_range(replace_range.clone())
            .collect::<String>();
        let lookbehind = newest_range_buffer
            .start
            .to_offset(buffer_snapshot)
            .saturating_sub(replace_range.start.to_offset(&buffer_snapshot));
        let lookahead = replace_range
            .end
            .to_offset(&buffer_snapshot)
            .saturating_sub(newest_range_buffer.end.to_offset(&buffer));
        let prefix = &old_text[..old_text.len().saturating_sub(lookahead)];
        let suffix = &old_text[lookbehind.min(old_text.len())..];

        let selections = self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx));
        let mut ranges = Vec::new();
        let mut all_commit_ranges = Vec::new();
        let mut linked_edits = LinkedEdits::new();

        let text: Arc<str> = new_text.clone().into();
        for selection in &selections {
            let range = if selection.id == newest_selection.id {
                replace_range_multibuffer.clone()
            } else {
                let mut range = selection.range();

                // if prefix is present, don't duplicate it
                if multibuffer_snapshot
                    .contains_str_at(range.start.saturating_sub_usize(lookbehind), prefix)
                {
                    range.start = range.start.saturating_sub_usize(lookbehind);

                    // if suffix is also present, mimic the newest cursor and replace it
                    if selection.id != newest_selection.id
                        && multibuffer_snapshot.contains_str_at(range.end, suffix)
                    {
                        range.end += lookahead;
                    }
                }
                range.to_anchors(&multibuffer_snapshot)
            };

            ranges.push(range.clone());

            let start_anchor = multibuffer_snapshot.anchor_before(range.start);
            let end_anchor = multibuffer_snapshot.anchor_after(range.end);

            if let Some((buffer_snapshot_2, anchor_range)) =
                multibuffer_snapshot.anchor_range_to_buffer_anchor_range(start_anchor..end_anchor)
                && buffer_snapshot_2.remote_id() == buffer_snapshot.remote_id()
            {
                all_commit_ranges.push(anchor_range.clone());
                if !self.linked_edit_ranges.is_empty() {
                    linked_edits.push(&self, anchor_range, text.clone(), cx);
                }
            }
        }

        let common_prefix_len = old_text
            .chars()
            .zip(new_text.chars())
            .take_while(|(a, b)| a == b)
            .map(|(a, _)| a.len_utf8())
            .sum::<usize>();

        cx.emit(EditorEvent::InputHandled {
            utf16_range_to_replace: None,
            text: new_text[common_prefix_len..].into(),
        });

        let tx_id = self.transact(window, cx, |editor, window, cx| {
            if let Some(mut snippet) = snippet {
                snippet.text = new_text.to_string();
                let offset_ranges = ranges
                    .iter()
                    .map(|range| range.to_offset(&multibuffer_snapshot))
                    .collect::<Vec<_>>();
                editor
                    .insert_snippet(&offset_ranges, snippet, window, cx)
                    .log_err();
            } else {
                editor.buffer.update(cx, |multi_buffer, cx| {
                    let auto_indent = match completion.insert_text_mode {
                        Some(InsertTextMode::AS_IS) => None,
                        _ => editor.autoindent_mode.clone(),
                    };
                    let edits = ranges.into_iter().map(|range| (range, new_text.as_str()));
                    multi_buffer.edit(edits, auto_indent, cx);
                });
            }
            linked_edits.apply(cx);
            editor.refresh_edit_prediction(true, false, window, cx);
        });
        self.invalidate_autoclose_regions(
            &self.selections.disjoint_anchors_arc(),
            &multibuffer_snapshot,
        );

        let show_new_completions_on_confirm = completion
            .confirm
            .as_ref()
            .is_some_and(|confirm| confirm(intent, window, cx));
        if show_new_completions_on_confirm {
            self.open_or_update_completions_menu(None, None, false, window, cx);
        }

        let provider = self.completion_provider.as_ref()?;

        let lsp_store = self.project().map(|project| project.read(cx).lsp_store());
        let command = lsp_store.as_ref().and_then(|lsp_store| {
            let CompletionSource::Lsp {
                lsp_completion,
                server_id,
                ..
            } = &completion.source
            else {
                return None;
            };
            let lsp_command = lsp_completion.command.as_ref()?;
            let available_commands = lsp_store
                .read(cx)
                .lsp_server_capabilities
                .get(server_id)
                .and_then(|server_capabilities| {
                    server_capabilities
                        .execute_command_provider
                        .as_ref()
                        .map(|options| options.commands.as_slice())
                })?;
            if available_commands.contains(&lsp_command.command) {
                Some(CodeAction {
                    server_id: *server_id,
                    range: language::Anchor::min_min_range_for_buffer(buffer.remote_id()),
                    lsp_action: LspAction::Command(lsp_command.clone()),
                    resolved: false,
                })
            } else {
                None
            }
        });

        drop(completion);
        let apply_edits = provider.apply_additional_edits_for_completion(
            buffer_handle.clone(),
            completions_menu.completions.clone(),
            candidate_id,
            true,
            all_commit_ranges,
            cx,
        );

        let editor_settings = EditorSettings::get_global(cx);
        if editor_settings.show_signature_help_after_edits || editor_settings.auto_signature_help {
            // After the code completion is finished, users often want to know what signatures are needed.
            // so we should automatically call signature_help
            self.show_signature_help(&ShowSignatureHelp, window, cx);
        }

        Some(cx.spawn_in(window, async move |editor, cx| {
            let additional_edits_tx = apply_edits.await?;

            if let Some((lsp_store, command)) = lsp_store.zip(command) {
                let title = command.lsp_action.title().to_owned();
                let project_transaction = lsp_store
                    .update(cx, |lsp_store, cx| {
                        lsp_store.apply_code_action(buffer_handle, command, false, cx)
                    })
                    .await
                    .context("applying post-completion command")?;
                if let Some(workspace) = editor.read_with(cx, |editor, _| editor.workspace())? {
                    Self::open_project_transaction(
                        &editor,
                        workspace.downgrade(),
                        project_transaction,
                        title,
                        cx,
                    )
                    .await?;
                }
            }

            if let Some(tx_id) = tx_id
                && let Some(additional_edits_tx) = additional_edits_tx
            {
                editor
                    .update(cx, |editor, cx| {
                        editor.buffer.update(cx, |buffer, cx| {
                            buffer.merge_transactions(additional_edits_tx.id, tx_id, cx)
                        });
                    })
                    .context("merge transactions")?;
            }

            Ok(())
        }))
    }

    pub fn toggle_code_actions(
        &mut self,
        action: &ToggleCodeActions,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let quick_launch = action.quick_launch;
        let mut context_menu = self.context_menu.borrow_mut();
        if let Some(CodeContextMenu::CodeActions(code_actions)) = context_menu.as_ref() {
            if code_actions.deployed_from == action.deployed_from {
                // Toggle if we're selecting the same one
                *context_menu = None;
                cx.notify();
                return;
            } else {
                // Otherwise, clear it and start a new one
                *context_menu = None;
                cx.notify();
            }
        }
        drop(context_menu);
        let snapshot = self.snapshot(window, cx);
        let deployed_from = action.deployed_from.clone();
        let action = action.clone();
        self.completion_tasks.clear();
        self.discard_edit_prediction(EditPredictionDiscardReason::Ignored, cx);

        let multibuffer_point = match &action.deployed_from {
            Some(CodeActionSource::Indicator(row)) | Some(CodeActionSource::RunMenu(row)) => {
                DisplayPoint::new(*row, 0).to_point(&snapshot)
            }
            _ => self
                .selections
                .newest::<Point>(&snapshot.display_snapshot)
                .head(),
        };
        let Some((buffer, buffer_row)) = snapshot
            .buffer_snapshot()
            .buffer_line_for_row(MultiBufferRow(multibuffer_point.row))
            .and_then(|(buffer_snapshot, range)| {
                self.buffer()
                    .read(cx)
                    .buffer(buffer_snapshot.remote_id())
                    .map(|buffer| (buffer, range.start.row))
            })
        else {
            return;
        };
        let buffer_id = buffer.read(cx).remote_id();
        let tasks = self
            .runnables
            .runnables((buffer_id, buffer_row))
            .map(|t| Arc::new(t.to_owned()));

        if !self.focus_handle.is_focused(window) {
            return;
        }
        let project = self.project.clone();

        let code_actions_task = match deployed_from {
            Some(CodeActionSource::RunMenu(_)) => Task::ready(None),
            _ => self.code_actions(buffer_row, window, cx),
        };

        let runnable_task = match deployed_from {
            Some(CodeActionSource::Indicator(_)) => Task::ready(Ok(Default::default())),
            _ => {
                let mut task_context_task = Task::ready(None);
                if let Some(tasks) = &tasks
                    && let Some(project) = project
                {
                    task_context_task =
                        Self::build_tasks_context(&project, &buffer, buffer_row, tasks, cx);
                }

                cx.spawn_in(window, {
                    let buffer = buffer.clone();
                    async move |editor, cx| {
                        let task_context = task_context_task.await;

                        let resolved_tasks =
                            tasks
                                .zip(task_context.clone())
                                .map(|(tasks, task_context)| ResolvedTasks {
                                    templates: tasks.resolve(&task_context).collect(),
                                    position: snapshot.buffer_snapshot().anchor_before(Point::new(
                                        multibuffer_point.row,
                                        tasks.column,
                                    )),
                                });
                        let debug_scenarios = editor
                            .update(cx, |editor, cx| {
                                editor.debug_scenarios(&resolved_tasks, &buffer, cx)
                            })?
                            .await;
                        anyhow::Ok((resolved_tasks, debug_scenarios, task_context))
                    }
                })
            }
        };

        cx.spawn_in(window, async move |editor, cx| {
            let (resolved_tasks, debug_scenarios, task_context) = runnable_task.await?;
            let code_actions = code_actions_task.await;
            let spawn_straight_away = quick_launch
                && resolved_tasks
                    .as_ref()
                    .is_some_and(|tasks| tasks.templates.len() == 1)
                && code_actions
                    .as_ref()
                    .is_none_or(|actions| actions.is_empty())
                && debug_scenarios.is_empty();

            editor.update_in(cx, |editor, window, cx| {
                crate::hover_popover::hide_hover(editor, cx);
                let actions = CodeActionContents::new(
                    resolved_tasks,
                    code_actions,
                    debug_scenarios,
                    task_context.unwrap_or_default(),
                );

                // Don't show the menu if there are no actions available
                if actions.is_empty() {
                    cx.notify();
                    return Task::ready(Ok(()));
                }

                *editor.context_menu.borrow_mut() =
                    Some(CodeContextMenu::CodeActions(CodeActionsMenu {
                        buffer,
                        actions,
                        selected_item: Default::default(),
                        scroll_handle: UniformListScrollHandle::default(),
                        deployed_from,
                    }));
                cx.notify();
                if spawn_straight_away
                    && let Some(task) = editor.confirm_code_action(
                        &ConfirmCodeAction { item_ix: Some(0) },
                        window,
                        cx,
                    )
                {
                    return task;
                }

                Task::ready(Ok(()))
            })
        })
        .detach_and_log_err(cx);
    }

    fn debug_scenarios(
        &mut self,
        resolved_tasks: &Option<ResolvedTasks>,
        buffer: &Entity<Buffer>,
        cx: &mut App,
    ) -> Task<Vec<task::DebugScenario>> {
        maybe!({
            let project = self.project()?;
            let dap_store = project.read(cx).dap_store();
            let mut scenarios = vec![];
            let resolved_tasks = resolved_tasks.as_ref()?;
            let buffer = buffer.read(cx);
            let language = buffer.language()?;
            let debug_adapter = LanguageSettings::for_buffer(&buffer, cx)
                .debuggers
                .first()
                .map(SharedString::from)
                .or_else(|| language.config().debuggers.first().map(SharedString::from))?;

            dap_store.update(cx, |dap_store, cx| {
                for (_, task) in &resolved_tasks.templates {
                    let maybe_scenario = dap_store.debug_scenario_for_build_task(
                        task.original_task().clone(),
                        debug_adapter.clone().into(),
                        task.display_label().to_owned().into(),
                        cx,
                    );
                    scenarios.push(maybe_scenario);
                }
            });
            Some(cx.background_spawn(async move {
                futures::future::join_all(scenarios)
                    .await
                    .into_iter()
                    .flatten()
                    .collect::<Vec<_>>()
            }))
        })
        .unwrap_or_else(|| Task::ready(vec![]))
    }

    fn code_actions(
        &mut self,
        buffer_row: u32,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Option<Rc<[AvailableCodeAction]>>> {
        let mut task = self.code_actions_task.take();
        cx.spawn_in(window, async move |editor, cx| {
            while let Some(prev_task) = task {
                prev_task.await.log_err();
                task = editor
                    .update(cx, |this, _| this.code_actions_task.take())
                    .ok()?;
            }

            editor
                .update(cx, |editor, cx| {
                    editor
                        .available_code_actions
                        .clone()
                        .and_then(|(location, code_actions)| {
                            let snapshot = location.buffer.read(cx).snapshot();
                            let point_range = location.range.to_point(&snapshot);
                            let point_range = point_range.start.row..=point_range.end.row;
                            if point_range.contains(&buffer_row) {
                                Some(code_actions)
                            } else {
                                None
                            }
                        })
                })
                .ok()
                .flatten()
        })
    }

    pub fn confirm_code_action(
        &mut self,
        action: &ConfirmCodeAction,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        let actions_menu =
            if let CodeContextMenu::CodeActions(menu) = self.hide_context_menu(window, cx)? {
                menu
            } else {
                return None;
            };

        let action_ix = action.item_ix.unwrap_or(actions_menu.selected_item);
        let action = actions_menu.actions.get(action_ix)?;
        let title = action.label();
        let buffer = actions_menu.buffer;
        let workspace = self.workspace()?;

        match action {
            CodeActionsItem::Task(task_source_kind, resolved_task) => {
                workspace.update(cx, |workspace, cx| {
                    workspace.schedule_resolved_task(
                        task_source_kind,
                        resolved_task,
                        false,
                        window,
                        cx,
                    );

                    Some(Task::ready(Ok(())))
                })
            }
            CodeActionsItem::CodeAction { action, provider } => {
                let apply_code_action =
                    provider.apply_code_action(buffer, action, true, window, cx);
                let workspace = workspace.downgrade();
                Some(cx.spawn_in(window, async move |editor, cx| {
                    let project_transaction = apply_code_action.await?;
                    Self::open_project_transaction(
                        &editor,
                        workspace,
                        project_transaction,
                        title,
                        cx,
                    )
                    .await
                }))
            }
            CodeActionsItem::DebugScenario(scenario) => {
                let context = actions_menu.actions.context.into();

                workspace.update(cx, |workspace, cx| {
                    dap::send_telemetry(&scenario, TelemetrySpawnLocation::Gutter, cx);
                    workspace.start_debug_session(
                        scenario,
                        context,
                        Some(buffer),
                        None,
                        window,
                        cx,
                    );
                });
                Some(Task::ready(Ok(())))
            }
        }
    }

    fn open_transaction_for_hidden_buffers(
        workspace: Entity<Workspace>,
        transaction: ProjectTransaction,
        title: String,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if transaction.0.is_empty() {
            return;
        }

        let edited_buffers_already_open = {
            let other_editors: Vec<Entity<Editor>> = workspace
                .read(cx)
                .panes()
                .iter()
                .flat_map(|pane| pane.read(cx).items_of_type::<Editor>())
                .filter(|editor| editor.entity_id() != cx.entity_id())
                .collect();

            transaction.0.keys().all(|buffer| {
                other_editors.iter().any(|editor| {
                    let multi_buffer = editor.read(cx).buffer();
                    multi_buffer.read(cx).is_singleton()
                        && multi_buffer
                            .read(cx)
                            .as_singleton()
                            .map_or(false, |singleton| {
                                singleton.entity_id() == buffer.entity_id()
                            })
                })
            })
        };
        if !edited_buffers_already_open {
            let workspace = workspace.downgrade();
            cx.defer_in(window, move |_, window, cx| {
                cx.spawn_in(window, async move |editor, cx| {
                    Self::open_project_transaction(&editor, workspace, transaction, title, cx)
                        .await
                        .ok()
                })
                .detach();
            });
        }
    }

    pub async fn open_project_transaction(
        editor: &WeakEntity<Editor>,
        workspace: WeakEntity<Workspace>,
        transaction: ProjectTransaction,
        title: String,
        cx: &mut AsyncWindowContext,
    ) -> Result<()> {
        let mut entries = transaction.0.into_iter().collect::<Vec<_>>();
        cx.update(|_, cx| {
            entries.sort_unstable_by_key(|(buffer, _)| {
                buffer.read(cx).file().map(|f| f.path().clone())
            });
        })?;
        if entries.is_empty() {
            return Ok(());
        }

        // If the project transaction's edits are all contained within this editor, then
        // avoid opening a new editor to display them.

        if let [(buffer, transaction)] = &*entries {
            let cursor_excerpt = editor.update(cx, |editor, cx| {
                let snapshot = editor.buffer().read(cx).snapshot(cx);
                let head = editor.selections.newest_anchor().head();
                let (buffer_snapshot, excerpt_range) = snapshot.excerpt_containing(head..head)?;
                if buffer_snapshot.remote_id() != buffer.read(cx).remote_id() {
                    return None;
                }
                Some(excerpt_range)
            })?;

            if let Some(excerpt_range) = cursor_excerpt {
                let all_edits_within_excerpt = buffer.read_with(cx, |buffer, _| {
                    let excerpt_range = excerpt_range.context.to_offset(buffer);
                    buffer
                        .edited_ranges_for_transaction::<usize>(transaction)
                        .all(|range| {
                            excerpt_range.start <= range.start && excerpt_range.end >= range.end
                        })
                });

                if all_edits_within_excerpt {
                    return Ok(());
                }
            }
        }

        let mut ranges_to_highlight = Vec::new();
        let excerpt_buffer = cx.new(|cx| {
            let mut multibuffer = MultiBuffer::new(Capability::ReadWrite).with_title(title);
            for (buffer_handle, transaction) in &entries {
                let edited_ranges = buffer_handle
                    .read(cx)
                    .edited_ranges_for_transaction::<Point>(transaction)
                    .collect::<Vec<_>>();
                multibuffer.set_excerpts_for_path(
                    PathKey::for_buffer(buffer_handle, cx),
                    buffer_handle.clone(),
                    edited_ranges.clone(),
                    multibuffer_context_lines(cx),
                    cx,
                );
                let snapshot = multibuffer.snapshot(cx);
                let buffer_snapshot = buffer_handle.read(cx).snapshot();
                ranges_to_highlight.extend(edited_ranges.into_iter().filter_map(|range| {
                    let text_range = buffer_snapshot.anchor_range_inside(range);
                    let start = snapshot.anchor_in_buffer(text_range.start)?;
                    let end = snapshot.anchor_in_buffer(text_range.end)?;
                    Some(start..end)
                }));
            }
            multibuffer.push_transaction(entries.iter().map(|(b, t)| (b, t)), cx);
            multibuffer
        });

        workspace.update_in(cx, |workspace, window, cx| {
            let project = workspace.project().clone();
            let editor =
                cx.new(|cx| Editor::for_multibuffer(excerpt_buffer, Some(project), window, cx));
            workspace.add_item_to_active_pane(Box::new(editor.clone()), None, true, window, cx);
            editor.update(cx, |editor, cx| {
                editor.highlight_background(
                    HighlightKey::Editor,
                    &ranges_to_highlight,
                    |_, theme| theme.colors().editor_highlighted_line_background,
                    cx,
                );
            });
        })?;

        Ok(())
    }

    pub fn clear_code_action_providers(&mut self) {
        self.code_action_providers.clear();
        self.available_code_actions.take();
    }

    pub fn add_code_action_provider(
        &mut self,
        provider: Rc<dyn CodeActionProvider>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self
            .code_action_providers
            .iter()
            .any(|existing_provider| existing_provider.id() == provider.id())
        {
            return;
        }

        self.code_action_providers.push(provider);
        self.refresh_code_actions(window, cx);
    }

    pub fn remove_code_action_provider(
        &mut self,
        id: Arc<str>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.code_action_providers
            .retain(|provider| provider.id() != id);
        self.refresh_code_actions(window, cx);
    }

    pub fn code_actions_enabled_for_toolbar(&self, cx: &App) -> bool {
        !self.code_action_providers.is_empty()
            && EditorSettings::get_global(cx).toolbar.code_actions
    }

    pub fn has_available_code_actions(&self) -> bool {
        self.available_code_actions
            .as_ref()
            .is_some_and(|(_, actions)| !actions.is_empty())
    }

    fn render_inline_code_actions(
        &self,
        icon_size: ui::IconSize,
        display_row: DisplayRow,
        is_active: bool,
        cx: &mut Context<Self>,
    ) -> AnyElement {
        let show_tooltip = !self.context_menu_visible();
        IconButton::new("inline_code_actions", ui::IconName::BoltFilled)
            .icon_size(icon_size)
            .shape(ui::IconButtonShape::Square)
            .icon_color(ui::Color::Hidden)
            .toggle_state(is_active)
            .when(show_tooltip, |this| {
                this.tooltip({
                    let focus_handle = self.focus_handle.clone();
                    move |_window, cx| {
                        Tooltip::for_action_in(
                            "Toggle Code Actions",
                            &ToggleCodeActions {
                                deployed_from: None,
                                quick_launch: false,
                            },
                            &focus_handle,
                            cx,
                        )
                    }
                })
            })
            .on_click(cx.listener(move |editor, _: &ClickEvent, window, cx| {
                window.focus(&editor.focus_handle(cx), cx);
                editor.toggle_code_actions(
                    &crate::actions::ToggleCodeActions {
                        deployed_from: Some(crate::actions::CodeActionSource::Indicator(
                            display_row,
                        )),
                        quick_launch: false,
                    },
                    window,
                    cx,
                );
            }))
            .into_any_element()
    }

    pub fn context_menu(&self) -> &RefCell<Option<CodeContextMenu>> {
        &self.context_menu
    }

    fn refresh_code_actions(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.code_actions_task = Some(cx.spawn_in(window, async move |this, cx| {
            cx.background_executor()
                .timer(CODE_ACTIONS_DEBOUNCE_TIMEOUT)
                .await;

            let (start_buffer, start, _, end, _newest_selection) = this
                .update(cx, |this, cx| {
                    let newest_selection = this.selections.newest_anchor().clone();
                    if newest_selection.head().diff_base_anchor().is_some() {
                        return None;
                    }
                    let display_snapshot = this.display_snapshot(cx);
                    let newest_selection_adjusted =
                        this.selections.newest_adjusted(&display_snapshot);
                    let buffer = this.buffer.read(cx);

                    let (start_buffer, start) =
                        buffer.text_anchor_for_position(newest_selection_adjusted.start, cx)?;
                    let (end_buffer, end) =
                        buffer.text_anchor_for_position(newest_selection_adjusted.end, cx)?;

                    Some((start_buffer, start, end_buffer, end, newest_selection))
                })?
                .filter(|(start_buffer, _, end_buffer, _, _)| start_buffer == end_buffer)
                .context(
                    "Expected selection to lie in a single buffer when refreshing code actions",
                )?;
            let (providers, tasks) = this.update_in(cx, |this, window, cx| {
                let providers = this.code_action_providers.clone();
                let tasks = this
                    .code_action_providers
                    .iter()
                    .map(|provider| provider.code_actions(&start_buffer, start..end, window, cx))
                    .collect::<Vec<_>>();
                (providers, tasks)
            })?;

            let mut actions = Vec::new();
            for (provider, provider_actions) in
                providers.into_iter().zip(future::join_all(tasks).await)
            {
                if let Some(provider_actions) = provider_actions.log_err() {
                    actions.extend(provider_actions.into_iter().map(|action| {
                        AvailableCodeAction {
                            action,
                            provider: provider.clone(),
                        }
                    }));
                }
            }

            this.update(cx, |this, cx| {
                this.available_code_actions = if actions.is_empty() {
                    None
                } else {
                    Some((
                        Location {
                            buffer: start_buffer,
                            range: start..end,
                        },
                        actions.into(),
                    ))
                };
                cx.notify();
            })
        }));
    }

    fn start_inline_blame_timer(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if let Some(delay) = ProjectSettings::get_global(cx).git.inline_blame_delay() {
            self.show_git_blame_inline = false;

            self.show_git_blame_inline_delay_task =
                Some(cx.spawn_in(window, async move |this, cx| {
                    cx.background_executor().timer(delay).await;

                    this.update(cx, |this, cx| {
                        this.show_git_blame_inline = true;
                        cx.notify();
                    })
                    .log_err();
                }));
        }
    }

    pub fn blame_hover(&mut self, _: &BlameHover, window: &mut Window, cx: &mut Context<Self>) {
        let snapshot = self.snapshot(window, cx);
        let cursor = self
            .selections
            .newest::<Point>(&snapshot.display_snapshot)
            .head();
        let Some((buffer, point)) = snapshot.buffer_snapshot().point_to_buffer_point(cursor) else {
            return;
        };

        if self.blame.is_none() {
            self.start_git_blame(true, window, cx);
        }
        let Some(blame) = self.blame.as_ref() else {
            return;
        };

        let row_info = RowInfo {
            buffer_id: Some(buffer.remote_id()),
            buffer_row: Some(point.row),
            ..Default::default()
        };
        let Some((buffer, blame_entry)) = blame
            .update(cx, |blame, cx| blame.blame_for_rows(&[row_info], cx).next())
            .flatten()
        else {
            return;
        };

        let anchor = self.selections.newest_anchor().head();
        let position = self.to_pixel_point(anchor, &snapshot, window, cx);
        if let (Some(position), Some(last_bounds)) = (position, self.last_bounds) {
            self.show_blame_popover(
                buffer,
                &blame_entry,
                position + last_bounds.origin,
                true,
                cx,
            );
        };
    }

    fn show_blame_popover(
        &mut self,
        buffer: BufferId,
        blame_entry: &BlameEntry,
        position: gpui::Point<Pixels>,
        ignore_timeout: bool,
        cx: &mut Context<Self>,
    ) {
        if let Some(state) = &mut self.inline_blame_popover {
            state.hide_task.take();
        } else {
            let blame_popover_delay = EditorSettings::get_global(cx).hover_popover_delay.0;
            let blame_entry = blame_entry.clone();
            let show_task = cx.spawn(async move |editor, cx| {
                if !ignore_timeout {
                    cx.background_executor()
                        .timer(std::time::Duration::from_millis(blame_popover_delay))
                        .await;
                }
                editor
                    .update(cx, |editor, cx| {
                        editor.inline_blame_popover_show_task.take();
                        let Some(blame) = editor.blame.as_ref() else {
                            return;
                        };
                        let blame = blame.read(cx);
                        let details = blame.details_for_entry(buffer, &blame_entry);
                        let markdown = cx.new(|cx| {
                            Markdown::new(
                                details
                                    .as_ref()
                                    .map(|message| message.message.clone())
                                    .unwrap_or_default(),
                                None,
                                None,
                                cx,
                            )
                        });
                        editor.inline_blame_popover = Some(InlineBlamePopover {
                            position,
                            hide_task: None,
                            popover_bounds: None,
                            popover_state: InlineBlamePopoverState {
                                scroll_handle: ScrollHandle::new(),
                                commit_message: details,
                                markdown,
                            },
                            keyboard_grace: ignore_timeout,
                        });
                        cx.notify();
                    })
                    .ok();
            });
            self.inline_blame_popover_show_task = Some(show_task);
        }
    }

    pub fn has_mouse_context_menu(&self) -> bool {
        self.mouse_context_menu.is_some()
    }

    pub fn hide_blame_popover(&mut self, ignore_timeout: bool, cx: &mut Context<Self>) -> bool {
        self.inline_blame_popover_show_task.take();
        if let Some(state) = &mut self.inline_blame_popover {
            let hide_task = cx.spawn(async move |editor, cx| {
                if !ignore_timeout {
                    cx.background_executor()
                        .timer(std::time::Duration::from_millis(100))
                        .await;
                }
                editor
                    .update(cx, |editor, cx| {
                        editor.inline_blame_popover.take();
                        cx.notify();
                    })
                    .ok();
            });
            state.hide_task = Some(hide_task);
            true
        } else {
            false
        }
    }

    fn refresh_document_highlights(&mut self, cx: &mut Context<Self>) -> Option<()> {
        if self.pending_rename.is_some() {
            return None;
        }

        let provider = self.semantics_provider.clone()?;
        let buffer = self.buffer.read(cx);
        let newest_selection = self.selections.newest_anchor().clone();
        let cursor_position = newest_selection.head();
        let (cursor_buffer, cursor_buffer_position) =
            buffer.text_anchor_for_position(cursor_position, cx)?;
        let (tail_buffer, tail_buffer_position) =
            buffer.text_anchor_for_position(newest_selection.tail(), cx)?;
        if cursor_buffer != tail_buffer {
            return None;
        }

        let snapshot = cursor_buffer.read(cx).snapshot();
        let word_ranges = cx.background_spawn(async move {
            // this might look odd to put on the background thread, but
            // `surrounding_word` can be quite expensive as it calls into
            // tree-sitter language scopes
            let (start_word_range, _) = snapshot.surrounding_word(cursor_buffer_position, None);
            let (end_word_range, _) = snapshot.surrounding_word(tail_buffer_position, None);
            (start_word_range, end_word_range)
        });

        let debounce = EditorSettings::get_global(cx).lsp_highlight_debounce.0;
        self.document_highlights_task = Some(cx.spawn(async move |this, cx| {
            let (start_word_range, end_word_range) = word_ranges.await;
            if start_word_range != end_word_range {
                this.update(cx, |this, cx| {
                    this.document_highlights_task.take();
                    this.clear_background_highlights(HighlightKey::DocumentHighlightRead, cx);
                    this.clear_background_highlights(HighlightKey::DocumentHighlightWrite, cx);
                })
                .ok();
                return;
            }
            cx.background_executor()
                .timer(Duration::from_millis(debounce))
                .await;

            let highlights = if let Some(highlights) = cx.update(|cx| {
                provider.document_highlights(&cursor_buffer, cursor_buffer_position, cx)
            }) {
                highlights.await.log_err()
            } else {
                None
            };

            if let Some(highlights) = highlights {
                this.update(cx, |this, cx| {
                    if this.pending_rename.is_some() {
                        return;
                    }

                    let buffer = this.buffer.read(cx);
                    if buffer
                        .text_anchor_for_position(cursor_position, cx)
                        .is_none_or(|(buffer, _)| buffer != cursor_buffer)
                    {
                        return;
                    }

                    let mut write_ranges = Vec::new();
                    let mut read_ranges = Vec::new();
                    let multibuffer_snapshot = buffer.snapshot(cx);
                    for highlight in highlights {
                        for range in
                            multibuffer_snapshot.buffer_range_to_excerpt_ranges(highlight.range)
                        {
                            if highlight.kind == lsp::DocumentHighlightKind::WRITE {
                                write_ranges.push(range);
                            } else {
                                read_ranges.push(range);
                            }
                        }
                    }

                    this.highlight_background(
                        HighlightKey::DocumentHighlightRead,
                        &read_ranges,
                        |_, theme| theme.colors().editor_document_highlight_read_background,
                        cx,
                    );
                    this.highlight_background(
                        HighlightKey::DocumentHighlightWrite,
                        &write_ranges,
                        |_, theme| theme.colors().editor_document_highlight_write_background,
                        cx,
                    );
                    cx.notify();
                })
                .log_err();
            }
        }));
        None
    }

    fn prepare_highlight_query_from_selection(
        &mut self,
        snapshot: &DisplaySnapshot,
        cx: &mut Context<Editor>,
    ) -> Option<(String, Range<Anchor>)> {
        if matches!(self.mode, EditorMode::SingleLine) {
            return None;
        }
        if !self.use_selection_highlight || !EditorSettings::get_global(cx).selection_highlight {
            return None;
        }
        if self.selections.count() != 1 || self.selections.line_mode() {
            return None;
        }
        let selection = self.selections.newest::<Point>(&snapshot);
        // If the selection spans multiple rows OR it is empty
        if selection.start.row != selection.end.row
            || selection.start.column == selection.end.column
        {
            return None;
        }
        let selection_anchor_range = selection.range().to_anchors(snapshot.buffer_snapshot());
        let query = snapshot
            .buffer_snapshot()
            .text_for_range(selection_anchor_range.clone())
            .collect::<String>();
        if query.trim().is_empty() {
            return None;
        }
        Some((query, selection_anchor_range))
    }

    #[ztracing::instrument(skip_all)]
    fn update_selection_occurrence_highlights(
        &mut self,
        multi_buffer_snapshot: MultiBufferSnapshot,
        query_text: String,
        query_range: Range<Anchor>,
        multi_buffer_range_to_query: Range<Point>,
        use_debounce: bool,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) -> Task<()> {
        cx.spawn_in(window, async move |editor, cx| {
            if use_debounce {
                cx.background_executor()
                    .timer(SELECTION_HIGHLIGHT_DEBOUNCE_TIMEOUT)
                    .await;
            }
            let match_task = cx.background_spawn(async move {
                let buffer_ranges = multi_buffer_snapshot
                    .range_to_buffer_ranges(
                        multi_buffer_range_to_query.start..multi_buffer_range_to_query.end,
                    )
                    .into_iter()
                    .filter(|(_, excerpt_visible_range, _)| !excerpt_visible_range.is_empty());
                let mut match_ranges = Vec::new();
                let Ok(regex) = project::search::SearchQuery::text(
                    query_text,
                    false,
                    false,
                    false,
                    Default::default(),
                    Default::default(),
                    false,
                    None,
                ) else {
                    return Vec::default();
                };
                let query_range = query_range.to_anchors(&multi_buffer_snapshot);
                for (buffer_snapshot, search_range, _) in buffer_ranges {
                    match_ranges.extend(
                        regex
                            .search(
                                &buffer_snapshot,
                                Some(search_range.start.0..search_range.end.0),
                            )
                            .await
                            .into_iter()
                            .filter_map(|match_range| {
                                let match_start = buffer_snapshot
                                    .anchor_after(search_range.start + match_range.start);
                                let match_end = buffer_snapshot
                                    .anchor_before(search_range.start + match_range.end);
                                {
                                    let range = multi_buffer_snapshot
                                        .anchor_in_buffer(match_start)?
                                        ..multi_buffer_snapshot.anchor_in_buffer(match_end)?;
                                    Some(range).filter(|match_anchor_range| {
                                        match_anchor_range != &query_range
                                    })
                                }
                            }),
                    );
                }
                match_ranges
            });
            let match_ranges = match_task.await;
            editor
                .update_in(cx, |editor, _, cx| {
                    if use_debounce {
                        editor.clear_background_highlights(HighlightKey::SelectedTextHighlight, cx);
                        editor.debounced_selection_highlight_complete = true;
                    } else if editor.debounced_selection_highlight_complete {
                        return;
                    }
                    if !match_ranges.is_empty() {
                        editor.highlight_background(
                            HighlightKey::SelectedTextHighlight,
                            &match_ranges,
                            |_, theme| theme.colors().editor_document_highlight_bracket_background,
                            cx,
                        )
                    }
                })
                .log_err();
        })
    }

    fn refresh_single_line_folds(&mut self, window: &mut Window, cx: &mut Context<Editor>) {
        struct NewlineFold;
        let type_id = std::any::TypeId::of::<NewlineFold>();
        if !self.mode.is_single_line() {
            return;
        }
        let snapshot = self.snapshot(window, cx);
        if snapshot.buffer_snapshot().max_point().row == 0 {
            return;
        }
        let task = cx.background_spawn(async move {
            let new_newlines = snapshot
                .buffer_chars_at(MultiBufferOffset(0))
                .filter_map(|(c, i)| {
                    if c == '\n' {
                        Some(
                            snapshot.buffer_snapshot().anchor_after(i)
                                ..snapshot.buffer_snapshot().anchor_before(i + 1usize),
                        )
                    } else {
                        None
                    }
                })
                .collect::<Vec<_>>();
            let existing_newlines = snapshot
                .folds_in_range(MultiBufferOffset(0)..snapshot.buffer_snapshot().len())
                .filter_map(|fold| {
                    if fold.placeholder.type_tag == Some(type_id) {
                        Some(fold.range.start..fold.range.end)
                    } else {
                        None
                    }
                })
                .collect::<Vec<_>>();

            (new_newlines, existing_newlines)
        });
        self.folding_newlines = cx.spawn(async move |this, cx| {
            let (new_newlines, existing_newlines) = task.await;
            if new_newlines == existing_newlines {
                return;
            }
            let placeholder = FoldPlaceholder {
                render: Arc::new(move |_, _, cx| {
                    div()
                        .bg(cx.theme().status().hint_background)
                        .border_b_1()
                        .size_full()
                        .font(ThemeSettings::get_global(cx).buffer_font.clone())
                        .border_color(cx.theme().status().hint)
                        .child("\\n")
                        .into_any()
                }),
                constrain_width: false,
                merge_adjacent: false,
                type_tag: Some(type_id),
                collapsed_text: None,
            };
            let creases = new_newlines
                .into_iter()
                .map(|range| Crease::simple(range, placeholder.clone()))
                .collect();
            this.update(cx, |this, cx| {
                this.display_map.update(cx, |display_map, cx| {
                    display_map.remove_folds_with_type(existing_newlines, type_id, cx);
                    display_map.fold(creases, cx);
                });
            })
            .ok();
        });
    }

    #[ztracing::instrument(skip_all)]
    fn refresh_outline_symbols_at_cursor(&mut self, cx: &mut Context<Editor>) {
        if !self.lsp_data_enabled() {
            return;
        }
        let cursor = self.selections.newest_anchor().head();
        let multi_buffer_snapshot = self.buffer().read(cx).snapshot(cx);

        if self.uses_lsp_document_symbols(cursor, &multi_buffer_snapshot, cx) {
            self.outline_symbols_at_cursor =
                self.lsp_symbols_at_cursor(cursor, &multi_buffer_snapshot, cx);
            cx.emit(EditorEvent::OutlineSymbolsChanged);
            cx.notify();
        } else {
            let syntax = cx.theme().syntax().clone();
            let background_task = cx.background_spawn(async move {
                multi_buffer_snapshot.symbols_containing(cursor, Some(&syntax))
            });
            self.refresh_outline_symbols_at_cursor_at_cursor_task =
                cx.spawn(async move |this, cx| {
                    let symbols = background_task.await;
                    this.update(cx, |this, cx| {
                        this.outline_symbols_at_cursor = symbols;
                        cx.emit(EditorEvent::OutlineSymbolsChanged);
                        cx.notify();
                    })
                    .ok();
                });
        }
    }

    #[ztracing::instrument(skip_all)]
    fn refresh_selected_text_highlights(
        &mut self,
        snapshot: &DisplaySnapshot,
        on_buffer_edit: bool,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) {
        let Some((query_text, query_range)) =
            self.prepare_highlight_query_from_selection(snapshot, cx)
        else {
            self.clear_background_highlights(HighlightKey::SelectedTextHighlight, cx);
            self.quick_selection_highlight_task.take();
            self.debounced_selection_highlight_task.take();
            self.debounced_selection_highlight_complete = false;
            return;
        };
        let display_snapshot = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let multi_buffer_snapshot = self.buffer().read(cx).snapshot(cx);
        let query_changed = self
            .quick_selection_highlight_task
            .as_ref()
            .is_none_or(|(prev_anchor_range, _)| prev_anchor_range != &query_range);
        if query_changed {
            self.debounced_selection_highlight_complete = false;
        }
        if on_buffer_edit || query_changed {
            self.quick_selection_highlight_task = Some((
                query_range.clone(),
                self.update_selection_occurrence_highlights(
                    snapshot.buffer.clone(),
                    query_text.clone(),
                    query_range.clone(),
                    self.multi_buffer_visible_range(&display_snapshot, cx),
                    false,
                    window,
                    cx,
                ),
            ));
        }
        if on_buffer_edit
            || self
                .debounced_selection_highlight_task
                .as_ref()
                .is_none_or(|(prev_anchor_range, _)| prev_anchor_range != &query_range)
        {
            let multi_buffer_start = multi_buffer_snapshot
                .anchor_before(MultiBufferOffset(0))
                .to_point(&multi_buffer_snapshot);
            let multi_buffer_end = multi_buffer_snapshot
                .anchor_after(multi_buffer_snapshot.len())
                .to_point(&multi_buffer_snapshot);
            let multi_buffer_full_range = multi_buffer_start..multi_buffer_end;
            self.debounced_selection_highlight_task = Some((
                query_range.clone(),
                self.update_selection_occurrence_highlights(
                    snapshot.buffer.clone(),
                    query_text,
                    query_range,
                    multi_buffer_full_range,
                    true,
                    window,
                    cx,
                ),
            ));
        }
    }

    pub fn multi_buffer_visible_range(
        &self,
        display_snapshot: &DisplaySnapshot,
        cx: &App,
    ) -> Range<Point> {
        let visible_start = self
            .scroll_manager
            .native_anchor(display_snapshot, cx)
            .anchor
            .to_point(display_snapshot.buffer_snapshot())
            .to_display_point(display_snapshot);

        let mut target_end = visible_start;
        *target_end.row_mut() += self.visible_line_count().unwrap_or(0.).ceil() as u32;

        visible_start.to_point(display_snapshot)
            ..display_snapshot
                .clip_point(target_end, Bias::Right)
                .to_point(display_snapshot)
    }

    pub fn refresh_edit_prediction(
        &mut self,
        debounce: bool,
        user_requested: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<()> {
        if self.leader_id.is_some() {
            self.discard_edit_prediction(EditPredictionDiscardReason::Ignored, cx);
            return None;
        }

        let cursor = self.selections.newest_anchor().head();
        let (buffer, cursor_buffer_position) =
            self.buffer.read(cx).text_anchor_for_position(cursor, cx)?;

        if DisableAiSettings::is_ai_disabled_for_buffer(Some(&buffer), cx) {
            return None;
        }

        if !self.edit_predictions_enabled_in_buffer(&buffer, cursor_buffer_position, cx) {
            self.discard_edit_prediction(EditPredictionDiscardReason::Ignored, cx);
            return None;
        }

        self.update_visible_edit_prediction(window, cx);

        if !user_requested
            && (!self.should_show_edit_predictions()
                || !self.is_focused(window)
                || buffer.read(cx).is_empty())
        {
            self.discard_edit_prediction(EditPredictionDiscardReason::Ignored, cx);
            return None;
        }

        self.edit_prediction_provider()?
            .refresh(buffer, cursor_buffer_position, debounce, cx);
        Some(())
    }

    fn show_edit_predictions_in_menu(&self) -> bool {
        match self.edit_prediction_settings {
            EditPredictionSettings::Disabled => false,
            EditPredictionSettings::Enabled { show_in_menu, .. } => show_in_menu,
        }
    }

    pub fn edit_predictions_enabled(&self) -> bool {
        match self.edit_prediction_settings {
            EditPredictionSettings::Disabled => false,
            EditPredictionSettings::Enabled { .. } => true,
        }
    }

    fn edit_prediction_requires_modifier(&self) -> bool {
        match self.edit_prediction_settings {
            EditPredictionSettings::Disabled => false,
            EditPredictionSettings::Enabled {
                preview_requires_modifier,
                ..
            } => preview_requires_modifier,
        }
    }

    pub fn update_edit_prediction_settings(&mut self, cx: &mut Context<Self>) {
        if self.edit_prediction_provider.is_none() {
            self.edit_prediction_settings = EditPredictionSettings::Disabled;
            self.discard_edit_prediction(EditPredictionDiscardReason::Ignored, cx);
            return;
        }

        let selection = self.selections.newest_anchor();
        let cursor = selection.head();

        if let Some((buffer, cursor_buffer_position)) =
            self.buffer.read(cx).text_anchor_for_position(cursor, cx)
        {
            if DisableAiSettings::is_ai_disabled_for_buffer(Some(&buffer), cx) {
                self.edit_prediction_settings = EditPredictionSettings::Disabled;
                self.discard_edit_prediction(EditPredictionDiscardReason::Ignored, cx);
                return;
            }
            self.edit_prediction_settings =
                self.edit_prediction_settings_at_position(&buffer, cursor_buffer_position, cx);
        }
    }

    fn edit_prediction_settings_at_position(
        &self,
        buffer: &Entity<Buffer>,
        buffer_position: language::Anchor,
        cx: &App,
    ) -> EditPredictionSettings {
        if !self.mode.is_full()
            || !self.show_edit_predictions_override.unwrap_or(true)
            || self.edit_predictions_disabled_in_scope(buffer, buffer_position, cx)
        {
            return EditPredictionSettings::Disabled;
        }

        if !LanguageSettings::for_buffer(&buffer.read(cx), cx).show_edit_predictions {
            return EditPredictionSettings::Disabled;
        };

        let by_provider = matches!(
            self.menu_edit_predictions_policy,
            MenuEditPredictionsPolicy::ByProvider
        );

        let show_in_menu = by_provider
            && self
                .edit_prediction_provider
                .as_ref()
                .is_some_and(|provider| provider.provider.show_predictions_in_menu());

        let file = buffer.read(cx).file();
        let preview_requires_modifier =
            all_language_settings(file, cx).edit_predictions_mode() == EditPredictionsMode::Subtle;

        EditPredictionSettings::Enabled {
            show_in_menu,
            preview_requires_modifier,
        }
    }

    fn should_show_edit_predictions(&self) -> bool {
        self.snippet_stack.is_empty() && self.edit_predictions_enabled()
    }

    pub fn edit_prediction_preview_is_active(&self) -> bool {
        matches!(
            self.edit_prediction_preview,
            EditPredictionPreview::Active { .. }
        )
    }

    pub fn edit_predictions_enabled_at_cursor(&self, cx: &App) -> bool {
        let cursor = self.selections.newest_anchor().head();
        if let Some((buffer, cursor_position)) =
            self.buffer.read(cx).text_anchor_for_position(cursor, cx)
        {
            self.edit_predictions_enabled_in_buffer(&buffer, cursor_position, cx)
        } else {
            false
        }
    }

    pub fn supports_minimap(&self, cx: &App) -> bool {
        !self.minimap_visibility.disabled() && self.buffer_kind(cx) == ItemBufferKind::Singleton
    }

    fn edit_predictions_enabled_in_buffer(
        &self,
        buffer: &Entity<Buffer>,
        buffer_position: language::Anchor,
        cx: &App,
    ) -> bool {
        maybe!({
            if self.read_only(cx) || self.leader_id.is_some() {
                return Some(false);
            }
            let provider = self.edit_prediction_provider()?;
            if !provider.is_enabled(buffer, buffer_position, cx) {
                return Some(false);
            }
            let buffer = buffer.read(cx);
            let Some(file) = buffer.file() else {
                return Some(true);
            };
            let settings = all_language_settings(Some(file), cx);
            Some(settings.edit_predictions_enabled_for_file(file, cx))
        })
        .unwrap_or(false)
    }

    pub fn show_edit_prediction(
        &mut self,
        _: &ShowEditPrediction,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.has_active_edit_prediction() {
            self.refresh_edit_prediction(false, true, window, cx);
            return;
        }

        self.update_visible_edit_prediction(window, cx);
    }

    pub fn display_cursor_names(
        &mut self,
        _: &DisplayCursorNames,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.show_cursor_names(window, cx);
    }

    fn show_cursor_names(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.show_cursor_names = true;
        cx.notify();
        cx.spawn_in(window, async move |this, cx| {
            cx.background_executor().timer(CURSORS_VISIBLE_FOR).await;
            this.update(cx, |this, cx| {
                this.show_cursor_names = false;
                cx.notify()
            })
            .ok()
        })
        .detach();
    }

    pub fn accept_partial_edit_prediction(
        &mut self,
        granularity: EditPredictionGranularity,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.show_edit_predictions_in_menu() {
            self.hide_context_menu(window, cx);
        }

        let Some(active_edit_prediction) = self.active_edit_prediction.as_ref() else {
            return;
        };

        if !matches!(granularity, EditPredictionGranularity::Full) && self.selections.count() != 1 {
            return;
        }

        match &active_edit_prediction.completion {
            EditPrediction::MoveWithin { target, .. } => {
                let target = *target;

                if matches!(granularity, EditPredictionGranularity::Full) {
                    if let Some(position_map) = &self.last_position_map {
                        let target_row = target.to_display_point(&position_map.snapshot).row();
                        let is_visible = position_map.visible_row_range.contains(&target_row);

                        if is_visible || !self.edit_prediction_requires_modifier() {
                            self.unfold_ranges(&[target..target], true, false, cx);
                            self.change_selections(
                                SelectionEffects::scroll(Autoscroll::newest()),
                                window,
                                cx,
                                |selections| {
                                    selections.select_anchor_ranges([target..target]);
                                },
                            );
                            self.clear_row_highlights::<EditPredictionPreview>();
                            self.edit_prediction_preview
                                .set_previous_scroll_position(None);
                        } else {
                            // Highlight and request scroll
                            self.edit_prediction_preview
                                .set_previous_scroll_position(Some(
                                    position_map.snapshot.scroll_anchor,
                                ));
                            self.highlight_rows::<EditPredictionPreview>(
                                target..target,
                                cx.theme().colors().editor_highlighted_line_background,
                                RowHighlightOptions {
                                    autoscroll: true,
                                    ..Default::default()
                                },
                                cx,
                            );
                            self.request_autoscroll(Autoscroll::fit(), cx);
                        }
                    }
                } else {
                    self.change_selections(
                        SelectionEffects::scroll(Autoscroll::newest()),
                        window,
                        cx,
                        |selections| {
                            selections.select_anchor_ranges([target..target]);
                        },
                    );
                }
            }
            EditPrediction::MoveOutside { snapshot, target } => {
                if let Some(workspace) = self.workspace() {
                    Self::open_editor_at_anchor(snapshot, *target, &workspace, window, cx)
                        .detach_and_log_err(cx);
                }
            }
            EditPrediction::Edit {
                edits,
                cursor_position,
                ..
            } => {
                self.report_edit_prediction_event(
                    active_edit_prediction.completion_id.clone(),
                    true,
                    cx,
                );

                match granularity {
                    EditPredictionGranularity::Full => {
                        let transaction_id_prev = self.buffer.read(cx).last_transaction_id(cx);

                        // Compute fallback cursor position BEFORE applying the edit,
                        // so the anchor tracks through the edit correctly
                        let fallback_cursor_target = {
                            let snapshot = self.buffer.read(cx).snapshot(cx);
                            edits.last().unwrap().0.end.bias_right(&snapshot)
                        };

                        self.buffer.update(cx, |buffer, cx| {
                            buffer.edit(edits.iter().cloned(), None, cx)
                        });

                        if let Some(provider) = self.edit_prediction_provider() {
                            provider.accept(cx);
                        }

                        // Resolve cursor position after the edit is applied
                        let cursor_target = if let Some((anchor, offset)) = cursor_position {
                            // The anchor tracks through the edit, then we add the offset
                            let snapshot = self.buffer.read(cx).snapshot(cx);
                            let base_offset = anchor.to_offset(&snapshot).0;
                            let target_offset =
                                MultiBufferOffset((base_offset + offset).min(snapshot.len().0));
                            snapshot.anchor_after(target_offset)
                        } else {
                            fallback_cursor_target
                        };

                        self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                            s.select_anchor_ranges([cursor_target..cursor_target]);
                        });

                        let selections = self.selections.disjoint_anchors_arc();
                        if let Some(transaction_id_now) =
                            self.buffer.read(cx).last_transaction_id(cx)
                        {
                            if transaction_id_prev != Some(transaction_id_now) {
                                self.selection_history
                                    .insert_transaction(transaction_id_now, selections);
                            }
                        }

                        self.update_visible_edit_prediction(window, cx);
                        if self.active_edit_prediction.is_none() {
                            self.refresh_edit_prediction(true, true, window, cx);
                        }
                        cx.notify();
                    }
                    _ => {
                        let snapshot = self.buffer.read(cx).snapshot(cx);
                        let cursor_offset = self
                            .selections
                            .newest::<MultiBufferOffset>(&self.display_snapshot(cx))
                            .head();

                        let insertion = edits.iter().find_map(|(range, text)| {
                            let range = range.to_offset(&snapshot);
                            if range.is_empty() && range.start == cursor_offset {
                                Some(text)
                            } else {
                                None
                            }
                        });

                        if let Some(text) = insertion {
                            let text_to_insert = match granularity {
                                EditPredictionGranularity::Word => {
                                    let mut partial = text
                                        .chars()
                                        .by_ref()
                                        .take_while(|c| c.is_alphabetic())
                                        .collect::<String>();
                                    if partial.is_empty() {
                                        partial = text
                                            .chars()
                                            .by_ref()
                                            .take_while(|c| c.is_whitespace() || !c.is_alphabetic())
                                            .collect::<String>();
                                    }
                                    partial
                                }
                                EditPredictionGranularity::Line => {
                                    if let Some(line) = text.split_inclusive('\n').next() {
                                        line.to_string()
                                    } else {
                                        text.to_string()
                                    }
                                }
                                EditPredictionGranularity::Full => unreachable!(),
                            };

                            cx.emit(EditorEvent::InputHandled {
                                utf16_range_to_replace: None,
                                text: text_to_insert.clone().into(),
                            });

                            self.replace_selections(&text_to_insert, None, window, cx, false);
                            self.refresh_edit_prediction(true, true, window, cx);
                            cx.notify();
                        } else {
                            self.accept_partial_edit_prediction(
                                EditPredictionGranularity::Full,
                                window,
                                cx,
                            );
                        }
                    }
                }
            }
        }
    }

    pub fn accept_next_word_edit_prediction(
        &mut self,
        _: &AcceptNextWordEditPrediction,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.accept_partial_edit_prediction(EditPredictionGranularity::Word, window, cx);
    }

    pub fn accept_next_line_edit_prediction(
        &mut self,
        _: &AcceptNextLineEditPrediction,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.accept_partial_edit_prediction(EditPredictionGranularity::Line, window, cx);
    }

    pub fn accept_edit_prediction(
        &mut self,
        _: &AcceptEditPrediction,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.accept_partial_edit_prediction(EditPredictionGranularity::Full, window, cx);
    }

    fn discard_edit_prediction(
        &mut self,
        reason: EditPredictionDiscardReason,
        cx: &mut Context<Self>,
    ) -> bool {
        if reason == EditPredictionDiscardReason::Rejected {
            let completion_id = self
                .active_edit_prediction
                .as_ref()
                .and_then(|active_completion| active_completion.completion_id.clone());

            self.report_edit_prediction_event(completion_id, false, cx);
        }

        if let Some(provider) = self.edit_prediction_provider() {
            provider.discard(reason, cx);
        }

        self.take_active_edit_prediction(reason == EditPredictionDiscardReason::Ignored, cx)
    }

    fn report_edit_prediction_event(&self, id: Option<SharedString>, accepted: bool, cx: &App) {
        let Some(provider) = self.edit_prediction_provider() else {
            return;
        };

        let buffer_snapshot = self.buffer.read(cx).snapshot(cx);
        let Some((position, _)) =
            buffer_snapshot.anchor_to_buffer_anchor(self.selections.newest_anchor().head())
        else {
            return;
        };
        let Some(buffer) = self.buffer.read(cx).buffer(position.buffer_id) else {
            return;
        };

        let extension = buffer
            .read(cx)
            .file()
            .and_then(|file| Some(file.path().extension()?.to_string()));

        let event_type = match accepted {
            true => "Edit Prediction Accepted",
            false => "Edit Prediction Discarded",
        };
        telemetry::event!(
            event_type,
            provider = provider.name(),
            prediction_id = id,
            suggestion_accepted = accepted,
            file_extension = extension,
        );
    }

    fn open_editor_at_anchor(
        snapshot: &language::BufferSnapshot,
        target: language::Anchor,
        workspace: &Entity<Workspace>,
        window: &mut Window,
        cx: &mut App,
    ) -> Task<Result<()>> {
        workspace.update(cx, |workspace, cx| {
            let path = snapshot.file().map(|file| file.full_path(cx));
            let Some(path) =
                path.and_then(|path| workspace.project().read(cx).find_project_path(path, cx))
            else {
                return Task::ready(Err(anyhow::anyhow!("Project path not found")));
            };
            let target = text::ToPoint::to_point(&target, snapshot);
            let item = workspace.open_path(path, None, true, window, cx);
            window.spawn(cx, async move |cx| {
                let Some(editor) = item.await?.downcast::<Editor>() else {
                    return Ok(());
                };
                editor
                    .update_in(cx, |editor, window, cx| {
                        editor.go_to_singleton_buffer_point(target, window, cx);
                    })
                    .ok();
                anyhow::Ok(())
            })
        })
    }

    pub fn has_active_edit_prediction(&self) -> bool {
        self.active_edit_prediction.is_some()
    }

    fn take_active_edit_prediction(
        &mut self,
        preserve_stale_in_menu: bool,
        cx: &mut Context<Self>,
    ) -> bool {
        let Some(active_edit_prediction) = self.active_edit_prediction.take() else {
            if !preserve_stale_in_menu {
                self.stale_edit_prediction_in_menu = None;
            }
            return false;
        };

        self.splice_inlays(&active_edit_prediction.inlay_ids, Default::default(), cx);
        self.clear_highlights(HighlightKey::EditPredictionHighlight, cx);
        self.stale_edit_prediction_in_menu =
            preserve_stale_in_menu.then_some(active_edit_prediction);
        true
    }

    /// Returns true when we're displaying the edit prediction popover below the cursor
    /// like we are not previewing and the LSP autocomplete menu is visible
    /// or we are in `when_holding_modifier` mode.
    pub fn edit_prediction_visible_in_cursor_popover(&self, has_completion: bool) -> bool {
        if self.edit_prediction_preview_is_active()
            || !self.show_edit_predictions_in_menu()
            || !self.edit_predictions_enabled()
        {
            return false;
        }

        if self.has_visible_completions_menu() {
            return true;
        }

        has_completion && self.edit_prediction_requires_modifier()
    }

    fn handle_modifiers_changed(
        &mut self,
        modifiers: Modifiers,
        position_map: &PositionMap,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.update_edit_prediction_settings(cx);

        // Ensure that the edit prediction preview is updated, even when not
        // enabled, if there's an active edit prediction preview.
        if self.show_edit_predictions_in_menu()
            || self.edit_prediction_requires_modifier()
            || matches!(
                self.edit_prediction_preview,
                EditPredictionPreview::Active { .. }
            )
        {
            self.update_edit_prediction_preview(&modifiers, window, cx);
        }

        self.update_selection_mode(&modifiers, position_map, window, cx);

        let mouse_position = window.mouse_position();
        if !position_map.text_hitbox.is_hovered(window) {
            return;
        }

        self.update_hovered_link(
            position_map.point_for_position(mouse_position),
            Some(mouse_position),
            &position_map.snapshot,
            modifiers,
            window,
            cx,
        )
    }

    fn is_cmd_or_ctrl_pressed(modifiers: &Modifiers, cx: &mut Context<Self>) -> bool {
        match EditorSettings::get_global(cx).multi_cursor_modifier {
            MultiCursorModifier::Alt => modifiers.secondary(),
            MultiCursorModifier::CmdOrCtrl => modifiers.alt,
        }
    }

    fn is_alt_pressed(modifiers: &Modifiers, cx: &mut Context<Self>) -> bool {
        match EditorSettings::get_global(cx).multi_cursor_modifier {
            MultiCursorModifier::Alt => modifiers.alt,
            MultiCursorModifier::CmdOrCtrl => modifiers.secondary(),
        }
    }

    fn columnar_selection_mode(
        modifiers: &Modifiers,
        cx: &mut Context<Self>,
    ) -> Option<ColumnarMode> {
        if modifiers.shift && modifiers.number_of_modifiers() == 2 {
            if Self::is_cmd_or_ctrl_pressed(modifiers, cx) {
                Some(ColumnarMode::FromMouse)
            } else if Self::is_alt_pressed(modifiers, cx) {
                Some(ColumnarMode::FromSelection)
            } else {
                None
            }
        } else {
            None
        }
    }

    fn update_selection_mode(
        &mut self,
        modifiers: &Modifiers,
        position_map: &PositionMap,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(mode) = Self::columnar_selection_mode(modifiers, cx) else {
            return;
        };
        if self.selections.pending_anchor().is_none() {
            return;
        }

        let mouse_position = window.mouse_position();
        let point_for_position = position_map.point_for_position(mouse_position);
        let position = point_for_position.previous_valid;

        self.select(
            SelectPhase::BeginColumnar {
                position,
                reset: false,
                mode,
                goal_column: point_for_position.exact_unclipped.column(),
            },
            window,
            cx,
        );
    }

    fn update_edit_prediction_preview(
        &mut self,
        modifiers: &Modifiers,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let modifiers_held = self.edit_prediction_preview_modifiers_held(modifiers, window, cx);

        if modifiers_held {
            if matches!(
                self.edit_prediction_preview,
                EditPredictionPreview::Inactive { .. }
            ) {
                self.edit_prediction_preview = EditPredictionPreview::Active {
                    previous_scroll_position: None,
                    since: Instant::now(),
                };

                self.update_visible_edit_prediction(window, cx);
                cx.notify();
            }
        } else if let EditPredictionPreview::Active {
            previous_scroll_position,
            since,
        } = self.edit_prediction_preview
        {
            if let (Some(previous_scroll_position), Some(position_map)) =
                (previous_scroll_position, self.last_position_map.as_ref())
            {
                self.set_scroll_position(
                    previous_scroll_position
                        .scroll_position(&position_map.snapshot.display_snapshot),
                    window,
                    cx,
                );
            }

            self.edit_prediction_preview = EditPredictionPreview::Inactive {
                released_too_fast: since.elapsed() < Duration::from_millis(200),
            };
            self.clear_row_highlights::<EditPredictionPreview>();
            self.update_visible_edit_prediction(window, cx);
            cx.notify();
        }
    }

    fn update_visible_edit_prediction(
        &mut self,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<()> {
        if self.ime_transaction.is_some() {
            self.discard_edit_prediction(EditPredictionDiscardReason::Ignored, cx);
            return None;
        }

        let selection = self.selections.newest_anchor();
        let multibuffer = self.buffer.read(cx).snapshot(cx);
        let cursor = selection.head();
        let (cursor_text_anchor, _) = multibuffer.anchor_to_buffer_anchor(cursor)?;
        let buffer = self.buffer.read(cx).buffer(cursor_text_anchor.buffer_id)?;

        // Check project-level disable_ai setting for the current buffer
        if DisableAiSettings::is_ai_disabled_for_buffer(Some(&buffer), cx) {
            return None;
        }
        let offset_selection = selection.map(|endpoint| endpoint.to_offset(&multibuffer));

        let show_in_menu = self.show_edit_predictions_in_menu();
        let completions_menu_has_precedence = !show_in_menu
            && (self.context_menu.borrow().is_some()
                || (!self.completion_tasks.is_empty() && !self.has_active_edit_prediction()));

        if completions_menu_has_precedence
            || !offset_selection.is_empty()
            || self
                .active_edit_prediction
                .as_ref()
                .is_some_and(|completion| {
                    let Some(invalidation_range) = completion.invalidation_range.as_ref() else {
                        return false;
                    };
                    let invalidation_range = invalidation_range.to_offset(&multibuffer);
                    let invalidation_range = invalidation_range.start..=invalidation_range.end;
                    !invalidation_range.contains(&offset_selection.head())
                })
        {
            self.discard_edit_prediction(EditPredictionDiscardReason::Ignored, cx);
            return None;
        }

        self.take_active_edit_prediction(true, cx);
        let Some(provider) = self.edit_prediction_provider() else {
            self.edit_prediction_settings = EditPredictionSettings::Disabled;
            return None;
        };

        self.edit_prediction_settings =
            self.edit_prediction_settings_at_position(&buffer, cursor_text_anchor, cx);

        self.in_leading_whitespace = multibuffer.is_line_whitespace_upto(cursor);

        if self.in_leading_whitespace {
            let cursor_point = cursor.to_point(&multibuffer);
            let mut suggested_indent = None;
            multibuffer.suggested_indents_callback(
                cursor_point.row..cursor_point.row + 1,
                &mut |_, indent| {
                    suggested_indent = Some(indent);
                    ControlFlow::Break(())
                },
                cx,
            );

            if let Some(indent) = suggested_indent
                && indent.len == cursor_point.column
            {
                self.in_leading_whitespace = false;
            }
        }

        let edit_prediction = provider.suggest(&buffer, cursor_text_anchor, cx)?;

        let (completion_id, edits, predicted_cursor_position, edit_preview) = match edit_prediction
        {
            edit_prediction_types::EditPrediction::Local {
                id,
                edits,
                cursor_position,
                edit_preview,
            } => (id, edits, cursor_position, edit_preview),
            edit_prediction_types::EditPrediction::Jump {
                id,
                snapshot,
                target,
            } => {
                if let Some(provider) = &self.edit_prediction_provider {
                    provider.provider.did_show(SuggestionDisplayType::Jump, cx);
                }
                self.stale_edit_prediction_in_menu = None;
                self.active_edit_prediction = Some(EditPredictionState {
                    inlay_ids: vec![],
                    completion: EditPrediction::MoveOutside { snapshot, target },
                    completion_id: id,
                    invalidation_range: None,
                });
                cx.notify();
                return Some(());
            }
        };

        let edits = edits
            .into_iter()
            .flat_map(|(range, new_text)| {
                Some((
                    multibuffer.buffer_anchor_range_to_anchor_range(range)?,
                    new_text,
                ))
            })
            .collect::<Vec<_>>();
        if edits.is_empty() {
            return None;
        }

        let cursor_position = predicted_cursor_position.and_then(|predicted| {
            let anchor = multibuffer.anchor_in_excerpt(predicted.anchor)?;
            Some((anchor, predicted.offset))
        });

        let first_edit_start = edits.first().unwrap().0.start;
        let first_edit_start_point = first_edit_start.to_point(&multibuffer);
        let edit_start_row = first_edit_start_point.row.saturating_sub(2);

        let last_edit_end = edits.last().unwrap().0.end;
        let last_edit_end_point = last_edit_end.to_point(&multibuffer);
        let edit_end_row = cmp::min(multibuffer.max_point().row, last_edit_end_point.row + 2);

        let cursor_row = cursor.to_point(&multibuffer).row;

        let snapshot = multibuffer
            .buffer_for_id(cursor_text_anchor.buffer_id)
            .cloned()?;

        let mut inlay_ids = Vec::new();
        let invalidation_row_range;
        let move_invalidation_row_range = if cursor_row < edit_start_row {
            Some(cursor_row..edit_end_row)
        } else if cursor_row > edit_end_row {
            Some(edit_start_row..cursor_row)
        } else {
            None
        };
        let supports_jump = self
            .edit_prediction_provider
            .as_ref()
            .map(|provider| provider.provider.supports_jump_to_edit())
            .unwrap_or(true);

        let is_move = supports_jump
            && (move_invalidation_row_range.is_some() || self.edit_predictions_hidden_for_vim_mode);
        let completion = if is_move {
            if let Some(provider) = &self.edit_prediction_provider {
                provider.provider.did_show(SuggestionDisplayType::Jump, cx);
            }
            invalidation_row_range =
                move_invalidation_row_range.unwrap_or(edit_start_row..edit_end_row);
            let target = first_edit_start;
            EditPrediction::MoveWithin { target, snapshot }
        } else {
            let show_completions_in_menu = self.has_visible_completions_menu();
            let show_completions_in_buffer = !self.edit_prediction_visible_in_cursor_popover(true)
                && !self.edit_predictions_hidden_for_vim_mode;

            let display_mode = if all_edits_insertions_or_deletions(&edits, &multibuffer) {
                if provider.show_tab_accept_marker() {
                    EditDisplayMode::TabAccept
                } else {
                    EditDisplayMode::Inline
                }
            } else {
                EditDisplayMode::DiffPopover
            };

            let report_shown = match display_mode {
                EditDisplayMode::DiffPopover | EditDisplayMode::Inline => {
                    show_completions_in_buffer || show_completions_in_menu
                }
                EditDisplayMode::TabAccept => {
                    show_completions_in_menu || self.edit_prediction_preview_is_active()
                }
            };

            if report_shown && let Some(provider) = &self.edit_prediction_provider {
                let suggestion_display_type = match display_mode {
                    EditDisplayMode::DiffPopover => SuggestionDisplayType::DiffPopover,
                    EditDisplayMode::Inline | EditDisplayMode::TabAccept => {
                        SuggestionDisplayType::GhostText
                    }
                };
                provider.provider.did_show(suggestion_display_type, cx);
            }

            if show_completions_in_buffer {
                if edits
                    .iter()
                    .all(|(range, _)| range.to_offset(&multibuffer).is_empty())
                {
                    let mut inlays = Vec::new();
                    for (range, new_text) in &edits {
                        let inlay = Inlay::edit_prediction(
                            post_inc(&mut self.next_inlay_id),
                            range.start,
                            new_text.as_ref(),
                        );
                        inlay_ids.push(inlay.id);
                        inlays.push(inlay);
                    }

                    self.splice_inlays(&[], inlays, cx);
                } else {
                    let background_color = cx.theme().status().deleted_background;
                    self.highlight_text(
                        HighlightKey::EditPredictionHighlight,
                        edits.iter().map(|(range, _)| range.clone()).collect(),
                        HighlightStyle {
                            background_color: Some(background_color),
                            ..Default::default()
                        },
                        cx,
                    );
                }
            }

            invalidation_row_range = edit_start_row..edit_end_row;

            EditPrediction::Edit {
                edits,
                cursor_position,
                edit_preview,
                display_mode,
                snapshot,
            }
        };

        let invalidation_range = multibuffer
            .anchor_before(Point::new(invalidation_row_range.start, 0))
            ..multibuffer.anchor_after(Point::new(
                invalidation_row_range.end,
                multibuffer.line_len(MultiBufferRow(invalidation_row_range.end)),
            ));

        self.stale_edit_prediction_in_menu = None;
        self.active_edit_prediction = Some(EditPredictionState {
            inlay_ids,
            completion,
            completion_id,
            invalidation_range: Some(invalidation_range),
        });

        cx.notify();

        Some(())
    }

    pub fn edit_prediction_provider(&self) -> Option<Arc<dyn EditPredictionDelegateHandle>> {
        Some(self.edit_prediction_provider.as_ref()?.provider.clone())
    }

    /// Get all display points of breakpoints that will be rendered within editor
    ///
    /// This function is used to handle overlaps between breakpoints and Code action/runner symbol.
    /// It's also used to set the color of line numbers with breakpoints to the breakpoint color.
    /// TODO debugger: Use this function to color toggle symbols that house nested breakpoints
    fn active_breakpoints(
        &self,
        range: Range<DisplayRow>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> HashMap<DisplayRow, (Anchor, Breakpoint, Option<BreakpointSessionState>)> {
        let mut breakpoint_display_points = HashMap::default();

        let Some(breakpoint_store) = self.breakpoint_store.clone() else {
            return breakpoint_display_points;
        };

        let snapshot = self.snapshot(window, cx);

        let multi_buffer_snapshot = snapshot.buffer_snapshot();

        let range = snapshot.display_point_to_point(DisplayPoint::new(range.start, 0), Bias::Left)
            ..snapshot.display_point_to_point(DisplayPoint::new(range.end, 0), Bias::Right);

        for (buffer_snapshot, range, _) in
            multi_buffer_snapshot.range_to_buffer_ranges(range.start..range.end)
        {
            let Some(buffer) = self.buffer().read(cx).buffer(buffer_snapshot.remote_id()) else {
                continue;
            };
            let breakpoints = breakpoint_store.read(cx).breakpoints(
                &buffer,
                Some(
                    buffer_snapshot.anchor_before(range.start)
                        ..buffer_snapshot.anchor_after(range.end),
                ),
                &buffer_snapshot,
                cx,
            );
            for (breakpoint, state) in breakpoints {
                let Some(multi_buffer_anchor) =
                    multi_buffer_snapshot.anchor_in_excerpt(breakpoint.position)
                else {
                    continue;
                };
                let position = multi_buffer_anchor
                    .to_point(&multi_buffer_snapshot)
                    .to_display_point(&snapshot);

                breakpoint_display_points.insert(
                    position.row(),
                    (multi_buffer_anchor, breakpoint.bp.clone(), state),
                );
            }
        }

        breakpoint_display_points
    }

    fn breakpoint_context_menu(
        &self,
        anchor: Anchor,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Entity<ui::ContextMenu> {
        let weak_editor = cx.weak_entity();
        let focus_handle = self.focus_handle(cx);

        let row = self
            .buffer
            .read(cx)
            .snapshot(cx)
            .summary_for_anchor::<Point>(&anchor)
            .row;

        let breakpoint = self
            .breakpoint_at_row(row, window, cx)
            .map(|(anchor, bp)| (anchor, Arc::from(bp)));

        let log_breakpoint_msg = if breakpoint.as_ref().is_some_and(|bp| bp.1.message.is_some()) {
            "Edit Log Breakpoint"
        } else {
            "Set Log Breakpoint"
        };

        let condition_breakpoint_msg = if breakpoint
            .as_ref()
            .is_some_and(|bp| bp.1.condition.is_some())
        {
            "Edit Condition Breakpoint"
        } else {
            "Set Condition Breakpoint"
        };

        let hit_condition_breakpoint_msg = if breakpoint
            .as_ref()
            .is_some_and(|bp| bp.1.hit_condition.is_some())
        {
            "Edit Hit Condition Breakpoint"
        } else {
            "Set Hit Condition Breakpoint"
        };

        let set_breakpoint_msg = if breakpoint.as_ref().is_some() {
            "Unset Breakpoint"
        } else {
            "Set Breakpoint"
        };

        let run_to_cursor = window.is_action_available(&RunToCursor, cx);

        let toggle_state_msg = breakpoint.as_ref().map_or(None, |bp| match bp.1.state {
            BreakpointState::Enabled => Some("Disable"),
            BreakpointState::Disabled => Some("Enable"),
        });

        let (anchor, breakpoint) =
            breakpoint.unwrap_or_else(|| (anchor, Arc::new(Breakpoint::new_standard())));

        ui::ContextMenu::build(window, cx, |menu, _, _cx| {
            menu.on_blur_subscription(Subscription::new(|| {}))
                .context(focus_handle)
                .when(run_to_cursor, |this| {
                    let weak_editor = weak_editor.clone();
                    this.entry("Run to Cursor", None, move |window, cx| {
                        weak_editor
                            .update(cx, |editor, cx| {
                                editor.change_selections(
                                    SelectionEffects::no_scroll(),
                                    window,
                                    cx,
                                    |s| s.select_ranges([Point::new(row, 0)..Point::new(row, 0)]),
                                );
                            })
                            .ok();

                        window.dispatch_action(Box::new(RunToCursor), cx);
                    })
                    .separator()
                })
                .when_some(toggle_state_msg, |this, msg| {
                    this.entry(msg, None, {
                        let weak_editor = weak_editor.clone();
                        let breakpoint = breakpoint.clone();
                        move |_window, cx| {
                            weak_editor
                                .update(cx, |this, cx| {
                                    this.edit_breakpoint_at_anchor(
                                        anchor,
                                        breakpoint.as_ref().clone(),
                                        BreakpointEditAction::InvertState,
                                        cx,
                                    );
                                })
                                .log_err();
                        }
                    })
                })
                .entry(set_breakpoint_msg, None, {
                    let weak_editor = weak_editor.clone();
                    let breakpoint = breakpoint.clone();
                    move |_window, cx| {
                        weak_editor
                            .update(cx, |this, cx| {
                                this.edit_breakpoint_at_anchor(
                                    anchor,
                                    breakpoint.as_ref().clone(),
                                    BreakpointEditAction::Toggle,
                                    cx,
                                );
                            })
                            .log_err();
                    }
                })
                .entry(log_breakpoint_msg, None, {
                    let breakpoint = breakpoint.clone();
                    let weak_editor = weak_editor.clone();
                    move |window, cx| {
                        weak_editor
                            .update(cx, |this, cx| {
                                this.add_edit_breakpoint_block(
                                    anchor,
                                    breakpoint.as_ref(),
                                    BreakpointPromptEditAction::Log,
                                    window,
                                    cx,
                                );
                            })
                            .log_err();
                    }
                })
                .entry(condition_breakpoint_msg, None, {
                    let breakpoint = breakpoint.clone();
                    let weak_editor = weak_editor.clone();
                    move |window, cx| {
                        weak_editor
                            .update(cx, |this, cx| {
                                this.add_edit_breakpoint_block(
                                    anchor,
                                    breakpoint.as_ref(),
                                    BreakpointPromptEditAction::Condition,
                                    window,
                                    cx,
                                );
                            })
                            .log_err();
                    }
                })
                .entry(hit_condition_breakpoint_msg, None, move |window, cx| {
                    weak_editor
                        .update(cx, |this, cx| {
                            this.add_edit_breakpoint_block(
                                anchor,
                                breakpoint.as_ref(),
                                BreakpointPromptEditAction::HitCondition,
                                window,
                                cx,
                            );
                        })
                        .log_err();
                })
        })
    }

    fn render_breakpoint(
        &self,
        position: Anchor,
        row: DisplayRow,
        breakpoint: &Breakpoint,
        state: Option<BreakpointSessionState>,
        cx: &mut Context<Self>,
    ) -> IconButton {
        let is_rejected = state.is_some_and(|s| !s.verified);
        // Is it a breakpoint that shows up when hovering over gutter?
        let (is_phantom, collides_with_existing) = self.gutter_breakpoint_indicator.0.map_or(
            (false, false),
            |PhantomBreakpointIndicator {
                 is_active,
                 display_row,
                 collides_with_existing_breakpoint,
             }| {
                (
                    is_active && display_row == row,
                    collides_with_existing_breakpoint,
                )
            },
        );

        let (color, icon) = {
            let icon = match (&breakpoint.message.is_some(), breakpoint.is_disabled()) {
                (false, false) => ui::IconName::DebugBreakpoint,
                (true, false) => ui::IconName::DebugLogBreakpoint,
                (false, true) => ui::IconName::DebugDisabledBreakpoint,
                (true, true) => ui::IconName::DebugDisabledLogBreakpoint,
            };

            let theme_colors = cx.theme().colors();

            let color = if is_phantom {
                if collides_with_existing {
                    Color::Custom(
                        theme_colors
                            .debugger_accent
                            .blend(theme_colors.text.opacity(0.6)),
                    )
                } else {
                    Color::Hint
                }
            } else if is_rejected {
                Color::Disabled
            } else {
                Color::Debugger
            };

            (color, icon)
        };

        let breakpoint = Arc::from(breakpoint.clone());

        let alt_as_text = gpui::Keystroke {
            modifiers: Modifiers::secondary_key(),
            ..Default::default()
        };
        let primary_action_text = if breakpoint.is_disabled() {
            "Enable breakpoint"
        } else if is_phantom && !collides_with_existing {
            "Set breakpoint"
        } else {
            "Unset breakpoint"
        };
        let focus_handle = self.focus_handle.clone();

        let meta = if is_rejected {
            SharedString::from("No executable code is associated with this line.")
        } else if collides_with_existing && !breakpoint.is_disabled() {
            SharedString::from(format!(
                "{alt_as_text}-click to disable,\nright-click for more options."
            ))
        } else {
            SharedString::from("Right-click for more options.")
        };
        IconButton::new(("breakpoint_indicator", row.0 as usize), icon)
            .icon_size(IconSize::XSmall)
            .size(ui::ButtonSize::None)
            .when(is_rejected, |this| {
                this.indicator(Indicator::icon(Icon::new(IconName::Warning)).color(Color::Warning))
            })
            .icon_color(color)
            .style(ButtonStyle::Transparent)
            .on_click(cx.listener({
                move |editor, event: &ClickEvent, window, cx| {
                    let edit_action = if event.modifiers().platform || breakpoint.is_disabled() {
                        BreakpointEditAction::InvertState
                    } else {
                        BreakpointEditAction::Toggle
                    };

                    window.focus(&editor.focus_handle(cx), cx);
                    editor.update_breakpoint_collision_on_toggle(row, &edit_action);
                    editor.edit_breakpoint_at_anchor(
                        position,
                        breakpoint.as_ref().clone(),
                        edit_action,
                        cx,
                    );
                }
            }))
            .on_right_click(cx.listener(move |editor, event: &ClickEvent, window, cx| {
                editor.set_breakpoint_context_menu(
                    row,
                    Some(position),
                    event.position(),
                    window,
                    cx,
                );
            }))
            .tooltip(move |_window, cx| {
                Tooltip::with_meta_in(
                    primary_action_text,
                    Some(&ToggleBreakpoint),
                    meta.clone(),
                    &focus_handle,
                    cx,
                )
            })
    }

    fn build_tasks_context(
        project: &Entity<Project>,
        buffer: &Entity<Buffer>,
        buffer_row: u32,
        tasks: &Arc<RunnableTasks>,
        cx: &mut Context<Self>,
    ) -> Task<Option<task::TaskContext>> {
        let position = Point::new(buffer_row, tasks.column);
        let range_start = buffer.read(cx).anchor_at(position, Bias::Right);
        let location = Location {
            buffer: buffer.clone(),
            range: range_start..range_start,
        };
        // Fill in the environmental variables from the tree-sitter captures
        let mut captured_task_variables = TaskVariables::default();
        for (capture_name, value) in tasks.extra_variables.clone() {
            captured_task_variables.insert(
                task::VariableName::Custom(capture_name.into()),
                value.clone(),
            );
        }
        project.update(cx, |project, cx| {
            project.task_store().update(cx, |task_store, cx| {
                task_store.task_context_for_location(captured_task_variables, location, cx)
            })
        })
    }

    pub fn context_menu_visible(&self) -> bool {
        !self.edit_prediction_preview_is_active()
            && self
                .context_menu
                .borrow()
                .as_ref()
                .is_some_and(|menu| menu.visible())
    }

    pub fn context_menu_origin(&self) -> Option<ContextMenuOrigin> {
        self.context_menu
            .borrow()
            .as_ref()
            .map(|menu| menu.origin())
    }

    pub fn set_context_menu_options(&mut self, options: ContextMenuOptions) {
        self.context_menu_options = Some(options);
    }

    const EDIT_PREDICTION_POPOVER_PADDING_X: Pixels = px(24.);
    const EDIT_PREDICTION_POPOVER_PADDING_Y: Pixels = px(2.);

    fn render_edit_prediction_popover(
        &mut self,
        text_bounds: &Bounds<Pixels>,
        content_origin: gpui::Point<Pixels>,
        right_margin: Pixels,
        editor_snapshot: &EditorSnapshot,
        visible_row_range: Range<DisplayRow>,
        scroll_top: ScrollOffset,
        scroll_bottom: ScrollOffset,
        line_layouts: &[LineWithInvisibles],
        line_height: Pixels,
        scroll_position: gpui::Point<ScrollOffset>,
        scroll_pixel_position: gpui::Point<ScrollPixelOffset>,
        newest_selection_head: Option<DisplayPoint>,
        editor_width: Pixels,
        style: &EditorStyle,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<(AnyElement, gpui::Point<Pixels>)> {
        if self.mode().is_minimap() {
            return None;
        }
        let active_edit_prediction = self.active_edit_prediction.as_ref()?;

        if self.edit_prediction_visible_in_cursor_popover(true) {
            return None;
        }

        match &active_edit_prediction.completion {
            EditPrediction::MoveWithin { target, .. } => {
                let target_display_point = target.to_display_point(editor_snapshot);

                if self.edit_prediction_requires_modifier() {
                    if !self.edit_prediction_preview_is_active() {
                        return None;
                    }

                    self.render_edit_prediction_modifier_jump_popover(
                        text_bounds,
                        content_origin,
                        visible_row_range,
                        line_layouts,
                        line_height,
                        scroll_pixel_position,
                        newest_selection_head,
                        target_display_point,
                        window,
                        cx,
                    )
                } else {
                    self.render_edit_prediction_eager_jump_popover(
                        text_bounds,
                        content_origin,
                        editor_snapshot,
                        visible_row_range,
                        scroll_top,
                        scroll_bottom,
                        line_height,
                        scroll_pixel_position,
                        target_display_point,
                        editor_width,
                        window,
                        cx,
                    )
                }
            }
            EditPrediction::Edit {
                display_mode: EditDisplayMode::Inline,
                ..
            } => None,
            EditPrediction::Edit {
                display_mode: EditDisplayMode::TabAccept,
                edits,
                ..
            } => {
                let range = &edits.first()?.0;
                let target_display_point = range.end.to_display_point(editor_snapshot);

                self.render_edit_prediction_end_of_line_popover(
                    "Accept",
                    editor_snapshot,
                    visible_row_range,
                    target_display_point,
                    line_height,
                    scroll_pixel_position,
                    content_origin,
                    editor_width,
                    window,
                    cx,
                )
            }
            EditPrediction::Edit {
                edits,
                edit_preview,
                display_mode: EditDisplayMode::DiffPopover,
                snapshot,
                ..
            } => self.render_edit_prediction_diff_popover(
                text_bounds,
                content_origin,
                right_margin,
                editor_snapshot,
                visible_row_range,
                line_layouts,
                line_height,
                scroll_position,
                scroll_pixel_position,
                newest_selection_head,
                editor_width,
                style,
                edits,
                edit_preview,
                snapshot,
                window,
                cx,
            ),
            EditPrediction::MoveOutside { snapshot, .. } => {
                let mut element = self
                    .render_edit_prediction_jump_outside_popover(snapshot, window, cx)
                    .into_any();

                let size = element.layout_as_root(AvailableSpace::min_size(), window, cx);
                let origin_x = text_bounds.size.width - size.width - px(30.);
                let origin = text_bounds.origin + gpui::Point::new(origin_x, px(16.));
                element.prepaint_at(origin, window, cx);

                Some((element, origin))
            }
        }
    }

    fn render_edit_prediction_modifier_jump_popover(
        &mut self,
        text_bounds: &Bounds<Pixels>,
        content_origin: gpui::Point<Pixels>,
        visible_row_range: Range<DisplayRow>,
        line_layouts: &[LineWithInvisibles],
        line_height: Pixels,
        scroll_pixel_position: gpui::Point<ScrollPixelOffset>,
        newest_selection_head: Option<DisplayPoint>,
        target_display_point: DisplayPoint,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<(AnyElement, gpui::Point<Pixels>)> {
        let scrolled_content_origin =
            content_origin - gpui::Point::new(scroll_pixel_position.x.into(), Pixels::ZERO);

        const SCROLL_PADDING_Y: Pixels = px(12.);

        if target_display_point.row() < visible_row_range.start {
            return self.render_edit_prediction_scroll_popover(
                &|_| SCROLL_PADDING_Y,
                IconName::ArrowUp,
                visible_row_range,
                line_layouts,
                newest_selection_head,
                scrolled_content_origin,
                window,
                cx,
            );
        } else if target_display_point.row() >= visible_row_range.end {
            return self.render_edit_prediction_scroll_popover(
                &|size| text_bounds.size.height - size.height - SCROLL_PADDING_Y,
                IconName::ArrowDown,
                visible_row_range,
                line_layouts,
                newest_selection_head,
                scrolled_content_origin,
                window,
                cx,
            );
        }

        const POLE_WIDTH: Pixels = px(2.);

        let line_layout =
            line_layouts.get(target_display_point.row().minus(visible_row_range.start) as usize)?;
        let target_column = target_display_point.column() as usize;

        let target_x = line_layout.x_for_index(target_column);
        let target_y = (target_display_point.row().as_f64() * f64::from(line_height))
            - scroll_pixel_position.y;

        let flag_on_right = target_x < text_bounds.size.width / 2.;

        let mut border_color = Self::edit_prediction_callout_popover_border_color(cx);
        border_color.l += 0.001;

        let mut element = v_flex()
            .items_end()
            .when(flag_on_right, |el| el.items_start())
            .child(if flag_on_right {
                self.render_edit_prediction_line_popover("Jump", None, window, cx)
                    .rounded_bl(px(0.))
                    .rounded_tl(px(0.))
                    .border_l_2()
                    .border_color(border_color)
            } else {
                self.render_edit_prediction_line_popover("Jump", None, window, cx)
                    .rounded_br(px(0.))
                    .rounded_tr(px(0.))
                    .border_r_2()
                    .border_color(border_color)
            })
            .child(div().w(POLE_WIDTH).bg(border_color).h(line_height))
            .into_any();

        let size = element.layout_as_root(AvailableSpace::min_size(), window, cx);

        let mut origin = scrolled_content_origin + point(target_x, target_y.into())
            - point(
                if flag_on_right {
                    POLE_WIDTH
                } else {
                    size.width - POLE_WIDTH
                },
                size.height - line_height,
            );

        origin.x = origin.x.max(content_origin.x);

        element.prepaint_at(origin, window, cx);

        Some((element, origin))
    }

    fn render_edit_prediction_scroll_popover(
        &mut self,
        to_y: &dyn Fn(Size<Pixels>) -> Pixels,
        scroll_icon: IconName,
        visible_row_range: Range<DisplayRow>,
        line_layouts: &[LineWithInvisibles],
        newest_selection_head: Option<DisplayPoint>,
        scrolled_content_origin: gpui::Point<Pixels>,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<(AnyElement, gpui::Point<Pixels>)> {
        let mut element = self
            .render_edit_prediction_line_popover("Scroll", Some(scroll_icon), window, cx)
            .into_any();

        let size = element.layout_as_root(AvailableSpace::min_size(), window, cx);

        let cursor = newest_selection_head?;
        let cursor_row_layout =
            line_layouts.get(cursor.row().minus(visible_row_range.start) as usize)?;
        let cursor_column = cursor.column() as usize;

        let cursor_character_x = cursor_row_layout.x_for_index(cursor_column);

        let origin = scrolled_content_origin + point(cursor_character_x, to_y(size));

        element.prepaint_at(origin, window, cx);
        Some((element, origin))
    }

    fn render_edit_prediction_eager_jump_popover(
        &mut self,
        text_bounds: &Bounds<Pixels>,
        content_origin: gpui::Point<Pixels>,
        editor_snapshot: &EditorSnapshot,
        visible_row_range: Range<DisplayRow>,
        scroll_top: ScrollOffset,
        scroll_bottom: ScrollOffset,
        line_height: Pixels,
        scroll_pixel_position: gpui::Point<ScrollPixelOffset>,
        target_display_point: DisplayPoint,
        editor_width: Pixels,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<(AnyElement, gpui::Point<Pixels>)> {
        if target_display_point.row().as_f64() < scroll_top {
            let mut element = self
                .render_edit_prediction_line_popover(
                    "Jump to Edit",
                    Some(IconName::ArrowUp),
                    window,
                    cx,
                )
                .into_any();

            let size = element.layout_as_root(AvailableSpace::min_size(), window, cx);
            let offset = point(
                (text_bounds.size.width - size.width) / 2.,
                Self::EDIT_PREDICTION_POPOVER_PADDING_Y,
            );

            let origin = text_bounds.origin + offset;
            element.prepaint_at(origin, window, cx);
            Some((element, origin))
        } else if (target_display_point.row().as_f64() + 1.) > scroll_bottom {
            let mut element = self
                .render_edit_prediction_line_popover(
                    "Jump to Edit",
                    Some(IconName::ArrowDown),
                    window,
                    cx,
                )
                .into_any();

            let size = element.layout_as_root(AvailableSpace::min_size(), window, cx);
            let offset = point(
                (text_bounds.size.width - size.width) / 2.,
                text_bounds.size.height - size.height - Self::EDIT_PREDICTION_POPOVER_PADDING_Y,
            );

            let origin = text_bounds.origin + offset;
            element.prepaint_at(origin, window, cx);
            Some((element, origin))
        } else {
            self.render_edit_prediction_end_of_line_popover(
                "Jump to Edit",
                editor_snapshot,
                visible_row_range,
                target_display_point,
                line_height,
                scroll_pixel_position,
                content_origin,
                editor_width,
                window,
                cx,
            )
        }
    }

    fn render_edit_prediction_end_of_line_popover(
        self: &mut Editor,
        label: &'static str,
        editor_snapshot: &EditorSnapshot,
        visible_row_range: Range<DisplayRow>,
        target_display_point: DisplayPoint,
        line_height: Pixels,
        scroll_pixel_position: gpui::Point<ScrollPixelOffset>,
        content_origin: gpui::Point<Pixels>,
        editor_width: Pixels,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<(AnyElement, gpui::Point<Pixels>)> {
        let target_line_end = DisplayPoint::new(
            target_display_point.row(),
            editor_snapshot.line_len(target_display_point.row()),
        );

        let mut element = self
            .render_edit_prediction_line_popover(label, None, window, cx)
            .into_any();

        let size = element.layout_as_root(AvailableSpace::min_size(), window, cx);

        let line_origin =
            self.display_to_pixel_point(target_line_end, editor_snapshot, window, cx)?;

        let start_point = content_origin - point(scroll_pixel_position.x.into(), Pixels::ZERO);
        let mut origin = start_point
            + line_origin
            + point(Self::EDIT_PREDICTION_POPOVER_PADDING_X, Pixels::ZERO);
        origin.x = origin.x.max(content_origin.x);

        let max_x = content_origin.x + editor_width - size.width;

        if origin.x > max_x {
            let offset = line_height + Self::EDIT_PREDICTION_POPOVER_PADDING_Y;

            let icon = if visible_row_range.contains(&(target_display_point.row() + 2)) {
                origin.y += offset;
                IconName::ArrowUp
            } else {
                origin.y -= offset;
                IconName::ArrowDown
            };

            element = self
                .render_edit_prediction_line_popover(label, Some(icon), window, cx)
                .into_any();

            let size = element.layout_as_root(AvailableSpace::min_size(), window, cx);

            origin.x = content_origin.x + editor_width - size.width - px(2.);
        }

        element.prepaint_at(origin, window, cx);
        Some((element, origin))
    }

    fn render_edit_prediction_diff_popover(
        self: &Editor,
        text_bounds: &Bounds<Pixels>,
        content_origin: gpui::Point<Pixels>,
        right_margin: Pixels,
        editor_snapshot: &EditorSnapshot,
        visible_row_range: Range<DisplayRow>,
        line_layouts: &[LineWithInvisibles],
        line_height: Pixels,
        scroll_position: gpui::Point<ScrollOffset>,
        scroll_pixel_position: gpui::Point<ScrollPixelOffset>,
        newest_selection_head: Option<DisplayPoint>,
        editor_width: Pixels,
        style: &EditorStyle,
        edits: &Vec<(Range<Anchor>, Arc<str>)>,
        edit_preview: &Option<language::EditPreview>,
        snapshot: &language::BufferSnapshot,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<(AnyElement, gpui::Point<Pixels>)> {
        let edit_start = edits
            .first()
            .unwrap()
            .0
            .start
            .to_display_point(editor_snapshot);
        let edit_end = edits
            .last()
            .unwrap()
            .0
            .end
            .to_display_point(editor_snapshot);

        let is_visible = visible_row_range.contains(&edit_start.row())
            || visible_row_range.contains(&edit_end.row());
        if !is_visible {
            return None;
        }

        let highlighted_edits = if let Some(edit_preview) = edit_preview.as_ref() {
            crate::edit_prediction_edit_text(
                snapshot,
                edits,
                edit_preview,
                false,
                editor_snapshot.buffer_snapshot(),
                cx,
            )
        } else {
            // Fallback for providers without edit_preview
            crate::edit_prediction_fallback_text(edits, cx)
        };

        let styled_text = highlighted_edits.to_styled_text(&style.text);
        let line_count = highlighted_edits.text.lines().count();

        const BORDER_WIDTH: Pixels = px(1.);

        let keybind = self.render_edit_prediction_keybind(window, cx);
        let has_keybind = keybind.is_some();

        let mut element = h_flex()
            .items_start()
            .child(
                h_flex()
                    .bg(cx.theme().colors().editor_background)
                    .border(BORDER_WIDTH)
                    .shadow_xs()
                    .border_color(cx.theme().colors().border)
                    .rounded_l_lg()
                    .when(line_count > 1, |el| el.rounded_br_lg())
                    .pr_1()
                    .child(styled_text),
            )
            .child(
                h_flex()
                    .h(line_height + BORDER_WIDTH * 2.)
                    .px_1p5()
                    .gap_1()
                    // Workaround: For some reason, there's a gap if we don't do this
                    .ml(-BORDER_WIDTH)
                    .shadow(vec![gpui::BoxShadow {
                        color: gpui::black().opacity(0.05),
                        offset: point(px(1.), px(1.)),
                        blur_radius: px(2.),
                        spread_radius: px(0.),
                    }])
                    .bg(Editor::edit_prediction_line_popover_bg_color(cx))
                    .border(BORDER_WIDTH)
                    .border_color(cx.theme().colors().border)
                    .rounded_r_lg()
                    .id("edit_prediction_diff_popover_keybind")
                    .when(!has_keybind, |el| {
                        let status_colors = cx.theme().status();

                        el.bg(status_colors.error_background)
                            .border_color(status_colors.error.opacity(0.6))
                            .child(Icon::new(IconName::Info).color(Color::Error))
                            .cursor_default()
                            .hoverable_tooltip(move |_window, cx| {
                                cx.new(|_| MissingEditPredictionKeybindingTooltip).into()
                            })
                    })
                    .children(keybind),
            )
            .into_any();

        let longest_row =
            editor_snapshot.longest_row_in_range(edit_start.row()..edit_end.row() + 1);
        let longest_line_width = if visible_row_range.contains(&longest_row) {
            line_layouts[(longest_row.0 - visible_row_range.start.0) as usize].width
        } else {
            layout_line(
                longest_row,
                editor_snapshot,
                style,
                editor_width,
                |_| false,
                window,
                cx,
            )
            .width
        };

        let viewport_bounds =
            Bounds::new(Default::default(), window.viewport_size()).extend(Edges {
                right: -right_margin,
                ..Default::default()
            });

        let x_after_longest = Pixels::from(
            ScrollPixelOffset::from(
                text_bounds.origin.x + longest_line_width + Self::EDIT_PREDICTION_POPOVER_PADDING_X,
            ) - scroll_pixel_position.x,
        );

        let element_bounds = element.layout_as_root(AvailableSpace::min_size(), window, cx);

        // Fully visible if it can be displayed within the window (allow overlapping other
        // panes). However, this is only allowed if the popover starts within text_bounds.
        let can_position_to_the_right = x_after_longest < text_bounds.right()
            && x_after_longest + element_bounds.width < viewport_bounds.right();

        let mut origin = if can_position_to_the_right {
            point(
                x_after_longest,
                text_bounds.origin.y
                    + Pixels::from(
                        edit_start.row().as_f64() * ScrollPixelOffset::from(line_height)
                            - scroll_pixel_position.y,
                    ),
            )
        } else {
            let cursor_row = newest_selection_head.map(|head| head.row());
            let above_edit = edit_start
                .row()
                .0
                .checked_sub(line_count as u32)
                .map(DisplayRow);
            let below_edit = Some(edit_end.row() + 1);
            let above_cursor =
                cursor_row.and_then(|row| row.0.checked_sub(line_count as u32).map(DisplayRow));
            let below_cursor = cursor_row.map(|cursor_row| cursor_row + 1);

            // Place the edit popover adjacent to the edit if there is a location
            // available that is onscreen and does not obscure the cursor. Otherwise,
            // place it adjacent to the cursor.
            let row_target = [above_edit, below_edit, above_cursor, below_cursor]
                .into_iter()
                .flatten()
                .find(|&start_row| {
                    let end_row = start_row + line_count as u32;
                    visible_row_range.contains(&start_row)
                        && visible_row_range.contains(&end_row)
                        && cursor_row
                            .is_none_or(|cursor_row| !((start_row..end_row).contains(&cursor_row)))
                })?;

            content_origin
                + point(
                    Pixels::from(-scroll_pixel_position.x),
                    Pixels::from(
                        (row_target.as_f64() - scroll_position.y) * f64::from(line_height),
                    ),
                )
        };

        origin.x -= BORDER_WIDTH;

        window.with_content_mask(
            Some(gpui::ContentMask {
                bounds: *text_bounds,
            }),
            |window| {
                window.defer_draw(element, origin, 1, Some(window.content_mask()));
            },
        );

        // Do not return an element, since it will already be drawn due to defer_draw.
        None
    }

    fn edit_prediction_cursor_popover_height(&self) -> Pixels {
        px(30.)
    }

    fn current_user_player_color(&self, cx: &mut App) -> PlayerColor {
        if self.read_only(cx) {
            cx.theme().players().read_only()
        } else {
            self.style.as_ref().unwrap().local_player
        }
    }

    fn render_edit_prediction_inline_keystroke(
        &self,
        keystroke: &gpui::KeybindingKeystroke,
        modifiers_color: Color,
        cx: &App,
    ) -> AnyElement {
        let is_platform_style_mac = PlatformStyle::platform() == PlatformStyle::Mac;

        h_flex()
            .px_0p5()
            .when(is_platform_style_mac, |parent| parent.gap_0p5())
            .font(
                theme_settings::ThemeSettings::get_global(cx)
                    .buffer_font
                    .clone(),
            )
            .text_size(TextSize::XSmall.rems(cx))
            .child(h_flex().children(ui::render_modifiers(
                keystroke.modifiers(),
                PlatformStyle::platform(),
                Some(modifiers_color),
                Some(IconSize::XSmall.rems().into()),
                true,
            )))
            .when(is_platform_style_mac, |parent| {
                parent.child(keystroke.key().to_string())
            })
            .when(!is_platform_style_mac, |parent| {
                parent.child(
                    Key::new(ui::utils::capitalize(keystroke.key()), Some(Color::Default))
                        .size(Some(IconSize::XSmall.rems().into())),
                )
            })
            .into_any()
    }

    fn render_edit_prediction_popover_keystroke(
        &self,
        keystroke: &gpui::KeybindingKeystroke,
        color: Color,
        cx: &App,
    ) -> AnyElement {
        let is_platform_style_mac = PlatformStyle::platform() == PlatformStyle::Mac;

        if keystroke.modifiers().modified() {
            h_flex()
                .font(
                    theme_settings::ThemeSettings::get_global(cx)
                        .buffer_font
                        .clone(),
                )
                .when(is_platform_style_mac, |parent| parent.gap_1())
                .child(h_flex().children(ui::render_modifiers(
                    keystroke.modifiers(),
                    PlatformStyle::platform(),
                    Some(color),
                    None,
                    false,
                )))
                .into_any()
        } else {
            Key::new(ui::utils::capitalize(keystroke.key()), Some(color))
                .size(Some(IconSize::XSmall.rems().into()))
                .into_any_element()
        }
    }

    fn render_edit_prediction_keybind(
        &self,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<AnyElement> {
        let keybind_display =
            self.edit_prediction_keybind_display(EditPredictionKeybindSurface::Inline, window, cx);
        let keystroke = keybind_display.displayed_keystroke.as_ref()?;

        let modifiers_color = if *keystroke.modifiers() == window.modifiers() {
            Color::Accent
        } else {
            Color::Muted
        };

        Some(self.render_edit_prediction_inline_keystroke(keystroke, modifiers_color, cx))
    }

    fn render_edit_prediction_line_popover(
        &self,
        label: impl Into<SharedString>,
        icon: Option<IconName>,
        window: &mut Window,
        cx: &mut App,
    ) -> Stateful<Div> {
        let padding_right = if icon.is_some() { px(4.) } else { px(8.) };

        let keybind = self.render_edit_prediction_keybind(window, cx);
        let has_keybind = keybind.is_some();
        let icons = Self::get_prediction_provider_icons(&self.edit_prediction_provider, cx);

        h_flex()
            .id("ep-line-popover")
            .py_0p5()
            .pl_1()
            .pr(padding_right)
            .gap_1()
            .rounded_md()
            .border_1()
            .bg(Self::edit_prediction_line_popover_bg_color(cx))
            .border_color(Self::edit_prediction_callout_popover_border_color(cx))
            .shadow_xs()
            .when(!has_keybind, |el| {
                let status_colors = cx.theme().status();

                el.bg(status_colors.error_background)
                    .border_color(status_colors.error.opacity(0.6))
                    .pl_2()
                    .child(Icon::new(icons.error).color(Color::Error))
                    .cursor_default()
                    .hoverable_tooltip(move |_window, cx| {
                        cx.new(|_| MissingEditPredictionKeybindingTooltip).into()
                    })
            })
            .children(keybind)
            .child(
                Label::new(label)
                    .size(LabelSize::Small)
                    .when(!has_keybind, |el| {
                        el.color(cx.theme().status().error.into()).strikethrough()
                    }),
            )
            .when(!has_keybind, |el| {
                el.child(
                    h_flex().ml_1().child(
                        Icon::new(IconName::Info)
                            .size(IconSize::Small)
                            .color(cx.theme().status().error.into()),
                    ),
                )
            })
            .when_some(icon, |element, icon| {
                element.child(
                    div()
                        .mt(px(1.5))
                        .child(Icon::new(icon).size(IconSize::Small)),
                )
            })
    }

    fn render_edit_prediction_jump_outside_popover(
        &self,
        snapshot: &BufferSnapshot,
        window: &mut Window,
        cx: &mut App,
    ) -> Stateful<Div> {
        let keybind = self.render_edit_prediction_keybind(window, cx);
        let has_keybind = keybind.is_some();
        let icons = Self::get_prediction_provider_icons(&self.edit_prediction_provider, cx);

        let file_name = snapshot
            .file()
            .map(|file| SharedString::new(file.file_name(cx)))
            .unwrap_or(SharedString::new_static("untitled"));

        h_flex()
            .id("ep-jump-outside-popover")
            .py_1()
            .px_2()
            .gap_1()
            .rounded_md()
            .border_1()
            .bg(Self::edit_prediction_line_popover_bg_color(cx))
            .border_color(Self::edit_prediction_callout_popover_border_color(cx))
            .shadow_xs()
            .when(!has_keybind, |el| {
                let status_colors = cx.theme().status();

                el.bg(status_colors.error_background)
                    .border_color(status_colors.error.opacity(0.6))
                    .pl_2()
                    .child(Icon::new(icons.error).color(Color::Error))
                    .cursor_default()
                    .hoverable_tooltip(move |_window, cx| {
                        cx.new(|_| MissingEditPredictionKeybindingTooltip).into()
                    })
            })
            .children(keybind)
            .child(
                Label::new(file_name)
                    .size(LabelSize::Small)
                    .buffer_font(cx)
                    .when(!has_keybind, |el| {
                        el.color(cx.theme().status().error.into()).strikethrough()
                    }),
            )
            .when(!has_keybind, |el| {
                el.child(
                    h_flex().ml_1().child(
                        Icon::new(IconName::Info)
                            .size(IconSize::Small)
                            .color(cx.theme().status().error.into()),
                    ),
                )
            })
            .child(
                div()
                    .mt(px(1.5))
                    .child(Icon::new(IconName::ArrowUpRight).size(IconSize::Small)),
            )
    }

    fn edit_prediction_line_popover_bg_color(cx: &App) -> Hsla {
        let accent_color = cx.theme().colors().text_accent;
        let editor_bg_color = cx.theme().colors().editor_background;
        editor_bg_color.blend(accent_color.opacity(0.1))
    }

    fn edit_prediction_callout_popover_border_color(cx: &App) -> Hsla {
        let accent_color = cx.theme().colors().text_accent;
        let editor_bg_color = cx.theme().colors().editor_background;
        editor_bg_color.blend(accent_color.opacity(0.6))
    }
    fn get_prediction_provider_icons(
        provider: &Option<RegisteredEditPredictionDelegate>,
        cx: &App,
    ) -> edit_prediction_types::EditPredictionIconSet {
        match provider {
            Some(provider) => provider.provider.icons(cx),
            None => edit_prediction_types::EditPredictionIconSet::new(IconName::ZedPredict),
        }
    }

    fn render_edit_prediction_cursor_popover(
        &self,
        min_width: Pixels,
        max_width: Pixels,
        cursor_point: Point,
        style: &EditorStyle,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) -> Option<AnyElement> {
        let provider = self.edit_prediction_provider.as_ref()?;
        let icons = Self::get_prediction_provider_icons(&self.edit_prediction_provider, cx);

        let is_refreshing = provider.provider.is_refreshing(cx);

        fn pending_completion_container(icon: IconName) -> Div {
            h_flex().h_full().flex_1().gap_2().child(Icon::new(icon))
        }

        let completion = match &self.active_edit_prediction {
            Some(prediction) => {
                if !self.has_visible_completions_menu() {
                    const RADIUS: Pixels = px(6.);
                    const BORDER_WIDTH: Pixels = px(1.);
                    let keybind_display = self.edit_prediction_keybind_display(
                        EditPredictionKeybindSurface::CursorPopoverCompact,
                        window,
                        cx,
                    );

                    return Some(
                        h_flex()
                            .elevation_2(cx)
                            .border(BORDER_WIDTH)
                            .border_color(cx.theme().colors().border)
                            .when(keybind_display.missing_accept_keystroke, |el| {
                                el.border_color(cx.theme().status().error)
                            })
                            .rounded(RADIUS)
                            .rounded_tl(px(0.))
                            .overflow_hidden()
                            .child(div().px_1p5().child(match &prediction.completion {
                                EditPrediction::MoveWithin { target, snapshot } => {
                                    use text::ToPoint as _;
                                    if target.text_anchor_in(&snapshot).to_point(snapshot).row
                                        > cursor_point.row
                                    {
                                        Icon::new(icons.down)
                                    } else {
                                        Icon::new(icons.up)
                                    }
                                }
                                EditPrediction::MoveOutside { .. } => {
                                    // TODO [zeta2] custom icon for external jump?
                                    Icon::new(icons.base)
                                }
                                EditPrediction::Edit { .. } => Icon::new(icons.base),
                            }))
                            .child(
                                h_flex()
                                    .gap_1()
                                    .py_1()
                                    .px_2()
                                    .rounded_r(RADIUS - BORDER_WIDTH)
                                    .border_l_1()
                                    .border_color(cx.theme().colors().border)
                                    .bg(Self::edit_prediction_line_popover_bg_color(cx))
                                    .when(keybind_display.show_hold_label, |el| {
                                        el.child(
                                            Label::new("Hold")
                                                .size(LabelSize::Small)
                                                .when(
                                                    keybind_display.missing_accept_keystroke,
                                                    |el| el.strikethrough(),
                                                )
                                                .line_height_style(LineHeightStyle::UiLabel),
                                        )
                                    })
                                    .id("edit_prediction_cursor_popover_keybind")
                                    .when(keybind_display.missing_accept_keystroke, |el| {
                                        let status_colors = cx.theme().status();

                                        el.bg(status_colors.error_background)
                                            .border_color(status_colors.error.opacity(0.6))
                                            .child(Icon::new(IconName::Info).color(Color::Error))
                                            .cursor_default()
                                            .hoverable_tooltip(move |_window, cx| {
                                                cx.new(|_| MissingEditPredictionKeybindingTooltip)
                                                    .into()
                                            })
                                    })
                                    .when_some(
                                        keybind_display.displayed_keystroke.as_ref(),
                                        |el, compact_keystroke| {
                                            el.child(self.render_edit_prediction_popover_keystroke(
                                                compact_keystroke,
                                                Color::Default,
                                                cx,
                                            ))
                                        },
                                    ),
                            )
                            .into_any(),
                    );
                }

                self.render_edit_prediction_cursor_popover_preview(
                    prediction,
                    cursor_point,
                    style,
                    cx,
                )?
            }

            None if is_refreshing => match &self.stale_edit_prediction_in_menu {
                Some(stale_completion) => self.render_edit_prediction_cursor_popover_preview(
                    stale_completion,
                    cursor_point,
                    style,
                    cx,
                )?,

                None => pending_completion_container(icons.base)
                    .child(Label::new("...").size(LabelSize::Small)),
            },

            None => pending_completion_container(icons.base)
                .child(Label::new("...").size(LabelSize::Small)),
        };

        let completion = if is_refreshing || self.active_edit_prediction.is_none() {
            completion
                .with_animation(
                    "loading-completion",
                    Animation::new(Duration::from_secs(2))
                        .repeat()
                        .with_easing(pulsating_between(0.4, 0.8)),
                    |label, delta| label.opacity(delta),
                )
                .into_any_element()
        } else {
            completion.into_any_element()
        };

        let has_completion = self.active_edit_prediction.is_some();
        let keybind_display = self.edit_prediction_keybind_display(
            EditPredictionKeybindSurface::CursorPopoverExpanded,
            window,
            cx,
        );

        Some(
            h_flex()
                .min_w(min_width)
                .max_w(max_width)
                .flex_1()
                .elevation_2(cx)
                .border_color(cx.theme().colors().border)
                .child(
                    div()
                        .flex_1()
                        .py_1()
                        .px_2()
                        .overflow_hidden()
                        .child(completion),
                )
                .when_some(
                    keybind_display.displayed_keystroke.as_ref(),
                    |el, keystroke| {
                        let key_color = if !has_completion {
                            Color::Muted
                        } else {
                            Color::Default
                        };

                        if keybind_display.action == EditPredictionKeybindAction::Preview {
                            el.child(
                                h_flex()
                                    .h_full()
                                    .border_l_1()
                                    .rounded_r_lg()
                                    .border_color(cx.theme().colors().border)
                                    .bg(Self::edit_prediction_line_popover_bg_color(cx))
                                    .gap_1()
                                    .py_1()
                                    .px_2()
                                    .child(self.render_edit_prediction_popover_keystroke(
                                        keystroke, key_color, cx,
                                    ))
                                    .child(Label::new("Preview").into_any_element())
                                    .opacity(if has_completion { 1.0 } else { 0.4 }),
                            )
                        } else {
                            el.child(
                                h_flex()
                                    .h_full()
                                    .border_l_1()
                                    .rounded_r_lg()
                                    .border_color(cx.theme().colors().border)
                                    .bg(Self::edit_prediction_line_popover_bg_color(cx))
                                    .gap_1()
                                    .py_1()
                                    .px_2()
                                    .child(self.render_edit_prediction_popover_keystroke(
                                        keystroke, key_color, cx,
                                    ))
                                    .opacity(if has_completion { 1.0 } else { 0.4 }),
                            )
                        }
                    },
                )
                .into_any(),
        )
    }

    fn render_edit_prediction_cursor_popover_preview(
        &self,
        completion: &EditPredictionState,
        cursor_point: Point,
        style: &EditorStyle,
        cx: &mut Context<Editor>,
    ) -> Option<Div> {
        use text::ToPoint as _;

        fn render_relative_row_jump(
            prefix: impl Into<String>,
            current_row: u32,
            target_row: u32,
        ) -> Div {
            let (row_diff, arrow) = if target_row < current_row {
                (current_row - target_row, IconName::ArrowUp)
            } else {
                (target_row - current_row, IconName::ArrowDown)
            };

            h_flex()
                .child(
                    Label::new(format!("{}{}", prefix.into(), row_diff))
                        .color(Color::Muted)
                        .size(LabelSize::Small),
                )
                .child(Icon::new(arrow).color(Color::Muted).size(IconSize::Small))
        }

        let supports_jump = self
            .edit_prediction_provider
            .as_ref()
            .map(|provider| provider.provider.supports_jump_to_edit())
            .unwrap_or(true);

        let icons = Self::get_prediction_provider_icons(&self.edit_prediction_provider, cx);

        match &completion.completion {
            EditPrediction::MoveWithin {
                target, snapshot, ..
            } => {
                if !supports_jump {
                    return None;
                }
                let (target, _) = self.display_snapshot(cx).anchor_to_buffer_anchor(*target)?;

                Some(
                    h_flex()
                        .px_2()
                        .gap_2()
                        .flex_1()
                        .child(if target.to_point(snapshot).row > cursor_point.row {
                            Icon::new(icons.down)
                        } else {
                            Icon::new(icons.up)
                        })
                        .child(Label::new("Jump to Edit")),
                )
            }
            EditPrediction::MoveOutside { snapshot, .. } => {
                let file_name = snapshot
                    .file()
                    .map(|file| file.file_name(cx))
                    .unwrap_or("untitled");
                Some(
                    h_flex()
                        .px_2()
                        .gap_2()
                        .flex_1()
                        .child(Icon::new(icons.base))
                        .child(Label::new(format!("Jump to {file_name}"))),
                )
            }
            EditPrediction::Edit {
                edits,
                edit_preview,
                snapshot,
                ..
            } => {
                let first_edit_row = self
                    .display_snapshot(cx)
                    .anchor_to_buffer_anchor(edits.first()?.0.start)?
                    .0
                    .to_point(snapshot)
                    .row;

                let (highlighted_edits, has_more_lines) =
                    if let Some(edit_preview) = edit_preview.as_ref() {
                        crate::edit_prediction_edit_text(
                            snapshot,
                            edits,
                            edit_preview,
                            true,
                            &self.display_snapshot(cx),
                            cx,
                        )
                        .first_line_preview()
                    } else {
                        crate::edit_prediction_fallback_text(edits, cx).first_line_preview()
                    };

                let styled_text = gpui::StyledText::new(highlighted_edits.text)
                    .with_default_highlights(&style.text, highlighted_edits.highlights);

                let preview = h_flex()
                    .gap_1()
                    .min_w_16()
                    .child(styled_text)
                    .when(has_more_lines, |parent| parent.child("…"));

                let left = if supports_jump && first_edit_row != cursor_point.row {
                    render_relative_row_jump("", cursor_point.row, first_edit_row)
                        .into_any_element()
                } else {
                    Icon::new(icons.base).into_any_element()
                };

                Some(
                    h_flex()
                        .h_full()
                        .flex_1()
                        .gap_2()
                        .pr_1()
                        .overflow_x_hidden()
                        .font(
                            theme_settings::ThemeSettings::get_global(cx)
                                .buffer_font
                                .clone(),
                        )
                        .child(left)
                        .child(preview),
                )
            }
        }
    }

    pub fn render_context_menu(
        &mut self,
        max_height_in_lines: u32,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) -> Option<AnyElement> {
        let menu = self.context_menu.borrow();
        let menu = menu.as_ref()?;
        if !menu.visible() {
            return None;
        };
        self.style
            .as_ref()
            .map(|style| menu.render(style, max_height_in_lines, window, cx))
    }

    fn render_context_menu_aside(
        &mut self,
        max_size: Size<Pixels>,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) -> Option<AnyElement> {
        self.context_menu.borrow_mut().as_mut().and_then(|menu| {
            if menu.visible() {
                menu.render_aside(max_size, window, cx)
            } else {
                None
            }
        })
    }

    fn hide_context_menu(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<CodeContextMenu> {
        cx.notify();
        self.completion_tasks.clear();
        let context_menu = self.context_menu.borrow_mut().take();
        self.stale_edit_prediction_in_menu.take();
        self.update_visible_edit_prediction(window, cx);
        if let Some(CodeContextMenu::Completions(_)) = &context_menu
            && let Some(completion_provider) = &self.completion_provider
        {
            completion_provider.selection_changed(None, window, cx);
        }
        context_menu
    }

    fn show_snippet_choices(
        &mut self,
        choices: &Vec<String>,
        selection: Range<Anchor>,
        cx: &mut Context<Self>,
    ) {
        let buffer_snapshot = self.buffer.read(cx).snapshot(cx);
        let Some((buffer_snapshot, range)) =
            buffer_snapshot.anchor_range_to_buffer_anchor_range(selection.clone())
        else {
            return;
        };
        let Some(buffer) = self.buffer.read(cx).buffer(buffer_snapshot.remote_id()) else {
            return;
        };

        let id = post_inc(&mut self.next_completion_id);
        let snippet_sort_order = EditorSettings::get_global(cx).snippet_sort_order;
        let mut context_menu = self.context_menu.borrow_mut();
        let old_menu = context_menu.take();
        *context_menu = Some(CodeContextMenu::Completions(
            CompletionsMenu::new_snippet_choices(
                id,
                true,
                choices,
                selection.start,
                range,
                buffer,
                old_menu.map(|menu| menu.primary_scroll_handle()),
                snippet_sort_order,
            ),
        ));
    }

    pub fn insert_snippet(
        &mut self,
        insertion_ranges: &[Range<MultiBufferOffset>],
        snippet: Snippet,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<()> {
        struct Tabstop<T> {
            is_end_tabstop: bool,
            ranges: Vec<Range<T>>,
            choices: Option<Vec<String>>,
        }

        let tabstops = self.buffer.update(cx, |buffer, cx| {
            let snippet_text: Arc<str> = snippet.text.clone().into();
            let edits = insertion_ranges
                .iter()
                .cloned()
                .map(|range| (range, snippet_text.clone()));
            let autoindent_mode = AutoindentMode::Block {
                original_indent_columns: Vec::new(),
            };
            buffer.edit(edits, Some(autoindent_mode), cx);

            let snapshot = &*buffer.read(cx);
            let snippet = &snippet;
            snippet
                .tabstops
                .iter()
                .map(|tabstop| {
                    let is_end_tabstop = tabstop.ranges.first().is_some_and(|tabstop| {
                        tabstop.is_empty() && tabstop.start == snippet.text.len() as isize
                    });
                    let mut tabstop_ranges = tabstop
                        .ranges
                        .iter()
                        .flat_map(|tabstop_range| {
                            let mut delta = 0_isize;
                            insertion_ranges.iter().map(move |insertion_range| {
                                let insertion_start = insertion_range.start + delta;
                                delta += snippet.text.len() as isize
                                    - (insertion_range.end - insertion_range.start) as isize;

                                let start =
                                    (insertion_start + tabstop_range.start).min(snapshot.len());
                                let end = (insertion_start + tabstop_range.end).min(snapshot.len());
                                snapshot.anchor_before(start)..snapshot.anchor_after(end)
                            })
                        })
                        .collect::<Vec<_>>();
                    tabstop_ranges.sort_unstable_by(|a, b| a.start.cmp(&b.start, snapshot));

                    Tabstop {
                        is_end_tabstop,
                        ranges: tabstop_ranges,
                        choices: tabstop.choices.clone(),
                    }
                })
                .collect::<Vec<_>>()
        });
        if let Some(tabstop) = tabstops.first() {
            self.change_selections(Default::default(), window, cx, |s| {
                // Reverse order so that the first range is the newest created selection.
                // Completions will use it and autoscroll will prioritize it.
                s.select_ranges(tabstop.ranges.iter().rev().cloned());
            });

            if let Some(choices) = &tabstop.choices
                && let Some(selection) = tabstop.ranges.first()
            {
                self.show_snippet_choices(choices, selection.clone(), cx)
            }

            // If we're already at the last tabstop and it's at the end of the snippet,
            // we're done, we don't need to keep the state around.
            if !tabstop.is_end_tabstop {
                let choices = tabstops
                    .iter()
                    .map(|tabstop| tabstop.choices.clone())
                    .collect();

                let ranges = tabstops
                    .into_iter()
                    .map(|tabstop| tabstop.ranges)
                    .collect::<Vec<_>>();

                self.snippet_stack.push(SnippetState {
                    active_index: 0,
                    ranges,
                    choices,
                });
            }

            // Check whether the just-entered snippet ends with an auto-closable bracket.
            if self.autoclose_regions.is_empty() {
                let snapshot = self.buffer.read(cx).snapshot(cx);
                for selection in &mut self.selections.all::<Point>(&self.display_snapshot(cx)) {
                    let selection_head = selection.head();
                    let Some(scope) = snapshot.language_scope_at(selection_head) else {
                        continue;
                    };

                    let mut bracket_pair = None;
                    let max_lookup_length = scope
                        .brackets()
                        .map(|(pair, _)| {
                            pair.start
                                .as_str()
                                .chars()
                                .count()
                                .max(pair.end.as_str().chars().count())
                        })
                        .max();
                    if let Some(max_lookup_length) = max_lookup_length {
                        let next_text = snapshot
                            .chars_at(selection_head)
                            .take(max_lookup_length)
                            .collect::<String>();
                        let prev_text = snapshot
                            .reversed_chars_at(selection_head)
                            .take(max_lookup_length)
                            .collect::<String>();

                        for (pair, enabled) in scope.brackets() {
                            if enabled
                                && pair.close
                                && prev_text.starts_with(pair.start.as_str())
                                && next_text.starts_with(pair.end.as_str())
                            {
                                bracket_pair = Some(pair.clone());
                                break;
                            }
                        }
                    }

                    if let Some(pair) = bracket_pair {
                        let snapshot_settings = snapshot.language_settings_at(selection_head, cx);
                        let autoclose_enabled =
                            self.use_autoclose && snapshot_settings.use_autoclose;
                        if autoclose_enabled {
                            let start = snapshot.anchor_after(selection_head);
                            let end = snapshot.anchor_after(selection_head);
                            self.autoclose_regions.push(AutocloseRegion {
                                selection_id: selection.id,
                                range: start..end,
                                pair,
                            });
                        }
                    }
                }
            }
        }
        Ok(())
    }

    pub fn move_to_next_snippet_tabstop(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> bool {
        self.move_to_snippet_tabstop(Bias::Right, window, cx)
    }

    pub fn move_to_prev_snippet_tabstop(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> bool {
        self.move_to_snippet_tabstop(Bias::Left, window, cx)
    }

    pub fn move_to_snippet_tabstop(
        &mut self,
        bias: Bias,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> bool {
        if let Some(mut snippet) = self.snippet_stack.pop() {
            match bias {
                Bias::Left => {
                    if snippet.active_index > 0 {
                        snippet.active_index -= 1;
                    } else {
                        self.snippet_stack.push(snippet);
                        return false;
                    }
                }
                Bias::Right => {
                    if snippet.active_index + 1 < snippet.ranges.len() {
                        snippet.active_index += 1;
                    } else {
                        self.snippet_stack.push(snippet);
                        return false;
                    }
                }
            }
            if let Some(current_ranges) = snippet.ranges.get(snippet.active_index) {
                self.change_selections(Default::default(), window, cx, |s| {
                    // Reverse order so that the first range is the newest created selection.
                    // Completions will use it and autoscroll will prioritize it.
                    s.select_ranges(current_ranges.iter().rev().cloned())
                });

                if let Some(choices) = &snippet.choices[snippet.active_index]
                    && let Some(selection) = current_ranges.first()
                {
                    self.show_snippet_choices(choices, selection.clone(), cx);
                }

                // If snippet state is not at the last tabstop, push it back on the stack
                if snippet.active_index + 1 < snippet.ranges.len() {
                    self.snippet_stack.push(snippet);
                }
                return true;
            }
        }

        false
    }

    pub fn clear(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.transact(window, cx, |this, window, cx| {
            this.select_all(&SelectAll, window, cx);
            this.insert("", window, cx);
        });
    }

    pub fn backspace(&mut self, _: &Backspace, window: &mut Window, cx: &mut Context<Self>) {
        if self.read_only(cx) {
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            this.select_autoclose_pair(window, cx);

            let linked_edits = this.linked_edits_for_selections(Arc::from(""), cx);

            let display_map = this.display_map.update(cx, |map, cx| map.snapshot(cx));
            let mut selections = this.selections.all::<MultiBufferPoint>(&display_map);
            for selection in &mut selections {
                if selection.is_empty() {
                    let old_head = selection.head();
                    let mut new_head =
                        movement::left(&display_map, old_head.to_display_point(&display_map))
                            .to_point(&display_map);
                    if let Some((buffer, line_buffer_range)) = display_map
                        .buffer_snapshot()
                        .buffer_line_for_row(MultiBufferRow(old_head.row))
                    {
                        let indent_size = buffer.indent_size_for_line(line_buffer_range.start.row);
                        let indent_len = match indent_size.kind {
                            IndentKind::Space => {
                                buffer.settings_at(line_buffer_range.start, cx).tab_size
                            }
                            IndentKind::Tab => NonZeroU32::new(1).unwrap(),
                        };
                        if old_head.column <= indent_size.len && old_head.column > 0 {
                            let indent_len = indent_len.get();
                            new_head = cmp::min(
                                new_head,
                                MultiBufferPoint::new(
                                    old_head.row,
                                    ((old_head.column - 1) / indent_len) * indent_len,
                                ),
                            );
                        }
                    }

                    selection.set_head(new_head, SelectionGoal::None);
                }
            }

            this.change_selections(Default::default(), window, cx, |s| s.select(selections));
            this.insert("", window, cx);
            linked_edits.apply_with_left_expansion(cx);
            this.refresh_edit_prediction(true, false, window, cx);
            refresh_linked_ranges(this, window, cx);
        });
    }

    pub fn delete(&mut self, _: &Delete, window: &mut Window, cx: &mut Context<Self>) {
        if self.read_only(cx) {
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            this.change_selections(Default::default(), window, cx, |s| {
                s.move_with(&mut |map, selection| {
                    if selection.is_empty() {
                        let cursor = movement::right(map, selection.head());
                        selection.end = cursor;
                        selection.reversed = true;
                        selection.goal = SelectionGoal::None;
                    }
                })
            });
            let linked_edits = this.linked_edits_for_selections(Arc::from(""), cx);
            this.insert("", window, cx);
            linked_edits.apply(cx);
            this.refresh_edit_prediction(true, false, window, cx);
            refresh_linked_ranges(this, window, cx);
        });
    }

    pub fn backtab(&mut self, _: &Backtab, window: &mut Window, cx: &mut Context<Self>) {
        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        if self.move_to_prev_snippet_tabstop(window, cx) {
            return;
        }
        self.outdent(&Outdent, window, cx);
    }

    pub fn next_snippet_tabstop(
        &mut self,
        _: &NextSnippetTabstop,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.mode.is_single_line() || self.snippet_stack.is_empty() {
            cx.propagate();
            return;
        }

        if self.move_to_next_snippet_tabstop(window, cx) {
            self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
            return;
        }
        cx.propagate();
    }

    pub fn previous_snippet_tabstop(
        &mut self,
        _: &PreviousSnippetTabstop,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.mode.is_single_line() || self.snippet_stack.is_empty() {
            cx.propagate();
            return;
        }

        if self.move_to_prev_snippet_tabstop(window, cx) {
            self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
            return;
        }
        cx.propagate();
    }

    pub fn tab(&mut self, _: &Tab, window: &mut Window, cx: &mut Context<Self>) {
        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        if self.move_to_next_snippet_tabstop(window, cx) {
            self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
            return;
        }
        if self.read_only(cx) {
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let mut selections = self.selections.all_adjusted(&self.display_snapshot(cx));
        let buffer = self.buffer.read(cx);
        let snapshot = buffer.snapshot(cx);
        let rows_iter = selections.iter().map(|s| s.head().row);
        let suggested_indents = snapshot.suggested_indents(rows_iter, cx);

        let has_some_cursor_in_whitespace = selections
            .iter()
            .filter(|selection| selection.is_empty())
            .any(|selection| {
                let cursor = selection.head();
                let current_indent = snapshot.indent_size_for_line(MultiBufferRow(cursor.row));
                cursor.column < current_indent.len
            });

        let mut edits = Vec::new();
        let mut prev_edited_row = 0;
        let mut row_delta = 0;
        for selection in &mut selections {
            if selection.start.row != prev_edited_row {
                row_delta = 0;
            }
            prev_edited_row = selection.end.row;

            // If cursor is after a list prefix, make selection non-empty to trigger line indent
            if selection.is_empty() {
                let cursor = selection.head();
                let settings = buffer.language_settings_at(cursor, cx);
                if settings.indent_list_on_tab {
                    if let Some(language) = snapshot.language_scope_at(Point::new(cursor.row, 0)) {
                        if is_list_prefix_row(MultiBufferRow(cursor.row), &snapshot, &language) {
                            row_delta = Self::indent_selection(
                                buffer, &snapshot, selection, &mut edits, row_delta, cx,
                            );
                            continue;
                        }
                    }
                }
            }

            // If the selection is non-empty, then increase the indentation of the selected lines.
            if !selection.is_empty() {
                row_delta =
                    Self::indent_selection(buffer, &snapshot, selection, &mut edits, row_delta, cx);
                continue;
            }

            let cursor = selection.head();
            let current_indent = snapshot.indent_size_for_line(MultiBufferRow(cursor.row));
            if let Some(suggested_indent) =
                suggested_indents.get(&MultiBufferRow(cursor.row)).copied()
            {
                // Don't do anything if already at suggested indent
                // and there is any other cursor which is not
                if has_some_cursor_in_whitespace
                    && cursor.column == current_indent.len
                    && current_indent.len == suggested_indent.len
                {
                    continue;
                }

                // Adjust line and move cursor to suggested indent
                // if cursor is not at suggested indent
                if cursor.column < suggested_indent.len
                    && cursor.column <= current_indent.len
                    && current_indent.len <= suggested_indent.len
                {
                    selection.start = Point::new(cursor.row, suggested_indent.len);
                    selection.end = selection.start;
                    if row_delta == 0 {
                        edits.extend(Buffer::edit_for_indent_size_adjustment(
                            cursor.row,
                            current_indent,
                            suggested_indent,
                        ));
                        row_delta = suggested_indent.len - current_indent.len;
                    }
                    continue;
                }

                // If current indent is more than suggested indent
                // only move cursor to current indent and skip indent
                if cursor.column < current_indent.len && current_indent.len > suggested_indent.len {
                    selection.start = Point::new(cursor.row, current_indent.len);
                    selection.end = selection.start;
                    continue;
                }
            }

            // Otherwise, insert a hard or soft tab.
            let settings = buffer.language_settings_at(cursor, cx);
            let tab_size = if settings.hard_tabs {
                IndentSize::tab()
            } else {
                let tab_size = settings.tab_size.get();
                let indent_remainder = snapshot
                    .text_for_range(Point::new(cursor.row, 0)..cursor)
                    .flat_map(str::chars)
                    .fold(row_delta % tab_size, |counter: u32, c| {
                        if c == '\t' {
                            0
                        } else {
                            (counter + 1) % tab_size
                        }
                    });

                let chars_to_next_tab_stop = tab_size - indent_remainder;
                IndentSize::spaces(chars_to_next_tab_stop)
            };
            selection.start = Point::new(cursor.row, cursor.column + row_delta + tab_size.len);
            selection.end = selection.start;
            edits.push((cursor..cursor, tab_size.chars().collect::<String>()));
            row_delta += tab_size.len;
        }

        self.transact(window, cx, |this, window, cx| {
            this.buffer.update(cx, |b, cx| b.edit(edits, None, cx));
            this.change_selections(Default::default(), window, cx, |s| s.select(selections));
            this.refresh_edit_prediction(true, false, window, cx);
        });
    }

    pub fn indent(&mut self, _: &Indent, window: &mut Window, cx: &mut Context<Self>) {
        if self.read_only(cx) {
            return;
        }
        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let mut selections = self.selections.all::<Point>(&self.display_snapshot(cx));
        let mut prev_edited_row = 0;
        let mut row_delta = 0;
        let mut edits = Vec::new();
        let buffer = self.buffer.read(cx);
        let snapshot = buffer.snapshot(cx);
        for selection in &mut selections {
            if selection.start.row != prev_edited_row {
                row_delta = 0;
            }
            prev_edited_row = selection.end.row;

            row_delta =
                Self::indent_selection(buffer, &snapshot, selection, &mut edits, row_delta, cx);
        }

        self.transact(window, cx, |this, window, cx| {
            this.buffer.update(cx, |b, cx| b.edit(edits, None, cx));
            this.change_selections(Default::default(), window, cx, |s| s.select(selections));
        });
    }

    fn indent_selection(
        buffer: &MultiBuffer,
        snapshot: &MultiBufferSnapshot,
        selection: &mut Selection<Point>,
        edits: &mut Vec<(Range<Point>, String)>,
        delta_for_start_row: u32,
        cx: &App,
    ) -> u32 {
        let settings = buffer.language_settings_at(selection.start, cx);
        let tab_size = settings.tab_size.get();
        let indent_kind = if settings.hard_tabs {
            IndentKind::Tab
        } else {
            IndentKind::Space
        };
        let mut start_row = selection.start.row;
        let mut end_row = selection.end.row + 1;

        // If a selection ends at the beginning of a line, don't indent
        // that last line.
        if selection.end.column == 0 && selection.end.row > selection.start.row {
            end_row -= 1;
        }

        // Avoid re-indenting a row that has already been indented by a
        // previous selection, but still update this selection's column
        // to reflect that indentation.
        if delta_for_start_row > 0 {
            start_row += 1;
            selection.start.column += delta_for_start_row;
            if selection.end.row == selection.start.row {
                selection.end.column += delta_for_start_row;
            }
        }

        let mut delta_for_end_row = 0;
        let has_multiple_rows = start_row + 1 != end_row;
        for row in start_row..end_row {
            let current_indent = snapshot.indent_size_for_line(MultiBufferRow(row));
            let indent_delta = match (current_indent.kind, indent_kind) {
                (IndentKind::Space, IndentKind::Space) => {
                    let columns_to_next_tab_stop = tab_size - (current_indent.len % tab_size);
                    IndentSize::spaces(columns_to_next_tab_stop)
                }
                (IndentKind::Tab, IndentKind::Space) => IndentSize::spaces(tab_size),
                (_, IndentKind::Tab) => IndentSize::tab(),
            };

            let start = if has_multiple_rows || current_indent.len < selection.start.column {
                0
            } else {
                selection.start.column
            };
            let row_start = Point::new(row, start);
            edits.push((
                row_start..row_start,
                indent_delta.chars().collect::<String>(),
            ));

            // Update this selection's endpoints to reflect the indentation.
            if row == selection.start.row {
                selection.start.column += indent_delta.len;
            }
            if row == selection.end.row {
                selection.end.column += indent_delta.len;
                delta_for_end_row = indent_delta.len;
            }
        }

        if selection.start.row == selection.end.row {
            delta_for_start_row + delta_for_end_row
        } else {
            delta_for_end_row
        }
    }

    pub fn outdent(&mut self, _: &Outdent, window: &mut Window, cx: &mut Context<Self>) {
        if self.read_only(cx) {
            return;
        }
        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let selections = self.selections.all::<Point>(&display_map);
        let mut deletion_ranges = Vec::new();
        let mut last_outdent = None;
        {
            let buffer = self.buffer.read(cx);
            let snapshot = buffer.snapshot(cx);
            for selection in &selections {
                let settings = buffer.language_settings_at(selection.start, cx);
                let tab_size = settings.tab_size.get();
                let mut rows = selection.spanned_rows(false, &display_map);

                // Avoid re-outdenting a row that has already been outdented by a
                // previous selection.
                if let Some(last_row) = last_outdent
                    && last_row == rows.start
                {
                    rows.start = rows.start.next_row();
                }
                let has_multiple_rows = rows.len() > 1;
                for row in rows.iter_rows() {
                    let indent_size = snapshot.indent_size_for_line(row);
                    if indent_size.len > 0 {
                        let deletion_len = match indent_size.kind {
                            IndentKind::Space => {
                                let columns_to_prev_tab_stop = indent_size.len % tab_size;
                                if columns_to_prev_tab_stop == 0 {
                                    tab_size
                                } else {
                                    columns_to_prev_tab_stop
                                }
                            }
                            IndentKind::Tab => 1,
                        };
                        let start = if has_multiple_rows
                            || deletion_len > selection.start.column
                            || indent_size.len < selection.start.column
                        {
                            0
                        } else {
                            selection.start.column - deletion_len
                        };
                        deletion_ranges.push(
                            Point::new(row.0, start)..Point::new(row.0, start + deletion_len),
                        );
                        last_outdent = Some(row);
                    }
                }
            }
        }

        self.transact(window, cx, |this, window, cx| {
            this.buffer.update(cx, |buffer, cx| {
                let empty_str: Arc<str> = Arc::default();
                buffer.edit(
                    deletion_ranges
                        .into_iter()
                        .map(|range| (range, empty_str.clone())),
                    None,
                    cx,
                );
            });
            let selections = this
                .selections
                .all::<MultiBufferOffset>(&this.display_snapshot(cx));
            this.change_selections(Default::default(), window, cx, |s| s.select(selections));
        });
    }

    pub fn autoindent(&mut self, _: &AutoIndent, window: &mut Window, cx: &mut Context<Self>) {
        if self.read_only(cx) {
            return;
        }
        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let selections = self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx))
            .into_iter()
            .map(|s| s.range());

        self.transact(window, cx, |this, window, cx| {
            this.buffer.update(cx, |buffer, cx| {
                buffer.autoindent_ranges(selections, cx);
            });
            let selections = this
                .selections
                .all::<MultiBufferOffset>(&this.display_snapshot(cx));
            this.change_selections(Default::default(), window, cx, |s| s.select(selections));
        });
    }

    pub fn delete_line(&mut self, _: &DeleteLine, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let selections = self.selections.all::<Point>(&display_map);

        let mut new_cursors = Vec::new();
        let mut edit_ranges = Vec::new();
        let mut selections = selections.iter().peekable();
        while let Some(selection) = selections.next() {
            let mut rows = selection.spanned_rows(false, &display_map);

            // Accumulate contiguous regions of rows that we want to delete.
            while let Some(next_selection) = selections.peek() {
                let next_rows = next_selection.spanned_rows(false, &display_map);
                if next_rows.start <= rows.end {
                    rows.end = next_rows.end;
                    selections.next().unwrap();
                } else {
                    break;
                }
            }

            let buffer = display_map.buffer_snapshot();
            let mut edit_start = ToOffset::to_offset(&Point::new(rows.start.0, 0), buffer);
            let (edit_end, target_row) = if buffer.max_point().row >= rows.end.0 {
                // If there's a line after the range, delete the \n from the end of the row range
                (
                    ToOffset::to_offset(&Point::new(rows.end.0, 0), buffer),
                    rows.end,
                )
            } else {
                // If there isn't a line after the range, delete the \n from the line before the
                // start of the row range
                edit_start = edit_start.saturating_sub_usize(1);
                (buffer.len(), rows.start.previous_row())
            };

            let text_layout_details = self.text_layout_details(window, cx);
            let x = display_map.x_for_display_point(
                selection.head().to_display_point(&display_map),
                &text_layout_details,
            );
            let row = Point::new(target_row.0, 0)
                .to_display_point(&display_map)
                .row();
            let column = display_map.display_column_for_x(row, x, &text_layout_details);

            new_cursors.push((
                selection.id,
                buffer.anchor_after(DisplayPoint::new(row, column).to_point(&display_map)),
                SelectionGoal::None,
            ));
            edit_ranges.push(edit_start..edit_end);
        }

        self.transact(window, cx, |this, window, cx| {
            let buffer = this.buffer.update(cx, |buffer, cx| {
                let empty_str: Arc<str> = Arc::default();
                buffer.edit(
                    edit_ranges
                        .into_iter()
                        .map(|range| (range, empty_str.clone())),
                    None,
                    cx,
                );
                buffer.snapshot(cx)
            });
            let new_selections = new_cursors
                .into_iter()
                .map(|(id, cursor, goal)| {
                    let cursor = cursor.to_point(&buffer);
                    Selection {
                        id,
                        start: cursor,
                        end: cursor,
                        reversed: false,
                        goal,
                    }
                })
                .collect();

            this.change_selections(Default::default(), window, cx, |s| {
                s.select(new_selections);
            });
        });
    }

    pub fn join_lines_impl(
        &mut self,
        insert_whitespace: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.read_only(cx) {
            return;
        }
        let mut row_ranges = Vec::<Range<MultiBufferRow>>::new();
        for selection in self.selections.all::<Point>(&self.display_snapshot(cx)) {
            let start = MultiBufferRow(selection.start.row);
            // Treat single line selections as if they include the next line. Otherwise this action
            // would do nothing for single line selections individual cursors.
            let end = if selection.start.row == selection.end.row {
                MultiBufferRow(selection.start.row + 1)
            } else if selection.end.column == 0 {
                // If the selection ends at the start of a line, it's logically at the end of the
                // previous line (plus its newline).
                // Don't include the end line unless there's only one line selected.
                if selection.start.row + 1 == selection.end.row {
                    MultiBufferRow(selection.end.row)
                } else {
                    MultiBufferRow(selection.end.row - 1)
                }
            } else {
                MultiBufferRow(selection.end.row)
            };

            if let Some(last_row_range) = row_ranges.last_mut()
                && start <= last_row_range.end
            {
                last_row_range.end = end;
                continue;
            }
            row_ranges.push(start..end);
        }

        let snapshot = self.buffer.read(cx).snapshot(cx);
        let mut cursor_positions = Vec::new();
        for row_range in &row_ranges {
            let anchor = snapshot.anchor_before(Point::new(
                row_range.end.previous_row().0,
                snapshot.line_len(row_range.end.previous_row()),
            ));
            cursor_positions.push(anchor..anchor);
        }

        self.transact(window, cx, |this, window, cx| {
            for row_range in row_ranges.into_iter().rev() {
                for row in row_range.iter_rows().rev() {
                    let end_of_line = Point::new(row.0, snapshot.line_len(row));
                    let next_line_row = row.next_row();
                    let indent = snapshot.indent_size_for_line(next_line_row);
                    let mut join_start_column = indent.len;

                    if let Some(language_scope) =
                        snapshot.language_scope_at(Point::new(next_line_row.0, indent.len))
                    {
                        let line_end =
                            Point::new(next_line_row.0, snapshot.line_len(next_line_row));
                        let line_text_after_indent = snapshot
                            .text_for_range(Point::new(next_line_row.0, indent.len)..line_end)
                            .collect::<String>();

                        if !line_text_after_indent.is_empty() {
                            let block_prefix = language_scope
                                .block_comment()
                                .map(|c| c.prefix.as_ref())
                                .filter(|p| !p.is_empty());
                            let doc_prefix = language_scope
                                .documentation_comment()
                                .map(|c| c.prefix.as_ref())
                                .filter(|p| !p.is_empty());
                            let all_prefixes = language_scope
                                .line_comment_prefixes()
                                .iter()
                                .map(|p| p.as_ref())
                                .chain(block_prefix)
                                .chain(doc_prefix)
                                .chain(language_scope.unordered_list().iter().map(|p| p.as_ref()));

                            let mut longest_prefix_len = None;
                            for prefix in all_prefixes {
                                let trimmed = prefix.trim_end();
                                if line_text_after_indent.starts_with(trimmed) {
                                    let candidate_len =
                                        if line_text_after_indent.starts_with(prefix) {
                                            prefix.len()
                                        } else {
                                            trimmed.len()
                                        };
                                    if longest_prefix_len.map_or(true, |len| candidate_len > len) {
                                        longest_prefix_len = Some(candidate_len);
                                    }
                                }
                            }

                            if let Some(prefix_len) = longest_prefix_len {
                                join_start_column =
                                    join_start_column.saturating_add(prefix_len as u32);
                            }
                        }
                    }

                    let start_of_next_line = Point::new(next_line_row.0, join_start_column);

                    let replace = if snapshot.line_len(next_line_row) > join_start_column
                        && insert_whitespace
                    {
                        " "
                    } else {
                        ""
                    };

                    this.buffer.update(cx, |buffer, cx| {
                        buffer.edit([(end_of_line..start_of_next_line, replace)], None, cx)
                    });
                }
            }

            this.change_selections(Default::default(), window, cx, |s| {
                s.select_anchor_ranges(cursor_positions)
            });
        });
    }

    pub fn join_lines(&mut self, _: &JoinLines, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.join_lines_impl(true, window, cx);
    }

    pub fn sort_lines_case_sensitive(
        &mut self,
        _: &SortLinesCaseSensitive,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_immutable_lines(window, cx, |lines| lines.sort())
    }

    pub fn sort_lines_by_length(
        &mut self,
        _: &SortLinesByLength,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_immutable_lines(window, cx, |lines| {
            lines.sort_by_key(|&line| line.chars().count())
        })
    }

    pub fn sort_lines_case_insensitive(
        &mut self,
        _: &SortLinesCaseInsensitive,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_immutable_lines(window, cx, |lines| {
            lines.sort_by_key(|line| line.to_lowercase())
        })
    }

    pub fn unique_lines_case_insensitive(
        &mut self,
        _: &UniqueLinesCaseInsensitive,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_immutable_lines(window, cx, |lines| {
            let mut seen = HashSet::default();
            lines.retain(|line| seen.insert(line.to_lowercase()));
        })
    }

    pub fn unique_lines_case_sensitive(
        &mut self,
        _: &UniqueLinesCaseSensitive,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_immutable_lines(window, cx, |lines| {
            let mut seen = HashSet::default();
            lines.retain(|line| seen.insert(*line));
        })
    }

    fn enable_wrap_selections_in_tag(&self, cx: &App) -> bool {
        let snapshot = self.buffer.read(cx).snapshot(cx);
        for selection in self.selections.disjoint_anchors_arc().iter() {
            if snapshot
                .language_at(selection.start)
                .and_then(|lang| lang.config().wrap_characters.as_ref())
                .is_some()
            {
                return true;
            }
        }
        false
    }

    fn wrap_selections_in_tag(
        &mut self,
        _: &WrapSelectionsInTag,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        let snapshot = self.buffer.read(cx).snapshot(cx);

        let mut edits = Vec::new();
        let mut boundaries = Vec::new();

        for selection in self
            .selections
            .all_adjusted(&self.display_snapshot(cx))
            .iter()
        {
            let Some(wrap_config) = snapshot
                .language_at(selection.start)
                .and_then(|lang| lang.config().wrap_characters.clone())
            else {
                continue;
            };

            let open_tag = format!("{}{}", wrap_config.start_prefix, wrap_config.start_suffix);
            let close_tag = format!("{}{}", wrap_config.end_prefix, wrap_config.end_suffix);

            let start_before = snapshot.anchor_before(selection.start);
            let end_after = snapshot.anchor_after(selection.end);

            edits.push((start_before..start_before, open_tag));
            edits.push((end_after..end_after, close_tag));

            boundaries.push((
                start_before,
                end_after,
                wrap_config.start_prefix.len(),
                wrap_config.end_suffix.len(),
            ));
        }

        if edits.is_empty() {
            return;
        }

        self.transact(window, cx, |this, window, cx| {
            let buffer = this.buffer.update(cx, |buffer, cx| {
                buffer.edit(edits, None, cx);
                buffer.snapshot(cx)
            });

            let mut new_selections = Vec::with_capacity(boundaries.len() * 2);
            for (start_before, end_after, start_prefix_len, end_suffix_len) in
                boundaries.into_iter()
            {
                let open_offset = start_before.to_offset(&buffer) + start_prefix_len;
                let close_offset = end_after
                    .to_offset(&buffer)
                    .saturating_sub_usize(end_suffix_len);
                new_selections.push(open_offset..open_offset);
                new_selections.push(close_offset..close_offset);
            }

            this.change_selections(Default::default(), window, cx, |s| {
                s.select_ranges(new_selections);
            });

            this.request_autoscroll(Autoscroll::fit(), cx);
        });
    }

    pub fn toggle_read_only(
        &mut self,
        _: &workspace::ToggleReadOnlyFile,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(buffer) = self.buffer.read(cx).as_singleton() {
            buffer.update(cx, |buffer, cx| {
                buffer.set_capability(
                    match buffer.capability() {
                        Capability::ReadWrite => Capability::Read,
                        Capability::Read => Capability::ReadWrite,
                        Capability::ReadOnly => Capability::ReadOnly,
                    },
                    cx,
                );
            })
        }
    }

    pub fn reload_file(&mut self, _: &ReloadFile, window: &mut Window, cx: &mut Context<Self>) {
        let Some(project) = self.project.clone() else {
            return;
        };
        let task = self.reload(project, window, cx);
        self.detach_and_notify_err(task, window, cx);
    }

    pub fn restore_file(
        &mut self,
        _: &::git::RestoreFile,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let mut buffer_ids = HashSet::default();
        let snapshot = self.buffer().read(cx).snapshot(cx);
        for selection in self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx))
        {
            buffer_ids.extend(snapshot.buffer_ids_for_range(selection.range()))
        }

        let ranges = buffer_ids
            .into_iter()
            .flat_map(|buffer_id| snapshot.range_for_buffer(buffer_id))
            .collect::<Vec<_>>();

        self.restore_hunks_in_ranges(ranges, window, cx);
    }

    pub fn git_restore(&mut self, _: &Restore, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let selections = self
            .selections
            .all(&self.display_snapshot(cx))
            .into_iter()
            .map(|s| s.range())
            .collect();
        self.restore_hunks_in_ranges(selections, window, cx);
    }

    /// Restores the diff hunks in the editor's selections and moves the cursor
    /// to the next diff hunk. Wraps around to the beginning of the buffer if
    /// not all diff hunks are expanded.
    pub fn restore_and_next(
        &mut self,
        _: &::git::RestoreAndNext,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let selections = self
            .selections
            .all(&self.display_snapshot(cx))
            .into_iter()
            .map(|selection| selection.range())
            .collect();

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.restore_hunks_in_ranges(selections, window, cx);

        let all_diff_hunks_expanded = self.buffer().read(cx).all_diff_hunks_expanded();
        let wrap_around = !all_diff_hunks_expanded;
        let snapshot = self.snapshot(window, cx);
        let position = self
            .selections
            .newest::<Point>(&snapshot.display_snapshot)
            .head();

        self.go_to_hunk_before_or_after_position(
            &snapshot,
            position,
            Direction::Next,
            wrap_around,
            window,
            cx,
        );
    }

    pub fn restore_hunks_in_ranges(
        &mut self,
        ranges: Vec<Range<Point>>,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) {
        if self.delegate_stage_and_restore {
            let hunks = self.snapshot(window, cx).hunks_for_ranges(ranges);
            if !hunks.is_empty() {
                cx.emit(EditorEvent::RestoreRequested { hunks });
            }
            return;
        }
        let hunks = self.snapshot(window, cx).hunks_for_ranges(ranges);
        self.transact(window, cx, |editor, window, cx| {
            editor.restore_diff_hunks(hunks, cx);
            let selections = editor
                .selections
                .all::<MultiBufferOffset>(&editor.display_snapshot(cx));
            editor.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                s.select(selections);
            });
        });
    }

    pub(crate) fn restore_diff_hunks(&self, hunks: Vec<MultiBufferDiffHunk>, cx: &mut App) {
        let mut revert_changes = HashMap::default();
        let chunk_by = hunks.into_iter().chunk_by(|hunk| hunk.buffer_id);
        for (buffer_id, hunks) in &chunk_by {
            let hunks = hunks.collect::<Vec<_>>();
            for hunk in &hunks {
                self.prepare_restore_change(&mut revert_changes, hunk, cx);
            }
            self.do_stage_or_unstage(false, buffer_id, hunks.into_iter(), cx);
        }
        if !revert_changes.is_empty() {
            self.buffer().update(cx, |multi_buffer, cx| {
                for (buffer_id, changes) in revert_changes {
                    if let Some(buffer) = multi_buffer.buffer(buffer_id) {
                        buffer.update(cx, |buffer, cx| {
                            buffer.edit(
                                changes
                                    .into_iter()
                                    .map(|(range, text)| (range, text.to_string())),
                                None,
                                cx,
                            );
                        });
                    }
                }
            });
        }
    }

    pub fn status_for_buffer_id(&self, buffer_id: BufferId, cx: &App) -> Option<FileStatus> {
        if let Some(status) = self
            .addons
            .iter()
            .find_map(|(_, addon)| addon.override_status_for_buffer_id(buffer_id, cx))
        {
            return Some(status);
        }
        self.project
            .as_ref()?
            .read(cx)
            .status_for_buffer_id(buffer_id, cx)
    }

    pub fn open_active_item_in_terminal(
        &mut self,
        _: &OpenInTerminal,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(working_directory) = self.active_buffer(cx).and_then(|buffer| {
            let project_path = buffer.read(cx).project_path(cx)?;
            let project = self.project()?.read(cx);
            let entry = project.entry_for_path(&project_path, cx)?;
            let parent = match &entry.canonical_path {
                Some(canonical_path) => canonical_path.to_path_buf(),
                None => project.absolute_path(&project_path, cx)?,
            }
            .parent()?
            .to_path_buf();
            Some(parent)
        }) {
            window.dispatch_action(
                OpenTerminal {
                    working_directory,
                    local: false,
                }
                .boxed_clone(),
                cx,
            );
        }
    }

    fn set_breakpoint_context_menu(
        &mut self,
        display_row: DisplayRow,
        position: Option<Anchor>,
        clicked_point: gpui::Point<Pixels>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let source = self
            .buffer
            .read(cx)
            .snapshot(cx)
            .anchor_before(Point::new(display_row.0, 0u32));

        let context_menu = self.breakpoint_context_menu(position.unwrap_or(source), window, cx);

        self.mouse_context_menu = MouseContextMenu::pinned_to_editor(
            self,
            source,
            clicked_point,
            context_menu,
            window,
            cx,
        );
    }

    fn add_edit_breakpoint_block(
        &mut self,
        anchor: Anchor,
        breakpoint: &Breakpoint,
        edit_action: BreakpointPromptEditAction,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let weak_editor = cx.weak_entity();
        let bp_prompt = cx.new(|cx| {
            BreakpointPromptEditor::new(
                weak_editor,
                anchor,
                breakpoint.clone(),
                edit_action,
                window,
                cx,
            )
        });

        let height = bp_prompt.update(cx, |this, cx| {
            this.prompt
                .update(cx, |prompt, cx| prompt.max_point(cx).row().0 + 1 + 2)
        });
        let cloned_prompt = bp_prompt.clone();
        let blocks = vec![BlockProperties {
            style: BlockStyle::Sticky,
            placement: BlockPlacement::Above(anchor),
            height: Some(height),
            render: Arc::new(move |cx| {
                *cloned_prompt.read(cx).editor_margins.lock() = *cx.margins;
                cloned_prompt.clone().into_any_element()
            }),
            priority: 0,
        }];

        let focus_handle = bp_prompt.focus_handle(cx);
        window.focus(&focus_handle, cx);

        let block_ids = self.insert_blocks(blocks, None, cx);
        bp_prompt.update(cx, |prompt, _| {
            prompt.add_block_ids(block_ids);
        });
    }

    pub(crate) fn breakpoint_at_row(
        &self,
        row: u32,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<(Anchor, Breakpoint)> {
        let snapshot = self.snapshot(window, cx);
        let breakpoint_position = snapshot.buffer_snapshot().anchor_before(Point::new(row, 0));

        self.breakpoint_at_anchor(breakpoint_position, &snapshot, cx)
    }

    pub(crate) fn breakpoint_at_anchor(
        &self,
        breakpoint_position: Anchor,
        snapshot: &EditorSnapshot,
        cx: &mut Context<Self>,
    ) -> Option<(Anchor, Breakpoint)> {
        let (breakpoint_position, _) = snapshot
            .buffer_snapshot()
            .anchor_to_buffer_anchor(breakpoint_position)?;
        let buffer = self.buffer.read(cx).buffer(breakpoint_position.buffer_id)?;

        let buffer_snapshot = buffer.read(cx).snapshot();

        let row = buffer_snapshot
            .summary_for_anchor::<text::PointUtf16>(&breakpoint_position)
            .row;

        let line_len = buffer_snapshot.line_len(row);
        let anchor_end = buffer_snapshot.anchor_after(Point::new(row, line_len));

        self.breakpoint_store
            .as_ref()?
            .read_with(cx, |breakpoint_store, cx| {
                breakpoint_store
                    .breakpoints(
                        &buffer,
                        Some(breakpoint_position..anchor_end),
                        &buffer_snapshot,
                        cx,
                    )
                    .next()
                    .and_then(|(bp, _)| {
                        let breakpoint_row = buffer_snapshot
                            .summary_for_anchor::<text::PointUtf16>(&bp.position)
                            .row;

                        if breakpoint_row == row {
                            snapshot
                                .buffer_snapshot()
                                .anchor_in_excerpt(bp.position)
                                .map(|position| (position, bp.bp.clone()))
                        } else {
                            None
                        }
                    })
            })
    }

    pub fn edit_log_breakpoint(
        &mut self,
        _: &EditLogBreakpoint,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.breakpoint_store.is_none() {
            return;
        }

        for (anchor, breakpoint) in self.breakpoints_at_cursors(window, cx) {
            let breakpoint = breakpoint.unwrap_or_else(|| Breakpoint {
                message: None,
                state: BreakpointState::Enabled,
                condition: None,
                hit_condition: None,
            });

            self.add_edit_breakpoint_block(
                anchor,
                &breakpoint,
                BreakpointPromptEditAction::Log,
                window,
                cx,
            );
        }
    }

    fn breakpoints_at_cursors(
        &self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Vec<(Anchor, Option<Breakpoint>)> {
        let snapshot = self.snapshot(window, cx);
        let cursors = self
            .selections
            .disjoint_anchors_arc()
            .iter()
            .map(|selection| {
                let cursor_position: Point = selection.head().to_point(&snapshot.buffer_snapshot());

                let breakpoint_position = self
                    .breakpoint_at_row(cursor_position.row, window, cx)
                    .map(|bp| bp.0)
                    .unwrap_or_else(|| {
                        snapshot
                            .display_snapshot
                            .buffer_snapshot()
                            .anchor_after(Point::new(cursor_position.row, 0))
                    });

                let breakpoint = self
                    .breakpoint_at_anchor(breakpoint_position, &snapshot, cx)
                    .map(|(anchor, breakpoint)| (anchor, Some(breakpoint)));

                breakpoint.unwrap_or_else(|| (breakpoint_position, None))
            })
            // There might be multiple cursors on the same line; all of them should have the same anchors though as their breakpoints positions, which makes it possible to sort and dedup the list.
            .collect::<HashMap<Anchor, _>>();

        cursors.into_iter().collect()
    }

    pub fn enable_breakpoint(
        &mut self,
        _: &crate::actions::EnableBreakpoint,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.breakpoint_store.is_none() {
            return;
        }

        for (anchor, breakpoint) in self.breakpoints_at_cursors(window, cx) {
            let Some(breakpoint) = breakpoint.filter(|breakpoint| breakpoint.is_disabled()) else {
                continue;
            };
            self.edit_breakpoint_at_anchor(
                anchor,
                breakpoint,
                BreakpointEditAction::InvertState,
                cx,
            );
        }
    }

    pub fn align_selections(
        &mut self,
        _: &crate::actions::AlignSelections,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        let display_snapshot = self.display_snapshot(cx);

        struct CursorData {
            anchor: Anchor,
            point: Point,
        }
        let cursor_data: Vec<CursorData> = self
            .selections
            .disjoint_anchors()
            .iter()
            .map(|selection| {
                let anchor = if selection.reversed {
                    selection.head()
                } else {
                    selection.tail()
                };
                CursorData {
                    anchor: anchor,
                    point: anchor.to_point(&display_snapshot.buffer_snapshot()),
                }
            })
            .collect();

        let rows_anchors_count: Vec<usize> = cursor_data
            .iter()
            .map(|cursor| cursor.point.row)
            .chunk_by(|&row| row)
            .into_iter()
            .map(|(_, group)| group.count())
            .collect();
        let max_columns = rows_anchors_count.iter().max().copied().unwrap_or(0);
        let mut rows_column_offset = vec![0; rows_anchors_count.len()];
        let mut edits = Vec::new();

        for column_idx in 0..max_columns {
            let mut cursor_index = 0;

            // Calculate target_column => position that the selections will go
            let mut target_column = 0;
            for (row_idx, cursor_count) in rows_anchors_count.iter().enumerate() {
                // Skip rows that don't have this column
                if column_idx >= *cursor_count {
                    cursor_index += cursor_count;
                    continue;
                }

                let point = &cursor_data[cursor_index + column_idx].point;
                let adjusted_column = point.column + rows_column_offset[row_idx];
                if adjusted_column > target_column {
                    target_column = adjusted_column;
                }
                cursor_index += cursor_count;
            }

            // Collect edits for this column
            cursor_index = 0;
            for (row_idx, cursor_count) in rows_anchors_count.iter().enumerate() {
                // Skip rows that don't have this column
                if column_idx >= *cursor_count {
                    cursor_index += *cursor_count;
                    continue;
                }

                let point = &cursor_data[cursor_index + column_idx].point;
                let spaces_needed = target_column - point.column - rows_column_offset[row_idx];
                if spaces_needed > 0 {
                    let anchor = cursor_data[cursor_index + column_idx]
                        .anchor
                        .bias_left(&display_snapshot);
                    edits.push((anchor..anchor, " ".repeat(spaces_needed as usize)));
                }
                rows_column_offset[row_idx] += spaces_needed;

                cursor_index += *cursor_count;
            }
        }

        if !edits.is_empty() {
            self.transact(window, cx, |editor, _window, cx| {
                editor.edit(edits, cx);
            });
        }
    }

    pub fn disable_breakpoint(
        &mut self,
        _: &crate::actions::DisableBreakpoint,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.breakpoint_store.is_none() {
            return;
        }

        for (anchor, breakpoint) in self.breakpoints_at_cursors(window, cx) {
            let Some(breakpoint) = breakpoint.filter(|breakpoint| breakpoint.is_enabled()) else {
                continue;
            };
            self.edit_breakpoint_at_anchor(
                anchor,
                breakpoint,
                BreakpointEditAction::InvertState,
                cx,
            );
        }
    }

    pub fn toggle_breakpoint(
        &mut self,
        _: &crate::actions::ToggleBreakpoint,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.breakpoint_store.is_none() {
            return;
        }

        let snapshot = self.snapshot(window, cx);
        for (anchor, breakpoint) in self.breakpoints_at_cursors(window, cx) {
            if self.gutter_breakpoint_indicator.0.is_some() {
                let display_row = anchor
                    .to_point(snapshot.buffer_snapshot())
                    .to_display_point(&snapshot.display_snapshot)
                    .row();
                self.update_breakpoint_collision_on_toggle(
                    display_row,
                    &BreakpointEditAction::Toggle,
                );
            }

            if let Some(breakpoint) = breakpoint {
                self.edit_breakpoint_at_anchor(
                    anchor,
                    breakpoint,
                    BreakpointEditAction::Toggle,
                    cx,
                );
            } else {
                self.edit_breakpoint_at_anchor(
                    anchor,
                    Breakpoint::new_standard(),
                    BreakpointEditAction::Toggle,
                    cx,
                );
            }
        }
    }

    fn update_breakpoint_collision_on_toggle(
        &mut self,
        display_row: DisplayRow,
        edit_action: &BreakpointEditAction,
    ) {
        if let Some(ref mut breakpoint_indicator) = self.gutter_breakpoint_indicator.0 {
            if breakpoint_indicator.display_row == display_row
                && matches!(edit_action, BreakpointEditAction::Toggle)
            {
                breakpoint_indicator.collides_with_existing_breakpoint =
                    !breakpoint_indicator.collides_with_existing_breakpoint;
            }
        }
    }

    pub fn edit_breakpoint_at_anchor(
        &mut self,
        breakpoint_position: Anchor,
        breakpoint: Breakpoint,
        edit_action: BreakpointEditAction,
        cx: &mut Context<Self>,
    ) {
        let Some(breakpoint_store) = &self.breakpoint_store else {
            return;
        };
        let buffer_snapshot = self.buffer.read(cx).snapshot(cx);
        let Some((position, _)) = buffer_snapshot.anchor_to_buffer_anchor(breakpoint_position)
        else {
            return;
        };
        let Some(buffer) = self.buffer.read(cx).buffer(position.buffer_id) else {
            return;
        };

        breakpoint_store.update(cx, |breakpoint_store, cx| {
            breakpoint_store.toggle_breakpoint(
                buffer,
                BreakpointWithPosition {
                    position,
                    bp: breakpoint,
                },
                edit_action,
                cx,
            );
        });

        cx.notify();
    }

    #[cfg(any(test, feature = "test-support"))]
    pub fn breakpoint_store(&self) -> Option<Entity<BreakpointStore>> {
        self.breakpoint_store.clone()
    }

    pub fn prepare_restore_change(
        &self,
        revert_changes: &mut HashMap<BufferId, Vec<(Range<text::Anchor>, Rope)>>,
        hunk: &MultiBufferDiffHunk,
        cx: &mut App,
    ) -> Option<()> {
        if hunk.is_created_file() {
            return None;
        }
        let multi_buffer = self.buffer.read(cx);
        let multi_buffer_snapshot = multi_buffer.snapshot(cx);
        let diff_snapshot = multi_buffer_snapshot.diff_for_buffer_id(hunk.buffer_id)?;
        let original_text = diff_snapshot
            .base_text()
            .as_rope()
            .slice(hunk.diff_base_byte_range.start.0..hunk.diff_base_byte_range.end.0);
        let buffer = multi_buffer.buffer(hunk.buffer_id)?;
        let buffer = buffer.read(cx);
        let buffer_snapshot = buffer.snapshot();
        let buffer_revert_changes = revert_changes.entry(buffer.remote_id()).or_default();
        if let Err(i) = buffer_revert_changes.binary_search_by(|probe| {
            probe
                .0
                .start
                .cmp(&hunk.buffer_range.start, &buffer_snapshot)
                .then(probe.0.end.cmp(&hunk.buffer_range.end, &buffer_snapshot))
        }) {
            buffer_revert_changes.insert(i, (hunk.buffer_range.clone(), original_text));
            Some(())
        } else {
            None
        }
    }

    pub fn reverse_lines(&mut self, _: &ReverseLines, window: &mut Window, cx: &mut Context<Self>) {
        self.manipulate_immutable_lines(window, cx, |lines| lines.reverse())
    }

    pub fn shuffle_lines(&mut self, _: &ShuffleLines, window: &mut Window, cx: &mut Context<Self>) {
        self.manipulate_immutable_lines(window, cx, |lines| lines.shuffle(&mut rand::rng()))
    }

    pub fn rotate_selections_forward(
        &mut self,
        _: &RotateSelectionsForward,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.rotate_selections(window, cx, false)
    }

    pub fn rotate_selections_backward(
        &mut self,
        _: &RotateSelectionsBackward,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.rotate_selections(window, cx, true)
    }

    fn rotate_selections(&mut self, window: &mut Window, cx: &mut Context<Self>, reverse: bool) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let display_snapshot = self.display_snapshot(cx);
        let selections = self.selections.all::<MultiBufferOffset>(&display_snapshot);

        if selections.len() < 2 {
            return;
        }

        let (edits, new_selections) = {
            let buffer = self.buffer.read(cx).read(cx);
            let has_selections = selections.iter().any(|s| !s.is_empty());
            if has_selections {
                let mut selected_texts: Vec<String> = selections
                    .iter()
                    .map(|selection| {
                        buffer
                            .text_for_range(selection.start..selection.end)
                            .collect()
                    })
                    .collect();

                if reverse {
                    selected_texts.rotate_left(1);
                } else {
                    selected_texts.rotate_right(1);
                }

                let mut offset_delta: i64 = 0;
                let mut new_selections = Vec::new();
                let edits: Vec<_> = selections
                    .iter()
                    .zip(selected_texts.iter())
                    .map(|(selection, new_text)| {
                        let old_len = (selection.end.0 - selection.start.0) as i64;
                        let new_len = new_text.len() as i64;
                        let adjusted_start =
                            MultiBufferOffset((selection.start.0 as i64 + offset_delta) as usize);
                        let adjusted_end =
                            MultiBufferOffset((adjusted_start.0 as i64 + new_len) as usize);

                        new_selections.push(Selection {
                            id: selection.id,
                            start: adjusted_start,
                            end: adjusted_end,
                            reversed: selection.reversed,
                            goal: selection.goal,
                        });

                        offset_delta += new_len - old_len;
                        (selection.start..selection.end, new_text.clone())
                    })
                    .collect();
                (edits, new_selections)
            } else {
                let mut all_rows: Vec<u32> = selections
                    .iter()
                    .map(|selection| buffer.offset_to_point(selection.start).row)
                    .collect();
                all_rows.sort_unstable();
                all_rows.dedup();

                if all_rows.len() < 2 {
                    return;
                }

                let line_ranges: Vec<Range<MultiBufferOffset>> = all_rows
                    .iter()
                    .map(|&row| {
                        let start = Point::new(row, 0);
                        let end = Point::new(row, buffer.line_len(MultiBufferRow(row)));
                        buffer.point_to_offset(start)..buffer.point_to_offset(end)
                    })
                    .collect();

                let mut line_texts: Vec<String> = line_ranges
                    .iter()
                    .map(|range| buffer.text_for_range(range.clone()).collect())
                    .collect();

                if reverse {
                    line_texts.rotate_left(1);
                } else {
                    line_texts.rotate_right(1);
                }

                let edits = line_ranges
                    .iter()
                    .zip(line_texts.iter())
                    .map(|(range, new_text)| (range.clone(), new_text.clone()))
                    .collect();

                let num_rows = all_rows.len();
                let row_to_index: std::collections::HashMap<u32, usize> = all_rows
                    .iter()
                    .enumerate()
                    .map(|(i, &row)| (row, i))
                    .collect();

                // Compute new line start offsets after rotation (handles CRLF)
                let newline_len = line_ranges[1].start.0 - line_ranges[0].end.0;
                let first_line_start = line_ranges[0].start.0;
                let mut new_line_starts: Vec<usize> = vec![first_line_start];
                for text in line_texts.iter().take(num_rows - 1) {
                    let prev_start = *new_line_starts.last().unwrap();
                    new_line_starts.push(prev_start + text.len() + newline_len);
                }

                let new_selections = selections
                    .iter()
                    .map(|selection| {
                        let point = buffer.offset_to_point(selection.start);
                        let old_index = row_to_index[&point.row];
                        let new_index = if reverse {
                            (old_index + num_rows - 1) % num_rows
                        } else {
                            (old_index + 1) % num_rows
                        };
                        let new_offset =
                            MultiBufferOffset(new_line_starts[new_index] + point.column as usize);
                        Selection {
                            id: selection.id,
                            start: new_offset,
                            end: new_offset,
                            reversed: selection.reversed,
                            goal: selection.goal,
                        }
                    })
                    .collect();

                (edits, new_selections)
            }
        };

        self.transact(window, cx, |this, window, cx| {
            this.buffer.update(cx, |buffer, cx| {
                buffer.edit(edits, None, cx);
            });
            this.change_selections(Default::default(), window, cx, |s| {
                s.select(new_selections);
            });
        });
    }

    fn manipulate_lines<M>(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
        mut manipulate: M,
    ) where
        M: FnMut(&str) -> LineManipulationResult,
    {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = self.buffer.read(cx).snapshot(cx);

        let mut edits = Vec::new();

        let selections = self.selections.all::<Point>(&display_map);
        let mut selections = selections.iter().peekable();
        let mut contiguous_row_selections = Vec::new();
        let mut new_selections = Vec::new();
        let mut added_lines = 0;
        let mut removed_lines = 0;

        while let Some(selection) = selections.next() {
            let (start_row, end_row) = consume_contiguous_rows(
                &mut contiguous_row_selections,
                selection,
                &display_map,
                &mut selections,
            );

            let start_point = Point::new(start_row.0, 0);
            let end_point = Point::new(
                end_row.previous_row().0,
                buffer.line_len(end_row.previous_row()),
            );
            let text = buffer
                .text_for_range(start_point..end_point)
                .collect::<String>();

            let LineManipulationResult {
                new_text,
                line_count_before,
                line_count_after,
            } = manipulate(&text);

            edits.push((start_point..end_point, new_text));

            // Selections must change based on added and removed line count
            let start_row =
                MultiBufferRow(start_point.row + added_lines as u32 - removed_lines as u32);
            let end_row = MultiBufferRow(start_row.0 + line_count_after.saturating_sub(1) as u32);
            new_selections.push(Selection {
                id: selection.id,
                start: start_row,
                end: end_row,
                goal: SelectionGoal::None,
                reversed: selection.reversed,
            });

            if line_count_after > line_count_before {
                added_lines += line_count_after - line_count_before;
            } else if line_count_before > line_count_after {
                removed_lines += line_count_before - line_count_after;
            }
        }

        self.transact(window, cx, |this, window, cx| {
            let buffer = this.buffer.update(cx, |buffer, cx| {
                buffer.edit(edits, None, cx);
                buffer.snapshot(cx)
            });

            // Recalculate offsets on newly edited buffer
            let new_selections = new_selections
                .iter()
                .map(|s| {
                    let start_point = Point::new(s.start.0, 0);
                    let end_point = Point::new(s.end.0, buffer.line_len(s.end));
                    Selection {
                        id: s.id,
                        start: buffer.point_to_offset(start_point),
                        end: buffer.point_to_offset(end_point),
                        goal: s.goal,
                        reversed: s.reversed,
                    }
                })
                .collect();

            this.change_selections(Default::default(), window, cx, |s| {
                s.select(new_selections);
            });

            this.request_autoscroll(Autoscroll::fit(), cx);
        });
    }

    fn manipulate_immutable_lines<Fn>(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
        mut callback: Fn,
    ) where
        Fn: FnMut(&mut Vec<&str>),
    {
        self.manipulate_lines(window, cx, |text| {
            let mut lines: Vec<&str> = text.split('\n').collect();
            let line_count_before = lines.len();

            callback(&mut lines);

            LineManipulationResult {
                new_text: lines.join("\n"),
                line_count_before,
                line_count_after: lines.len(),
            }
        });
    }

    fn manipulate_mutable_lines<Fn>(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
        mut callback: Fn,
    ) where
        Fn: FnMut(&mut Vec<Cow<'_, str>>),
    {
        self.manipulate_lines(window, cx, |text| {
            let mut lines: Vec<Cow<str>> = text.split('\n').map(Cow::from).collect();
            let line_count_before = lines.len();

            callback(&mut lines);

            LineManipulationResult {
                new_text: lines.join("\n"),
                line_count_before,
                line_count_after: lines.len(),
            }
        });
    }

    pub fn convert_indentation_to_spaces(
        &mut self,
        _: &ConvertIndentationToSpaces,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let settings = self.buffer.read(cx).language_settings(cx);
        let tab_size = settings.tab_size.get() as usize;

        self.manipulate_mutable_lines(window, cx, |lines| {
            // Allocates a reasonably sized scratch buffer once for the whole loop
            let mut reindented_line = String::with_capacity(MAX_LINE_LEN);
            // Avoids recomputing spaces that could be inserted many times
            let space_cache: Vec<Vec<char>> = (1..=tab_size)
                .map(|n| IndentSize::spaces(n as u32).chars().collect())
                .collect();

            for line in lines.iter_mut().filter(|line| !line.is_empty()) {
                let mut chars = line.as_ref().chars();
                let mut col = 0;
                let mut changed = false;

                for ch in chars.by_ref() {
                    match ch {
                        ' ' => {
                            reindented_line.push(' ');
                            col += 1;
                        }
                        '\t' => {
                            // \t are converted to spaces depending on the current column
                            let spaces_len = tab_size - (col % tab_size);
                            reindented_line.extend(&space_cache[spaces_len - 1]);
                            col += spaces_len;
                            changed = true;
                        }
                        _ => {
                            // If we dont append before break, the character is consumed
                            reindented_line.push(ch);
                            break;
                        }
                    }
                }

                if !changed {
                    reindented_line.clear();
                    continue;
                }
                // Append the rest of the line and replace old reference with new one
                reindented_line.extend(chars);
                *line = Cow::Owned(reindented_line.clone());
                reindented_line.clear();
            }
        });
    }

    pub fn convert_indentation_to_tabs(
        &mut self,
        _: &ConvertIndentationToTabs,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let settings = self.buffer.read(cx).language_settings(cx);
        let tab_size = settings.tab_size.get() as usize;

        self.manipulate_mutable_lines(window, cx, |lines| {
            // Allocates a reasonably sized buffer once for the whole loop
            let mut reindented_line = String::with_capacity(MAX_LINE_LEN);
            // Avoids recomputing spaces that could be inserted many times
            let space_cache: Vec<Vec<char>> = (1..=tab_size)
                .map(|n| IndentSize::spaces(n as u32).chars().collect())
                .collect();

            for line in lines.iter_mut().filter(|line| !line.is_empty()) {
                let mut chars = line.chars();
                let mut spaces_count = 0;
                let mut first_non_indent_char = None;
                let mut changed = false;

                for ch in chars.by_ref() {
                    match ch {
                        ' ' => {
                            // Keep track of spaces. Append \t when we reach tab_size
                            spaces_count += 1;
                            changed = true;
                            if spaces_count == tab_size {
                                reindented_line.push('\t');
                                spaces_count = 0;
                            }
                        }
                        '\t' => {
                            reindented_line.push('\t');
                            spaces_count = 0;
                        }
                        _ => {
                            // Dont append it yet, we might have remaining spaces
                            first_non_indent_char = Some(ch);
                            break;
                        }
                    }
                }

                if !changed {
                    reindented_line.clear();
                    continue;
                }
                // Remaining spaces that didn't make a full tab stop
                if spaces_count > 0 {
                    reindented_line.extend(&space_cache[spaces_count - 1]);
                }
                // If we consume an extra character that was not indentation, add it back
                if let Some(extra_char) = first_non_indent_char {
                    reindented_line.push(extra_char);
                }
                // Append the rest of the line and replace old reference with new one
                reindented_line.extend(chars);
                *line = Cow::Owned(reindented_line.clone());
                reindented_line.clear();
            }
        });
    }

    pub fn convert_to_upper_case(
        &mut self,
        _: &ConvertToUpperCase,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| text.to_uppercase())
    }

    pub fn convert_to_lower_case(
        &mut self,
        _: &ConvertToLowerCase,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| text.to_lowercase())
    }

    pub fn convert_to_title_case(
        &mut self,
        _: &ConvertToTitleCase,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| {
            Self::convert_text_case(text, Case::Title)
        })
    }

    pub fn convert_to_snake_case(
        &mut self,
        _: &ConvertToSnakeCase,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| {
            Self::convert_text_case(text, Case::Snake)
        })
    }

    pub fn convert_to_kebab_case(
        &mut self,
        _: &ConvertToKebabCase,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| {
            Self::convert_text_case(text, Case::Kebab)
        })
    }

    pub fn convert_to_upper_camel_case(
        &mut self,
        _: &ConvertToUpperCamelCase,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| {
            Self::convert_text_case(text, Case::UpperCamel)
        })
    }

    pub fn convert_to_lower_camel_case(
        &mut self,
        _: &ConvertToLowerCamelCase,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| {
            Self::convert_text_case(text, Case::Camel)
        })
    }

    pub fn convert_to_opposite_case(
        &mut self,
        _: &ConvertToOppositeCase,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| {
            text.chars()
                .fold(String::with_capacity(text.len()), |mut t, c| {
                    if c.is_uppercase() {
                        t.extend(c.to_lowercase());
                    } else {
                        t.extend(c.to_uppercase());
                    }
                    t
                })
        })
    }

    pub fn convert_to_sentence_case(
        &mut self,
        _: &ConvertToSentenceCase,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| {
            Self::convert_text_case(text, Case::Sentence)
        })
    }

    pub fn toggle_case(&mut self, _: &ToggleCase, window: &mut Window, cx: &mut Context<Self>) {
        self.manipulate_text(window, cx, |text| {
            let has_upper_case_characters = text.chars().any(|c| c.is_uppercase());
            if has_upper_case_characters {
                text.to_lowercase()
            } else {
                text.to_uppercase()
            }
        })
    }

    pub fn convert_to_rot13(
        &mut self,
        _: &ConvertToRot13,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| {
            text.chars()
                .map(|c| match c {
                    'A'..='M' | 'a'..='m' => ((c as u8) + 13) as char,
                    'N'..='Z' | 'n'..='z' => ((c as u8) - 13) as char,
                    _ => c,
                })
                .collect()
        })
    }

    fn convert_text_case(text: &str, case: Case) -> String {
        text.lines()
            .map(|line| {
                let trimmed_start = line.trim_start();
                let leading = &line[..line.len() - trimmed_start.len()];
                let trimmed = trimmed_start.trim_end();
                let trailing = &trimmed_start[trimmed.len()..];
                format!("{}{}{}", leading, trimmed.to_case(case), trailing)
            })
            .join("\n")
    }

    pub fn convert_to_rot47(
        &mut self,
        _: &ConvertToRot47,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.manipulate_text(window, cx, |text| {
            text.chars()
                .map(|c| {
                    let code_point = c as u32;
                    if code_point >= 33 && code_point <= 126 {
                        return char::from_u32(33 + ((code_point + 14) % 94)).unwrap();
                    }
                    c
                })
                .collect()
        })
    }

    fn manipulate_text<Fn>(&mut self, window: &mut Window, cx: &mut Context<Self>, mut callback: Fn)
    where
        Fn: FnMut(&str) -> String,
    {
        let buffer = self.buffer.read(cx).snapshot(cx);

        let mut new_selections = Vec::new();
        let mut edits = Vec::new();
        let mut selection_adjustment = 0isize;

        for selection in self.selections.all_adjusted(&self.display_snapshot(cx)) {
            let selection_is_empty = selection.is_empty();

            let (start, end) = if selection_is_empty {
                let (word_range, _) = buffer.surrounding_word(selection.start, None);
                (word_range.start, word_range.end)
            } else {
                (
                    buffer.point_to_offset(selection.start),
                    buffer.point_to_offset(selection.end),
                )
            };

            let text = buffer.text_for_range(start..end).collect::<String>();
            let old_length = text.len() as isize;
            let text = callback(&text);

            new_selections.push(Selection {
                start: MultiBufferOffset((start.0 as isize - selection_adjustment) as usize),
                end: MultiBufferOffset(
                    ((start.0 + text.len()) as isize - selection_adjustment) as usize,
                ),
                goal: SelectionGoal::None,
                id: selection.id,
                reversed: selection.reversed,
            });

            selection_adjustment += old_length - text.len() as isize;

            edits.push((start..end, text));
        }

        self.transact(window, cx, |this, window, cx| {
            this.buffer.update(cx, |buffer, cx| {
                buffer.edit(edits, None, cx);
            });

            this.change_selections(Default::default(), window, cx, |s| {
                s.select(new_selections);
            });

            this.request_autoscroll(Autoscroll::fit(), cx);
        });
    }

    pub fn move_selection_on_drop(
        &mut self,
        selection: &Selection<Anchor>,
        target: DisplayPoint,
        is_cut: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = display_map.buffer_snapshot();
        let mut edits = Vec::new();
        let insert_point = display_map
            .clip_point(target, Bias::Left)
            .to_point(&display_map);
        let text = buffer
            .text_for_range(selection.start..selection.end)
            .collect::<String>();
        if is_cut {
            edits.push(((selection.start..selection.end), String::new()));
        }
        let insert_anchor = buffer.anchor_before(insert_point);
        edits.push(((insert_anchor..insert_anchor), text));
        let last_edit_start = insert_anchor.bias_left(buffer);
        let last_edit_end = insert_anchor.bias_right(buffer);
        self.transact(window, cx, |this, window, cx| {
            this.buffer.update(cx, |buffer, cx| {
                buffer.edit(edits, None, cx);
            });
            this.change_selections(Default::default(), window, cx, |s| {
                s.select_anchor_ranges([last_edit_start..last_edit_end]);
            });
        });
    }

    pub fn clear_selection_drag_state(&mut self) {
        self.selection_drag_state = SelectionDragState::None;
    }

    pub fn duplicate(
        &mut self,
        upwards: bool,
        whole_lines: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = display_map.buffer_snapshot();
        let selections = self.selections.all::<Point>(&display_map);

        let mut edits = Vec::new();
        let mut selections_iter = selections.iter().peekable();
        while let Some(selection) = selections_iter.next() {
            let mut rows = selection.spanned_rows(false, &display_map);
            // duplicate line-wise
            if whole_lines || selection.start == selection.end {
                // Avoid duplicating the same lines twice.
                while let Some(next_selection) = selections_iter.peek() {
                    let next_rows = next_selection.spanned_rows(false, &display_map);
                    if next_rows.start < rows.end {
                        rows.end = next_rows.end;
                        selections_iter.next().unwrap();
                    } else {
                        break;
                    }
                }

                // Copy the text from the selected row region and splice it either at the start
                // or end of the region.
                let start = Point::new(rows.start.0, 0);
                let end = Point::new(
                    rows.end.previous_row().0,
                    buffer.line_len(rows.end.previous_row()),
                );

                let mut text = buffer.text_for_range(start..end).collect::<String>();

                let insert_location = if upwards {
                    // When duplicating upward, we need to insert before the current line.
                    // If we're on the last line and it doesn't end with a newline,
                    // we need to add a newline before the duplicated content.
                    let needs_leading_newline = rows.end.0 >= buffer.max_point().row
                        && buffer.max_point().column > 0
                        && !text.ends_with('\n');

                    if needs_leading_newline {
                        text.insert(0, '\n');
                        end
                    } else {
                        text.push('\n');
                        Point::new(rows.start.0, 0)
                    }
                } else {
                    text.push('\n');
                    start
                };
                edits.push((insert_location..insert_location, text));
            } else {
                // duplicate character-wise
                let start = selection.start;
                let end = selection.end;
                let text = buffer.text_for_range(start..end).collect::<String>();
                edits.push((selection.end..selection.end, text));
            }
        }

        self.transact(window, cx, |this, window, cx| {
            this.buffer.update(cx, |buffer, cx| {
                buffer.edit(edits, None, cx);
            });

            // When duplicating upward with whole lines, move the cursor to the duplicated line
            if upwards && whole_lines {
                let display_map = this.display_map.update(cx, |map, cx| map.snapshot(cx));

                this.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                    let mut new_ranges = Vec::new();
                    let selections = s.all::<Point>(&display_map);
                    let mut selections_iter = selections.iter().peekable();

                    while let Some(first_selection) = selections_iter.next() {
                        // Group contiguous selections together to find the total row span
                        let mut group_selections = vec![first_selection];
                        let mut rows = first_selection.spanned_rows(false, &display_map);

                        while let Some(next_selection) = selections_iter.peek() {
                            let next_rows = next_selection.spanned_rows(false, &display_map);
                            if next_rows.start < rows.end {
                                rows.end = next_rows.end;
                                group_selections.push(selections_iter.next().unwrap());
                            } else {
                                break;
                            }
                        }

                        let row_count = rows.end.0 - rows.start.0;

                        // Move all selections in this group up by the total number of duplicated rows
                        for selection in group_selections {
                            let new_start = Point::new(
                                selection.start.row.saturating_sub(row_count),
                                selection.start.column,
                            );

                            let new_end = Point::new(
                                selection.end.row.saturating_sub(row_count),
                                selection.end.column,
                            );

                            new_ranges.push(new_start..new_end);
                        }
                    }

                    s.select_ranges(new_ranges);
                });
            }

            this.request_autoscroll(Autoscroll::fit(), cx);
        });
    }

    pub fn duplicate_line_up(
        &mut self,
        _: &DuplicateLineUp,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.duplicate(true, true, window, cx);
    }

    pub fn duplicate_line_down(
        &mut self,
        _: &DuplicateLineDown,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.duplicate(false, true, window, cx);
    }

    pub fn duplicate_selection(
        &mut self,
        _: &DuplicateSelection,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.duplicate(false, false, window, cx);
    }

    pub fn move_line_up(&mut self, _: &MoveLineUp, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = self.buffer.read(cx).snapshot(cx);

        let mut edits = Vec::new();
        let mut unfold_ranges = Vec::new();
        let mut refold_creases = Vec::new();

        let selections = self.selections.all::<Point>(&display_map);
        let mut selections = selections.iter().peekable();
        let mut contiguous_row_selections = Vec::new();
        let mut new_selections = Vec::new();

        while let Some(selection) = selections.next() {
            // Find all the selections that span a contiguous row range
            let (start_row, end_row) = consume_contiguous_rows(
                &mut contiguous_row_selections,
                selection,
                &display_map,
                &mut selections,
            );

            // Move the text spanned by the row range to be before the line preceding the row range
            if start_row.0 > 0 {
                let range_to_move = Point::new(
                    start_row.previous_row().0,
                    buffer.line_len(start_row.previous_row()),
                )
                    ..Point::new(
                        end_row.previous_row().0,
                        buffer.line_len(end_row.previous_row()),
                    );
                let insertion_point = display_map
                    .prev_line_boundary(Point::new(start_row.previous_row().0, 0))
                    .0;

                // Don't move lines across excerpts
                if buffer
                    .excerpt_containing(insertion_point..range_to_move.end)
                    .is_some()
                {
                    let text = buffer
                        .text_for_range(range_to_move.clone())
                        .flat_map(|s| s.chars())
                        .skip(1)
                        .chain(['\n'])
                        .collect::<String>();

                    edits.push((
                        buffer.anchor_after(range_to_move.start)
                            ..buffer.anchor_before(range_to_move.end),
                        String::new(),
                    ));
                    let insertion_anchor = buffer.anchor_after(insertion_point);
                    edits.push((insertion_anchor..insertion_anchor, text));

                    let row_delta = range_to_move.start.row - insertion_point.row + 1;

                    // Move selections up
                    new_selections.extend(contiguous_row_selections.drain(..).map(
                        |mut selection| {
                            selection.start.row -= row_delta;
                            selection.end.row -= row_delta;
                            selection
                        },
                    ));

                    // Move folds up
                    unfold_ranges.push(range_to_move.clone());
                    for fold in display_map.folds_in_range(
                        buffer.anchor_before(range_to_move.start)
                            ..buffer.anchor_after(range_to_move.end),
                    ) {
                        let mut start = fold.range.start.to_point(&buffer);
                        let mut end = fold.range.end.to_point(&buffer);
                        start.row -= row_delta;
                        end.row -= row_delta;
                        refold_creases.push(Crease::simple(start..end, fold.placeholder.clone()));
                    }
                }
            }

            // If we didn't move line(s), preserve the existing selections
            new_selections.append(&mut contiguous_row_selections);
        }

        self.transact(window, cx, |this, window, cx| {
            this.unfold_ranges(&unfold_ranges, true, true, cx);
            this.buffer.update(cx, |buffer, cx| {
                for (range, text) in edits {
                    buffer.edit([(range, text)], None, cx);
                }
            });
            this.fold_creases(refold_creases, true, window, cx);
            this.change_selections(Default::default(), window, cx, |s| {
                s.select(new_selections);
            })
        });
    }

    pub fn move_line_down(
        &mut self,
        _: &MoveLineDown,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = self.buffer.read(cx).snapshot(cx);

        let mut edits = Vec::new();
        let mut unfold_ranges = Vec::new();
        let mut refold_creases = Vec::new();

        let selections = self.selections.all::<Point>(&display_map);
        let mut selections = selections.iter().peekable();
        let mut contiguous_row_selections = Vec::new();
        let mut new_selections = Vec::new();

        while let Some(selection) = selections.next() {
            // Find all the selections that span a contiguous row range
            let (start_row, end_row) = consume_contiguous_rows(
                &mut contiguous_row_selections,
                selection,
                &display_map,
                &mut selections,
            );

            // Move the text spanned by the row range to be after the last line of the row range
            if end_row.0 <= buffer.max_point().row {
                let range_to_move =
                    MultiBufferPoint::new(start_row.0, 0)..MultiBufferPoint::new(end_row.0, 0);
                let insertion_point = display_map
                    .next_line_boundary(MultiBufferPoint::new(end_row.0, 0))
                    .0;

                // Don't move lines across excerpt boundaries
                if buffer
                    .excerpt_containing(range_to_move.start..insertion_point)
                    .is_some()
                {
                    let mut text = String::from("\n");
                    text.extend(buffer.text_for_range(range_to_move.clone()));
                    text.pop(); // Drop trailing newline
                    edits.push((
                        buffer.anchor_after(range_to_move.start)
                            ..buffer.anchor_before(range_to_move.end),
                        String::new(),
                    ));
                    let insertion_anchor = buffer.anchor_after(insertion_point);
                    edits.push((insertion_anchor..insertion_anchor, text));

                    let row_delta = insertion_point.row - range_to_move.end.row + 1;

                    // Move selections down
                    new_selections.extend(contiguous_row_selections.drain(..).map(
                        |mut selection| {
                            selection.start.row += row_delta;
                            selection.end.row += row_delta;
                            selection
                        },
                    ));

                    // Move folds down
                    unfold_ranges.push(range_to_move.clone());
                    for fold in display_map.folds_in_range(
                        buffer.anchor_before(range_to_move.start)
                            ..buffer.anchor_after(range_to_move.end),
                    ) {
                        let mut start = fold.range.start.to_point(&buffer);
                        let mut end = fold.range.end.to_point(&buffer);
                        start.row += row_delta;
                        end.row += row_delta;
                        refold_creases.push(Crease::simple(start..end, fold.placeholder.clone()));
                    }
                }
            }

            // If we didn't move line(s), preserve the existing selections
            new_selections.append(&mut contiguous_row_selections);
        }

        self.transact(window, cx, |this, window, cx| {
            this.unfold_ranges(&unfold_ranges, true, true, cx);
            this.buffer.update(cx, |buffer, cx| {
                for (range, text) in edits {
                    buffer.edit([(range, text)], None, cx);
                }
            });
            this.fold_creases(refold_creases, true, window, cx);
            this.change_selections(Default::default(), window, cx, |s| s.select(new_selections));
        });
    }

    pub fn transpose(&mut self, _: &Transpose, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let text_layout_details = &self.text_layout_details(window, cx);
        self.transact(window, cx, |this, window, cx| {
            let edits = this.change_selections(Default::default(), window, cx, |s| {
                let mut edits: Vec<(Range<MultiBufferOffset>, String)> = Default::default();
                s.move_with(&mut |display_map, selection| {
                    if !selection.is_empty() {
                        return;
                    }

                    let mut head = selection.head();
                    let mut transpose_offset = head.to_offset(display_map, Bias::Right);
                    if head.column() == display_map.line_len(head.row()) {
                        transpose_offset = display_map
                            .buffer_snapshot()
                            .clip_offset(transpose_offset.saturating_sub_usize(1), Bias::Left);
                    }

                    if transpose_offset == MultiBufferOffset(0) {
                        return;
                    }

                    *head.column_mut() += 1;
                    head = display_map.clip_point(head, Bias::Right);
                    let goal = SelectionGoal::HorizontalPosition(
                        display_map
                            .x_for_display_point(head, text_layout_details)
                            .into(),
                    );
                    selection.collapse_to(head, goal);

                    let transpose_start = display_map
                        .buffer_snapshot()
                        .clip_offset(transpose_offset.saturating_sub_usize(1), Bias::Left);
                    if edits.last().is_none_or(|e| e.0.end <= transpose_start) {
                        let transpose_end = display_map
                            .buffer_snapshot()
                            .clip_offset(transpose_offset + 1usize, Bias::Right);
                        if let Some(ch) = display_map
                            .buffer_snapshot()
                            .chars_at(transpose_start)
                            .next()
                        {
                            edits.push((transpose_start..transpose_offset, String::new()));
                            edits.push((transpose_end..transpose_end, ch.to_string()));
                        }
                    }
                });
                edits
            });
            this.buffer
                .update(cx, |buffer, cx| buffer.edit(edits, None, cx));
            let selections = this
                .selections
                .all::<MultiBufferOffset>(&this.display_snapshot(cx));
            this.change_selections(Default::default(), window, cx, |s| {
                s.select(selections);
            });
        });
    }

    pub fn rewrap(&mut self, _: &Rewrap, _: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        self.rewrap_impl(RewrapOptions::default(), cx)
    }

    pub fn rewrap_impl(&mut self, options: RewrapOptions, cx: &mut Context<Self>) {
        let buffer = self.buffer.read(cx).snapshot(cx);
        let selections = self.selections.all::<Point>(&self.display_snapshot(cx));

        #[derive(Clone, Debug, PartialEq)]
        enum CommentFormat {
            /// single line comment, with prefix for line
            Line(String),
            /// single line within a block comment, with prefix for line
            BlockLine(String),
            /// a single line of a block comment that includes the initial delimiter
            BlockCommentWithStart(BlockCommentConfig),
            /// a single line of a block comment that includes the ending delimiter
            BlockCommentWithEnd(BlockCommentConfig),
        }

        // Split selections to respect paragraph, indent, and comment prefix boundaries.
        let wrap_ranges = selections.into_iter().flat_map(|selection| {
            let language_settings = buffer.language_settings_at(selection.head(), cx);
            let language_scope = buffer.language_scope_at(selection.head());

            let indent_and_prefix_for_row =
                |row: u32| -> (IndentSize, Option<CommentFormat>, Option<String>) {
                    let indent = buffer.indent_size_for_line(MultiBufferRow(row));
                    let (comment_prefix, rewrap_prefix) = if let Some(language_scope) =
                        &language_scope
                    {
                        let indent_end = Point::new(row, indent.len);
                        let line_end = Point::new(row, buffer.line_len(MultiBufferRow(row)));
                        let line_text_after_indent = buffer
                            .text_for_range(indent_end..line_end)
                            .collect::<String>();

                        let is_within_comment_override = buffer
                            .language_scope_at(indent_end)
                            .is_some_and(|scope| scope.override_name() == Some("comment"));
                        let comment_delimiters = if is_within_comment_override {
                            // we are within a comment syntax node, but we don't
                            // yet know what kind of comment: block, doc or line
                            match (
                                language_scope.documentation_comment(),
                                language_scope.block_comment(),
                            ) {
                                (Some(config), _) | (_, Some(config))
                                    if buffer.contains_str_at(indent_end, &config.start) =>
                                {
                                    Some(CommentFormat::BlockCommentWithStart(config.clone()))
                                }
                                (Some(config), _) | (_, Some(config))
                                    if line_text_after_indent.ends_with(config.end.as_ref()) =>
                                {
                                    Some(CommentFormat::BlockCommentWithEnd(config.clone()))
                                }
                                (Some(config), _) | (_, Some(config))
                                    if buffer.contains_str_at(indent_end, &config.prefix) =>
                                {
                                    Some(CommentFormat::BlockLine(config.prefix.to_string()))
                                }
                                (_, _) => language_scope
                                    .line_comment_prefixes()
                                    .iter()
                                    .find(|prefix| buffer.contains_str_at(indent_end, prefix))
                                    .map(|prefix| CommentFormat::Line(prefix.to_string())),
                            }
                        } else {
                            // we not in an overridden comment node, but we may
                            // be within a non-overridden line comment node
                            language_scope
                                .line_comment_prefixes()
                                .iter()
                                .find(|prefix| buffer.contains_str_at(indent_end, prefix))
                                .map(|prefix| CommentFormat::Line(prefix.to_string()))
                        };

                        let rewrap_prefix = language_scope
                            .rewrap_prefixes()
                            .iter()
                            .find_map(|prefix_regex| {
                                prefix_regex.find(&line_text_after_indent).map(|mat| {
                                    if mat.start() == 0 {
                                        Some(mat.as_str().to_string())
                                    } else {
                                        None
                                    }
                                })
                            })
                            .flatten();
                        (comment_delimiters, rewrap_prefix)
                    } else {
                        (None, None)
                    };
                    (indent, comment_prefix, rewrap_prefix)
                };

            let mut start_row = selection.start.row;
            let mut end_row = selection.end.row;

            if selection.is_empty() {
                let cursor_row = selection.start.row;

                let (mut indent_size, comment_prefix, _) = indent_and_prefix_for_row(cursor_row);
                let line_prefix = match &comment_prefix {
                    Some(CommentFormat::Line(prefix) | CommentFormat::BlockLine(prefix)) => {
                        Some(prefix.as_str())
                    }
                    Some(CommentFormat::BlockCommentWithEnd(BlockCommentConfig {
                        prefix, ..
                    })) => Some(prefix.as_ref()),
                    Some(CommentFormat::BlockCommentWithStart(BlockCommentConfig {
                        start: _,
                        end: _,
                        prefix,
                        tab_size,
                    })) => {
                        indent_size.len += tab_size;
                        Some(prefix.as_ref())
                    }
                    None => None,
                };
                let indent_prefix = indent_size.chars().collect::<String>();
                let line_prefix = format!("{indent_prefix}{}", line_prefix.unwrap_or(""));

                'expand_upwards: while start_row > 0 {
                    let prev_row = start_row - 1;
                    if buffer.contains_str_at(Point::new(prev_row, 0), &line_prefix)
                        && buffer.line_len(MultiBufferRow(prev_row)) as usize > line_prefix.len()
                        && !buffer.is_line_blank(MultiBufferRow(prev_row))
                    {
                        start_row = prev_row;
                    } else {
                        break 'expand_upwards;
                    }
                }

                'expand_downwards: while end_row < buffer.max_point().row {
                    let next_row = end_row + 1;
                    if buffer.contains_str_at(Point::new(next_row, 0), &line_prefix)
                        && buffer.line_len(MultiBufferRow(next_row)) as usize > line_prefix.len()
                        && !buffer.is_line_blank(MultiBufferRow(next_row))
                    {
                        end_row = next_row;
                    } else {
                        break 'expand_downwards;
                    }
                }
            }

            let mut non_blank_rows_iter = (start_row..=end_row)
                .filter(|row| !buffer.is_line_blank(MultiBufferRow(*row)))
                .peekable();

            let first_row = if let Some(&row) = non_blank_rows_iter.peek() {
                row
            } else {
                return Vec::new();
            };

            let mut ranges = Vec::new();

            let mut current_range_start = first_row;
            let mut prev_row = first_row;
            let (
                mut current_range_indent,
                mut current_range_comment_delimiters,
                mut current_range_rewrap_prefix,
            ) = indent_and_prefix_for_row(first_row);

            for row in non_blank_rows_iter.skip(1) {
                let has_paragraph_break = row > prev_row + 1;

                let (row_indent, row_comment_delimiters, row_rewrap_prefix) =
                    indent_and_prefix_for_row(row);

                let has_indent_change = row_indent != current_range_indent;
                let has_comment_change = row_comment_delimiters != current_range_comment_delimiters;

                let has_boundary_change = has_comment_change
                    || row_rewrap_prefix.is_some()
                    || (has_indent_change && current_range_comment_delimiters.is_some());

                if has_paragraph_break || has_boundary_change {
                    ranges.push((
                        language_settings.clone(),
                        Point::new(current_range_start, 0)
                            ..Point::new(prev_row, buffer.line_len(MultiBufferRow(prev_row))),
                        current_range_indent,
                        current_range_comment_delimiters.clone(),
                        current_range_rewrap_prefix.clone(),
                    ));
                    current_range_start = row;
                    current_range_indent = row_indent;
                    current_range_comment_delimiters = row_comment_delimiters;
                    current_range_rewrap_prefix = row_rewrap_prefix;
                }
                prev_row = row;
            }

            ranges.push((
                language_settings.clone(),
                Point::new(current_range_start, 0)
                    ..Point::new(prev_row, buffer.line_len(MultiBufferRow(prev_row))),
                current_range_indent,
                current_range_comment_delimiters,
                current_range_rewrap_prefix,
            ));

            ranges
        });

        let mut edits = Vec::new();
        let mut rewrapped_row_ranges = Vec::<RangeInclusive<u32>>::new();

        for (language_settings, wrap_range, mut indent_size, comment_prefix, rewrap_prefix) in
            wrap_ranges
        {
            let start_row = wrap_range.start.row;
            let end_row = wrap_range.end.row;

            // Skip selections that overlap with a range that has already been rewrapped.
            let selection_range = start_row..end_row;
            if rewrapped_row_ranges
                .iter()
                .any(|range| range.overlaps(&selection_range))
            {
                continue;
            }

            let tab_size = language_settings.tab_size;

            let (line_prefix, inside_comment) = match &comment_prefix {
                Some(CommentFormat::Line(prefix) | CommentFormat::BlockLine(prefix)) => {
                    (Some(prefix.as_str()), true)
                }
                Some(CommentFormat::BlockCommentWithEnd(BlockCommentConfig { prefix, .. })) => {
                    (Some(prefix.as_ref()), true)
                }
                Some(CommentFormat::BlockCommentWithStart(BlockCommentConfig {
                    start: _,
                    end: _,
                    prefix,
                    tab_size,
                })) => {
                    indent_size.len += tab_size;
                    (Some(prefix.as_ref()), true)
                }
                None => (None, false),
            };
            let indent_prefix = indent_size.chars().collect::<String>();
            let line_prefix = format!("{indent_prefix}{}", line_prefix.unwrap_or(""));

            let allow_rewrap_based_on_language = match language_settings.allow_rewrap {
                RewrapBehavior::InComments => inside_comment,
                RewrapBehavior::InSelections => !wrap_range.is_empty(),
                RewrapBehavior::Anywhere => true,
            };

            let should_rewrap = options.override_language_settings
                || allow_rewrap_based_on_language
                || self.hard_wrap.is_some();
            if !should_rewrap {
                continue;
            }

            let start = Point::new(start_row, 0);
            let start_offset = ToOffset::to_offset(&start, &buffer);
            let end = Point::new(end_row, buffer.line_len(MultiBufferRow(end_row)));
            let selection_text = buffer.text_for_range(start..end).collect::<String>();
            let mut first_line_delimiter = None;
            let mut last_line_delimiter = None;
            let Some(lines_without_prefixes) = selection_text
                .lines()
                .enumerate()
                .map(|(ix, line)| {
                    let line_trimmed = line.trim_start();
                    if rewrap_prefix.is_some() && ix > 0 {
                        Ok(line_trimmed)
                    } else if let Some(
                        CommentFormat::BlockCommentWithStart(BlockCommentConfig {
                            start,
                            prefix,
                            end,
                            tab_size,
                        })
                        | CommentFormat::BlockCommentWithEnd(BlockCommentConfig {
                            start,
                            prefix,
                            end,
                            tab_size,
                        }),
                    ) = &comment_prefix
                    {
                        let line_trimmed = line_trimmed
                            .strip_prefix(start.as_ref())
                            .map(|s| {
                                let mut indent_size = indent_size;
                                indent_size.len -= tab_size;
                                let indent_prefix: String = indent_size.chars().collect();
                                first_line_delimiter = Some((indent_prefix, start));
                                s.trim_start()
                            })
                            .unwrap_or(line_trimmed);
                        let line_trimmed = line_trimmed
                            .strip_suffix(end.as_ref())
                            .map(|s| {
                                last_line_delimiter = Some(end);
                                s.trim_end()
                            })
                            .unwrap_or(line_trimmed);
                        let line_trimmed = line_trimmed
                            .strip_prefix(prefix.as_ref())
                            .unwrap_or(line_trimmed);
                        Ok(line_trimmed)
                    } else if let Some(CommentFormat::BlockLine(prefix)) = &comment_prefix {
                        line_trimmed.strip_prefix(prefix).with_context(|| {
                            format!("line did not start with prefix {prefix:?}: {line:?}")
                        })
                    } else {
                        line_trimmed
                            .strip_prefix(&line_prefix.trim_start())
                            .with_context(|| {
                                format!("line did not start with prefix {line_prefix:?}: {line:?}")
                            })
                    }
                })
                .collect::<Result<Vec<_>, _>>()
                .log_err()
            else {
                continue;
            };

            let wrap_column = options.line_length.or(self.hard_wrap).unwrap_or_else(|| {
                buffer
                    .language_settings_at(Point::new(start_row, 0), cx)
                    .preferred_line_length as usize
            });

            let subsequent_lines_prefix = if let Some(rewrap_prefix_str) = &rewrap_prefix {
                format!("{}{}", indent_prefix, " ".repeat(rewrap_prefix_str.len()))
            } else {
                line_prefix.clone()
            };

            let wrapped_text = {
                let mut wrapped_text = wrap_with_prefix(
                    line_prefix,
                    subsequent_lines_prefix,
                    lines_without_prefixes.join("\n"),
                    wrap_column,
                    tab_size,
                    options.preserve_existing_whitespace,
                );

                if let Some((indent, delimiter)) = first_line_delimiter {
                    wrapped_text = format!("{indent}{delimiter}\n{wrapped_text}");
                }
                if let Some(last_line) = last_line_delimiter {
                    wrapped_text = format!("{wrapped_text}\n{indent_prefix}{last_line}");
                }

                wrapped_text
            };

            // TODO: should always use char-based diff while still supporting cursor behavior that
            // matches vim.
            let mut diff_options = DiffOptions::default();
            if options.override_language_settings {
                diff_options.max_word_diff_len = 0;
                diff_options.max_word_diff_line_count = 0;
            } else {
                diff_options.max_word_diff_len = usize::MAX;
                diff_options.max_word_diff_line_count = usize::MAX;
            }

            for (old_range, new_text) in
                text_diff_with_options(&selection_text, &wrapped_text, diff_options)
            {
                let edit_start = buffer.anchor_after(start_offset + old_range.start);
                let edit_end = buffer.anchor_after(start_offset + old_range.end);
                edits.push((edit_start..edit_end, new_text));
            }

            rewrapped_row_ranges.push(start_row..=end_row);
        }

        self.buffer
            .update(cx, |buffer, cx| buffer.edit(edits, None, cx));
    }

    pub fn cut_common(
        &mut self,
        cut_no_selection_line: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> ClipboardItem {
        let mut text = String::new();
        let buffer = self.buffer.read(cx).snapshot(cx);
        let mut selections = self.selections.all::<Point>(&self.display_snapshot(cx));
        let mut clipboard_selections = Vec::with_capacity(selections.len());
        {
            let max_point = buffer.max_point();
            let mut is_first = true;
            let mut prev_selection_was_entire_line = false;
            for selection in &mut selections {
                let is_entire_line =
                    (selection.is_empty() && cut_no_selection_line) || self.selections.line_mode();
                if is_entire_line {
                    selection.start = Point::new(selection.start.row, 0);
                    if !selection.is_empty() && selection.end.column == 0 {
                        selection.end = cmp::min(max_point, selection.end);
                    } else {
                        selection.end = cmp::min(max_point, Point::new(selection.end.row + 1, 0));
                    }
                    selection.goal = SelectionGoal::None;
                }
                if is_first {
                    is_first = false;
                } else if !prev_selection_was_entire_line {
                    text += "\n";
                }
                prev_selection_was_entire_line = is_entire_line;
                let mut len = 0;
                for chunk in buffer.text_for_range(selection.start..selection.end) {
                    text.push_str(chunk);
                    len += chunk.len();
                }

                clipboard_selections.push(ClipboardSelection::for_buffer(
                    len,
                    is_entire_line,
                    selection.range(),
                    &buffer,
                    self.project.as_ref(),
                    cx,
                ));
            }
        }

        self.transact(window, cx, |this, window, cx| {
            this.change_selections(Default::default(), window, cx, |s| {
                s.select(selections);
            });
            this.insert("", window, cx);
        });
        ClipboardItem::new_string_with_json_metadata(text, clipboard_selections)
    }

    pub fn cut(&mut self, _: &Cut, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let item = self.cut_common(true, window, cx);
        cx.write_to_clipboard(item);
    }

    pub fn kill_ring_cut(&mut self, _: &KillRingCut, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
            s.move_with(&mut |snapshot, sel| {
                if sel.is_empty() {
                    sel.end = DisplayPoint::new(sel.end.row(), snapshot.line_len(sel.end.row()));
                }
                if sel.is_empty() {
                    sel.end = DisplayPoint::new(sel.end.row() + 1_u32, 0);
                }
            });
        });
        let item = self.cut_common(false, window, cx);
        cx.set_global(KillRing(item))
    }

    pub fn kill_ring_yank(
        &mut self,
        _: &KillRingYank,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let (text, metadata) = if let Some(KillRing(item)) = cx.try_global() {
            if let Some(ClipboardEntry::String(kill_ring)) = item.entries().first() {
                (kill_ring.text().to_string(), kill_ring.metadata_json())
            } else {
                return;
            }
        } else {
            return;
        };
        self.do_paste(&text, metadata, false, window, cx);
    }

    pub fn copy_and_trim(&mut self, _: &CopyAndTrim, _: &mut Window, cx: &mut Context<Self>) {
        self.do_copy(true, cx);
    }

    pub fn copy(&mut self, _: &Copy, _: &mut Window, cx: &mut Context<Self>) {
        self.do_copy(false, cx);
    }

    fn do_copy(&self, strip_leading_indents: bool, cx: &mut Context<Self>) {
        let selections = self.selections.all::<Point>(&self.display_snapshot(cx));
        let buffer = self.buffer.read(cx).read(cx);
        let mut text = String::new();
        let mut clipboard_selections = Vec::with_capacity(selections.len());

        let max_point = buffer.max_point();
        let mut is_first = true;
        for selection in &selections {
            let mut start = selection.start;
            let mut end = selection.end;
            let is_entire_line = selection.is_empty() || self.selections.line_mode();
            let mut add_trailing_newline = false;
            if is_entire_line {
                start = Point::new(start.row, 0);
                let next_line_start = Point::new(end.row + 1, 0);
                if next_line_start <= max_point {
                    end = next_line_start;
                } else {
                    // We're on the last line without a trailing newline.
                    // Copy to the end of the line and add a newline afterwards.
                    end = Point::new(end.row, buffer.line_len(MultiBufferRow(end.row)));
                    add_trailing_newline = true;
                }
            }

            let mut trimmed_selections = Vec::new();
            if strip_leading_indents && end.row.saturating_sub(start.row) > 0 {
                let row = MultiBufferRow(start.row);
                let first_indent = buffer.indent_size_for_line(row);
                if first_indent.len == 0 || start.column > first_indent.len {
                    trimmed_selections.push(start..end);
                } else {
                    trimmed_selections.push(
                        Point::new(row.0, first_indent.len)
                            ..Point::new(row.0, buffer.line_len(row)),
                    );
                    for row in start.row + 1..=end.row {
                        let mut line_len = buffer.line_len(MultiBufferRow(row));
                        if row == end.row {
                            line_len = end.column;
                        }
                        if line_len == 0 {
                            trimmed_selections.push(Point::new(row, 0)..Point::new(row, line_len));
                            continue;
                        }
                        let row_indent_size = buffer.indent_size_for_line(MultiBufferRow(row));
                        if row_indent_size.len >= first_indent.len {
                            trimmed_selections
                                .push(Point::new(row, first_indent.len)..Point::new(row, line_len));
                        } else {
                            trimmed_selections.clear();
                            trimmed_selections.push(start..end);
                            break;
                        }
                    }
                }
            } else {
                trimmed_selections.push(start..end);
            }

            let is_multiline_trim = trimmed_selections.len() > 1;
            let mut selection_len: usize = 0;
            let prev_selection_was_entire_line = is_entire_line && !is_multiline_trim;

            for trimmed_range in trimmed_selections {
                if is_first {
                    is_first = false;
                } else if is_multiline_trim || !prev_selection_was_entire_line {
                    text.push('\n');
                    if is_multiline_trim {
                        selection_len += 1;
                    }
                }
                for chunk in buffer.text_for_range(trimmed_range.start..trimmed_range.end) {
                    text.push_str(chunk);
                    selection_len += chunk.len();
                }
                if add_trailing_newline {
                    text.push('\n');
                    selection_len += 1;
                }
            }

            clipboard_selections.push(ClipboardSelection::for_buffer(
                selection_len,
                is_entire_line,
                start..end,
                &buffer,
                self.project.as_ref(),
                cx,
            ));
        }

        cx.write_to_clipboard(ClipboardItem::new_string_with_json_metadata(
            text,
            clipboard_selections,
        ));
    }

    pub fn do_paste(
        &mut self,
        text: &String,
        clipboard_selections: Option<Vec<ClipboardSelection>>,
        handle_entire_lines: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.read_only(cx) {
            return;
        }

        self.finalize_last_transaction(cx);

        let clipboard_text = Cow::Borrowed(text.as_str());

        self.transact(window, cx, |this, window, cx| {
            let had_active_edit_prediction = this.has_active_edit_prediction();
            let display_map = this.display_snapshot(cx);
            let old_selections = this.selections.all::<MultiBufferOffset>(&display_map);
            let cursor_offset = this
                .selections
                .last::<MultiBufferOffset>(&display_map)
                .head();

            if let Some(mut clipboard_selections) = clipboard_selections {
                let all_selections_were_entire_line =
                    clipboard_selections.iter().all(|s| s.is_entire_line);
                let first_selection_indent_column =
                    clipboard_selections.first().map(|s| s.first_line_indent);
                if clipboard_selections.len() != old_selections.len() {
                    clipboard_selections.drain(..);
                }
                let mut auto_indent_on_paste = true;

                this.buffer.update(cx, |buffer, cx| {
                    let snapshot = buffer.read(cx);
                    auto_indent_on_paste = snapshot
                        .language_settings_at(cursor_offset, cx)
                        .auto_indent_on_paste;

                    let mut start_offset = 0;
                    let mut edits = Vec::new();
                    let mut original_indent_columns = Vec::new();
                    for (ix, selection) in old_selections.iter().enumerate() {
                        let to_insert;
                        let entire_line;
                        let original_indent_column;
                        if let Some(clipboard_selection) = clipboard_selections.get(ix) {
                            let end_offset = start_offset + clipboard_selection.len;
                            to_insert = &clipboard_text[start_offset..end_offset];
                            entire_line = clipboard_selection.is_entire_line;
                            start_offset = if entire_line {
                                end_offset
                            } else {
                                end_offset + 1
                            };
                            original_indent_column = Some(clipboard_selection.first_line_indent);
                        } else {
                            to_insert = &*clipboard_text;
                            entire_line = all_selections_were_entire_line;
                            original_indent_column = first_selection_indent_column
                        }

                        let (range, to_insert) =
                            if selection.is_empty() && handle_entire_lines && entire_line {
                                // If the corresponding selection was empty when this slice of the
                                // clipboard text was written, then the entire line containing the
                                // selection was copied. If this selection is also currently empty,
                                // then paste the line before the current line of the buffer.
                                let column = selection.start.to_point(&snapshot).column as usize;
                                let line_start = selection.start - column;
                                (line_start..line_start, Cow::Borrowed(to_insert))
                            } else {
                                let language = snapshot.language_at(selection.head());
                                let range = selection.range();
                                if let Some(language) = language
                                    && language.name() == "Markdown"
                                {
                                    edit_for_markdown_paste(
                                        &snapshot,
                                        range,
                                        to_insert,
                                        url::Url::parse(to_insert).ok(),
                                    )
                                } else {
                                    (range, Cow::Borrowed(to_insert))
                                }
                            };

                        edits.push((range, to_insert));
                        original_indent_columns.push(original_indent_column);
                    }
                    drop(snapshot);

                    buffer.edit(
                        edits,
                        if auto_indent_on_paste {
                            Some(AutoindentMode::Block {
                                original_indent_columns,
                            })
                        } else {
                            None
                        },
                        cx,
                    );
                });

                let selections = this
                    .selections
                    .all::<MultiBufferOffset>(&this.display_snapshot(cx));
                this.change_selections(Default::default(), window, cx, |s| s.select(selections));
            } else {
                let url = url::Url::parse(&clipboard_text).ok();

                let auto_indent_mode = if !clipboard_text.is_empty() {
                    Some(AutoindentMode::Block {
                        original_indent_columns: Vec::new(),
                    })
                } else {
                    None
                };

                let selection_anchors = this.buffer.update(cx, |buffer, cx| {
                    let snapshot = buffer.snapshot(cx);

                    let anchors = old_selections
                        .iter()
                        .map(|s| {
                            let anchor = snapshot.anchor_after(s.head());
                            s.map(|_| anchor)
                        })
                        .collect::<Vec<_>>();

                    let mut edits = Vec::new();

                    // When pasting text without metadata (e.g. copied from an
                    // external editor using multiple cursors) and the number of
                    // lines matches the number of selections, distribute one
                    // line per cursor instead of pasting the whole text at each.
                    let lines: Vec<&str> = clipboard_text.split('\n').collect();
                    let distribute_lines =
                        old_selections.len() > 1 && lines.len() == old_selections.len();

                    for (ix, selection) in old_selections.iter().enumerate() {
                        let language = snapshot.language_at(selection.head());
                        let range = selection.range();

                        let text_for_cursor: &str = if distribute_lines {
                            lines[ix]
                        } else {
                            &clipboard_text
                        };

                        let (edit_range, edit_text) = if let Some(language) = language
                            && language.name() == "Markdown"
                        {
                            edit_for_markdown_paste(&snapshot, range, text_for_cursor, url.clone())
                        } else {
                            (range, Cow::Borrowed(text_for_cursor))
                        };

                        edits.push((edit_range, edit_text));
                    }

                    drop(snapshot);
                    buffer.edit(edits, auto_indent_mode, cx);

                    anchors
                });

                this.change_selections(Default::default(), window, cx, |s| {
                    s.select_anchors(selection_anchors);
                });
            }

            //   🤔                 |    ..     | show_in_menu |
            // | ..                  |   true        true
            // | had_edit_prediction |   false       true

            let trigger_in_words =
                this.show_edit_predictions_in_menu() || !had_active_edit_prediction;

            this.trigger_completion_on_input(text, trigger_in_words, window, cx);
        });
    }

    pub fn diff_clipboard_with_selection(
        &mut self,
        _: &DiffClipboardWithSelection,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let selections = self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx));

        if selections.is_empty() {
            log::warn!("There should always be at least one selection in Zed. This is a bug.");
            return;
        };

        let clipboard_text = cx.read_from_clipboard().and_then(|item| {
            item.entries().iter().find_map(|entry| match entry {
                ClipboardEntry::String(text) => Some(text.text().to_string()),
                _ => None,
            })
        });

        let Some(clipboard_text) = clipboard_text else {
            log::warn!("Clipboard doesn't contain text.");
            return;
        };

        window.dispatch_action(
            Box::new(DiffClipboardWithSelectionData {
                clipboard_text,
                editor: cx.entity(),
            }),
            cx,
        );
    }

    pub fn paste(&mut self, _: &Paste, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        if let Some(item) = cx.read_from_clipboard() {
            let clipboard_string = item.entries().iter().find_map(|entry| match entry {
                ClipboardEntry::String(s) => Some(s),
                _ => None,
            });
            match clipboard_string {
                Some(clipboard_string) => self.do_paste(
                    clipboard_string.text(),
                    clipboard_string.metadata_json::<Vec<ClipboardSelection>>(),
                    true,
                    window,
                    cx,
                ),
                _ => self.do_paste(&item.text().unwrap_or_default(), None, true, window, cx),
            }
        }
    }

    pub fn undo(&mut self, _: &Undo, window: &mut Window, cx: &mut Context<Self>) {
        if self.read_only(cx) {
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        if let Some(transaction_id) = self.buffer.update(cx, |buffer, cx| buffer.undo(cx)) {
            if let Some((selections, _)) =
                self.selection_history.transaction(transaction_id).cloned()
            {
                self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                    s.select_anchors(selections.to_vec());
                });
            } else {
                log::error!(
                    "No entry in selection_history found for undo. \
                     This may correspond to a bug where undo does not update the selection. \
                     If this is occurring, please add details to \
                     https://github.com/zed-industries/zed/issues/22692"
                );
            }
            self.request_autoscroll(Autoscroll::fit(), cx);
            self.unmark_text(window, cx);
            self.refresh_edit_prediction(true, false, window, cx);
            cx.emit(EditorEvent::Edited { transaction_id });
            cx.emit(EditorEvent::TransactionUndone { transaction_id });
        }
    }

    pub fn redo(&mut self, _: &Redo, window: &mut Window, cx: &mut Context<Self>) {
        if self.read_only(cx) {
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        if let Some(transaction_id) = self.buffer.update(cx, |buffer, cx| buffer.redo(cx)) {
            if let Some((_, Some(selections))) =
                self.selection_history.transaction(transaction_id).cloned()
            {
                self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                    s.select_anchors(selections.to_vec());
                });
            } else {
                log::error!(
                    "No entry in selection_history found for redo. \
                     This may correspond to a bug where undo does not update the selection. \
                     If this is occurring, please add details to \
                     https://github.com/zed-industries/zed/issues/22692"
                );
            }
            self.request_autoscroll(Autoscroll::fit(), cx);
            self.unmark_text(window, cx);
            self.refresh_edit_prediction(true, false, window, cx);
            cx.emit(EditorEvent::Edited { transaction_id });
        }
    }

    pub fn finalize_last_transaction(&mut self, cx: &mut Context<Self>) {
        self.buffer
            .update(cx, |buffer, cx| buffer.finalize_last_transaction(cx));
    }

    pub fn group_until_transaction(&mut self, tx_id: TransactionId, cx: &mut Context<Self>) {
        self.buffer
            .update(cx, |buffer, cx| buffer.group_until_transaction(tx_id, cx));
    }

    pub fn move_left(&mut self, _: &MoveLeft, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                let cursor = if selection.is_empty() {
                    movement::left(map, selection.start)
                } else {
                    selection.start
                };
                selection.collapse_to(cursor, SelectionGoal::None);
            });
        })
    }

    pub fn select_left(&mut self, _: &SelectLeft, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| (movement::left(map, head), SelectionGoal::None));
        })
    }

    pub fn move_right(&mut self, _: &MoveRight, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                let cursor = if selection.is_empty() {
                    movement::right(map, selection.end)
                } else {
                    selection.end
                };
                selection.collapse_to(cursor, SelectionGoal::None)
            });
        })
    }

    pub fn select_right(&mut self, _: &SelectRight, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (movement::right(map, head), SelectionGoal::None)
            });
        });
    }

    pub fn move_up(&mut self, _: &MoveUp, window: &mut Window, cx: &mut Context<Self>) {
        if self.take_rename(true, window, cx).is_some() {
            return;
        }

        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let text_layout_details = &self.text_layout_details(window, cx);
        let selection_count = self.selections.count();
        let first_selection = self.selections.first_anchor();

        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                if !selection.is_empty() {
                    selection.goal = SelectionGoal::None;
                }
                let (cursor, goal) = movement::up(
                    map,
                    selection.start,
                    selection.goal,
                    false,
                    text_layout_details,
                );
                selection.collapse_to(cursor, goal);
            });
        });

        if selection_count == 1 && first_selection.range() == self.selections.first_anchor().range()
        {
            cx.propagate();
        }
    }

    pub fn move_up_by_lines(
        &mut self,
        action: &MoveUpByLines,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.take_rename(true, window, cx).is_some() {
            return;
        }

        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let text_layout_details = &self.text_layout_details(window, cx);

        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                if !selection.is_empty() {
                    selection.goal = SelectionGoal::None;
                }
                let (cursor, goal) = movement::up_by_rows(
                    map,
                    selection.start,
                    action.lines,
                    selection.goal,
                    false,
                    text_layout_details,
                );
                selection.collapse_to(cursor, goal);
            });
        })
    }

    pub fn move_down_by_lines(
        &mut self,
        action: &MoveDownByLines,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.take_rename(true, window, cx).is_some() {
            return;
        }

        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let text_layout_details = &self.text_layout_details(window, cx);

        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                if !selection.is_empty() {
                    selection.goal = SelectionGoal::None;
                }
                let (cursor, goal) = movement::down_by_rows(
                    map,
                    selection.start,
                    action.lines,
                    selection.goal,
                    false,
                    text_layout_details,
                );
                selection.collapse_to(cursor, goal);
            });
        })
    }

    pub fn select_down_by_lines(
        &mut self,
        action: &SelectDownByLines,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let text_layout_details = &self.text_layout_details(window, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, goal| {
                movement::down_by_rows(map, head, action.lines, goal, false, text_layout_details)
            })
        })
    }

    pub fn select_up_by_lines(
        &mut self,
        action: &SelectUpByLines,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let text_layout_details = &self.text_layout_details(window, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, goal| {
                movement::up_by_rows(map, head, action.lines, goal, false, text_layout_details)
            })
        })
    }

    pub fn select_page_up(
        &mut self,
        _: &SelectPageUp,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(row_count) = self.visible_row_count() else {
            return;
        };

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let text_layout_details = &self.text_layout_details(window, cx);

        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, goal| {
                movement::up_by_rows(map, head, row_count, goal, false, text_layout_details)
            })
        })
    }

    pub fn move_page_up(
        &mut self,
        action: &MovePageUp,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.take_rename(true, window, cx).is_some() {
            return;
        }

        if self
            .context_menu
            .borrow_mut()
            .as_mut()
            .map(|menu| menu.select_first(self.completion_provider.as_deref(), window, cx))
            .unwrap_or(false)
        {
            return;
        }

        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }

        let Some(row_count) = self.visible_row_count() else {
            return;
        };

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let effects = if action.center_cursor {
            SelectionEffects::scroll(Autoscroll::center())
        } else {
            SelectionEffects::default()
        };

        let text_layout_details = &self.text_layout_details(window, cx);

        self.change_selections(effects, window, cx, |s| {
            s.move_with(&mut |map, selection| {
                if !selection.is_empty() {
                    selection.goal = SelectionGoal::None;
                }
                let (cursor, goal) = movement::up_by_rows(
                    map,
                    selection.end,
                    row_count,
                    selection.goal,
                    false,
                    text_layout_details,
                );
                selection.collapse_to(cursor, goal);
            });
        });
    }

    pub fn select_up(&mut self, _: &SelectUp, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let text_layout_details = &self.text_layout_details(window, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, goal| {
                movement::up(map, head, goal, false, text_layout_details)
            })
        })
    }

    pub fn move_down(&mut self, _: &MoveDown, window: &mut Window, cx: &mut Context<Self>) {
        self.take_rename(true, window, cx);

        if self.mode.is_single_line() {
            cx.propagate();
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let text_layout_details = &self.text_layout_details(window, cx);
        let selection_count = self.selections.count();
        let first_selection = self.selections.first_anchor();

        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                if !selection.is_empty() {
                    selection.goal = SelectionGoal::None;
                }
                let (cursor, goal) = movement::down(
                    map,
                    selection.end,
                    selection.goal,
                    false,
                    text_layout_details,
                );
                selection.collapse_to(cursor, goal);
            });
        });

        if selection_count == 1 && first_selection.range() == self.selections.first_anchor().range()
        {
            cx.propagate();
        }
    }

    pub fn select_page_down(
        &mut self,
        _: &SelectPageDown,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(row_count) = self.visible_row_count() else {
            return;
        };

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let text_layout_details = &self.text_layout_details(window, cx);

        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, goal| {
                movement::down_by_rows(map, head, row_count, goal, false, text_layout_details)
            })
        })
    }

    pub fn move_page_down(
        &mut self,
        action: &MovePageDown,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.take_rename(true, window, cx).is_some() {
            return;
        }

        if self
            .context_menu
            .borrow_mut()
            .as_mut()
            .map(|menu| menu.select_last(self.completion_provider.as_deref(), window, cx))
            .unwrap_or(false)
        {
            return;
        }

        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }

        let Some(row_count) = self.visible_row_count() else {
            return;
        };

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let effects = if action.center_cursor {
            SelectionEffects::scroll(Autoscroll::center())
        } else {
            SelectionEffects::default()
        };

        let text_layout_details = &self.text_layout_details(window, cx);
        self.change_selections(effects, window, cx, |s| {
            s.move_with(&mut |map, selection| {
                if !selection.is_empty() {
                    selection.goal = SelectionGoal::None;
                }
                let (cursor, goal) = movement::down_by_rows(
                    map,
                    selection.end,
                    row_count,
                    selection.goal,
                    false,
                    text_layout_details,
                );
                selection.collapse_to(cursor, goal);
            });
        });
    }

    pub fn select_down(&mut self, _: &SelectDown, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let text_layout_details = &self.text_layout_details(window, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, goal| {
                movement::down(map, head, goal, false, text_layout_details)
            })
        });
    }

    pub fn context_menu_first(
        &mut self,
        _: &ContextMenuFirst,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(context_menu) = self.context_menu.borrow_mut().as_mut() {
            context_menu.select_first(self.completion_provider.as_deref(), window, cx);
        }
    }

    pub fn context_menu_prev(
        &mut self,
        _: &ContextMenuPrevious,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(context_menu) = self.context_menu.borrow_mut().as_mut() {
            context_menu.select_prev(self.completion_provider.as_deref(), window, cx);
        }
    }

    pub fn context_menu_next(
        &mut self,
        _: &ContextMenuNext,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(context_menu) = self.context_menu.borrow_mut().as_mut() {
            context_menu.select_next(self.completion_provider.as_deref(), window, cx);
        }
    }

    pub fn context_menu_last(
        &mut self,
        _: &ContextMenuLast,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(context_menu) = self.context_menu.borrow_mut().as_mut() {
            context_menu.select_last(self.completion_provider.as_deref(), window, cx);
        }
    }

    pub fn signature_help_prev(
        &mut self,
        _: &SignatureHelpPrevious,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(popover) = self.signature_help_state.popover_mut() {
            if popover.current_signature == 0 {
                popover.current_signature = popover.signatures.len() - 1;
            } else {
                popover.current_signature -= 1;
            }
            cx.notify();
        }
    }

    pub fn signature_help_next(
        &mut self,
        _: &SignatureHelpNext,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(popover) = self.signature_help_state.popover_mut() {
            if popover.current_signature + 1 == popover.signatures.len() {
                popover.current_signature = 0;
            } else {
                popover.current_signature += 1;
            }
            cx.notify();
        }
    }

    pub fn move_to_previous_word_start(
        &mut self,
        _: &MoveToPreviousWordStart,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_cursors_with(&mut |map, head, _| {
                (
                    movement::previous_word_start(map, head),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn move_to_previous_subword_start(
        &mut self,
        _: &MoveToPreviousSubwordStart,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_cursors_with(&mut |map, head, _| {
                (
                    movement::previous_subword_start(map, head),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn select_to_previous_word_start(
        &mut self,
        _: &SelectToPreviousWordStart,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (
                    movement::previous_word_start(map, head),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn select_to_previous_subword_start(
        &mut self,
        _: &SelectToPreviousSubwordStart,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (
                    movement::previous_subword_start(map, head),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn delete_to_previous_word_start(
        &mut self,
        action: &DeleteToPreviousWordStart,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            this.select_autoclose_pair(window, cx);
            this.change_selections(Default::default(), window, cx, |s| {
                s.move_with(&mut |map, selection| {
                    if selection.is_empty() {
                        let mut cursor = if action.ignore_newlines {
                            movement::previous_word_start(map, selection.head())
                        } else {
                            movement::previous_word_start_or_newline(map, selection.head())
                        };
                        cursor = movement::adjust_greedy_deletion(
                            map,
                            selection.head(),
                            cursor,
                            action.ignore_brackets,
                        );
                        selection.set_head(cursor, SelectionGoal::None);
                    }
                });
            });
            this.insert("", window, cx);
        });
    }

    pub fn delete_to_previous_subword_start(
        &mut self,
        action: &DeleteToPreviousSubwordStart,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            this.select_autoclose_pair(window, cx);
            this.change_selections(Default::default(), window, cx, |s| {
                s.move_with(&mut |map, selection| {
                    if selection.is_empty() {
                        let mut cursor = if action.ignore_newlines {
                            movement::previous_subword_start(map, selection.head())
                        } else {
                            movement::previous_subword_start_or_newline(map, selection.head())
                        };
                        cursor = movement::adjust_greedy_deletion(
                            map,
                            selection.head(),
                            cursor,
                            action.ignore_brackets,
                        );
                        selection.set_head(cursor, SelectionGoal::None);
                    }
                });
            });
            this.insert("", window, cx);
        });
    }

    pub fn move_to_next_word_end(
        &mut self,
        _: &MoveToNextWordEnd,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_cursors_with(&mut |map, head, _| {
                (movement::next_word_end(map, head), SelectionGoal::None)
            });
        })
    }

    pub fn move_to_next_subword_end(
        &mut self,
        _: &MoveToNextSubwordEnd,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_cursors_with(&mut |map, head, _| {
                (movement::next_subword_end(map, head), SelectionGoal::None)
            });
        })
    }

    pub fn select_to_next_word_end(
        &mut self,
        _: &SelectToNextWordEnd,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (movement::next_word_end(map, head), SelectionGoal::None)
            });
        })
    }

    pub fn select_to_next_subword_end(
        &mut self,
        _: &SelectToNextSubwordEnd,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (movement::next_subword_end(map, head), SelectionGoal::None)
            });
        })
    }

    pub fn delete_to_next_word_end(
        &mut self,
        action: &DeleteToNextWordEnd,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            this.change_selections(Default::default(), window, cx, |s| {
                s.move_with(&mut |map, selection| {
                    if selection.is_empty() {
                        let mut cursor = if action.ignore_newlines {
                            movement::next_word_end(map, selection.head())
                        } else {
                            movement::next_word_end_or_newline(map, selection.head())
                        };
                        cursor = movement::adjust_greedy_deletion(
                            map,
                            selection.head(),
                            cursor,
                            action.ignore_brackets,
                        );
                        selection.set_head(cursor, SelectionGoal::None);
                    }
                });
            });
            this.insert("", window, cx);
        });
    }

    pub fn delete_to_next_subword_end(
        &mut self,
        action: &DeleteToNextSubwordEnd,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            this.change_selections(Default::default(), window, cx, |s| {
                s.move_with(&mut |map, selection| {
                    if selection.is_empty() {
                        let mut cursor = if action.ignore_newlines {
                            movement::next_subword_end(map, selection.head())
                        } else {
                            movement::next_subword_end_or_newline(map, selection.head())
                        };
                        cursor = movement::adjust_greedy_deletion(
                            map,
                            selection.head(),
                            cursor,
                            action.ignore_brackets,
                        );
                        selection.set_head(cursor, SelectionGoal::None);
                    }
                });
            });
            this.insert("", window, cx);
        });
    }

    pub fn move_to_beginning_of_line(
        &mut self,
        action: &MoveToBeginningOfLine,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let stop_at_indent = action.stop_at_indent && !self.mode.is_single_line();
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_cursors_with(&mut |map, head, _| {
                (
                    movement::indented_line_beginning(
                        map,
                        head,
                        action.stop_at_soft_wraps,
                        stop_at_indent,
                    ),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn select_to_beginning_of_line(
        &mut self,
        action: &SelectToBeginningOfLine,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let stop_at_indent = action.stop_at_indent && !self.mode.is_single_line();
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (
                    movement::indented_line_beginning(
                        map,
                        head,
                        action.stop_at_soft_wraps,
                        stop_at_indent,
                    ),
                    SelectionGoal::None,
                )
            });
        });
    }

    pub fn delete_to_beginning_of_line(
        &mut self,
        action: &DeleteToBeginningOfLine,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            this.change_selections(Default::default(), window, cx, |s| {
                s.move_with(&mut |_, selection| {
                    selection.reversed = true;
                });
            });

            this.select_to_beginning_of_line(
                &SelectToBeginningOfLine {
                    stop_at_soft_wraps: false,
                    stop_at_indent: action.stop_at_indent,
                },
                window,
                cx,
            );
            this.backspace(&Backspace, window, cx);
        });
    }

    pub fn move_to_end_of_line(
        &mut self,
        action: &MoveToEndOfLine,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_cursors_with(&mut |map, head, _| {
                (
                    movement::line_end(map, head, action.stop_at_soft_wraps),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn select_to_end_of_line(
        &mut self,
        action: &SelectToEndOfLine,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (
                    movement::line_end(map, head, action.stop_at_soft_wraps),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn delete_to_end_of_line(
        &mut self,
        _: &DeleteToEndOfLine,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            this.select_to_end_of_line(
                &SelectToEndOfLine {
                    stop_at_soft_wraps: false,
                },
                window,
                cx,
            );
            this.delete(&Delete, window, cx);
        });
    }

    pub fn cut_to_end_of_line(
        &mut self,
        action: &CutToEndOfLine,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            this.select_to_end_of_line(
                &SelectToEndOfLine {
                    stop_at_soft_wraps: false,
                },
                window,
                cx,
            );
            if !action.stop_at_newlines {
                this.change_selections(Default::default(), window, cx, |s| {
                    s.move_with(&mut |_, sel| {
                        if sel.is_empty() {
                            sel.end = DisplayPoint::new(sel.end.row() + 1_u32, 0);
                        }
                    });
                });
            }
            this.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
            let item = this.cut_common(false, window, cx);
            cx.write_to_clipboard(item);
        });
    }

    pub fn move_to_start_of_paragraph(
        &mut self,
        _: &MoveToStartOfParagraph,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                selection.collapse_to(
                    movement::start_of_paragraph(map, selection.head(), 1),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn move_to_end_of_paragraph(
        &mut self,
        _: &MoveToEndOfParagraph,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                selection.collapse_to(
                    movement::end_of_paragraph(map, selection.head(), 1),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn select_to_start_of_paragraph(
        &mut self,
        _: &SelectToStartOfParagraph,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (
                    movement::start_of_paragraph(map, head, 1),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn select_to_end_of_paragraph(
        &mut self,
        _: &SelectToEndOfParagraph,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (
                    movement::end_of_paragraph(map, head, 1),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn move_to_start_of_excerpt(
        &mut self,
        _: &MoveToStartOfExcerpt,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                selection.collapse_to(
                    movement::start_of_excerpt(
                        map,
                        selection.head(),
                        workspace::searchable::Direction::Prev,
                    ),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn move_to_start_of_next_excerpt(
        &mut self,
        _: &MoveToStartOfNextExcerpt,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }

        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                selection.collapse_to(
                    movement::start_of_excerpt(
                        map,
                        selection.head(),
                        workspace::searchable::Direction::Next,
                    ),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn move_to_end_of_excerpt(
        &mut self,
        _: &MoveToEndOfExcerpt,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                selection.collapse_to(
                    movement::end_of_excerpt(
                        map,
                        selection.head(),
                        workspace::searchable::Direction::Next,
                    ),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn move_to_end_of_previous_excerpt(
        &mut self,
        _: &MoveToEndOfPreviousExcerpt,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_with(&mut |map, selection| {
                selection.collapse_to(
                    movement::end_of_excerpt(
                        map,
                        selection.head(),
                        workspace::searchable::Direction::Prev,
                    ),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn select_to_start_of_excerpt(
        &mut self,
        _: &SelectToStartOfExcerpt,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (
                    movement::start_of_excerpt(map, head, workspace::searchable::Direction::Prev),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn select_to_start_of_next_excerpt(
        &mut self,
        _: &SelectToStartOfNextExcerpt,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (
                    movement::start_of_excerpt(map, head, workspace::searchable::Direction::Next),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn select_to_end_of_excerpt(
        &mut self,
        _: &SelectToEndOfExcerpt,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (
                    movement::end_of_excerpt(map, head, workspace::searchable::Direction::Next),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn select_to_end_of_previous_excerpt(
        &mut self,
        _: &SelectToEndOfPreviousExcerpt,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_heads_with(&mut |map, head, _| {
                (
                    movement::end_of_excerpt(map, head, workspace::searchable::Direction::Prev),
                    SelectionGoal::None,
                )
            });
        })
    }

    pub fn move_to_beginning(
        &mut self,
        _: &MoveToBeginning,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.select_ranges(vec![Anchor::Min..Anchor::Min]);
        });
    }

    pub fn select_to_beginning(
        &mut self,
        _: &SelectToBeginning,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let mut selection = self.selections.last::<Point>(&self.display_snapshot(cx));
        selection.set_head(Point::zero(), SelectionGoal::None);
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.select(vec![selection]);
        });
    }

    pub fn move_to_end(&mut self, _: &MoveToEnd, window: &mut Window, cx: &mut Context<Self>) {
        if matches!(self.mode, EditorMode::SingleLine) {
            cx.propagate();
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let cursor = self.buffer.read(cx).read(cx).len();
        self.change_selections(Default::default(), window, cx, |s| {
            s.select_ranges(vec![cursor..cursor])
        });
    }

    pub fn set_nav_history(&mut self, nav_history: Option<ItemNavHistory>) {
        self.nav_history = nav_history;
    }

    pub fn nav_history(&self) -> Option<&ItemNavHistory> {
        self.nav_history.as_ref()
    }

    pub fn create_nav_history_entry(&mut self, cx: &mut Context<Self>) {
        self.push_to_nav_history(
            self.selections.newest_anchor().head(),
            None,
            false,
            true,
            cx,
        );
    }

    fn navigation_data(&self, cursor_anchor: Anchor, cx: &mut Context<Self>) -> NavigationData {
        let display_snapshot = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = self.buffer.read(cx).read(cx);
        let cursor_position = cursor_anchor.to_point(&buffer);
        let scroll_anchor = self.scroll_manager.native_anchor(&display_snapshot, cx);
        let scroll_top_row = scroll_anchor.top_row(&buffer);
        drop(buffer);

        NavigationData {
            cursor_anchor,
            cursor_position,
            scroll_anchor,
            scroll_top_row,
        }
    }

    fn navigation_entry(
        &self,
        cursor_anchor: Anchor,
        cx: &mut Context<Self>,
    ) -> Option<NavigationEntry> {
        let Some(history) = self.nav_history.clone() else {
            return None;
        };
        let data = self.navigation_data(cursor_anchor, cx);
        Some(history.navigation_entry(Some(Arc::new(data) as Arc<dyn Any + Send + Sync>)))
    }

    fn push_to_nav_history(
        &mut self,
        cursor_anchor: Anchor,
        new_position: Option<Point>,
        is_deactivate: bool,
        always: bool,
        cx: &mut Context<Self>,
    ) {
        let data = self.navigation_data(cursor_anchor, cx);
        if let Some(nav_history) = self.nav_history.as_mut() {
            if let Some(new_position) = new_position {
                let row_delta = (new_position.row as i64 - data.cursor_position.row as i64).abs();
                if row_delta == 0 || (row_delta < MIN_NAVIGATION_HISTORY_ROW_DELTA && !always) {
                    return;
                }
            }

            let cursor_row = data.cursor_position.row;
            nav_history.push(Some(data), Some(cursor_row), cx);
            cx.emit(EditorEvent::PushedToNavHistory {
                anchor: cursor_anchor,
                is_deactivate,
            })
        }
    }

    pub fn select_to_end(&mut self, _: &SelectToEnd, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let buffer = self.buffer.read(cx).snapshot(cx);
        let mut selection = self
            .selections
            .first::<MultiBufferOffset>(&self.display_snapshot(cx));
        selection.set_head(buffer.len(), SelectionGoal::None);
        self.change_selections(Default::default(), window, cx, |s| {
            s.select(vec![selection]);
        });
    }

    pub fn select_all(&mut self, _: &SelectAll, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
            s.select_ranges(vec![Anchor::Min..Anchor::Max]);
        });
    }

    pub fn select_line(&mut self, _: &SelectLine, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let mut selections = self.selections.all::<Point>(&display_map);
        let max_point = display_map.buffer_snapshot().max_point();
        for selection in &mut selections {
            let rows = selection.spanned_rows(true, &display_map);
            selection.start = Point::new(rows.start.0, 0);
            selection.end = cmp::min(max_point, Point::new(rows.end.0, 0));
            selection.reversed = false;
        }
        self.change_selections(Default::default(), window, cx, |s| {
            s.select(selections);
        });
    }

    pub fn split_selection_into_lines(
        &mut self,
        action: &SplitSelectionIntoLines,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let selections = self
            .selections
            .all::<Point>(&self.display_snapshot(cx))
            .into_iter()
            .map(|selection| selection.start..selection.end)
            .collect::<Vec<_>>();
        self.unfold_ranges(&selections, true, false, cx);

        let mut new_selection_ranges = Vec::new();
        {
            let buffer = self.buffer.read(cx).read(cx);
            for selection in selections {
                for row in selection.start.row..selection.end.row {
                    let line_start = Point::new(row, 0);
                    let line_end = Point::new(row, buffer.line_len(MultiBufferRow(row)));

                    if action.keep_selections {
                        // Keep the selection range for each line
                        let selection_start = if row == selection.start.row {
                            selection.start
                        } else {
                            line_start
                        };
                        new_selection_ranges.push(selection_start..line_end);
                    } else {
                        // Collapse to cursor at end of line
                        new_selection_ranges.push(line_end..line_end);
                    }
                }

                let is_multiline_selection = selection.start.row != selection.end.row;
                // Don't insert last one if it's a multi-line selection ending at the start of a line,
                // so this action feels more ergonomic when paired with other selection operations
                let should_skip_last = is_multiline_selection && selection.end.column == 0;
                if !should_skip_last {
                    if action.keep_selections {
                        if is_multiline_selection {
                            let line_start = Point::new(selection.end.row, 0);
                            new_selection_ranges.push(line_start..selection.end);
                        } else {
                            new_selection_ranges.push(selection.start..selection.end);
                        }
                    } else {
                        new_selection_ranges.push(selection.end..selection.end);
                    }
                }
            }
        }
        self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
            s.select_ranges(new_selection_ranges);
        });
    }

    pub fn add_selection_above(
        &mut self,
        action: &AddSelectionAbove,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.add_selection(true, action.skip_soft_wrap, window, cx);
    }

    pub fn add_selection_below(
        &mut self,
        action: &AddSelectionBelow,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.add_selection(false, action.skip_soft_wrap, window, cx);
    }

    fn add_selection(
        &mut self,
        above: bool,
        skip_soft_wrap: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let all_selections = self.selections.all::<Point>(&display_map);
        let text_layout_details = self.text_layout_details(window, cx);

        let (mut columnar_selections, new_selections_to_columnarize) = {
            if let Some(state) = self.add_selections_state.as_ref() {
                let columnar_selection_ids: HashSet<_> = state
                    .groups
                    .iter()
                    .flat_map(|group| group.stack.iter())
                    .copied()
                    .collect();

                all_selections
                    .into_iter()
                    .partition(|s| columnar_selection_ids.contains(&s.id))
            } else {
                (Vec::new(), all_selections)
            }
        };

        let mut state = self
            .add_selections_state
            .take()
            .unwrap_or_else(|| AddSelectionsState { groups: Vec::new() });

        for selection in new_selections_to_columnarize {
            let range = selection.display_range(&display_map).sorted();
            let start_x = display_map.x_for_display_point(range.start, &text_layout_details);
            let end_x = display_map.x_for_display_point(range.end, &text_layout_details);
            let positions = start_x.min(end_x)..start_x.max(end_x);
            let mut stack = Vec::new();
            for row in range.start.row().0..=range.end.row().0 {
                if let Some(selection) = self.selections.build_columnar_selection(
                    &display_map,
                    DisplayRow(row),
                    &positions,
                    selection.reversed,
                    &text_layout_details,
                ) {
                    stack.push(selection.id);
                    columnar_selections.push(selection);
                }
            }
            if !stack.is_empty() {
                if above {
                    stack.reverse();
                }
                state.groups.push(AddSelectionsGroup { above, stack });
            }
        }

        let mut final_selections = Vec::new();
        let end_row = if above {
            DisplayRow(0)
        } else {
            display_map.max_point().row()
        };

        // When `skip_soft_wrap` is true, we use UTF-16 columns instead of pixel
        // positions to place new selections, so we need to keep track of the
        // column range of the oldest selection in each group, because
        // intermediate selections may have been clamped to shorter lines.
        let mut goal_columns_by_selection_id = if skip_soft_wrap {
            let mut map = HashMap::default();
            for group in state.groups.iter() {
                if let Some(oldest_id) = group.stack.first() {
                    if let Some(oldest_selection) =
                        columnar_selections.iter().find(|s| s.id == *oldest_id)
                    {
                        let snapshot = display_map.buffer_snapshot();
                        let start_col =
                            snapshot.point_to_point_utf16(oldest_selection.start).column;
                        let end_col = snapshot.point_to_point_utf16(oldest_selection.end).column;
                        let goal_columns = start_col.min(end_col)..start_col.max(end_col);
                        for id in &group.stack {
                            map.insert(*id, goal_columns.clone());
                        }
                    }
                }
            }
            map
        } else {
            HashMap::default()
        };

        let mut last_added_item_per_group = HashMap::default();
        for group in state.groups.iter_mut() {
            if let Some(last_id) = group.stack.last() {
                last_added_item_per_group.insert(*last_id, group);
            }
        }

        for selection in columnar_selections {
            if let Some(group) = last_added_item_per_group.get_mut(&selection.id) {
                if above == group.above {
                    let range = selection.display_range(&display_map).sorted();
                    debug_assert_eq!(range.start.row(), range.end.row());
                    let row = range.start.row();
                    let positions =
                        if let SelectionGoal::HorizontalRange { start, end } = selection.goal {
                            Pixels::from(start)..Pixels::from(end)
                        } else {
                            let start_x =
                                display_map.x_for_display_point(range.start, &text_layout_details);
                            let end_x =
                                display_map.x_for_display_point(range.end, &text_layout_details);
                            start_x.min(end_x)..start_x.max(end_x)
                        };

                    let maybe_new_selection = if skip_soft_wrap {
                        let goal_columns = goal_columns_by_selection_id
                            .remove(&selection.id)
                            .unwrap_or_else(|| {
                                let snapshot = display_map.buffer_snapshot();
                                let start_col =
                                    snapshot.point_to_point_utf16(selection.start).column;
                                let end_col = snapshot.point_to_point_utf16(selection.end).column;
                                start_col.min(end_col)..start_col.max(end_col)
                            });
                        self.selections.find_next_columnar_selection_by_buffer_row(
                            &display_map,
                            row,
                            end_row,
                            above,
                            &goal_columns,
                            selection.reversed,
                            &text_layout_details,
                        )
                    } else {
                        self.selections.find_next_columnar_selection_by_display_row(
                            &display_map,
                            row,
                            end_row,
                            above,
                            &positions,
                            selection.reversed,
                            &text_layout_details,
                        )
                    };

                    if let Some(new_selection) = maybe_new_selection {
                        group.stack.push(new_selection.id);
                        if above {
                            final_selections.push(new_selection);
                            final_selections.push(selection);
                        } else {
                            final_selections.push(selection);
                            final_selections.push(new_selection);
                        }
                    } else {
                        final_selections.push(selection);
                    }
                } else {
                    group.stack.pop();
                }
            } else {
                final_selections.push(selection);
            }
        }

        self.change_selections(Default::default(), window, cx, |s| {
            s.select(final_selections);
        });

        let final_selection_ids: HashSet<_> = self
            .selections
            .all::<Point>(&display_map)
            .iter()
            .map(|s| s.id)
            .collect();
        state.groups.retain_mut(|group| {
            // selections might get merged above so we remove invalid items from stacks
            group.stack.retain(|id| final_selection_ids.contains(id));

            // single selection in stack can be treated as initial state
            group.stack.len() > 1
        });

        if !state.groups.is_empty() {
            self.add_selections_state = Some(state);
        }
    }

    pub fn insert_snippet_at_selections(
        &mut self,
        action: &InsertSnippet,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.try_insert_snippet_at_selections(action, window, cx)
            .log_err();
    }

    fn try_insert_snippet_at_selections(
        &mut self,
        action: &InsertSnippet,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<()> {
        let insertion_ranges = self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx))
            .into_iter()
            .map(|selection| selection.range())
            .collect_vec();

        let snippet = if let Some(snippet_body) = &action.snippet {
            if action.language.is_none() && action.name.is_none() {
                Snippet::parse(snippet_body)?
            } else {
                bail!("`snippet` is mutually exclusive with `language` and `name`")
            }
        } else if let Some(name) = &action.name {
            let project = self.project().context("no project")?;
            let snippet_store = project.read(cx).snippets().read(cx);
            let snippet = snippet_store
                .snippets_for(action.language.clone(), cx)
                .into_iter()
                .find(|snippet| snippet.name == *name)
                .context("snippet not found")?;
            Snippet::parse(&snippet.body)?
        } else {
            // todo(andrew): open modal to select snippet
            bail!("`name` or `snippet` is required")
        };

        self.insert_snippet(&insertion_ranges, snippet, window, cx)
    }

    fn select_match_ranges(
        &mut self,
        range: Range<MultiBufferOffset>,
        reversed: bool,
        replace_newest: bool,
        auto_scroll: Option<Autoscroll>,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) {
        self.unfold_ranges(
            std::slice::from_ref(&range),
            false,
            auto_scroll.is_some(),
            cx,
        );
        let effects = if let Some(scroll) = auto_scroll {
            SelectionEffects::scroll(scroll)
        } else {
            SelectionEffects::no_scroll()
        };
        self.change_selections(effects, window, cx, |s| {
            if replace_newest {
                s.delete(s.newest_anchor().id);
            }
            if reversed {
                s.insert_range(range.end..range.start);
            } else {
                s.insert_range(range);
            }
        });
    }

    pub fn select_next_match_internal(
        &mut self,
        display_map: &DisplaySnapshot,
        replace_newest: bool,
        autoscroll: Option<Autoscroll>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<()> {
        let buffer = display_map.buffer_snapshot();
        let mut selections = self.selections.all::<MultiBufferOffset>(&display_map);
        if let Some(mut select_next_state) = self.select_next_state.take() {
            let query = &select_next_state.query;
            if !select_next_state.done {
                let first_selection = selections.iter().min_by_key(|s| s.id).unwrap();
                let last_selection = selections.iter().max_by_key(|s| s.id).unwrap();
                let mut next_selected_range = None;

                let bytes_after_last_selection =
                    buffer.bytes_in_range(last_selection.end..buffer.len());
                let bytes_before_first_selection =
                    buffer.bytes_in_range(MultiBufferOffset(0)..first_selection.start);
                let query_matches = query
                    .stream_find_iter(bytes_after_last_selection)
                    .map(|result| (last_selection.end, result))
                    .chain(
                        query
                            .stream_find_iter(bytes_before_first_selection)
                            .map(|result| (MultiBufferOffset(0), result)),
                    );

                for (start_offset, query_match) in query_matches {
                    let query_match = query_match.unwrap(); // can only fail due to I/O
                    let offset_range =
                        start_offset + query_match.start()..start_offset + query_match.end();

                    if !select_next_state.wordwise
                        || (!buffer.is_inside_word(offset_range.start, None)
                            && !buffer.is_inside_word(offset_range.end, None))
                    {
                        let idx = selections
                            .partition_point(|selection| selection.end <= offset_range.start);
                        let overlaps = selections
                            .get(idx)
                            .map_or(false, |selection| selection.start < offset_range.end);

                        if !overlaps {
                            next_selected_range = Some(offset_range);
                            break;
                        }
                    }
                }

                if let Some(next_selected_range) = next_selected_range {
                    self.select_match_ranges(
                        next_selected_range,
                        last_selection.reversed,
                        replace_newest,
                        autoscroll,
                        window,
                        cx,
                    );
                } else {
                    select_next_state.done = true;
                }
            }

            self.select_next_state = Some(select_next_state);
        } else {
            let mut only_carets = true;
            let mut same_text_selected = true;
            let mut selected_text = None;

            let mut selections_iter = selections.iter().peekable();
            while let Some(selection) = selections_iter.next() {
                if selection.start != selection.end {
                    only_carets = false;
                }

                if same_text_selected {
                    if selected_text.is_none() {
                        selected_text =
                            Some(buffer.text_for_range(selection.range()).collect::<String>());
                    }

                    if let Some(next_selection) = selections_iter.peek() {
                        if next_selection.len() == selection.len() {
                            let next_selected_text = buffer
                                .text_for_range(next_selection.range())
                                .collect::<String>();
                            if Some(next_selected_text) != selected_text {
                                same_text_selected = false;
                                selected_text = None;
                            }
                        } else {
                            same_text_selected = false;
                            selected_text = None;
                        }
                    }
                }
            }

            if only_carets {
                for selection in &mut selections {
                    let (word_range, _) = buffer.surrounding_word(selection.start, None);
                    selection.start = word_range.start;
                    selection.end = word_range.end;
                    selection.goal = SelectionGoal::None;
                    selection.reversed = false;
                    self.select_match_ranges(
                        selection.start..selection.end,
                        selection.reversed,
                        replace_newest,
                        autoscroll,
                        window,
                        cx,
                    );
                }

                if selections.len() == 1 {
                    let selection = selections
                        .last()
                        .expect("ensured that there's only one selection");
                    let query = buffer
                        .text_for_range(selection.start..selection.end)
                        .collect::<String>();
                    let is_empty = query.is_empty();
                    let select_state = SelectNextState {
                        query: self.build_query(&[query], cx)?,
                        wordwise: true,
                        done: is_empty,
                    };
                    self.select_next_state = Some(select_state);
                } else {
                    self.select_next_state = None;
                }
            } else if let Some(selected_text) = selected_text {
                self.select_next_state = Some(SelectNextState {
                    query: self.build_query(&[selected_text], cx)?,
                    wordwise: false,
                    done: false,
                });
                self.select_next_match_internal(
                    display_map,
                    replace_newest,
                    autoscroll,
                    window,
                    cx,
                )?;
            }
        }
        Ok(())
    }

    pub fn select_all_matches(
        &mut self,
        _action: &SelectAllMatches,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<()> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));

        self.select_next_match_internal(&display_map, false, None, window, cx)?;
        let Some(select_next_state) = self.select_next_state.as_mut().filter(|state| !state.done)
        else {
            return Ok(());
        };

        let mut new_selections = Vec::new();
        let initial_selection = self.selections.oldest::<MultiBufferOffset>(&display_map);
        let reversed = initial_selection.reversed;
        let buffer = display_map.buffer_snapshot();
        let query_matches = select_next_state
            .query
            .stream_find_iter(buffer.bytes_in_range(MultiBufferOffset(0)..buffer.len()));

        for query_match in query_matches.into_iter() {
            let query_match = query_match.context("query match for select all action")?; // can only fail due to I/O
            let offset_range = if reversed {
                MultiBufferOffset(query_match.end())..MultiBufferOffset(query_match.start())
            } else {
                MultiBufferOffset(query_match.start())..MultiBufferOffset(query_match.end())
            };

            let is_partial_word_match = select_next_state.wordwise
                && (buffer.is_inside_word(offset_range.start, None)
                    || buffer.is_inside_word(offset_range.end, None));

            let is_initial_selection = MultiBufferOffset(query_match.start())
                == initial_selection.start
                && MultiBufferOffset(query_match.end()) == initial_selection.end;

            if !is_partial_word_match && !is_initial_selection {
                new_selections.push(offset_range);
            }
        }

        // Ensure that the initial range is the last selection, as
        // `MutableSelectionsCollection::select_ranges` makes the last selection
        // the newest selection, which the editor then relies on as the primary
        // cursor for scroll targeting. Without this, the last match would then
        // be automatically focused when the user started editing the selected
        // matches.
        let initial_directed_range = if reversed {
            initial_selection.end..initial_selection.start
        } else {
            initial_selection.start..initial_selection.end
        };
        new_selections.push(initial_directed_range);

        select_next_state.done = true;
        self.unfold_ranges(&new_selections, false, false, cx);
        self.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
            selections.select_ranges(new_selections)
        });

        Ok(())
    }

    pub fn select_next(
        &mut self,
        action: &SelectNext,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<()> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        self.select_next_match_internal(
            &display_map,
            action.replace_newest,
            Some(Autoscroll::newest()),
            window,
            cx,
        )
    }

    pub fn select_previous(
        &mut self,
        action: &SelectPrevious,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<()> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = display_map.buffer_snapshot();
        let mut selections = self.selections.all::<MultiBufferOffset>(&display_map);
        if let Some(mut select_prev_state) = self.select_prev_state.take() {
            let query = &select_prev_state.query;
            if !select_prev_state.done {
                let first_selection = selections.iter().min_by_key(|s| s.id).unwrap();
                let last_selection = selections.iter().max_by_key(|s| s.id).unwrap();
                let mut next_selected_range = None;
                // When we're iterating matches backwards, the oldest match will actually be the furthest one in the buffer.
                let bytes_before_last_selection =
                    buffer.reversed_bytes_in_range(MultiBufferOffset(0)..last_selection.start);
                let bytes_after_first_selection =
                    buffer.reversed_bytes_in_range(first_selection.end..buffer.len());
                let query_matches = query
                    .stream_find_iter(bytes_before_last_selection)
                    .map(|result| (last_selection.start, result))
                    .chain(
                        query
                            .stream_find_iter(bytes_after_first_selection)
                            .map(|result| (buffer.len(), result)),
                    );
                for (end_offset, query_match) in query_matches {
                    let query_match = query_match.unwrap(); // can only fail due to I/O
                    let offset_range =
                        end_offset - query_match.end()..end_offset - query_match.start();

                    if !select_prev_state.wordwise
                        || (!buffer.is_inside_word(offset_range.start, None)
                            && !buffer.is_inside_word(offset_range.end, None))
                    {
                        next_selected_range = Some(offset_range);
                        break;
                    }
                }

                if let Some(next_selected_range) = next_selected_range {
                    self.select_match_ranges(
                        next_selected_range,
                        last_selection.reversed,
                        action.replace_newest,
                        Some(Autoscroll::newest()),
                        window,
                        cx,
                    );
                } else {
                    select_prev_state.done = true;
                }
            }

            self.select_prev_state = Some(select_prev_state);
        } else {
            let mut only_carets = true;
            let mut same_text_selected = true;
            let mut selected_text = None;

            let mut selections_iter = selections.iter().peekable();
            while let Some(selection) = selections_iter.next() {
                if selection.start != selection.end {
                    only_carets = false;
                }

                if same_text_selected {
                    if selected_text.is_none() {
                        selected_text =
                            Some(buffer.text_for_range(selection.range()).collect::<String>());
                    }

                    if let Some(next_selection) = selections_iter.peek() {
                        if next_selection.len() == selection.len() {
                            let next_selected_text = buffer
                                .text_for_range(next_selection.range())
                                .collect::<String>();
                            if Some(next_selected_text) != selected_text {
                                same_text_selected = false;
                                selected_text = None;
                            }
                        } else {
                            same_text_selected = false;
                            selected_text = None;
                        }
                    }
                }
            }

            if only_carets {
                for selection in &mut selections {
                    let (word_range, _) = buffer.surrounding_word(selection.start, None);
                    selection.start = word_range.start;
                    selection.end = word_range.end;
                    selection.goal = SelectionGoal::None;
                    selection.reversed = false;
                    self.select_match_ranges(
                        selection.start..selection.end,
                        selection.reversed,
                        action.replace_newest,
                        Some(Autoscroll::newest()),
                        window,
                        cx,
                    );
                }
                if selections.len() == 1 {
                    let selection = selections
                        .last()
                        .expect("ensured that there's only one selection");
                    let query = buffer
                        .text_for_range(selection.start..selection.end)
                        .collect::<String>();
                    let is_empty = query.is_empty();
                    let select_state = SelectNextState {
                        query: self.build_query(&[query.chars().rev().collect::<String>()], cx)?,
                        wordwise: true,
                        done: is_empty,
                    };
                    self.select_prev_state = Some(select_state);
                } else {
                    self.select_prev_state = None;
                }
            } else if let Some(selected_text) = selected_text {
                self.select_prev_state = Some(SelectNextState {
                    query: self
                        .build_query(&[selected_text.chars().rev().collect::<String>()], cx)?,
                    wordwise: false,
                    done: false,
                });
                self.select_previous(action, window, cx)?;
            }
        }
        Ok(())
    }

    /// Builds an `AhoCorasick` automaton from the provided patterns, while
    /// setting the case sensitivity based on the global
    /// `SelectNextCaseSensitive` setting, if set, otherwise based on the
    /// editor's settings.
    fn build_query<I, P>(&self, patterns: I, cx: &Context<Self>) -> Result<AhoCorasick, BuildError>
    where
        I: IntoIterator<Item = P>,
        P: AsRef<[u8]>,
    {
        let case_sensitive = self
            .select_next_is_case_sensitive
            .unwrap_or_else(|| EditorSettings::get_global(cx).search.case_sensitive);

        let mut builder = AhoCorasickBuilder::new();
        builder.ascii_case_insensitive(!case_sensitive);
        builder.build(patterns)
    }

    pub fn find_next_match(
        &mut self,
        _: &FindNextMatch,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<()> {
        let selections = self.selections.disjoint_anchors_arc();
        match selections.first() {
            Some(first) if selections.len() >= 2 => {
                self.change_selections(Default::default(), window, cx, |s| {
                    s.select_ranges([first.range()]);
                });
            }
            _ => self.select_next(
                &SelectNext {
                    replace_newest: true,
                },
                window,
                cx,
            )?,
        }
        Ok(())
    }

    pub fn find_previous_match(
        &mut self,
        _: &FindPreviousMatch,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<()> {
        let selections = self.selections.disjoint_anchors_arc();
        match selections.last() {
            Some(last) if selections.len() >= 2 => {
                self.change_selections(Default::default(), window, cx, |s| {
                    s.select_ranges([last.range()]);
                });
            }
            _ => self.select_previous(
                &SelectPrevious {
                    replace_newest: true,
                },
                window,
                cx,
            )?,
        }
        Ok(())
    }

    pub fn toggle_comments(
        &mut self,
        action: &ToggleComments,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.read_only(cx) {
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let text_layout_details = &self.text_layout_details(window, cx);
        self.transact(window, cx, |this, window, cx| {
            let mut selections = this
                .selections
                .all::<MultiBufferPoint>(&this.display_snapshot(cx));
            let mut edits = Vec::new();
            let mut selection_edit_ranges = Vec::new();
            let mut last_toggled_row = None;
            let snapshot = this.buffer.read(cx).read(cx);
            let empty_str: Arc<str> = Arc::default();
            let mut suffixes_inserted = Vec::new();
            let ignore_indent = action.ignore_indent;

            fn comment_prefix_range(
                snapshot: &MultiBufferSnapshot,
                row: MultiBufferRow,
                comment_prefix: &str,
                comment_prefix_whitespace: &str,
                ignore_indent: bool,
            ) -> Range<Point> {
                let indent_size = if ignore_indent {
                    0
                } else {
                    snapshot.indent_size_for_line(row).len
                };

                let start = Point::new(row.0, indent_size);

                let mut line_bytes = snapshot
                    .bytes_in_range(start..snapshot.max_point())
                    .flatten()
                    .copied();

                // If this line currently begins with the line comment prefix, then record
                // the range containing the prefix.
                if line_bytes
                    .by_ref()
                    .take(comment_prefix.len())
                    .eq(comment_prefix.bytes())
                {
                    // Include any whitespace that matches the comment prefix.
                    let matching_whitespace_len = line_bytes
                        .zip(comment_prefix_whitespace.bytes())
                        .take_while(|(a, b)| a == b)
                        .count() as u32;
                    let end = Point::new(
                        start.row,
                        start.column + comment_prefix.len() as u32 + matching_whitespace_len,
                    );
                    start..end
                } else {
                    start..start
                }
            }

            fn comment_suffix_range(
                snapshot: &MultiBufferSnapshot,
                row: MultiBufferRow,
                comment_suffix: &str,
                comment_suffix_has_leading_space: bool,
            ) -> Range<Point> {
                let end = Point::new(row.0, snapshot.line_len(row));
                let suffix_start_column = end.column.saturating_sub(comment_suffix.len() as u32);

                let mut line_end_bytes = snapshot
                    .bytes_in_range(Point::new(end.row, suffix_start_column.saturating_sub(1))..end)
                    .flatten()
                    .copied();

                let leading_space_len = if suffix_start_column > 0
                    && line_end_bytes.next() == Some(b' ')
                    && comment_suffix_has_leading_space
                {
                    1
                } else {
                    0
                };

                // If this line currently begins with the line comment prefix, then record
                // the range containing the prefix.
                if line_end_bytes.by_ref().eq(comment_suffix.bytes()) {
                    let start = Point::new(end.row, suffix_start_column - leading_space_len);
                    start..end
                } else {
                    end..end
                }
            }

            // TODO: Handle selections that cross excerpts
            for selection in &mut selections {
                let start_column = snapshot
                    .indent_size_for_line(MultiBufferRow(selection.start.row))
                    .len;
                let language = if let Some(language) =
                    snapshot.language_scope_at(Point::new(selection.start.row, start_column))
                {
                    language
                } else {
                    continue;
                };

                selection_edit_ranges.clear();

                // If multiple selections contain a given row, avoid processing that
                // row more than once.
                let mut start_row = MultiBufferRow(selection.start.row);
                if last_toggled_row == Some(start_row) {
                    start_row = start_row.next_row();
                }
                let end_row =
                    if selection.end.row > selection.start.row && selection.end.column == 0 {
                        MultiBufferRow(selection.end.row - 1)
                    } else {
                        MultiBufferRow(selection.end.row)
                    };
                last_toggled_row = Some(end_row);

                if start_row > end_row {
                    continue;
                }

                // If the language has line comments, toggle those.
                let mut full_comment_prefixes = language.line_comment_prefixes().to_vec();

                // If ignore_indent is set, trim spaces from the right side of all full_comment_prefixes
                if ignore_indent {
                    full_comment_prefixes = full_comment_prefixes
                        .into_iter()
                        .map(|s| Arc::from(s.trim_end()))
                        .collect();
                }

                if !full_comment_prefixes.is_empty() {
                    let first_prefix = full_comment_prefixes
                        .first()
                        .expect("prefixes is non-empty");
                    let prefix_trimmed_lengths = full_comment_prefixes
                        .iter()
                        .map(|p| p.trim_end_matches(' ').len())
                        .collect::<SmallVec<[usize; 4]>>();

                    let mut all_selection_lines_are_comments = true;

                    for row in start_row.0..=end_row.0 {
                        let row = MultiBufferRow(row);
                        if start_row < end_row && snapshot.is_line_blank(row) {
                            continue;
                        }

                        let prefix_range = full_comment_prefixes
                            .iter()
                            .zip(prefix_trimmed_lengths.iter().copied())
                            .map(|(prefix, trimmed_prefix_len)| {
                                comment_prefix_range(
                                    snapshot.deref(),
                                    row,
                                    &prefix[..trimmed_prefix_len],
                                    &prefix[trimmed_prefix_len..],
                                    ignore_indent,
                                )
                            })
                            .max_by_key(|range| range.end.column - range.start.column)
                            .expect("prefixes is non-empty");

                        if prefix_range.is_empty() {
                            all_selection_lines_are_comments = false;
                        }

                        selection_edit_ranges.push(prefix_range);
                    }

                    if all_selection_lines_are_comments {
                        edits.extend(
                            selection_edit_ranges
                                .iter()
                                .cloned()
                                .map(|range| (range, empty_str.clone())),
                        );
                    } else {
                        let min_column = selection_edit_ranges
                            .iter()
                            .map(|range| range.start.column)
                            .min()
                            .unwrap_or(0);
                        edits.extend(selection_edit_ranges.iter().map(|range| {
                            let position = Point::new(range.start.row, min_column);
                            (position..position, first_prefix.clone())
                        }));
                    }
                } else if let Some(BlockCommentConfig {
                    start: full_comment_prefix,
                    end: comment_suffix,
                    ..
                }) = language.block_comment()
                {
                    let comment_prefix = full_comment_prefix.trim_end_matches(' ');
                    let comment_prefix_whitespace = &full_comment_prefix[comment_prefix.len()..];
                    let prefix_range = comment_prefix_range(
                        snapshot.deref(),
                        start_row,
                        comment_prefix,
                        comment_prefix_whitespace,
                        ignore_indent,
                    );
                    let suffix_range = comment_suffix_range(
                        snapshot.deref(),
                        end_row,
                        comment_suffix.trim_start_matches(' '),
                        comment_suffix.starts_with(' '),
                    );

                    if prefix_range.is_empty() || suffix_range.is_empty() {
                        edits.push((
                            prefix_range.start..prefix_range.start,
                            full_comment_prefix.clone(),
                        ));
                        edits.push((suffix_range.end..suffix_range.end, comment_suffix.clone()));
                        suffixes_inserted.push((end_row, comment_suffix.len()));
                    } else {
                        edits.push((prefix_range, empty_str.clone()));
                        edits.push((suffix_range, empty_str.clone()));
                    }
                } else {
                    continue;
                }
            }

            drop(snapshot);
            this.buffer.update(cx, |buffer, cx| {
                buffer.edit(edits, None, cx);
            });

            // Adjust selections so that they end before any comment suffixes that
            // were inserted.
            let mut suffixes_inserted = suffixes_inserted.into_iter().peekable();
            let mut selections = this.selections.all::<Point>(&this.display_snapshot(cx));
            let snapshot = this.buffer.read(cx).read(cx);
            for selection in &mut selections {
                while let Some((row, suffix_len)) = suffixes_inserted.peek().copied() {
                    match row.cmp(&MultiBufferRow(selection.end.row)) {
                        Ordering::Less => {
                            suffixes_inserted.next();
                            continue;
                        }
                        Ordering::Greater => break,
                        Ordering::Equal => {
                            if selection.end.column == snapshot.line_len(row) {
                                if selection.is_empty() {
                                    selection.start.column -= suffix_len as u32;
                                }
                                selection.end.column -= suffix_len as u32;
                            }
                            break;
                        }
                    }
                }
            }

            drop(snapshot);
            this.change_selections(Default::default(), window, cx, |s| s.select(selections));

            let selections = this.selections.all::<Point>(&this.display_snapshot(cx));
            let selections_on_single_row = selections.windows(2).all(|selections| {
                selections[0].start.row == selections[1].start.row
                    && selections[0].end.row == selections[1].end.row
                    && selections[0].start.row == selections[0].end.row
            });
            let selections_selecting = selections
                .iter()
                .any(|selection| selection.start != selection.end);
            let advance_downwards = action.advance_downwards
                && selections_on_single_row
                && !selections_selecting
                && !matches!(this.mode, EditorMode::SingleLine);

            if advance_downwards {
                let snapshot = this.buffer.read(cx).snapshot(cx);

                this.change_selections(Default::default(), window, cx, |s| {
                    s.move_cursors_with(&mut |display_snapshot, display_point, _| {
                        let mut point = display_point.to_point(display_snapshot);
                        point.row += 1;
                        point = snapshot.clip_point(point, Bias::Left);
                        let display_point = point.to_display_point(display_snapshot);
                        let goal = SelectionGoal::HorizontalPosition(
                            display_snapshot
                                .x_for_display_point(display_point, text_layout_details)
                                .into(),
                        );
                        (display_point, goal)
                    })
                });
            }
        });
    }

    pub fn select_enclosing_symbol(
        &mut self,
        _: &SelectEnclosingSymbol,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let buffer = self.buffer.read(cx).snapshot(cx);
        let old_selections = self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx))
            .into_boxed_slice();

        fn update_selection(
            selection: &Selection<MultiBufferOffset>,
            buffer_snap: &MultiBufferSnapshot,
        ) -> Option<Selection<MultiBufferOffset>> {
            let cursor = selection.head();
            let (_buffer_id, symbols) = buffer_snap.symbols_containing(cursor, None)?;
            for symbol in symbols.iter().rev() {
                let start = symbol.range.start.to_offset(buffer_snap);
                let end = symbol.range.end.to_offset(buffer_snap);
                let new_range = start..end;
                if start < selection.start || end > selection.end {
                    return Some(Selection {
                        id: selection.id,
                        start: new_range.start,
                        end: new_range.end,
                        goal: SelectionGoal::None,
                        reversed: selection.reversed,
                    });
                }
            }
            None
        }

        let mut selected_larger_symbol = false;
        let new_selections = old_selections
            .iter()
            .map(|selection| match update_selection(selection, &buffer) {
                Some(new_selection) => {
                    if new_selection.range() != selection.range() {
                        selected_larger_symbol = true;
                    }
                    new_selection
                }
                None => selection.clone(),
            })
            .collect::<Vec<_>>();

        if selected_larger_symbol {
            self.change_selections(Default::default(), window, cx, |s| {
                s.select(new_selections);
            });
        }
    }

    pub fn select_larger_syntax_node(
        &mut self,
        _: &SelectLargerSyntaxNode,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(visible_row_count) = self.visible_row_count() else {
            return;
        };
        let old_selections: Box<[_]> = self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx))
            .into();
        if old_selections.is_empty() {
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = self.buffer.read(cx).snapshot(cx);

        let mut selected_larger_node = false;
        let mut new_selections = old_selections
            .iter()
            .map(|selection| {
                let old_range = selection.start..selection.end;

                if let Some((node, _)) = buffer.syntax_ancestor(old_range.clone()) {
                    // manually select word at selection
                    if ["string_content", "inline"].contains(&node.kind()) {
                        let (word_range, _) = buffer.surrounding_word(old_range.start, None);
                        // ignore if word is already selected
                        if !word_range.is_empty() && old_range != word_range {
                            let (last_word_range, _) = buffer.surrounding_word(old_range.end, None);
                            // only select word if start and end point belongs to same word
                            if word_range == last_word_range {
                                selected_larger_node = true;
                                return Selection {
                                    id: selection.id,
                                    start: word_range.start,
                                    end: word_range.end,
                                    goal: SelectionGoal::None,
                                    reversed: selection.reversed,
                                };
                            }
                        }
                    }
                }

                let mut new_range = old_range.clone();
                while let Some((node, range)) = buffer.syntax_ancestor(new_range.clone()) {
                    new_range = range;
                    if !node.is_named() {
                        continue;
                    }
                    if !display_map.intersects_fold(new_range.start)
                        && !display_map.intersects_fold(new_range.end)
                    {
                        break;
                    }
                }

                selected_larger_node |= new_range != old_range;
                Selection {
                    id: selection.id,
                    start: new_range.start,
                    end: new_range.end,
                    goal: SelectionGoal::None,
                    reversed: selection.reversed,
                }
            })
            .collect::<Vec<_>>();

        if !selected_larger_node {
            return; // don't put this call in the history
        }

        // scroll based on transformation done to the last selection created by the user
        let (last_old, last_new) = old_selections
            .last()
            .zip(new_selections.last().cloned())
            .expect("old_selections isn't empty");

        let is_selection_reversed = if new_selections.len() == 1 {
            let should_be_reversed = last_old.start != last_new.start;
            new_selections.last_mut().expect("checked above").reversed = should_be_reversed;
            should_be_reversed
        } else {
            last_new.reversed
        };

        if selected_larger_node {
            self.select_syntax_node_history.disable_clearing = true;
            self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                s.select(new_selections.clone());
            });
            self.select_syntax_node_history.disable_clearing = false;
        }

        let start_row = last_new.start.to_display_point(&display_map).row().0;
        let end_row = last_new.end.to_display_point(&display_map).row().0;
        let selection_height = end_row - start_row + 1;
        let scroll_margin_rows = self.vertical_scroll_margin() as u32;

        let fits_on_the_screen = visible_row_count >= selection_height + scroll_margin_rows * 2;
        let scroll_behavior = if fits_on_the_screen {
            self.request_autoscroll(Autoscroll::fit(), cx);
            SelectSyntaxNodeScrollBehavior::FitSelection
        } else if is_selection_reversed {
            self.scroll_cursor_top(&ScrollCursorTop, window, cx);
            SelectSyntaxNodeScrollBehavior::CursorTop
        } else {
            self.scroll_cursor_bottom(&ScrollCursorBottom, window, cx);
            SelectSyntaxNodeScrollBehavior::CursorBottom
        };

        let old_selections: Box<[Selection<Anchor>]> = old_selections
            .iter()
            .map(|s| s.map(|offset| buffer.anchor_before(offset)))
            .collect();
        self.select_syntax_node_history.push((
            old_selections,
            scroll_behavior,
            is_selection_reversed,
        ));
    }

    pub fn select_smaller_syntax_node(
        &mut self,
        _: &SelectSmallerSyntaxNode,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        if let Some((mut selections, scroll_behavior, is_selection_reversed)) =
            self.select_syntax_node_history.pop()
        {
            if let Some(selection) = selections.last_mut() {
                selection.reversed = is_selection_reversed;
            }

            let snapshot = self.buffer.read(cx).snapshot(cx);
            let selections: Vec<Selection<MultiBufferOffset>> = selections
                .iter()
                .map(|s| s.map(|anchor| anchor.to_offset(&snapshot)))
                .collect();

            self.select_syntax_node_history.disable_clearing = true;
            self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                s.select(selections);
            });
            self.select_syntax_node_history.disable_clearing = false;

            match scroll_behavior {
                SelectSyntaxNodeScrollBehavior::CursorTop => {
                    self.scroll_cursor_top(&ScrollCursorTop, window, cx);
                }
                SelectSyntaxNodeScrollBehavior::FitSelection => {
                    self.request_autoscroll(Autoscroll::fit(), cx);
                }
                SelectSyntaxNodeScrollBehavior::CursorBottom => {
                    self.scroll_cursor_bottom(&ScrollCursorBottom, window, cx);
                }
            }
        }
    }

    pub fn unwrap_syntax_node(
        &mut self,
        _: &UnwrapSyntaxNode,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let buffer = self.buffer.read(cx).snapshot(cx);
        let selections = self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx))
            .into_iter()
            // subtracting the offset requires sorting
            .sorted_by_key(|i| i.start);

        let full_edits = selections
            .into_iter()
            .filter_map(|selection| {
                let child = if selection.is_empty()
                    && let Some((_, ancestor_range)) =
                        buffer.syntax_ancestor(selection.start..selection.end)
                {
                    ancestor_range
                } else {
                    selection.range()
                };

                let mut parent = child.clone();
                while let Some((_, ancestor_range)) = buffer.syntax_ancestor(parent.clone()) {
                    parent = ancestor_range;
                    if parent.start < child.start || parent.end > child.end {
                        break;
                    }
                }

                if parent == child {
                    return None;
                }
                let text = buffer.text_for_range(child).collect::<String>();
                Some((selection.id, parent, text))
            })
            .collect::<Vec<_>>();
        if full_edits.is_empty() {
            return;
        }

        self.transact(window, cx, |this, window, cx| {
            this.buffer.update(cx, |buffer, cx| {
                buffer.edit(
                    full_edits
                        .iter()
                        .map(|(_, p, t)| (p.clone(), t.clone()))
                        .collect::<Vec<_>>(),
                    None,
                    cx,
                );
            });
            this.change_selections(Default::default(), window, cx, |s| {
                let mut offset = 0;
                let mut selections = vec![];
                for (id, parent, text) in full_edits {
                    let start = parent.start - offset;
                    offset += (parent.end - parent.start) - text.len();
                    selections.push(Selection {
                        id,
                        start,
                        end: start + text.len(),
                        reversed: false,
                        goal: Default::default(),
                    });
                }
                s.select(selections);
            });
        });
    }

    pub fn select_next_syntax_node(
        &mut self,
        _: &SelectNextSyntaxNode,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let old_selections = self.selections.all_anchors(&self.display_snapshot(cx));
        if old_selections.is_empty() {
            return;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let buffer = self.buffer.read(cx).snapshot(cx);
        let mut selected_sibling = false;

        let new_selections = old_selections
            .iter()
            .map(|selection| {
                let old_range =
                    selection.start.to_offset(&buffer)..selection.end.to_offset(&buffer);
                if let Some(results) = buffer.map_excerpt_ranges(
                    old_range,
                    |buf, _excerpt_range, input_buffer_range| {
                        let Some(node) = buf.syntax_next_sibling(input_buffer_range) else {
                            return Vec::new();
                        };
                        vec![(
                            BufferOffset(node.byte_range().start)
                                ..BufferOffset(node.byte_range().end),
                            (),
                        )]
                    },
                ) && let [(new_range, _)] = results.as_slice()
                {
                    selected_sibling = true;
                    let new_range =
                        buffer.anchor_after(new_range.start)..buffer.anchor_before(new_range.end);
                    Selection {
                        id: selection.id,
                        start: new_range.start,
                        end: new_range.end,
                        goal: SelectionGoal::None,
                        reversed: selection.reversed,
                    }
                } else {
                    selection.clone()
                }
            })
            .collect::<Vec<_>>();

        if selected_sibling {
            self.change_selections(
                SelectionEffects::scroll(Autoscroll::fit()),
                window,
                cx,
                |s| {
                    s.select(new_selections);
                },
            );
        }
    }

    pub fn select_prev_syntax_node(
        &mut self,
        _: &SelectPreviousSyntaxNode,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let old_selections: Arc<[_]> = self.selections.all_anchors(&self.display_snapshot(cx));

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let multibuffer_snapshot = self.buffer.read(cx).snapshot(cx);
        let mut selected_sibling = false;

        let new_selections = old_selections
            .iter()
            .map(|selection| {
                let old_range = selection.start.to_offset(&multibuffer_snapshot)
                    ..selection.end.to_offset(&multibuffer_snapshot);
                if let Some(results) = multibuffer_snapshot.map_excerpt_ranges(
                    old_range,
                    |buf, _excerpt_range, input_buffer_range| {
                        let Some(node) = buf.syntax_prev_sibling(input_buffer_range) else {
                            return Vec::new();
                        };
                        vec![(
                            BufferOffset(node.byte_range().start)
                                ..BufferOffset(node.byte_range().end),
                            (),
                        )]
                    },
                ) && let [(new_range, _)] = results.as_slice()
                {
                    selected_sibling = true;
                    let new_range = multibuffer_snapshot.anchor_after(new_range.start)
                        ..multibuffer_snapshot.anchor_before(new_range.end);
                    Selection {
                        id: selection.id,
                        start: new_range.start,
                        end: new_range.end,
                        goal: SelectionGoal::None,
                        reversed: selection.reversed,
                    }
                } else {
                    selection.clone()
                }
            })
            .collect::<Vec<_>>();

        if selected_sibling {
            self.change_selections(
                SelectionEffects::scroll(Autoscroll::fit()),
                window,
                cx,
                |s| {
                    s.select(new_selections);
                },
            );
        }
    }

    pub fn move_to_start_of_larger_syntax_node(
        &mut self,
        _: &MoveToStartOfLargerSyntaxNode,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.move_cursors_to_syntax_nodes(window, cx, false);
    }

    pub fn move_to_end_of_larger_syntax_node(
        &mut self,
        _: &MoveToEndOfLargerSyntaxNode,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.move_cursors_to_syntax_nodes(window, cx, true);
    }

    fn find_syntax_node_boundary(
        &self,
        selection_pos: MultiBufferOffset,
        move_to_end: bool,
        display_map: &DisplaySnapshot,
        buffer: &MultiBufferSnapshot,
    ) -> MultiBufferOffset {
        let old_range = selection_pos..selection_pos;
        let mut new_pos = selection_pos;
        let mut search_range = old_range;
        while let Some((node, range)) = buffer.syntax_ancestor(search_range.clone()) {
            search_range = range.clone();
            if !node.is_named()
                || display_map.intersects_fold(range.start)
                || display_map.intersects_fold(range.end)
                // If cursor is already at the end of the syntax node, continue searching
                || (move_to_end && range.end == selection_pos)
                // If cursor is already at the start of the syntax node, continue searching
                || (!move_to_end && range.start == selection_pos)
            {
                continue;
            }

            // If we found a string_content node, find the largest parent that is still string_content
            // Enables us to skip to the end of strings without taking multiple steps inside the string
            let (_, final_range) = if node.kind() == "string_content" {
                let mut current_node = node;
                let mut current_range = range;
                while let Some((parent, parent_range)) =
                    buffer.syntax_ancestor(current_range.clone())
                {
                    if parent.kind() == "string_content" {
                        current_node = parent;
                        current_range = parent_range;
                    } else {
                        break;
                    }
                }

                (current_node, current_range)
            } else {
                (node, range)
            };

            new_pos = if move_to_end {
                final_range.end
            } else {
                final_range.start
            };

            break;
        }

        new_pos
    }

    fn move_cursors_to_syntax_nodes(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
        move_to_end: bool,
    ) -> bool {
        let old_selections: Box<[_]> = self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx))
            .into();
        if old_selections.is_empty() {
            return false;
        }

        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = self.buffer.read(cx).snapshot(cx);

        let mut any_cursor_moved = false;
        let new_selections = old_selections
            .iter()
            .map(|selection| {
                if !selection.is_empty() {
                    return selection.clone();
                }

                let selection_pos = selection.head();
                let new_pos = self.find_syntax_node_boundary(
                    selection_pos,
                    move_to_end,
                    &display_map,
                    &buffer,
                );

                any_cursor_moved |= new_pos != selection_pos;

                Selection {
                    id: selection.id,
                    start: new_pos,
                    end: new_pos,
                    goal: SelectionGoal::None,
                    reversed: false,
                }
            })
            .collect::<Vec<_>>();

        self.change_selections(Default::default(), window, cx, |s| {
            s.select(new_selections);
        });
        self.request_autoscroll(Autoscroll::newest(), cx);

        any_cursor_moved
    }

    pub fn select_to_start_of_larger_syntax_node(
        &mut self,
        _: &SelectToStartOfLargerSyntaxNode,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.select_to_syntax_nodes(window, cx, false);
    }

    pub fn select_to_end_of_larger_syntax_node(
        &mut self,
        _: &SelectToEndOfLargerSyntaxNode,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.select_to_syntax_nodes(window, cx, true);
    }

    fn select_to_syntax_nodes(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
        move_to_end: bool,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let buffer = self.buffer.read(cx).snapshot(cx);
        let old_selections = self.selections.all::<MultiBufferOffset>(&display_map);

        let new_selections = old_selections
            .iter()
            .map(|selection| {
                let new_pos = self.find_syntax_node_boundary(
                    selection.head(),
                    move_to_end,
                    &display_map,
                    &buffer,
                );

                let mut new_selection = selection.clone();
                new_selection.set_head(new_pos, SelectionGoal::None);
                new_selection
            })
            .collect::<Vec<_>>();

        self.change_selections(Default::default(), window, cx, |s| {
            s.select(new_selections);
        });
    }

    pub fn move_to_enclosing_bracket(
        &mut self,
        _: &MoveToEnclosingBracket,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.change_selections(Default::default(), window, cx, |s| {
            s.move_offsets_with(&mut |snapshot, selection| {
                let Some(enclosing_bracket_ranges) =
                    snapshot.enclosing_bracket_ranges(selection.start..selection.end)
                else {
                    return;
                };

                let mut best_length = usize::MAX;
                let mut best_inside = false;
                let mut best_in_bracket_range = false;
                let mut best_destination = None;
                for (open, close) in enclosing_bracket_ranges {
                    let close = close.to_inclusive();
                    let length = *close.end() - open.start;
                    let inside = selection.start >= open.end && selection.end <= *close.start();
                    let in_bracket_range = open.to_inclusive().contains(&selection.head())
                        || close.contains(&selection.head());

                    // If best is next to a bracket and current isn't, skip
                    if !in_bracket_range && best_in_bracket_range {
                        continue;
                    }

                    // Prefer smaller lengths unless best is inside and current isn't
                    if length > best_length && (best_inside || !inside) {
                        continue;
                    }

                    best_length = length;
                    best_inside = inside;
                    best_in_bracket_range = in_bracket_range;
                    best_destination = Some(
                        if close.contains(&selection.start) && close.contains(&selection.end) {
                            if inside { open.end } else { open.start }
                        } else if inside {
                            *close.start()
                        } else {
                            *close.end()
                        },
                    );
                }

                if let Some(destination) = best_destination {
                    selection.collapse_to(destination, SelectionGoal::None);
                }
            })
        });
    }

    pub fn undo_selection(
        &mut self,
        _: &UndoSelection,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        if let Some(entry) = self.selection_history.undo_stack.pop_back() {
            self.selection_history.mode = SelectionHistoryMode::Undoing;
            self.with_selection_effects_deferred(window, cx, |this, window, cx| {
                this.end_selection(window, cx);
                this.change_selections(
                    SelectionEffects::scroll(Autoscroll::newest()),
                    window,
                    cx,
                    |s| s.select_anchors(entry.selections.to_vec()),
                );
            });
            self.selection_history.mode = SelectionHistoryMode::Normal;

            self.select_next_state = entry.select_next_state;
            self.select_prev_state = entry.select_prev_state;
            self.add_selections_state = entry.add_selections_state;
        }
    }

    pub fn redo_selection(
        &mut self,
        _: &RedoSelection,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        if let Some(entry) = self.selection_history.redo_stack.pop_back() {
            self.selection_history.mode = SelectionHistoryMode::Redoing;
            self.with_selection_effects_deferred(window, cx, |this, window, cx| {
                this.end_selection(window, cx);
                this.change_selections(
                    SelectionEffects::scroll(Autoscroll::newest()),
                    window,
                    cx,
                    |s| s.select_anchors(entry.selections.to_vec()),
                );
            });
            self.selection_history.mode = SelectionHistoryMode::Normal;

            self.select_next_state = entry.select_next_state;
            self.select_prev_state = entry.select_prev_state;
            self.add_selections_state = entry.add_selections_state;
        }
    }

    pub fn expand_excerpts(
        &mut self,
        action: &ExpandExcerpts,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.expand_excerpts_for_direction(action.lines, ExpandExcerptDirection::UpAndDown, cx)
    }

    pub fn expand_excerpts_down(
        &mut self,
        action: &ExpandExcerptsDown,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.expand_excerpts_for_direction(action.lines, ExpandExcerptDirection::Down, cx)
    }

    pub fn expand_excerpts_up(
        &mut self,
        action: &ExpandExcerptsUp,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.expand_excerpts_for_direction(action.lines, ExpandExcerptDirection::Up, cx)
    }

    pub fn expand_excerpts_for_direction(
        &mut self,
        lines: u32,
        direction: ExpandExcerptDirection,
        cx: &mut Context<Self>,
    ) {
        let selections = self.selections.disjoint_anchors_arc();

        let lines = if lines == 0 {
            EditorSettings::get_global(cx).expand_excerpt_lines
        } else {
            lines
        };

        let snapshot = self.buffer.read(cx).snapshot(cx);
        let excerpt_anchors = selections
            .iter()
            .flat_map(|selection| {
                snapshot
                    .range_to_buffer_ranges(selection.range())
                    .into_iter()
                    .filter_map(|(buffer_snapshot, range, _)| {
                        snapshot.anchor_in_excerpt(buffer_snapshot.anchor_after(range.start))
                    })
            })
            .collect::<Vec<_>>();

        if self.delegate_expand_excerpts {
            cx.emit(EditorEvent::ExpandExcerptsRequested {
                excerpt_anchors,
                lines,
                direction,
            });
            return;
        }

        self.buffer.update(cx, |buffer, cx| {
            buffer.expand_excerpts(excerpt_anchors, lines, direction, cx)
        })
    }

    pub(crate) fn expand_excerpt(
        &mut self,
        excerpt_anchor: Anchor,
        direction: ExpandExcerptDirection,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let lines_to_expand = EditorSettings::get_global(cx).expand_excerpt_lines;

        if self.delegate_expand_excerpts {
            cx.emit(EditorEvent::ExpandExcerptsRequested {
                excerpt_anchors: vec![excerpt_anchor],
                lines: lines_to_expand,
                direction,
            });
            return;
        }

        let current_scroll_position = self.scroll_position(cx);
        let mut scroll = None;

        if direction == ExpandExcerptDirection::Down {
            let multi_buffer = self.buffer.read(cx);
            let snapshot = multi_buffer.snapshot(cx);
            if let Some((buffer_snapshot, excerpt_range)) =
                snapshot.excerpt_containing(excerpt_anchor..excerpt_anchor)
            {
                let excerpt_end_row =
                    Point::from_anchor(&excerpt_range.context.end, &buffer_snapshot).row;
                let last_row = buffer_snapshot.max_point().row;
                let lines_below = last_row.saturating_sub(excerpt_end_row);
                if lines_below >= lines_to_expand {
                    scroll = Some(
                        current_scroll_position
                            + gpui::Point::new(0.0, lines_to_expand as ScrollOffset),
                    );
                }
            }
        }
        if direction == ExpandExcerptDirection::Up
            && self
                .buffer
                .read(cx)
                .snapshot(cx)
                .excerpt_before(excerpt_anchor)
                .is_none()
        {
            scroll = Some(current_scroll_position);
        }

        self.buffer.update(cx, |buffer, cx| {
            buffer.expand_excerpts([excerpt_anchor], lines_to_expand, direction, cx)
        });

        if let Some(new_scroll_position) = scroll {
            self.set_scroll_position(new_scroll_position, window, cx);
        }
    }

    pub fn go_to_singleton_buffer_point(
        &mut self,
        point: Point,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.go_to_singleton_buffer_range(point..point, window, cx);
    }

    pub fn go_to_singleton_buffer_range(
        &mut self,
        range: Range<Point>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let multibuffer = self.buffer().read(cx);
        if !multibuffer.is_singleton() {
            return;
        };
        let anchor_range = range.to_anchors(&multibuffer.snapshot(cx));
        self.change_selections(
            SelectionEffects::default().nav_history(true),
            window,
            cx,
            |s| s.select_anchor_ranges([anchor_range]),
        );
    }

    pub fn go_to_diagnostic(
        &mut self,
        action: &GoToDiagnostic,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.diagnostics_enabled() {
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.go_to_diagnostic_impl(Direction::Next, action.severity, window, cx)
    }

    pub fn go_to_prev_diagnostic(
        &mut self,
        action: &GoToPreviousDiagnostic,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.diagnostics_enabled() {
            return;
        }
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        self.go_to_diagnostic_impl(Direction::Prev, action.severity, window, cx)
    }

    pub fn go_to_diagnostic_impl(
        &mut self,
        direction: Direction,
        severity: GoToDiagnosticSeverityFilter,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let buffer = self.buffer.read(cx).snapshot(cx);
        let selection = self
            .selections
            .newest::<MultiBufferOffset>(&self.display_snapshot(cx));

        let mut active_group_id = None;
        if let ActiveDiagnostic::Group(active_group) = &self.active_diagnostics
            && active_group.active_range.start.to_offset(&buffer) == selection.start
        {
            active_group_id = Some(active_group.group_id);
        }

        fn filtered<'a>(
            severity: GoToDiagnosticSeverityFilter,
            diagnostics: impl Iterator<Item = DiagnosticEntryRef<'a, MultiBufferOffset>>,
        ) -> impl Iterator<Item = DiagnosticEntryRef<'a, MultiBufferOffset>> {
            diagnostics
                .filter(move |entry| severity.matches(entry.diagnostic.severity))
                .filter(|entry| entry.range.start != entry.range.end)
                .filter(|entry| !entry.diagnostic.is_unnecessary)
        }

        let before = filtered(
            severity,
            buffer
                .diagnostics_in_range(MultiBufferOffset(0)..selection.start)
                .filter(|entry| entry.range.start <= selection.start),
        );
        let after = filtered(
            severity,
            buffer
                .diagnostics_in_range(selection.start..buffer.len())
                .filter(|entry| entry.range.start >= selection.start),
        );

        let mut found: Option<DiagnosticEntryRef<MultiBufferOffset>> = None;
        if direction == Direction::Prev {
            'outer: for prev_diagnostics in [before.collect::<Vec<_>>(), after.collect::<Vec<_>>()]
            {
                for diagnostic in prev_diagnostics.into_iter().rev() {
                    if diagnostic.range.start != selection.start
                        || active_group_id
                            .is_some_and(|active| diagnostic.diagnostic.group_id < active)
                    {
                        found = Some(diagnostic);
                        break 'outer;
                    }
                }
            }
        } else {
            for diagnostic in after.chain(before) {
                if diagnostic.range.start != selection.start
                    || active_group_id.is_some_and(|active| diagnostic.diagnostic.group_id > active)
                {
                    found = Some(diagnostic);
                    break;
                }
            }
        }
        let Some(next_diagnostic) = found else {
            return;
        };

        let next_diagnostic_start = buffer.anchor_after(next_diagnostic.range.start);
        let Some((buffer_anchor, _)) = buffer.anchor_to_buffer_anchor(next_diagnostic_start) else {
            return;
        };
        let buffer_id = buffer_anchor.buffer_id;
        let snapshot = self.snapshot(window, cx);
        if snapshot.intersects_fold(next_diagnostic.range.start) {
            self.unfold_ranges(
                std::slice::from_ref(&next_diagnostic.range),
                true,
                false,
                cx,
            );
        }
        self.change_selections(Default::default(), window, cx, |s| {
            s.select_ranges(vec![
                next_diagnostic.range.start..next_diagnostic.range.start,
            ])
        });
        self.activate_diagnostics(buffer_id, next_diagnostic, window, cx);
        self.refresh_edit_prediction(false, true, window, cx);
    }

    pub fn go_to_next_hunk(&mut self, _: &GoToHunk, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let snapshot = self.snapshot(window, cx);
        let selection = self.selections.newest::<Point>(&self.display_snapshot(cx));
        self.go_to_hunk_before_or_after_position(
            &snapshot,
            selection.head(),
            Direction::Next,
            true,
            window,
            cx,
        );
    }

    pub fn go_to_hunk_before_or_after_position(
        &mut self,
        snapshot: &EditorSnapshot,
        position: Point,
        direction: Direction,
        wrap_around: bool,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) {
        let row = if direction == Direction::Next {
            self.hunk_after_position(snapshot, position, wrap_around)
                .map(|hunk| hunk.row_range.start)
        } else {
            self.hunk_before_position(snapshot, position, wrap_around)
        };

        if let Some(row) = row {
            let destination = Point::new(row.0, 0);
            let autoscroll = Autoscroll::center();

            self.unfold_ranges(&[destination..destination], false, false, cx);
            self.change_selections(SelectionEffects::scroll(autoscroll), window, cx, |s| {
                s.select_ranges([destination..destination]);
            });
        }
    }

    fn hunk_after_position(
        &mut self,
        snapshot: &EditorSnapshot,
        position: Point,
        wrap_around: bool,
    ) -> Option<MultiBufferDiffHunk> {
        let result = snapshot
            .buffer_snapshot()
            .diff_hunks_in_range(position..snapshot.buffer_snapshot().max_point())
            .find(|hunk| hunk.row_range.start.0 > position.row);

        if wrap_around {
            result.or_else(|| {
                snapshot
                    .buffer_snapshot()
                    .diff_hunks_in_range(Point::zero()..position)
                    .find(|hunk| hunk.row_range.end.0 < position.row)
            })
        } else {
            result
        }
    }

    fn go_to_prev_hunk(
        &mut self,
        _: &GoToPreviousHunk,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let snapshot = self.snapshot(window, cx);
        let selection = self.selections.newest::<Point>(&snapshot.display_snapshot);
        self.go_to_hunk_before_or_after_position(
            &snapshot,
            selection.head(),
            Direction::Prev,
            true,
            window,
            cx,
        );
    }

    fn hunk_before_position(
        &mut self,
        snapshot: &EditorSnapshot,
        position: Point,
        wrap_around: bool,
    ) -> Option<MultiBufferRow> {
        let result = snapshot.buffer_snapshot().diff_hunk_before(position);

        if wrap_around {
            result.or_else(|| snapshot.buffer_snapshot().diff_hunk_before(Point::MAX))
        } else {
            result
        }
    }

    fn go_to_next_change(
        &mut self,
        _: &GoToNextChange,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(selections) = self
            .change_list
            .next_change(1, Direction::Next)
            .map(|s| s.to_vec())
        {
            self.change_selections(Default::default(), window, cx, |s| {
                let map = s.display_snapshot();
                s.select_display_ranges(selections.iter().map(|a| {
                    let point = a.to_display_point(&map);
                    point..point
                }))
            })
        }
    }

    fn go_to_previous_change(
        &mut self,
        _: &GoToPreviousChange,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(selections) = self
            .change_list
            .next_change(1, Direction::Prev)
            .map(|s| s.to_vec())
        {
            self.change_selections(Default::default(), window, cx, |s| {
                let map = s.display_snapshot();
                s.select_display_ranges(selections.iter().map(|a| {
                    let point = a.to_display_point(&map);
                    point..point
                }))
            })
        }
    }

    pub fn go_to_next_document_highlight(
        &mut self,
        _: &GoToNextDocumentHighlight,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.go_to_document_highlight_before_or_after_position(Direction::Next, window, cx);
    }

    pub fn go_to_prev_document_highlight(
        &mut self,
        _: &GoToPreviousDocumentHighlight,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.go_to_document_highlight_before_or_after_position(Direction::Prev, window, cx);
    }

    pub fn go_to_document_highlight_before_or_after_position(
        &mut self,
        direction: Direction,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::MovementAction, cx);
        let snapshot = self.snapshot(window, cx);
        let buffer = &snapshot.buffer_snapshot();
        let position = self
            .selections
            .newest::<Point>(&snapshot.display_snapshot)
            .head();
        let anchor_position = buffer.anchor_after(position);

        // Get all document highlights (both read and write)
        let mut all_highlights = Vec::new();

        if let Some((_, read_highlights)) = self
            .background_highlights
            .get(&HighlightKey::DocumentHighlightRead)
        {
            all_highlights.extend(read_highlights.iter());
        }

        if let Some((_, write_highlights)) = self
            .background_highlights
            .get(&HighlightKey::DocumentHighlightWrite)
        {
            all_highlights.extend(write_highlights.iter());
        }

        if all_highlights.is_empty() {
            return;
        }

        // Sort highlights by position
        all_highlights.sort_by(|a, b| a.start.cmp(&b.start, buffer));

        let target_highlight = match direction {
            Direction::Next => {
                // Find the first highlight after the current position
                all_highlights
                    .iter()
                    .find(|highlight| highlight.start.cmp(&anchor_position, buffer).is_gt())
            }
            Direction::Prev => {
                // Find the last highlight before the current position
                all_highlights
                    .iter()
                    .rev()
                    .find(|highlight| highlight.end.cmp(&anchor_position, buffer).is_lt())
            }
        };

        if let Some(highlight) = target_highlight {
            let destination = highlight.start.to_point(buffer);
            let autoscroll = Autoscroll::center();

            self.unfold_ranges(&[destination..destination], false, false, cx);
            self.change_selections(SelectionEffects::scroll(autoscroll), window, cx, |s| {
                s.select_ranges([destination..destination]);
            });
        }
    }

    fn go_to_line<T: 'static>(
        &mut self,
        position: Anchor,
        highlight_color: Option<Hsla>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let snapshot = self.snapshot(window, cx).display_snapshot;
        let position = position.to_point(&snapshot.buffer_snapshot());
        let start = snapshot
            .buffer_snapshot()
            .clip_point(Point::new(position.row, 0), Bias::Left);
        let end = start + Point::new(1, 0);
        let start = snapshot.buffer_snapshot().anchor_before(start);
        let end = snapshot.buffer_snapshot().anchor_before(end);

        self.highlight_rows::<T>(
            start..end,
            highlight_color
                .unwrap_or_else(|| cx.theme().colors().editor_highlighted_line_background),
            Default::default(),
            cx,
        );

        if self.buffer.read(cx).is_singleton() {
            self.request_autoscroll(Autoscroll::center().for_anchor(start), cx);
        }
    }

    pub fn go_to_definition(
        &mut self,
        _: &GoToDefinition,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<Navigated>> {
        let definition =
            self.go_to_definition_of_kind(GotoDefinitionKind::Symbol, false, window, cx);
        let fallback_strategy = EditorSettings::get_global(cx).go_to_definition_fallback;
        cx.spawn_in(window, async move |editor, cx| {
            if definition.await? == Navigated::Yes {
                return Ok(Navigated::Yes);
            }
            match fallback_strategy {
                GoToDefinitionFallback::None => Ok(Navigated::No),
                GoToDefinitionFallback::FindAllReferences => {
                    match editor.update_in(cx, |editor, window, cx| {
                        editor.find_all_references(&FindAllReferences::default(), window, cx)
                    })? {
                        Some(references) => references.await,
                        None => Ok(Navigated::No),
                    }
                }
            }
        })
    }

    pub fn go_to_declaration(
        &mut self,
        _: &GoToDeclaration,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<Navigated>> {
        self.go_to_definition_of_kind(GotoDefinitionKind::Declaration, false, window, cx)
    }

    pub fn go_to_declaration_split(
        &mut self,
        _: &GoToDeclaration,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<Navigated>> {
        self.go_to_definition_of_kind(GotoDefinitionKind::Declaration, true, window, cx)
    }

    pub fn go_to_implementation(
        &mut self,
        _: &GoToImplementation,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<Navigated>> {
        self.go_to_definition_of_kind(GotoDefinitionKind::Implementation, false, window, cx)
    }

    pub fn go_to_implementation_split(
        &mut self,
        _: &GoToImplementationSplit,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<Navigated>> {
        self.go_to_definition_of_kind(GotoDefinitionKind::Implementation, true, window, cx)
    }

    pub fn go_to_type_definition(
        &mut self,
        _: &GoToTypeDefinition,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<Navigated>> {
        self.go_to_definition_of_kind(GotoDefinitionKind::Type, false, window, cx)
    }

    pub fn go_to_definition_split(
        &mut self,
        _: &GoToDefinitionSplit,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<Navigated>> {
        self.go_to_definition_of_kind(GotoDefinitionKind::Symbol, true, window, cx)
    }

    pub fn go_to_type_definition_split(
        &mut self,
        _: &GoToTypeDefinitionSplit,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<Navigated>> {
        self.go_to_definition_of_kind(GotoDefinitionKind::Type, true, window, cx)
    }

    fn go_to_definition_of_kind(
        &mut self,
        kind: GotoDefinitionKind,
        split: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<Navigated>> {
        let Some(provider) = self.semantics_provider.clone() else {
            return Task::ready(Ok(Navigated::No));
        };
        let head = self
            .selections
            .newest::<MultiBufferOffset>(&self.display_snapshot(cx))
            .head();
        let buffer = self.buffer.read(cx);
        let Some((buffer, head)) = buffer.text_anchor_for_position(head, cx) else {
            return Task::ready(Ok(Navigated::No));
        };
        let Some(definitions) = provider.definitions(&buffer, head, kind, cx) else {
            return Task::ready(Ok(Navigated::No));
        };

        let nav_entry = self.navigation_entry(self.selections.newest_anchor().head(), cx);

        cx.spawn_in(window, async move |editor, cx| {
            let Some(definitions) = definitions.await? else {
                return Ok(Navigated::No);
            };
            let navigated = editor
                .update_in(cx, |editor, window, cx| {
                    editor.navigate_to_hover_links(
                        Some(kind),
                        definitions
                            .into_iter()
                            .filter(|location| {
                                hover_links::exclude_link_to_position(&buffer, &head, location, cx)
                            })
                            .map(HoverLink::Text)
                            .collect::<Vec<_>>(),
                        nav_entry,
                        split,
                        window,
                        cx,
                    )
                })?
                .await?;
            anyhow::Ok(navigated)
        })
    }

    pub fn open_url(&mut self, _: &OpenUrl, window: &mut Window, cx: &mut Context<Self>) {
        let selection = self.selections.newest_anchor();
        let head = selection.head();
        let tail = selection.tail();

        let Some((buffer, start_position)) =
            self.buffer.read(cx).text_anchor_for_position(head, cx)
        else {
            return;
        };

        let end_position = if head != tail {
            let Some((_, pos)) = self.buffer.read(cx).text_anchor_for_position(tail, cx) else {
                return;
            };
            Some(pos)
        } else {
            None
        };

        let url_finder = cx.spawn_in(window, async move |_editor, cx| {
            let url = if let Some(end_pos) = end_position {
                find_url_from_range(&buffer, start_position..end_pos, cx.clone())
            } else {
                find_url(&buffer, start_position, cx.clone()).map(|(_, url)| url)
            };

            if let Some(url) = url {
                cx.update(|window, cx| {
                    if parse_zed_link(&url, cx).is_some() {
                        window.dispatch_action(Box::new(zed_actions::OpenZedUrl { url }), cx);
                    } else {
                        cx.open_url(&url);
                    }
                })?;
            }

            anyhow::Ok(())
        });

        url_finder.detach();
    }

    pub fn open_selected_filename(
        &mut self,
        _: &OpenSelectedFilename,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(workspace) = self.workspace() else {
            return;
        };

        let position = self.selections.newest_anchor().head();

        let Some((buffer, buffer_position)) =
            self.buffer.read(cx).text_anchor_for_position(position, cx)
        else {
            return;
        };

        let project = self.project.clone();

        cx.spawn_in(window, async move |_, cx| {
            let result = find_file(&buffer, project, buffer_position, cx).await;

            if let Some((_, path)) = result {
                workspace
                    .update_in(cx, |workspace, window, cx| {
                        workspace.open_resolved_path(path, window, cx)
                    })?
                    .await?;
            }
            anyhow::Ok(())
        })
        .detach();
    }

    pub(crate) fn navigate_to_hover_links(
        &mut self,
        kind: Option<GotoDefinitionKind>,
        definitions: Vec<HoverLink>,
        origin: Option<NavigationEntry>,
        split: bool,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) -> Task<Result<Navigated>> {
        // Separate out url and file links, we can only handle one of them at most or an arbitrary number of locations
        let mut first_url_or_file = None;
        let definitions: Vec<_> = definitions
            .into_iter()
            .filter_map(|def| match def {
                HoverLink::Text(link) => Some(Task::ready(anyhow::Ok(Some(link.target)))),
                HoverLink::InlayHint(lsp_location, server_id) => {
                    let computation =
                        self.compute_target_location(lsp_location, server_id, window, cx);
                    Some(cx.background_spawn(computation))
                }
                HoverLink::Url(url) => {
                    first_url_or_file = Some(Either::Left(url));
                    None
                }
                HoverLink::File(path) => {
                    first_url_or_file = Some(Either::Right(path));
                    None
                }
            })
            .collect();

        let workspace = self.workspace();

        let excerpt_context_lines = multi_buffer::excerpt_context_lines(cx);
        cx.spawn_in(window, async move |editor, cx| {
            let locations: Vec<Location> = future::join_all(definitions)
                .await
                .into_iter()
                .filter_map(|location| location.transpose())
                .collect::<Result<_>>()
                .context("location tasks")?;
            let mut locations = cx.update(|_, cx| {
                locations
                    .into_iter()
                    .map(|location| {
                        let buffer = location.buffer.read(cx);
                        (location.buffer, location.range.to_point(buffer))
                    })
                    .into_group_map()
            })?;
            let mut num_locations = 0;
            for ranges in locations.values_mut() {
                ranges.sort_by_key(|range| (range.start, Reverse(range.end)));
                ranges.dedup();
                // Merge overlapping or contained ranges. After sorting by
                // (start, Reverse(end)), we can merge in a single pass:
                // if the next range starts before the current one ends,
                // extend the current range's end if needed.
                let mut i = 0;
                while i + 1 < ranges.len() {
                    if ranges[i + 1].start <= ranges[i].end {
                        let merged_end = ranges[i].end.max(ranges[i + 1].end);
                        ranges[i].end = merged_end;
                        ranges.remove(i + 1);
                    } else {
                        i += 1;
                    }
                }
                let fits_in_one_excerpt = ranges
                    .iter()
                    .tuple_windows()
                    .all(|(a, b)| b.start.row - a.end.row <= 2 * excerpt_context_lines);
                num_locations += if fits_in_one_excerpt { 1 } else { ranges.len() };
            }

            if num_locations > 1 {
                let tab_kind = match kind {
                    Some(GotoDefinitionKind::Implementation) => "Implementations",
                    Some(GotoDefinitionKind::Symbol) | None => "Definitions",
                    Some(GotoDefinitionKind::Declaration) => "Declarations",
                    Some(GotoDefinitionKind::Type) => "Types",
                };
                let title = editor
                    .update_in(cx, |_, _, cx| {
                        let target = locations
                            .iter()
                            .flat_map(|(k, v)| iter::repeat(k.clone()).zip(v))
                            .map(|(buffer, location)| {
                                buffer
                                    .read(cx)
                                    .text_for_range(location.clone())
                                    .collect::<String>()
                            })
                            .filter(|text| !text.contains('\n'))
                            .unique()
                            .take(3)
                            .join(", ");
                        if target.is_empty() {
                            tab_kind.to_owned()
                        } else {
                            format!("{tab_kind} for {target}")
                        }
                    })
                    .context("buffer title")?;

                let Some(workspace) = workspace else {
                    return Ok(Navigated::No);
                };

                let opened = workspace
                    .update_in(cx, |workspace, window, cx| {
                        let allow_preview = PreviewTabsSettings::get_global(cx)
                            .enable_preview_multibuffer_from_code_navigation;
                        if let Some((target_editor, target_pane)) =
                            Self::open_locations_in_multibuffer(
                                workspace,
                                locations,
                                title,
                                split,
                                allow_preview,
                                MultibufferSelectionMode::First,
                                window,
                                cx,
                            )
                        {
                            // We create our own nav history instead of using
                            // `target_editor.nav_history` because `nav_history`
                            // seems to be populated asynchronously when an item
                            // is added to a pane
                            let mut nav_history = target_pane
                                .update(cx, |pane, _| pane.nav_history_for_item(&target_editor));
                            target_editor.update(cx, |editor, cx| {
                                let nav_data = editor
                                    .navigation_data(editor.selections.newest_anchor().head(), cx);
                                let target =
                                    Some(nav_history.navigation_entry(Some(
                                        Arc::new(nav_data) as Arc<dyn Any + Send + Sync>
                                    )));
                                nav_history.push_tag(origin, target);
                            })
                        }
                    })
                    .is_ok();

                anyhow::Ok(Navigated::from_bool(opened))
            } else if num_locations == 0 {
                // If there is one url or file, open it directly
                match first_url_or_file {
                    Some(Either::Left(url)) => {
                        cx.update(|window, cx| {
                            if parse_zed_link(&url, cx).is_some() {
                                window
                                    .dispatch_action(Box::new(zed_actions::OpenZedUrl { url }), cx);
                            } else {
                                cx.open_url(&url);
                            }
                        })?;
                        Ok(Navigated::Yes)
                    }
                    Some(Either::Right(path)) => {
                        // TODO(andrew): respect preview tab settings
                        //               `enable_keep_preview_on_code_navigation` and
                        //               `enable_preview_file_from_code_navigation`
                        let Some(workspace) = workspace else {
                            return Ok(Navigated::No);
                        };
                        workspace
                            .update_in(cx, |workspace, window, cx| {
                                workspace.open_resolved_path(path, window, cx)
                            })?
                            .await?;
                        Ok(Navigated::Yes)
                    }
                    None => Ok(Navigated::No),
                }
            } else {
                let (target_buffer, target_ranges) = locations.into_iter().next().unwrap();

                editor.update_in(cx, |editor, window, cx| {
                    let target_ranges = target_ranges
                        .into_iter()
                        .map(|r| editor.range_for_match(&r))
                        .map(collapse_multiline_range)
                        .collect::<Vec<_>>();
                    if !split
                        && Some(&target_buffer) == editor.buffer.read(cx).as_singleton().as_ref()
                    {
                        let multibuffer = editor.buffer.read(cx);
                        let target_ranges = target_ranges
                            .into_iter()
                            .filter_map(|r| {
                                let start = multibuffer.buffer_point_to_anchor(
                                    &target_buffer,
                                    r.start,
                                    cx,
                                )?;
                                let end = multibuffer.buffer_point_to_anchor(
                                    &target_buffer,
                                    r.end,
                                    cx,
                                )?;
                                Some(start..end)
                            })
                            .collect::<Vec<_>>();
                        if target_ranges.is_empty() {
                            return Navigated::No;
                        }

                        editor.change_selections(
                            SelectionEffects::default().nav_history(true),
                            window,
                            cx,
                            |s| s.select_anchor_ranges(target_ranges),
                        );

                        let target =
                            editor.navigation_entry(editor.selections.newest_anchor().head(), cx);
                        if let Some(mut nav_history) = editor.nav_history.clone() {
                            nav_history.push_tag(origin, target);
                        }
                    } else {
                        let Some(workspace) = workspace else {
                            return Navigated::No;
                        };
                        let pane = workspace.read(cx).active_pane().clone();
                        window.defer(cx, move |window, cx| {
                            let (target_editor, target_pane): (Entity<Self>, Entity<Pane>) =
                                workspace.update(cx, |workspace, cx| {
                                    let pane = if split {
                                        workspace.adjacent_pane(window, cx)
                                    } else {
                                        workspace.active_pane().clone()
                                    };

                                    let preview_tabs_settings = PreviewTabsSettings::get_global(cx);
                                    let keep_old_preview = preview_tabs_settings
                                        .enable_keep_preview_on_code_navigation;
                                    let allow_new_preview = preview_tabs_settings
                                        .enable_preview_file_from_code_navigation;

                                    let editor = workspace.open_project_item(
                                        pane.clone(),
                                        target_buffer.clone(),
                                        true,
                                        true,
                                        keep_old_preview,
                                        allow_new_preview,
                                        window,
                                        cx,
                                    );
                                    (editor, pane)
                                });
                            // We create our own nav history instead of using
                            // `target_editor.nav_history` because `nav_history`
                            // seems to be populated asynchronously when an item
                            // is added to a pane
                            let mut nav_history = target_pane
                                .update(cx, |pane, _| pane.nav_history_for_item(&target_editor));
                            target_editor.update(cx, |target_editor, cx| {
                                // When selecting a definition in a different buffer, disable the nav history
                                // to avoid creating a history entry at the previous cursor location.
                                pane.update(cx, |pane, _| pane.disable_history());

                                let multibuffer = target_editor.buffer.read(cx);
                                let Some(target_buffer) = multibuffer.as_singleton() else {
                                    return Navigated::No;
                                };
                                let target_ranges = target_ranges
                                    .into_iter()
                                    .filter_map(|r| {
                                        let start = multibuffer.buffer_point_to_anchor(
                                            &target_buffer,
                                            r.start,
                                            cx,
                                        )?;
                                        let end = multibuffer.buffer_point_to_anchor(
                                            &target_buffer,
                                            r.end,
                                            cx,
                                        )?;
                                        Some(start..end)
                                    })
                                    .collect::<Vec<_>>();
                                if target_ranges.is_empty() {
                                    return Navigated::No;
                                }

                                target_editor.change_selections(
                                    SelectionEffects::default().nav_history(true),
                                    window,
                                    cx,
                                    |s| s.select_anchor_ranges(target_ranges),
                                );

                                let nav_data = target_editor.navigation_data(
                                    target_editor.selections.newest_anchor().head(),
                                    cx,
                                );
                                let target =
                                    Some(nav_history.navigation_entry(Some(
                                        Arc::new(nav_data) as Arc<dyn Any + Send + Sync>
                                    )));
                                nav_history.push_tag(origin, target);
                                pane.update(cx, |pane, _| pane.enable_history());
                                Navigated::Yes
                            });
                        });
                    }
                    Navigated::Yes
                })
            }
        })
    }

    fn compute_target_location(
        &self,
        lsp_location: lsp::Location,
        server_id: LanguageServerId,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<anyhow::Result<Option<Location>>> {
        let Some(project) = self.project.clone() else {
            return Task::ready(Ok(None));
        };

        cx.spawn_in(window, async move |editor, cx| {
            let location_task = editor.update(cx, |_, cx| {
                project.update(cx, |project, cx| {
                    project.open_local_buffer_via_lsp(lsp_location.uri.clone(), server_id, cx)
                })
            })?;
            let location = Some({
                let target_buffer_handle = location_task.await.context("open local buffer")?;
                let range = target_buffer_handle.read_with(cx, |target_buffer, _| {
                    let target_start = target_buffer
                        .clip_point_utf16(point_from_lsp(lsp_location.range.start), Bias::Left);
                    let target_end = target_buffer
                        .clip_point_utf16(point_from_lsp(lsp_location.range.end), Bias::Left);
                    target_buffer.anchor_after(target_start)
                        ..target_buffer.anchor_before(target_end)
                });
                Location {
                    buffer: target_buffer_handle,
                    range,
                }
            });
            Ok(location)
        })
    }

    fn go_to_next_reference(
        &mut self,
        _: &GoToNextReference,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let task = self.go_to_reference_before_or_after_position(Direction::Next, 1, window, cx);
        if let Some(task) = task {
            task.detach();
        };
    }

    fn go_to_prev_reference(
        &mut self,
        _: &GoToPreviousReference,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let task = self.go_to_reference_before_or_after_position(Direction::Prev, 1, window, cx);
        if let Some(task) = task {
            task.detach();
        };
    }

    fn go_to_symbol_by_offset(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
        offset: i8,
    ) -> Task<Result<()>> {
        let editor_snapshot = self.snapshot(window, cx);

        // We don't care about multi-buffer symbols
        if !editor_snapshot.is_singleton() {
            return Task::ready(Ok(()));
        }

        let cursor_offset = self
            .selections
            .newest::<MultiBufferOffset>(&editor_snapshot.display_snapshot)
            .head();

        cx.spawn_in(window, async move |editor, wcx| -> Result<()> {
            let Ok(Some(remote_id)) = editor.update(wcx, |ed, cx| {
                let buffer = ed.buffer.read(cx).as_singleton()?;
                Some(buffer.read(cx).remote_id())
            }) else {
                return Ok(());
            };

            let task = editor.update(wcx, |ed, cx| ed.buffer_outline_items(remote_id, cx))?;
            let outline_items: Vec<OutlineItem<text::Anchor>> = task.await;

            let multi_snapshot = editor_snapshot.buffer();
            let buffer_range = |range: &Range<_>| {
                Some(
                    multi_snapshot
                        .buffer_anchor_range_to_anchor_range(range.clone())?
                        .to_offset(multi_snapshot),
                )
            };

            wcx.update_window(wcx.window_handle(), |_, window, acx| {
                let current_idx = outline_items
                    .iter()
                    .enumerate()
                    .filter_map(|(idx, item)| {
                        // Find the closest outline item by distance between outline text and cursor location
                        let source_range = buffer_range(&item.source_range_for_text)?;
                        let distance_to_closest_endpoint = cmp::min(
                            (source_range.start.0 as isize - cursor_offset.0 as isize).abs(),
                            (source_range.end.0 as isize - cursor_offset.0 as isize).abs(),
                        );

                        let item_towards_offset =
                            (source_range.start.0 as isize - cursor_offset.0 as isize).signum()
                                == (offset as isize).signum();

                        let source_range_contains_cursor = source_range.contains(&cursor_offset);

                        // To pick the next outline to jump to, we should jump in the direction of the offset, and
                        // we should not already be within the outline's source range. We then pick the closest outline
                        // item.
                        (item_towards_offset && !source_range_contains_cursor)
                            .then_some((distance_to_closest_endpoint, idx))
                    })
                    .min()
                    .map(|(_, idx)| idx);

                let Some(idx) = current_idx else {
                    return;
                };

                let Some(range) = buffer_range(&outline_items[idx].source_range_for_text) else {
                    return;
                };
                let selection = [range.start..range.start];

                let _ = editor
                    .update(acx, |editor, ecx| {
                        editor.change_selections(
                            SelectionEffects::scroll(Autoscroll::newest()),
                            window,
                            ecx,
                            |s| s.select_ranges(selection),
                        );
                    })
                    .ok();
            })?;

            Ok(())
        })
    }

    fn go_to_next_symbol(
        &mut self,
        _: &GoToNextSymbol,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.go_to_symbol_by_offset(window, cx, 1).detach();
    }

    fn go_to_previous_symbol(
        &mut self,
        _: &GoToPreviousSymbol,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.go_to_symbol_by_offset(window, cx, -1).detach();
    }

    pub fn go_to_reference_before_or_after_position(
        &mut self,
        direction: Direction,
        count: usize,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        let selection = self.selections.newest_anchor();
        let head = selection.head();

        let multi_buffer = self.buffer.read(cx);

        let (buffer, text_head) = multi_buffer.text_anchor_for_position(head, cx)?;
        let workspace = self.workspace()?;
        let project = workspace.read(cx).project().clone();
        let references =
            project.update(cx, |project, cx| project.references(&buffer, text_head, cx));
        Some(cx.spawn_in(window, async move |editor, cx| -> Result<()> {
            let Some(locations) = references.await? else {
                return Ok(());
            };

            if locations.is_empty() {
                // totally normal - the cursor may be on something which is not
                // a symbol (e.g. a keyword)
                log::info!("no references found under cursor");
                return Ok(());
            }

            let multi_buffer = editor.read_with(cx, |editor, _| editor.buffer().clone())?;

            let (locations, current_location_index) =
                multi_buffer.update(cx, |multi_buffer, cx| {
                    let multi_buffer_snapshot = multi_buffer.snapshot(cx);
                    let mut locations = locations
                        .into_iter()
                        .filter_map(|loc| {
                            let start = multi_buffer_snapshot.anchor_in_excerpt(loc.range.start)?;
                            let end = multi_buffer_snapshot.anchor_in_excerpt(loc.range.end)?;
                            Some(start..end)
                        })
                        .collect::<Vec<_>>();
                    // There is an O(n) implementation, but given this list will be
                    // small (usually <100 items), the extra O(log(n)) factor isn't
                    // worth the (surprisingly large amount of) extra complexity.
                    locations
                        .sort_unstable_by(|l, r| l.start.cmp(&r.start, &multi_buffer_snapshot));

                    let head_offset = head.to_offset(&multi_buffer_snapshot);

                    let current_location_index = locations.iter().position(|loc| {
                        loc.start.to_offset(&multi_buffer_snapshot) <= head_offset
                            && loc.end.to_offset(&multi_buffer_snapshot) >= head_offset
                    });

                    (locations, current_location_index)
                });

            let Some(current_location_index) = current_location_index else {
                // This indicates something has gone wrong, because we already
                // handle the "no references" case above
                log::error!(
                    "failed to find current reference under cursor. Total references: {}",
                    locations.len()
                );
                return Ok(());
            };

            let destination_location_index = match direction {
                Direction::Next => (current_location_index + count) % locations.len(),
                Direction::Prev => {
                    (current_location_index + locations.len() - count % locations.len())
                        % locations.len()
                }
            };

            // TODO(cameron): is this needed?
            // the thinking is to avoid "jumping to the current location" (avoid
            // polluting "jumplist" in vim terms)
            if current_location_index == destination_location_index {
                return Ok(());
            }

            let Range { start, end } = locations[destination_location_index];

            editor.update_in(cx, |editor, window, cx| {
                let effects = SelectionEffects::default();

                editor.unfold_ranges(&[start..end], false, false, cx);
                editor.change_selections(effects, window, cx, |s| {
                    s.select_ranges([start..start]);
                });
            })?;

            Ok(())
        }))
    }

    pub fn find_all_references(
        &mut self,
        action: &FindAllReferences,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<Navigated>>> {
        let always_open_multibuffer = action.always_open_multibuffer;
        let selection = self.selections.newest_anchor();
        let multi_buffer = self.buffer.read(cx);
        let multi_buffer_snapshot = multi_buffer.snapshot(cx);
        let selection_offset = selection.map(|anchor| anchor.to_offset(&multi_buffer_snapshot));
        let selection_point = selection.map(|anchor| anchor.to_point(&multi_buffer_snapshot));
        let head = selection_offset.head();

        let head_anchor = multi_buffer_snapshot.anchor_at(
            head,
            if head < selection_offset.tail() {
                Bias::Right
            } else {
                Bias::Left
            },
        );

        match self
            .find_all_references_task_sources
            .binary_search_by(|anchor| anchor.cmp(&head_anchor, &multi_buffer_snapshot))
        {
            Ok(_) => {
                log::info!(
                    "Ignoring repeated FindAllReferences invocation with the position of already running task"
                );
                return None;
            }
            Err(i) => {
                self.find_all_references_task_sources.insert(i, head_anchor);
            }
        }

        let (buffer, head) = multi_buffer.text_anchor_for_position(head, cx)?;
        let workspace = self.workspace()?;
        let project = workspace.read(cx).project().clone();
        let references = project.update(cx, |project, cx| project.references(&buffer, head, cx));
        Some(cx.spawn_in(window, async move |editor, cx| {
            let _cleanup = cx.on_drop(&editor, move |editor, _| {
                if let Ok(i) = editor
                    .find_all_references_task_sources
                    .binary_search_by(|anchor| anchor.cmp(&head_anchor, &multi_buffer_snapshot))
                {
                    editor.find_all_references_task_sources.remove(i);
                }
            });

            let Some(locations) = references.await? else {
                return anyhow::Ok(Navigated::No);
            };
            let mut locations = cx.update(|_, cx| {
                locations
                    .into_iter()
                    .map(|location| {
                        let buffer = location.buffer.read(cx);
                        (location.buffer, location.range.to_point(buffer))
                    })
                    // if special-casing the single-match case, remove ranges
                    // that intersect current selection
                    .filter(|(location_buffer, location)| {
                        if always_open_multibuffer || &buffer != location_buffer {
                            return true;
                        }

                        !location.contains_inclusive(&selection_point.range())
                    })
                    .into_group_map()
            })?;
            if locations.is_empty() {
                return anyhow::Ok(Navigated::No);
            }
            for ranges in locations.values_mut() {
                ranges.sort_by_key(|range| (range.start, Reverse(range.end)));
                ranges.dedup();
            }
            let mut num_locations = 0;
            for ranges in locations.values_mut() {
                ranges.sort_by_key(|range| (range.start, Reverse(range.end)));
                ranges.dedup();
                num_locations += ranges.len();
            }

            if num_locations == 1 && !always_open_multibuffer {
                let (target_buffer, target_ranges) = locations.into_iter().next().unwrap();
                let target_range = target_ranges.first().unwrap().clone();

                return editor.update_in(cx, |editor, window, cx| {
                    let range = target_range.to_point(target_buffer.read(cx));
                    let range = editor.range_for_match(&range);
                    let range = range.start..range.start;

                    if Some(&target_buffer) == editor.buffer.read(cx).as_singleton().as_ref() {
                        editor.go_to_singleton_buffer_range(range, window, cx);
                    } else {
                        let pane = workspace.read(cx).active_pane().clone();
                        window.defer(cx, move |window, cx| {
                            let target_editor: Entity<Self> =
                                workspace.update(cx, |workspace, cx| {
                                    let pane = workspace.active_pane().clone();

                                    let preview_tabs_settings = PreviewTabsSettings::get_global(cx);
                                    let keep_old_preview = preview_tabs_settings
                                        .enable_keep_preview_on_code_navigation;
                                    let allow_new_preview = preview_tabs_settings
                                        .enable_preview_file_from_code_navigation;

                                    workspace.open_project_item(
                                        pane,
                                        target_buffer.clone(),
                                        true,
                                        true,
                                        keep_old_preview,
                                        allow_new_preview,
                                        window,
                                        cx,
                                    )
                                });
                            target_editor.update(cx, |target_editor, cx| {
                                // When selecting a definition in a different buffer, disable the nav history
                                // to avoid creating a history entry at the previous cursor location.
                                pane.update(cx, |pane, _| pane.disable_history());
                                target_editor.go_to_singleton_buffer_range(range, window, cx);
                                pane.update(cx, |pane, _| pane.enable_history());
                            });
                        });
                    }
                    Navigated::No
                });
            }

            workspace.update_in(cx, |workspace, window, cx| {
                let target = locations
                    .iter()
                    .flat_map(|(k, v)| iter::repeat(k.clone()).zip(v))
                    .map(|(buffer, location)| {
                        buffer
                            .read(cx)
                            .text_for_range(location.clone())
                            .collect::<String>()
                    })
                    .filter(|text| !text.contains('\n'))
                    .unique()
                    .take(3)
                    .join(", ");
                let title = if target.is_empty() {
                    "References".to_owned()
                } else {
                    format!("References to {target}")
                };
                let allow_preview = PreviewTabsSettings::get_global(cx)
                    .enable_preview_multibuffer_from_code_navigation;
                Self::open_locations_in_multibuffer(
                    workspace,
                    locations,
                    title,
                    false,
                    allow_preview,
                    MultibufferSelectionMode::First,
                    window,
                    cx,
                );
                Navigated::Yes
            })
        }))
    }

    /// Opens a multibuffer with the given project locations in it.
    pub fn open_locations_in_multibuffer(
        workspace: &mut Workspace,
        locations: std::collections::HashMap<Entity<Buffer>, Vec<Range<Point>>>,
        title: String,
        split: bool,
        allow_preview: bool,
        multibuffer_selection_mode: MultibufferSelectionMode,
        window: &mut Window,
        cx: &mut Context<Workspace>,
    ) -> Option<(Entity<Editor>, Entity<Pane>)> {
        if locations.is_empty() {
            log::error!("bug: open_locations_in_multibuffer called with empty list of locations");
            return None;
        }

        let capability = workspace.project().read(cx).capability();
        let mut ranges = <Vec<Range<Anchor>>>::new();

        // a key to find existing multibuffer editors with the same set of locations
        // to prevent us from opening more and more multibuffer tabs for searches and the like
        let mut key = (title.clone(), vec![]);
        let excerpt_buffer = cx.new(|cx| {
            let key = &mut key.1;
            let mut multibuffer = MultiBuffer::new(capability);
            for (buffer, mut ranges_for_buffer) in locations {
                ranges_for_buffer.sort_by_key(|range| (range.start, Reverse(range.end)));
                key.push((buffer.read(cx).remote_id(), ranges_for_buffer.clone()));
                multibuffer.set_excerpts_for_path(
                    PathKey::for_buffer(&buffer, cx),
                    buffer.clone(),
                    ranges_for_buffer.clone(),
                    multibuffer_context_lines(cx),
                    cx,
                );
                let snapshot = multibuffer.snapshot(cx);
                let buffer_snapshot = buffer.read(cx).snapshot();
                ranges.extend(ranges_for_buffer.into_iter().filter_map(|range| {
                    let text_range = buffer_snapshot.anchor_range_inside(range);
                    let start = snapshot.anchor_in_buffer(text_range.start)?;
                    let end = snapshot.anchor_in_buffer(text_range.end)?;
                    Some(start..end)
                }))
            }

            multibuffer.with_title(title)
        });
        let existing = workspace.active_pane().update(cx, |pane, cx| {
            pane.items()
                .filter_map(|item| item.downcast::<Editor>())
                .find(|editor| {
                    editor
                        .read(cx)
                        .lookup_key
                        .as_ref()
                        .and_then(|it| {
                            it.downcast_ref::<(String, Vec<(BufferId, Vec<Range<Point>>)>)>()
                        })
                        .is_some_and(|it| *it == key)
                })
        });
        let was_existing = existing.is_some();
        let editor = existing.unwrap_or_else(|| {
            cx.new(|cx| {
                let mut editor = Editor::for_multibuffer(
                    excerpt_buffer,
                    Some(workspace.project().clone()),
                    window,
                    cx,
                );
                editor.lookup_key = Some(Box::new(key));
                editor
            })
        });
        editor.update(cx, |editor, cx| match multibuffer_selection_mode {
            MultibufferSelectionMode::First => {
                if let Some(first_range) = ranges.first() {
                    editor.change_selections(
                        SelectionEffects::no_scroll(),
                        window,
                        cx,
                        |selections| {
                            selections.clear_disjoint();
                            selections.select_anchor_ranges(std::iter::once(first_range.clone()));
                        },
                    );
                }
                editor.highlight_background(
                    HighlightKey::Editor,
                    &ranges,
                    |_, theme| theme.colors().editor_highlighted_line_background,
                    cx,
                );
            }
            MultibufferSelectionMode::All => {
                editor.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
                    selections.clear_disjoint();
                    selections.select_anchor_ranges(ranges);
                });
            }
        });

        let item = Box::new(editor.clone());

        let pane = if split {
            workspace.adjacent_pane(window, cx)
        } else {
            workspace.active_pane().clone()
        };
        let activate_pane = split;

        let mut destination_index = None;
        pane.update(cx, |pane, cx| {
            if allow_preview && !was_existing {
                destination_index = pane.replace_preview_item_id(item.item_id(), window, cx);
            }
            if was_existing && !allow_preview {
                pane.unpreview_item_if_preview(item.item_id());
            }
            pane.add_item(item, activate_pane, true, destination_index, window, cx);
        });

        Some((editor, pane))
    }

    pub fn rename(
        &mut self,
        _: &Rename,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        use language::ToOffset as _;

        let provider = self.semantics_provider.clone()?;
        let selection = self.selections.newest_anchor().clone();
        let (cursor_buffer, cursor_buffer_position) = self
            .buffer
            .read(cx)
            .text_anchor_for_position(selection.head(), cx)?;
        let (tail_buffer, cursor_buffer_position_end) = self
            .buffer
            .read(cx)
            .text_anchor_for_position(selection.tail(), cx)?;
        if tail_buffer != cursor_buffer {
            return None;
        }

        let snapshot = cursor_buffer.read(cx).snapshot();
        let cursor_buffer_offset = cursor_buffer_position.to_offset(&snapshot);
        let cursor_buffer_offset_end = cursor_buffer_position_end.to_offset(&snapshot);
        let prepare_rename = provider.range_for_rename(&cursor_buffer, cursor_buffer_position, cx);
        drop(snapshot);

        Some(cx.spawn_in(window, async move |this, cx| {
            let rename_range = prepare_rename.await?;
            if let Some(rename_range) = rename_range {
                this.update_in(cx, |this, window, cx| {
                    let snapshot = cursor_buffer.read(cx).snapshot();
                    let rename_buffer_range = rename_range.to_offset(&snapshot);
                    let cursor_offset_in_rename_range =
                        cursor_buffer_offset.saturating_sub(rename_buffer_range.start);
                    let cursor_offset_in_rename_range_end =
                        cursor_buffer_offset_end.saturating_sub(rename_buffer_range.start);

                    this.take_rename(false, window, cx);
                    let buffer = this.buffer.read(cx).read(cx);
                    let cursor_offset = selection.head().to_offset(&buffer);
                    let rename_start =
                        cursor_offset.saturating_sub_usize(cursor_offset_in_rename_range);
                    let rename_end = rename_start + rename_buffer_range.len();
                    let range = buffer.anchor_before(rename_start)..buffer.anchor_after(rename_end);
                    let mut old_highlight_id = None;
                    let old_name: Arc<str> = buffer
                        .chunks(
                            rename_start..rename_end,
                            LanguageAwareStyling {
                                tree_sitter: true,
                                diagnostics: true,
                            },
                        )
                        .map(|chunk| {
                            if old_highlight_id.is_none() {
                                old_highlight_id = chunk.syntax_highlight_id;
                            }
                            chunk.text
                        })
                        .collect::<String>()
                        .into();

                    drop(buffer);

                    // Position the selection in the rename editor so that it matches the current selection.
                    this.show_local_selections = false;
                    let rename_editor = cx.new(|cx| {
                        let mut editor = Editor::single_line(window, cx);
                        editor.buffer.update(cx, |buffer, cx| {
                            buffer.edit(
                                [(MultiBufferOffset(0)..MultiBufferOffset(0), old_name.clone())],
                                None,
                                cx,
                            )
                        });
                        let cursor_offset_in_rename_range =
                            MultiBufferOffset(cursor_offset_in_rename_range);
                        let cursor_offset_in_rename_range_end =
                            MultiBufferOffset(cursor_offset_in_rename_range_end);
                        let rename_selection_range = match cursor_offset_in_rename_range
                            .cmp(&cursor_offset_in_rename_range_end)
                        {
                            Ordering::Equal => {
                                editor.select_all(&SelectAll, window, cx);
                                return editor;
                            }
                            Ordering::Less => {
                                cursor_offset_in_rename_range..cursor_offset_in_rename_range_end
                            }
                            Ordering::Greater => {
                                cursor_offset_in_rename_range_end..cursor_offset_in_rename_range
                            }
                        };
                        if rename_selection_range.end.0 > old_name.len() {
                            editor.select_all(&SelectAll, window, cx);
                        } else {
                            editor.change_selections(Default::default(), window, cx, |s| {
                                s.select_ranges([rename_selection_range]);
                            });
                        }
                        editor
                    });
                    cx.subscribe(&rename_editor, |_, _, e: &EditorEvent, cx| {
                        if e == &EditorEvent::Focused {
                            cx.emit(EditorEvent::FocusedIn)
                        }
                    })
                    .detach();

                    let write_highlights =
                        this.clear_background_highlights(HighlightKey::DocumentHighlightWrite, cx);
                    let read_highlights =
                        this.clear_background_highlights(HighlightKey::DocumentHighlightRead, cx);
                    let ranges = write_highlights
                        .iter()
                        .flat_map(|(_, ranges)| ranges.iter())
                        .chain(read_highlights.iter().flat_map(|(_, ranges)| ranges.iter()))
                        .cloned()
                        .collect();

                    this.highlight_text(
                        HighlightKey::Rename,
                        ranges,
                        HighlightStyle {
                            fade_out: Some(0.6),
                            ..Default::default()
                        },
                        cx,
                    );
                    let rename_focus_handle = rename_editor.focus_handle(cx);
                    window.focus(&rename_focus_handle, cx);
                    let block_id = this.insert_blocks(
                        [BlockProperties {
                            style: BlockStyle::Flex,
                            placement: BlockPlacement::Below(range.start),
                            height: Some(1),
                            render: Arc::new({
                                let rename_editor = rename_editor.clone();
                                move |cx: &mut BlockContext| {
                                    let mut text_style = cx.editor_style.text.clone();
                                    if let Some(highlight_style) = old_highlight_id
                                        .and_then(|h| cx.editor_style.syntax.get(h).cloned())
                                    {
                                        text_style = text_style.highlight(highlight_style);
                                    }
                                    div()
                                        .block_mouse_except_scroll()
                                        .pl(cx.anchor_x)
                                        .child(EditorElement::new(
                                            &rename_editor,
                                            EditorStyle {
                                                background: cx.theme().system().transparent,
                                                local_player: cx.editor_style.local_player,
                                                text: text_style,
                                                scrollbar_width: cx.editor_style.scrollbar_width,
                                                syntax: cx.editor_style.syntax.clone(),
                                                status: cx.editor_style.status.clone(),
                                                inlay_hints_style: HighlightStyle {
                                                    font_weight: Some(FontWeight::BOLD),
                                                    ..make_inlay_hints_style(cx.app)
                                                },
                                                edit_prediction_styles: make_suggestion_styles(
                                                    cx.app,
                                                ),
                                                ..EditorStyle::default()
                                            },
                                        ))
                                        .into_any_element()
                                }
                            }),
                            priority: 0,
                        }],
                        Some(Autoscroll::fit()),
                        cx,
                    )[0];
                    this.pending_rename = Some(RenameState {
                        range,
                        old_name,
                        editor: rename_editor,
                        block_id,
                    });
                })?;
            }

            Ok(())
        }))
    }

    pub fn confirm_rename(
        &mut self,
        _: &ConfirmRename,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        let rename = self.take_rename(false, window, cx)?;
        let workspace = self.workspace()?.downgrade();
        let (buffer, start) = self
            .buffer
            .read(cx)
            .text_anchor_for_position(rename.range.start, cx)?;
        let (end_buffer, _) = self
            .buffer
            .read(cx)
            .text_anchor_for_position(rename.range.end, cx)?;
        if buffer != end_buffer {
            return None;
        }

        let old_name = rename.old_name;
        let new_name = rename.editor.read(cx).text(cx);

        let rename = self.semantics_provider.as_ref()?.perform_rename(
            &buffer,
            start,
            new_name.clone(),
            cx,
        )?;

        Some(cx.spawn_in(window, async move |editor, cx| {
            let project_transaction = rename.await?;
            Self::open_project_transaction(
                &editor,
                workspace,
                project_transaction,
                format!("Rename: {} → {}", old_name, new_name),
                cx,
            )
            .await?;

            editor.update(cx, |editor, cx| {
                editor.refresh_document_highlights(cx);
            })?;
            Ok(())
        }))
    }

    fn take_rename(
        &mut self,
        moving_cursor: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<RenameState> {
        let rename = self.pending_rename.take()?;
        if rename.editor.focus_handle(cx).is_focused(window) {
            window.focus(&self.focus_handle, cx);
        }

        self.remove_blocks(
            [rename.block_id].into_iter().collect(),
            Some(Autoscroll::fit()),
            cx,
        );
        self.clear_highlights(HighlightKey::Rename, cx);
        self.show_local_selections = true;

        if moving_cursor {
            let cursor_in_rename_editor = rename.editor.update(cx, |editor, cx| {
                editor
                    .selections
                    .newest::<MultiBufferOffset>(&editor.display_snapshot(cx))
                    .head()
            });

            // Update the selection to match the position of the selection inside
            // the rename editor.
            let snapshot = self.buffer.read(cx).read(cx);
            let rename_range = rename.range.to_offset(&snapshot);
            let cursor_in_editor = snapshot
                .clip_offset(rename_range.start + cursor_in_rename_editor, Bias::Left)
                .min(rename_range.end);
            drop(snapshot);

            self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                s.select_ranges(vec![cursor_in_editor..cursor_in_editor])
            });
        } else {
            self.refresh_document_highlights(cx);
        }

        Some(rename)
    }

    pub fn pending_rename(&self) -> Option<&RenameState> {
        self.pending_rename.as_ref()
    }

    fn format(
        &mut self,
        _: &Format,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        let project = match &self.project {
            Some(project) => project.clone(),
            None => return None,
        };

        Some(self.perform_format(
            project,
            FormatTrigger::Manual,
            FormatTarget::Buffers(self.buffer.read(cx).all_buffers()),
            window,
            cx,
        ))
    }

    fn format_selections(
        &mut self,
        _: &FormatSelections,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        let project = match &self.project {
            Some(project) => project.clone(),
            None => return None,
        };

        let ranges = self
            .selections
            .all_adjusted(&self.display_snapshot(cx))
            .into_iter()
            .map(|selection| selection.range())
            .collect_vec();

        Some(self.perform_format(
            project,
            FormatTrigger::Manual,
            FormatTarget::Ranges(ranges),
            window,
            cx,
        ))
    }

    fn perform_format(
        &mut self,
        project: Entity<Project>,
        trigger: FormatTrigger,
        target: FormatTarget,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<()>> {
        let buffer = self.buffer.clone();
        let (buffers, target) = match target {
            FormatTarget::Buffers(buffers) => (buffers, LspFormatTarget::Buffers),
            FormatTarget::Ranges(selection_ranges) => {
                let multi_buffer = buffer.read(cx);
                let snapshot = multi_buffer.read(cx);
                let mut buffers = HashSet::default();
                let mut buffer_id_to_ranges: BTreeMap<BufferId, Vec<Range<text::Anchor>>> =
                    BTreeMap::new();
                for selection_range in selection_ranges {
                    for (buffer_snapshot, buffer_range, _) in
                        snapshot.range_to_buffer_ranges(selection_range.start..selection_range.end)
                    {
                        let buffer_id = buffer_snapshot.remote_id();
                        let start = buffer_snapshot.anchor_before(buffer_range.start);
                        let end = buffer_snapshot.anchor_after(buffer_range.end);
                        buffers.insert(multi_buffer.buffer(buffer_id).unwrap());
                        buffer_id_to_ranges
                            .entry(buffer_id)
                            .and_modify(|buffer_ranges| buffer_ranges.push(start..end))
                            .or_insert_with(|| vec![start..end]);
                    }
                }
                (buffers, LspFormatTarget::Ranges(buffer_id_to_ranges))
            }
        };

        let transaction_id_prev = buffer.read(cx).last_transaction_id(cx);
        let selections_prev = transaction_id_prev
            .and_then(|transaction_id_prev| {
                // default to selections as they were after the last edit, if we have them,
                // instead of how they are now.
                // This will make it so that editing, moving somewhere else, formatting, then undoing the format
                // will take you back to where you made the last edit, instead of staying where you scrolled
                self.selection_history
                    .transaction(transaction_id_prev)
                    .map(|t| t.0.clone())
            })
            .unwrap_or_else(|| self.selections.disjoint_anchors_arc());

        let mut timeout = cx.background_executor().timer(FORMAT_TIMEOUT).fuse();
        let format = project.update(cx, |project, cx| {
            project.format(buffers, target, true, trigger, cx)
        });

        cx.spawn_in(window, async move |editor, cx| {
            let transaction = futures::select_biased! {
                transaction = format.log_err().fuse() => transaction,
                () = timeout => {
                    log::warn!("timed out waiting for formatting");
                    None
                }
            };

            buffer.update(cx, |buffer, cx| {
                if let Some(transaction) = transaction
                    && !buffer.is_singleton()
                {
                    buffer.push_transaction(&transaction.0, cx);
                }
                cx.notify();
            });

            if let Some(transaction_id_now) =
                buffer.read_with(cx, |b, cx| b.last_transaction_id(cx))
            {
                let has_new_transaction = transaction_id_prev != Some(transaction_id_now);
                if has_new_transaction {
                    editor
                        .update(cx, |editor, _| {
                            editor
                                .selection_history
                                .insert_transaction(transaction_id_now, selections_prev);
                        })
                        .ok();
                }
            }

            Ok(())
        })
    }

    fn organize_imports(
        &mut self,
        _: &OrganizeImports,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Task<Result<()>>> {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let project = match &self.project {
            Some(project) => project.clone(),
            None => return None,
        };
        Some(self.perform_code_action_kind(
            project,
            CodeActionKind::SOURCE_ORGANIZE_IMPORTS,
            window,
            cx,
        ))
    }

    fn perform_code_action_kind(
        &mut self,
        project: Entity<Project>,
        kind: CodeActionKind,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Task<Result<()>> {
        let buffer = self.buffer.clone();
        let buffers = buffer.read(cx).all_buffers();
        let mut timeout = cx.background_executor().timer(CODE_ACTION_TIMEOUT).fuse();
        let apply_action = project.update(cx, |project, cx| {
            project.apply_code_action_kind(buffers, kind, true, cx)
        });
        cx.spawn_in(window, async move |_, cx| {
            let transaction = futures::select_biased! {
                () = timeout => {
                    log::warn!("timed out waiting for executing code action");
                    None
                }
                transaction = apply_action.log_err().fuse() => transaction,
            };
            buffer.update(cx, |buffer, cx| {
                // check if we need this
                if let Some(transaction) = transaction
                    && !buffer.is_singleton()
                {
                    buffer.push_transaction(&transaction.0, cx);
                }
                cx.notify();
            });
            Ok(())
        })
    }

    pub fn restart_language_server(
        &mut self,
        _: &RestartLanguageServer,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(project) = self.project.clone() {
            self.buffer.update(cx, |multi_buffer, cx| {
                project.update(cx, |project, cx| {
                    project.restart_language_servers_for_buffers(
                        multi_buffer.all_buffers().into_iter().collect(),
                        HashSet::default(),
                        cx,
                    );
                });
            })
        }
    }

    pub fn stop_language_server(
        &mut self,
        _: &StopLanguageServer,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(project) = self.project.clone() {
            self.buffer.update(cx, |multi_buffer, cx| {
                project.update(cx, |project, cx| {
                    project.stop_language_servers_for_buffers(
                        multi_buffer.all_buffers().into_iter().collect(),
                        HashSet::default(),
                        cx,
                    );
                });
            });
        }
    }

    fn cancel_language_server_work(
        workspace: &mut Workspace,
        _: &actions::CancelLanguageServerWork,
        _: &mut Window,
        cx: &mut Context<Workspace>,
    ) {
        let project = workspace.project();
        let buffers = workspace
            .active_item(cx)
            .and_then(|item| item.act_as::<Editor>(cx))
            .map_or(HashSet::default(), |editor| {
                editor.read(cx).buffer.read(cx).all_buffers()
            });
        project.update(cx, |project, cx| {
            project.cancel_language_server_work_for_buffers(buffers, cx);
        });
    }

    fn show_character_palette(
        &mut self,
        _: &ShowCharacterPalette,
        window: &mut Window,
        _: &mut Context<Self>,
    ) {
        window.show_character_palette();
    }

    fn refresh_active_diagnostics(&mut self, cx: &mut Context<Editor>) {
        if !self.diagnostics_enabled() {
            return;
        }

        if let ActiveDiagnostic::Group(active_diagnostics) = &mut self.active_diagnostics {
            let buffer = self.buffer.read(cx).snapshot(cx);
            let primary_range_start = active_diagnostics.active_range.start.to_offset(&buffer);
            let primary_range_end = active_diagnostics.active_range.end.to_offset(&buffer);
            let is_valid = buffer
                .diagnostics_in_range::<MultiBufferOffset>(primary_range_start..primary_range_end)
                .any(|entry| {
                    entry.diagnostic.is_primary
                        && !entry.range.is_empty()
                        && entry.range.start == primary_range_start
                        && entry.diagnostic.message == active_diagnostics.active_message
                });

            if !is_valid {
                self.dismiss_diagnostics(cx);
            }
        }
    }

    pub fn active_diagnostic_group(&self) -> Option<&ActiveDiagnosticGroup> {
        match &self.active_diagnostics {
            ActiveDiagnostic::Group(group) => Some(group),
            _ => None,
        }
    }

    pub fn set_all_diagnostics_active(&mut self, cx: &mut Context<Self>) {
        if !self.diagnostics_enabled() {
            return;
        }
        self.dismiss_diagnostics(cx);
        self.active_diagnostics = ActiveDiagnostic::All;
    }

    fn activate_diagnostics(
        &mut self,
        buffer_id: BufferId,
        diagnostic: DiagnosticEntryRef<'_, MultiBufferOffset>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.diagnostics_enabled() || matches!(self.active_diagnostics, ActiveDiagnostic::All) {
            return;
        }
        self.dismiss_diagnostics(cx);
        let snapshot = self.snapshot(window, cx);
        let buffer = self.buffer.read(cx).snapshot(cx);
        let Some(renderer) = GlobalDiagnosticRenderer::global(cx) else {
            return;
        };

        let diagnostic_group = buffer
            .diagnostic_group(buffer_id, diagnostic.diagnostic.group_id)
            .collect::<Vec<_>>();

        let language_registry = self
            .project()
            .map(|project| project.read(cx).languages().clone());

        let blocks = renderer.render_group(
            diagnostic_group,
            buffer_id,
            snapshot,
            cx.weak_entity(),
            language_registry,
            cx,
        );

        let blocks = self.display_map.update(cx, |display_map, cx| {
            display_map.insert_blocks(blocks, cx).into_iter().collect()
        });
        self.active_diagnostics = ActiveDiagnostic::Group(ActiveDiagnosticGroup {
            active_range: buffer.anchor_before(diagnostic.range.start)
                ..buffer.anchor_after(diagnostic.range.end),
            active_message: diagnostic.diagnostic.message.clone(),
            group_id: diagnostic.diagnostic.group_id,
            blocks,
        });
        cx.notify();
    }

    fn dismiss_diagnostics(&mut self, cx: &mut Context<Self>) {
        if matches!(self.active_diagnostics, ActiveDiagnostic::All) {
            return;
        };

        let prev = mem::replace(&mut self.active_diagnostics, ActiveDiagnostic::None);
        if let ActiveDiagnostic::Group(group) = prev {
            self.display_map.update(cx, |display_map, cx| {
                display_map.remove_blocks(group.blocks, cx);
            });
            cx.notify();
        }
    }

    /// Disable inline diagnostics rendering for this editor.
    pub fn disable_inline_diagnostics(&mut self) {
        self.inline_diagnostics_enabled = false;
        self.inline_diagnostics_update = Task::ready(());
        self.inline_diagnostics.clear();
    }

    pub fn disable_diagnostics(&mut self, cx: &mut Context<Self>) {
        self.diagnostics_enabled = false;
        self.dismiss_diagnostics(cx);
        self.inline_diagnostics_update = Task::ready(());
        self.inline_diagnostics.clear();
    }

    pub fn disable_word_completions(&mut self) {
        self.word_completions_enabled = false;
    }

    pub fn diagnostics_enabled(&self) -> bool {
        self.diagnostics_enabled && self.lsp_data_enabled()
    }

    pub fn inline_diagnostics_enabled(&self) -> bool {
        self.inline_diagnostics_enabled && self.diagnostics_enabled()
    }

    pub fn show_inline_diagnostics(&self) -> bool {
        self.show_inline_diagnostics
    }

    pub fn toggle_inline_diagnostics(
        &mut self,
        _: &ToggleInlineDiagnostics,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) {
        self.show_inline_diagnostics = !self.show_inline_diagnostics;
        self.refresh_inline_diagnostics(false, window, cx);
    }

    pub fn set_max_diagnostics_severity(&mut self, severity: DiagnosticSeverity, cx: &mut App) {
        self.diagnostics_max_severity = severity;
        self.display_map.update(cx, |display_map, _| {
            display_map.diagnostics_max_severity = self.diagnostics_max_severity;
        });
    }

    pub fn toggle_diagnostics(
        &mut self,
        _: &ToggleDiagnostics,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) {
        if !self.diagnostics_enabled() {
            return;
        }

        let new_severity = if self.diagnostics_max_severity == DiagnosticSeverity::Off {
            EditorSettings::get_global(cx)
                .diagnostics_max_severity
                .filter(|severity| severity != &DiagnosticSeverity::Off)
                .unwrap_or(DiagnosticSeverity::Hint)
        } else {
            DiagnosticSeverity::Off
        };
        self.set_max_diagnostics_severity(new_severity, cx);
        if self.diagnostics_max_severity == DiagnosticSeverity::Off {
            self.active_diagnostics = ActiveDiagnostic::None;
            self.inline_diagnostics_update = Task::ready(());
            self.inline_diagnostics.clear();
        } else {
            self.refresh_inline_diagnostics(false, window, cx);
        }

        cx.notify();
    }

    pub fn toggle_minimap(
        &mut self,
        _: &ToggleMinimap,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) {
        if self.supports_minimap(cx) {
            self.set_minimap_visibility(self.minimap_visibility.toggle_visibility(), window, cx);
        }
    }

    fn refresh_inline_diagnostics(
        &mut self,
        debounce: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let max_severity = ProjectSettings::get_global(cx)
            .diagnostics
            .inline
            .max_severity
            .unwrap_or(self.diagnostics_max_severity);

        if !self.inline_diagnostics_enabled()
            || !self.diagnostics_enabled()
            || !self.show_inline_diagnostics
            || max_severity == DiagnosticSeverity::Off
        {
            self.inline_diagnostics_update = Task::ready(());
            self.inline_diagnostics.clear();
            return;
        }

        let debounce_ms = ProjectSettings::get_global(cx)
            .diagnostics
            .inline
            .update_debounce_ms;
        let debounce = if debounce && debounce_ms > 0 {
            Some(Duration::from_millis(debounce_ms))
        } else {
            None
        };
        self.inline_diagnostics_update = cx.spawn_in(window, async move |editor, cx| {
            if let Some(debounce) = debounce {
                cx.background_executor().timer(debounce).await;
            }
            let Some(snapshot) = editor.upgrade().map(|editor| {
                editor.update(cx, |editor, cx| editor.buffer().read(cx).snapshot(cx))
            }) else {
                return;
            };

            let new_inline_diagnostics = cx
                .background_spawn(async move {
                    let mut inline_diagnostics = Vec::<(Anchor, InlineDiagnostic)>::new();
                    for diagnostic_entry in
                        snapshot.diagnostics_in_range(MultiBufferOffset(0)..snapshot.len())
                    {
                        let message = diagnostic_entry
                            .diagnostic
                            .message
                            .split_once('\n')
                            .map(|(line, _)| line)
                            .map(SharedString::new)
                            .unwrap_or_else(|| {
                                SharedString::new(&*diagnostic_entry.diagnostic.message)
                            });
                        let start_anchor = snapshot.anchor_before(diagnostic_entry.range.start);
                        let (Ok(i) | Err(i)) = inline_diagnostics
                            .binary_search_by(|(probe, _)| probe.cmp(&start_anchor, &snapshot));
                        inline_diagnostics.insert(
                            i,
                            (
                                start_anchor,
                                InlineDiagnostic {
                                    message,
                                    group_id: diagnostic_entry.diagnostic.group_id,
                                    start: diagnostic_entry.range.start.to_point(&snapshot),
                                    is_primary: diagnostic_entry.diagnostic.is_primary,
                                    severity: diagnostic_entry.diagnostic.severity,
                                },
                            ),
                        );
                    }
                    inline_diagnostics
                })
                .await;

            editor
                .update(cx, |editor, cx| {
                    editor.inline_diagnostics = new_inline_diagnostics;
                    cx.notify();
                })
                .ok();
        });
    }

    fn pull_diagnostics(
        &mut self,
        buffer_id: BufferId,
        _window: &Window,
        cx: &mut Context<Self>,
    ) -> Option<()> {
        // `ActiveDiagnostic::All` is a special mode where editor's diagnostics are managed by the external view,
        // skip any LSP updates for it.

        if self.active_diagnostics == ActiveDiagnostic::All || !self.diagnostics_enabled() {
            return None;
        }
        let pull_diagnostics_settings = ProjectSettings::get_global(cx)
            .diagnostics
            .lsp_pull_diagnostics;
        if !pull_diagnostics_settings.enabled {
            return None;
        }
        let debounce = Duration::from_millis(pull_diagnostics_settings.debounce_ms);
        let project = self.project()?.downgrade();
        let buffer = self.buffer().read(cx).buffer(buffer_id)?;

        self.pull_diagnostics_task = cx.spawn(async move |_, cx| {
            cx.background_executor().timer(debounce).await;
            if let Ok(task) = project.update(cx, |project, cx| {
                project.lsp_store().update(cx, |lsp_store, cx| {
                    lsp_store.pull_diagnostics_for_buffer(buffer, cx)
                })
            }) {
                task.await.log_err();
            }
            project
                .update(cx, |project, cx| {
                    project.lsp_store().update(cx, |lsp_store, cx| {
                        lsp_store.pull_document_diagnostics_for_buffer_edit(buffer_id, cx);
                    })
                })
                .log_err();
        });

        Some(())
    }

    pub fn set_selections_from_remote(
        &mut self,
        selections: Vec<Selection<Anchor>>,
        pending_selection: Option<Selection<Anchor>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let old_cursor_position = self.selections.newest_anchor().head();
        self.selections
            .change_with(&self.display_snapshot(cx), |s| {
                s.select_anchors(selections);
                if let Some(pending_selection) = pending_selection {
                    s.set_pending(pending_selection, SelectMode::Character);
                } else {
                    s.clear_pending();
                }
            });
        self.selections_did_change(
            false,
            &old_cursor_position,
            SelectionEffects::default(),
            window,
            cx,
        );
    }

    pub fn transact(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
        update: impl FnOnce(&mut Self, &mut Window, &mut Context<Self>),
    ) -> Option<TransactionId> {
        self.with_selection_effects_deferred(window, cx, |this, window, cx| {
            this.start_transaction_at(Instant::now(), window, cx);
            update(this, window, cx);
            this.end_transaction_at(Instant::now(), cx)
        })
    }

    pub fn start_transaction_at(
        &mut self,
        now: Instant,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<TransactionId> {
        self.end_selection(window, cx);
        if let Some(tx_id) = self
            .buffer
            .update(cx, |buffer, cx| buffer.start_transaction_at(now, cx))
        {
            self.selection_history
                .insert_transaction(tx_id, self.selections.disjoint_anchors_arc());
            cx.emit(EditorEvent::TransactionBegun {
                transaction_id: tx_id,
            });
            Some(tx_id)
        } else {
            None
        }
    }

    pub fn end_transaction_at(
        &mut self,
        now: Instant,
        cx: &mut Context<Self>,
    ) -> Option<TransactionId> {
        if let Some(transaction_id) = self
            .buffer
            .update(cx, |buffer, cx| buffer.end_transaction_at(now, cx))
        {
            if let Some((_, end_selections)) =
                self.selection_history.transaction_mut(transaction_id)
            {
                *end_selections = Some(self.selections.disjoint_anchors_arc());
            } else {
                log::error!("unexpectedly ended a transaction that wasn't started by this editor");
            }

            cx.emit(EditorEvent::Edited { transaction_id });
            Some(transaction_id)
        } else {
            None
        }
    }

    pub fn modify_transaction_selection_history(
        &mut self,
        transaction_id: TransactionId,
        modify: impl FnOnce(&mut (Arc<[Selection<Anchor>]>, Option<Arc<[Selection<Anchor>]>>)),
    ) -> bool {
        self.selection_history
            .transaction_mut(transaction_id)
            .map(modify)
            .is_some()
    }

    pub fn set_mark(&mut self, _: &actions::SetMark, window: &mut Window, cx: &mut Context<Self>) {
        if self.selection_mark_mode {
            self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                s.move_with(&mut |_, sel| {
                    sel.collapse_to(sel.head(), SelectionGoal::None);
                });
            })
        }
        self.selection_mark_mode = true;
        cx.notify();
    }

    pub fn swap_selection_ends(
        &mut self,
        _: &actions::SwapSelectionEnds,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
            s.move_with(&mut |_, sel| {
                if sel.start != sel.end {
                    sel.reversed = !sel.reversed
                }
            });
        });
        self.request_autoscroll(Autoscroll::newest(), cx);
        cx.notify();
    }

    pub fn toggle_focus(
        workspace: &mut Workspace,
        _: &actions::ToggleFocus,
        window: &mut Window,
        cx: &mut Context<Workspace>,
    ) {
        let Some(item) = workspace.recent_active_item_by_type::<Self>(cx) else {
            return;
        };
        workspace.activate_item(&item, true, true, window, cx);
    }

    pub fn toggle_fold(
        &mut self,
        _: &actions::ToggleFold,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.buffer_kind(cx) == ItemBufferKind::Singleton {
            let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
            let selection = self.selections.newest::<Point>(&display_map);

            let range = if selection.is_empty() {
                let point = selection.head().to_display_point(&display_map);
                let start = DisplayPoint::new(point.row(), 0).to_point(&display_map);
                let end = DisplayPoint::new(point.row(), display_map.line_len(point.row()))
                    .to_point(&display_map);
                start..end
            } else {
                selection.range()
            };
            if display_map.folds_in_range(range).next().is_some() {
                self.unfold_lines(&Default::default(), window, cx)
            } else {
                self.fold(&Default::default(), window, cx)
            }
        } else {
            let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx);
            let buffer_ids: HashSet<_> = self
                .selections
                .disjoint_anchor_ranges()
                .flat_map(|range| multi_buffer_snapshot.buffer_ids_for_range(range))
                .collect();

            let should_unfold = buffer_ids
                .iter()
                .any(|buffer_id| self.is_buffer_folded(*buffer_id, cx));

            for buffer_id in buffer_ids {
                if should_unfold {
                    self.unfold_buffer(buffer_id, cx);
                } else {
                    self.fold_buffer(buffer_id, cx);
                }
            }
        }
    }

    pub fn toggle_fold_recursive(
        &mut self,
        _: &actions::ToggleFoldRecursive,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let selection = self.selections.newest::<Point>(&self.display_snapshot(cx));

        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let range = if selection.is_empty() {
            let point = selection.head().to_display_point(&display_map);
            let start = DisplayPoint::new(point.row(), 0).to_point(&display_map);
            let end = DisplayPoint::new(point.row(), display_map.line_len(point.row()))
                .to_point(&display_map);
            start..end
        } else {
            selection.range()
        };
        if display_map.folds_in_range(range).next().is_some() {
            self.unfold_recursive(&Default::default(), window, cx)
        } else {
            self.fold_recursive(&Default::default(), window, cx)
        }
    }

    pub fn fold(&mut self, _: &actions::Fold, window: &mut Window, cx: &mut Context<Self>) {
        if self.buffer_kind(cx) == ItemBufferKind::Singleton {
            let mut to_fold = Vec::new();
            let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
            let selections = self.selections.all_adjusted(&display_map);

            for selection in selections {
                let range = selection.range().sorted();
                let buffer_start_row = range.start.row;

                if range.start.row != range.end.row {
                    let mut found = false;
                    let mut row = range.start.row;
                    while row <= range.end.row {
                        if let Some(crease) = display_map.crease_for_buffer_row(MultiBufferRow(row))
                        {
                            found = true;
                            row = crease.range().end.row + 1;
                            to_fold.push(crease);
                        } else {
                            row += 1
                        }
                    }
                    if found {
                        continue;
                    }
                }

                for row in (0..=range.start.row).rev() {
                    if let Some(crease) = display_map.crease_for_buffer_row(MultiBufferRow(row))
                        && crease.range().end.row >= buffer_start_row
                    {
                        to_fold.push(crease);
                        if row <= range.start.row {
                            break;
                        }
                    }
                }
            }

            self.fold_creases(to_fold, true, window, cx);
        } else {
            let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx);
            let buffer_ids = self
                .selections
                .disjoint_anchor_ranges()
                .flat_map(|range| multi_buffer_snapshot.buffer_ids_for_range(range))
                .collect::<HashSet<_>>();
            for buffer_id in buffer_ids {
                self.fold_buffer(buffer_id, cx);
            }
        }
    }

    pub fn toggle_fold_all(
        &mut self,
        _: &actions::ToggleFoldAll,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let has_folds = if self.buffer.read(cx).is_singleton() {
            let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
            let has_folds = display_map
                .folds_in_range(MultiBufferOffset(0)..display_map.buffer_snapshot().len())
                .next()
                .is_some();
            has_folds
        } else {
            let snapshot = self.buffer.read(cx).snapshot(cx);
            let has_folds = snapshot
                .all_buffer_ids()
                .any(|buffer_id| self.is_buffer_folded(buffer_id, cx));
            has_folds
        };

        if has_folds {
            self.unfold_all(&actions::UnfoldAll, window, cx);
        } else {
            self.fold_all(&actions::FoldAll, window, cx);
        }
    }

    fn fold_at_level(
        &mut self,
        fold_at: &FoldAtLevel,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.buffer.read(cx).is_singleton() {
            return;
        }

        let fold_at_level = fold_at.0;
        let snapshot = self.buffer.read(cx).snapshot(cx);
        let mut to_fold = Vec::new();
        let mut stack = vec![(0, snapshot.max_row().0, 1)];

        let row_ranges_to_keep: Vec<Range<u32>> = self
            .selections
            .all::<Point>(&self.display_snapshot(cx))
            .into_iter()
            .map(|sel| sel.start.row..sel.end.row)
            .collect();

        while let Some((mut start_row, end_row, current_level)) = stack.pop() {
            while start_row < end_row {
                match self
                    .snapshot(window, cx)
                    .crease_for_buffer_row(MultiBufferRow(start_row))
                {
                    Some(crease) => {
                        let nested_start_row = crease.range().start.row + 1;
                        let nested_end_row = crease.range().end.row;

                        if current_level < fold_at_level {
                            stack.push((nested_start_row, nested_end_row, current_level + 1));
                        } else if current_level == fold_at_level {
                            // Fold iff there is no selection completely contained within the fold region
                            if !row_ranges_to_keep.iter().any(|selection| {
                                selection.end >= nested_start_row
                                    && selection.start <= nested_end_row
                            }) {
                                to_fold.push(crease);
                            }
                        }

                        start_row = nested_end_row + 1;
                    }
                    None => start_row += 1,
                }
            }
        }

        self.fold_creases(to_fold, true, window, cx);
    }

    pub fn fold_at_level_1(
        &mut self,
        _: &actions::FoldAtLevel1,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.fold_at_level(&actions::FoldAtLevel(1), window, cx);
    }

    pub fn fold_at_level_2(
        &mut self,
        _: &actions::FoldAtLevel2,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.fold_at_level(&actions::FoldAtLevel(2), window, cx);
    }

    pub fn fold_at_level_3(
        &mut self,
        _: &actions::FoldAtLevel3,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.fold_at_level(&actions::FoldAtLevel(3), window, cx);
    }

    pub fn fold_at_level_4(
        &mut self,
        _: &actions::FoldAtLevel4,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.fold_at_level(&actions::FoldAtLevel(4), window, cx);
    }

    pub fn fold_at_level_5(
        &mut self,
        _: &actions::FoldAtLevel5,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.fold_at_level(&actions::FoldAtLevel(5), window, cx);
    }

    pub fn fold_at_level_6(
        &mut self,
        _: &actions::FoldAtLevel6,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.fold_at_level(&actions::FoldAtLevel(6), window, cx);
    }

    pub fn fold_at_level_7(
        &mut self,
        _: &actions::FoldAtLevel7,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.fold_at_level(&actions::FoldAtLevel(7), window, cx);
    }

    pub fn fold_at_level_8(
        &mut self,
        _: &actions::FoldAtLevel8,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.fold_at_level(&actions::FoldAtLevel(8), window, cx);
    }

    pub fn fold_at_level_9(
        &mut self,
        _: &actions::FoldAtLevel9,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.fold_at_level(&actions::FoldAtLevel(9), window, cx);
    }

    pub fn fold_all(&mut self, _: &actions::FoldAll, window: &mut Window, cx: &mut Context<Self>) {
        if self.buffer.read(cx).is_singleton() {
            let mut fold_ranges = Vec::new();
            let snapshot = self.buffer.read(cx).snapshot(cx);

            for row in 0..snapshot.max_row().0 {
                if let Some(foldable_range) = self
                    .snapshot(window, cx)
                    .crease_for_buffer_row(MultiBufferRow(row))
                {
                    fold_ranges.push(foldable_range);
                }
            }

            self.fold_creases(fold_ranges, true, window, cx);
        } else {
            self.toggle_fold_multiple_buffers = cx.spawn_in(window, async move |editor, cx| {
                editor
                    .update_in(cx, |editor, _, cx| {
                        let snapshot = editor.buffer.read(cx).snapshot(cx);
                        for buffer_id in snapshot.all_buffer_ids() {
                            editor.fold_buffer(buffer_id, cx);
                        }
                    })
                    .ok();
            });
        }
    }

    pub fn fold_function_bodies(
        &mut self,
        _: &actions::FoldFunctionBodies,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let snapshot = self.buffer.read(cx).snapshot(cx);

        let ranges = snapshot
            .text_object_ranges(
                MultiBufferOffset(0)..snapshot.len(),
                TreeSitterOptions::default(),
            )
            .filter_map(|(range, obj)| (obj == TextObject::InsideFunction).then_some(range))
            .collect::<Vec<_>>();

        let creases = ranges
            .into_iter()
            .map(|range| Crease::simple(range, self.display_map.read(cx).fold_placeholder.clone()))
            .collect();

        self.fold_creases(creases, true, window, cx);
    }

    pub fn fold_recursive(
        &mut self,
        _: &actions::FoldRecursive,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let mut to_fold = Vec::new();
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let selections = self.selections.all_adjusted(&display_map);

        for selection in selections {
            let range = selection.range().sorted();
            let buffer_start_row = range.start.row;

            if range.start.row != range.end.row {
                let mut found = false;
                for row in range.start.row..=range.end.row {
                    if let Some(crease) = display_map.crease_for_buffer_row(MultiBufferRow(row)) {
                        found = true;
                        to_fold.push(crease);
                    }
                }
                if found {
                    continue;
                }
            }

            for row in (0..=range.start.row).rev() {
                if let Some(crease) = display_map.crease_for_buffer_row(MultiBufferRow(row)) {
                    if crease.range().end.row >= buffer_start_row {
                        to_fold.push(crease);
                    } else {
                        break;
                    }
                }
            }
        }

        self.fold_creases(to_fold, true, window, cx);
    }

    pub fn fold_at(
        &mut self,
        buffer_row: MultiBufferRow,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));

        if let Some(crease) = display_map.crease_for_buffer_row(buffer_row) {
            let autoscroll = self
                .selections
                .all::<Point>(&display_map)
                .iter()
                .any(|selection| crease.range().overlaps(&selection.range()));

            self.fold_creases(vec![crease], autoscroll, window, cx);
        }
    }

    pub fn unfold_lines(&mut self, _: &UnfoldLines, _window: &mut Window, cx: &mut Context<Self>) {
        if self.buffer_kind(cx) == ItemBufferKind::Singleton {
            let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
            let buffer = display_map.buffer_snapshot();
            let selections = self.selections.all::<Point>(&display_map);
            let ranges = selections
                .iter()
                .map(|s| {
                    let range = s.display_range(&display_map).sorted();
                    let mut start = range.start.to_point(&display_map);
                    let mut end = range.end.to_point(&display_map);
                    start.column = 0;
                    end.column = buffer.line_len(MultiBufferRow(end.row));
                    start..end
                })
                .collect::<Vec<_>>();

            self.unfold_ranges(&ranges, true, true, cx);
        } else {
            let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx);
            let buffer_ids = self
                .selections
                .disjoint_anchor_ranges()
                .flat_map(|range| multi_buffer_snapshot.buffer_ids_for_range(range))
                .collect::<HashSet<_>>();
            for buffer_id in buffer_ids {
                self.unfold_buffer(buffer_id, cx);
            }
        }
    }

    pub fn unfold_recursive(
        &mut self,
        _: &UnfoldRecursive,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let selections = self.selections.all::<Point>(&display_map);
        let ranges = selections
            .iter()
            .map(|s| {
                let mut range = s.display_range(&display_map).sorted();
                *range.start.column_mut() = 0;
                *range.end.column_mut() = display_map.line_len(range.end.row());
                let start = range.start.to_point(&display_map);
                let end = range.end.to_point(&display_map);
                start..end
            })
            .collect::<Vec<_>>();

        self.unfold_ranges(&ranges, true, true, cx);
    }

    pub fn unfold_at(
        &mut self,
        buffer_row: MultiBufferRow,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));

        let intersection_range = Point::new(buffer_row.0, 0)
            ..Point::new(
                buffer_row.0,
                display_map.buffer_snapshot().line_len(buffer_row),
            );

        let autoscroll = self
            .selections
            .all::<Point>(&display_map)
            .iter()
            .any(|selection| RangeExt::overlaps(&selection.range(), &intersection_range));

        self.unfold_ranges(&[intersection_range], true, autoscroll, cx);
    }

    pub fn unfold_all(
        &mut self,
        _: &actions::UnfoldAll,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.buffer.read(cx).is_singleton() {
            let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
            self.unfold_ranges(
                &[MultiBufferOffset(0)..display_map.buffer_snapshot().len()],
                true,
                true,
                cx,
            );
        } else {
            self.toggle_fold_multiple_buffers = cx.spawn(async move |editor, cx| {
                editor
                    .update(cx, |editor, cx| {
                        let snapshot = editor.buffer.read(cx).snapshot(cx);
                        for buffer_id in snapshot.all_buffer_ids() {
                            editor.unfold_buffer(buffer_id, cx);
                        }
                    })
                    .ok();
            });
        }
    }

    pub fn fold_selected_ranges(
        &mut self,
        _: &FoldSelectedRanges,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let selections = self.selections.all_adjusted(&display_map);
        let ranges = selections
            .into_iter()
            .map(|s| Crease::simple(s.range(), display_map.fold_placeholder.clone()))
            .collect::<Vec<_>>();
        self.fold_creases(ranges, true, window, cx);
    }

    pub fn fold_ranges<T: ToOffset + Clone>(
        &mut self,
        ranges: Vec<Range<T>>,
        auto_scroll: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let ranges = ranges
            .into_iter()
            .map(|r| Crease::simple(r, display_map.fold_placeholder.clone()))
            .collect::<Vec<_>>();
        self.fold_creases(ranges, auto_scroll, window, cx);
    }

    pub fn fold_creases<T: ToOffset + Clone>(
        &mut self,
        creases: Vec<Crease<T>>,
        auto_scroll: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if creases.is_empty() {
            return;
        }

        self.display_map.update(cx, |map, cx| map.fold(creases, cx));

        if auto_scroll {
            self.request_autoscroll(Autoscroll::fit(), cx);
        }

        cx.notify();

        self.scrollbar_marker_state.dirty = true;
        self.update_data_on_scroll(window, cx);
        self.folds_did_change(cx);
    }

    /// Removes any folds whose ranges intersect any of the given ranges.
    pub fn unfold_ranges<T: ToOffset + Clone>(
        &mut self,
        ranges: &[Range<T>],
        inclusive: bool,
        auto_scroll: bool,
        cx: &mut Context<Self>,
    ) {
        self.remove_folds_with(ranges, auto_scroll, cx, |map, cx| {
            map.unfold_intersecting(ranges.iter().cloned(), inclusive, cx);
        });
        self.folds_did_change(cx);
    }

    pub fn fold_buffer(&mut self, buffer_id: BufferId, cx: &mut Context<Self>) {
        self.fold_buffers([buffer_id], cx);
    }

    pub fn fold_buffers(
        &mut self,
        buffer_ids: impl IntoIterator<Item = BufferId>,
        cx: &mut Context<Self>,
    ) {
        if self.buffer().read(cx).is_singleton() {
            return;
        }

        let ids_to_fold: Vec<BufferId> = buffer_ids
            .into_iter()
            .filter(|id| !self.is_buffer_folded(*id, cx))
            .collect();

        if ids_to_fold.is_empty() {
            return;
        }

        self.display_map.update(cx, |display_map, cx| {
            display_map.fold_buffers(ids_to_fold.clone(), cx)
        });

        let snapshot = self.display_snapshot(cx);
        self.selections.change_with(&snapshot, |selections| {
            for buffer_id in ids_to_fold.iter().copied() {
                selections.remove_selections_from_buffer(buffer_id);
            }
        });

        cx.emit(EditorEvent::BufferFoldToggled {
            ids: ids_to_fold,
            folded: true,
        });
        cx.notify();
    }

    pub fn unfold_buffer(&mut self, buffer_id: BufferId, cx: &mut Context<Self>) {
        if self.buffer().read(cx).is_singleton() || !self.is_buffer_folded(buffer_id, cx) {
            return;
        }
        self.display_map.update(cx, |display_map, cx| {
            display_map.unfold_buffers([buffer_id], cx);
        });
        cx.emit(EditorEvent::BufferFoldToggled {
            ids: vec![buffer_id],
            folded: false,
        });
        cx.notify();
    }

    pub fn is_buffer_folded(&self, buffer: BufferId, cx: &App) -> bool {
        self.display_map.read(cx).is_buffer_folded(buffer)
    }

    pub fn has_any_buffer_folded(&self, cx: &App) -> bool {
        if self.buffer().read(cx).is_singleton() {
            return false;
        }
        !self.folded_buffers(cx).is_empty()
    }

    pub fn folded_buffers<'a>(&self, cx: &'a App) -> &'a HashSet<BufferId> {
        self.display_map.read(cx).folded_buffers()
    }

    pub fn disable_header_for_buffer(&mut self, buffer_id: BufferId, cx: &mut Context<Self>) {
        self.display_map.update(cx, |display_map, cx| {
            display_map.disable_header_for_buffer(buffer_id, cx);
        });
        cx.notify();
    }

    /// Removes any folds with the given ranges.
    pub fn remove_folds_with_type<T: ToOffset + Clone>(
        &mut self,
        ranges: &[Range<T>],
        type_id: TypeId,
        auto_scroll: bool,
        cx: &mut Context<Self>,
    ) {
        self.remove_folds_with(ranges, auto_scroll, cx, |map, cx| {
            map.remove_folds_with_type(ranges.iter().cloned(), type_id, cx)
        });
        self.folds_did_change(cx);
    }

    fn remove_folds_with<T: ToOffset + Clone>(
        &mut self,
        ranges: &[Range<T>],
        auto_scroll: bool,
        cx: &mut Context<Self>,
        update: impl FnOnce(&mut DisplayMap, &mut Context<DisplayMap>),
    ) {
        if ranges.is_empty() {
            return;
        }

        self.display_map.update(cx, update);

        if auto_scroll {
            self.request_autoscroll(Autoscroll::fit(), cx);
        }

        cx.notify();
        self.scrollbar_marker_state.dirty = true;
        self.active_indent_guides_state.dirty = true;
    }

    pub fn update_renderer_widths(
        &mut self,
        widths: impl IntoIterator<Item = (ChunkRendererId, Pixels)>,
        cx: &mut Context<Self>,
    ) -> bool {
        self.display_map
            .update(cx, |map, cx| map.update_fold_widths(widths, cx))
    }

    pub fn default_fold_placeholder(&self, cx: &App) -> FoldPlaceholder {
        self.display_map.read(cx).fold_placeholder.clone()
    }

    pub fn set_expand_all_diff_hunks(&mut self, cx: &mut App) {
        self.buffer.update(cx, |buffer, cx| {
            buffer.set_all_diff_hunks_expanded(cx);
        });
    }

    pub fn expand_all_diff_hunks(
        &mut self,
        _: &ExpandAllDiffHunks,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.buffer.update(cx, |buffer, cx| {
            buffer.expand_diff_hunks(vec![Anchor::Min..Anchor::Max], cx)
        });
    }

    pub fn collapse_all_diff_hunks(
        &mut self,
        _: &CollapseAllDiffHunks,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.buffer.update(cx, |buffer, cx| {
            buffer.collapse_diff_hunks(vec![Anchor::Min..Anchor::Max], cx)
        });
    }

    pub fn toggle_selected_diff_hunks(
        &mut self,
        _: &ToggleSelectedDiffHunks,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let ranges: Vec<_> = self
            .selections
            .disjoint_anchors()
            .iter()
            .map(|s| s.range())
            .collect();
        self.toggle_diff_hunks_in_ranges(ranges, cx);
    }

    pub fn diff_hunks_in_ranges<'a>(
        &'a self,
        ranges: &'a [Range<Anchor>],
        buffer: &'a MultiBufferSnapshot,
    ) -> impl 'a + Iterator<Item = MultiBufferDiffHunk> {
        ranges.iter().flat_map(move |range| {
            let end_excerpt = buffer.excerpt_containing(range.end..range.end);
            let range = range.to_point(buffer);
            let mut peek_end = range.end;
            if range.end.row < buffer.max_row().0 {
                peek_end = Point::new(range.end.row + 1, 0);
            }
            buffer
                .diff_hunks_in_range(range.start..peek_end)
                .filter(move |hunk| {
                    if let Some((_, excerpt_range)) = &end_excerpt
                        && let Some(end_anchor) =
                            buffer.anchor_in_excerpt(excerpt_range.context.end)
                        && let Some(hunk_end_anchor) =
                            buffer.anchor_in_excerpt(hunk.excerpt_range.context.end)
                        && hunk_end_anchor.cmp(&end_anchor, buffer).is_gt()
                    {
                        false
                    } else {
                        true
                    }
                })
        })
    }

    pub fn has_stageable_diff_hunks_in_ranges(
        &self,
        ranges: &[Range<Anchor>],
        snapshot: &MultiBufferSnapshot,
    ) -> bool {
        let mut hunks = self.diff_hunks_in_ranges(ranges, snapshot);
        hunks.any(|hunk| hunk.status().has_secondary_hunk())
    }

    pub fn toggle_staged_selected_diff_hunks(
        &mut self,
        _: &::git::ToggleStaged,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let snapshot = self.buffer.read(cx).snapshot(cx);
        let ranges: Vec<_> = self
            .selections
            .disjoint_anchors()
            .iter()
            .map(|s| s.range())
            .collect();
        let stage = self.has_stageable_diff_hunks_in_ranges(&ranges, &snapshot);
        self.stage_or_unstage_diff_hunks(stage, ranges, cx);
    }

    pub fn set_render_diff_hunk_controls(
        &mut self,
        render_diff_hunk_controls: RenderDiffHunkControlsFn,
        cx: &mut Context<Self>,
    ) {
        self.render_diff_hunk_controls = render_diff_hunk_controls;
        cx.notify();
    }

    pub fn stage_and_next(
        &mut self,
        _: &::git::StageAndNext,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.do_stage_or_unstage_and_next(true, window, cx);
    }

    pub fn unstage_and_next(
        &mut self,
        _: &::git::UnstageAndNext,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.do_stage_or_unstage_and_next(false, window, cx);
    }

    pub fn stage_or_unstage_diff_hunks(
        &mut self,
        stage: bool,
        ranges: Vec<Range<Anchor>>,
        cx: &mut Context<Self>,
    ) {
        if self.delegate_stage_and_restore {
            let snapshot = self.buffer.read(cx).snapshot(cx);
            let hunks: Vec<_> = self.diff_hunks_in_ranges(&ranges, &snapshot).collect();
            if !hunks.is_empty() {
                cx.emit(EditorEvent::StageOrUnstageRequested { stage, hunks });
            }
            return;
        }
        let task = self.save_buffers_for_ranges_if_needed(&ranges, cx);
        cx.spawn(async move |this, cx| {
            task.await?;
            this.update(cx, |this, cx| {
                let snapshot = this.buffer.read(cx).snapshot(cx);
                let chunk_by = this
                    .diff_hunks_in_ranges(&ranges, &snapshot)
                    .chunk_by(|hunk| hunk.buffer_id);
                for (buffer_id, hunks) in &chunk_by {
                    this.do_stage_or_unstage(stage, buffer_id, hunks, cx);
                }
            })
        })
        .detach_and_log_err(cx);
    }

    fn save_buffers_for_ranges_if_needed(
        &mut self,
        ranges: &[Range<Anchor>],
        cx: &mut Context<Editor>,
    ) -> Task<Result<()>> {
        let multibuffer = self.buffer.read(cx);
        let snapshot = multibuffer.read(cx);
        let buffer_ids: HashSet<_> = ranges
            .iter()
            .flat_map(|range| snapshot.buffer_ids_for_range(range.clone()))
            .collect();
        drop(snapshot);

        let mut buffers = HashSet::default();
        for buffer_id in buffer_ids {
            if let Some(buffer_entity) = multibuffer.buffer(buffer_id) {
                let buffer = buffer_entity.read(cx);
                if buffer.file().is_some_and(|file| file.disk_state().exists()) && buffer.is_dirty()
                {
                    buffers.insert(buffer_entity);
                }
            }
        }

        if let Some(project) = &self.project {
            project.update(cx, |project, cx| project.save_buffers(buffers, cx))
        } else {
            Task::ready(Ok(()))
        }
    }

    fn do_stage_or_unstage_and_next(
        &mut self,
        stage: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let ranges = self.selections.disjoint_anchor_ranges().collect::<Vec<_>>();

        if ranges.iter().any(|range| range.start != range.end) {
            self.stage_or_unstage_diff_hunks(stage, ranges, cx);
            return;
        }

        self.stage_or_unstage_diff_hunks(stage, ranges, cx);

        let all_diff_hunks_expanded = self.buffer().read(cx).all_diff_hunks_expanded();
        let wrap_around = !all_diff_hunks_expanded;
        let snapshot = self.snapshot(window, cx);
        let position = self
            .selections
            .newest::<Point>(&snapshot.display_snapshot)
            .head();

        self.go_to_hunk_before_or_after_position(
            &snapshot,
            position,
            Direction::Next,
            wrap_around,
            window,
            cx,
        );
    }

    pub(crate) fn do_stage_or_unstage(
        &self,
        stage: bool,
        buffer_id: BufferId,
        hunks: impl Iterator<Item = MultiBufferDiffHunk>,
        cx: &mut App,
    ) -> Option<()> {
        let project = self.project()?;
        let buffer = project.read(cx).buffer_for_id(buffer_id, cx)?;
        let diff = self.buffer.read(cx).diff_for(buffer_id)?;
        let buffer_snapshot = buffer.read(cx).snapshot();
        let file_exists = buffer_snapshot
            .file()
            .is_some_and(|file| file.disk_state().exists());
        diff.update(cx, |diff, cx| {
            diff.stage_or_unstage_hunks(
                stage,
                &hunks
                    .map(|hunk| buffer_diff::DiffHunk {
                        buffer_range: hunk.buffer_range,
                        // We don't need to pass in word diffs here because they're only used for rendering and
                        // this function changes internal state
                        base_word_diffs: Vec::default(),
                        buffer_word_diffs: Vec::default(),
                        diff_base_byte_range: hunk.diff_base_byte_range.start.0
                            ..hunk.diff_base_byte_range.end.0,
                        secondary_status: hunk.status.secondary,
                        range: Point::zero()..Point::zero(), // unused
                    })
                    .collect::<Vec<_>>(),
                &buffer_snapshot,
                file_exists,
                cx,
            )
        });
        None
    }

    pub fn expand_selected_diff_hunks(&mut self, cx: &mut Context<Self>) {
        let ranges: Vec<_> = self
            .selections
            .disjoint_anchors()
            .iter()
            .map(|s| s.range())
            .collect();
        self.buffer
            .update(cx, |buffer, cx| buffer.expand_diff_hunks(ranges, cx))
    }

    pub fn clear_expanded_diff_hunks(&mut self, cx: &mut Context<Self>) -> bool {
        self.buffer.update(cx, |buffer, cx| {
            let ranges = vec![Anchor::Min..Anchor::Max];
            if !buffer.all_diff_hunks_expanded()
                && buffer.has_expanded_diff_hunks_in_ranges(&ranges, cx)
            {
                buffer.collapse_diff_hunks(ranges, cx);
                true
            } else {
                false
            }
        })
    }

    fn has_any_expanded_diff_hunks(&self, cx: &App) -> bool {
        if self.buffer.read(cx).all_diff_hunks_expanded() {
            return true;
        }
        let ranges = vec![Anchor::Min..Anchor::Max];
        self.buffer
            .read(cx)
            .has_expanded_diff_hunks_in_ranges(&ranges, cx)
    }

    fn toggle_diff_hunks_in_ranges(
        &mut self,
        ranges: Vec<Range<Anchor>>,
        cx: &mut Context<Editor>,
    ) {
        self.buffer.update(cx, |buffer, cx| {
            let expand = !buffer.has_expanded_diff_hunks_in_ranges(&ranges, cx);
            buffer.expand_or_collapse_diff_hunks(ranges, expand, cx);
        })
    }

    fn toggle_single_diff_hunk(&mut self, range: Range<Anchor>, cx: &mut Context<Self>) {
        self.buffer.update(cx, |buffer, cx| {
            buffer.toggle_single_diff_hunk(range, cx);
        })
    }

    pub(crate) fn apply_all_diff_hunks(
        &mut self,
        _: &ApplyAllDiffHunks,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);

        let buffers = self.buffer.read(cx).all_buffers();
        for branch_buffer in buffers {
            branch_buffer.update(cx, |branch_buffer, cx| {
                branch_buffer.merge_into_base(Vec::new(), cx);
            });
        }

        if let Some(project) = self.project.clone() {
            self.save(
                SaveOptions {
                    format: true,
                    autosave: false,
                },
                project,
                window,
                cx,
            )
            .detach_and_log_err(cx);
        }
    }

    pub(crate) fn apply_selected_diff_hunks(
        &mut self,
        _: &ApplyDiffHunk,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        let snapshot = self.snapshot(window, cx);
        let hunks = snapshot.hunks_for_ranges(
            self.selections
                .all(&snapshot.display_snapshot)
                .into_iter()
                .map(|selection| selection.range()),
        );
        let mut ranges_by_buffer = HashMap::default();
        self.transact(window, cx, |editor, _window, cx| {
            for hunk in hunks {
                if let Some(buffer) = editor.buffer.read(cx).buffer(hunk.buffer_id) {
                    ranges_by_buffer
                        .entry(buffer.clone())
                        .or_insert_with(Vec::new)
                        .push(hunk.buffer_range.to_offset(buffer.read(cx)));
                }
            }

            for (buffer, ranges) in ranges_by_buffer {
                buffer.update(cx, |buffer, cx| {
                    buffer.merge_into_base(ranges, cx);
                });
            }
        });

        if let Some(project) = self.project.clone() {
            self.save(
                SaveOptions {
                    format: true,
                    autosave: false,
                },
                project,
                window,
                cx,
            )
            .detach_and_log_err(cx);
        }
    }

    pub fn set_gutter_hovered(&mut self, hovered: bool, cx: &mut Context<Self>) {
        if hovered != self.gutter_hovered {
            self.gutter_hovered = hovered;
            cx.notify();
        }
    }

    pub fn insert_blocks(
        &mut self,
        blocks: impl IntoIterator<Item = BlockProperties<Anchor>>,
        autoscroll: Option<Autoscroll>,
        cx: &mut Context<Self>,
    ) -> Vec<CustomBlockId> {
        let blocks = self
            .display_map
            .update(cx, |display_map, cx| display_map.insert_blocks(blocks, cx));
        if let Some(autoscroll) = autoscroll {
            self.request_autoscroll(autoscroll, cx);
        }
        cx.notify();
        blocks
    }

    pub fn resize_blocks(
        &mut self,
        heights: HashMap<CustomBlockId, u32>,
        autoscroll: Option<Autoscroll>,
        cx: &mut Context<Self>,
    ) {
        self.display_map
            .update(cx, |display_map, cx| display_map.resize_blocks(heights, cx));
        if let Some(autoscroll) = autoscroll {
            self.request_autoscroll(autoscroll, cx);
        }
        cx.notify();
    }

    pub fn replace_blocks(
        &mut self,
        renderers: HashMap<CustomBlockId, RenderBlock>,
        autoscroll: Option<Autoscroll>,
        cx: &mut Context<Self>,
    ) {
        self.display_map
            .update(cx, |display_map, _cx| display_map.replace_blocks(renderers));
        if let Some(autoscroll) = autoscroll {
            self.request_autoscroll(autoscroll, cx);
        }
        cx.notify();
    }

    pub fn remove_blocks(
        &mut self,
        block_ids: HashSet<CustomBlockId>,
        autoscroll: Option<Autoscroll>,
        cx: &mut Context<Self>,
    ) {
        self.display_map.update(cx, |display_map, cx| {
            display_map.remove_blocks(block_ids, cx)
        });
        if let Some(autoscroll) = autoscroll {
            self.request_autoscroll(autoscroll, cx);
        }
        cx.notify();
    }

    pub fn row_for_block(
        &self,
        block_id: CustomBlockId,
        cx: &mut Context<Self>,
    ) -> Option<DisplayRow> {
        self.display_map
            .update(cx, |map, cx| map.row_for_block(block_id, cx))
    }

    pub(crate) fn set_focused_block(&mut self, focused_block: FocusedBlock) {
        self.focused_block = Some(focused_block);
    }

    pub(crate) fn take_focused_block(&mut self) -> Option<FocusedBlock> {
        self.focused_block.take()
    }

    pub fn insert_creases(
        &mut self,
        creases: impl IntoIterator<Item = Crease<Anchor>>,
        cx: &mut Context<Self>,
    ) -> Vec<CreaseId> {
        self.display_map
            .update(cx, |map, cx| map.insert_creases(creases, cx))
    }

    pub fn remove_creases(
        &mut self,
        ids: impl IntoIterator<Item = CreaseId>,
        cx: &mut Context<Self>,
    ) -> Vec<(CreaseId, Range<Anchor>)> {
        self.display_map
            .update(cx, |map, cx| map.remove_creases(ids, cx))
    }

    pub fn longest_row(&self, cx: &mut App) -> DisplayRow {
        self.display_map
            .update(cx, |map, cx| map.snapshot(cx))
            .longest_row()
    }

    pub fn max_point(&self, cx: &mut App) -> DisplayPoint {
        self.display_map
            .update(cx, |map, cx| map.snapshot(cx))
            .max_point()
    }

    pub fn text(&self, cx: &App) -> String {
        self.buffer.read(cx).read(cx).text()
    }

    pub fn is_empty(&self, cx: &App) -> bool {
        self.buffer.read(cx).read(cx).is_empty()
    }

    pub fn text_option(&self, cx: &App) -> Option<String> {
        let text = self.text(cx);
        let text = text.trim();

        if text.is_empty() {
            return None;
        }

        Some(text.to_string())
    }

    pub fn set_text(
        &mut self,
        text: impl Into<Arc<str>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.transact(window, cx, |this, _, cx| {
            this.buffer
                .read(cx)
                .as_singleton()
                .expect("you can only call set_text on editors for singleton buffers")
                .update(cx, |buffer, cx| buffer.set_text(text, cx));
        });
    }

    pub fn display_text(&self, cx: &mut App) -> String {
        self.display_map
            .update(cx, |map, cx| map.snapshot(cx))
            .text()
    }

    fn create_minimap(
        &self,
        minimap_settings: MinimapSettings,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Entity<Self>> {
        (minimap_settings.minimap_enabled() && self.buffer_kind(cx) == ItemBufferKind::Singleton)
            .then(|| self.initialize_new_minimap(minimap_settings, window, cx))
    }

    fn initialize_new_minimap(
        &self,
        minimap_settings: MinimapSettings,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Entity<Self> {
        const MINIMAP_FONT_WEIGHT: gpui::FontWeight = gpui::FontWeight::BLACK;
        const MINIMAP_FONT_FAMILY: SharedString = SharedString::new_static(".ZedMono");

        let mut minimap = Editor::new_internal(
            EditorMode::Minimap {
                parent: cx.weak_entity(),
            },
            self.buffer.clone(),
            None,
            Some(self.display_map.clone()),
            window,
            cx,
        );
        let my_snapshot = self.display_map.update(cx, |map, cx| map.snapshot(cx));
        let minimap_snapshot = minimap.display_map.update(cx, |map, cx| map.snapshot(cx));
        minimap.scroll_manager.clone_state(
            &self.scroll_manager,
            &my_snapshot,
            &minimap_snapshot,
            cx,
        );
        minimap.set_text_style_refinement(TextStyleRefinement {
            font_size: Some(MINIMAP_FONT_SIZE),
            font_weight: Some(MINIMAP_FONT_WEIGHT),
            font_family: Some(MINIMAP_FONT_FAMILY),
            ..Default::default()
        });
        minimap.update_minimap_configuration(minimap_settings, cx);
        cx.new(|_| minimap)
    }

    fn update_minimap_configuration(&mut self, minimap_settings: MinimapSettings, cx: &App) {
        let current_line_highlight = minimap_settings
            .current_line_highlight
            .unwrap_or_else(|| EditorSettings::get_global(cx).current_line_highlight);
        self.set_current_line_highlight(Some(current_line_highlight));
    }

    pub fn minimap(&self) -> Option<&Entity<Self>> {
        self.minimap
            .as_ref()
            .filter(|_| self.minimap_visibility.visible())
    }

    pub fn wrap_guides(&self, cx: &App) -> SmallVec<[(usize, bool); 2]> {
        let mut wrap_guides = smallvec![];

        if self.show_wrap_guides == Some(false) {
            return wrap_guides;
        }

        let settings = self.buffer.read(cx).language_settings(cx);
        if settings.show_wrap_guides {
            match self.soft_wrap_mode(cx) {
                SoftWrap::Column(soft_wrap) => {
                    wrap_guides.push((soft_wrap as usize, true));
                }
                SoftWrap::Bounded(soft_wrap) => {
                    wrap_guides.push((soft_wrap as usize, true));
                }
                SoftWrap::GitDiff | SoftWrap::None | SoftWrap::EditorWidth => {}
            }
            wrap_guides.extend(settings.wrap_guides.iter().map(|guide| (*guide, false)))
        }

        wrap_guides
    }

    pub fn soft_wrap_mode(&self, cx: &App) -> SoftWrap {
        let settings = self.buffer.read(cx).language_settings(cx);
        let mode = self.soft_wrap_mode_override.unwrap_or(settings.soft_wrap);
        match mode {
            language_settings::SoftWrap::PreferLine | language_settings::SoftWrap::None => {
                SoftWrap::None
            }
            language_settings::SoftWrap::EditorWidth => SoftWrap::EditorWidth,
            language_settings::SoftWrap::PreferredLineLength => {
                SoftWrap::Column(settings.preferred_line_length)
            }
            language_settings::SoftWrap::Bounded => {
                SoftWrap::Bounded(settings.preferred_line_length)
            }
        }
    }

    pub fn set_soft_wrap_mode(
        &mut self,
        mode: language_settings::SoftWrap,
        cx: &mut Context<Self>,
    ) {
        self.soft_wrap_mode_override = Some(mode);
        cx.notify();
    }

    pub fn set_hard_wrap(&mut self, hard_wrap: Option<usize>, cx: &mut Context<Self>) {
        self.hard_wrap = hard_wrap;
        cx.notify();
    }

    pub fn set_text_style_refinement(&mut self, style: TextStyleRefinement) {
        self.text_style_refinement = Some(style);
    }

    /// called by the Element so we know what style we were most recently rendered with.
    pub fn set_style(&mut self, style: EditorStyle, window: &mut Window, cx: &mut Context<Self>) {
        // We intentionally do not inform the display map about the minimap style
        // so that wrapping is not recalculated and stays consistent for the editor
        // and its linked minimap.
        if !self.mode.is_minimap() {
            let font = style.text.font();
            let font_size = style.text.font_size.to_pixels(window.rem_size());
            let display_map = self
                .placeholder_display_map
                .as_ref()
                .filter(|_| self.is_empty(cx))
                .unwrap_or(&self.display_map);

            display_map.update(cx, |map, cx| map.set_font(font, font_size, cx));
        }
        self.style = Some(style);
    }

    pub fn style(&mut self, cx: &App) -> &EditorStyle {
        if self.style.is_none() {
            self.style = Some(self.create_style(cx));
        }
        self.style.as_ref().unwrap()
    }

    // Called by the element. This method is not designed to be called outside of the editor
    // element's layout code because it does not notify when rewrapping is computed synchronously.
    pub(crate) fn set_wrap_width(&self, width: Option<Pixels>, cx: &mut App) -> bool {
        if self.is_empty(cx) {
            self.placeholder_display_map
                .as_ref()
                .map_or(false, |display_map| {
                    display_map.update(cx, |map, cx| map.set_wrap_width(width, cx))
                })
        } else {
            self.display_map
                .update(cx, |map, cx| map.set_wrap_width(width, cx))
        }
    }

    pub fn set_soft_wrap(&mut self) {
        self.soft_wrap_mode_override = Some(language_settings::SoftWrap::EditorWidth)
    }

    pub fn toggle_soft_wrap(&mut self, _: &ToggleSoftWrap, _: &mut Window, cx: &mut Context<Self>) {
        if self.soft_wrap_mode_override.is_some() {
            self.soft_wrap_mode_override.take();
        } else {
            let soft_wrap = match self.soft_wrap_mode(cx) {
                SoftWrap::GitDiff => return,
                SoftWrap::None => language_settings::SoftWrap::EditorWidth,
                SoftWrap::EditorWidth | SoftWrap::Column(_) | SoftWrap::Bounded(_) => {
                    language_settings::SoftWrap::None
                }
            };
            self.soft_wrap_mode_override = Some(soft_wrap);
        }
        cx.notify();
    }

    pub fn toggle_tab_bar(&mut self, _: &ToggleTabBar, _: &mut Window, cx: &mut Context<Self>) {
        let Some(workspace) = self.workspace() else {
            return;
        };
        let fs = workspace.read(cx).app_state().fs.clone();
        let current_show = TabBarSettings::get_global(cx).show;
        update_settings_file(fs, cx, move |setting, _| {
            setting.tab_bar.get_or_insert_default().show = Some(!current_show);
        });
    }

    pub fn toggle_indent_guides(
        &mut self,
        _: &ToggleIndentGuides,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let currently_enabled = self.should_show_indent_guides().unwrap_or_else(|| {
            self.buffer
                .read(cx)
                .language_settings(cx)
                .indent_guides
                .enabled
        });
        self.show_indent_guides = Some(!currently_enabled);
        cx.notify();
    }

    fn should_show_indent_guides(&self) -> Option<bool> {
        self.show_indent_guides
    }

    pub fn disable_indent_guides_for_buffer(
        &mut self,
        buffer_id: BufferId,
        cx: &mut Context<Self>,
    ) {
        self.buffers_with_disabled_indent_guides.insert(buffer_id);
        cx.notify();
    }

    pub fn has_indent_guides_disabled_for_buffer(&self, buffer_id: BufferId) -> bool {
        self.buffers_with_disabled_indent_guides
            .contains(&buffer_id)
    }

    pub fn toggle_line_numbers(
        &mut self,
        _: &ToggleLineNumbers,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let mut editor_settings = EditorSettings::get_global(cx).clone();
        editor_settings.gutter.line_numbers = !editor_settings.gutter.line_numbers;
        EditorSettings::override_global(editor_settings, cx);
    }

    pub fn line_numbers_enabled(&self, cx: &App) -> bool {
        if let Some(show_line_numbers) = self.show_line_numbers {
            return show_line_numbers;
        }
        EditorSettings::get_global(cx).gutter.line_numbers
    }

    pub fn relative_line_numbers(&self, cx: &App) -> RelativeLineNumbers {
        match (
            self.use_relative_line_numbers,
            EditorSettings::get_global(cx).relative_line_numbers,
        ) {
            (None, setting) => setting,
            (Some(false), _) => RelativeLineNumbers::Disabled,
            (Some(true), RelativeLineNumbers::Wrapped) => RelativeLineNumbers::Wrapped,
            (Some(true), _) => RelativeLineNumbers::Enabled,
        }
    }

    pub fn toggle_relative_line_numbers(
        &mut self,
        _: &ToggleRelativeLineNumbers,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let is_relative = self.relative_line_numbers(cx);
        self.set_relative_line_number(Some(!is_relative.enabled()), cx)
    }

    pub fn set_relative_line_number(&mut self, is_relative: Option<bool>, cx: &mut Context<Self>) {
        self.use_relative_line_numbers = is_relative;
        cx.notify();
    }

    pub fn set_show_gutter(&mut self, show_gutter: bool, cx: &mut Context<Self>) {
        self.show_gutter = show_gutter;
        cx.notify();
    }

    pub fn set_show_scrollbars(&mut self, show: bool, cx: &mut Context<Self>) {
        self.show_scrollbars = ScrollbarAxes {
            horizontal: show,
            vertical: show,
        };
        cx.notify();
    }

    pub fn set_show_vertical_scrollbar(&mut self, show: bool, cx: &mut Context<Self>) {
        self.show_scrollbars.vertical = show;
        cx.notify();
    }

    pub fn set_show_horizontal_scrollbar(&mut self, show: bool, cx: &mut Context<Self>) {
        self.show_scrollbars.horizontal = show;
        cx.notify();
    }

    pub fn set_minimap_visibility(
        &mut self,
        minimap_visibility: MinimapVisibility,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.minimap_visibility != minimap_visibility {
            if minimap_visibility.visible() && self.minimap.is_none() {
                let minimap_settings = EditorSettings::get_global(cx).minimap;
                self.minimap =
                    self.create_minimap(minimap_settings.with_show_override(), window, cx);
            }
            self.minimap_visibility = minimap_visibility;
            cx.notify();
        }
    }

    pub fn disable_scrollbars_and_minimap(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.set_show_scrollbars(false, cx);
        self.set_minimap_visibility(MinimapVisibility::Disabled, window, cx);
    }

    pub fn hide_minimap_by_default(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.set_minimap_visibility(self.minimap_visibility.hidden(), window, cx);
    }

    /// Normally the text in full mode and auto height editors is padded on the
    /// left side by roughly half a character width for improved hit testing.
    ///
    /// Use this method to disable this for cases where this is not wanted (e.g.
    /// if you want to align the editor text with some other text above or below)
    /// or if you want to add this padding to single-line editors.
    pub fn set_offset_content(&mut self, offset_content: bool, cx: &mut Context<Self>) {
        self.offset_content = offset_content;
        cx.notify();
    }

    pub fn set_show_line_numbers(&mut self, show_line_numbers: bool, cx: &mut Context<Self>) {
        self.show_line_numbers = Some(show_line_numbers);
        cx.notify();
    }

    pub fn disable_expand_excerpt_buttons(&mut self, cx: &mut Context<Self>) {
        self.disable_expand_excerpt_buttons = true;
        cx.notify();
    }

    pub fn set_number_deleted_lines(&mut self, number: bool, cx: &mut Context<Self>) {
        self.number_deleted_lines = number;
        cx.notify();
    }

    pub fn set_delegate_expand_excerpts(&mut self, delegate: bool) {
        self.delegate_expand_excerpts = delegate;
    }

    pub fn set_delegate_stage_and_restore(&mut self, delegate: bool) {
        self.delegate_stage_and_restore = delegate;
    }

    pub fn set_delegate_open_excerpts(&mut self, delegate: bool) {
        self.delegate_open_excerpts = delegate;
    }

    pub fn set_on_local_selections_changed(
        &mut self,
        callback: Option<Box<dyn Fn(Point, &mut Window, &mut Context<Self>) + 'static>>,
    ) {
        self.on_local_selections_changed = callback;
    }

    pub fn set_suppress_selection_callback(&mut self, suppress: bool) {
        self.suppress_selection_callback = suppress;
    }

    pub fn set_show_git_diff_gutter(&mut self, show_git_diff_gutter: bool, cx: &mut Context<Self>) {
        self.show_git_diff_gutter = Some(show_git_diff_gutter);
        cx.notify();
    }

    pub fn set_show_code_actions(&mut self, show_code_actions: bool, cx: &mut Context<Self>) {
        self.show_code_actions = Some(show_code_actions);
        cx.notify();
    }

    pub fn set_show_runnables(&mut self, show_runnables: bool, cx: &mut Context<Self>) {
        self.show_runnables = Some(show_runnables);
        cx.notify();
    }

    pub fn set_show_breakpoints(&mut self, show_breakpoints: bool, cx: &mut Context<Self>) {
        self.show_breakpoints = Some(show_breakpoints);
        cx.notify();
    }

    pub fn set_show_diff_review_button(&mut self, show: bool, cx: &mut Context<Self>) {
        self.show_diff_review_button = show;
        cx.notify();
    }

    pub fn show_diff_review_button(&self) -> bool {
        self.show_diff_review_button
    }

    pub fn render_diff_review_button(
        &self,
        display_row: DisplayRow,
        width: Pixels,
        cx: &mut Context<Self>,
    ) -> impl IntoElement {
        let text_color = cx.theme().colors().text;
        let icon_color = cx.theme().colors().icon_accent;

        h_flex()
            .id("diff_review_button")
            .cursor_pointer()
            .w(width - px(1.))
            .h(relative(0.9))
            .justify_center()
            .rounded_sm()
            .border_1()
            .border_color(text_color.opacity(0.1))
            .bg(text_color.opacity(0.15))
            .hover(|s| {
                s.bg(icon_color.opacity(0.4))
                    .border_color(icon_color.opacity(0.5))
            })
            .child(Icon::new(IconName::Plus).size(IconSize::Small))
            .tooltip(Tooltip::text("Add Review (drag to select multiple lines)"))
            .on_mouse_down(
                gpui::MouseButton::Left,
                cx.listener(move |editor, _event: &gpui::MouseDownEvent, window, cx| {
                    editor.start_diff_review_drag(display_row, window, cx);
                }),
            )
    }

    pub fn start_diff_review_drag(
        &mut self,
        display_row: DisplayRow,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let snapshot = self.snapshot(window, cx);
        let point = snapshot
            .display_snapshot
            .display_point_to_point(DisplayPoint::new(display_row, 0), Bias::Left);
        let anchor = snapshot.buffer_snapshot().anchor_before(point);
        self.diff_review_drag_state = Some(DiffReviewDragState {
            start_anchor: anchor,
            current_anchor: anchor,
        });
        cx.notify();
    }

    pub fn update_diff_review_drag(
        &mut self,
        display_row: DisplayRow,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.diff_review_drag_state.is_none() {
            return;
        }
        let snapshot = self.snapshot(window, cx);
        let point = snapshot
            .display_snapshot
            .display_point_to_point(display_row.as_display_point(), Bias::Left);
        let anchor = snapshot.buffer_snapshot().anchor_before(point);
        if let Some(drag_state) = &mut self.diff_review_drag_state {
            drag_state.current_anchor = anchor;
            cx.notify();
        }
    }

    pub fn end_diff_review_drag(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if let Some(drag_state) = self.diff_review_drag_state.take() {
            let snapshot = self.snapshot(window, cx);
            let range = drag_state.row_range(&snapshot.display_snapshot);
            self.show_diff_review_overlay(*range.start()..*range.end(), window, cx);
        }
        cx.notify();
    }

    pub fn cancel_diff_review_drag(&mut self, cx: &mut Context<Self>) {
        self.diff_review_drag_state = None;
        cx.notify();
    }

    /// Calculates the appropriate block height for the diff review overlay.
    /// Height is in lines: 2 for input row, 1 for header when comments exist,
    /// and 2 lines per comment when expanded.
    fn calculate_overlay_height(
        &self,
        hunk_key: &DiffHunkKey,
        comments_expanded: bool,
        snapshot: &MultiBufferSnapshot,
    ) -> u32 {
        let comment_count = self.hunk_comment_count(hunk_key, snapshot);
        let base_height: u32 = 2; // Input row with avatar and buttons

        if comment_count == 0 {
            base_height
        } else if comments_expanded {
            // Header (1 line) + 2 lines per comment
            base_height + 1 + (comment_count as u32 * 2)
        } else {
            // Just header when collapsed
            base_height + 1
        }
    }

    pub fn show_diff_review_overlay(
        &mut self,
        display_range: Range<DisplayRow>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Range { start, end } = display_range.sorted();

        let buffer_snapshot = self.buffer.read(cx).snapshot(cx);
        let editor_snapshot = self.snapshot(window, cx);

        // Convert display rows to multibuffer points
        let start_point = editor_snapshot
            .display_snapshot
            .display_point_to_point(start.as_display_point(), Bias::Left);
        let end_point = editor_snapshot
            .display_snapshot
            .display_point_to_point(end.as_display_point(), Bias::Left);
        let end_multi_buffer_row = MultiBufferRow(end_point.row);

        // Create anchor range for the selected lines (start of first line to end of last line)
        let line_end = Point::new(
            end_point.row,
            buffer_snapshot.line_len(end_multi_buffer_row),
        );
        let anchor_range =
            buffer_snapshot.anchor_after(start_point)..buffer_snapshot.anchor_before(line_end);

        // Compute the hunk key for this display row
        let file_path = buffer_snapshot
            .file_at(start_point)
            .map(|file: &Arc<dyn language::File>| file.path().clone())
            .unwrap_or_else(|| Arc::from(util::rel_path::RelPath::empty()));
        let hunk_start_anchor = buffer_snapshot.anchor_before(start_point);
        let new_hunk_key = DiffHunkKey {
            file_path,
            hunk_start_anchor,
        };

        // Check if we already have an overlay for this hunk
        if let Some(existing_overlay) = self.diff_review_overlays.iter().find(|overlay| {
            Self::hunk_keys_match(&overlay.hunk_key, &new_hunk_key, &buffer_snapshot)
        }) {
            // Just focus the existing overlay's prompt editor
            let focus_handle = existing_overlay.prompt_editor.focus_handle(cx);
            window.focus(&focus_handle, cx);
            return;
        }

        // Dismiss overlays that have no comments for their hunks
        self.dismiss_overlays_without_comments(cx);

        // Get the current user's avatar URI from the project's user_store
        let user_avatar_uri = self.project.as_ref().and_then(|project| {
            let user_store = project.read(cx).user_store();
            user_store
                .read(cx)
                .current_user()
                .map(|user| user.avatar_uri.clone())
        });

        // Create anchor at the end of the last row so the block appears immediately below it
        // Use multibuffer coordinates for anchor creation
        let line_len = buffer_snapshot.line_len(end_multi_buffer_row);
        let anchor = buffer_snapshot.anchor_after(Point::new(end_multi_buffer_row.0, line_len));

        // Use the hunk key we already computed
        let hunk_key = new_hunk_key;

        // Create the prompt editor for the review input
        let prompt_editor = cx.new(|cx| {
            let mut editor = Editor::single_line(window, cx);
            editor.set_placeholder_text("Add a review comment...", window, cx);
            editor
        });

        // Register the Newline action on the prompt editor to submit the review
        let parent_editor = cx.entity().downgrade();
        let subscription = prompt_editor.update(cx, |prompt_editor, _cx| {
            prompt_editor.register_action({
                let parent_editor = parent_editor.clone();
                move |_: &crate::actions::Newline, window, cx| {
                    if let Some(editor) = parent_editor.upgrade() {
                        editor.update(cx, |editor, cx| {
                            editor.submit_diff_review_comment(window, cx);
                        });
                    }
                }
            })
        });

        // Calculate initial height based on existing comments for this hunk
        let initial_height = self.calculate_overlay_height(&hunk_key, true, &buffer_snapshot);

        // Create the overlay block
        let prompt_editor_for_render = prompt_editor.clone();
        let hunk_key_for_render = hunk_key.clone();
        let editor_handle = cx.entity().downgrade();
        let block = BlockProperties {
            style: BlockStyle::Sticky,
            placement: BlockPlacement::Below(anchor),
            height: Some(initial_height),
            render: Arc::new(move |cx| {
                Self::render_diff_review_overlay(
                    &prompt_editor_for_render,
                    &hunk_key_for_render,
                    &editor_handle,
                    cx,
                )
            }),
            priority: 0,
        };

        let block_ids = self.insert_blocks([block], None, cx);
        let Some(block_id) = block_ids.into_iter().next() else {
            log::error!("Failed to insert diff review overlay block");
            return;
        };

        self.diff_review_overlays.push(DiffReviewOverlay {
            anchor_range,
            block_id,
            prompt_editor: prompt_editor.clone(),
            hunk_key,
            comments_expanded: true,
            inline_edit_editors: HashMap::default(),
            inline_edit_subscriptions: HashMap::default(),
            user_avatar_uri,
            _subscription: subscription,
        });

        // Focus the prompt editor
        let focus_handle = prompt_editor.focus_handle(cx);
        window.focus(&focus_handle, cx);

        cx.notify();
    }

    /// Dismisses all diff review overlays.
    pub fn dismiss_all_diff_review_overlays(&mut self, cx: &mut Context<Self>) {
        if self.diff_review_overlays.is_empty() {
            return;
        }
        let block_ids: HashSet<_> = self
            .diff_review_overlays
            .drain(..)
            .map(|overlay| overlay.block_id)
            .collect();
        self.remove_blocks(block_ids, None, cx);
        cx.notify();
    }

    /// Dismisses overlays that have no comments stored for their hunks.
    /// Keeps overlays that have at least one comment.
    fn dismiss_overlays_without_comments(&mut self, cx: &mut Context<Self>) {
        let snapshot = self.buffer.read(cx).snapshot(cx);

        // First, compute which overlays have comments (to avoid borrow issues with retain)
        let overlays_with_comments: Vec<bool> = self
            .diff_review_overlays
            .iter()
            .map(|overlay| self.hunk_comment_count(&overlay.hunk_key, &snapshot) > 0)
            .collect();

        // Now collect block IDs to remove and retain overlays
        let mut block_ids_to_remove = HashSet::default();
        let mut index = 0;
        self.diff_review_overlays.retain(|overlay| {
            let has_comments = overlays_with_comments[index];
            index += 1;
            if !has_comments {
                block_ids_to_remove.insert(overlay.block_id);
            }
            has_comments
        });

        if !block_ids_to_remove.is_empty() {
            self.remove_blocks(block_ids_to_remove, None, cx);
            cx.notify();
        }
    }

    /// Refreshes the diff review overlay block to update its height and render function.
    /// Uses resize_blocks and replace_blocks to avoid visual flicker from remove+insert.
    fn refresh_diff_review_overlay_height(
        &mut self,
        hunk_key: &DiffHunkKey,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        // Extract all needed data from overlay first to avoid borrow conflicts
        let snapshot = self.buffer.read(cx).snapshot(cx);
        let (comments_expanded, block_id, prompt_editor) = {
            let Some(overlay) = self
                .diff_review_overlays
                .iter()
                .find(|overlay| Self::hunk_keys_match(&overlay.hunk_key, hunk_key, &snapshot))
            else {
                return;
            };

            (
                overlay.comments_expanded,
                overlay.block_id,
                overlay.prompt_editor.clone(),
            )
        };

        // Calculate new height
        let snapshot = self.buffer.read(cx).snapshot(cx);
        let new_height = self.calculate_overlay_height(hunk_key, comments_expanded, &snapshot);

        // Update the block height using resize_blocks (avoids flicker)
        let mut heights = HashMap::default();
        heights.insert(block_id, new_height);
        self.resize_blocks(heights, None, cx);

        // Update the render function using replace_blocks (avoids flicker)
        let hunk_key_for_render = hunk_key.clone();
        let editor_handle = cx.entity().downgrade();
        let render: Arc<dyn Fn(&mut BlockContext) -> AnyElement + Send + Sync> =
            Arc::new(move |cx| {
                Self::render_diff_review_overlay(
                    &prompt_editor,
                    &hunk_key_for_render,
                    &editor_handle,
                    cx,
                )
            });

        let mut renderers = HashMap::default();
        renderers.insert(block_id, render);
        self.replace_blocks(renderers, None, cx);
    }

    /// Action handler for SubmitDiffReviewComment.
    pub fn submit_diff_review_comment_action(
        &mut self,
        _: &SubmitDiffReviewComment,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.submit_diff_review_comment(window, cx);
    }

    /// Stores the diff review comment locally.
    /// Comments are stored per-hunk and can later be batch-submitted to the Agent panel.
    pub fn submit_diff_review_comment(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        // Find the overlay that currently has focus
        let overlay_index = self
            .diff_review_overlays
            .iter()
            .position(|overlay| overlay.prompt_editor.focus_handle(cx).is_focused(window));
        let Some(overlay_index) = overlay_index else {
            return;
        };
        let overlay = &self.diff_review_overlays[overlay_index];

        let comment_text = overlay.prompt_editor.read(cx).text(cx).trim().to_string();
        if comment_text.is_empty() {
            return;
        }

        let anchor_range = overlay.anchor_range.clone();
        let hunk_key = overlay.hunk_key.clone();

        self.add_review_comment(hunk_key.clone(), comment_text, anchor_range, cx);

        // Clear the prompt editor but keep the overlay open
        if let Some(overlay) = self.diff_review_overlays.get(overlay_index) {
            overlay.prompt_editor.update(cx, |editor, cx| {
                editor.clear(window, cx);
            });
        }

        // Refresh the overlay to update the block height for the new comment
        self.refresh_diff_review_overlay_height(&hunk_key, window, cx);

        cx.notify();
    }

    /// Returns the prompt editor for the diff review overlay, if one is active.
    /// This is primarily used for testing.
    pub fn diff_review_prompt_editor(&self) -> Option<&Entity<Editor>> {
        self.diff_review_overlays
            .first()
            .map(|overlay| &overlay.prompt_editor)
    }

    /// Returns the line range for the first diff review overlay, if one is active.
    /// Returns (start_row, end_row) as physical line numbers in the underlying file.
    pub fn diff_review_line_range(&self, cx: &App) -> Option<(u32, u32)> {
        let overlay = self.diff_review_overlays.first()?;
        let snapshot = self.buffer.read(cx).snapshot(cx);
        let start_point = overlay.anchor_range.start.to_point(&snapshot);
        let end_point = overlay.anchor_range.end.to_point(&snapshot);
        let start_row = snapshot
            .point_to_buffer_point(start_point)
            .map(|(_, p)| p.row)
            .unwrap_or(start_point.row);
        let end_row = snapshot
            .point_to_buffer_point(end_point)
            .map(|(_, p)| p.row)
            .unwrap_or(end_point.row);
        Some((start_row, end_row))
    }

    /// Sets whether the comments section is expanded in the diff review overlay.
    /// This is primarily used for testing.
    pub fn set_diff_review_comments_expanded(&mut self, expanded: bool, cx: &mut Context<Self>) {
        for overlay in &mut self.diff_review_overlays {
            overlay.comments_expanded = expanded;
        }
        cx.notify();
    }

    /// Compares two DiffHunkKeys for equality by resolving their anchors.
    fn hunk_keys_match(a: &DiffHunkKey, b: &DiffHunkKey, snapshot: &MultiBufferSnapshot) -> bool {
        a.file_path == b.file_path
            && a.hunk_start_anchor.to_point(snapshot) == b.hunk_start_anchor.to_point(snapshot)
    }

    /// Returns comments for a specific hunk, ordered by creation time.
    pub fn comments_for_hunk<'a>(
        &'a self,
        key: &DiffHunkKey,
        snapshot: &MultiBufferSnapshot,
    ) -> &'a [StoredReviewComment] {
        let key_point = key.hunk_start_anchor.to_point(snapshot);
        self.stored_review_comments
            .iter()
            .find(|(k, _)| {
                k.file_path == key.file_path && k.hunk_start_anchor.to_point(snapshot) == key_point
            })
            .map(|(_, comments)| comments.as_slice())
            .unwrap_or(&[])
    }

    /// Returns the total count of stored review comments across all hunks.
    pub fn total_review_comment_count(&self) -> usize {
        self.stored_review_comments
            .iter()
            .map(|(_, v)| v.len())
            .sum()
    }

    /// Returns the count of comments for a specific hunk.
    pub fn hunk_comment_count(&self, key: &DiffHunkKey, snapshot: &MultiBufferSnapshot) -> usize {
        let key_point = key.hunk_start_anchor.to_point(snapshot);
        self.stored_review_comments
            .iter()
            .find(|(k, _)| {
                k.file_path == key.file_path && k.hunk_start_anchor.to_point(snapshot) == key_point
            })
            .map(|(_, v)| v.len())
            .unwrap_or(0)
    }

    /// Adds a new review comment to a specific hunk.
    pub fn add_review_comment(
        &mut self,
        hunk_key: DiffHunkKey,
        comment: String,
        anchor_range: Range<Anchor>,
        cx: &mut Context<Self>,
    ) -> usize {
        let id = self.next_review_comment_id;
        self.next_review_comment_id += 1;

        let stored_comment = StoredReviewComment::new(id, comment, anchor_range);

        let snapshot = self.buffer.read(cx).snapshot(cx);
        let key_point = hunk_key.hunk_start_anchor.to_point(&snapshot);

        // Find existing entry for this hunk or add a new one
        if let Some((_, comments)) = self.stored_review_comments.iter_mut().find(|(k, _)| {
            k.file_path == hunk_key.file_path
                && k.hunk_start_anchor.to_point(&snapshot) == key_point
        }) {
            comments.push(stored_comment);
        } else {
            self.stored_review_comments
                .push((hunk_key, vec![stored_comment]));
        }

        cx.emit(EditorEvent::ReviewCommentsChanged {
            total_count: self.total_review_comment_count(),
        });
        cx.notify();
        id
    }

    /// Removes a review comment by ID from any hunk.
    pub fn remove_review_comment(&mut self, id: usize, cx: &mut Context<Self>) -> bool {
        for (_, comments) in self.stored_review_comments.iter_mut() {
            if let Some(index) = comments.iter().position(|c| c.id == id) {
                comments.remove(index);
                cx.emit(EditorEvent::ReviewCommentsChanged {
                    total_count: self.total_review_comment_count(),
                });
                cx.notify();
                return true;
            }
        }
        false
    }

    /// Updates a review comment's text by ID.
    pub fn update_review_comment(
        &mut self,
        id: usize,
        new_comment: String,
        cx: &mut Context<Self>,
    ) -> bool {
        for (_, comments) in self.stored_review_comments.iter_mut() {
            if let Some(comment) = comments.iter_mut().find(|c| c.id == id) {
                comment.comment = new_comment;
                comment.is_editing = false;
                cx.emit(EditorEvent::ReviewCommentsChanged {
                    total_count: self.total_review_comment_count(),
                });
                cx.notify();
                return true;
            }
        }
        false
    }

    /// Sets a comment's editing state.
    pub fn set_comment_editing(&mut self, id: usize, is_editing: bool, cx: &mut Context<Self>) {
        for (_, comments) in self.stored_review_comments.iter_mut() {
            if let Some(comment) = comments.iter_mut().find(|c| c.id == id) {
                comment.is_editing = is_editing;
                cx.notify();
                return;
            }
        }
    }

    /// Takes all stored comments from all hunks, clearing the storage.
    /// Returns a Vec of (hunk_key, comments) pairs.
    pub fn take_all_review_comments(
        &mut self,
        cx: &mut Context<Self>,
    ) -> Vec<(DiffHunkKey, Vec<StoredReviewComment>)> {
        // Dismiss all overlays when taking comments (e.g., when sending to agent)
        self.dismiss_all_diff_review_overlays(cx);
        let comments = std::mem::take(&mut self.stored_review_comments);
        // Reset the ID counter since all comments have been taken
        self.next_review_comment_id = 0;
        cx.emit(EditorEvent::ReviewCommentsChanged { total_count: 0 });
        cx.notify();
        comments
    }

    /// Removes review comments whose anchors are no longer valid or whose
    /// associated diff hunks no longer exist.
    ///
    /// This should be called when the buffer changes to prevent orphaned comments
    /// from accumulating.
    pub fn cleanup_orphaned_review_comments(&mut self, cx: &mut Context<Self>) {
        let snapshot = self.buffer.read(cx).snapshot(cx);
        let original_count = self.total_review_comment_count();

        // Remove comments with invalid hunk anchors
        self.stored_review_comments
            .retain(|(hunk_key, _)| hunk_key.hunk_start_anchor.is_valid(&snapshot));

        // Also clean up individual comments with invalid anchor ranges
        for (_, comments) in &mut self.stored_review_comments {
            comments.retain(|comment| {
                comment.range.start.is_valid(&snapshot) && comment.range.end.is_valid(&snapshot)
            });
        }

        // Remove empty hunk entries
        self.stored_review_comments
            .retain(|(_, comments)| !comments.is_empty());

        let new_count = self.total_review_comment_count();
        if new_count != original_count {
            cx.emit(EditorEvent::ReviewCommentsChanged {
                total_count: new_count,
            });
            cx.notify();
        }
    }

    /// Toggles the expanded state of the comments section in the overlay.
    pub fn toggle_review_comments_expanded(
        &mut self,
        _: &ToggleReviewCommentsExpanded,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        // Find the overlay that currently has focus, or use the first one
        let overlay_info = self.diff_review_overlays.iter_mut().find_map(|overlay| {
            if overlay.prompt_editor.focus_handle(cx).is_focused(window) {
                overlay.comments_expanded = !overlay.comments_expanded;
                Some(overlay.hunk_key.clone())
            } else {
                None
            }
        });

        // If no focused overlay found, toggle the first one
        let hunk_key = overlay_info.or_else(|| {
            self.diff_review_overlays.first_mut().map(|overlay| {
                overlay.comments_expanded = !overlay.comments_expanded;
                overlay.hunk_key.clone()
            })
        });

        if let Some(hunk_key) = hunk_key {
            self.refresh_diff_review_overlay_height(&hunk_key, window, cx);
            cx.notify();
        }
    }

    /// Handles the EditReviewComment action - sets a comment into editing mode.
    pub fn edit_review_comment(
        &mut self,
        action: &EditReviewComment,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let comment_id = action.id;

        // Set the comment to editing mode
        self.set_comment_editing(comment_id, true, cx);

        // Find the overlay that contains this comment and create an inline editor if needed
        // First, find which hunk this comment belongs to
        let hunk_key = self
            .stored_review_comments
            .iter()
            .find_map(|(key, comments)| {
                if comments.iter().any(|c| c.id == comment_id) {
                    Some(key.clone())
                } else {
                    None
                }
            });

        let snapshot = self.buffer.read(cx).snapshot(cx);
        if let Some(hunk_key) = hunk_key {
            if let Some(overlay) = self
                .diff_review_overlays
                .iter_mut()
                .find(|overlay| Self::hunk_keys_match(&overlay.hunk_key, &hunk_key, &snapshot))
            {
                if let std::collections::hash_map::Entry::Vacant(entry) =
                    overlay.inline_edit_editors.entry(comment_id)
                {
                    // Find the comment text
                    let comment_text = self
                        .stored_review_comments
                        .iter()
                        .flat_map(|(_, comments)| comments)
                        .find(|c| c.id == comment_id)
                        .map(|c| c.comment.clone())
                        .unwrap_or_default();

                    // Create inline editor
                    let parent_editor = cx.entity().downgrade();
                    let inline_editor = cx.new(|cx| {
                        let mut editor = Editor::single_line(window, cx);
                        editor.set_text(&*comment_text, window, cx);
                        // Select all text for easy replacement
                        editor.select_all(&crate::actions::SelectAll, window, cx);
                        editor
                    });

                    // Register the Newline action to confirm the edit
                    let subscription = inline_editor.update(cx, |inline_editor, _cx| {
                        inline_editor.register_action({
                            let parent_editor = parent_editor.clone();
                            move |_: &crate::actions::Newline, window, cx| {
                                if let Some(editor) = parent_editor.upgrade() {
                                    editor.update(cx, |editor, cx| {
                                        editor.confirm_edit_review_comment(comment_id, window, cx);
                                    });
                                }
                            }
                        })
                    });

                    // Store the subscription to keep the action handler alive
                    overlay
                        .inline_edit_subscriptions
                        .insert(comment_id, subscription);

                    // Focus the inline editor
                    let focus_handle = inline_editor.focus_handle(cx);
                    window.focus(&focus_handle, cx);

                    entry.insert(inline_editor);
                }
            }
        }

        cx.notify();
    }

    /// Confirms an inline edit of a review comment.
    pub fn confirm_edit_review_comment(
        &mut self,
        comment_id: usize,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        // Get the new text from the inline editor
        // Find the overlay containing this comment's inline editor
        let snapshot = self.buffer.read(cx).snapshot(cx);
        let hunk_key = self
            .stored_review_comments
            .iter()
            .find_map(|(key, comments)| {
                if comments.iter().any(|c| c.id == comment_id) {
                    Some(key.clone())
                } else {
                    None
                }
            });

        let new_text = hunk_key
            .as_ref()
            .and_then(|hunk_key| {
                self.diff_review_overlays
                    .iter()
                    .find(|overlay| Self::hunk_keys_match(&overlay.hunk_key, hunk_key, &snapshot))
            })
            .as_ref()
            .and_then(|overlay| overlay.inline_edit_editors.get(&comment_id))
            .map(|editor| editor.read(cx).text(cx).trim().to_string());

        if let Some(new_text) = new_text {
            if !new_text.is_empty() {
                self.update_review_comment(comment_id, new_text, cx);
            }
        }

        // Remove the inline editor and its subscription
        if let Some(hunk_key) = hunk_key {
            if let Some(overlay) = self
                .diff_review_overlays
                .iter_mut()
                .find(|overlay| Self::hunk_keys_match(&overlay.hunk_key, &hunk_key, &snapshot))
            {
                overlay.inline_edit_editors.remove(&comment_id);
                overlay.inline_edit_subscriptions.remove(&comment_id);
            }
        }

        // Clear editing state
        self.set_comment_editing(comment_id, false, cx);
    }

    /// Cancels an inline edit of a review comment.
    pub fn cancel_edit_review_comment(
        &mut self,
        comment_id: usize,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        // Find which hunk this comment belongs to
        let hunk_key = self
            .stored_review_comments
            .iter()
            .find_map(|(key, comments)| {
                if comments.iter().any(|c| c.id == comment_id) {
                    Some(key.clone())
                } else {
                    None
                }
            });

        // Remove the inline editor and its subscription
        if let Some(hunk_key) = hunk_key {
            let snapshot = self.buffer.read(cx).snapshot(cx);
            if let Some(overlay) = self
                .diff_review_overlays
                .iter_mut()
                .find(|overlay| Self::hunk_keys_match(&overlay.hunk_key, &hunk_key, &snapshot))
            {
                overlay.inline_edit_editors.remove(&comment_id);
                overlay.inline_edit_subscriptions.remove(&comment_id);
            }
        }

        // Clear editing state
        self.set_comment_editing(comment_id, false, cx);
    }

    /// Action handler for ConfirmEditReviewComment.
    pub fn confirm_edit_review_comment_action(
        &mut self,
        action: &ConfirmEditReviewComment,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.confirm_edit_review_comment(action.id, window, cx);
    }

    /// Action handler for CancelEditReviewComment.
    pub fn cancel_edit_review_comment_action(
        &mut self,
        action: &CancelEditReviewComment,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.cancel_edit_review_comment(action.id, window, cx);
    }

    /// Handles the DeleteReviewComment action - removes a comment.
    pub fn delete_review_comment(
        &mut self,
        action: &DeleteReviewComment,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        // Get the hunk key before removing the comment
        // Find the hunk key from the comment itself
        let comment_id = action.id;
        let hunk_key = self
            .stored_review_comments
            .iter()
            .find_map(|(key, comments)| {
                if comments.iter().any(|c| c.id == comment_id) {
                    Some(key.clone())
                } else {
                    None
                }
            });

        // Also get it from the overlay for refresh purposes
        let overlay_hunk_key = self
            .diff_review_overlays
            .first()
            .map(|o| o.hunk_key.clone());

        self.remove_review_comment(action.id, cx);

        // Refresh the overlay height after removing a comment
        if let Some(hunk_key) = hunk_key.or(overlay_hunk_key) {
            self.refresh_diff_review_overlay_height(&hunk_key, window, cx);
        }
    }

    fn render_diff_review_overlay(
        prompt_editor: &Entity<Editor>,
        hunk_key: &DiffHunkKey,
        editor_handle: &WeakEntity<Editor>,
        cx: &mut BlockContext,
    ) -> AnyElement {
        fn format_line_ranges(ranges: &[(u32, u32)]) -> Option<String> {
            if ranges.is_empty() {
                return None;
            }
            let formatted: Vec<String> = ranges
                .iter()
                .map(|(start, end)| {
                    let start_line = start + 1;
                    let end_line = end + 1;
                    if start_line == end_line {
                        format!("Line {start_line}")
                    } else {
                        format!("Lines {start_line}-{end_line}")
                    }
                })
                .collect();
            // Don't show label for single line in single excerpt
            if ranges.len() == 1 && ranges[0].0 == ranges[0].1 {
                return None;
            }
            Some(formatted.join(" ⋯ "))
        }

        let theme = cx.theme();
        let colors = theme.colors();

        let (comments, comments_expanded, inline_editors, user_avatar_uri, line_ranges) =
            editor_handle
                .upgrade()
                .map(|editor| {
                    let editor = editor.read(cx);
                    let snapshot = editor.buffer().read(cx).snapshot(cx);
                    let comments = editor.comments_for_hunk(hunk_key, &snapshot).to_vec();
                    let (expanded, editors, avatar_uri, line_ranges) = editor
                        .diff_review_overlays
                        .iter()
                        .find(|overlay| {
                            Editor::hunk_keys_match(&overlay.hunk_key, hunk_key, &snapshot)
                        })
                        .map(|o| {
                            let start_point = o.anchor_range.start.to_point(&snapshot);
                            let end_point = o.anchor_range.end.to_point(&snapshot);
                            // Get line ranges per excerpt to detect discontinuities
                            let buffer_ranges =
                                snapshot.range_to_buffer_ranges(start_point..end_point);
                            let ranges: Vec<(u32, u32)> = buffer_ranges
                                .iter()
                                .map(|(buffer_snapshot, range, _)| {
                                    let start = buffer_snapshot.offset_to_point(range.start.0).row;
                                    let end = buffer_snapshot.offset_to_point(range.end.0).row;
                                    (start, end)
                                })
                                .collect();
                            (
                                o.comments_expanded,
                                o.inline_edit_editors.clone(),
                                o.user_avatar_uri.clone(),
                                if ranges.is_empty() {
                                    None
                                } else {
                                    Some(ranges)
                                },
                            )
                        })
                        .unwrap_or((true, HashMap::default(), None, None));
                    (comments, expanded, editors, avatar_uri, line_ranges)
                })
                .unwrap_or((Vec::new(), true, HashMap::default(), None, None));

        let comment_count = comments.len();
        let avatar_size = px(20.);
        let action_icon_size = IconSize::XSmall;

        v_flex()
            .w_full()
            .bg(colors.editor_background)
            .border_b_1()
            .border_color(colors.border)
            .px_2()
            .pb_2()
            .gap_2()
            // Line range indicator (only shown for multi-line selections or multiple excerpts)
            .when_some(line_ranges, |el, ranges| {
                let label = format_line_ranges(&ranges);
                if let Some(label) = label {
                    el.child(
                        h_flex()
                            .w_full()
                            .px_2()
                            .child(Label::new(label).size(LabelSize::Small).color(Color::Muted)),
                    )
                } else {
                    el
                }
            })
            // Top row: editable input with user's avatar
            .child(
                h_flex()
                    .w_full()
                    .items_center()
                    .gap_2()
                    .px_2()
                    .py_1p5()
                    .rounded_md()
                    .bg(colors.surface_background)
                    .child(
                        div()
                            .size(avatar_size)
                            .flex_shrink_0()
                            .rounded_full()
                            .overflow_hidden()
                            .child(if let Some(ref avatar_uri) = user_avatar_uri {
                                Avatar::new(avatar_uri.clone())
                                    .size(avatar_size)
                                    .into_any_element()
                            } else {
                                Icon::new(IconName::Person)
                                    .size(IconSize::Small)
                                    .color(ui::Color::Muted)
                                    .into_any_element()
                            }),
                    )
                    .child(
                        div()
                            .flex_1()
                            .border_1()
                            .border_color(colors.border)
                            .rounded_md()
                            .bg(colors.editor_background)
                            .px_2()
                            .py_1()
                            .child(prompt_editor.clone()),
                    )
                    .child(
                        h_flex()
                            .flex_shrink_0()
                            .gap_1()
                            .child(
                                IconButton::new("diff-review-close", IconName::Close)
                                    .icon_color(ui::Color::Muted)
                                    .icon_size(action_icon_size)
                                    .tooltip(Tooltip::text("Close"))
                                    .on_click(|_, window, cx| {
                                        window
                                            .dispatch_action(Box::new(crate::actions::Cancel), cx);
                                    }),
                            )
                            .child(
                                IconButton::new("diff-review-add", IconName::Return)
                                    .icon_color(ui::Color::Muted)
                                    .icon_size(action_icon_size)
                                    .tooltip(Tooltip::text("Add comment"))
                                    .on_click(|_, window, cx| {
                                        window.dispatch_action(
                                            Box::new(crate::actions::SubmitDiffReviewComment),
                                            cx,
                                        );
                                    }),
                            ),
                    ),
            )
            // Expandable comments section (only shown when there are comments)
            .when(comment_count > 0, |el| {
                el.child(Self::render_comments_section(
                    comments,
                    comments_expanded,
                    inline_editors,
                    user_avatar_uri,
                    avatar_size,
                    action_icon_size,
                    colors,
                ))
            })
            .into_any_element()
    }

    fn render_comments_section(
        comments: Vec<StoredReviewComment>,
        expanded: bool,
        inline_editors: HashMap<usize, Entity<Editor>>,
        user_avatar_uri: Option<SharedUri>,
        avatar_size: Pixels,
        action_icon_size: IconSize,
        colors: &theme::ThemeColors,
    ) -> impl IntoElement {
        let comment_count = comments.len();

        v_flex()
            .w_full()
            .gap_1()
            // Header with expand/collapse toggle
            .child(
                h_flex()
                    .id("review-comments-header")
                    .w_full()
                    .items_center()
                    .gap_1()
                    .px_2()
                    .py_1()
                    .cursor_pointer()
                    .rounded_md()
                    .hover(|style| style.bg(colors.ghost_element_hover))
                    .on_click(|_, window: &mut Window, cx| {
                        window.dispatch_action(
                            Box::new(crate::actions::ToggleReviewCommentsExpanded),
                            cx,
                        );
                    })
                    .child(
                        Icon::new(if expanded {
                            IconName::ChevronDown
                        } else {
                            IconName::ChevronRight
                        })
                        .size(IconSize::Small)
                        .color(ui::Color::Muted),
                    )
                    .child(
                        Label::new(format!(
                            "{} Comment{}",
                            comment_count,
                            if comment_count == 1 { "" } else { "s" }
                        ))
                        .size(LabelSize::Small)
                        .color(Color::Muted),
                    ),
            )
            // Comments list (when expanded)
            .when(expanded, |el| {
                el.children(comments.into_iter().map(|comment| {
                    let inline_editor = inline_editors.get(&comment.id).cloned();
                    Self::render_comment_row(
                        comment,
                        inline_editor,
                        user_avatar_uri.clone(),
                        avatar_size,
                        action_icon_size,
                        colors,
                    )
                }))
            })
    }

    fn render_comment_row(
        comment: StoredReviewComment,
        inline_editor: Option<Entity<Editor>>,
        user_avatar_uri: Option<SharedUri>,
        avatar_size: Pixels,
        action_icon_size: IconSize,
        colors: &theme::ThemeColors,
    ) -> impl IntoElement {
        let comment_id = comment.id;
        let is_editing = inline_editor.is_some();

        h_flex()
            .w_full()
            .items_center()
            .gap_2()
            .px_2()
            .py_1p5()
            .rounded_md()
            .bg(colors.surface_background)
            .child(
                div()
                    .size(avatar_size)
                    .flex_shrink_0()
                    .rounded_full()
                    .overflow_hidden()
                    .child(if let Some(ref avatar_uri) = user_avatar_uri {
                        Avatar::new(avatar_uri.clone())
                            .size(avatar_size)
                            .into_any_element()
                    } else {
                        Icon::new(IconName::Person)
                            .size(IconSize::Small)
                            .color(ui::Color::Muted)
                            .into_any_element()
                    }),
            )
            .child(if let Some(editor) = inline_editor {
                // Inline edit mode: show an editable text field
                div()
                    .flex_1()
                    .border_1()
                    .border_color(colors.border)
                    .rounded_md()
                    .bg(colors.editor_background)
                    .px_2()
                    .py_1()
                    .child(editor)
                    .into_any_element()
            } else {
                // Display mode: show the comment text
                div()
                    .flex_1()
                    .text_sm()
                    .text_color(colors.text)
                    .child(comment.comment)
                    .into_any_element()
            })
            .child(if is_editing {
                // Editing mode: show close and confirm buttons
                h_flex()
                    .gap_1()
                    .child(
                        IconButton::new(
                            format!("diff-review-cancel-edit-{comment_id}"),
                            IconName::Close,
                        )
                        .icon_color(ui::Color::Muted)
                        .icon_size(action_icon_size)
                        .tooltip(Tooltip::text("Cancel"))
                        .on_click(move |_, window, cx| {
                            window.dispatch_action(
                                Box::new(crate::actions::CancelEditReviewComment {
                                    id: comment_id,
                                }),
                                cx,
                            );
                        }),
                    )
                    .child(
                        IconButton::new(
                            format!("diff-review-confirm-edit-{comment_id}"),
                            IconName::Return,
                        )
                        .icon_color(ui::Color::Muted)
                        .icon_size(action_icon_size)
                        .tooltip(Tooltip::text("Confirm"))
                        .on_click(move |_, window, cx| {
                            window.dispatch_action(
                                Box::new(crate::actions::ConfirmEditReviewComment {
                                    id: comment_id,
                                }),
                                cx,
                            );
                        }),
                    )
                    .into_any_element()
            } else {
                // Display mode: no action buttons for now (edit/delete not yet implemented)
                gpui::Empty.into_any_element()
            })
    }

    pub fn set_masked(&mut self, masked: bool, cx: &mut Context<Self>) {
        if self.display_map.read(cx).masked != masked {
            self.display_map.update(cx, |map, _| map.masked = masked);
        }
        cx.notify()
    }

    pub fn set_show_wrap_guides(&mut self, show_wrap_guides: bool, cx: &mut Context<Self>) {
        self.show_wrap_guides = Some(show_wrap_guides);
        cx.notify();
    }

    pub fn set_show_indent_guides(&mut self, show_indent_guides: bool, cx: &mut Context<Self>) {
        self.show_indent_guides = Some(show_indent_guides);
        cx.notify();
    }

    pub fn working_directory(&self, cx: &App) -> Option<PathBuf> {
        if let Some(buffer) = self.buffer().read(cx).as_singleton() {
            if let Some(file) = buffer.read(cx).file().and_then(|f| f.as_local())
                && let Some(dir) = file.abs_path(cx).parent()
            {
                return Some(dir.to_owned());
            }
        }

        None
    }

    fn target_file<'a>(&self, cx: &'a App) -> Option<&'a dyn language::LocalFile> {
        self.active_buffer(cx)?
            .read(cx)
            .file()
            .and_then(|f| f.as_local())
    }

    pub fn target_file_abs_path(&self, cx: &mut Context<Self>) -> Option<PathBuf> {
        self.active_buffer(cx).and_then(|buffer| {
            let buffer = buffer.read(cx);
            if let Some(project_path) = buffer.project_path(cx) {
                let project = self.project()?.read(cx);
                project.absolute_path(&project_path, cx)
            } else {
                buffer
                    .file()
                    .and_then(|file| file.as_local().map(|file| file.abs_path(cx)))
            }
        })
    }

    pub fn reveal_in_finder(
        &mut self,
        _: &RevealInFileManager,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(path) = self.target_file_abs_path(cx) {
            if let Some(project) = self.project() {
                project.update(cx, |project, cx| project.reveal_path(&path, cx));
            } else {
                cx.reveal_path(&path);
            }
        }
    }

    pub fn copy_path(
        &mut self,
        _: &zed_actions::workspace::CopyPath,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(path) = self.target_file_abs_path(cx)
            && let Some(path) = path.to_str()
        {
            cx.write_to_clipboard(ClipboardItem::new_string(path.to_string()));
        } else {
            cx.propagate();
        }
    }

    pub fn copy_relative_path(
        &mut self,
        _: &zed_actions::workspace::CopyRelativePath,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(path) = self.active_buffer(cx).and_then(|buffer| {
            let project = self.project()?.read(cx);
            let path = buffer.read(cx).file()?.path();
            let path = path.display(project.path_style(cx));
            Some(path)
        }) {
            cx.write_to_clipboard(ClipboardItem::new_string(path.to_string()));
        } else {
            cx.propagate();
        }
    }

    /// Returns the project path for the editor's buffer, if any buffer is
    /// opened in the editor.
    pub fn project_path(&self, cx: &App) -> Option<ProjectPath> {
        if let Some(buffer) = self.buffer.read(cx).as_singleton() {
            buffer.read(cx).project_path(cx)
        } else {
            None
        }
    }

    // Returns true if the editor handled a go-to-line request
    pub fn go_to_active_debug_line(&mut self, window: &mut Window, cx: &mut Context<Self>) -> bool {
        maybe!({
            let breakpoint_store = self.breakpoint_store.as_ref()?;

            let (active_stack_frame, debug_line_pane_id) = {
                let store = breakpoint_store.read(cx);
                let active_stack_frame = store.active_position().cloned();
                let debug_line_pane_id = store.active_debug_line_pane_id();
                (active_stack_frame, debug_line_pane_id)
            };

            let Some(active_stack_frame) = active_stack_frame else {
                self.clear_row_highlights::<ActiveDebugLine>();
                return None;
            };

            if let Some(debug_line_pane_id) = debug_line_pane_id {
                if let Some(workspace) = self
                    .workspace
                    .as_ref()
                    .and_then(|(workspace, _)| workspace.upgrade())
                {
                    let editor_pane_id = workspace
                        .read(cx)
                        .pane_for_item_id(cx.entity_id())
                        .map(|pane| pane.entity_id());

                    if editor_pane_id.is_some_and(|id| id != debug_line_pane_id) {
                        self.clear_row_highlights::<ActiveDebugLine>();
                        return None;
                    }
                }
            }

            let position = active_stack_frame.position;

            let snapshot = self.buffer.read(cx).snapshot(cx);
            let multibuffer_anchor = snapshot.anchor_in_excerpt(position)?;

            self.clear_row_highlights::<ActiveDebugLine>();

            self.go_to_line::<ActiveDebugLine>(
                multibuffer_anchor,
                Some(cx.theme().colors().editor_debugger_active_line_background),
                window,
                cx,
            );

            cx.notify();

            Some(())
        })
        .is_some()
    }

    pub fn copy_file_name_without_extension(
        &mut self,
        _: &CopyFileNameWithoutExtension,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(file_stem) = self.active_buffer(cx).and_then(|buffer| {
            let file = buffer.read(cx).file()?;
            file.path().file_stem()
        }) {
            cx.write_to_clipboard(ClipboardItem::new_string(file_stem.to_string()));
        }
    }

    pub fn copy_file_name(&mut self, _: &CopyFileName, _: &mut Window, cx: &mut Context<Self>) {
        if let Some(file_name) = self.active_buffer(cx).and_then(|buffer| {
            let file = buffer.read(cx).file()?;
            Some(file.file_name(cx))
        }) {
            cx.write_to_clipboard(ClipboardItem::new_string(file_name.to_string()));
        }
    }

    pub fn toggle_git_blame(
        &mut self,
        _: &::git::Blame,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.show_git_blame_gutter = !self.show_git_blame_gutter;

        if self.show_git_blame_gutter && !self.has_blame_entries(cx) {
            self.start_git_blame(true, window, cx);
        }

        cx.notify();
    }

    pub fn toggle_git_blame_inline(
        &mut self,
        _: &ToggleGitBlameInline,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.toggle_git_blame_inline_internal(true, window, cx);
        cx.notify();
    }

    pub fn open_git_blame_commit(
        &mut self,
        _: &OpenGitBlameCommit,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.open_git_blame_commit_internal(window, cx);
    }

    fn open_git_blame_commit_internal(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<()> {
        let blame = self.blame.as_ref()?;
        let snapshot = self.snapshot(window, cx);
        let cursor = self
            .selections
            .newest::<Point>(&snapshot.display_snapshot)
            .head();
        let (buffer, point) = snapshot.buffer_snapshot().point_to_buffer_point(cursor)?;
        let (_, blame_entry) = blame
            .update(cx, |blame, cx| {
                blame
                    .blame_for_rows(
                        &[RowInfo {
                            buffer_id: Some(buffer.remote_id()),
                            buffer_row: Some(point.row),
                            ..Default::default()
                        }],
                        cx,
                    )
                    .next()
            })
            .flatten()?;
        let renderer = cx.global::<GlobalBlameRenderer>().0.clone();
        let repo = blame.read(cx).repository(cx, buffer.remote_id())?;
        let workspace = self.workspace()?.downgrade();
        renderer.open_blame_commit(blame_entry, repo, workspace, window, cx);
        None
    }

    pub fn git_blame_inline_enabled(&self) -> bool {
        self.git_blame_inline_enabled
    }

    pub fn toggle_selection_menu(
        &mut self,
        _: &ToggleSelectionMenu,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.show_selection_menu = self
            .show_selection_menu
            .map(|show_selections_menu| !show_selections_menu)
            .or_else(|| Some(!EditorSettings::get_global(cx).toolbar.selections_menu));

        cx.notify();
    }

    pub fn selection_menu_enabled(&self, cx: &App) -> bool {
        self.show_selection_menu
            .unwrap_or_else(|| EditorSettings::get_global(cx).toolbar.selections_menu)
    }

    fn start_git_blame(
        &mut self,
        user_triggered: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if let Some(project) = self.project() {
            if let Some(buffer) = self.buffer().read(cx).as_singleton()
                && buffer.read(cx).file().is_none()
            {
                return;
            }

            let focused = self.focus_handle(cx).contains_focused(window, cx);

            let project = project.clone();
            let blame = cx
                .new(|cx| GitBlame::new(self.buffer.clone(), project, user_triggered, focused, cx));
            self.blame_subscription =
                Some(cx.observe_in(&blame, window, |_, _, _, cx| cx.notify()));
            self.blame = Some(blame);
        }
    }

    fn toggle_git_blame_inline_internal(
        &mut self,
        user_triggered: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.git_blame_inline_enabled {
            self.git_blame_inline_enabled = false;
            self.show_git_blame_inline = false;
            self.show_git_blame_inline_delay_task.take();
        } else {
            self.git_blame_inline_enabled = true;
            self.start_git_blame_inline(user_triggered, window, cx);
        }

        cx.notify();
    }

    fn start_git_blame_inline(
        &mut self,
        user_triggered: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.start_git_blame(user_triggered, window, cx);

        if ProjectSettings::get_global(cx)
            .git
            .inline_blame_delay()
            .is_some()
        {
            self.start_inline_blame_timer(window, cx);
        } else {
            self.show_git_blame_inline = true
        }
    }

    pub fn blame(&self) -> Option<&Entity<GitBlame>> {
        self.blame.as_ref()
    }

    pub fn show_git_blame_gutter(&self) -> bool {
        self.show_git_blame_gutter
    }

    pub fn render_git_blame_gutter(&self, cx: &App) -> bool {
        !self.mode().is_minimap() && self.show_git_blame_gutter && self.has_blame_entries(cx)
    }

    pub fn render_git_blame_inline(&self, window: &Window, cx: &App) -> bool {
        self.show_git_blame_inline
            && (self.focus_handle.is_focused(window) || self.inline_blame_popover.is_some())
            && !self.newest_selection_head_on_empty_line(cx)
            && self.has_blame_entries(cx)
    }

    fn has_blame_entries(&self, cx: &App) -> bool {
        self.blame()
            .is_some_and(|blame| blame.read(cx).has_generated_entries())
    }

    fn newest_selection_head_on_empty_line(&self, cx: &App) -> bool {
        let cursor_anchor = self.selections.newest_anchor().head();

        let snapshot = self.buffer.read(cx).snapshot(cx);
        let buffer_row = MultiBufferRow(cursor_anchor.to_point(&snapshot).row);

        snapshot.line_len(buffer_row) == 0
    }

    fn get_permalink_to_line(&self, cx: &mut Context<Self>) -> Task<Result<url::Url>> {
        let buffer_and_selection = maybe!({
            let selection = self.selections.newest::<Point>(&self.display_snapshot(cx));
            let selection_range = selection.range();

            let multi_buffer = self.buffer().read(cx);
            let multi_buffer_snapshot = multi_buffer.snapshot(cx);
            let buffer_ranges = multi_buffer_snapshot
                .range_to_buffer_ranges(selection_range.start..selection_range.end);

            let (buffer_snapshot, range, _) = if selection.reversed {
                buffer_ranges.first()
            } else {
                buffer_ranges.last()
            }?;

            let buffer_range = range.to_point(buffer_snapshot);
            let buffer = multi_buffer.buffer(buffer_snapshot.remote_id()).unwrap();

            let Some(buffer_diff) = multi_buffer.diff_for(buffer_snapshot.remote_id()) else {
                return Some((buffer, buffer_range.start.row..buffer_range.end.row));
            };

            let buffer_diff_snapshot = buffer_diff.read(cx).snapshot(cx);
            let start = buffer_diff_snapshot
                .buffer_point_to_base_text_point(buffer_range.start, &buffer_snapshot);
            let end = buffer_diff_snapshot
                .buffer_point_to_base_text_point(buffer_range.end, &buffer_snapshot);

            Some((buffer, start.row..end.row))
        });

        let Some((buffer, selection)) = buffer_and_selection else {
            return Task::ready(Err(anyhow!("failed to determine buffer and selection")));
        };

        let Some(project) = self.project() else {
            return Task::ready(Err(anyhow!("editor does not have project")));
        };

        project.update(cx, |project, cx| {
            project.get_permalink_to_line(&buffer, selection, cx)
        })
    }

    pub fn copy_permalink_to_line(
        &mut self,
        _: &CopyPermalinkToLine,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let permalink_task = self.get_permalink_to_line(cx);
        let workspace = self.workspace();

        cx.spawn_in(window, async move |_, cx| match permalink_task.await {
            Ok(permalink) => {
                cx.update(|_, cx| {
                    cx.write_to_clipboard(ClipboardItem::new_string(permalink.to_string()));
                })
                .ok();
            }
            Err(err) => {
                let message = format!("Failed to copy permalink: {err}");

                anyhow::Result::<()>::Err(err).log_err();

                if let Some(workspace) = workspace {
                    workspace
                        .update_in(cx, |workspace, _, cx| {
                            struct CopyPermalinkToLine;

                            workspace.show_toast(
                                Toast::new(
                                    NotificationId::unique::<CopyPermalinkToLine>(),
                                    message,
                                ),
                                cx,
                            )
                        })
                        .ok();
                }
            }
        })
        .detach();
    }

    pub fn copy_file_location(
        &mut self,
        _: &CopyFileLocation,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let selection = self.selections.newest::<Point>(&self.display_snapshot(cx));

        let start_line = selection.start.row + 1;
        let end_line = selection.end.row + 1;

        let end_line = if selection.end.column == 0 && end_line > start_line {
            end_line - 1
        } else {
            end_line
        };

        if let Some(file_location) = self.active_buffer(cx).and_then(|buffer| {
            let project = self.project()?.read(cx);
            let file = buffer.read(cx).file()?;
            let path = file.path().display(project.path_style(cx));

            let location = if start_line == end_line {
                format!("{path}:{start_line}")
            } else {
                format!("{path}:{start_line}-{end_line}")
            };
            Some(location)
        }) {
            cx.write_to_clipboard(ClipboardItem::new_string(file_location));
        }
    }

    pub fn open_permalink_to_line(
        &mut self,
        _: &OpenPermalinkToLine,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let permalink_task = self.get_permalink_to_line(cx);
        let workspace = self.workspace();

        cx.spawn_in(window, async move |_, cx| match permalink_task.await {
            Ok(permalink) => {
                cx.update(|_, cx| {
                    cx.open_url(permalink.as_ref());
                })
                .ok();
            }
            Err(err) => {
                let message = format!("Failed to open permalink: {err}");

                anyhow::Result::<()>::Err(err).log_err();

                if let Some(workspace) = workspace {
                    workspace.update(cx, |workspace, cx| {
                        struct OpenPermalinkToLine;

                        workspace.show_toast(
                            Toast::new(NotificationId::unique::<OpenPermalinkToLine>(), message),
                            cx,
                        )
                    });
                }
            }
        })
        .detach();
    }

    pub fn insert_uuid_v4(
        &mut self,
        _: &InsertUuidV4,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.insert_uuid(UuidVersion::V4, window, cx);
    }

    pub fn insert_uuid_v7(
        &mut self,
        _: &InsertUuidV7,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.insert_uuid(UuidVersion::V7, window, cx);
    }

    fn insert_uuid(&mut self, version: UuidVersion, window: &mut Window, cx: &mut Context<Self>) {
        self.hide_mouse_cursor(HideMouseCursorOrigin::TypingAction, cx);
        self.transact(window, cx, |this, window, cx| {
            let edits = this
                .selections
                .all::<Point>(&this.display_snapshot(cx))
                .into_iter()
                .map(|selection| {
                    let uuid = match version {
                        UuidVersion::V4 => uuid::Uuid::new_v4(),
                        UuidVersion::V7 => uuid::Uuid::now_v7(),
                    };

                    (selection.range(), uuid.to_string())
                });
            this.edit(edits, cx);
            this.refresh_edit_prediction(true, false, window, cx);
        });
    }

    pub fn open_selections_in_multibuffer(
        &mut self,
        _: &OpenSelectionsInMultibuffer,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let multibuffer = self.buffer.read(cx);

        let Some(buffer) = multibuffer.as_singleton() else {
            return;
        };
        let buffer_snapshot = buffer.read(cx).snapshot();

        let Some(workspace) = self.workspace() else {
            return;
        };

        let title = multibuffer.title(cx).to_string();

        let locations = self
            .selections
            .all_anchors(&self.display_snapshot(cx))
            .iter()
            .map(|selection| {
                (
                    buffer.clone(),
                    (selection.start.text_anchor_in(&buffer_snapshot)
                        ..selection.end.text_anchor_in(&buffer_snapshot))
                        .to_point(buffer.read(cx)),
                )
            })
            .into_group_map();

        cx.spawn_in(window, async move |_, cx| {
            workspace.update_in(cx, |workspace, window, cx| {
                Self::open_locations_in_multibuffer(
                    workspace,
                    locations,
                    format!("Selections for '{title}'"),
                    false,
                    false,
                    MultibufferSelectionMode::All,
                    window,
                    cx,
                );
            })
        })
        .detach();
    }

    /// Adds a row highlight for the given range. If a row has multiple highlights, the
    /// last highlight added will be used.
    ///
    /// If the range ends at the beginning of a line, then that line will not be highlighted.
    pub fn highlight_rows<T: 'static>(
        &mut self,
        range: Range<Anchor>,
        color: Hsla,
        options: RowHighlightOptions,
        cx: &mut Context<Self>,
    ) {
        let snapshot = self.buffer().read(cx).snapshot(cx);
        let row_highlights = self.highlighted_rows.entry(TypeId::of::<T>()).or_default();
        let ix = row_highlights.binary_search_by(|highlight| {
            Ordering::Equal
                .then_with(|| highlight.range.start.cmp(&range.start, &snapshot))
                .then_with(|| highlight.range.end.cmp(&range.end, &snapshot))
        });

        if let Err(mut ix) = ix {
            let index = post_inc(&mut self.highlight_order);

            // If this range intersects with the preceding highlight, then merge it with
            // the preceding highlight. Otherwise insert a new highlight.
            let mut merged = false;
            if ix > 0 {
                let prev_highlight = &mut row_highlights[ix - 1];
                if prev_highlight
                    .range
                    .end
                    .cmp(&range.start, &snapshot)
                    .is_ge()
                {
                    ix -= 1;
                    if prev_highlight.range.end.cmp(&range.end, &snapshot).is_lt() {
                        prev_highlight.range.end = range.end;
                    }
                    merged = true;
                    prev_highlight.index = index;
                    prev_highlight.color = color;
                    prev_highlight.options = options;
                }
            }

            if !merged {
                row_highlights.insert(
                    ix,
                    RowHighlight {
                        range,
                        index,
                        color,
                        options,
                        type_id: TypeId::of::<T>(),
                    },
                );
            }

            // If any of the following highlights intersect with this one, merge them.
            while let Some(next_highlight) = row_highlights.get(ix + 1) {
                let highlight = &row_highlights[ix];
                if next_highlight
                    .range
                    .start
                    .cmp(&highlight.range.end, &snapshot)
                    .is_le()
                {
                    if next_highlight
                        .range
                        .end
                        .cmp(&highlight.range.end, &snapshot)
                        .is_gt()
                    {
                        row_highlights[ix].range.end = next_highlight.range.end;
                    }
                    row_highlights.remove(ix + 1);
                } else {
                    break;
                }
            }
        }
    }

    /// Remove any highlighted row ranges of the given type that intersect the
    /// given ranges.
    pub fn remove_highlighted_rows<T: 'static>(
        &mut self,
        ranges_to_remove: Vec<Range<Anchor>>,
        cx: &mut Context<Self>,
    ) {
        let snapshot = self.buffer().read(cx).snapshot(cx);
        let row_highlights = self.highlighted_rows.entry(TypeId::of::<T>()).or_default();
        let mut ranges_to_remove = ranges_to_remove.iter().peekable();
        row_highlights.retain(|highlight| {
            while let Some(range_to_remove) = ranges_to_remove.peek() {
                match range_to_remove.end.cmp(&highlight.range.start, &snapshot) {
                    Ordering::Less | Ordering::Equal => {
                        ranges_to_remove.next();
                    }
                    Ordering::Greater => {
                        match range_to_remove.start.cmp(&highlight.range.end, &snapshot) {
                            Ordering::Less | Ordering::Equal => {
                                return false;
                            }
                            Ordering::Greater => break,
                        }
                    }
                }
            }

            true
        })
    }

    /// Clear all anchor ranges for a certain highlight context type, so no corresponding rows will be highlighted.
    pub fn clear_row_highlights<T: 'static>(&mut self) {
        self.highlighted_rows.remove(&TypeId::of::<T>());
    }

    /// For a highlight given context type, gets all anchor ranges that will be used for row highlighting.
    pub fn highlighted_rows<T: 'static>(&self) -> impl '_ + Iterator<Item = (Range<Anchor>, Hsla)> {
        self.highlighted_rows
            .get(&TypeId::of::<T>())
            .map_or(&[] as &[_], |vec| vec.as_slice())
            .iter()
            .map(|highlight| (highlight.range.clone(), highlight.color))
    }

    /// Merges all anchor ranges for all context types ever set, picking the last highlight added in case of a row conflict.
    /// Returns a map of display rows that are highlighted and their corresponding highlight color.
    /// Allows to ignore certain kinds of highlights.
    pub fn highlighted_display_rows(
        &self,
        window: &mut Window,
        cx: &mut App,
    ) -> BTreeMap<DisplayRow, LineHighlight> {
        let snapshot = self.snapshot(window, cx);
        let mut used_highlight_orders = HashMap::default();
        self.highlighted_rows
            .iter()
            .flat_map(|(_, highlighted_rows)| highlighted_rows.iter())
            .fold(
                BTreeMap::<DisplayRow, LineHighlight>::new(),
                |mut unique_rows, highlight| {
                    let start = highlight.range.start.to_display_point(&snapshot);
                    let end = highlight.range.end.to_display_point(&snapshot);
                    let start_row = start.row().0;
                    let end_row = if !highlight.range.end.is_max() && end.column() == 0 {
                        end.row().0.saturating_sub(1)
                    } else {
                        end.row().0
                    };
                    for row in start_row..=end_row {
                        let used_index =
                            used_highlight_orders.entry(row).or_insert(highlight.index);
                        if highlight.index >= *used_index {
                            *used_index = highlight.index;
                            unique_rows.insert(
                                DisplayRow(row),
                                LineHighlight {
                                    include_gutter: highlight.options.include_gutter,
                                    border: None,
                                    background: highlight.color.into(),
                                    type_id: Some(highlight.type_id),
                                },
                            );
                        }
                    }
                    unique_rows
                },
            )
    }

    pub fn highlighted_display_row_for_autoscroll(
        &self,
        snapshot: &DisplaySnapshot,
    ) -> Option<DisplayRow> {
        self.highlighted_rows
            .values()
            .flat_map(|highlighted_rows| highlighted_rows.iter())
            .filter_map(|highlight| {
                if highlight.options.autoscroll {
                    Some(highlight.range.start.to_display_point(snapshot).row())
                } else {
                    None
                }
            })
            .min()
    }

    pub fn set_search_within_ranges(&mut self, ranges: &[Range<Anchor>], cx: &mut Context<Self>) {
        self.highlight_background(
            HighlightKey::SearchWithinRange,
            ranges,
            |_, colors| colors.colors().editor_document_highlight_read_background,
            cx,
        )
    }

    pub fn set_breadcrumb_header(&mut self, new_header: String) {
        self.breadcrumb_header = Some(new_header);
    }

    pub fn clear_search_within_ranges(&mut self, cx: &mut Context<Self>) {
        self.clear_background_highlights(HighlightKey::SearchWithinRange, cx);
    }

    pub fn highlight_background(
        &mut self,
        key: HighlightKey,
        ranges: &[Range<Anchor>],
        color_fetcher: impl Fn(&usize, &Theme) -> Hsla + Send + Sync + 'static,
        cx: &mut Context<Self>,
    ) {
        self.background_highlights
            .insert(key, (Arc::new(color_fetcher), Arc::from(ranges)));
        self.scrollbar_marker_state.dirty = true;
        cx.notify();
    }

    pub fn clear_background_highlights(
        &mut self,
        key: HighlightKey,
        cx: &mut Context<Self>,
    ) -> Option<BackgroundHighlight> {
        let text_highlights = self.background_highlights.remove(&key)?;
        if !text_highlights.1.is_empty() {
            self.scrollbar_marker_state.dirty = true;
            cx.notify();
        }
        Some(text_highlights)
    }

    pub fn highlight_gutter<T: 'static>(
        &mut self,
        ranges: impl Into<Vec<Range<Anchor>>>,
        color_fetcher: fn(&App) -> Hsla,
        cx: &mut Context<Self>,
    ) {
        self.gutter_highlights
            .insert(TypeId::of::<T>(), (color_fetcher, ranges.into()));
        cx.notify();
    }

    pub fn clear_gutter_highlights<T: 'static>(
        &mut self,
        cx: &mut Context<Self>,
    ) -> Option<GutterHighlight> {
        cx.notify();
        self.gutter_highlights.remove(&TypeId::of::<T>())
    }

    pub fn insert_gutter_highlight<T: 'static>(
        &mut self,
        range: Range<Anchor>,
        color_fetcher: fn(&App) -> Hsla,
        cx: &mut Context<Self>,
    ) {
        let snapshot = self.buffer().read(cx).snapshot(cx);
        let mut highlights = self
            .gutter_highlights
            .remove(&TypeId::of::<T>())
            .map(|(_, highlights)| highlights)
            .unwrap_or_default();
        let ix = highlights.binary_search_by(|highlight| {
            Ordering::Equal
                .then_with(|| highlight.start.cmp(&range.start, &snapshot))
                .then_with(|| highlight.end.cmp(&range.end, &snapshot))
        });
        if let Err(ix) = ix {
            highlights.insert(ix, range);
        }
        self.gutter_highlights
            .insert(TypeId::of::<T>(), (color_fetcher, highlights));
    }

    pub fn remove_gutter_highlights<T: 'static>(
        &mut self,
        ranges_to_remove: Vec<Range<Anchor>>,
        cx: &mut Context<Self>,
    ) {
        let snapshot = self.buffer().read(cx).snapshot(cx);
        let Some((color_fetcher, mut gutter_highlights)) =
            self.gutter_highlights.remove(&TypeId::of::<T>())
        else {
            return;
        };
        let mut ranges_to_remove = ranges_to_remove.iter().peekable();
        gutter_highlights.retain(|highlight| {
            while let Some(range_to_remove) = ranges_to_remove.peek() {
                match range_to_remove.end.cmp(&highlight.start, &snapshot) {
                    Ordering::Less | Ordering::Equal => {
                        ranges_to_remove.next();
                    }
                    Ordering::Greater => {
                        match range_to_remove.start.cmp(&highlight.end, &snapshot) {
                            Ordering::Less | Ordering::Equal => {
                                return false;
                            }
                            Ordering::Greater => break,
                        }
                    }
                }
            }

            true
        });
        self.gutter_highlights
            .insert(TypeId::of::<T>(), (color_fetcher, gutter_highlights));
    }

    #[cfg(any(test, feature = "test-support"))]
    pub fn all_text_highlights(
        &self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Vec<(HighlightStyle, Vec<Range<DisplayPoint>>)> {
        let snapshot = self.snapshot(window, cx);
        self.display_map.update(cx, |display_map, _| {
            display_map
                .all_text_highlights()
                .map(|(_, highlight)| {
                    let (style, ranges) = highlight.as_ref();
                    (
                        *style,
                        ranges
                            .iter()
                            .map(|range| range.clone().to_display_points(&snapshot))
                            .collect(),
                    )
                })
                .collect()
        })
    }

    #[cfg(any(test, feature = "test-support"))]
    pub fn all_text_background_highlights(
        &self,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Vec<(Range<DisplayPoint>, Hsla)> {
        let snapshot = self.snapshot(window, cx);
        let buffer = &snapshot.buffer_snapshot();
        let start = buffer.anchor_before(MultiBufferOffset(0));
        let end = buffer.anchor_after(buffer.len());
        self.sorted_background_highlights_in_range(start..end, &snapshot, cx.theme())
    }

    #[cfg(any(test, feature = "test-support"))]
    pub fn sorted_background_highlights_in_range(
        &self,
        search_range: Range<Anchor>,
        display_snapshot: &DisplaySnapshot,
        theme: &Theme,
    ) -> Vec<(Range<DisplayPoint>, Hsla)> {
        let mut res = self.background_highlights_in_range(search_range, display_snapshot, theme);
        res.sort_by(|a, b| {
            a.0.start
                .cmp(&b.0.start)
                .then_with(|| a.0.end.cmp(&b.0.end))
                .then_with(|| a.1.cmp(&b.1))
        });
        res
    }

    #[cfg(any(test, feature = "test-support"))]
    pub fn search_background_highlights(&mut self, cx: &mut Context<Self>) -> Vec<Range<Point>> {
        let snapshot = self.buffer().read(cx).snapshot(cx);

        let highlights = self
            .background_highlights
            .get(&HighlightKey::BufferSearchHighlights);

        if let Some((_color, ranges)) = highlights {
            ranges
                .iter()
                .map(|range| range.start.to_point(&snapshot)..range.end.to_point(&snapshot))
                .collect_vec()
        } else {
            vec![]
        }
    }

    pub fn has_background_highlights(&self, key: HighlightKey) -> bool {
        self.background_highlights
            .get(&key)
            .is_some_and(|(_, highlights)| !highlights.is_empty())
    }

    /// Returns all background highlights for a given range.
    ///
    /// The order of highlights is not deterministic, do sort the ranges if needed for the logic.
    pub fn background_highlights_in_range(
        &self,
        search_range: Range<Anchor>,
        display_snapshot: &DisplaySnapshot,
        theme: &Theme,
    ) -> Vec<(Range<DisplayPoint>, Hsla)> {
        let mut results = Vec::new();
        for (color_fetcher, ranges) in self.background_highlights.values() {
            let start_ix = match ranges.binary_search_by(|probe| {
                let cmp = probe
                    .end
                    .cmp(&search_range.start, &display_snapshot.buffer_snapshot());
                if cmp.is_gt() {
                    Ordering::Greater
                } else {
                    Ordering::Less
                }
            }) {
                Ok(i) | Err(i) => i,
            };
            for (index, range) in ranges[start_ix..].iter().enumerate() {
                if range
                    .start
                    .cmp(&search_range.end, &display_snapshot.buffer_snapshot())
                    .is_ge()
                {
                    break;
                }

                let color = color_fetcher(&(start_ix + index), theme);
                let start = range.start.to_display_point(display_snapshot);
                let end = range.end.to_display_point(display_snapshot);
                results.push((start..end, color))
            }
        }
        results
    }

    pub fn gutter_highlights_in_range(
        &self,
        search_range: Range<Anchor>,
        display_snapshot: &DisplaySnapshot,
        cx: &App,
    ) -> Vec<(Range<DisplayPoint>, Hsla)> {
        let mut results = Vec::new();
        for (color_fetcher, ranges) in self.gutter_highlights.values() {
            let color = color_fetcher(cx);
            let start_ix = match ranges.binary_search_by(|probe| {
                let cmp = probe
                    .end
                    .cmp(&search_range.start, &display_snapshot.buffer_snapshot());
                if cmp.is_gt() {
                    Ordering::Greater
                } else {
                    Ordering::Less
                }
            }) {
                Ok(i) | Err(i) => i,
            };
            for range in &ranges[start_ix..] {
                if range
                    .start
                    .cmp(&search_range.end, &display_snapshot.buffer_snapshot())
                    .is_ge()
                {
                    break;
                }

                let start = range.start.to_display_point(display_snapshot);
                let end = range.end.to_display_point(display_snapshot);
                results.push((start..end, color))
            }
        }
        results
    }

    /// Get the text ranges corresponding to the redaction query
    pub fn redacted_ranges(
        &self,
        search_range: Range<Anchor>,
        display_snapshot: &DisplaySnapshot,
        cx: &App,
    ) -> Vec<Range<DisplayPoint>> {
        display_snapshot
            .buffer_snapshot()
            .redacted_ranges(search_range, |file| {
                if let Some(file) = file {
                    file.is_private()
                        && EditorSettings::get(
                            Some(SettingsLocation {
                                worktree_id: file.worktree_id(cx),
                                path: file.path().as_ref(),
                            }),
                            cx,
                        )
                        .redact_private_values
                } else {
                    false
                }
            })
            .map(|range| {
                range.start.to_display_point(display_snapshot)
                    ..range.end.to_display_point(display_snapshot)
            })
            .collect()
    }

    pub fn highlight_text_key(
        &mut self,
        key: HighlightKey,
        ranges: Vec<Range<Anchor>>,
        style: HighlightStyle,
        merge: bool,
        cx: &mut Context<Self>,
    ) {
        self.display_map.update(cx, |map, cx| {
            map.highlight_text(key, ranges, style, merge, cx);
        });
        cx.notify();
    }

    pub fn highlight_text(
        &mut self,
        key: HighlightKey,
        ranges: Vec<Range<Anchor>>,
        style: HighlightStyle,
        cx: &mut Context<Self>,
    ) {
        self.display_map.update(cx, |map, cx| {
            map.highlight_text(key, ranges, style, false, cx)
        });
        cx.notify();
    }

    pub fn text_highlights<'a>(
        &'a self,
        key: HighlightKey,
        cx: &'a App,
    ) -> Option<(HighlightStyle, &'a [Range<Anchor>])> {
        self.display_map.read(cx).text_highlights(key)
    }

    pub fn clear_highlights(&mut self, key: HighlightKey, cx: &mut Context<Self>) {
        let cleared = self
            .display_map
            .update(cx, |map, _| map.clear_highlights(key));
        if cleared {
            cx.notify();
        }
    }

    pub fn clear_highlights_with(
        &mut self,
        f: &mut dyn FnMut(&HighlightKey) -> bool,
        cx: &mut Context<Self>,
    ) {
        let cleared = self
            .display_map
            .update(cx, |map, _| map.clear_highlights_with(f));
        if cleared {
            cx.notify();
        }
    }

    pub fn show_local_cursors(&self, window: &mut Window, cx: &mut App) -> bool {
        (self.read_only(cx) || self.blink_manager.read(cx).visible())
            && self.focus_handle.is_focused(window)
    }

    pub fn set_show_cursor_when_unfocused(&mut self, is_enabled: bool, cx: &mut Context<Self>) {
        self.show_cursor_when_unfocused = is_enabled;
        cx.notify();
    }

    fn on_buffer_changed(&mut self, _: Entity<MultiBuffer>, cx: &mut Context<Self>) {
        cx.notify();
    }

    fn on_debug_session_event(
        &mut self,
        _session: Entity<Session>,
        event: &SessionEvent,
        cx: &mut Context<Self>,
    ) {
        if let SessionEvent::InvalidateInlineValue = event {
            self.refresh_inline_values(cx);
        }
    }

    pub fn refresh_inline_values(&mut self, cx: &mut Context<Self>) {
        let Some(semantics) = self.semantics_provider.clone() else {
            return;
        };

        if !self.inline_value_cache.enabled {
            let inlays = std::mem::take(&mut self.inline_value_cache.inlays);
            self.splice_inlays(&inlays, Vec::new(), cx);
            return;
        }

        let current_execution_position = self
            .highlighted_rows
            .get(&TypeId::of::<ActiveDebugLine>())
            .and_then(|lines| lines.last().map(|line| line.range.end));

        self.inline_value_cache.refresh_task = cx.spawn(async move |editor, cx| {
            let inline_values = editor
                .update(cx, |editor, cx| {
                    let Some(current_execution_position) = current_execution_position else {
                        return Some(Task::ready(Ok(Vec::new())));
                    };

                    let (buffer, buffer_anchor) =
                        editor.buffer.read_with(cx, |multibuffer, cx| {
                            let multibuffer_snapshot = multibuffer.snapshot(cx);
                            let (buffer_anchor, _) = multibuffer_snapshot
                                .anchor_to_buffer_anchor(current_execution_position)?;
                            let buffer = multibuffer.buffer(buffer_anchor.buffer_id)?;
                            Some((buffer, buffer_anchor))
                        })?;

                    let range = buffer.read(cx).anchor_before(0)..buffer_anchor;

                    semantics.inline_values(buffer, range, cx)
                })
                .ok()
                .flatten()?
                .await
                .context("refreshing debugger inlays")
                .log_err()?;

            let mut buffer_inline_values: HashMap<BufferId, Vec<InlayHint>> = HashMap::default();

            for (buffer_id, inline_value) in inline_values
                .into_iter()
                .map(|hint| (hint.position.buffer_id, hint))
            {
                buffer_inline_values
                    .entry(buffer_id)
                    .or_default()
                    .push(inline_value);
            }

            editor
                .update(cx, |editor, cx| {
                    let snapshot = editor.buffer.read(cx).snapshot(cx);
                    let mut new_inlays = Vec::default();

                    for (_buffer_id, inline_values) in buffer_inline_values {
                        for hint in inline_values {
                            let Some(anchor) = snapshot.anchor_in_excerpt(hint.position) else {
                                continue;
                            };
                            let inlay = Inlay::debugger(
                                post_inc(&mut editor.next_inlay_id),
                                anchor,
                                hint.text(),
                            );
                            if !inlay.text().chars().contains(&'\n') {
                                new_inlays.push(inlay);
                            }
                        }
                    }

                    let mut inlay_ids = new_inlays.iter().map(|inlay| inlay.id).collect();
                    std::mem::swap(&mut editor.inline_value_cache.inlays, &mut inlay_ids);

                    editor.splice_inlays(&inlay_ids, new_inlays, cx);
                })
                .ok()?;
            Some(())
        });
    }

    fn on_buffer_event(
        &mut self,
        multibuffer: &Entity<MultiBuffer>,
        event: &multi_buffer::Event,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        match event {
            multi_buffer::Event::Edited {
                edited_buffer,
                is_local,
            } => {
                self.scrollbar_marker_state.dirty = true;
                self.active_indent_guides_state.dirty = true;
                self.refresh_active_diagnostics(cx);
                self.refresh_code_actions(window, cx);
                self.refresh_single_line_folds(window, cx);
                let snapshot = self.snapshot(window, cx);
                self.refresh_matching_bracket_highlights(&snapshot, cx);
                self.refresh_outline_symbols_at_cursor(cx);
                self.refresh_sticky_headers(&snapshot, cx);
                if *is_local && self.has_active_edit_prediction() {
                    self.update_visible_edit_prediction(window, cx);
                }

                // Clean up orphaned review comments after edits
                self.cleanup_orphaned_review_comments(cx);

                if let Some(buffer) = edited_buffer {
                    if buffer.read(cx).file().is_none() {
                        cx.emit(EditorEvent::TitleChanged);
                    }

                    if self.project.is_some() {
                        let buffer_id = buffer.read(cx).remote_id();
                        self.register_buffer(buffer_id, cx);
                        self.update_lsp_data(Some(buffer_id), window, cx);
                        self.refresh_inlay_hints(
                            InlayHintRefreshReason::BufferEdited(buffer_id),
                            cx,
                        );
                    }
                }

                cx.emit(EditorEvent::BufferEdited);
                cx.emit(SearchEvent::MatchesInvalidated);

                let Some(project) = &self.project else { return };
                let (telemetry, is_via_ssh) = {
                    let project = project.read(cx);
                    let telemetry = project.client().telemetry().clone();
                    let is_via_ssh = project.is_via_remote_server();
                    (telemetry, is_via_ssh)
                };
                telemetry.log_edit_event("editor", is_via_ssh);
            }
            multi_buffer::Event::BufferRangesUpdated {
                buffer,
                ranges,
                path_key,
            } => {
                self.refresh_document_highlights(cx);
                let buffer_id = buffer.read(cx).remote_id();
                if self.buffer.read(cx).diff_for(buffer_id).is_none()
                    && let Some(project) = &self.project
                {
                    update_uncommitted_diff_for_buffer(
                        cx.entity(),
                        project,
                        [buffer.clone()],
                        self.buffer.clone(),
                        cx,
                    )
                    .detach();
                }
                self.register_visible_buffers(cx);
                self.update_lsp_data(Some(buffer_id), window, cx);
                self.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx);
                self.refresh_runnables(None, window, cx);
                self.bracket_fetched_tree_sitter_chunks
                    .retain(|range, _| range.start.buffer_id != buffer_id);
                self.colorize_brackets(false, cx);
                self.refresh_selected_text_highlights(&self.display_snapshot(cx), true, window, cx);
                self.semantic_token_state.invalidate_buffer(&buffer_id);
                cx.emit(EditorEvent::BufferRangesUpdated {
                    buffer: buffer.clone(),
                    ranges: ranges.clone(),
                    path_key: path_key.clone(),
                });
            }
            multi_buffer::Event::BuffersRemoved { removed_buffer_ids } => {
                if let Some(inlay_hints) = &mut self.inlay_hints {
                    inlay_hints.remove_inlay_chunk_data(removed_buffer_ids);
                }
                self.refresh_inlay_hints(
                    InlayHintRefreshReason::BuffersRemoved(removed_buffer_ids.clone()),
                    cx,
                );
                for buffer_id in removed_buffer_ids {
                    self.registered_buffers.remove(buffer_id);
                    self.clear_runnables(Some(*buffer_id));
                    self.semantic_token_state.invalidate_buffer(buffer_id);
                    self.display_map.update(cx, |display_map, cx| {
                        display_map.invalidate_semantic_highlights(*buffer_id);
                        display_map.clear_lsp_folding_ranges(*buffer_id, cx);
                    });
                }

                self.display_map.update(cx, |display_map, cx| {
                    display_map.unfold_buffers(removed_buffer_ids.iter().copied(), cx);
                });

                jsx_tag_auto_close::refresh_enabled_in_any_buffer(self, multibuffer, cx);
                cx.emit(EditorEvent::BuffersRemoved {
                    removed_buffer_ids: removed_buffer_ids.clone(),
                });
            }
            multi_buffer::Event::BuffersEdited { buffer_ids } => {
                self.display_map.update(cx, |map, cx| {
                    map.unfold_buffers(buffer_ids.iter().copied(), cx)
                });
                cx.emit(EditorEvent::BuffersEdited {
                    buffer_ids: buffer_ids.clone(),
                });
            }
            multi_buffer::Event::Reparsed(buffer_id) => {
                self.refresh_runnables(Some(*buffer_id), window, cx);
                self.refresh_selected_text_highlights(&self.display_snapshot(cx), true, window, cx);
                self.colorize_brackets(true, cx);
                jsx_tag_auto_close::refresh_enabled_in_any_buffer(self, multibuffer, cx);

                cx.emit(EditorEvent::Reparsed(*buffer_id));
            }
            multi_buffer::Event::DiffHunksToggled => {
                self.refresh_runnables(None, window, cx);
            }
            multi_buffer::Event::LanguageChanged(buffer_id, is_fresh_language) => {
                if !is_fresh_language {
                    self.registered_buffers.remove(&buffer_id);
                }
                jsx_tag_auto_close::refresh_enabled_in_any_buffer(self, multibuffer, cx);
                cx.emit(EditorEvent::Reparsed(*buffer_id));
                self.update_edit_prediction_settings(cx);
                cx.notify();
            }
            multi_buffer::Event::DirtyChanged => cx.emit(EditorEvent::DirtyChanged),
            multi_buffer::Event::Saved => cx.emit(EditorEvent::Saved),
            multi_buffer::Event::FileHandleChanged
            | multi_buffer::Event::Reloaded
            | multi_buffer::Event::BufferDiffChanged => cx.emit(EditorEvent::TitleChanged),
            multi_buffer::Event::DiagnosticsUpdated => {
                self.update_diagnostics_state(window, cx);
            }
            _ => {}
        };
    }

    fn update_diagnostics_state(&mut self, window: &mut Window, cx: &mut Context<'_, Editor>) {
        if !self.diagnostics_enabled() {
            return;
        }
        self.refresh_active_diagnostics(cx);
        self.refresh_inline_diagnostics(true, window, cx);
        self.scrollbar_marker_state.dirty = true;
        cx.notify();
    }

    pub fn start_temporary_diff_override(&mut self) {
        self.load_diff_task.take();
        self.temporary_diff_override = true;
    }

    pub fn end_temporary_diff_override(&mut self, cx: &mut Context<Self>) {
        self.temporary_diff_override = false;
        self.set_render_diff_hunk_controls(Arc::new(render_diff_hunk_controls), cx);
        self.buffer.update(cx, |buffer, cx| {
            buffer.set_all_diff_hunks_collapsed(cx);
        });

        if let Some(project) = self.project.clone() {
            self.load_diff_task = Some(
                update_uncommitted_diff_for_buffer(
                    cx.entity(),
                    &project,
                    self.buffer.read(cx).all_buffers(),
                    self.buffer.clone(),
                    cx,
                )
                .shared(),
            );
        }
    }

    fn on_display_map_changed(
        &mut self,
        _: Entity<DisplayMap>,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        cx.notify();
    }

    fn fetch_accent_data(&self, cx: &App) -> Option<AccentData> {
        if !self.mode.is_full() {
            return None;
        }

        let theme_settings = theme_settings::ThemeSettings::get_global(cx);
        let theme = cx.theme();
        let accent_colors = theme.accents().clone();

        let accent_overrides = theme_settings
            .theme_overrides
            .get(theme.name.as_ref())
            .map(|theme_style| &theme_style.accents)
            .into_iter()
            .flatten()
            .chain(
                theme_settings
                    .experimental_theme_overrides
                    .as_ref()
                    .map(|overrides| &overrides.accents)
                    .into_iter()
                    .flatten(),
            )
            .flat_map(|accent| accent.0.clone().map(SharedString::from))
            .collect();

        Some(AccentData {
            colors: accent_colors,
            overrides: accent_overrides,
        })
    }

    fn fetch_applicable_language_settings(
        &self,
        cx: &App,
    ) -> HashMap<Option<LanguageName>, LanguageSettings> {
        if !self.mode.is_full() {
            return HashMap::default();
        }

        self.buffer().read(cx).all_buffers().into_iter().fold(
            HashMap::default(),
            |mut acc, buffer| {
                let buffer = buffer.read(cx);
                let language = buffer.language().map(|language| language.name());
                if let hash_map::Entry::Vacant(v) = acc.entry(language) {
                    v.insert(LanguageSettings::for_buffer(&buffer, cx).into_owned());
                }
                acc
            },
        )
    }

    fn settings_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let new_language_settings = self.fetch_applicable_language_settings(cx);
        let language_settings_changed = new_language_settings != self.applicable_language_settings;
        self.applicable_language_settings = new_language_settings;

        let new_accents = self.fetch_accent_data(cx);
        let accents_changed = new_accents != self.accent_data;
        self.accent_data = new_accents;

        if self.diagnostics_enabled() {
            let new_severity = EditorSettings::get_global(cx)
                .diagnostics_max_severity
                .unwrap_or(DiagnosticSeverity::Hint);
            self.set_max_diagnostics_severity(new_severity, cx);
        }
        self.refresh_runnables(None, window, cx);
        self.update_edit_prediction_settings(cx);
        self.refresh_edit_prediction(true, false, window, cx);
        self.refresh_inline_values(cx);

        let old_cursor_shape = self.cursor_shape;
        let old_show_breadcrumbs = self.show_breadcrumbs;

        {
            let editor_settings = EditorSettings::get_global(cx);
            self.scroll_manager.vertical_scroll_margin = editor_settings.vertical_scroll_margin;
            self.show_breadcrumbs = editor_settings.toolbar.breadcrumbs;
            self.cursor_shape = editor_settings.cursor_shape.unwrap_or_default();
            self.hide_mouse_mode = editor_settings.hide_mouse.unwrap_or_default();
        }

        if old_cursor_shape != self.cursor_shape {
            cx.emit(EditorEvent::CursorShapeChanged);
        }

        if old_show_breadcrumbs != self.show_breadcrumbs {
            cx.emit(EditorEvent::BreadcrumbsChanged);
        }

        let (restore_unsaved_buffers, show_inline_diagnostics, inline_blame_enabled) = {
            let project_settings = ProjectSettings::get_global(cx);
            (
                project_settings.session.restore_unsaved_buffers,
                project_settings.diagnostics.inline.enabled,
                project_settings.git.inline_blame.enabled,
            )
        };
        self.buffer_serialization = self
            .should_serialize_buffer()
            .then(|| BufferSerialization::new(restore_unsaved_buffers));

        if self.mode.is_full() {
            if self.show_inline_diagnostics != show_inline_diagnostics {
                self.show_inline_diagnostics = show_inline_diagnostics;
                self.refresh_inline_diagnostics(false, window, cx);
            }

            if self.git_blame_inline_enabled != inline_blame_enabled {
                self.toggle_git_blame_inline_internal(false, window, cx);
            }

            let minimap_settings = EditorSettings::get_global(cx).minimap;
            if self.minimap_visibility != MinimapVisibility::Disabled {
                if self.minimap_visibility.settings_visibility()
                    != minimap_settings.minimap_enabled()
                {
                    self.set_minimap_visibility(
                        MinimapVisibility::for_mode(self.mode(), cx),
                        window,
                        cx,
                    );
                } else if let Some(minimap_entity) = self.minimap.as_ref() {
                    minimap_entity.update(cx, |minimap_editor, cx| {
                        minimap_editor.update_minimap_configuration(minimap_settings, cx)
                    })
                }
            }

            if language_settings_changed || accents_changed {
                self.colorize_brackets(true, cx);
            }

            if language_settings_changed {
                self.clear_disabled_lsp_folding_ranges(window, cx);
                self.refresh_document_symbols(None, cx);
            }

            if let Some(inlay_splice) = self.colors.as_mut().and_then(|colors| {
                colors.render_mode_updated(EditorSettings::get_global(cx).lsp_document_colors)
            }) {
                if !inlay_splice.is_empty() {
                    self.splice_inlays(&inlay_splice.to_remove, inlay_splice.to_insert, cx);
                }
                self.refresh_document_colors(None, window, cx);
            }

            self.refresh_inlay_hints(
                InlayHintRefreshReason::SettingsChange(inlay_hint_settings(
                    self.selections.newest_anchor().head(),
                    &self.buffer.read(cx).snapshot(cx),
                    cx,
                )),
                cx,
            );

            let new_semantic_token_rules = ProjectSettings::get_global(cx)
                .global_lsp_settings
                .semantic_token_rules
                .clone();
            let semantic_token_rules_changed = self
                .semantic_token_state
                .update_rules(new_semantic_token_rules);
            if language_settings_changed || semantic_token_rules_changed {
                self.invalidate_semantic_tokens(None);
                self.refresh_semantic_tokens(None, None, cx);
            }
        }

        cx.notify();
    }

    fn theme_changed(&mut self, _: &mut Window, cx: &mut Context<Self>) {
        if !self.mode.is_full() {
            return;
        }

        let new_accents = self.fetch_accent_data(cx);
        if new_accents != self.accent_data {
            self.accent_data = new_accents;
            self.colorize_brackets(true, cx);
        }

        self.invalidate_semantic_tokens(None);
        self.refresh_semantic_tokens(None, None, cx);
    }

    pub fn set_searchable(&mut self, searchable: bool) {
        self.searchable = searchable;
    }

    pub fn searchable(&self) -> bool {
        self.searchable
    }

    pub fn open_excerpts_in_split(
        &mut self,
        _: &OpenExcerptsSplit,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.open_excerpts_common(None, true, window, cx)
    }

    pub fn open_excerpts(&mut self, _: &OpenExcerpts, window: &mut Window, cx: &mut Context<Self>) {
        self.open_excerpts_common(None, false, window, cx)
    }

    pub(crate) fn open_excerpts_common(
        &mut self,
        jump_data: Option<JumpData>,
        split: bool,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if self.buffer.read(cx).is_singleton() {
            cx.propagate();
            return;
        }

        let mut new_selections_by_buffer = HashMap::default();
        match &jump_data {
            Some(JumpData::MultiBufferPoint {
                anchor,
                position,
                line_offset_from_top,
            }) => {
                if let Some(buffer) = self.buffer.read(cx).buffer(anchor.buffer_id) {
                    let buffer_snapshot = buffer.read(cx).snapshot();
                    let jump_to_point = if buffer_snapshot.can_resolve(&anchor) {
                        language::ToPoint::to_point(anchor, &buffer_snapshot)
                    } else {
                        buffer_snapshot.clip_point(*position, Bias::Left)
                    };
                    let jump_to_offset = buffer_snapshot.point_to_offset(jump_to_point);
                    new_selections_by_buffer.insert(
                        buffer,
                        (
                            vec![BufferOffset(jump_to_offset)..BufferOffset(jump_to_offset)],
                            Some(*line_offset_from_top),
                        ),
                    );
                }
            }
            Some(JumpData::MultiBufferRow {
                row,
                line_offset_from_top,
            }) => {
                let point = MultiBufferPoint::new(row.0, 0);
                if let Some((buffer, buffer_point)) =
                    self.buffer.read(cx).point_to_buffer_point(point, cx)
                {
                    let buffer_offset = buffer.read(cx).point_to_offset(buffer_point);
                    new_selections_by_buffer
                        .entry(buffer)
                        .or_insert((Vec::new(), Some(*line_offset_from_top)))
                        .0
                        .push(BufferOffset(buffer_offset)..BufferOffset(buffer_offset))
                }
            }
            None => {
                let selections = self
                    .selections
                    .all::<MultiBufferOffset>(&self.display_snapshot(cx));
                let multi_buffer = self.buffer.read(cx);
                let multi_buffer_snapshot = multi_buffer.snapshot(cx);
                for selection in selections {
                    for (snapshot, range, anchor) in multi_buffer_snapshot
                        .range_to_buffer_ranges_with_deleted_hunks(selection.range())
                    {
                        if let Some((text_anchor, _)) = anchor.and_then(|anchor| {
                            multi_buffer_snapshot.anchor_to_buffer_anchor(anchor)
                        }) {
                            let Some(buffer_handle) = multi_buffer.buffer(text_anchor.buffer_id)
                            else {
                                continue;
                            };
                            let offset = text::ToOffset::to_offset(
                                &text_anchor,
                                &buffer_handle.read(cx).snapshot(),
                            );
                            let range = BufferOffset(offset)..BufferOffset(offset);
                            new_selections_by_buffer
                                .entry(buffer_handle)
                                .or_insert((Vec::new(), None))
                                .0
                                .push(range)
                        } else {
                            let Some(buffer_handle) = multi_buffer.buffer(snapshot.remote_id())
                            else {
                                continue;
                            };
                            new_selections_by_buffer
                                .entry(buffer_handle)
                                .or_insert((Vec::new(), None))
                                .0
                                .push(range)
                        }
                    }
                }
            }
        }

        if self.delegate_open_excerpts {
            let selections_by_buffer: HashMap<_, _> = new_selections_by_buffer
                .into_iter()
                .map(|(buffer, value)| (buffer.read(cx).remote_id(), value))
                .collect();
            if !selections_by_buffer.is_empty() {
                cx.emit(EditorEvent::OpenExcerptsRequested {
                    selections_by_buffer,
                    split,
                });
            }
            return;
        }

        let Some(workspace) = self.workspace() else {
            cx.propagate();
            return;
        };

        new_selections_by_buffer
            .retain(|buffer, _| buffer.read(cx).file().is_none_or(|file| file.can_open()));

        if new_selections_by_buffer.is_empty() {
            return;
        }

        Self::open_buffers_in_workspace(
            workspace.downgrade(),
            new_selections_by_buffer,
            split,
            window,
            cx,
        );
    }

    pub(crate) fn open_buffers_in_workspace(
        workspace: WeakEntity<Workspace>,
        new_selections_by_buffer: HashMap<
            Entity<language::Buffer>,
            (Vec<Range<BufferOffset>>, Option<u32>),
        >,
        split: bool,
        window: &mut Window,
        cx: &mut App,
    ) {
        // We defer the pane interaction because we ourselves are a workspace item
        // and activating a new item causes the pane to call a method on us reentrantly,
        // which panics if we're on the stack.
        window.defer(cx, move |window, cx| {
            workspace
                .update(cx, |workspace, cx| {
                    let pane = if split {
                        workspace.adjacent_pane(window, cx)
                    } else {
                        workspace.active_pane().clone()
                    };

                    for (buffer, (ranges, scroll_offset)) in new_selections_by_buffer {
                        let buffer_read = buffer.read(cx);
                        let (has_file, is_project_file) = if let Some(file) = buffer_read.file() {
                            (true, project::File::from_dyn(Some(file)).is_some())
                        } else {
                            (false, false)
                        };

                        // If project file is none workspace.open_project_item will fail to open the excerpt
                        // in a pre existing workspace item if one exists, because Buffer entity_id will be None
                        // so we check if there's a tab match in that case first
                        let editor = (!has_file || !is_project_file)
                            .then(|| {
                                // Handle file-less buffers separately: those are not really the project items, so won't have a project path or entity id,
                                // so `workspace.open_project_item` will never find them, always opening a new editor.
                                // Instead, we try to activate the existing editor in the pane first.
                                let (editor, pane_item_index, pane_item_id) =
                                    pane.read(cx).items().enumerate().find_map(|(i, item)| {
                                        let editor = item.downcast::<Editor>()?;
                                        let singleton_buffer =
                                            editor.read(cx).buffer().read(cx).as_singleton()?;
                                        if singleton_buffer == buffer {
                                            Some((editor, i, item.item_id()))
                                        } else {
                                            None
                                        }
                                    })?;
                                pane.update(cx, |pane, cx| {
                                    pane.activate_item(pane_item_index, true, true, window, cx);
                                    if !PreviewTabsSettings::get_global(cx)
                                        .enable_preview_from_multibuffer
                                    {
                                        pane.unpreview_item_if_preview(pane_item_id);
                                    }
                                });
                                Some(editor)
                            })
                            .flatten()
                            .unwrap_or_else(|| {
                                let keep_old_preview = PreviewTabsSettings::get_global(cx)
                                    .enable_keep_preview_on_code_navigation;
                                let allow_new_preview = PreviewTabsSettings::get_global(cx)
                                    .enable_preview_from_multibuffer;
                                workspace.open_project_item::<Self>(
                                    pane.clone(),
                                    buffer,
                                    true,
                                    true,
                                    keep_old_preview,
                                    allow_new_preview,
                                    window,
                                    cx,
                                )
                            });

                        editor.update(cx, |editor, cx| {
                            if has_file && !is_project_file {
                                editor.set_read_only(true);
                            }
                            let autoscroll = match scroll_offset {
                                Some(scroll_offset) => {
                                    Autoscroll::top_relative(scroll_offset as usize)
                                }
                                None => Autoscroll::newest(),
                            };
                            let nav_history = editor.nav_history.take();
                            let multibuffer_snapshot = editor.buffer().read(cx).snapshot(cx);
                            let Some(buffer_snapshot) = multibuffer_snapshot.as_singleton() else {
                                return;
                            };
                            editor.change_selections(
                                SelectionEffects::scroll(autoscroll),
                                window,
                                cx,
                                |s| {
                                    s.select_ranges(ranges.into_iter().map(|range| {
                                        let range = buffer_snapshot.anchor_before(range.start)
                                            ..buffer_snapshot.anchor_after(range.end);
                                        multibuffer_snapshot
                                            .buffer_anchor_range_to_anchor_range(range)
                                            .unwrap()
                                    }));
                                },
                            );
                            editor.nav_history = nav_history;
                        });
                    }
                })
                .ok();
        });
    }

    fn marked_text_ranges(&self, cx: &App) -> Option<Vec<Range<MultiBufferOffsetUtf16>>> {
        let snapshot = self.buffer.read(cx).read(cx);
        let (_, ranges) = self.text_highlights(HighlightKey::InputComposition, cx)?;
        Some(
            ranges
                .iter()
                .map(move |range| {
                    range.start.to_offset_utf16(&snapshot)..range.end.to_offset_utf16(&snapshot)
                })
                .collect(),
        )
    }

    fn selection_replacement_ranges(
        &self,
        range: Range<MultiBufferOffsetUtf16>,
        cx: &mut App,
    ) -> Vec<Range<MultiBufferOffsetUtf16>> {
        let selections = self
            .selections
            .all::<MultiBufferOffsetUtf16>(&self.display_snapshot(cx));
        let newest_selection = selections
            .iter()
            .max_by_key(|selection| selection.id)
            .unwrap();
        let start_delta = range.start.0.0 as isize - newest_selection.start.0.0 as isize;
        let end_delta = range.end.0.0 as isize - newest_selection.end.0.0 as isize;
        let snapshot = self.buffer.read(cx).read(cx);
        selections
            .into_iter()
            .map(|mut selection| {
                selection.start.0.0 =
                    (selection.start.0.0 as isize).saturating_add(start_delta) as usize;
                selection.end.0.0 = (selection.end.0.0 as isize).saturating_add(end_delta) as usize;
                snapshot.clip_offset_utf16(selection.start, Bias::Left)
                    ..snapshot.clip_offset_utf16(selection.end, Bias::Right)
            })
            .collect()
    }

    fn report_editor_event(
        &self,
        reported_event: ReportEditorEvent,
        file_extension: Option<String>,
        cx: &App,
    ) {
        if cfg!(any(test, feature = "test-support")) {
            return;
        }

        let Some(project) = &self.project else { return };

        // If None, we are in a file without an extension
        let file = self
            .buffer
            .read(cx)
            .as_singleton()
            .and_then(|b| b.read(cx).file());
        let file_extension = file_extension.or(file
            .as_ref()
            .and_then(|file| Path::new(file.file_name(cx)).extension())
            .and_then(|e| e.to_str())
            .map(|a| a.to_string()));

        let vim_mode = vim_mode_setting::VimModeSetting::try_get(cx)
            .map(|vim_mode| vim_mode.0)
            .unwrap_or(false);

        let edit_predictions_provider = all_language_settings(file, cx).edit_predictions.provider;
        let copilot_enabled = edit_predictions_provider
            == language::language_settings::EditPredictionProvider::Copilot;
        let copilot_enabled_for_language = self
            .buffer
            .read(cx)
            .language_settings(cx)
            .show_edit_predictions;

        let project = project.read(cx);
        let event_type = reported_event.event_type();

        if let ReportEditorEvent::Saved { auto_saved } = reported_event {
            telemetry::event!(
                event_type,
                type = if auto_saved {"autosave"} else {"manual"},
                file_extension,
                vim_mode,
                copilot_enabled,
                copilot_enabled_for_language,
                edit_predictions_provider,
                is_via_ssh = project.is_via_remote_server(),
            );
        } else {
            telemetry::event!(
                event_type,
                file_extension,
                vim_mode,
                copilot_enabled,
                copilot_enabled_for_language,
                edit_predictions_provider,
                is_via_ssh = project.is_via_remote_server(),
            );
        };
    }

    /// Copy the highlighted chunks to the clipboard as JSON. The format is an array of lines,
    /// with each line being an array of {text, highlight} objects.
    fn copy_highlight_json(
        &mut self,
        _: &CopyHighlightJson,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) {
        #[derive(Serialize)]
        struct Chunk<'a> {
            text: String,
            highlight: Option<&'a str>,
        }

        let snapshot = self.buffer.read(cx).snapshot(cx);
        let mut selection = self.selections.newest::<Point>(&self.display_snapshot(cx));
        let max_point = snapshot.max_point();

        let range = if self.selections.line_mode() {
            selection.start = Point::new(selection.start.row, 0);
            selection.end = cmp::min(max_point, Point::new(selection.end.row + 1, 0));
            selection.goal = SelectionGoal::None;
            selection.range()
        } else if selection.is_empty() {
            Point::new(0, 0)..max_point
        } else {
            selection.range()
        };

        let chunks = snapshot.chunks(
            range,
            LanguageAwareStyling {
                tree_sitter: true,
                diagnostics: true,
            },
        );
        let mut lines = Vec::new();
        let mut line: VecDeque<Chunk> = VecDeque::new();

        let Some(style) = self.style.as_ref() else {
            return;
        };

        for chunk in chunks {
            let highlight = chunk
                .syntax_highlight_id
                .and_then(|id| style.syntax.get_capture_name(id));

            let mut chunk_lines = chunk.text.split('\n').peekable();
            while let Some(text) = chunk_lines.next() {
                let mut merged_with_last_token = false;
                if let Some(last_token) = line.back_mut()
                    && last_token.highlight == highlight
                {
                    last_token.text.push_str(text);
                    merged_with_last_token = true;
                }

                if !merged_with_last_token {
                    line.push_back(Chunk {
                        text: text.into(),
                        highlight,
                    });
                }

                if chunk_lines.peek().is_some() {
                    if line.len() > 1 && line.front().unwrap().text.is_empty() {
                        line.pop_front();
                    }
                    if line.len() > 1 && line.back().unwrap().text.is_empty() {
                        line.pop_back();
                    }

                    lines.push(mem::take(&mut line));
                }
            }
        }

        if line.iter().any(|chunk| !chunk.text.is_empty()) {
            lines.push(line);
        }

        let Some(lines) = serde_json::to_string_pretty(&lines).log_err() else {
            return;
        };
        cx.write_to_clipboard(ClipboardItem::new_string(lines));
    }

    pub fn open_context_menu(
        &mut self,
        _: &OpenContextMenu,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.request_autoscroll(Autoscroll::newest(), cx);
        let position = self
            .selections
            .newest_display(&self.display_snapshot(cx))
            .start;
        mouse_context_menu::deploy_context_menu(self, None, position, window, cx);
    }

    pub fn replay_insert_event(
        &mut self,
        text: &str,
        relative_utf16_range: Option<Range<isize>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.input_enabled {
            cx.emit(EditorEvent::InputIgnored { text: text.into() });
            return;
        }
        if let Some(relative_utf16_range) = relative_utf16_range {
            let selections = self
                .selections
                .all::<MultiBufferOffsetUtf16>(&self.display_snapshot(cx));
            self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                let new_ranges = selections.into_iter().map(|range| {
                    let start = MultiBufferOffsetUtf16(OffsetUtf16(
                        range
                            .head()
                            .0
                            .0
                            .saturating_add_signed(relative_utf16_range.start),
                    ));
                    let end = MultiBufferOffsetUtf16(OffsetUtf16(
                        range
                            .head()
                            .0
                            .0
                            .saturating_add_signed(relative_utf16_range.end),
                    ));
                    start..end
                });
                s.select_ranges(new_ranges);
            });
        }

        self.handle_input(text, window, cx);
    }

    pub fn is_focused(&self, window: &Window) -> bool {
        self.focus_handle.is_focused(window)
    }

    fn handle_focus(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        cx.emit(EditorEvent::Focused);

        if let Some(descendant) = self
            .last_focused_descendant
            .take()
            .and_then(|descendant| descendant.upgrade())
        {
            window.focus(&descendant, cx);
        } else {
            if let Some(blame) = self.blame.as_ref() {
                blame.update(cx, GitBlame::focus)
            }

            self.blink_manager.update(cx, BlinkManager::enable);
            self.show_cursor_names(window, cx);
            self.buffer.update(cx, |buffer, cx| {
                buffer.finalize_last_transaction(cx);
                if self.leader_id.is_none() {
                    buffer.set_active_selections(
                        &self.selections.disjoint_anchors_arc(),
                        self.selections.line_mode(),
                        self.cursor_shape,
                        cx,
                    );
                }
            });

            if let Some(position_map) = self.last_position_map.clone()
                && !self.mouse_cursor_hidden
            {
                EditorElement::mouse_moved(
                    self,
                    &MouseMoveEvent {
                        position: window.mouse_position(),
                        pressed_button: None,
                        modifiers: window.modifiers(),
                    },
                    &position_map,
                    None,
                    window,
                    cx,
                );
            }
        }
    }

    fn handle_focus_in(&mut self, _: &mut Window, cx: &mut Context<Self>) {
        cx.emit(EditorEvent::FocusedIn)
    }

    fn handle_focus_out(
        &mut self,
        event: FocusOutEvent,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if event.blurred != self.focus_handle {
            self.last_focused_descendant = Some(event.blurred);
        }
        self.selection_drag_state = SelectionDragState::None;
        self.refresh_inlay_hints(InlayHintRefreshReason::ModifiersChanged(false), cx);
    }

    pub fn handle_blur(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        self.blink_manager.update(cx, BlinkManager::disable);
        self.buffer
            .update(cx, |buffer, cx| buffer.remove_active_selections(cx));

        if let Some(blame) = self.blame.as_ref() {
            blame.update(cx, GitBlame::blur)
        }
        if !self.hover_state.focused(window, cx) {
            hide_hover(self, cx);
        }
        if !self
            .context_menu
            .borrow()
            .as_ref()
            .is_some_and(|context_menu| context_menu.focused(window, cx))
        {
            self.hide_context_menu(window, cx);
        }
        self.take_active_edit_prediction(true, cx);
        cx.emit(EditorEvent::Blurred);
        cx.notify();
    }

    pub fn observe_pending_input(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let mut pending: String = window
            .pending_input_keystrokes()
            .into_iter()
            .flatten()
            .filter_map(|keystroke| keystroke.key_char.clone())
            .collect();

        if !self.input_enabled || self.read_only || !self.focus_handle.is_focused(window) {
            pending = "".to_string();
        }

        let existing_pending = self
            .text_highlights(HighlightKey::PendingInput, cx)
            .map(|(_, ranges)| ranges.to_vec());
        if existing_pending.is_none() && pending.is_empty() {
            return;
        }
        let transaction =
            self.transact(window, cx, |this, window, cx| {
                let selections = this
                    .selections
                    .all::<MultiBufferOffset>(&this.display_snapshot(cx));
                let edits = selections
                    .iter()
                    .map(|selection| (selection.end..selection.end, pending.clone()));
                this.edit(edits, cx);
                this.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                    s.select_ranges(selections.into_iter().enumerate().map(|(ix, sel)| {
                        sel.start + ix * pending.len()..sel.end + ix * pending.len()
                    }));
                });
                if let Some(existing_ranges) = existing_pending {
                    let edits = existing_ranges.iter().map(|range| (range.clone(), ""));
                    this.edit(edits, cx);
                }
            });

        let snapshot = self.snapshot(window, cx);
        let ranges = self
            .selections
            .all::<MultiBufferOffset>(&snapshot.display_snapshot)
            .into_iter()
            .map(|selection| {
                snapshot.buffer_snapshot().anchor_after(selection.end)
                    ..snapshot
                        .buffer_snapshot()
                        .anchor_before(selection.end + pending.len())
            })
            .collect();

        if pending.is_empty() {
            self.clear_highlights(HighlightKey::PendingInput, cx);
        } else {
            self.highlight_text(
                HighlightKey::PendingInput,
                ranges,
                HighlightStyle {
                    underline: Some(UnderlineStyle {
                        thickness: px(1.),
                        color: None,
                        wavy: false,
                    }),
                    ..Default::default()
                },
                cx,
            );
        }

        self.ime_transaction = self.ime_transaction.or(transaction);
        if let Some(transaction) = self.ime_transaction {
            self.buffer.update(cx, |buffer, cx| {
                buffer.group_until_transaction(transaction, cx);
            });
        }

        if self
            .text_highlights(HighlightKey::PendingInput, cx)
            .is_none()
        {
            self.ime_transaction.take();
        }
    }

    pub fn register_action_renderer(
        &mut self,
        listener: impl Fn(&Editor, &mut Window, &mut Context<Editor>) + 'static,
    ) -> Subscription {
        let id = self.next_editor_action_id.post_inc();
        self.editor_actions
            .borrow_mut()
            .insert(id, Box::new(listener));

        let editor_actions = self.editor_actions.clone();
        Subscription::new(move || {
            editor_actions.borrow_mut().remove(&id);
        })
    }

    pub fn register_action<A: Action>(
        &mut self,
        listener: impl Fn(&A, &mut Window, &mut App) + 'static,
    ) -> Subscription {
        let id = self.next_editor_action_id.post_inc();
        let listener = Arc::new(listener);
        self.editor_actions.borrow_mut().insert(
            id,
            Box::new(move |_, window, _| {
                let listener = listener.clone();
                window.on_action(TypeId::of::<A>(), move |action, phase, window, cx| {
                    let action = action.downcast_ref().unwrap();
                    if phase == DispatchPhase::Bubble {
                        listener(action, window, cx)
                    }
                })
            }),
        );

        let editor_actions = self.editor_actions.clone();
        Subscription::new(move || {
            editor_actions.borrow_mut().remove(&id);
        })
    }

    pub fn file_header_size(&self) -> u32 {
        FILE_HEADER_HEIGHT
    }

    pub fn restore(
        &mut self,
        revert_changes: HashMap<BufferId, Vec<(Range<text::Anchor>, Rope)>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.buffer().update(cx, |multi_buffer, cx| {
            for (buffer_id, changes) in revert_changes {
                if let Some(buffer) = multi_buffer.buffer(buffer_id) {
                    buffer.update(cx, |buffer, cx| {
                        buffer.edit(
                            changes
                                .into_iter()
                                .map(|(range, text)| (range, text.to_string())),
                            None,
                            cx,
                        );
                    });
                }
            }
        });
        let selections = self
            .selections
            .all::<MultiBufferOffset>(&self.display_snapshot(cx));
        self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
            s.select(selections);
        });
    }

    pub fn to_pixel_point(
        &mut self,
        source: Anchor,
        editor_snapshot: &EditorSnapshot,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<gpui::Point<Pixels>> {
        let source_point = source.to_display_point(editor_snapshot);
        self.display_to_pixel_point(source_point, editor_snapshot, window, cx)
    }

    pub fn display_to_pixel_point(
        &mut self,
        source: DisplayPoint,
        editor_snapshot: &EditorSnapshot,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<gpui::Point<Pixels>> {
        let line_height = self.style(cx).text.line_height_in_pixels(window.rem_size());
        let text_layout_details = self.text_layout_details(window, cx);
        let scroll_top = text_layout_details
            .scroll_anchor
            .scroll_position(editor_snapshot)
            .y;

        if source.row().as_f64() < scroll_top.floor() {
            return None;
        }
        let source_x = editor_snapshot.x_for_display_point(source, &text_layout_details);
        let source_y = line_height * (source.row().as_f64() - scroll_top) as f32;
        Some(gpui::Point::new(source_x, source_y))
    }

    pub fn has_visible_completions_menu(&self) -> bool {
        !self.edit_prediction_preview_is_active()
            && self.context_menu.borrow().as_ref().is_some_and(|menu| {
                menu.visible() && matches!(menu, CodeContextMenu::Completions(_))
            })
    }

    pub fn register_addon<T: Addon>(&mut self, instance: T) {
        if self.mode.is_minimap() {
            return;
        }
        self.addons
            .insert(std::any::TypeId::of::<T>(), Box::new(instance));
    }

    pub fn unregister_addon<T: Addon>(&mut self) {
        self.addons.remove(&std::any::TypeId::of::<T>());
    }

    pub fn addon<T: Addon>(&self) -> Option<&T> {
        let type_id = std::any::TypeId::of::<T>();
        self.addons
            .get(&type_id)
            .and_then(|item| item.to_any().downcast_ref::<T>())
    }

    pub fn addon_mut<T: Addon>(&mut self) -> Option<&mut T> {
        let type_id = std::any::TypeId::of::<T>();
        self.addons
            .get_mut(&type_id)
            .and_then(|item| item.to_any_mut()?.downcast_mut::<T>())
    }

    fn character_dimensions(&self, window: &mut Window, cx: &mut App) -> CharacterDimensions {
        let text_layout_details = self.text_layout_details(window, cx);
        let style = &text_layout_details.editor_style;
        let font_id = window.text_system().resolve_font(&style.text.font());
        let font_size = style.text.font_size.to_pixels(window.rem_size());
        let line_height = style.text.line_height_in_pixels(window.rem_size());
        let em_width = window.text_system().em_width(font_id, font_size).unwrap();
        let em_advance = window.text_system().em_advance(font_id, font_size).unwrap();

        CharacterDimensions {
            em_width,
            em_advance,
            line_height,
        }
    }

    pub fn wait_for_diff_to_load(&self) -> Option<Shared<Task<()>>> {
        self.load_diff_task.clone()
    }

    fn read_metadata_from_db(
        &mut self,
        item_id: u64,
        workspace_id: WorkspaceId,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) {
        if self.buffer_kind(cx) == ItemBufferKind::Singleton
            && !self.mode.is_minimap()
            && WorkspaceSettings::get(None, cx).restore_on_startup
                != RestoreOnStartupBehavior::EmptyTab
        {
            let buffer_snapshot = OnceCell::new();

            // Get file path for path-based fold lookup
            let file_path: Option<Arc<Path>> =
                self.buffer().read(cx).as_singleton().and_then(|buffer| {
                    project::File::from_dyn(buffer.read(cx).file())
                        .map(|file| Arc::from(file.abs_path(cx)))
                });

            // Try file_folds (path-based) first, fallback to editor_folds (migration)
            let db = EditorDb::global(cx);
            let (folds, needs_migration) = if let Some(ref path) = file_path {
                if let Some(folds) = db.get_file_folds(workspace_id, path).log_err()
                    && !folds.is_empty()
                {
                    (Some(folds), false)
                } else if let Some(folds) = db.get_editor_folds(item_id, workspace_id).log_err()
                    && !folds.is_empty()
                {
                    // Found old editor_folds data, will migrate to file_folds
                    (Some(folds), true)
                } else {
                    (None, false)
                }
            } else {
                // No file path, try editor_folds as fallback
                let folds = db.get_editor_folds(item_id, workspace_id).log_err();
                (folds.filter(|f| !f.is_empty()), false)
            };

            if let Some(folds) = folds {
                let snapshot = buffer_snapshot.get_or_init(|| self.buffer.read(cx).snapshot(cx));
                let snapshot_len = snapshot.len().0;

                // Helper: search for fingerprint in buffer, return offset if found
                let find_fingerprint = |fingerprint: &str, search_start: usize| -> Option<usize> {
                    // Ensure we start at a character boundary (defensive)
                    let search_start = snapshot
                        .clip_offset(MultiBufferOffset(search_start), Bias::Left)
                        .0;
                    let search_end = snapshot_len.saturating_sub(fingerprint.len());

                    let mut byte_offset = search_start;
                    for ch in snapshot.chars_at(MultiBufferOffset(search_start)) {
                        if byte_offset > search_end {
                            break;
                        }
                        if snapshot.contains_str_at(MultiBufferOffset(byte_offset), fingerprint) {
                            return Some(byte_offset);
                        }
                        byte_offset += ch.len_utf8();
                    }
                    None
                };

                // Track search position to handle duplicate fingerprints correctly.
                // Folds are stored in document order, so we advance after each match.
                let mut search_start = 0usize;

                // Collect db_folds for migration (only folds with valid fingerprints)
                let mut db_folds_for_migration: Vec<(usize, usize, String, String)> = Vec::new();

                let valid_folds: Vec<_> = folds
                    .into_iter()
                    .filter_map(|(stored_start, stored_end, start_fp, end_fp)| {
                        // Skip folds without fingerprints (old data before migration)
                        let sfp = start_fp?;
                        let efp = end_fp?;
                        let efp_len = efp.len();

                        // Fast path: check if fingerprints match at stored offsets
                        // Note: end_fp is content BEFORE fold end, so check at (stored_end - efp_len)
                        let start_matches = stored_start < snapshot_len
                            && snapshot.contains_str_at(MultiBufferOffset(stored_start), &sfp);
                        let efp_check_pos = stored_end.saturating_sub(efp_len);
                        let end_matches = efp_check_pos >= stored_start
                            && stored_end <= snapshot_len
                            && snapshot.contains_str_at(MultiBufferOffset(efp_check_pos), &efp);

                        let (new_start, new_end) = if start_matches && end_matches {
                            // Offsets unchanged, use stored values
                            (stored_start, stored_end)
                        } else if sfp == efp {
                            // Short fold: identical fingerprints can only match once per search
                            // Use stored fold length to compute new_end
                            let new_start = find_fingerprint(&sfp, search_start)?;
                            let fold_len = stored_end - stored_start;
                            let new_end = new_start + fold_len;
                            (new_start, new_end)
                        } else {
                            // Slow path: search for fingerprints in buffer
                            let new_start = find_fingerprint(&sfp, search_start)?;
                            // Search for end_fp after start, then add efp_len to get actual fold end
                            let efp_pos = find_fingerprint(&efp, new_start + sfp.len())?;
                            let new_end = efp_pos + efp_len;
                            (new_start, new_end)
                        };

                        // Advance search position for next fold
                        search_start = new_end;

                        // Validate fold makes sense (end must be after start)
                        if new_end <= new_start {
                            return None;
                        }

                        // Collect for migration if needed
                        if needs_migration {
                            db_folds_for_migration.push((new_start, new_end, sfp, efp));
                        }

                        Some(
                            snapshot.clip_offset(MultiBufferOffset(new_start), Bias::Left)
                                ..snapshot.clip_offset(MultiBufferOffset(new_end), Bias::Right),
                        )
                    })
                    .collect();

                if !valid_folds.is_empty() {
                    self.fold_ranges(valid_folds, false, window, cx);

                    // Migrate from editor_folds to file_folds if we loaded from old table
                    if needs_migration {
                        if let Some(ref path) = file_path {
                            let path = path.clone();
                            let db = EditorDb::global(cx);
                            cx.spawn(async move |_, _| {
                                db.save_file_folds(workspace_id, path, db_folds_for_migration)
                                    .await
                                    .log_err();
                            })
                            .detach();
                        }
                    }
                }
            }

            if let Some(selections) = db.get_editor_selections(item_id, workspace_id).log_err()
                && !selections.is_empty()
            {
                let snapshot = buffer_snapshot.get_or_init(|| self.buffer.read(cx).snapshot(cx));
                // skip adding the initial selection to selection history
                self.selection_history.mode = SelectionHistoryMode::Skipping;
                self.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                    s.select_ranges(selections.into_iter().map(|(start, end)| {
                        snapshot.clip_offset(MultiBufferOffset(start), Bias::Left)
                            ..snapshot.clip_offset(MultiBufferOffset(end), Bias::Right)
                    }));
                });
                self.selection_history.mode = SelectionHistoryMode::Normal;
            };
        }

        self.read_scroll_position_from_db(item_id, workspace_id, window, cx);
    }

    /// Load folds from the file_folds database table by file path.
    /// Used when manually opening a file that was previously closed.
    fn load_folds_from_db(
        &mut self,
        workspace_id: WorkspaceId,
        file_path: PathBuf,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) {
        if self.mode.is_minimap()
            || WorkspaceSettings::get(None, cx).restore_on_startup
                == RestoreOnStartupBehavior::EmptyTab
        {
            return;
        }

        let Some(folds) = EditorDb::global(cx)
            .get_file_folds(workspace_id, &file_path)
            .log_err()
        else {
            return;
        };
        if folds.is_empty() {
            return;
        }

        let snapshot = self.buffer.read(cx).snapshot(cx);
        let snapshot_len = snapshot.len().0;

        // Helper: search for fingerprint in buffer, return offset if found
        let find_fingerprint = |fingerprint: &str, search_start: usize| -> Option<usize> {
            let search_start = snapshot
                .clip_offset(MultiBufferOffset(search_start), Bias::Left)
                .0;
            let search_end = snapshot_len.saturating_sub(fingerprint.len());

            let mut byte_offset = search_start;
            for ch in snapshot.chars_at(MultiBufferOffset(search_start)) {
                if byte_offset > search_end {
                    break;
                }
                if snapshot.contains_str_at(MultiBufferOffset(byte_offset), fingerprint) {
                    return Some(byte_offset);
                }
                byte_offset += ch.len_utf8();
            }
            None
        };

        let mut search_start = 0usize;

        let valid_folds: Vec<_> = folds
            .into_iter()
            .filter_map(|(stored_start, stored_end, start_fp, end_fp)| {
                let sfp = start_fp?;
                let efp = end_fp?;
                let efp_len = efp.len();

                let start_matches = stored_start < snapshot_len
                    && snapshot.contains_str_at(MultiBufferOffset(stored_start), &sfp);
                let efp_check_pos = stored_end.saturating_sub(efp_len);
                let end_matches = efp_check_pos >= stored_start
                    && stored_end <= snapshot_len
                    && snapshot.contains_str_at(MultiBufferOffset(efp_check_pos), &efp);

                let (new_start, new_end) = if start_matches && end_matches {
                    (stored_start, stored_end)
                } else if sfp == efp {
                    let new_start = find_fingerprint(&sfp, search_start)?;
                    let fold_len = stored_end - stored_start;
                    let new_end = new_start + fold_len;
                    (new_start, new_end)
                } else {
                    let new_start = find_fingerprint(&sfp, search_start)?;
                    let efp_pos = find_fingerprint(&efp, new_start + sfp.len())?;
                    let new_end = efp_pos + efp_len;
                    (new_start, new_end)
                };

                search_start = new_end;

                if new_end <= new_start {
                    return None;
                }

                Some(
                    snapshot.clip_offset(MultiBufferOffset(new_start), Bias::Left)
                        ..snapshot.clip_offset(MultiBufferOffset(new_end), Bias::Right),
                )
            })
            .collect();

        if !valid_folds.is_empty() {
            self.fold_ranges(valid_folds, false, window, cx);
        }
    }

    fn lsp_data_enabled(&self) -> bool {
        self.enable_lsp_data && self.mode().is_full()
    }

    fn update_lsp_data(
        &mut self,
        for_buffer: Option<BufferId>,
        window: &mut Window,
        cx: &mut Context<'_, Self>,
    ) {
        if !self.lsp_data_enabled() {
            return;
        }

        if let Some(buffer_id) = for_buffer {
            self.pull_diagnostics(buffer_id, window, cx);
        }
        self.refresh_semantic_tokens(for_buffer, None, cx);
        self.refresh_document_colors(for_buffer, window, cx);
        self.refresh_folding_ranges(for_buffer, window, cx);
        self.refresh_document_symbols(for_buffer, cx);
    }

    fn register_visible_buffers(&mut self, cx: &mut Context<Self>) {
        if !self.lsp_data_enabled() {
            return;
        }
        let visible_buffers: Vec<_> = self
            .visible_buffers(cx)
            .into_iter()
            .filter(|buffer| self.is_lsp_relevant(buffer.read(cx).file(), cx))
            .collect();
        for visible_buffer in visible_buffers {
            self.register_buffer(visible_buffer.read(cx).remote_id(), cx);
        }
    }

    fn register_buffer(&mut self, buffer_id: BufferId, cx: &mut Context<Self>) {
        if !self.lsp_data_enabled() {
            return;
        }

        if !self.registered_buffers.contains_key(&buffer_id)
            && let Some(project) = self.project.as_ref()
        {
            if let Some(buffer) = self.buffer.read(cx).buffer(buffer_id) {
                project.update(cx, |project, cx| {
                    self.registered_buffers.insert(
                        buffer_id,
                        project.register_buffer_with_language_servers(&buffer, cx),
                    );
                });
            } else {
                self.registered_buffers.remove(&buffer_id);
            }
        }
    }

    fn create_style(&self, cx: &App) -> EditorStyle {
        let settings = ThemeSettings::get_global(cx);

        let mut text_style = match self.mode {
            EditorMode::SingleLine | EditorMode::AutoHeight { .. } => TextStyle {
                color: cx.theme().colors().editor_foreground,
                font_family: settings.ui_font.family.clone(),
                font_features: settings.ui_font.features.clone(),
                font_fallbacks: settings.ui_font.fallbacks.clone(),
                font_size: rems(0.875).into(),
                font_weight: settings.ui_font.weight,
                line_height: relative(settings.buffer_line_height.value()),
                ..Default::default()
            },
            EditorMode::Full { .. } | EditorMode::Minimap { .. } => TextStyle {
                color: cx.theme().colors().editor_foreground,
                font_family: settings.buffer_font.family.clone(),
                font_features: settings.buffer_font.features.clone(),
                font_fallbacks: settings.buffer_font.fallbacks.clone(),
                font_size: settings.buffer_font_size(cx).into(),
                font_weight: settings.buffer_font.weight,
                line_height: relative(settings.buffer_line_height.value()),
                ..Default::default()
            },
        };
        if let Some(text_style_refinement) = &self.text_style_refinement {
            text_style.refine(text_style_refinement)
        }

        let background = match self.mode {
            EditorMode::SingleLine => cx.theme().system().transparent,
            EditorMode::AutoHeight { .. } => cx.theme().system().transparent,
            EditorMode::Full { .. } => cx.theme().colors().editor_background,
            EditorMode::Minimap { .. } => cx.theme().colors().editor_background.opacity(0.7),
        };

        EditorStyle {
            background,
            border: cx.theme().colors().border,
            local_player: cx.theme().players().local(),
            text: text_style,
            scrollbar_width: EditorElement::SCROLLBAR_WIDTH,
            syntax: cx.theme().syntax().clone(),
            status: cx.theme().status().clone(),
            inlay_hints_style: make_inlay_hints_style(cx),
            edit_prediction_styles: make_suggestion_styles(cx),
            unnecessary_code_fade: settings.unnecessary_code_fade,
            show_underlines: self.diagnostics_enabled(),
        }
    }

    fn breadcrumbs_inner(&self, cx: &App) -> Option<Vec<HighlightedText>> {
        let multibuffer = self.buffer().read(cx);
        let is_singleton = multibuffer.is_singleton();
        let (buffer_id, symbols) = self.outline_symbols_at_cursor.as_ref()?;
        let buffer = multibuffer.buffer(*buffer_id)?;

        let buffer = buffer.read(cx);
        // In a multi-buffer layout, we don't want to include the filename in the breadcrumbs
        let mut breadcrumbs = if is_singleton {
            let text = self.breadcrumb_header.clone().unwrap_or_else(|| {
                buffer
                    .snapshot()
                    .resolve_file_path(
                        self.project
                            .as_ref()
                            .map(|project| project.read(cx).visible_worktrees(cx).count() > 1)
                            .unwrap_or_default(),
                        cx,
                    )
                    .unwrap_or_else(|| {
                        if multibuffer.is_singleton() {
                            multibuffer.title(cx).to_string()
                        } else {
                            "untitled".to_string()
                        }
                    })
            });
            vec![HighlightedText {
                text: text.into(),
                highlights: vec![],
            }]
        } else {
            vec![]
        };

        breadcrumbs.extend(symbols.iter().map(|symbol| HighlightedText {
            text: symbol.text.clone().into(),
            highlights: symbol.highlight_ranges.clone(),
        }));
        Some(breadcrumbs)
    }

    fn disable_lsp_data(&mut self) {
        self.enable_lsp_data = false;
    }

    fn disable_runnables(&mut self) {
        self.enable_runnables = false;
    }

    fn update_data_on_scroll(&mut self, window: &mut Window, cx: &mut Context<'_, Self>) {
        self.register_visible_buffers(cx);
        self.colorize_brackets(false, cx);
        self.refresh_inlay_hints(InlayHintRefreshReason::NewLinesShown, cx);
        if !self.buffer().read(cx).is_singleton() {
            self.update_lsp_data(None, window, cx);
            self.refresh_runnables(None, window, cx);
        }
    }
}

fn edit_for_markdown_paste<'a>(
    buffer: &MultiBufferSnapshot,
    range: Range<MultiBufferOffset>,
    to_insert: &'a str,
    url: Option<url::Url>,
) -> (Range<MultiBufferOffset>, Cow<'a, str>) {
    if url.is_none() {
        return (range, Cow::Borrowed(to_insert));
    };

    let old_text = buffer.text_for_range(range.clone()).collect::<String>();

    let new_text = if range.is_empty() || url::Url::parse(&old_text).is_ok() {
        Cow::Borrowed(to_insert)
    } else {
        Cow::Owned(format!("[{old_text}]({to_insert})"))
    };
    (range, new_text)
}

fn process_completion_for_edit(
    completion: &Completion,
    intent: CompletionIntent,
    buffer: &Entity<Buffer>,
    cursor_position: &text::Anchor,
    cx: &mut Context<Editor>,
) -> CompletionEdit {
    let buffer = buffer.read(cx);
    let buffer_snapshot = buffer.snapshot();
    let (snippet, new_text) = if completion.is_snippet() {
        let mut snippet_source = completion.new_text.clone();
        // Workaround for typescript language server issues so that methods don't expand within
        // strings and functions with type expressions. The previous point is used because the query
        // for function identifier doesn't match when the cursor is immediately after. See PR #30312
        let previous_point = text::ToPoint::to_point(cursor_position, &buffer_snapshot);
        let previous_point = if previous_point.column > 0 {
            cursor_position.to_previous_offset(&buffer_snapshot)
        } else {
            cursor_position.to_offset(&buffer_snapshot)
        };
        if let Some(scope) = buffer_snapshot.language_scope_at(previous_point)
            && scope.prefers_label_for_snippet_in_completion()
            && let Some(label) = completion.label()
            && matches!(
                completion.kind(),
                Some(CompletionItemKind::FUNCTION) | Some(CompletionItemKind::METHOD)
            )
        {
            snippet_source = label;
        }
        match Snippet::parse(&snippet_source).log_err() {
            Some(parsed_snippet) => (Some(parsed_snippet.clone()), parsed_snippet.text),
            None => (None, completion.new_text.clone()),
        }
    } else {
        (None, completion.new_text.clone())
    };

    let mut range_to_replace = {
        let replace_range = &completion.replace_range;
        if let CompletionSource::Lsp {
            insert_range: Some(insert_range),
            ..
        } = &completion.source
        {
            debug_assert_eq!(
                insert_range.start, replace_range.start,
                "insert_range and replace_range should start at the same position"
            );
            debug_assert!(
                insert_range
                    .start
                    .cmp(cursor_position, &buffer_snapshot)
                    .is_le(),
                "insert_range should start before or at cursor position"
            );
            debug_assert!(
                replace_range
                    .start
                    .cmp(cursor_position, &buffer_snapshot)
                    .is_le(),
                "replace_range should start before or at cursor position"
            );

            let should_replace = match intent {
                CompletionIntent::CompleteWithInsert => false,
                CompletionIntent::CompleteWithReplace => true,
                CompletionIntent::Complete | CompletionIntent::Compose => {
                    let insert_mode = LanguageSettings::for_buffer(&buffer, cx)
                        .completions
                        .lsp_insert_mode;
                    match insert_mode {
                        LspInsertMode::Insert => false,
                        LspInsertMode::Replace => true,
                        LspInsertMode::ReplaceSubsequence => {
                            let mut text_to_replace = buffer.chars_for_range(
                                buffer.anchor_before(replace_range.start)
                                    ..buffer.anchor_after(replace_range.end),
                            );
                            let mut current_needle = text_to_replace.next();
                            for haystack_ch in completion.label.text.chars() {
                                if let Some(needle_ch) = current_needle
                                    && haystack_ch.eq_ignore_ascii_case(&needle_ch)
                                {
                                    current_needle = text_to_replace.next();
                                }
                            }
                            current_needle.is_none()
                        }
                        LspInsertMode::ReplaceSuffix => {
                            if replace_range
                                .end
                                .cmp(cursor_position, &buffer_snapshot)
                                .is_gt()
                            {
                                let range_after_cursor = *cursor_position..replace_range.end;
                                let text_after_cursor = buffer
                                    .text_for_range(
                                        buffer.anchor_before(range_after_cursor.start)
                                            ..buffer.anchor_after(range_after_cursor.end),
                                    )
                                    .collect::<String>()
                                    .to_ascii_lowercase();
                                completion
                                    .label
                                    .text
                                    .to_ascii_lowercase()
                                    .ends_with(&text_after_cursor)
                            } else {
                                true
                            }
                        }
                    }
                }
            };

            if should_replace {
                replace_range.clone()
            } else {
                insert_range.clone()
            }
        } else {
            replace_range.clone()
        }
    };

    if range_to_replace
        .end
        .cmp(cursor_position, &buffer_snapshot)
        .is_lt()
    {
        range_to_replace.end = *cursor_position;
    }

    CompletionEdit {
        new_text,
        replace_range: range_to_replace,
        snippet,
    }
}

struct CompletionEdit {
    new_text: String,
    replace_range: Range<text::Anchor>,
    snippet: Option<Snippet>,
}

fn comment_delimiter_for_newline(
    start_point: &Point,
    buffer: &MultiBufferSnapshot,
    language: &LanguageScope,
) -> Option<Arc<str>> {
    let delimiters = language.line_comment_prefixes();
    let max_len_of_delimiter = delimiters.iter().map(|delimiter| delimiter.len()).max()?;
    let (snapshot, range) = buffer.buffer_line_for_row(MultiBufferRow(start_point.row))?;

    let num_of_whitespaces = snapshot
        .chars_for_range(range.clone())
        .take_while(|c| c.is_whitespace())
        .count();
    let comment_candidate = snapshot
        .chars_for_range(range.clone())
        .skip(num_of_whitespaces)
        .take(max_len_of_delimiter + 2)
        .collect::<String>();
    let (delimiter, trimmed_len, is_repl) = delimiters
        .iter()
        .filter_map(|delimiter| {
            let prefix = delimiter.trim_end();
            if comment_candidate.starts_with(prefix) {
                let is_repl = if let Some(stripped_comment) = comment_candidate.strip_prefix(prefix)
                {
                    stripped_comment.starts_with(" %%")
                } else {
                    false
                };
                Some((delimiter, prefix.len(), is_repl))
            } else {
                None
            }
        })
        .max_by_key(|(_, len, _)| *len)?;

    if let Some(BlockCommentConfig {
        start: block_start, ..
    }) = language.block_comment()
    {
        let block_start_trimmed = block_start.trim_end();
        if block_start_trimmed.starts_with(delimiter.trim_end()) {
            let line_content = snapshot
                .chars_for_range(range.clone())
                .skip(num_of_whitespaces)
                .take(block_start_trimmed.len())
                .collect::<String>();

            if line_content.starts_with(block_start_trimmed) {
                return None;
            }
        }
    }

    let cursor_is_placed_after_comment_marker =
        num_of_whitespaces + trimmed_len <= start_point.column as usize;
    if cursor_is_placed_after_comment_marker {
        if !is_repl {
            return Some(delimiter.clone());
        }

        let line_content_after_cursor: String = snapshot
            .chars_for_range(range)
            .skip(start_point.column as usize)
            .collect();

        if line_content_after_cursor.trim().is_empty() {
            return None;
        } else {
            return Some(delimiter.clone());
        }
    } else {
        None
    }
}

fn documentation_delimiter_for_newline(
    start_point: &Point,
    buffer: &MultiBufferSnapshot,
    language: &LanguageScope,
    newline_config: &mut NewlineConfig,
) -> Option<Arc<str>> {
    let BlockCommentConfig {
        start: start_tag,
        end: end_tag,
        prefix: delimiter,
        tab_size: len,
    } = language.documentation_comment()?;
    let is_within_block_comment = buffer
        .language_scope_at(*start_point)
        .is_some_and(|scope| scope.override_name() == Some("comment"));
    if !is_within_block_comment {
        return None;
    }

    let (snapshot, range) = buffer.buffer_line_for_row(MultiBufferRow(start_point.row))?;

    let num_of_whitespaces = snapshot
        .chars_for_range(range.clone())
        .take_while(|c| c.is_whitespace())
        .count();

    // It is safe to use a column from MultiBufferPoint in context of a single buffer ranges, because we're only ever looking at a single line at a time.
    let column = start_point.column;
    let cursor_is_after_start_tag = {
        let start_tag_len = start_tag.len();
        let start_tag_line = snapshot
            .chars_for_range(range.clone())
            .skip(num_of_whitespaces)
            .take(start_tag_len)
            .collect::<String>();
        if start_tag_line.starts_with(start_tag.as_ref()) {
            num_of_whitespaces + start_tag_len <= column as usize
        } else {
            false
        }
    };

    let cursor_is_after_delimiter = {
        let delimiter_trim = delimiter.trim_end();
        let delimiter_line = snapshot
            .chars_for_range(range.clone())
            .skip(num_of_whitespaces)
            .take(delimiter_trim.len())
            .collect::<String>();
        if delimiter_line.starts_with(delimiter_trim) {
            num_of_whitespaces + delimiter_trim.len() <= column as usize
        } else {
            false
        }
    };

    let mut needs_extra_line = false;
    let mut extra_line_additional_indent = IndentSize::spaces(0);

    let cursor_is_before_end_tag_if_exists = {
        let mut char_position = 0u32;
        let mut end_tag_offset = None;

        'outer: for chunk in snapshot.text_for_range(range) {
            if let Some(byte_pos) = chunk.find(&**end_tag) {
                let chars_before_match = chunk[..byte_pos].chars().count() as u32;
                end_tag_offset = Some(char_position + chars_before_match);
                break 'outer;
            }
            char_position += chunk.chars().count() as u32;
        }

        if let Some(end_tag_offset) = end_tag_offset {
            let cursor_is_before_end_tag = column <= end_tag_offset;
            if cursor_is_after_start_tag {
                if cursor_is_before_end_tag {
                    needs_extra_line = true;
                }
                let cursor_is_at_start_of_end_tag = column == end_tag_offset;
                if cursor_is_at_start_of_end_tag {
                    extra_line_additional_indent.len = *len;
                }
            }
            cursor_is_before_end_tag
        } else {
            true
        }
    };

    if (cursor_is_after_start_tag || cursor_is_after_delimiter)
        && cursor_is_before_end_tag_if_exists
    {
        let additional_indent = if cursor_is_after_start_tag {
            IndentSize::spaces(*len)
        } else {
            IndentSize::spaces(0)
        };

        *newline_config = NewlineConfig::Newline {
            additional_indent,
            extra_line_additional_indent: if needs_extra_line {
                Some(extra_line_additional_indent)
            } else {
                None
            },
            prevent_auto_indent: true,
        };
        Some(delimiter.clone())
    } else {
        None
    }
}

const ORDERED_LIST_MAX_MARKER_LEN: usize = 16;

fn list_delimiter_for_newline(
    start_point: &Point,
    buffer: &MultiBufferSnapshot,
    language: &LanguageScope,
    newline_config: &mut NewlineConfig,
) -> Option<Arc<str>> {
    let (snapshot, range) = buffer.buffer_line_for_row(MultiBufferRow(start_point.row))?;

    let num_of_whitespaces = snapshot
        .chars_for_range(range.clone())
        .take_while(|c| c.is_whitespace())
        .count();

    let task_list_entries: Vec<_> = language
        .task_list()
        .into_iter()
        .flat_map(|config| {
            config
                .prefixes
                .iter()
                .map(|prefix| (prefix.as_ref(), config.continuation.as_ref()))
        })
        .collect();
    let unordered_list_entries: Vec<_> = language
        .unordered_list()
        .iter()
        .map(|marker| (marker.as_ref(), marker.as_ref()))
        .collect();

    let all_entries: Vec<_> = task_list_entries
        .into_iter()
        .chain(unordered_list_entries)
        .collect();

    if let Some(max_prefix_len) = all_entries.iter().map(|(p, _)| p.len()).max() {
        let candidate: String = snapshot
            .chars_for_range(range.clone())
            .skip(num_of_whitespaces)
            .take(max_prefix_len)
            .collect();

        if let Some((prefix, continuation)) = all_entries
            .iter()
            .filter(|(prefix, _)| candidate.starts_with(*prefix))
            .max_by_key(|(prefix, _)| prefix.len())
        {
            let end_of_prefix = num_of_whitespaces + prefix.len();
            let cursor_is_after_prefix = end_of_prefix <= start_point.column as usize;
            let has_content_after_marker = snapshot
                .chars_for_range(range)
                .skip(end_of_prefix)
                .any(|c| !c.is_whitespace());

            if has_content_after_marker && cursor_is_after_prefix {
                return Some((*continuation).into());
            }

            if start_point.column as usize == end_of_prefix {
                if num_of_whitespaces == 0 {
                    *newline_config = NewlineConfig::ClearCurrentLine;
                } else {
                    *newline_config = NewlineConfig::UnindentCurrentLine {
                        continuation: (*continuation).into(),
                    };
                }
            }

            return None;
        }
    }

    let candidate: String = snapshot
        .chars_for_range(range.clone())
        .skip(num_of_whitespaces)
        .take(ORDERED_LIST_MAX_MARKER_LEN)
        .collect();

    for ordered_config in language.ordered_list() {
        let regex = match Regex::new(&ordered_config.pattern) {
            Ok(r) => r,
            Err(_) => continue,
        };

        if let Some(captures) = regex.captures(&candidate) {
            let full_match = captures.get(0)?;
            let marker_len = full_match.len();
            let end_of_prefix = num_of_whitespaces + marker_len;
            let cursor_is_after_prefix = end_of_prefix <= start_point.column as usize;

            let has_content_after_marker = snapshot
                .chars_for_range(range)
                .skip(end_of_prefix)
                .any(|c| !c.is_whitespace());

            if has_content_after_marker && cursor_is_after_prefix {
                let number: u32 = captures.get(1)?.as_str().parse().ok()?;
                let continuation = ordered_config
                    .format
                    .replace("{1}", &(number + 1).to_string());
                return Some(continuation.into());
            }

            if start_point.column as usize == end_of_prefix {
                let continuation = ordered_config.format.replace("{1}", "1");
                if num_of_whitespaces == 0 {
                    *newline_config = NewlineConfig::ClearCurrentLine;
                } else {
                    *newline_config = NewlineConfig::UnindentCurrentLine {
                        continuation: continuation.into(),
                    };
                }
            }

            return None;
        }
    }

    None
}

fn is_list_prefix_row(
    row: MultiBufferRow,
    buffer: &MultiBufferSnapshot,
    language: &LanguageScope,
) -> bool {
    let Some((snapshot, range)) = buffer.buffer_line_for_row(row) else {
        return false;
    };

    let num_of_whitespaces = snapshot
        .chars_for_range(range.clone())
        .take_while(|c| c.is_whitespace())
        .count();

    let task_list_prefixes: Vec<_> = language
        .task_list()
        .into_iter()
        .flat_map(|config| {
            config
                .prefixes
                .iter()
                .map(|p| p.as_ref())
                .collect::<Vec<_>>()
        })
        .collect();
    let unordered_list_markers: Vec<_> = language
        .unordered_list()
        .iter()
        .map(|marker| marker.as_ref())
        .collect();
    let all_prefixes: Vec<_> = task_list_prefixes
        .into_iter()
        .chain(unordered_list_markers)
        .collect();
    if let Some(max_prefix_len) = all_prefixes.iter().map(|p| p.len()).max() {
        let candidate: String = snapshot
            .chars_for_range(range.clone())
            .skip(num_of_whitespaces)
            .take(max_prefix_len)
            .collect();
        if all_prefixes
            .iter()
            .any(|prefix| candidate.starts_with(*prefix))
        {
            return true;
        }
    }

    let ordered_list_candidate: String = snapshot
        .chars_for_range(range)
        .skip(num_of_whitespaces)
        .take(ORDERED_LIST_MAX_MARKER_LEN)
        .collect();
    for ordered_config in language.ordered_list() {
        let regex = match Regex::new(&ordered_config.pattern) {
            Ok(r) => r,
            Err(_) => continue,
        };
        if let Some(captures) = regex.captures(&ordered_list_candidate) {
            return captures.get(0).is_some();
        }
    }

    false
}

#[derive(Debug)]
enum NewlineConfig {
    /// Insert newline with optional additional indent and optional extra blank line
    Newline {
        additional_indent: IndentSize,
        extra_line_additional_indent: Option<IndentSize>,
        prevent_auto_indent: bool,
    },
    /// Clear the current line
    ClearCurrentLine,
    /// Unindent the current line and add continuation
    UnindentCurrentLine { continuation: Arc<str> },
}

impl NewlineConfig {
    fn has_extra_line(&self) -> bool {
        matches!(
            self,
            Self::Newline {
                extra_line_additional_indent: Some(_),
                ..
            }
        )
    }

    fn insert_extra_newline_brackets(
        buffer: &MultiBufferSnapshot,
        range: Range<MultiBufferOffset>,
        language: &language::LanguageScope,
    ) -> bool {
        let leading_whitespace_len = buffer
            .reversed_chars_at(range.start)
            .take_while(|c| c.is_whitespace() && *c != '\n')
            .map(|c| c.len_utf8())
            .sum::<usize>();
        let trailing_whitespace_len = buffer
            .chars_at(range.end)
            .take_while(|c| c.is_whitespace() && *c != '\n')
            .map(|c| c.len_utf8())
            .sum::<usize>();
        let range = range.start - leading_whitespace_len..range.end + trailing_whitespace_len;

        language.brackets().any(|(pair, enabled)| {
            let pair_start = pair.start.trim_end();
            let pair_end = pair.end.trim_start();

            enabled
                && pair.newline
                && buffer.contains_str_at(range.end, pair_end)
                && buffer.contains_str_at(
                    range.start.saturating_sub_usize(pair_start.len()),
                    pair_start,
                )
        })
    }

    fn insert_extra_newline_tree_sitter(
        buffer: &MultiBufferSnapshot,
        range: Range<MultiBufferOffset>,
    ) -> bool {
        let (buffer, range) = match buffer
            .range_to_buffer_ranges(range.start..range.end)
            .as_slice()
        {
            [(buffer_snapshot, range, _)] => (buffer_snapshot.clone(), range.clone()),
            _ => return false,
        };
        let pair = {
            let mut result: Option<BracketMatch<usize>> = None;

            for pair in buffer
                .all_bracket_ranges(range.start.0..range.end.0)
                .filter(move |pair| {
                    pair.open_range.start <= range.start.0 && pair.close_range.end >= range.end.0
                })
            {
                let len = pair.close_range.end - pair.open_range.start;

                if let Some(existing) = &result {
                    let existing_len = existing.close_range.end - existing.open_range.start;
                    if len > existing_len {
                        continue;
                    }
                }

                result = Some(pair);
            }

            result
        };
        let Some(pair) = pair else {
            return false;
        };
        pair.newline_only
            && buffer
                .chars_for_range(pair.open_range.end..range.start.0)
                .chain(buffer.chars_for_range(range.end.0..pair.close_range.start))
                .all(|c| c.is_whitespace() && c != '\n')
    }
}

fn update_uncommitted_diff_for_buffer(
    editor: Entity<Editor>,
    project: &Entity<Project>,
    buffers: impl IntoIterator<Item = Entity<Buffer>>,
    buffer: Entity<MultiBuffer>,
    cx: &mut App,
) -> Task<()> {
    let mut tasks = Vec::new();
    project.update(cx, |project, cx| {
        for buffer in buffers {
            if project::File::from_dyn(buffer.read(cx).file()).is_some() {
                tasks.push(project.open_uncommitted_diff(buffer.clone(), cx))
            }
        }
    });
    cx.spawn(async move |cx| {
        let diffs = future::join_all(tasks).await;
        if editor.read_with(cx, |editor, _cx| editor.temporary_diff_override) {
            return;
        }

        buffer.update(cx, |buffer, cx| {
            for diff in diffs.into_iter().flatten() {
                buffer.add_diff(diff, cx);
            }
        });
    })
}

fn char_len_with_expanded_tabs(offset: usize, text: &str, tab_size: NonZeroU32) -> usize {
    let tab_size = tab_size.get() as usize;
    let mut width = offset;

    for ch in text.chars() {
        width += if ch == '\t' {
            tab_size - (width % tab_size)
        } else {
            1
        };
    }

    width - offset
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_string_size_with_expanded_tabs() {
        let nz = |val| NonZeroU32::new(val).unwrap();
        assert_eq!(char_len_with_expanded_tabs(0, "", nz(4)), 0);
        assert_eq!(char_len_with_expanded_tabs(0, "hello", nz(4)), 5);
        assert_eq!(char_len_with_expanded_tabs(0, "\thello", nz(4)), 9);
        assert_eq!(char_len_with_expanded_tabs(0, "abc\tab", nz(4)), 6);
        assert_eq!(char_len_with_expanded_tabs(0, "hello\t", nz(4)), 8);
        assert_eq!(char_len_with_expanded_tabs(0, "\t\t", nz(8)), 16);
        assert_eq!(char_len_with_expanded_tabs(0, "x\t", nz(8)), 8);
        assert_eq!(char_len_with_expanded_tabs(7, "x\t", nz(8)), 9);
    }
}

/// Tokenizes a string into runs of text that should stick together, or that is whitespace.
struct WordBreakingTokenizer<'a> {
    input: &'a str,
}

impl<'a> WordBreakingTokenizer<'a> {
    fn new(input: &'a str) -> Self {
        Self { input }
    }
}

fn is_char_ideographic(ch: char) -> bool {
    use unicode_script::Script::*;
    use unicode_script::UnicodeScript;
    matches!(ch.script(), Han | Tangut | Yi)
}

fn is_grapheme_ideographic(text: &str) -> bool {
    text.chars().any(is_char_ideographic)
}

fn is_grapheme_whitespace(text: &str) -> bool {
    text.chars().any(|x| x.is_whitespace())
}

fn should_stay_with_preceding_ideograph(text: &str) -> bool {
    text.chars()
        .next()
        .is_some_and(|ch| matches!(ch, '。' | '、' | '，' | '？' | '！' | '：' | '；' | '…'))
}

#[derive(PartialEq, Eq, Debug, Clone, Copy)]
enum WordBreakToken<'a> {
    Word { token: &'a str, grapheme_len: usize },
    InlineWhitespace { token: &'a str, grapheme_len: usize },
    Newline,
}

impl<'a> Iterator for WordBreakingTokenizer<'a> {
    /// Yields a span, the count of graphemes in the token, and whether it was
    /// whitespace. Note that it also breaks at word boundaries.
    type Item = WordBreakToken<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        use unicode_segmentation::UnicodeSegmentation;
        if self.input.is_empty() {
            return None;
        }

        let mut iter = self.input.graphemes(true).peekable();
        let mut offset = 0;
        let mut grapheme_len = 0;
        if let Some(first_grapheme) = iter.next() {
            let is_newline = first_grapheme == "\n";
            let is_whitespace = is_grapheme_whitespace(first_grapheme);
            offset += first_grapheme.len();
            grapheme_len += 1;
            if is_grapheme_ideographic(first_grapheme) && !is_whitespace {
                if let Some(grapheme) = iter.peek().copied()
                    && should_stay_with_preceding_ideograph(grapheme)
                {
                    offset += grapheme.len();
                    grapheme_len += 1;
                }
            } else {
                let mut words = self.input[offset..].split_word_bound_indices().peekable();
                let mut next_word_bound = words.peek().copied();
                if next_word_bound.is_some_and(|(i, _)| i == 0) {
                    next_word_bound = words.next();
                }
                while let Some(grapheme) = iter.peek().copied() {
                    if next_word_bound.is_some_and(|(i, _)| i == offset) {
                        break;
                    };
                    if is_grapheme_whitespace(grapheme) != is_whitespace
                        || (grapheme == "\n") != is_newline
                    {
                        break;
                    };
                    offset += grapheme.len();
                    grapheme_len += 1;
                    iter.next();
                }
            }
            let token = &self.input[..offset];
            self.input = &self.input[offset..];
            if token == "\n" {
                Some(WordBreakToken::Newline)
            } else if is_whitespace {
                Some(WordBreakToken::InlineWhitespace {
                    token,
                    grapheme_len,
                })
            } else {
                Some(WordBreakToken::Word {
                    token,
                    grapheme_len,
                })
            }
        } else {
            None
        }
    }
}

#[test]
fn test_word_breaking_tokenizer() {
    let tests: &[(&str, &[WordBreakToken<'static>])] = &[
        ("", &[]),
        ("  ", &[whitespace("  ", 2)]),
        ("Ʒ", &[word("Ʒ", 1)]),
        ("Ǽ", &[word("Ǽ", 1)]),
        ("⋑", &[word("⋑", 1)]),
        ("⋑⋑", &[word("⋑⋑", 2)]),
        (
            "原理，进而",
            &[word("原", 1), word("理，", 2), word("进", 1), word("而", 1)],
        ),
        (
            "hello world",
            &[word("hello", 5), whitespace(" ", 1), word("world", 5)],
        ),
        (
            "hello, world",
            &[word("hello,", 6), whitespace(" ", 1), word("world", 5)],
        ),
        (
            "  hello world",
            &[
                whitespace("  ", 2),
                word("hello", 5),
                whitespace(" ", 1),
                word("world", 5),
            ],
        ),
        (
            "这是什么 \n 钢笔",
            &[
                word("这", 1),
                word("是", 1),
                word("什", 1),
                word("么", 1),
                whitespace(" ", 1),
                newline(),
                whitespace(" ", 1),
                word("钢", 1),
                word("笔", 1),
            ],
        ),
        (" mutton", &[whitespace(" ", 1), word("mutton", 6)]),
    ];

    fn word(token: &'static str, grapheme_len: usize) -> WordBreakToken<'static> {
        WordBreakToken::Word {
            token,
            grapheme_len,
        }
    }

    fn whitespace(token: &'static str, grapheme_len: usize) -> WordBreakToken<'static> {
        WordBreakToken::InlineWhitespace {
            token,
            grapheme_len,
        }
    }

    fn newline() -> WordBreakToken<'static> {
        WordBreakToken::Newline
    }

    for (input, result) in tests {
        assert_eq!(
            WordBreakingTokenizer::new(input)
                .collect::<Vec<_>>()
                .as_slice(),
            *result,
        );
    }
}

fn wrap_with_prefix(
    first_line_prefix: String,
    subsequent_lines_prefix: String,
    unwrapped_text: String,
    wrap_column: usize,
    tab_size: NonZeroU32,
    preserve_existing_whitespace: bool,
) -> String {
    let first_line_prefix_len = char_len_with_expanded_tabs(0, &first_line_prefix, tab_size);
    let subsequent_lines_prefix_len =
        char_len_with_expanded_tabs(0, &subsequent_lines_prefix, tab_size);
    let mut wrapped_text = String::new();
    let mut current_line = first_line_prefix;
    let mut is_first_line = true;

    let tokenizer = WordBreakingTokenizer::new(&unwrapped_text);
    let mut current_line_len = first_line_prefix_len;
    let mut in_whitespace = false;
    for token in tokenizer {
        let have_preceding_whitespace = in_whitespace;
        match token {
            WordBreakToken::Word {
                token,
                grapheme_len,
            } => {
                in_whitespace = false;
                let current_prefix_len = if is_first_line {
                    first_line_prefix_len
                } else {
                    subsequent_lines_prefix_len
                };
                if current_line_len + grapheme_len > wrap_column
                    && current_line_len != current_prefix_len
                {
                    wrapped_text.push_str(current_line.trim_end());
                    wrapped_text.push('\n');
                    is_first_line = false;
                    current_line = subsequent_lines_prefix.clone();
                    current_line_len = subsequent_lines_prefix_len;
                }
                current_line.push_str(token);
                current_line_len += grapheme_len;
            }
            WordBreakToken::InlineWhitespace {
                mut token,
                mut grapheme_len,
            } => {
                in_whitespace = true;
                if have_preceding_whitespace && !preserve_existing_whitespace {
                    continue;
                }
                if !preserve_existing_whitespace {
                    // Keep a single whitespace grapheme as-is
                    if let Some(first) =
                        unicode_segmentation::UnicodeSegmentation::graphemes(token, true).next()
                    {
                        token = first;
                    } else {
                        token = " ";
                    }
                    grapheme_len = 1;
                }
                let current_prefix_len = if is_first_line {
                    first_line_prefix_len
                } else {
                    subsequent_lines_prefix_len
                };
                if current_line_len + grapheme_len > wrap_column {
                    wrapped_text.push_str(current_line.trim_end());
                    wrapped_text.push('\n');
                    is_first_line = false;
                    current_line = subsequent_lines_prefix.clone();
                    current_line_len = subsequent_lines_prefix_len;
                } else if current_line_len != current_prefix_len || preserve_existing_whitespace {
                    current_line.push_str(token);
                    current_line_len += grapheme_len;
                }
            }
            WordBreakToken::Newline => {
                in_whitespace = true;
                let current_prefix_len = if is_first_line {
                    first_line_prefix_len
                } else {
                    subsequent_lines_prefix_len
                };
                if preserve_existing_whitespace {
                    wrapped_text.push_str(current_line.trim_end());
                    wrapped_text.push('\n');
                    is_first_line = false;
                    current_line = subsequent_lines_prefix.clone();
                    current_line_len = subsequent_lines_prefix_len;
                } else if have_preceding_whitespace {
                    continue;
                } else if current_line_len + 1 > wrap_column
                    && current_line_len != current_prefix_len
                {
                    wrapped_text.push_str(current_line.trim_end());
                    wrapped_text.push('\n');
                    is_first_line = false;
                    current_line = subsequent_lines_prefix.clone();
                    current_line_len = subsequent_lines_prefix_len;
                } else if current_line_len != current_prefix_len {
                    current_line.push(' ');
                    current_line_len += 1;
                }
            }
        }
    }

    if !current_line.is_empty() {
        wrapped_text.push_str(&current_line);
    }
    wrapped_text
}

#[test]
fn test_wrap_with_prefix() {
    assert_eq!(
        wrap_with_prefix(
            "# ".to_string(),
            "# ".to_string(),
            "abcdefg".to_string(),
            4,
            NonZeroU32::new(4).unwrap(),
            false,
        ),
        "# abcdefg"
    );
    assert_eq!(
        wrap_with_prefix(
            "".to_string(),
            "".to_string(),
            "\thello world".to_string(),
            8,
            NonZeroU32::new(4).unwrap(),
            false,
        ),
        "hello\nworld"
    );
    assert_eq!(
        wrap_with_prefix(
            "// ".to_string(),
            "// ".to_string(),
            "xx \nyy zz aa bb cc".to_string(),
            12,
            NonZeroU32::new(4).unwrap(),
            false,
        ),
        "// xx yy zz\n// aa bb cc"
    );
    assert_eq!(
        wrap_with_prefix(
            String::new(),
            String::new(),
            "这是什么 \n 钢笔".to_string(),
            3,
            NonZeroU32::new(4).unwrap(),
            false,
        ),
        "这是什\n么 钢\n笔"
    );
    assert_eq!(
        wrap_with_prefix(
            String::new(),
            String::new(),
            format!("foo{}bar", '\u{2009}'), // thin space
            80,
            NonZeroU32::new(4).unwrap(),
            false,
        ),
        format!("foo{}bar", '\u{2009}')
    );
}

pub trait CollaborationHub {
    fn collaborators<'a>(&self, cx: &'a App) -> &'a HashMap<PeerId, Collaborator>;
    fn user_participant_indices<'a>(&self, cx: &'a App) -> &'a HashMap<u64, ParticipantIndex>;
    fn user_names(&self, cx: &App) -> HashMap<u64, SharedString>;
}

impl CollaborationHub for Entity<Project> {
    fn collaborators<'a>(&self, cx: &'a App) -> &'a HashMap<PeerId, Collaborator> {
        self.read(cx).collaborators()
    }

    fn user_participant_indices<'a>(&self, cx: &'a App) -> &'a HashMap<u64, ParticipantIndex> {
        self.read(cx).user_store().read(cx).participant_indices()
    }

    fn user_names(&self, cx: &App) -> HashMap<u64, SharedString> {
        let this = self.read(cx);
        let user_ids = this.collaborators().values().map(|c| c.user_id);
        this.user_store().read(cx).participant_names(user_ids, cx)
    }
}

pub trait SemanticsProvider {
    fn hover(
        &self,
        buffer: &Entity<Buffer>,
        position: text::Anchor,
        cx: &mut App,
    ) -> Option<Task<Option<Vec<project::Hover>>>>;

    fn inline_values(
        &self,
        buffer_handle: Entity<Buffer>,
        range: Range<text::Anchor>,
        cx: &mut App,
    ) -> Option<Task<anyhow::Result<Vec<InlayHint>>>>;

    fn applicable_inlay_chunks(
        &self,
        buffer: &Entity<Buffer>,
        ranges: &[Range<text::Anchor>],
        cx: &mut App,
    ) -> Vec<Range<BufferRow>>;

    fn invalidate_inlay_hints(&self, for_buffers: &HashSet<BufferId>, cx: &mut App);

    fn inlay_hints(
        &self,
        invalidate: InvalidationStrategy,
        buffer: Entity<Buffer>,
        ranges: Vec<Range<text::Anchor>>,
        known_chunks: Option<(clock::Global, HashSet<Range<BufferRow>>)>,
        cx: &mut App,
    ) -> Option<HashMap<Range<BufferRow>, Task<Result<CacheInlayHints>>>>;

    fn semantic_tokens(
        &self,
        buffer: Entity<Buffer>,
        refresh: Option<RefreshForServer>,
        cx: &mut App,
    ) -> Option<Shared<Task<std::result::Result<BufferSemanticTokens, Arc<anyhow::Error>>>>>;

    fn supports_inlay_hints(&self, buffer: &Entity<Buffer>, cx: &mut App) -> bool;

    fn supports_semantic_tokens(&self, buffer: &Entity<Buffer>, cx: &mut App) -> bool;

    fn document_highlights(
        &self,
        buffer: &Entity<Buffer>,
        position: text::Anchor,
        cx: &mut App,
    ) -> Option<Task<Result<Vec<DocumentHighlight>>>>;

    fn definitions(
        &self,
        buffer: &Entity<Buffer>,
        position: text::Anchor,
        kind: GotoDefinitionKind,
        cx: &mut App,
    ) -> Option<Task<Result<Option<Vec<LocationLink>>>>>;

    fn range_for_rename(
        &self,
        buffer: &Entity<Buffer>,
        position: text::Anchor,
        cx: &mut App,
    ) -> Task<Result<Option<Range<text::Anchor>>>>;

    fn perform_rename(
        &self,
        buffer: &Entity<Buffer>,
        position: text::Anchor,
        new_name: String,
        cx: &mut App,
    ) -> Option<Task<Result<ProjectTransaction>>>;
}

pub trait CompletionProvider {
    fn completions(
        &self,
        buffer: &Entity<Buffer>,
        buffer_position: text::Anchor,
        trigger: CompletionContext,
        window: &mut Window,
        cx: &mut Context<Editor>,
    ) -> Task<Result<Vec<CompletionResponse>>>;

    fn resolve_completions(
        &self,
        _buffer: Entity<Buffer>,
        _completion_indices: Vec<usize>,
        _completions: Rc<RefCell<Box<[Completion]>>>,
        _cx: &mut Context<Editor>,
    ) -> Task<Result<bool>> {
        Task::ready(Ok(false))
    }

    fn apply_additional_edits_for_completion(
        &self,
        _buffer: Entity<Buffer>,
        _completions: Rc<RefCell<Box<[Completion]>>>,
        _completion_index: usize,
        _push_to_history: bool,
        _all_commit_ranges: Vec<Range<language::Anchor>>,
        _cx: &mut Context<Editor>,
    ) -> Task<Result<Option<language::Transaction>>> {
        Task::ready(Ok(None))
    }

    fn is_completion_trigger(
        &self,
        buffer: &Entity<Buffer>,
        position: language::Anchor,
        text: &str,
        trigger_in_words: bool,
        cx: &mut Context<Editor>,
    ) -> bool;

    fn selection_changed(&self, _mat: Option<&StringMatch>, _window: &mut Window, _cx: &mut App) {}

    fn sort_completions(&self) -> bool {
        true
    }

    fn filter_completions(&self) -> bool {
        true
    }

    fn show_snippets(&self) -> bool {
        false
    }
}

pub trait CodeActionProvider {
    fn id(&self) -> Arc<str>;

    fn code_actions(
        &self,
        buffer: &Entity<Buffer>,
        range: Range<text::Anchor>,
        window: &mut Window,
        cx: &mut App,
    ) -> Task<Result<Vec<CodeAction>>>;

    fn apply_code_action(
        &self,
        buffer_handle: Entity<Buffer>,
        action: CodeAction,
        push_to_history: bool,
        window: &mut Window,
        cx: &mut App,
    ) -> Task<Result<ProjectTransaction>>;
}

impl CodeActionProvider for Entity<Project> {
    fn id(&self) -> Arc<str> {
        "project".into()
    }

    fn code_actions(
        &self,
        buffer: &Entity<Buffer>,
        range: Range<text::Anchor>,
        _window: &mut Window,
        cx: &mut App,
    ) -> Task<Result<Vec<CodeAction>>> {
        self.update(cx, |project, cx| {
            let code_lens_actions = project.code_lens_actions(buffer, range.clone(), cx);
            let code_actions = project.code_actions(buffer, range, None, cx);
            cx.background_spawn(async move {
                let (code_lens_actions, code_actions) = join(code_lens_actions, code_actions).await;
                Ok(code_lens_actions
                    .context("code lens fetch")?
                    .into_iter()
                    .flatten()
                    .chain(
                        code_actions
                            .context("code action fetch")?
                            .into_iter()
                            .flatten(),
                    )
                    .collect())
            })
        })
    }

    fn apply_code_action(
        &self,
        buffer_handle: Entity<Buffer>,
        action: CodeAction,
        push_to_history: bool,
        _window: &mut Window,
        cx: &mut App,
    ) -> Task<Result<ProjectTransaction>> {
        self.update(cx, |project, cx| {
            project.apply_code_action(buffer_handle, action, push_to_history, cx)
        })
    }
}

fn snippet_completions(
    project: &Project,
    buffer: &Entity<Buffer>,
    buffer_anchor: text::Anchor,
    classifier: CharClassifier,
    cx: &mut App,
) -> Task<Result<CompletionResponse>> {
    let languages = buffer.read(cx).languages_at(buffer_anchor);
    let snippet_store = project.snippets().read(cx);

    let scopes: Vec<_> = languages
        .iter()
        .filter_map(|language| {
            let language_name = language.lsp_id();
            let snippets = snippet_store.snippets_for(Some(language_name), cx);

            if snippets.is_empty() {
                None
            } else {
                Some((language.default_scope(), snippets))
            }
        })
        .collect();

    if scopes.is_empty() {
        return Task::ready(Ok(CompletionResponse {
            completions: vec![],
            display_options: CompletionDisplayOptions::default(),
            is_incomplete: false,
        }));
    }

    let snapshot = buffer.read(cx).text_snapshot();
    let executor = cx.background_executor().clone();

    cx.background_spawn(async move {
        let is_word_char = |c| classifier.is_word(c);

        let mut is_incomplete = false;
        let mut completions: Vec<Completion> = Vec::new();

        const MAX_PREFIX_LEN: usize = 128;
        let buffer_offset = text::ToOffset::to_offset(&buffer_anchor, &snapshot);
        let window_start = buffer_offset.saturating_sub(MAX_PREFIX_LEN);
        let window_start = snapshot.clip_offset(window_start, Bias::Left);

        let max_buffer_window: String = snapshot
            .text_for_range(window_start..buffer_offset)
            .collect();

        if max_buffer_window.is_empty() {
            return Ok(CompletionResponse {
                completions: vec![],
                display_options: CompletionDisplayOptions::default(),
                is_incomplete: true,
            });
        }

        for (_scope, snippets) in scopes.into_iter() {
            // Sort snippets by word count to match longer snippet prefixes first.
            let mut sorted_snippet_candidates = snippets
                .iter()
                .enumerate()
                .flat_map(|(snippet_ix, snippet)| {
                    snippet
                        .prefix
                        .iter()
                        .enumerate()
                        .map(move |(prefix_ix, prefix)| {
                            let word_count =
                                snippet_candidate_suffixes(prefix, &is_word_char).count();
                            ((snippet_ix, prefix_ix), prefix, word_count)
                        })
                })
                .collect_vec();
            sorted_snippet_candidates
                .sort_unstable_by_key(|(_, _, word_count)| Reverse(*word_count));

            // Each prefix may be matched multiple times; the completion menu must filter out duplicates.

            let buffer_windows = snippet_candidate_suffixes(&max_buffer_window, &is_word_char)
                .take(
                    sorted_snippet_candidates
                        .first()
                        .map(|(_, _, word_count)| *word_count)
                        .unwrap_or_default(),
                )
                .collect_vec();

            const MAX_RESULTS: usize = 100;
            // Each match also remembers how many characters from the buffer it consumed
            let mut matches: Vec<(StringMatch, usize)> = vec![];

            let mut snippet_list_cutoff_index = 0;
            for (buffer_index, buffer_window) in buffer_windows.iter().enumerate().rev() {
                let word_count = buffer_index + 1;
                // Increase `snippet_list_cutoff_index` until we have all of the
                // snippets with sufficiently many words.
                while sorted_snippet_candidates
                    .get(snippet_list_cutoff_index)
                    .is_some_and(|(_ix, _prefix, snippet_word_count)| {
                        *snippet_word_count >= word_count
                    })
                {
                    snippet_list_cutoff_index += 1;
                }

                // Take only the candidates with at least `word_count` many words
                let snippet_candidates_at_word_len =
                    &sorted_snippet_candidates[..snippet_list_cutoff_index];

                let candidates = snippet_candidates_at_word_len
                    .iter()
                    .map(|(_snippet_ix, prefix, _snippet_word_count)| prefix)
                    .enumerate() // index in `sorted_snippet_candidates`
                    // First char must match
                    .filter(|(_ix, prefix)| {
                        itertools::equal(
                            prefix
                                .chars()
                                .next()
                                .into_iter()
                                .flat_map(|c| c.to_lowercase()),
                            buffer_window
                                .chars()
                                .next()
                                .into_iter()
                                .flat_map(|c| c.to_lowercase()),
                        )
                    })
                    .map(|(ix, prefix)| StringMatchCandidate::new(ix, prefix))
                    .collect::<Vec<StringMatchCandidate>>();

                matches.extend(
                    fuzzy::match_strings(
                        &candidates,
                        &buffer_window,
                        buffer_window.chars().any(|c| c.is_uppercase()),
                        true,
                        MAX_RESULTS - matches.len(), // always prioritize longer snippets
                        &Default::default(),
                        executor.clone(),
                    )
                    .await
                    .into_iter()
                    .map(|string_match| (string_match, buffer_window.len())),
                );

                if matches.len() >= MAX_RESULTS {
                    break;
                }
            }

            let to_lsp = |point: &text::Anchor| {
                let end = text::ToPointUtf16::to_point_utf16(point, &snapshot);
                point_to_lsp(end)
            };
            let lsp_end = to_lsp(&buffer_anchor);

            if matches.len() >= MAX_RESULTS {
                is_incomplete = true;
            }

            completions.extend(matches.iter().map(|(string_match, buffer_window_len)| {
                let ((snippet_index, prefix_index), matching_prefix, _snippet_word_count) =
                    sorted_snippet_candidates[string_match.candidate_id];
                let snippet = &snippets[snippet_index];
                let start = buffer_offset - buffer_window_len;
                let start = snapshot.anchor_before(start);
                let range = start..buffer_anchor;
                let lsp_start = to_lsp(&start);
                let lsp_range = lsp::Range {
                    start: lsp_start,
                    end: lsp_end,
                };
                Completion {
                    replace_range: range,
                    new_text: snippet.body.clone(),
                    source: CompletionSource::Lsp {
                        insert_range: None,
                        server_id: LanguageServerId(usize::MAX),
                        resolved: true,
                        lsp_completion: Box::new(lsp::CompletionItem {
                            label: snippet.prefix.first().unwrap().clone(),
                            kind: Some(CompletionItemKind::SNIPPET),
                            label_details: snippet.description.as_ref().map(|description| {
                                lsp::CompletionItemLabelDetails {
                                    detail: Some(description.clone()),
                                    description: None,
                                }
                            }),
                            insert_text_format: Some(InsertTextFormat::SNIPPET),
                            text_edit: Some(lsp::CompletionTextEdit::InsertAndReplace(
                                lsp::InsertReplaceEdit {
                                    new_text: snippet.body.clone(),
                                    insert: lsp_range,
                                    replace: lsp_range,
                                },
                            )),
                            filter_text: Some(snippet.body.clone()),
                            sort_text: Some(char::MAX.to_string()),
                            ..lsp::CompletionItem::default()
                        }),
                        lsp_defaults: None,
                    },
                    label: CodeLabel {
                        text: matching_prefix.clone(),
                        runs: Vec::new(),
                        filter_range: 0..matching_prefix.len(),
                    },
                    icon_path: None,
                    documentation: Some(CompletionDocumentation::SingleLineAndMultiLinePlainText {
                        single_line: snippet.name.clone().into(),
                        plain_text: snippet
                            .description
                            .clone()
                            .map(|description| description.into()),
                    }),
                    insert_text_mode: None,
                    confirm: None,
                    match_start: Some(start),
                    snippet_deduplication_key: Some((snippet_index, prefix_index)),
                }
            }));
        }

        Ok(CompletionResponse {
            completions,
            display_options: CompletionDisplayOptions::default(),
            is_incomplete,
        })
    })
}

impl CompletionProvider for Entity<Project> {
    fn completions(
        &self,
        buffer: &Entity<Buffer>,
        buffer_position: text::Anchor,
        options: CompletionContext,
        _window: &mut Window,
        cx: &mut Context<Editor>,
    ) -> Task<Result<Vec<CompletionResponse>>> {
        self.update(cx, |project, cx| {
            let task = project.completions(buffer, buffer_position, options, cx);
            cx.background_spawn(task)
        })
    }

    fn resolve_completions(
        &self,
        buffer: Entity<Buffer>,
        completion_indices: Vec<usize>,
        completions: Rc<RefCell<Box<[Completion]>>>,
        cx: &mut Context<Editor>,
    ) -> Task<Result<bool>> {
        self.update(cx, |project, cx| {
            project.lsp_store().update(cx, |lsp_store, cx| {
                lsp_store.resolve_completions(buffer, completion_indices, completions, cx)
            })
        })
    }

    fn apply_additional_edits_for_completion(
        &self,
        buffer: Entity<Buffer>,
        completions: Rc<RefCell<Box<[Completion]>>>,
        completion_index: usize,
        push_to_history: bool,
        all_commit_ranges: Vec<Range<language::Anchor>>,
        cx: &mut Context<Editor>,
    ) -> Task<Result<Option<language::Transaction>>> {
        self.update(cx, |project, cx| {
            project.lsp_store().update(cx, |lsp_store, cx| {
                lsp_store.apply_additional_edits_for_completion(
                    buffer,
                    completions,
                    completion_index,
                    push_to_history,
                    all_commit_ranges,
                    cx,
                )
            })
        })
    }

    fn is_completion_trigger(
        &self,
        buffer: &Entity<Buffer>,
        position: language::Anchor,
        text: &str,
        trigger_in_words: bool,
        cx: &mut Context<Editor>,
    ) -> bool {
        let mut chars = text.chars();
        let char = if let Some(char) = chars.next() {
            char
        } else {
            return false;
        };
        if chars.next().is_some() {
            return false;
        }

        let buffer = buffer.read(cx);
        let snapshot = buffer.snapshot();
        let classifier = snapshot
            .char_classifier_at(position)
            .scope_context(Some(CharScopeContext::Completion));
        if trigger_in_words && classifier.is_word(char) {
            return true;
        }

        buffer.completion_triggers().contains(text)
    }

    fn show_snippets(&self) -> bool {
        true
    }
}

impl SemanticsProvider for WeakEntity<Project> {
    fn hover(
        &self,
        buffer: &Entity<Buffer>,
        position: text::Anchor,
        cx: &mut App,
    ) -> Option<Task<Option<Vec<project::Hover>>>> {
        self.update(cx, |project, cx| project.hover(buffer, position, cx))
            .ok()
    }

    fn document_highlights(
        &self,
        buffer: &Entity<Buffer>,
        position: text::Anchor,
        cx: &mut App,
    ) -> Option<Task<Result<Vec<DocumentHighlight>>>> {
        self.update(cx, |project, cx| {
            project.document_highlights(buffer, position, cx)
        })
        .ok()
    }

    fn definitions(
        &self,
        buffer: &Entity<Buffer>,
        position: text::Anchor,
        kind: GotoDefinitionKind,
        cx: &mut App,
    ) -> Option<Task<Result<Option<Vec<LocationLink>>>>> {
        self.update(cx, |project, cx| match kind {
            GotoDefinitionKind::Symbol => project.definitions(buffer, position, cx),
            GotoDefinitionKind::Declaration => project.declarations(buffer, position, cx),
            GotoDefinitionKind::Type => project.type_definitions(buffer, position, cx),
            GotoDefinitionKind::Implementation => project.implementations(buffer, position, cx),
        })
        .ok()
    }

    fn supports_inlay_hints(&self, buffer: &Entity<Buffer>, cx: &mut App) -> bool {
        self.update(cx, |project, cx| {
            if project
                .active_debug_session(cx)
                .is_some_and(|(session, _)| session.read(cx).any_stopped_thread())
            {
                return true;
            }

            buffer.update(cx, |buffer, cx| {
                project.any_language_server_supports_inlay_hints(buffer, cx)
            })
        })
        .unwrap_or(false)
    }

    fn supports_semantic_tokens(&self, buffer: &Entity<Buffer>, cx: &mut App) -> bool {
        self.update(cx, |project, cx| {
            buffer.update(cx, |buffer, cx| {
                project.any_language_server_supports_semantic_tokens(buffer, cx)
            })
        })
        .unwrap_or(false)
    }

    fn inline_values(
        &self,
        buffer_handle: Entity<Buffer>,
        range: Range<text::Anchor>,
        cx: &mut App,
    ) -> Option<Task<anyhow::Result<Vec<InlayHint>>>> {
        self.update(cx, |project, cx| {
            let (session, active_stack_frame) = project.active_debug_session(cx)?;

            Some(project.inline_values(session, active_stack_frame, buffer_handle, range, cx))
        })
        .ok()
        .flatten()
    }

    fn applicable_inlay_chunks(
        &self,
        buffer: &Entity<Buffer>,
        ranges: &[Range<text::Anchor>],
        cx: &mut App,
    ) -> Vec<Range<BufferRow>> {
        self.update(cx, |project, cx| {
            project.lsp_store().update(cx, |lsp_store, cx| {
                lsp_store.applicable_inlay_chunks(buffer, ranges, cx)
            })
        })
        .unwrap_or_default()
    }

    fn invalidate_inlay_hints(&self, for_buffers: &HashSet<BufferId>, cx: &mut App) {
        self.update(cx, |project, cx| {
            project.lsp_store().update(cx, |lsp_store, _| {
                lsp_store.invalidate_inlay_hints(for_buffers)
            })
        })
        .ok();
    }

    fn inlay_hints(
        &self,
        invalidate: InvalidationStrategy,
        buffer: Entity<Buffer>,
        ranges: Vec<Range<text::Anchor>>,
        known_chunks: Option<(clock::Global, HashSet<Range<BufferRow>>)>,
        cx: &mut App,
    ) -> Option<HashMap<Range<BufferRow>, Task<Result<CacheInlayHints>>>> {
        self.update(cx, |project, cx| {
            project.lsp_store().update(cx, |lsp_store, cx| {
                lsp_store.inlay_hints(invalidate, buffer, ranges, known_chunks, cx)
            })
        })
        .ok()
    }

    fn semantic_tokens(
        &self,
        buffer: Entity<Buffer>,
        refresh: Option<RefreshForServer>,
        cx: &mut App,
    ) -> Option<Shared<Task<std::result::Result<BufferSemanticTokens, Arc<anyhow::Error>>>>> {
        self.update(cx, |this, cx| {
            this.lsp_store().update(cx, |lsp_store, cx| {
                lsp_store.semantic_tokens(buffer, refresh, cx)
            })
        })
        .ok()
    }

    fn range_for_rename(
        &self,
        buffer: &Entity<Buffer>,
        position: text::Anchor,
        cx: &mut App,
    ) -> Task<Result<Option<Range<text::Anchor>>>> {
        let Some(this) = self.upgrade() else {
            return Task::ready(Ok(None));
        };

        this.update(cx, |project, cx| {
            let buffer = buffer.clone();
            let task = project.prepare_rename(buffer.clone(), position, cx);
            cx.spawn(async move |_, cx| {
                Ok(match task.await? {
                    PrepareRenameResponse::Success(range) => Some(range),
                    PrepareRenameResponse::InvalidPosition => None,
                    PrepareRenameResponse::OnlyUnpreparedRenameSupported => {
                        // Fallback on using TreeSitter info to determine identifier range
                        buffer.read_with(cx, |buffer, _| {
                            let snapshot = buffer.snapshot();
                            let (range, kind) = snapshot.surrounding_word(position, None);
                            if kind != Some(CharKind::Word) {
                                return None;
                            }
                            Some(
                                snapshot.anchor_before(range.start)
                                    ..snapshot.anchor_after(range.end),
                            )
                        })
                    }
                })
            })
        })
    }

    fn perform_rename(
        &self,
        buffer: &Entity<Buffer>,
        position: text::Anchor,
        new_name: String,
        cx: &mut App,
    ) -> Option<Task<Result<ProjectTransaction>>> {
        self.update(cx, |project, cx| {
            project.perform_rename(buffer.clone(), position, new_name, cx)
        })
        .ok()
    }
}

fn consume_contiguous_rows(
    contiguous_row_selections: &mut Vec<Selection<Point>>,
    selection: &Selection<Point>,
    display_map: &DisplaySnapshot,
    selections: &mut Peekable<std::slice::Iter<Selection<Point>>>,
) -> (MultiBufferRow, MultiBufferRow) {
    contiguous_row_selections.push(selection.clone());
    let start_row = starting_row(selection, display_map);
    let mut end_row = ending_row(selection, display_map);

    while let Some(next_selection) = selections.peek() {
        if next_selection.start.row <= end_row.0 {
            end_row = ending_row(next_selection, display_map);
            contiguous_row_selections.push(selections.next().unwrap().clone());
        } else {
            break;
        }
    }
    (start_row, end_row)
}

fn starting_row(selection: &Selection<Point>, display_map: &DisplaySnapshot) -> MultiBufferRow {
    if selection.start.column > 0 {
        MultiBufferRow(display_map.prev_line_boundary(selection.start).0.row)
    } else {
        MultiBufferRow(selection.start.row)
    }
}

fn ending_row(next_selection: &Selection<Point>, display_map: &DisplaySnapshot) -> MultiBufferRow {
    if next_selection.end.column > 0 || next_selection.is_empty() {
        MultiBufferRow(display_map.next_line_boundary(next_selection.end).0.row + 1)
    } else {
        MultiBufferRow(next_selection.end.row)
    }
}

impl EditorSnapshot {
    pub fn remote_selections_in_range<'a>(
        &'a self,
        range: &'a Range<Anchor>,
        collaboration_hub: &dyn CollaborationHub,
        cx: &'a App,
    ) -> impl 'a + Iterator<Item = RemoteSelection> {
        let participant_names = collaboration_hub.user_names(cx);
        let participant_indices = collaboration_hub.user_participant_indices(cx);
        let collaborators_by_peer_id = collaboration_hub.collaborators(cx);
        let collaborators_by_replica_id = collaborators_by_peer_id
            .values()
            .map(|collaborator| (collaborator.replica_id, collaborator))
            .collect::<HashMap<_, _>>();
        self.buffer_snapshot()
            .selections_in_range(range, false)
            .filter_map(move |(replica_id, line_mode, cursor_shape, selection)| {
                if replica_id == ReplicaId::AGENT {
                    Some(RemoteSelection {
                        replica_id,
                        selection,
                        cursor_shape,
                        line_mode,
                        collaborator_id: CollaboratorId::Agent,
                        user_name: Some("Agent".into()),
                        color: cx.theme().players().agent(),
                    })
                } else {
                    let collaborator = collaborators_by_replica_id.get(&replica_id)?;
                    let participant_index = participant_indices.get(&collaborator.user_id).copied();
                    let user_name = participant_names.get(&collaborator.user_id).cloned();
                    Some(RemoteSelection {
                        replica_id,
                        selection,
                        cursor_shape,
                        line_mode,
                        collaborator_id: CollaboratorId::PeerId(collaborator.peer_id),
                        user_name,
                        color: if let Some(index) = participant_index {
                            cx.theme().players().color_for_participant(index.0)
                        } else {
                            cx.theme().players().absent()
                        },
                    })
                }
            })
    }

    pub fn hunks_for_ranges(
        &self,
        ranges: impl IntoIterator<Item = Range<Point>>,
    ) -> Vec<MultiBufferDiffHunk> {
        let mut hunks = Vec::new();
        let mut processed_buffer_rows: HashMap<BufferId, HashSet<Range<text::Anchor>>> =
            HashMap::default();
        for query_range in ranges {
            let query_rows =
                MultiBufferRow(query_range.start.row)..MultiBufferRow(query_range.end.row + 1);
            for hunk in self.buffer_snapshot().diff_hunks_in_range(
                Point::new(query_rows.start.0, 0)..Point::new(query_rows.end.0, 0),
            ) {
                // Include deleted hunks that are adjacent to the query range, because
                // otherwise they would be missed.
                let mut intersects_range = hunk.row_range.overlaps(&query_rows);
                if hunk.status().is_deleted() {
                    intersects_range |= hunk.row_range.start == query_rows.end;
                    intersects_range |= hunk.row_range.end == query_rows.start;
                }
                if intersects_range {
                    if !processed_buffer_rows
                        .entry(hunk.buffer_id)
                        .or_default()
                        .insert(hunk.buffer_range.start..hunk.buffer_range.end)
                    {
                        continue;
                    }
                    hunks.push(hunk);
                }
            }
        }

        hunks
    }

    fn display_diff_hunks_for_rows<'a>(
        &'a self,
        display_rows: Range<DisplayRow>,
        folded_buffers: &'a HashSet<BufferId>,
    ) -> impl 'a + Iterator<Item = DisplayDiffHunk> {
        let buffer_start = DisplayPoint::new(display_rows.start, 0).to_point(self);
        let buffer_end = DisplayPoint::new(display_rows.end, 0).to_point(self);

        self.buffer_snapshot()
            .diff_hunks_in_range(buffer_start..buffer_end)
            .filter_map(|hunk| {
                if folded_buffers.contains(&hunk.buffer_id)
                    || (hunk.row_range.is_empty() && self.buffer.all_diff_hunks_expanded())
                {
                    return None;
                }

                let hunk_start_point = Point::new(hunk.row_range.start.0, 0);
                let hunk_end_point = if hunk.row_range.end > hunk.row_range.start {
                    let last_row = MultiBufferRow(hunk.row_range.end.0 - 1);
                    let line_len = self.buffer_snapshot().line_len(last_row);
                    Point::new(last_row.0, line_len)
                } else {
                    Point::new(hunk.row_range.end.0, 0)
                };

                let hunk_display_start = self.point_to_display_point(hunk_start_point, Bias::Left);
                let hunk_display_end = self.point_to_display_point(hunk_end_point, Bias::Right);

                let display_hunk = if hunk_display_start.column() != 0 {
                    DisplayDiffHunk::Folded {
                        display_row: hunk_display_start.row(),
                    }
                } else {
                    let mut end_row = hunk_display_end.row();
                    if hunk.row_range.end > hunk.row_range.start || hunk_display_end.column() > 0 {
                        end_row.0 += 1;
                    }
                    let is_created_file = hunk.is_created_file();
                    let multi_buffer_range = hunk.multi_buffer_range.clone();

                    DisplayDiffHunk::Unfolded {
                        status: hunk.status(),
                        diff_base_byte_range: hunk.diff_base_byte_range.start.0
                            ..hunk.diff_base_byte_range.end.0,
                        word_diffs: hunk.word_diffs,
                        display_row_range: hunk_display_start.row()..end_row,
                        multi_buffer_range,
                        is_created_file,
                    }
                };

                Some(display_hunk)
            })
    }

    pub fn language_at<T: ToOffset>(&self, position: T) -> Option<&Arc<Language>> {
        self.display_snapshot
            .buffer_snapshot()
            .language_at(position)
    }

    pub fn is_focused(&self) -> bool {
        self.is_focused
    }

    pub fn placeholder_text(&self) -> Option<String> {
        self.placeholder_display_snapshot
            .as_ref()
            .map(|display_map| display_map.text())
    }

    pub fn scroll_position(&self) -> gpui::Point<ScrollOffset> {
        self.scroll_anchor.scroll_position(&self.display_snapshot)
    }

    pub fn gutter_dimensions(
        &self,
        font_id: FontId,
        font_size: Pixels,
        style: &EditorStyle,
        window: &mut Window,
        cx: &App,
    ) -> GutterDimensions {
        if self.show_gutter
            && let Some(ch_width) = cx.text_system().ch_width(font_id, font_size).log_err()
            && let Some(ch_advance) = cx.text_system().ch_advance(font_id, font_size).log_err()
        {
            let show_git_gutter = self.show_git_diff_gutter.unwrap_or_else(|| {
                matches!(
                    ProjectSettings::get_global(cx).git.git_gutter,
                    GitGutterSetting::TrackedFiles
                )
            });
            let gutter_settings = EditorSettings::get_global(cx).gutter;
            let show_line_numbers = self
                .show_line_numbers
                .unwrap_or(gutter_settings.line_numbers);
            let line_gutter_width = if show_line_numbers {
                // Avoid flicker-like gutter resizes when the line number gains another digit by
                // only resizing the gutter on files with > 10**min_line_number_digits lines.
                let min_width_for_number_on_gutter =
                    ch_advance * gutter_settings.min_line_number_digits as f32;
                self.max_line_number_width(style, window)
                    .max(min_width_for_number_on_gutter)
            } else {
                0.0.into()
            };

            let show_runnables = self.show_runnables.unwrap_or(gutter_settings.runnables);
            let show_breakpoints = self.show_breakpoints.unwrap_or(gutter_settings.breakpoints);

            let git_blame_entries_width =
                self.git_blame_gutter_max_author_length
                    .map(|max_author_length| {
                        let renderer = cx.global::<GlobalBlameRenderer>().0.clone();
                        const MAX_RELATIVE_TIMESTAMP: &str = "60 minutes ago";

                        /// The number of characters to dedicate to gaps and margins.
                        const SPACING_WIDTH: usize = 4;

                        let max_char_count = max_author_length.min(renderer.max_author_length())
                            + ::git::SHORT_SHA_LENGTH
                            + MAX_RELATIVE_TIMESTAMP.len()
                            + SPACING_WIDTH;

                        ch_advance * max_char_count
                    });

            let is_singleton = self.buffer_snapshot().is_singleton();

            let mut left_padding = git_blame_entries_width.unwrap_or(Pixels::ZERO);
            left_padding += if !is_singleton {
                ch_width * 4.0
            } else if show_runnables || show_breakpoints {
                ch_width * 3.0
            } else if show_git_gutter && show_line_numbers {
                ch_width * 2.0
            } else if show_git_gutter || show_line_numbers {
                ch_width
            } else {
                px(0.)
            };

            let shows_folds = is_singleton && gutter_settings.folds;

            let right_padding = if shows_folds && show_line_numbers {
                ch_width * 4.0
            } else if shows_folds || (!is_singleton && show_line_numbers) {
                ch_width * 3.0
            } else if show_line_numbers {
                ch_width
            } else {
                px(0.)
            };

            GutterDimensions {
                left_padding,
                right_padding,
                width: line_gutter_width + left_padding + right_padding,
                margin: GutterDimensions::default_gutter_margin(font_id, font_size, cx),
                git_blame_entries_width,
            }
        } else if self.offset_content {
            GutterDimensions::default_with_margin(font_id, font_size, cx)
        } else {
            GutterDimensions::default()
        }
    }

    pub fn render_crease_toggle(
        &self,
        buffer_row: MultiBufferRow,
        row_contains_cursor: bool,
        editor: Entity<Editor>,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<AnyElement> {
        let folded = self.is_line_folded(buffer_row);
        let mut is_foldable = false;

        if let Some(crease) = self
            .crease_snapshot
            .query_row(buffer_row, self.buffer_snapshot())
        {
            is_foldable = true;
            match crease {
                Crease::Inline { render_toggle, .. } | Crease::Block { render_toggle, .. } => {
                    if let Some(render_toggle) = render_toggle {
                        let toggle_callback =
                            Arc::new(move |folded, window: &mut Window, cx: &mut App| {
                                if folded {
                                    editor.update(cx, |editor, cx| {
                                        editor.fold_at(buffer_row, window, cx)
                                    });
                                } else {
                                    editor.update(cx, |editor, cx| {
                                        editor.unfold_at(buffer_row, window, cx)
                                    });
                                }
                            });
                        return Some((render_toggle)(
                            buffer_row,
                            folded,
                            toggle_callback,
                            window,
                            cx,
                        ));
                    }
                }
            }
        }

        is_foldable |= !self.use_lsp_folding_ranges && self.starts_indent(buffer_row);

        if folded || (is_foldable && (row_contains_cursor || self.gutter_hovered)) {
            Some(
                Disclosure::new(("gutter_crease", buffer_row.0), !folded)
                    .toggle_state(folded)
                    .on_click(window.listener_for(&editor, move |this, _e, window, cx| {
                        if folded {
                            this.unfold_at(buffer_row, window, cx);
                        } else {
                            this.fold_at(buffer_row, window, cx);
                        }
                    }))
                    .into_any_element(),
            )
        } else {
            None
        }
    }

    pub fn render_crease_trailer(
        &self,
        buffer_row: MultiBufferRow,
        window: &mut Window,
        cx: &mut App,
    ) -> Option<AnyElement> {
        let folded = self.is_line_folded(buffer_row);
        if let Crease::Inline { render_trailer, .. } = self
            .crease_snapshot
            .query_row(buffer_row, self.buffer_snapshot())?
        {
            let render_trailer = render_trailer.as_ref()?;
            Some(render_trailer(buffer_row, folded, window, cx))
        } else {
            None
        }
    }

    pub fn max_line_number_width(&self, style: &EditorStyle, window: &mut Window) -> Pixels {
        let digit_count = self.widest_line_number().ilog10() + 1;
        column_pixels(style, digit_count as usize, window)
    }

    /// Returns the line delta from `base` to `line` in the multibuffer, ignoring wrapped lines.
    ///
    /// This is positive if `base` is before `line`.
    fn relative_line_delta(
        &self,
        current_selection_head: DisplayRow,
        first_visible_row: DisplayRow,
        consider_wrapped_lines: bool,
    ) -> i64 {
        let current_selection_head = current_selection_head.as_display_point().to_point(self);
        let first_visible_row = first_visible_row.as_display_point().to_point(self);

        if consider_wrapped_lines {
            let wrap_snapshot = self.wrap_snapshot();
            let base_wrap_row = wrap_snapshot
                .make_wrap_point(current_selection_head, Bias::Left)
                .row();
            let wrap_row = wrap_snapshot
                .make_wrap_point(first_visible_row, Bias::Left)
                .row();

            wrap_row.0 as i64 - base_wrap_row.0 as i64
        } else {
            let fold_snapshot = self.fold_snapshot();
            let base_fold_row = fold_snapshot
                .to_fold_point(self.to_inlay_point(current_selection_head), Bias::Left)
                .row();
            let fold_row = fold_snapshot
                .to_fold_point(self.to_inlay_point(first_visible_row), Bias::Left)
                .row();

            fold_row as i64 - base_fold_row as i64
        }
    }

    /// Returns the unsigned relative line number to display for each row in `rows`.
    ///
    /// Wrapped rows are excluded from the hashmap if `count_relative_lines` is `false`.
    pub fn calculate_relative_line_numbers(
        &self,
        rows: &Range<DisplayRow>,
        current_selection_head: DisplayRow,
        count_wrapped_lines: bool,
    ) -> HashMap<DisplayRow, u32> {
        let initial_offset =
            self.relative_line_delta(current_selection_head, rows.start, count_wrapped_lines);

        self.row_infos(rows.start)
            .take(rows.len())
            .enumerate()
            .map(|(i, row_info)| (DisplayRow(rows.start.0 + i as u32), row_info))
            .filter(|(_row, row_info)| {
                row_info.buffer_row.is_some()
                    || (count_wrapped_lines && row_info.wrapped_buffer_row.is_some())
            })
            .enumerate()
            .filter_map(|(i, (row, row_info))| {
                // We want to ensure here that the current line has absolute
                // numbering, even if we are in a soft-wrapped line. With the
                // exception that if we are in a deleted line, we should number this
                // relative with 0, as otherwise it would have no line number at all
                let relative_line_number = (initial_offset + i as i64).unsigned_abs() as u32;

                (relative_line_number != 0
                    || row_info
                        .diff_status
                        .is_some_and(|status| status.is_deleted()))
                .then_some((row, relative_line_number))
            })
            .collect()
    }
}

pub fn column_pixels(style: &EditorStyle, column: usize, window: &Window) -> Pixels {
    let font_size = style.text.font_size.to_pixels(window.rem_size());
    let layout = window.text_system().shape_line(
        SharedString::from(" ".repeat(column)),
        font_size,
        &[TextRun {
            len: column,
            font: style.text.font(),
            color: Hsla::default(),
            ..Default::default()
        }],
        None,
    );

    layout.width
}

impl Deref for EditorSnapshot {
    type Target = DisplaySnapshot;

    fn deref(&self) -> &Self::Target {
        &self.display_snapshot
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EditorEvent {
    /// Emitted when the stored review comments change (added, removed, or updated).
    ReviewCommentsChanged {
        /// The new total count of review comments.
        total_count: usize,
    },
    InputIgnored {
        text: Arc<str>,
    },
    InputHandled {
        utf16_range_to_replace: Option<Range<isize>>,
        text: Arc<str>,
    },
    BufferRangesUpdated {
        buffer: Entity<Buffer>,
        path_key: PathKey,
        ranges: Vec<ExcerptRange<text::Anchor>>,
    },
    BuffersRemoved {
        removed_buffer_ids: Vec<BufferId>,
    },
    BuffersEdited {
        buffer_ids: Vec<BufferId>,
    },
    BufferFoldToggled {
        ids: Vec<BufferId>,
        folded: bool,
    },
    ExpandExcerptsRequested {
        excerpt_anchors: Vec<Anchor>,
        lines: u32,
        direction: ExpandExcerptDirection,
    },
    StageOrUnstageRequested {
        stage: bool,
        hunks: Vec<MultiBufferDiffHunk>,
    },
    OpenExcerptsRequested {
        selections_by_buffer: HashMap<BufferId, (Vec<Range<BufferOffset>>, Option<u32>)>,
        split: bool,
    },
    RestoreRequested {
        hunks: Vec<MultiBufferDiffHunk>,
    },
    BufferEdited,
    Edited {
        transaction_id: clock::Lamport,
    },
    Reparsed(BufferId),
    Focused,
    FocusedIn,
    Blurred,
    DirtyChanged,
    Saved,
    TitleChanged,
    SelectionsChanged {
        local: bool,
    },
    ScrollPositionChanged {
        local: bool,
        autoscroll: bool,
    },
    TransactionUndone {
        transaction_id: clock::Lamport,
    },
    TransactionBegun {
        transaction_id: clock::Lamport,
    },
    CursorShapeChanged,
    BreadcrumbsChanged,
    OutlineSymbolsChanged,
    PushedToNavHistory {
        anchor: Anchor,
        is_deactivate: bool,
    },
}

impl EventEmitter<EditorEvent> for Editor {}

impl Focusable for Editor {
    fn focus_handle(&self, _cx: &App) -> FocusHandle {
        self.focus_handle.clone()
    }
}

impl Render for Editor {
    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        EditorElement::new(&cx.entity(), self.create_style(cx))
    }
}

impl EntityInputHandler for Editor {
    fn text_for_range(
        &mut self,
        range_utf16: Range<usize>,
        adjusted_range: &mut Option<Range<usize>>,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<String> {
        let snapshot = self.buffer.read(cx).read(cx);
        let start = snapshot.clip_offset_utf16(
            MultiBufferOffsetUtf16(OffsetUtf16(range_utf16.start)),
            Bias::Left,
        );
        let end = snapshot.clip_offset_utf16(
            MultiBufferOffsetUtf16(OffsetUtf16(range_utf16.end)),
            Bias::Right,
        );
        if (start.0.0..end.0.0) != range_utf16 {
            adjusted_range.replace(start.0.0..end.0.0);
        }
        Some(snapshot.text_for_range(start..end).collect())
    }

    fn selected_text_range(
        &mut self,
        ignore_disabled_input: bool,
        _: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<UTF16Selection> {
        // Prevent the IME menu from appearing when holding down an alphabetic key
        // while input is disabled.
        if !ignore_disabled_input && !self.input_enabled {
            return None;
        }

        let selection = self
            .selections
            .newest::<MultiBufferOffsetUtf16>(&self.display_snapshot(cx));
        let range = selection.range();

        Some(UTF16Selection {
            range: range.start.0.0..range.end.0.0,
            reversed: selection.reversed,
        })
    }

    fn marked_text_range(&self, _: &mut Window, cx: &mut Context<Self>) -> Option<Range<usize>> {
        let snapshot = self.buffer.read(cx).read(cx);
        let range = self
            .text_highlights(HighlightKey::InputComposition, cx)?
            .1
            .first()?;
        Some(range.start.to_offset_utf16(&snapshot).0.0..range.end.to_offset_utf16(&snapshot).0.0)
    }

    fn unmark_text(&mut self, _: &mut Window, cx: &mut Context<Self>) {
        self.clear_highlights(HighlightKey::InputComposition, cx);
        self.ime_transaction.take();
    }

    fn replace_text_in_range(
        &mut self,
        range_utf16: Option<Range<usize>>,
        text: &str,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.input_enabled {
            cx.emit(EditorEvent::InputIgnored { text: text.into() });
            return;
        }

        self.transact(window, cx, |this, window, cx| {
            let new_selected_ranges = if let Some(range_utf16) = range_utf16 {
                if let Some(marked_ranges) = this.marked_text_ranges(cx) {
                    // During IME composition, macOS reports the replacement range
                    // relative to the first marked region (the only one visible via
                    // marked_text_range). The correct targets for replacement are the
                    // marked ranges themselves — one per cursor — so use them directly.
                    Some(marked_ranges)
                } else if range_utf16.start == range_utf16.end {
                    // An empty replacement range means "insert at cursor" with no text
                    // to replace. macOS reports the cursor position from its own
                    // (single-cursor) view of the buffer, which diverges from our actual
                    // cursor positions after multi-cursor edits have shifted offsets.
                    // Treating this as range_utf16=None lets each cursor insert in place.
                    None
                } else {
                    // Outside of IME composition (e.g. Accessibility Keyboard word
                    // completion), the range is an absolute document offset for the
                    // newest cursor. Fan it out to all cursors via
                    // selection_replacement_ranges, which applies the delta relative
                    // to the newest selection to every cursor.
                    let range_utf16 = MultiBufferOffsetUtf16(OffsetUtf16(range_utf16.start))
                        ..MultiBufferOffsetUtf16(OffsetUtf16(range_utf16.end));
                    Some(this.selection_replacement_ranges(range_utf16, cx))
                }
            } else {
                this.marked_text_ranges(cx)
            };

            let range_to_replace = new_selected_ranges.as_ref().and_then(|ranges_to_replace| {
                let newest_selection_id = this.selections.newest_anchor().id;
                this.selections
                    .all::<MultiBufferOffsetUtf16>(&this.display_snapshot(cx))
                    .iter()
                    .zip(ranges_to_replace.iter())
                    .find_map(|(selection, range)| {
                        if selection.id == newest_selection_id {
                            Some(
                                (range.start.0.0 as isize - selection.head().0.0 as isize)
                                    ..(range.end.0.0 as isize - selection.head().0.0 as isize),
                            )
                        } else {
                            None
                        }
                    })
            });

            cx.emit(EditorEvent::InputHandled {
                utf16_range_to_replace: range_to_replace,
                text: text.into(),
            });

            if let Some(new_selected_ranges) = new_selected_ranges {
                // Only backspace if at least one range covers actual text. When all
                // ranges are empty (e.g. a trailing-space insertion from Accessibility
                // Keyboard sends replacementRange=cursor..cursor), backspace would
                // incorrectly delete the character just before the cursor.
                let should_backspace = new_selected_ranges.iter().any(|r| r.start != r.end);
                this.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
                    selections.select_ranges(new_selected_ranges)
                });
                if should_backspace {
                    this.backspace(&Default::default(), window, cx);
                }
            }

            this.handle_input(text, window, cx);
        });

        if let Some(transaction) = self.ime_transaction {
            self.buffer.update(cx, |buffer, cx| {
                buffer.group_until_transaction(transaction, cx);
            });
        }

        self.unmark_text(window, cx);
    }

    fn replace_and_mark_text_in_range(
        &mut self,
        range_utf16: Option<Range<usize>>,
        text: &str,
        new_selected_range_utf16: Option<Range<usize>>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        if !self.input_enabled {
            return;
        }

        let transaction = self.transact(window, cx, |this, window, cx| {
            let ranges_to_replace = if let Some(mut marked_ranges) = this.marked_text_ranges(cx) {
                let snapshot = this.buffer.read(cx).read(cx);
                if let Some(relative_range_utf16) = range_utf16.as_ref() {
                    for marked_range in &mut marked_ranges {
                        marked_range.end = marked_range.start + relative_range_utf16.end;
                        marked_range.start += relative_range_utf16.start;
                        marked_range.start =
                            snapshot.clip_offset_utf16(marked_range.start, Bias::Left);
                        marked_range.end =
                            snapshot.clip_offset_utf16(marked_range.end, Bias::Right);
                    }
                }
                Some(marked_ranges)
            } else if let Some(range_utf16) = range_utf16 {
                let range_utf16 = MultiBufferOffsetUtf16(OffsetUtf16(range_utf16.start))
                    ..MultiBufferOffsetUtf16(OffsetUtf16(range_utf16.end));
                Some(this.selection_replacement_ranges(range_utf16, cx))
            } else {
                None
            };

            let range_to_replace = ranges_to_replace.as_ref().and_then(|ranges_to_replace| {
                let newest_selection_id = this.selections.newest_anchor().id;
                this.selections
                    .all::<MultiBufferOffsetUtf16>(&this.display_snapshot(cx))
                    .iter()
                    .zip(ranges_to_replace.iter())
                    .find_map(|(selection, range)| {
                        if selection.id == newest_selection_id {
                            Some(
                                (range.start.0.0 as isize - selection.head().0.0 as isize)
                                    ..(range.end.0.0 as isize - selection.head().0.0 as isize),
                            )
                        } else {
                            None
                        }
                    })
            });

            cx.emit(EditorEvent::InputHandled {
                utf16_range_to_replace: range_to_replace,
                text: text.into(),
            });

            if let Some(ranges) = ranges_to_replace {
                this.change_selections(SelectionEffects::no_scroll(), window, cx, |s| {
                    s.select_ranges(ranges)
                });
            }

            let marked_ranges = {
                let snapshot = this.buffer.read(cx).read(cx);
                this.selections
                    .disjoint_anchors_arc()
                    .iter()
                    .map(|selection| {
                        selection.start.bias_left(&snapshot)..selection.end.bias_right(&snapshot)
                    })
                    .collect::<Vec<_>>()
            };

            if text.is_empty() {
                this.unmark_text(window, cx);
            } else {
                this.highlight_text(
                    HighlightKey::InputComposition,
                    marked_ranges.clone(),
                    HighlightStyle {
                        underline: Some(UnderlineStyle {
                            thickness: px(1.),
                            color: None,
                            wavy: false,
                        }),
                        ..Default::default()
                    },
                    cx,
                );
            }

            // Disable auto-closing when composing text (i.e. typing a `"` on a Brazilian keyboard)
            let use_autoclose = this.use_autoclose;
            let use_auto_surround = this.use_auto_surround;
            this.set_use_autoclose(false);
            this.set_use_auto_surround(false);
            this.handle_input(text, window, cx);
            this.set_use_autoclose(use_autoclose);
            this.set_use_auto_surround(use_auto_surround);

            if let Some(new_selected_range) = new_selected_range_utf16 {
                let snapshot = this.buffer.read(cx).read(cx);
                let new_selected_ranges = marked_ranges
                    .into_iter()
                    .map(|marked_range| {
                        let insertion_start = marked_range.start.to_offset_utf16(&snapshot).0;
                        let new_start = MultiBufferOffsetUtf16(OffsetUtf16(
                            insertion_start.0 + new_selected_range.start,
                        ));
                        let new_end = MultiBufferOffsetUtf16(OffsetUtf16(
                            insertion_start.0 + new_selected_range.end,
                        ));
                        snapshot.clip_offset_utf16(new_start, Bias::Left)
                            ..snapshot.clip_offset_utf16(new_end, Bias::Right)
                    })
                    .collect::<Vec<_>>();

                drop(snapshot);
                this.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
                    selections.select_ranges(new_selected_ranges)
                });
            }
        });

        self.ime_transaction = self.ime_transaction.or(transaction);
        if let Some(transaction) = self.ime_transaction {
            self.buffer.update(cx, |buffer, cx| {
                buffer.group_until_transaction(transaction, cx);
            });
        }

        if self
            .text_highlights(HighlightKey::InputComposition, cx)
            .is_none()
        {
            self.ime_transaction.take();
        }
    }

    fn bounds_for_range(
        &mut self,
        range_utf16: Range<usize>,
        element_bounds: gpui::Bounds<Pixels>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<gpui::Bounds<Pixels>> {
        let text_layout_details = self.text_layout_details(window, cx);
        let CharacterDimensions {
            em_width,
            em_advance,
            line_height,
        } = self.character_dimensions(window, cx);

        let snapshot = self.snapshot(window, cx);
        let scroll_position = snapshot.scroll_position();
        let scroll_left = scroll_position.x * ScrollOffset::from(em_advance);

        let start =
            MultiBufferOffsetUtf16(OffsetUtf16(range_utf16.start)).to_display_point(&snapshot);
        let x = Pixels::from(
            ScrollOffset::from(
                snapshot.x_for_display_point(start, &text_layout_details)
                    + self.gutter_dimensions.full_width(),
            ) - scroll_left,
        );
        let y = line_height * (start.row().as_f64() - scroll_position.y) as f32;

        Some(Bounds {
            origin: element_bounds.origin + point(x, y),
            size: size(em_width, line_height),
        })
    }

    fn character_index_for_point(
        &mut self,
        point: gpui::Point<Pixels>,
        _window: &mut Window,
        _cx: &mut Context<Self>,
    ) -> Option<usize> {
        let position_map = self.last_position_map.as_ref()?;
        if !position_map.text_hitbox.contains(&point) {
            return None;
        }
        let display_point = position_map.point_for_position(point).previous_valid;
        let anchor = position_map
            .snapshot
            .display_point_to_anchor(display_point, Bias::Left);
        let utf16_offset = anchor.to_offset_utf16(&position_map.snapshot.buffer_snapshot());
        Some(utf16_offset.0.0)
    }

    fn accepts_text_input(&self, _window: &mut Window, _cx: &mut Context<Self>) -> bool {
        self.expects_character_input
    }
}

trait SelectionExt {
    fn display_range(&self, map: &DisplaySnapshot) -> Range<DisplayPoint>;
    fn spanned_rows(
        &self,
        include_end_if_at_line_start: bool,
        map: &DisplaySnapshot,
    ) -> Range<MultiBufferRow>;
}

impl<T: ToPoint + ToOffset> SelectionExt for Selection<T> {
    fn display_range(&self, map: &DisplaySnapshot) -> Range<DisplayPoint> {
        let start = self
            .start
            .to_point(map.buffer_snapshot())
            .to_display_point(map);
        let end = self
            .end
            .to_point(map.buffer_snapshot())
            .to_display_point(map);
        if self.reversed {
            end..start
        } else {
            start..end
        }
    }

    fn spanned_rows(
        &self,
        include_end_if_at_line_start: bool,
        map: &DisplaySnapshot,
    ) -> Range<MultiBufferRow> {
        let start = self.start.to_point(map.buffer_snapshot());
        let mut end = self.end.to_point(map.buffer_snapshot());
        if !include_end_if_at_line_start && start.row != end.row && end.column == 0 {
            end.row -= 1;
        }

        let buffer_start = map.prev_line_boundary(start).0;
        let buffer_end = map.next_line_boundary(end).0;
        MultiBufferRow(buffer_start.row)..MultiBufferRow(buffer_end.row + 1)
    }
}

impl<T: InvalidationRegion> InvalidationStack<T> {
    fn invalidate<S>(&mut self, selections: &[Selection<S>], buffer: &MultiBufferSnapshot)
    where
        S: Clone + ToOffset,
    {
        while let Some(region) = self.last() {
            let all_selections_inside_invalidation_ranges =
                if selections.len() == region.ranges().len() {
                    selections
                        .iter()
                        .zip(region.ranges().iter().map(|r| r.to_offset(buffer)))
                        .all(|(selection, invalidation_range)| {
                            let head = selection.head().to_offset(buffer);
                            invalidation_range.start <= head && invalidation_range.end >= head
                        })
                } else {
                    false
                };

            if all_selections_inside_invalidation_ranges {
                break;
            } else {
                self.pop();
            }
        }
    }
}

#[derive(Clone)]
struct ErasedEditorImpl(Entity<Editor>);

impl ui_input::ErasedEditor for ErasedEditorImpl {
    fn text(&self, cx: &App) -> String {
        self.0.read(cx).text(cx)
    }

    fn set_text(&self, text: &str, window: &mut Window, cx: &mut App) {
        self.0.update(cx, |this, cx| {
            this.set_text(text, window, cx);
        })
    }

    fn clear(&self, window: &mut Window, cx: &mut App) {
        self.0.update(cx, |this, cx| this.clear(window, cx));
    }

    fn set_placeholder_text(&self, text: &str, window: &mut Window, cx: &mut App) {
        self.0.update(cx, |this, cx| {
            this.set_placeholder_text(text, window, cx);
        });
    }

    fn focus_handle(&self, cx: &App) -> FocusHandle {
        self.0.read(cx).focus_handle(cx)
    }

    fn render(&self, _: &mut Window, cx: &App) -> AnyElement {
        let settings = ThemeSettings::get_global(cx);
        let theme_color = cx.theme().colors();

        let text_style = TextStyle {
            font_family: settings.ui_font.family.clone(),
            font_features: settings.ui_font.features.clone(),
            font_size: rems(0.875).into(),
            font_weight: settings.ui_font.weight,
            font_style: FontStyle::Normal,
            line_height: relative(1.2),
            color: theme_color.text,
            ..Default::default()
        };
        let editor_style = EditorStyle {
            background: theme_color.ghost_element_background,
            local_player: cx.theme().players().local(),
            syntax: cx.theme().syntax().clone(),
            text: text_style,
            ..Default::default()
        };
        EditorElement::new(&self.0, editor_style).into_any()
    }

    fn as_any(&self) -> &dyn Any {
        &self.0
    }

    fn move_selection_to_end(&self, window: &mut Window, cx: &mut App) {
        self.0.update(cx, |editor, cx| {
            let editor_offset = editor.buffer().read(cx).len(cx);
            editor.change_selections(
                SelectionEffects::scroll(Autoscroll::Next),
                window,
                cx,
                |s| s.select_ranges(Some(editor_offset..editor_offset)),
            );
        });
    }

    fn subscribe(
        &self,
        mut callback: Box<dyn FnMut(ui_input::ErasedEditorEvent, &mut Window, &mut App) + 'static>,
        window: &mut Window,
        cx: &mut App,
    ) -> Subscription {
        window.subscribe(&self.0, cx, move |_, event: &EditorEvent, window, cx| {
            let event = match event {
                EditorEvent::BufferEdited => ui_input::ErasedEditorEvent::BufferEdited,
                EditorEvent::Blurred => ui_input::ErasedEditorEvent::Blurred,
                _ => return,
            };
            (callback)(event, window, cx);
        })
    }

    fn set_masked(&self, masked: bool, _window: &mut Window, cx: &mut App) {
        self.0.update(cx, |editor, cx| {
            editor.set_masked(masked, cx);
        });
    }
}
impl<T> Default for InvalidationStack<T> {
    fn default() -> Self {
        Self(Default::default())
    }
}

impl<T> Deref for InvalidationStack<T> {
    type Target = Vec<T>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> DerefMut for InvalidationStack<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl InvalidationRegion for SnippetState {
    fn ranges(&self) -> &[Range<Anchor>] {
        &self.ranges[self.active_index]
    }
}

fn edit_prediction_edit_text(
    current_snapshot: &BufferSnapshot,
    edits: &[(Range<Anchor>, impl AsRef<str>)],
    edit_preview: &EditPreview,
    include_deletions: bool,
    multibuffer_snapshot: &MultiBufferSnapshot,
    cx: &App,
) -> HighlightedText {
    let edits = edits
        .iter()
        .filter_map(|(anchor, text)| {
            Some((
                multibuffer_snapshot
                    .anchor_range_to_buffer_anchor_range(anchor.clone())?
                    .1,
                text,
            ))
        })
        .collect::<Vec<_>>();

    edit_preview.highlight_edits(current_snapshot, &edits, include_deletions, cx)
}

fn edit_prediction_fallback_text(edits: &[(Range<Anchor>, Arc<str>)], cx: &App) -> HighlightedText {
    // Fallback for providers that don't provide edit_preview (like Copilot)
    // Just show the raw edit text with basic styling
    let mut text = String::new();
    let mut highlights = Vec::new();

    let insertion_highlight_style = HighlightStyle {
        color: Some(cx.theme().colors().text),
        ..Default::default()
    };

    for (_, edit_text) in edits {
        let start_offset = text.len();
        text.push_str(edit_text);
        let end_offset = text.len();

        if start_offset < end_offset {
            highlights.push((start_offset..end_offset, insertion_highlight_style));
        }
    }

    HighlightedText {
        text: text.into(),
        highlights,
    }
}

pub fn diagnostic_style(severity: lsp::DiagnosticSeverity, colors: &StatusColors) -> Hsla {
    match severity {
        lsp::DiagnosticSeverity::ERROR => colors.error,
        lsp::DiagnosticSeverity::WARNING => colors.warning,
        lsp::DiagnosticSeverity::INFORMATION => colors.info,
        lsp::DiagnosticSeverity::HINT => colors.info,
        _ => colors.ignored,
    }
}

pub fn styled_runs_for_code_label<'a>(
    label: &'a CodeLabel,
    syntax_theme: &'a theme::SyntaxTheme,
    local_player: &'a theme::PlayerColor,
) -> impl 'a + Iterator<Item = (Range<usize>, HighlightStyle)> {
    let fade_out = HighlightStyle {
        fade_out: Some(0.35),
        ..Default::default()
    };

    if label.runs.is_empty() {
        let desc_start = label.filter_range.end;
        let fade_run =
            (desc_start < label.text.len()).then(|| (desc_start..label.text.len(), fade_out));
        return Either::Left(fade_run.into_iter());
    }

    let mut prev_end = label.filter_range.end;
    Either::Right(
        label
            .runs
            .iter()
            .enumerate()
            .flat_map(move |(ix, (range, highlight_id))| {
                let style = if *highlight_id == language::HighlightId::TABSTOP_INSERT_ID {
                    HighlightStyle {
                        color: Some(local_player.cursor),
                        ..Default::default()
                    }
                } else if *highlight_id == language::HighlightId::TABSTOP_REPLACE_ID {
                    HighlightStyle {
                        background_color: Some(local_player.selection),
                        ..Default::default()
                    }
                } else if let Some(style) = syntax_theme.get(*highlight_id).cloned() {
                    style
                } else {
                    return Default::default();
                };

                let mut runs = SmallVec::<[(Range<usize>, HighlightStyle); 3]>::new();
                let muted_style = style.highlight(fade_out);
                if range.start >= label.filter_range.end {
                    if range.start > prev_end {
                        runs.push((prev_end..range.start, fade_out));
                    }
                    runs.push((range.clone(), muted_style));
                } else if range.end <= label.filter_range.end {
                    runs.push((range.clone(), style));
                } else {
                    runs.push((range.start..label.filter_range.end, style));
                    runs.push((label.filter_range.end..range.end, muted_style));
                }
                prev_end = cmp::max(prev_end, range.end);

                if ix + 1 == label.runs.len() && label.text.len() > prev_end {
                    runs.push((prev_end..label.text.len(), fade_out));
                }

                runs
            }),
    )
}

pub(crate) fn split_words(text: &str) -> impl std::iter::Iterator<Item = &str> + '_ {
    let mut prev_index = 0;
    let mut prev_codepoint: Option<char> = None;
    text.char_indices()
        .chain([(text.len(), '\0')])
        .filter_map(move |(index, codepoint)| {
            let prev_codepoint = prev_codepoint.replace(codepoint)?;
            let is_boundary = index == text.len()
                || !prev_codepoint.is_uppercase() && codepoint.is_uppercase()
                || !prev_codepoint.is_alphanumeric() && codepoint.is_alphanumeric();
            if is_boundary {
                let chunk = &text[prev_index..index];
                prev_index = index;
                Some(chunk)
            } else {
                None
            }
        })
}

/// Given a string of text immediately before the cursor, iterates over possible
/// strings a snippet could match to. More precisely: returns an iterator over
/// suffixes of `text` created by splitting at word boundaries (before & after
/// every non-word character).
///
/// Shorter suffixes are returned first.
pub(crate) fn snippet_candidate_suffixes<'a>(
    text: &'a str,
    is_word_char: &'a dyn Fn(char) -> bool,
) -> impl std::iter::Iterator<Item = &'a str> + 'a {
    let mut prev_index = text.len();
    let mut prev_codepoint = None;
    text.char_indices()
        .rev()
        .chain([(0, '\0')])
        .filter_map(move |(index, codepoint)| {
            let prev_index = std::mem::replace(&mut prev_index, index);
            let prev_codepoint = prev_codepoint.replace(codepoint)?;
            if is_word_char(prev_codepoint) && is_word_char(codepoint) {
                None
            } else {
                let chunk = &text[prev_index..]; // go to end of string
                Some(chunk)
            }
        })
}

pub trait RangeToAnchorExt: Sized {
    fn to_anchors(self, snapshot: &MultiBufferSnapshot) -> Range<Anchor>;

    fn to_display_points(self, snapshot: &EditorSnapshot) -> Range<DisplayPoint> {
        let anchor_range = self.to_anchors(&snapshot.buffer_snapshot());
        anchor_range.start.to_display_point(snapshot)..anchor_range.end.to_display_point(snapshot)
    }
}

impl<T: ToOffset> RangeToAnchorExt for Range<T> {
    fn to_anchors(self, snapshot: &MultiBufferSnapshot) -> Range<Anchor> {
        let start_offset = self.start.to_offset(snapshot);
        let end_offset = self.end.to_offset(snapshot);
        if start_offset == end_offset {
            snapshot.anchor_before(start_offset)..snapshot.anchor_before(end_offset)
        } else {
            snapshot.anchor_after(self.start)..snapshot.anchor_before(self.end)
        }
    }
}

pub trait RowExt {
    fn as_f64(&self) -> f64;

    fn next_row(&self) -> Self;

    fn previous_row(&self) -> Self;

    fn minus(&self, other: Self) -> u32;
}

impl RowExt for DisplayRow {
    fn as_f64(&self) -> f64 {
        self.0 as _
    }

    fn next_row(&self) -> Self {
        Self(self.0 + 1)
    }

    fn previous_row(&self) -> Self {
        Self(self.0.saturating_sub(1))
    }

    fn minus(&self, other: Self) -> u32 {
        self.0 - other.0
    }
}

impl RowExt for MultiBufferRow {
    fn as_f64(&self) -> f64 {
        self.0 as _
    }

    fn next_row(&self) -> Self {
        Self(self.0 + 1)
    }

    fn previous_row(&self) -> Self {
        Self(self.0.saturating_sub(1))
    }

    fn minus(&self, other: Self) -> u32 {
        self.0 - other.0
    }
}

trait RowRangeExt {
    type Row;

    fn len(&self) -> usize;

    fn iter_rows(&self) -> impl DoubleEndedIterator<Item = Self::Row>;
}

impl RowRangeExt for Range<MultiBufferRow> {
    type Row = MultiBufferRow;

    fn len(&self) -> usize {
        (self.end.0 - self.start.0) as usize
    }

    fn iter_rows(&self) -> impl DoubleEndedIterator<Item = MultiBufferRow> {
        (self.start.0..self.end.0).map(MultiBufferRow)
    }
}

impl RowRangeExt for Range<DisplayRow> {
    type Row = DisplayRow;

    fn len(&self) -> usize {
        (self.end.0 - self.start.0) as usize
    }

    fn iter_rows(&self) -> impl DoubleEndedIterator<Item = DisplayRow> {
        (self.start.0..self.end.0).map(DisplayRow)
    }
}

/// If select range has more than one line, we
/// just point the cursor to range.start.
fn collapse_multiline_range(range: Range<Point>) -> Range<Point> {
    if range.start.row == range.end.row {
        range
    } else {
        range.start..range.start
    }
}
pub struct KillRing(ClipboardItem);
impl Global for KillRing {}

const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50);

enum BreakpointPromptEditAction {
    Log,
    Condition,
    HitCondition,
}

struct BreakpointPromptEditor {
    pub(crate) prompt: Entity<Editor>,
    editor: WeakEntity<Editor>,
    breakpoint_anchor: Anchor,
    breakpoint: Breakpoint,
    edit_action: BreakpointPromptEditAction,
    block_ids: HashSet<CustomBlockId>,
    editor_margins: Arc<Mutex<EditorMargins>>,
    _subscriptions: Vec<Subscription>,
}

impl BreakpointPromptEditor {
    const MAX_LINES: u8 = 4;

    fn new(
        editor: WeakEntity<Editor>,
        breakpoint_anchor: Anchor,
        breakpoint: Breakpoint,
        edit_action: BreakpointPromptEditAction,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        let base_text = match edit_action {
            BreakpointPromptEditAction::Log => breakpoint.message.as_ref(),
            BreakpointPromptEditAction::Condition => breakpoint.condition.as_ref(),
            BreakpointPromptEditAction::HitCondition => breakpoint.hit_condition.as_ref(),
        }
        .map(|msg| msg.to_string())
        .unwrap_or_default();

        let buffer = cx.new(|cx| Buffer::local(base_text, cx));
        let buffer = cx.new(|cx| MultiBuffer::singleton(buffer, cx));

        let prompt = cx.new(|cx| {
            let mut prompt = Editor::new(
                EditorMode::AutoHeight {
                    min_lines: 1,
                    max_lines: Some(Self::MAX_LINES as usize),
                },
                buffer,
                None,
                window,
                cx,
            );
            prompt.set_soft_wrap_mode(language::language_settings::SoftWrap::EditorWidth, cx);
            prompt.set_show_cursor_when_unfocused(false, cx);
            prompt.set_placeholder_text(
                match edit_action {
                    BreakpointPromptEditAction::Log => "Message to log when a breakpoint is hit. Expressions within {} are interpolated.",
                    BreakpointPromptEditAction::Condition => "Condition when a breakpoint is hit. Expressions within {} are interpolated.",
                    BreakpointPromptEditAction::HitCondition => "How many breakpoint hits to ignore",
                },
                window,
                cx,
            );

            prompt
        });

        Self {
            prompt,
            editor,
            breakpoint_anchor,
            breakpoint,
            edit_action,
            editor_margins: Arc::new(Mutex::new(EditorMargins::default())),
            block_ids: Default::default(),
            _subscriptions: vec![],
        }
    }

    pub(crate) fn add_block_ids(&mut self, block_ids: Vec<CustomBlockId>) {
        self.block_ids.extend(block_ids)
    }

    fn confirm(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
        if let Some(editor) = self.editor.upgrade() {
            let message = self
                .prompt
                .read(cx)
                .buffer
                .read(cx)
                .as_singleton()
                .expect("A multi buffer in breakpoint prompt isn't possible")
                .read(cx)
                .as_rope()
                .to_string();

            editor.update(cx, |editor, cx| {
                editor.edit_breakpoint_at_anchor(
                    self.breakpoint_anchor,
                    self.breakpoint.clone(),
                    match self.edit_action {
                        BreakpointPromptEditAction::Log => {
                            BreakpointEditAction::EditLogMessage(message.into())
                        }
                        BreakpointPromptEditAction::Condition => {
                            BreakpointEditAction::EditCondition(message.into())
                        }
                        BreakpointPromptEditAction::HitCondition => {
                            BreakpointEditAction::EditHitCondition(message.into())
                        }
                    },
                    cx,
                );

                editor.remove_blocks(self.block_ids.clone(), None, cx);
                cx.focus_self(window);
            });
        }
    }

    fn cancel(&mut self, _: &menu::Cancel, window: &mut Window, cx: &mut Context<Self>) {
        self.editor
            .update(cx, |editor, cx| {
                editor.remove_blocks(self.block_ids.clone(), None, cx);
                window.focus(&editor.focus_handle, cx);
            })
            .log_err();
    }

    fn render_prompt_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let settings = ThemeSettings::get_global(cx);
        let text_style = TextStyle {
            color: if self.prompt.read(cx).read_only(cx) {
                cx.theme().colors().text_disabled
            } else {
                cx.theme().colors().text
            },
            font_family: settings.buffer_font.family.clone(),
            font_fallbacks: settings.buffer_font.fallbacks.clone(),
            font_size: settings.buffer_font_size(cx).into(),
            font_weight: settings.buffer_font.weight,
            line_height: relative(settings.buffer_line_height.value()),
            ..Default::default()
        };
        EditorElement::new(
            &self.prompt,
            EditorStyle {
                background: cx.theme().colors().editor_background,
                local_player: cx.theme().players().local(),
                text: text_style,
                ..Default::default()
            },
        )
    }

    fn render_close_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let focus_handle = self.prompt.focus_handle(cx);
        IconButton::new("cancel", IconName::Close)
            .icon_color(Color::Muted)
            .shape(IconButtonShape::Square)
            .tooltip(move |_window, cx| {
                Tooltip::for_action_in("Cancel", &menu::Cancel, &focus_handle, cx)
            })
            .on_click(cx.listener(|this, _, window, cx| {
                this.cancel(&menu::Cancel, window, cx);
            }))
    }

    fn render_confirm_button(&self, cx: &mut Context<Self>) -> impl IntoElement {
        let focus_handle = self.prompt.focus_handle(cx);
        IconButton::new("confirm", IconName::Return)
            .icon_color(Color::Muted)
            .shape(IconButtonShape::Square)
            .tooltip(move |_window, cx| {
                Tooltip::for_action_in("Confirm", &menu::Confirm, &focus_handle, cx)
            })
            .on_click(cx.listener(|this, _, window, cx| {
                this.confirm(&menu::Confirm, window, cx);
            }))
    }
}

impl Render for BreakpointPromptEditor {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let ui_font_size = ThemeSettings::get_global(cx).ui_font_size(cx);
        let editor_margins = *self.editor_margins.lock();
        let gutter_dimensions = editor_margins.gutter;
        let left_gutter_width = gutter_dimensions.full_width() + (gutter_dimensions.margin / 2.0);
        let right_padding = editor_margins.right + px(9.);
        h_flex()
            .key_context("Editor")
            .bg(cx.theme().colors().editor_background)
            .border_y_1()
            .border_color(cx.theme().status().info_border)
            .size_full()
            .py(window.line_height() / 2.5)
            .pr(right_padding)
            .on_action(cx.listener(Self::confirm))
            .on_action(cx.listener(Self::cancel))
            .child(
                WithRemSize::new(ui_font_size)
                    .h_full()
                    .w(left_gutter_width)
                    .flex()
                    .flex_row()
                    .flex_shrink_0()
                    .items_center()
                    .justify_center()
                    .gap_1()
                    .child(self.render_close_button(cx)),
            )
            .child(
                h_flex()
                    .w_full()
                    .justify_between()
                    .child(div().flex_1().child(self.render_prompt_editor(cx)))
                    .child(
                        WithRemSize::new(ui_font_size)
                            .flex()
                            .flex_row()
                            .items_center()
                            .child(self.render_confirm_button(cx)),
                    ),
            )
    }
}

impl Focusable for BreakpointPromptEditor {
    fn focus_handle(&self, cx: &App) -> FocusHandle {
        self.prompt.focus_handle(cx)
    }
}

fn all_edits_insertions_or_deletions(
    edits: &Vec<(Range<Anchor>, Arc<str>)>,
    snapshot: &MultiBufferSnapshot,
) -> bool {
    let mut all_insertions = true;
    let mut all_deletions = true;

    for (range, new_text) in edits.iter() {
        let range_is_empty = range.to_offset(snapshot).is_empty();
        let text_is_empty = new_text.is_empty();

        if range_is_empty != text_is_empty {
            if range_is_empty {
                all_deletions = false;
            } else {
                all_insertions = false;
            }
        } else {
            return false;
        }

        if !all_insertions && !all_deletions {
            return false;
        }
    }
    all_insertions || all_deletions
}

struct MissingEditPredictionKeybindingTooltip;

impl Render for MissingEditPredictionKeybindingTooltip {
    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        ui::tooltip_container(cx, |container, cx| {
            container
                .flex_shrink_0()
                .max_w_80()
                .min_h(rems_from_px(124.))
                .justify_between()
                .child(
                    v_flex()
                        .flex_1()
                        .text_ui_sm(cx)
                        .child(Label::new("Conflict with Accept Keybinding"))
                        .child("Your keymap currently overrides the default accept keybinding. To continue, assign one keybinding for the `editor::AcceptEditPrediction` action.")
                )
                .child(
                    h_flex()
                        .pb_1()
                        .gap_1()
                        .items_end()
                        .w_full()
                        .child(Button::new("open-keymap", "Assign Keybinding").size(ButtonSize::Compact).on_click(|_ev, window, cx| {
                            window.dispatch_action(zed_actions::OpenKeymapFile.boxed_clone(), cx)
                        }))
                        .child(Button::new("see-docs", "See Docs").size(ButtonSize::Compact).on_click(|_ev, _window, cx| {
                            cx.open_url("https://zed.dev/docs/completions#edit-predictions-missing-keybinding");
                        })),
                )
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LineHighlight {
    pub background: Background,
    pub border: Option<gpui::Hsla>,
    pub include_gutter: bool,
    pub type_id: Option<TypeId>,
}

struct LineManipulationResult {
    pub new_text: String,
    pub line_count_before: usize,
    pub line_count_after: usize,
}

fn render_diff_hunk_controls(
    row: u32,
    status: &DiffHunkStatus,
    hunk_range: Range<Anchor>,
    is_created_file: bool,
    line_height: Pixels,
    editor: &Entity<Editor>,
    _window: &mut Window,
    cx: &mut App,
) -> AnyElement {
    h_flex()
        .h(line_height)
        .mr_1()
        .gap_1()
        .px_0p5()
        .pb_1()
        .border_x_1()
        .border_b_1()
        .border_color(cx.theme().colors().border_variant)
        .rounded_b_lg()
        .bg(cx.theme().colors().editor_background)
        .gap_1()
        .block_mouse_except_scroll()
        .shadow_md()
        .child(if status.has_secondary_hunk() {
            Button::new(("stage", row as u64), "Stage")
                .alpha(if status.is_pending() { 0.66 } else { 1.0 })
                .tooltip({
                    let focus_handle = editor.focus_handle(cx);
                    move |_window, cx| {
                        Tooltip::for_action_in(
                            "Stage Hunk",
                            &::git::ToggleStaged,
                            &focus_handle,
                            cx,
                        )
                    }
                })
                .on_click({
                    let editor = editor.clone();
                    move |_event, _window, cx| {
                        editor.update(cx, |editor, cx| {
                            editor.stage_or_unstage_diff_hunks(
                                true,
                                vec![hunk_range.start..hunk_range.start],
                                cx,
                            );
                        });
                    }
                })
        } else {
            Button::new(("unstage", row as u64), "Unstage")
                .alpha(if status.is_pending() { 0.66 } else { 1.0 })
                .tooltip({
                    let focus_handle = editor.focus_handle(cx);
                    move |_window, cx| {
                        Tooltip::for_action_in(
                            "Unstage Hunk",
                            &::git::ToggleStaged,
                            &focus_handle,
                            cx,
                        )
                    }
                })
                .on_click({
                    let editor = editor.clone();
                    move |_event, _window, cx| {
                        editor.update(cx, |editor, cx| {
                            editor.stage_or_unstage_diff_hunks(
                                false,
                                vec![hunk_range.start..hunk_range.start],
                                cx,
                            );
                        });
                    }
                })
        })
        .child(
            Button::new(("restore", row as u64), "Restore")
                .tooltip({
                    let focus_handle = editor.focus_handle(cx);
                    move |_window, cx| {
                        Tooltip::for_action_in("Restore Hunk", &::git::Restore, &focus_handle, cx)
                    }
                })
                .on_click({
                    let editor = editor.clone();
                    move |_event, window, cx| {
                        editor.update(cx, |editor, cx| {
                            let snapshot = editor.snapshot(window, cx);
                            let point = hunk_range.start.to_point(&snapshot.buffer_snapshot());
                            editor.restore_hunks_in_ranges(vec![point..point], window, cx);
                        });
                    }
                })
                .disabled(is_created_file),
        )
        .when(
            !editor.read(cx).buffer().read(cx).all_diff_hunks_expanded(),
            |el| {
                el.child(
                    IconButton::new(("next-hunk", row as u64), IconName::ArrowDown)
                        .shape(IconButtonShape::Square)
                        .icon_size(IconSize::Small)
                        // .disabled(!has_multiple_hunks)
                        .tooltip({
                            let focus_handle = editor.focus_handle(cx);
                            move |_window, cx| {
                                Tooltip::for_action_in("Next Hunk", &GoToHunk, &focus_handle, cx)
                            }
                        })
                        .on_click({
                            let editor = editor.clone();
                            move |_event, window, cx| {
                                editor.update(cx, |editor, cx| {
                                    let snapshot = editor.snapshot(window, cx);
                                    let position =
                                        hunk_range.end.to_point(&snapshot.buffer_snapshot());
                                    editor.go_to_hunk_before_or_after_position(
                                        &snapshot,
                                        position,
                                        Direction::Next,
                                        true,
                                        window,
                                        cx,
                                    );
                                    editor.expand_selected_diff_hunks(cx);
                                });
                            }
                        }),
                )
                .child(
                    IconButton::new(("prev-hunk", row as u64), IconName::ArrowUp)
                        .shape(IconButtonShape::Square)
                        .icon_size(IconSize::Small)
                        // .disabled(!has_multiple_hunks)
                        .tooltip({
                            let focus_handle = editor.focus_handle(cx);
                            move |_window, cx| {
                                Tooltip::for_action_in(
                                    "Previous Hunk",
                                    &GoToPreviousHunk,
                                    &focus_handle,
                                    cx,
                                )
                            }
                        })
                        .on_click({
                            let editor = editor.clone();
                            move |_event, window, cx| {
                                editor.update(cx, |editor, cx| {
                                    let snapshot = editor.snapshot(window, cx);
                                    let point =
                                        hunk_range.start.to_point(&snapshot.buffer_snapshot());
                                    editor.go_to_hunk_before_or_after_position(
                                        &snapshot,
                                        point,
                                        Direction::Prev,
                                        true,
                                        window,
                                        cx,
                                    );
                                    editor.expand_selected_diff_hunks(cx);
                                });
                            }
                        }),
                )
            },
        )
        .into_any_element()
}

pub fn multibuffer_context_lines(cx: &App) -> u32 {
    EditorSettings::try_get(cx)
        .map(|settings| settings.excerpt_context_lines)
        .unwrap_or(2)
        .min(32)
}
