Skip to content

Visual Studio Code, rebuilt in Rust

Rust
6.8 KB
Raw Blame
//! The one announcement of a model content change and of a cursor move: textModel.ts `onDidChangeContent` of the
//! model, then codeEditorWidget.ts `onDidChangeModelContent` and `onDidChangeCursorPosition` of the active code
//! editor when it shows it. The host hears the change first, so every provider request follows the edit.

use std::rc::Rc;

use gpui::Context;

use super::Workbench;
use crate::editor::{ContentChanged, EditReason, Reveal, VerticalReveal, uri_string};
use crate::navigation::Reason;

/// How the active editor's cursors moved with an announcement.
pub(super) struct Cursor<'a> {
    pub moved: bool,
    /// cursorEvents.ts `source`: "keyboard", "mouse", "api", "modelChange", ...
    pub source: &'a str,
    pub reveal: Reveal,
}

impl Cursor<'_> {
    /// An edit made without the editor (bulk edit, host edit, save participant, revert): the cursors only follow it.
    pub(super) fn model_change() -> Cursor<'static> {
        Cursor { moved: false, source: "modelChange", reveal: Reveal::Nothing }
    }
}

/// cursor.ts `source` of an edit made through the code editor: `executeEdits` and `executeCommands` pass theirs,
/// which the reason carries; undo, redo and an EOL change reach the cursors by `onModelContentChanged`.
pub(super) fn edit_source(reason: &EditReason) -> &'static str {
    match reason {
        EditReason::Cursor { detailed_source, .. } => detailed_source,
        EditReason::Unknown(name) => name,
        EditReason::ApplyEdits | EditReason::EolChange => "modelChange",
    }
}

impl Workbench {
    /// `onDidChangeContent` of the change's model, then `onDidChangeModelContent` and `onDidChangeCursorPosition`
    /// when the active code editor shows it. Without a change, `cursor_position_changed`.
    pub(super) fn model_content_changed(&mut self, change: Option<ContentChanged>, cursor: Cursor, cx: &mut Context<Self>) {
        let Some(change) = change else { return self.cursor_position_changed(cursor, cx) };
        let shown = self.editors.active().is_some_and(|editor| Rc::ptr_eq(&editor.model, &change.model));
        self.send_model_change(&change, shown, cursor.source);
        self.model_listeners(&change, cx);
        if shown {
            self.editor_listeners(Some(&change), cursor, cx);
        }
        cx.notify();
    }

    /// `onDidChangeCursorPosition` of the active code editor.
    pub(super) fn cursor_position_changed(&mut self, cursor: Cursor, cx: &mut Context<Self>) {
        if self.editors.active().is_none() {
            return;
        }
        self.editor_listeners(None, cursor, cx);
        cx.notify();
    }

    /// mainThreadDocuments.ts and mainThreadEditor.ts: `$acceptModelChanged`, then the selection of the shown model.
    fn send_model_change(&mut self, change: &ContentChanged, shown: bool, source: &str) {
        let editor = self.editors.all_editors().find(|editor| Rc::ptr_eq(&editor.model, &change.model)).expect("an edited model is open");
        self.host.send_model_change(&self.editors, editor, change);
        if shown {
            self.host.send_selection(self.editors.active().expect("shown"), source);
        }
    }

    /// The listeners of any model.
    fn model_listeners(&mut self, change: &ContentChanged, cx: &mut Context<Self>) {
        let model = change.model.borrow();
        let (scheme, path) = (model.document.scheme.clone(), model.document.path.clone());
        drop(model);
        self.search_model_changed(&scheme, &path);
        self.language_detection_content_changed(&scheme, &path, cx);
        self.semantic_tokens_content_changed(&uri_string(&scheme, &path), cx);
        self.cell_status_bar_content_changed(&scheme, &path, cx);
    }

    /// The listeners of the active code editor: its content changed with `change`, its cursors as `cursor` says.
    fn editor_listeners(&mut self, change: Option<&ContentChanged>, cursor: Cursor, cx: &mut Context<Self>) {
        let Cursor { moved, source, reveal } = cursor;
        let scrolled = self.scroll_smoothly(cx, |editor, viewport| match reveal {
            Reveal::Nothing => {}
            Reveal::All => editor.reveal_cursors(source, viewport),
            Reveal::Position(position) => editor.reveal_position(position, source, viewport),
            Reveal::CenterIfOutside(position) => editor.reveal_range(position, position, VerticalReveal::CenterIfOutsideViewport, viewport),
        });
        if scrolled {
            self.editor_scrolled(cx);
        }
        if change.is_none() && !moved {
            return;
        }
        self.find_editor_edited(change);
        self.record_navigation(Some(if change.is_some() { Reason::Edit } else { Reason::of_source(source) }));
        if change.is_none() {
            self.host.send_selection(self.editors.active().expect("checked by the caller"), source);
        }
        if let Some(change) = change {
            self.editor_content_listeners(change, cx);
        }
        if moved {
            self.output_cursor_moved();
        }
        self.snippet_state_changed(cx);
        self.suggest_editor_changed(change.is_some(), source, cx);
        self.parameter_hints_editor_changed(source, cx);
        self.code_actions_cursor_changed(change.is_some(), cx);
        self.standalone_color_picker_cursor_changed(cx);
        self.linked_editing_editor_changed(change, true, cx);
        match change {
            Some(change) => self.inline_completions_edited(change, source, cx),
            None => self.inline_completions_cursor_moved(cx),
        }
    }

    /// `onDidChangeModelContent` of the active code editor.
    fn editor_content_listeners(&mut self, change: &ContentChanged, cx: &mut Context<Self>) {
        // The edit can unfold a region or change the line count, which changes the visible lines.
        self.host.send_visible_ranges(&self.editors);
        let document = self.editors.active().expect("shown").document();
        let (scheme, path) = (document.scheme.clone(), document.path.clone());
        drop(document);
        self.schedule_debug_inline_values(cx);
        self.search_editor_edited(&path, change);
        self.format_on_type_content_changed(change);
        self.links_content_changed(change, cx);
        // untitledTextEditorModel.ts `onDidChangeName`: the first line names the tab.
        if scheme == "untitled" {
            self.host.send_tabs(&self.editors);
        }
        // commentReply.ts `onDidChangeModelContent`: the comment editor grows or shrinks by its lines.
        if scheme == super::comments::INPUT_SCHEME {
            self.sync_comment_widgets();
        }
        self.schedule_keybinding_marks(cx);
        self.schedule_unsupported_settings_markers(cx);
        self.code_lens_content_changed(cx);
        self.refresh_commenting_ranges(true, cx);
        self.inlay_hints_content_changed(cx);
        self.color_detector_content_changed(cx);
        self.color_picker_content_changed(cx);
    }
}

#[cfg(test)]
mod tests;