Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

3.4 KB
Raw Blame
/**
 * Review in the diff viewer (IntelliJ's `GHPRReviewDiffExtension`): pull request diffs
 * (opened by `openPullRequestFileDiff` / the details panel's commit diffs, context
 * `PULL_REQUEST_DIFF`) get their review threads as inlays, the gutter "+" with single- and
 * multi-line comments, Start Review / Add Single Comment, the review toolbar and the viewed
 * toggle, through the vcs diff viewer provider point. The editor actions also serve review mode's editors.
 */
import { registerDiffViewerProvider } from "@/features/vcs/diffViewerProviders";
import { registerActions } from "@/platform/actions";
import { getContext } from "@/platform/context";
import { useGhRepository } from "../data/repository";
import { activeReviewEditor } from "../reviewMode/editor";
import { clearNewComments } from "./newComments";
import { activeReviewDiff, createReviewDiff } from "./reviewDiff";

/** The editor actions apply to the focused review diff, or to the review in the active editor (review mode). */
const WHEN = "projectOpen && githubConnected && (githubReviewDiff || githubReviewEditor)";

function reviewTarget() {
  const diff = activeReviewDiff();
  return (getContext().diffEditorFocus && diff) || activeReviewEditor() || diff;
}

/** "Mark File as Viewed" / "Mark File as Not Viewed" in the focused review diff; false when none is focused. */
export function toggleReviewDiffViewed(): boolean {
  const diff = activeReviewDiff();
  if (!diff) return false;
  diff.toggleViewed();
  return true;
}

export function activateReviewDiff(): () => void {
  const disposers = [
    registerDiffViewerProvider({ id: "github.review", create: createReviewDiff }),
    registerActions([
      {
        // Code.Review.Editor.New.Comment
        id: "github.pullRequest.diff.addComment",
        title: "Add Review Comment",
        description: "Create a new code review comment on the line under the caret",
        category: "GitHub",
        when: WHEN,
        run: () => reviewTarget()?.addComment(false),
      },
      {
        id: "github.pullRequest.diff.addMultilineComment",
        title: "Add Multiline Review Comment",
        description: "Create a new code review comment on the selected lines",
        category: "GitHub",
        when: WHEN,
        run: () => reviewTarget()?.addComment(true),
      },
      {
        // CodeReview.PreviousComment
        id: "github.pullRequest.diff.previousComment",
        title: "Previous Comment",
        description: "Go to previous visible comment",
        category: "GitHub",
        icon: "arrow-up",
        when: WHEN,
        run: () => void reviewTarget()?.gotoComment(-1),
      },
      {
        // CodeReview.NextComment
        id: "github.pullRequest.diff.nextComment",
        title: "Next Comment",
        description: "Go to next visible comment",
        category: "GitHub",
        icon: "arrow-down",
        when: WHEN,
        run: () => void reviewTarget()?.gotoComment(1),
      },
      {
        id: "github.pullRequest.diff.refreshReview",
        title: "Refresh Review Data",
        category: "GitHub",
        icon: "refresh",
        when: WHEN,
        run: () => reviewTarget()?.refresh(),
      },
    ]),
    useGhRepository.subscribe((s, prev) => {
      const a = s.connection?.coordinates;
      const b = prev.connection?.coordinates;
      if (a?.owner !== b?.owner || a?.name !== b?.name) clearNewComments();
    }),
  ];
  return () => disposers.reverse().forEach((d) => d());
}