Skip to content

Visual Studio Code, rebuilt in Rust

Rust
12.4 KB
Raw Blame
//! mainThreadComments.ts: the comment controllers extensions register and their threads, kept for the Comments
//! view (workbench/comments_view.rs).

use gpui::{Context, Window};
use serde_json::Value;

use super::Workbench;
use super::comment_thread_widget::{Place, Reply, ThreadWidget};
use crate::commands::Outcome;

#[cfg(test)]
mod tests;

/// `Schemas.commentsInput`: the models of the comment reply and edit editors.
pub const INPUT_SCHEME: &str = "comment";

pub(super) struct Comments {
    pub(super) controllers: Vec<Controller>,
    pub(super) collapsed: std::collections::HashSet<String>,
    pub(super) widgets: Vec<ThreadWidget>,
    /// The open reply and edit forms, any number across threads and comments (`CommentReply`, `CommentNode`).
    pub(super) forms: Vec<Reply>,
    /// The drawn height of each comment's body by controller, thread, editor model and `uniqueIdInThread`, which sizes
    /// its zone in that editor.
    pub(super) body_heights: std::collections::HashMap<(u64, u64, u32, u64), f32>,
    /// The drawn height of each thread's additional actions by controller, thread and editor model, 0 while hidden.
    pub(super) additional_heights: std::collections::HashMap<(u64, u64, u32), f32>,
    /// commentThreadBody.ts `.comments-container` (tabIndex 0): the body `CommentWidgetFocus.Widget` focuses, of the
    /// thread `focused_thread` names (controller, thread, the comment the arrows moved to).
    pub(super) thread_focus: gpui::FocusHandle,
    pub(super) focused_thread: Option<(u64, u64, Option<usize>)>,
    pub(super) view: super::comments_view::ViewState,
    pub(super) commenting: super::commenting_ranges::CommentingRanges,
    /// Toggle Editor Commenting turned commenting off (`commentingEnabled` false): no glyphs, ranges or threads show.
    pub(super) commenting_disabled: bool,
    /// The `reaction.command.<label>` commands of the last reaction picker.
    pub(super) reaction_commands: Vec<(String, crate::commands::Registration)>,
    pub(super) floating: super::comment_thread_floating::FloatingWidgets,
    /// The thread whose widget the pointer is over (`setCurrentCommentThread`).
    pub(super) hovered: Option<(u64, u64)>,
    /// commentReply.ts `INMEM_MODEL_ID`: numbers the reply models of threads without comments.
    pub(super) input_models: u64,
}

impl Comments {
    pub(super) fn new(cx: &mut Context<Workbench>) -> Comments {
        let view = super::comments_view::ViewState::new(cx);
        Comments { controllers: Vec::new(), collapsed: Default::default(), widgets: Vec::new(), forms: Vec::new(), body_heights: Default::default(), additional_heights: Default::default(), thread_focus: cx.focus_handle(), focused_thread: None, view, commenting: Default::default(), commenting_disabled: false, reaction_commands: Vec::new(), floating: Default::default(), hovered: None, input_models: 0 }
    }
}

/// `MainThreadCommentController`.
pub(super) struct Controller {
    pub(super) handle: u64,
    pub(super) id: String,
    pub(super) extension_id: String,
    pub(super) label: String,
    /// `CommentControllerFeatures.contextValue`, the `commentController` context key.
    pub(super) context_value: Option<String>,
    /// `CommentControllerFeatures.reactionHandler`: reactions toggle.
    pub(super) reaction_handler: bool,
    /// `updateCommentingRanges` was called, which registers the view upstream.
    pub(super) has_commenting_ranges: bool,
    pub(super) threads: Vec<Thread>,
}

/// `MainThreadCommentThread`: the fields of `CommentThreadChanges`.
#[derive(Clone, Debug)]
pub(super) struct Thread {
    pub(super) handle: u64,
    pub(super) thread_id: String,
    pub(super) resource: Value,
    pub(super) range: Option<Value>,
    pub(super) label: Option<String>,
    pub(super) context_value: Option<String>,
    pub(super) comments: Vec<Value>,
    pub(super) collapse_state: u64,
    /// A new empty template shown by `shouldReveal` (ReviewZoneWidget `makeVisible`), until a collapse other than
    /// Dismiss Comment.
    pub(super) revealed: bool,
    pub(super) can_reply: Value,
    /// `CommentThreadState`: 0 unresolved, 1 resolved.
    pub(super) state: Option<u64>,
    pub(super) is_template: bool,
    /// The editors that showed the thread's resource, so had its zone widget, when its state changed:
    /// commentThreadZoneWidget.ts sets the head's background and the body's top border only in `onDidChangeState`.
    pub(super) state_styled_in: Vec<u32>,
}

impl Workbench {
    pub(crate) fn handle_comments_request(&mut self, method: &str, args: &[Value], window: &mut Window, cx: &mut Context<Self>) -> Outcome {
        let arg = |index: usize| args.get(index).cloned().unwrap_or(Value::Null);
        let handle = arg(0).as_u64().unwrap_or_default();
        match method {
            "$registerCommentController" => {
                let text = |index: usize| arg(index).as_str().unwrap_or_default().to_owned();
                self.comments.controllers.push(Controller { handle, id: text(1), extension_id: text(3), label: text(2), context_value: None, reaction_handler: false, has_commenting_ranges: false, threads: Vec::new() });
            }
            "$updateCommentControllerFeatures" => {
                if let Some(controller) = self.comment_controller_mut(handle) {
                    controller.context_value = arg(1)["contextValue"].as_str().map(str::to_owned);
                    controller.reaction_handler = arg(1)["reactionHandler"] == true;
                }
            }
            "$unregisterCommentController" => self.comments.controllers.retain(|controller| controller.handle != handle),
            "$createCommentThread" => {
                let Some(controller) = self.comment_controller_mut(handle) else { return Outcome::done() };
                let range = Some(arg(4)).filter(|range| !range.is_null());
                let comments = arg(5).as_array().cloned().unwrap_or_default();
                // `MainThreadCommentThread`: a new thread is collapsed and can be replied to.
                let mut thread = Thread { handle: arg(1).as_u64().unwrap_or_default(), thread_id: arg(2).as_str().unwrap_or_default().to_owned(), resource: arg(3), range, label: None, context_value: None, comments, collapse_state: 0, revealed: false, can_reply: Value::Bool(true), state: None, is_template: arg(7) == true, state_styled_in: Vec::new() };
                // commentsController.ts `shouldReveal`: a new template without comments shows, its reply form focused,
                // without touching its Collapsed state, so a later collapse changes nothing and the zone stays.
                let reveal = thread.is_template && thread.comments.is_empty();
                thread.revealed = reveal;
                let thread_handle = thread.handle;
                controller.threads.push(thread);
                if reveal {
                    self.expand_comment_reply(handle, thread_handle, window, cx);
                }
            }
            "$updateCommentThread" => {
                let Some(controller) = self.comment_controller_mut(handle) else { return Outcome::done() };
                let thread_handle = arg(1).as_u64().unwrap_or_default();
                let Some(thread) = controller.threads.iter_mut().find(|thread| thread.handle == thread_handle) else { return Outcome::done() };
                let before = thread.comments.clone();
                thread.update(&arg(4));
                if arg(4).get("state").is_some() {
                    let zones = self.comments.widgets.iter().filter(|widget| (widget.controller, widget.thread) == (handle, thread_handle) && matches!(widget.place, Place::Zone(_)));
                    let model_ids: Vec<u32> = zones.map(|widget| widget.model_id).collect();
                    self.comment_thread_mut(handle, thread_handle).expect("found above").state_styled_in.extend(model_ids);
                }
                self.sync_comment_edits(handle, thread_handle, &before, window, cx);
            }
            "$deleteCommentThread" => {
                let thread_handle = arg(1).as_u64().unwrap_or_default();
                if let Some(controller) = self.comment_controller_mut(handle) {
                    controller.threads.retain(|thread| thread.handle != thread_handle);
                }
            }
            "$updateCommentingRanges" => {
                if let Some(controller) = self.comment_controller_mut(handle) {
                    controller.has_commenting_ranges = true;
                }
                self.refresh_commenting_ranges(false, cx);
            }
            "$revealCommentThread" => self.reveal_comment_thread(handle, arg(1).as_u64().unwrap_or_default(), arg(3)["focusReply"] == true, window, cx),
            "$hideCommentThread" => {
                let thread_handle = arg(1).as_u64().unwrap_or_default();
                if let Some(thread) = self.comment_controller_mut(handle).and_then(|controller| controller.threads.iter_mut().find(|thread| thread.handle == thread_handle)) {
                    thread.set_collapse_state(0);
                }
            }
            _ => {}
        }
        self.sync_comment_widgets();
        self.select_first_comment_thread();
        cx.notify();
        Outcome::done()
    }

    fn comment_controller_mut(&mut self, handle: u64) -> Option<&mut Controller> {
        self.comments.controllers.iter_mut().find(|controller| controller.handle == handle)
    }

    /// `registerView` runs once a controller has threads or commenting ranges; the container `hideIfEmpty`.
    pub(super) fn comments_view_registered(&self) -> bool {
        self.comments.controllers.iter().any(|controller| controller.has_commenting_ranges || !controller.threads.is_empty())
    }

    /// `revealCommentThread` for a document thread: its file opens at the start of its range and the thread's zone
    /// expands (`makeVisible`); with `focus_reply` (`CommentWidgetFocus.Editor`) its reply editor opens focused.
    pub(super) fn reveal_comment_thread(&mut self, controller: u64, thread: u64, focus_reply: bool, window: &mut Window, cx: &mut Context<Self>) {
        self.comments.commenting_disabled = false;
        let Some(state) = self.comment_thread_mut(controller, thread) else { return };
        state.set_collapse_state(super::comment_thread_widget::EXPANDED);
        let mut target = state.range.clone().unwrap_or_else(|| serde_json::json!({ "startLineNumber": 1, "startColumn": 1 }));
        target["resource"] = state.resource.clone();
        self.open_marker_related(&target, window, cx);
        self.sync_comment_widgets();
        if focus_reply {
            self.expand_comment_reply(controller, thread, window, cx);
        }
    }
}

impl Thread {
    /// `MainThreadCommentThread.collapsibleState`'s setter: only a change reaches the zone, which then follows it.
    pub(super) fn set_collapse_state(&mut self, state: u64) -> bool {
        if state == self.collapse_state {
            return false;
        }
        self.collapse_state = state;
        // The host's Expanded echo of a revealed template keeps it revealed; a collapse closes it.
        self.revealed &= state == 1;
        true
    }

    /// The zone is shown: Expanded, or revealed and not collapsed since.
    pub(super) fn zone_shown(&self) -> bool {
        self.collapse_state == 1 || self.revealed
    }

    /// `MainThreadCommentThread.batchUpdate`: only the fields the changes carry.
    fn update(&mut self, changes: &Value) {
        let Some(changes) = changes.as_object() else { return };
        if let Some(range) = changes.get("range") {
            self.range = Some(range.clone()).filter(|range| !range.is_null());
        }
        if let Some(label) = changes.get("label") {
            self.label = label.as_str().map(str::to_owned);
        }
        if let Some(context_value) = changes.get("contextValue") {
            self.context_value = context_value.as_str().map(str::to_owned);
        }
        if let Some(comments) = changes.get("comments") {
            self.comments = comments.as_array().cloned().unwrap_or_default();
        }
        if let Some(collapse_state) = changes.get("collapseState").and_then(Value::as_u64) {
            self.set_collapse_state(collapse_state);
        }
        if let Some(can_reply) = changes.get("canReply") {
            self.can_reply = can_reply.clone();
        }
        if let Some(state) = changes.get("state") {
            self.state = state.as_u64();
        }
        if let Some(is_template) = changes.get("isTemplate") {
            self.is_template = is_template == true;
        }
    }
}