Files
openidea/src/features/vcs/diffOptions.ts
T
l0ng-ai 401c542647 feat(vcs): improve Git workflows (#5174)
Improve OpenIDEA's Git workflows across commit and staging, branch and remote operations, merge and rebase, Git Log and signing, worktrees, stash and shelf, patch application, and clone settings. Adds a one-click stash for tracked changes across Git roots and lets users open a worktree branch's associated GitHub review from its context menu. Cloning now includes submodules by default (Settings › Version Control › Git, can be turned off).

Repository entry points added here run in each repository's execution scope, so they use the project's own git executable.
2026-11-22 21:57:30 +08:00

264 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Diff viewer settings (IDEA's diff toolbar): viewer kind, whitespace policy,
* highlighting, collapsed unchanged fragments and synchronized scrolling, plus
* chunks computed by IDEA's comparison and per-chunk transfer helpers.
*/
import { Change, Chunk, type DiffConfig } from "@codemirror/merge";
import type { ChangeDesc, Text } from "@codemirror/state";
import { compareTexts, type ComparisonPolicy, type LineFragment } from "./comparison";
import { DiffTooBigError } from "./comparison/lcs";
import { findMovedBlocks, type MovedBlock } from "./comparison/moves";
import { stripSpaces } from "./comparison/text";
export type DiffMode = "split" | "unified";
export type IgnorePolicy = "none" | "trim" | "whitespace" | "whitespaceAndLines";
export type HighlightPolicy = "words" | "lines" | "none";
export interface DiffViewOptions {
mode: DiffMode;
ignore: IgnorePolicy;
highlight: HighlightPolicy;
collapse: boolean;
/** Side-by-side panes scroll together. */
syncScroll: boolean;
/**
* Side-by-side panes are padded so that unchanged lines line up. Off, each pane
* keeps its own height and a curved divider joins the changes (IDEA's default).
*/
alignChanges: boolean;
/** Long lines wrap; null follows Settings › Editor › General › Soft-wrap files for the file. */
softWrap: boolean | null;
}
export const IGNORE_OPTIONS: ReadonlyArray<{ value: IgnorePolicy; label: string }> = [
{ value: "none", label: "Do not ignore" },
{ value: "trim", label: "Trim whitespaces" },
{ value: "whitespace", label: "Ignore whitespaces" },
{ value: "whitespaceAndLines", label: "Ignore whitespaces and empty lines" },
];
export const HIGHLIGHT_OPTIONS: ReadonlyArray<{ value: HighlightPolicy; label: string }> = [
{ value: "words", label: "Highlight words" },
{ value: "lines", label: "Highlight lines" },
{ value: "none", label: "Do not highlight" },
];
export const DEFAULT_DIFF_OPTIONS: DiffViewOptions = {
mode: "split",
ignore: "none",
highlight: "words",
collapse: true,
syncScroll: true,
alignChanges: false,
softWrap: null,
};
const KEY = "openidea.vcs.diffOptions";
const oneOf = <T extends string>(v: unknown, values: readonly { value: T }[], fallback: T): T =>
values.some((o) => o.value === v) ? (v as T) : fallback;
/** Parses stored options, falling back to defaults per field. */
export function parseDiffOptions(raw: unknown): DiffViewOptions {
const o = raw && typeof raw === "object" ? (raw as Record<string, unknown>) : {};
const d = DEFAULT_DIFF_OPTIONS;
return {
mode: o.mode === "unified" ? "unified" : "split",
ignore: oneOf(o.ignore, IGNORE_OPTIONS, d.ignore),
highlight: oneOf(o.highlight, HIGHLIGHT_OPTIONS, d.highlight),
collapse: typeof o.collapse === "boolean" ? o.collapse : d.collapse,
syncScroll: typeof o.syncScroll === "boolean" ? o.syncScroll : d.syncScroll,
alignChanges: typeof o.alignChanges === "boolean" ? o.alignChanges : d.alignChanges,
softWrap: typeof o.softWrap === "boolean" ? o.softWrap : d.softWrap,
};
}
export function loadDiffOptions(): DiffViewOptions {
try {
const raw = localStorage.getItem(KEY);
return parseDiffOptions(raw ? JSON.parse(raw) : null);
} catch {
return DEFAULT_DIFF_OPTIONS;
}
}
export function saveDiffOptions(o: DiffViewOptions) {
try {
localStorage.setItem(KEY, JSON.stringify(o));
} catch {
// ignore
}
}
const IDEA = Symbol("ideaDiff");
interface IdeaDiff {
policy: ComparisonPolicy;
words: boolean;
trimChunks: boolean;
}
const COMPARISON_POLICY: Record<IgnorePolicy, ComparisonPolicy> = {
none: "default",
trim: "trim",
whitespace: "ignore",
whitespaceAndLines: "ignore",
};
/**
* A MergeView `diffConfig` whose chunks come from IDEA's comparison
* (`./comparison`) instead of `@codemirror/merge`'s own diff: lines matched as
* IDEA does, word fragments by IDEA's rules, no merging of nearby changes.
*/
export function ideaDiffConfig(ignore: IgnorePolicy, highlight: HighlightPolicy): DiffConfig {
const idea: IdeaDiff = {
policy: COMPARISON_POLICY[ignore],
words: highlight === "words",
trimChunks: ignore === "whitespaceAndLines",
};
const config: DiffConfig = {};
(config as Record<symbol, IdeaDiff>)[IDEA] = idea;
return config;
}
/** The line fragment a chunk was built from. */
const fragments = new WeakMap<Chunk, LineFragment>();
interface DiffInsights {
readonly moves: readonly MovedBlock[];
readonly whitespaceOnly: boolean;
}
const insights = new WeakMap<readonly Chunk[], DiffInsights>();
const NO_INSIGHTS: DiffInsights = { moves: [], whitespaceOnly: false };
export const getDiffInsights = (chunks: readonly Chunk[]): DiffInsights => insights.get(chunks) ?? NO_INSIGHTS;
/**
* Whether the chunk has IDEA's inner fragments (its words were compared):
* such a chunk gets the lighter line background (TextDiffType "ignored"
* colour) with its `changes` in full colour.
*/
export const hasInnerFragments = (chunk: Chunk) => !!fragments.get(chunk)?.inner;
/** IDEA's line fragments as chunks; `changes` hold the inner fragments, if any. */
export function ideaChunks(a: Text, b: Text, ignore: IgnorePolicy, highlight: HighlightPolicy): readonly Chunk[] {
return toChunks(a, b, ideaOf(ideaDiffConfig(ignore, highlight))!);
}
function toChunks(a: Text, b: Text, idea: IdeaDiff): readonly Chunk[] {
const textA = a.toString();
const textB = b.toString();
const remember = (chunks: readonly Chunk[]) => {
insights.set(chunks, {
moves: findMovedBlocks(a, b, chunks),
whitespaceOnly: textA !== textB && stripSpaces(textA) === stripSpaces(textB),
});
return chunks;
};
let compared: LineFragment[];
try {
compared = compareTexts(textA, textB, idea);
} catch (error) {
if (!(error instanceof DiffTooBigError)) throw error;
// Repeated lines can exhaust IDEA's search. Keep the viewer usable with a bounded, coarser diff,
// its blocks without words as IDEA's own.
const coarse = build.call(Chunk, a, b, { scanLimit: 500 });
return remember(coarse.map((c) => new Chunk([], c.fromA, c.toA, c.fromB, c.toB, c.precise)));
}
const chunks = compared.map((f) => {
// `to` is one past the last line's end, past the document for its last line.
const toA = f.startLine1 === f.endLine1 ? f.startOffset1 : f.endLine1 === a.lines ? a.length + 1 : f.endOffset1;
const toB = f.startLine2 === f.endLine2 ? f.startOffset2 : f.endLine2 === b.lines ? b.length + 1 : f.endOffset2;
// A block without inner fragments has no words to mark (the unified viewer marks `changes`).
// A fresh array each: @codemirror/merge caches deleted-line widgets by it.
const changes = f.inner ? f.inner.map((r) => new Change(r.start1, r.end1, r.start2, r.end2)) : [];
const chunk = new Chunk(changes, f.startOffset1, toA, f.startOffset2, toB);
fragments.set(chunk, f);
return chunk;
});
return remember(chunks);
}
// Every chunk computation of MergeView and unifiedMergeView goes through these.
const ideaOf = (conf?: DiffConfig) => (conf as Record<symbol, IdeaDiff | undefined> | undefined)?.[IDEA];
const { build, updateA, updateB } = Chunk;
Chunk.build = (a: Text, b: Text, conf?: DiffConfig) => {
const idea = ideaOf(conf);
return idea ? toChunks(a, b, idea) : build.call(Chunk, a, b, conf);
};
Chunk.updateA = (chunks: readonly Chunk[], a: Text, b: Text, changes: ChangeDesc, conf?: DiffConfig) => {
const idea = ideaOf(conf);
return idea ? toChunks(a, b, idea) : updateA.call(Chunk, chunks, a, b, changes, conf);
};
Chunk.updateB = (chunks: readonly Chunk[], a: Text, b: Text, changes: ChangeDesc, conf?: DiffConfig) => {
const idea = ideaOf(conf);
return idea ? toChunks(a, b, idea) : updateB.call(Chunk, chunks, a, b, changes, conf);
};
/** Text of lines `[from, to)` (0-based), without the last line break. */
const linesContent = (doc: Text, from: number, to: number) => doc.sliceString(doc.line(from + 1).from, doc.line(to).to);
/**
* The change that copies a chunk's content from one side to the other
* (IDEA's ">>" / "<<" and Revert), in the destination's coordinates. Lines are
* replaced as DiffUtil.applyModification does, so a missing final line break
* stays missing.
*/
export function transferChunk(
chunk: Chunk,
source: "a" | "b",
src: Text,
dst: Text,
ranges?: { source: import("./stagingRange").DiffRange; destination: import("./stagingRange").DiffRange },
) {
const f = fragments.get(chunk);
if (!f && !ranges) return transferByOffsets(chunk, source, src, dst);
const wholeLines = (doc: Text, range: NonNullable<typeof ranges>["source"], from: number, to: number) => {
const start = Math.max(0, doc.lineAt(Math.min(range.from + from, range.to)).number - 1 - range.line);
const end =
to === from ? start : Math.max(start, doc.lineAt(Math.min(range.from + to - 1, range.to)).number - range.line);
return [start, end] as const;
};
const fromA = source === "a";
const a = f
? ([f.startLine1, f.endLine1] as const)
: wholeLines(fromA ? src : dst, fromA ? ranges!.source : ranges!.destination, chunk.fromA, chunk.toA);
const b = f
? ([f.startLine2, f.endLine2] as const)
: wholeLines(fromA ? dst : src, fromA ? ranges!.destination : ranges!.source, chunk.fromB, chunk.toB);
const restore = (span: readonly [number, number], range: NonNullable<typeof ranges>["source"] | undefined) =>
range
? ([range.line + Math.min(span[0], range.lines), range.line + Math.min(span[1], range.lines)] as const)
: span;
const [srcFrom, srcTo] = restore(fromA ? a : b, ranges?.source);
const [dstFrom, dstTo] = restore(fromA ? b : a, ranges?.destination);
if (dstFrom === dstTo) {
if (srcFrom === srcTo) return { from: 0, to: 0, insert: "" };
const text = linesContent(src, srcFrom, srcTo);
if (ranges && dst.length === 0) return { from: 0, to: 0, insert: text };
return dstFrom === dst.lines
? { from: dst.length, to: dst.length, insert: "\n" + text }
: { from: dst.line(dstFrom + 1).from, to: dst.line(dstFrom + 1).from, insert: text + "\n" };
}
let from = dst.line(dstFrom + 1).from;
let to = dst.line(dstTo).to;
if (srcFrom === srcTo) {
// Lines removed with a line break: the one before them, or else the one after.
if (from > 0) from--;
else if (to < dst.length) to++;
return { from, to, insert: "" };
}
return { from, to, insert: linesContent(src, srcFrom, srcTo) };
}
/** `transferChunk` for chunks not built from line fragments. */
function transferByOffsets(chunk: Chunk, source: "a" | "b", src: Text, dst: Text) {
const [srcFrom, srcTo, dstFrom, dstTo] =
source === "a"
? [chunk.fromA, chunk.toA, chunk.fromB, chunk.toB]
: [chunk.fromB, chunk.toB, chunk.fromA, chunk.toA];
let insert = src.sliceString(srcFrom, Math.max(srcFrom, srcTo - 1));
if (srcFrom !== srcTo && dstTo <= dst.length) insert += "\n";
return { from: dstFrom, to: Math.min(dst.length, dstTo), insert };
}