Visual Studio Code, rebuilt in Rust
Rust
//! commentsEditorContribution.ts: Collapse All, Expand All and Expand Unresolved Comments, Dismiss Comment, Toggle
//! Editor Commenting, Focus Comment on Current Line, and the controller's next and previous comment thread (Alt+F9,
//! Shift+Alt+F9), commented range (Alt+F10, Shift+Alt+F10) and commenting range. The thread reached by next and
//! previous thread or Focus Comment on Current Line takes the focus in its comments (`CommentWidgetFocus.Widget`).
use gpui::{Context, Window};
use serde_json::{Value, json};
use super::Workbench;
use crate::commands::Outcome;
use crate::editor::{Position, Selection, VerticalReveal};
/// `CommentThreadCollapsibleState`.
const COLLAPSED: u64 = 0;
const EXPANDED: u64 = 1;
/// `CommentThreadState.Unresolved`.
const UNRESOLVED: u64 = 0;
#[cfg(test)]
mod tests;
impl Workbench {
pub(super) fn register_comment_navigation_commands(&mut self) {
self.commands.register("workbench.action.collapseAllComments", |workbench, _, _, cx| workbench.change_all_collapse_states(|_| COLLAPSED, cx));
self.commands.register("workbench.action.expandAllComments", |workbench, _, _, cx| workbench.change_all_collapse_states(|_| EXPANDED, cx));
self.commands.register("workbench.action.expandUnresolvedComments", |workbench, _, _, cx| {
workbench.change_all_collapse_states(|state| if state == Some(UNRESOLVED) { EXPANDED } else { COLLAPSED }, cx)
});
self.commands.register("editor.action.nextCommentThreadAction", |workbench, _, window, cx| {
workbench.reveal_nearest_comment_thread(false, true, window, cx);
Outcome::done()
});
self.commands.register("editor.action.previousCommentThreadAction", |workbench, _, window, cx| {
workbench.reveal_nearest_comment_thread(true, true, window, cx);
Outcome::done()
});
// `nextCommentThread(false)`: the same move without focusing the widget.
self.commands.register("editor.action.nextCommentedRangeAction", |workbench, _, window, cx| {
workbench.reveal_nearest_comment_thread(false, false, window, cx);
Outcome::done()
});
self.commands.register("editor.action.previousCommentedRangeAction", |workbench, _, window, cx| {
workbench.reveal_nearest_comment_thread(true, false, window, cx);
Outcome::done()
});
self.commands.register("workbench.action.toggleCommenting", |workbench, _, _, cx| {
workbench.comments.commenting_disabled = !workbench.comments.commenting_disabled;
workbench.sync_comment_widgets();
cx.notify();
Outcome::done()
});
self.commands.register("workbench.action.focusCommentOnCurrentLine", |workbench, _, window, cx| {
match workbench.expand_comment_thread_at_cursor() {
Some((controller, thread)) => workbench.focus_comment_thread(controller, thread, window, cx),
None => workbench.notify_error("The cursor must be on a line with a comment to focus the comment"),
}
cx.notify();
Outcome::done()
});
self.commands.register("editor.action.nextCommentingRange", |workbench, _, window, cx| {
workbench.reveal_nearest_commenting_range(false, window, cx);
Outcome::done()
});
self.commands.register("editor.action.previousCommentingRange", |workbench, _, window, cx| {
workbench.reveal_nearest_commenting_range(true, window, cx);
Outcome::done()
});
// `CommentCommandId.Submit`: the focused reply or edit form's first enabled action.
self.commands.register("editor.action.submitComment", |workbench, _, window, cx| {
match workbench.focused_comment_form().map(|form| form.comment.is_some()) {
Some(true) => workbench.submit_comment_edit(window, cx),
Some(false) => workbench.submit_comment_reply(window, cx),
None => {}
}
Outcome::done()
});
// `CommentCommandId.Hide` in a comment input: `getParentThread().collapse()` selects the thread's range.
self.commands.register("workbench.action.hideComment", |workbench, _, window, cx| {
// `collapseAndFocusRange` of the thread whose comments have the focus: the editor takes it back on the range.
if let Some((controller, thread, _)) = workbench.comments.focused_thread.filter(|_| workbench.comments.thread_focus.is_focused(window)) {
let range = workbench.comment_thread_mut(controller, thread).and_then(|state| {
state.set_collapse_state(COLLAPSED);
state.range.clone()
});
if let Some(range) = range {
workbench.select_comment_range(&range);
}
workbench.sync_comment_widgets();
window.focus(&workbench.editor_focus, cx);
cx.notify();
return Outcome::done();
}
let Some((controller, thread, comment)) = workbench.focused_comment_form().map(|form| (form.controller, form.thread, form.comment)) else { return Outcome::done() };
let state = workbench.comments.controllers.iter_mut().find(|candidate| candidate.handle == controller).and_then(|owner| owner.threads.iter_mut().find(|candidate| candidate.handle == thread));
// The reference leaves a revealed template open on Escape, text typed or not, without the confirmation
// `collapse(true)` would ask for (ui-pair steps-comment-escape-typed.json): its collapse never runs.
let Some(state) = state.filter(|state| !state.revealed) else { return Outcome::done() };
let hides = state.set_collapse_state(COLLAPSED);
let range = state.range.clone();
if hides {
workbench.hide_comment_reply(controller, thread, comment, window, cx);
}
if let Some(range) = range {
workbench.select_comment_range(&range);
}
workbench.sync_comment_widgets();
cx.notify();
Outcome::done()
});
}
/// `changeAllCollapseState`: every thread takes the state `new_state` gives for its `CommentThreadState`.
fn change_all_collapse_states(&mut self, new_state: impl Fn(Option<u64>) -> u64, cx: &mut Context<Self>) -> Outcome {
for thread in self.comments.controllers.iter_mut().flat_map(|controller| &mut controller.threads) {
thread.set_collapse_state(new_state(thread.state));
}
self.sync_comment_widgets();
cx.notify();
Outcome::done()
}
/// `_findNearestCommentThread`: the first thread of the active editor starting after the selection's end (before
/// its start when `reverse`), selected and expanded, its comments focused with `focus_thread`.
fn reveal_nearest_comment_thread(&mut self, reverse: bool, focus_thread: bool, window: &mut Window, cx: &mut Context<Self>) {
let Some(editor) = self.editors.active() else { return };
let uri = editor.document().uri();
let selection = editor.selection();
let after = if reverse { selection.start() } else { selection.end() };
let mut threads: Vec<(u64, u64, Option<Value>)> = Vec::new();
for controller in &self.comments.controllers {
for thread in controller.threads.iter().filter(|thread| thread.resource["path"] == uri["path"] && thread.resource["scheme"] == uri["scheme"]) {
threads.push((controller.handle, thread.handle, thread.range.clone()));
}
}
let Some((controller, thread, range)) = nearest_thread(threads, after, reverse) else { return };
self.select_comment_range(&range.unwrap_or_else(|| json!({})));
if let Some(state) = self.comments.controllers.iter_mut().find(|candidate| candidate.handle == controller).and_then(|owner| owner.threads.iter_mut().find(|candidate| candidate.handle == thread)) {
state.set_collapse_state(EXPANDED);
}
self.sync_comment_widgets();
self.reveal_floating_comment_widget(controller, thread);
match focus_thread {
true => self.focus_comment_thread(controller, thread, window, cx),
false => window.focus(&self.editor_focus, cx),
}
cx.notify();
}
/// commentThreadBody.ts `focus()`: the thread's comments take the focus, no comment yet singled out.
pub(super) fn focus_comment_thread(&mut self, controller: u64, thread: u64, window: &mut Window, cx: &mut Context<Self>) {
self.comments.focused_thread = Some((controller, thread, None));
window.focus(&self.comments.thread_focus, cx);
cx.notify();
}
}
impl Workbench {
/// `_findNearestCommentingRange`: the cursor moves to the nearest commenting range's start (its end in reverse).
fn reveal_nearest_commenting_range(&mut self, reverse: bool, window: &mut Window, cx: &mut Context<Self>) {
let Some(editor) = self.editors.active() else { return };
let line = editor.selection().end().line;
let ranges = self.commenting_ranges(editor);
let Some(position) = nearest_commenting_position(&ranges, line, reverse) else { return };
let viewport = self.editors.viewport;
if let Some(editor) = self.editors.active_mut() {
editor.set_selections(vec![Selection::caret(position)]);
editor.reveal_range(position, position, VerticalReveal::CenterIfOutsideViewport, viewport);
}
window.focus(&self.editor_focus, cx);
cx.notify();
}
/// `getCommentsAtLine` then `revealCommentThread`: the first thread whose range holds the cursor's line expands.
fn expand_comment_thread_at_cursor(&mut self) -> Option<(u64, u64)> {
let editor = self.editors.active()?;
let uri = editor.document().uri();
let line = editor.selection().head.line as u64 + 1;
let holds = |range: &Value| range["startLineNumber"].as_u64().is_some_and(|start| start <= line) && range["endLineNumber"].as_u64().is_some_and(|end| line <= end);
let threads = self.comments.controllers.iter_mut().flat_map(|controller| controller.threads.iter_mut().map(|thread| (controller.handle, thread)));
let mut threads = threads.filter(|(_, thread)| thread.resource["path"] == uri["path"] && thread.resource["scheme"] == uri["scheme"]);
let (controller, thread) = threads.find(|(_, thread)| thread.range.as_ref().is_some_and(holds))?;
thread.set_collapse_state(EXPANDED);
let found = (controller, thread.handle);
self.sync_comment_widgets();
Some(found)
}
/// The active editor selects `range` (an `IRange`, 1-based), revealed in the center when outside the viewport.
fn select_comment_range(&mut self, range: &Value) {
let (start, end) = (position(range, "startLineNumber", "startColumn"), position(range, "endLineNumber", "endColumn"));
let viewport = self.editors.viewport;
let Some(editor) = self.editors.active_mut() else { return };
editor.set_selections(vec![Selection { anchor: start, head: end }]);
editor.reveal_range(start, end, VerticalReveal::CenterIfOutsideViewport, viewport);
}
}
/// `_findNearestCommentingRange`'s target: the nearest range's start, its end in reverse.
fn nearest_commenting_position(ranges: &[(Position, Position)], line: usize, reverse: bool) -> Option<Position> {
let (start, end) = nearest_commenting_range(ranges, line, reverse)?;
let mut position = if reverse { end } else { start };
// `_doUpdate` splits the range holding the cursor's line into decorations at column 1.
if (start.line..=end.line).contains(&line) {
position.column = 0;
}
Some(position)
}
/// `getNearestCommentingRange`: the range containing `line` and the ranges touching it by line are passed over as
/// one; the first range after it (before it in reverse) wins, else the first range.
fn nearest_commenting_range(ranges: &[(Position, Position)], line: usize, reverse: bool) -> Option<(Position, Position)> {
let ordered: Vec<(Position, Position)> = if reverse { ranges.iter().rev().copied().collect() } else { ranges.to_vec() };
let mut containing: Option<(usize, usize)> = None;
for &(start, end) in &ordered {
if let Some((within_start, within_end)) = containing {
if end.line + 1 >= within_start && within_end + 1 >= start.line {
containing = Some((within_start.min(start.line), within_end.max(end.line)));
continue;
}
}
if start.line <= line && line <= end.line {
containing = Some((start.line, end.line));
continue;
}
if (!reverse && end.line < line) || (reverse && start.line > line) {
continue;
}
return Some((start, end));
}
ordered.first().copied()
}
/// The sort and `findFirstIdxMonotonousOrArrLen` of `_findNearestCommentThread`: threads without a range sort first;
/// forward takes the first starting after `after`, reverse the last starting before it.
fn nearest_thread(mut threads: Vec<(u64, u64, Option<Value>)>, after: Position, reverse: bool) -> Option<(u64, u64, Option<Value>)> {
let start = |range: &Option<Value>| range.as_ref().map(|range| position(range, "startLineNumber", "startColumn"));
threads.sort_by_key(|(.., range)| start(range).map(|start| (start.line, start.column)));
if reverse {
threads.reverse();
return threads.into_iter().find(|(.., range)| start(range).is_none_or(|start| (after.line, after.column) > (start.line, start.column)));
}
threads.into_iter().find(|(.., range)| start(range).is_some_and(|start| (start.line, start.column) > (after.line, after.column)))
}
fn position(range: &Value, line: &str, column: &str) -> Position {
let number = |key: &str| (range[key].as_u64().unwrap_or(1) as usize).max(1) - 1;
Position::new(number(line), number(column))
}