Файли
openidea/src/features/vcs/diffRangeView.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

62 рядки
2.3 KiB
TypeScript

import { EditorSelection, EditorState, StateEffect, StateField, type Transaction } from "@codemirror/state";
import { Decoration, EditorView } from "@codemirror/view";
import { isExternalChange } from "@/editor/api";
import { mapDiffRange, type DiffRange } from "./stagingRange";
export const setDiffRange = StateEffect.define<DiffRange | null>();
function updatedRange(tr: Transaction, range: DiffRange | null) {
for (const effect of tr.effects) if (effect.is(setDiffRange)) return effect.value;
if (!range || !tr.docChanged) return range;
return mapDiffRange(range, tr.startState.doc, tr.newDoc, tr.changes) ?? { from: 0, to: 0, line: 0, lines: 0 };
}
const rangeField = StateField.define<DiffRange | null>({
create: () => null,
update: (range, tr) => updatedRange(tr, range),
});
const hidden = EditorView.decorations.compute([rangeField, "doc"], (state) => {
const range = state.field(rangeField);
if (!range) return Decoration.none;
const hide = Decoration.replace({ block: true, inclusive: true });
return Decoration.set([
...(range.from > 0 ? [hide.range(0, range.from)] : []),
...(range.to < state.doc.length ? [hide.range(range.to, state.doc.length)] : []),
]);
});
/** Show only the excerpt while keeping the original document, syntax and edit history. */
export const diffRangeView = [
rangeField,
hidden,
EditorState.changeFilter.of((tr) => {
const range = tr.startState.field(rangeField);
if (
!range ||
!tr.docChanged ||
isExternalChange(tr) ||
tr.isUserEvent("revert") ||
tr.isUserEvent("undo") ||
tr.isUserEvent("redo")
)
return true;
let outside = false;
tr.changes.iterChangedRanges((from, to) => {
if (from < range.from || to > range.to) outside = true;
});
return !outside;
}),
EditorState.transactionFilter.of((tr) => {
const range = updatedRange(tr, tr.startState.field(rangeField));
if (!range) return tr;
const selection = tr.newSelection;
const clamp = (pos: number) => Math.max(range.from, Math.min(range.to, pos));
const clipped = EditorSelection.create(
selection.ranges.map((r) => EditorSelection.range(clamp(r.anchor), clamp(r.head))),
selection.mainIndex,
);
return clipped.eq(selection) ? tr : [tr, { selection: clipped, sequential: true }];
}),
];