дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
The Settings window starts from one window_take_settings_init snapshot (launch, project, global settings, remote, updates) instead of several startup calls; on a remote session it paints with the client settings and reconciles with the host's projectCurrent() afterwards, rebasing without touching staged edits. Seven heavy pages (Color Scheme, Inspections, Scopes, Updates, Deployment, Run Targets, SSH) are split into a spec module (metadata and search labels, loaded with the registry) and a lazily loaded body; lazyComponent gains preload() so the target page is warmed before it is revealed. A launch that arrives between the snapshot and the event listener is still applied. Co-authored-by: dengliu <[email protected]>
260 рядки
10 KiB
TypeScript
260 рядки
10 KiB
TypeScript
/**
|
|
* Keymap editing logic: effective bindings for a staged preset + overrides
|
|
* (plugins' default shortcuts count as the preset's), add / remove / reset,
|
|
* conflict detection with `when` overlap, and shortcut search.
|
|
*/
|
|
import { allActions, type Action } from "@/platform/actions";
|
|
import { evaluateWhen, type ContextKeys } from "@/platform/context";
|
|
import {
|
|
customKeymapId,
|
|
defaultPreset,
|
|
normalizeBinding,
|
|
presetBindings,
|
|
PRESET_TITLES,
|
|
PRESETS,
|
|
useKeymap,
|
|
withBindings,
|
|
type Keymap,
|
|
type KeymapDraft,
|
|
} from "@/platform/keymap";
|
|
|
|
export { presetBindings, withBindings, type KeymapDraft };
|
|
|
|
export function keymapSearchLabels(): string[] {
|
|
const draft = useKeymap.getState();
|
|
return allActions()
|
|
.filter((a) => isKeymapAction(a, draft))
|
|
.map((a) => a.title);
|
|
}
|
|
|
|
export function resolveBindings(draft: KeymapDraft, id: string): string[] {
|
|
return draft.overrides[id]?.map(normalizeBinding) ?? presetBindings(draft.preset, id);
|
|
}
|
|
|
|
/** Hidden actions with shortcuts stay configurable even after their bindings are removed. */
|
|
export function isKeymapAction(action: Action, draft: KeymapDraft): boolean {
|
|
return (
|
|
!action.hidden ||
|
|
!!action.keymap ||
|
|
action.id in draft.overrides ||
|
|
presetBindings(draft.preset, action.id).length > 0
|
|
);
|
|
}
|
|
|
|
export function isModified(draft: KeymapDraft, id: string): boolean {
|
|
return id in draft.overrides;
|
|
}
|
|
|
|
export function addBinding(draft: KeymapDraft, id: string, binding: string): Keymap {
|
|
return withBindings(draft, id, [...resolveBindings(draft, id), binding]);
|
|
}
|
|
|
|
export function removeBinding(draft: KeymapDraft, id: string, binding: string): Keymap {
|
|
const b = normalizeBinding(binding);
|
|
return withBindings(
|
|
draft,
|
|
id,
|
|
resolveBindings(draft, id).filter((x) => x !== b),
|
|
);
|
|
}
|
|
|
|
export function resetBindings(draft: KeymapDraft, id: string): Keymap {
|
|
const overrides = { ...draft.overrides };
|
|
delete overrides[id];
|
|
return overrides;
|
|
}
|
|
|
|
/** Re-bases overrides on another preset: drops overrides that now equal the new defaults. */
|
|
export function switchPreset(draft: KeymapDraft, preset: string): KeymapDraft {
|
|
const next: KeymapDraft = { preset, overrides: {} };
|
|
for (const [id, keys] of Object.entries(draft.overrides)) next.overrides = withBindings(next, id, keys);
|
|
return next;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// `when` overlap
|
|
|
|
const COMPARISON = /([\w.\-:/]+)\s*(==|!=)\s*('[^']*'|"[^"]*"|[\w.\-:/]+)/g;
|
|
const IDENT = /[A-Za-z_][\w.\-:/]*/g;
|
|
const MAX_COMBINATIONS = 4096;
|
|
|
|
function literal(raw: string): string | number | boolean {
|
|
if (/^['"]/.test(raw)) return raw.slice(1, -1);
|
|
if (raw === "true") return true;
|
|
if (raw === "false") return false;
|
|
return Number.isFinite(+raw) ? +raw : raw;
|
|
}
|
|
|
|
type Value = string | number | boolean | undefined;
|
|
|
|
/**
|
|
* Whether two `when` clauses can be true at the same time. Enumerates the
|
|
* context keys they mention (true / unset / each compared literal / other) and
|
|
* evaluates both. Focus keys are modelled as one focus location: `editor…Focus`
|
|
* keys only hold in the editor, a view key (`projectViewFocus`, `terminalFocus`…)
|
|
* only in that view, `toolWindowFocus` in any view. Too many keys, or an
|
|
* unparsable clause, counts as overlapping.
|
|
*/
|
|
export function whensOverlap(a: string | undefined, b: string | undefined): boolean {
|
|
if (!a?.trim() || !b?.trim() || a.trim() === b.trim()) return true;
|
|
const domain = new Map<string, Set<Value>>();
|
|
const add = (key: string, v?: Value) => {
|
|
let set = domain.get(key);
|
|
if (!set) domain.set(key, (set = new Set<Value>([true, undefined, "\u0000other"])));
|
|
if (v !== undefined) set.add(v);
|
|
};
|
|
for (const src of [a, b]) {
|
|
const rest = src.replace(COMPARISON, (_m, key: string, _op, raw: string) => {
|
|
add(key, literal(raw));
|
|
return " ";
|
|
});
|
|
for (const m of rest.match(IDENT) ?? []) add(m);
|
|
}
|
|
const focus = [...domain.keys()].filter((k) => /Focus$/.test(k));
|
|
const editorKeys = focus.filter((k) => k.startsWith("editor"));
|
|
const viewKeys = focus.filter((k) => !k.startsWith("editor") && k !== "toolWindowFocus");
|
|
const locations = ["none", "editor", "toolWindow", ...viewKeys];
|
|
// Free variables: everything except the derived view / tool-window focus keys.
|
|
const free = [...domain.keys()].filter((k) => !viewKeys.includes(k) && k !== "toolWindowFocus");
|
|
const values = free.map((k) => (editorKeys.includes(k) ? [true, false] : [...domain.get(k)!]));
|
|
const total = values.reduce((n, v) => n * v.length, 1) * locations.length;
|
|
if (total > MAX_COMBINATIONS) return true;
|
|
try {
|
|
for (const loc of locations) {
|
|
for (let i = 0; i < total / locations.length; i++) {
|
|
const ctx: ContextKeys = {};
|
|
let rem = i;
|
|
free.forEach((k, ki) => {
|
|
const vs = values[ki]!;
|
|
ctx[k] = vs[rem % vs.length];
|
|
rem = Math.floor(rem / vs.length);
|
|
});
|
|
for (const k of editorKeys) if (loc !== "editor") ctx[k] = false;
|
|
for (const k of viewKeys) ctx[k] = loc === k;
|
|
ctx.toolWindowFocus = loc === "toolWindow" || viewKeys.includes(loc);
|
|
if (evaluateWhen(a, ctx) && evaluateWhen(b, ctx)) return true;
|
|
}
|
|
}
|
|
} catch {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Conflicts
|
|
|
|
export interface Conflict {
|
|
actionId: string;
|
|
title: string;
|
|
category?: string;
|
|
binding: string;
|
|
/** `same`: identical binding; `prefix`: one is the first stroke of the other's chord. */
|
|
kind: "same" | "prefix";
|
|
}
|
|
|
|
/** Other actions whose bindings clash with `binding` in an overlapping `when` context. */
|
|
export function findConflicts(
|
|
draft: KeymapDraft,
|
|
binding: string,
|
|
actionId: string,
|
|
actions: readonly Pick<Action, "id" | "title" | "category" | "when">[],
|
|
): Conflict[] {
|
|
const b = normalizeBinding(binding);
|
|
const self = actions.find((x) => x.id === actionId);
|
|
const out: Conflict[] = [];
|
|
for (const other of actions) {
|
|
if (other.id === actionId) continue;
|
|
for (const ob of resolveBindings(draft, other.id)) {
|
|
const kind = ob === b ? "same" : ob.startsWith(b + " ") || b.startsWith(ob + " ") ? "prefix" : null;
|
|
if (!kind || !whensOverlap(self?.when, other.when)) continue;
|
|
out.push({ actionId: other.id, title: other.title, category: other.category, binding: ob, kind });
|
|
}
|
|
}
|
|
return out.sort((x, y) => (x.kind === y.kind ? x.title.localeCompare(y.title) : x.kind === "same" ? -1 : 1));
|
|
}
|
|
|
|
/** Overrides with every conflicting assignment removed (IDEA's "Remove other assignments"). */
|
|
export function removeConflicts(draft: KeymapDraft, conflicts: readonly Conflict[]): Keymap {
|
|
let overrides = draft.overrides;
|
|
for (const c of conflicts) overrides = removeBinding({ preset: draft.preset, overrides }, c.actionId, c.binding);
|
|
return overrides;
|
|
}
|
|
|
|
/** Shortcut filter: an action matches when one of its bindings equals or starts with `strokes`. */
|
|
export function bindingMatches(bindings: readonly string[], strokes: string): boolean {
|
|
if (!strokes) return true;
|
|
const q = normalizeBinding(strokes);
|
|
return bindings.some((b) => b === q || b.startsWith(q + " "));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// User keymaps (gear menu: Duplicate… / Rename… / Restore Defaults / Delete)
|
|
|
|
export interface CustomKeymap {
|
|
name: string;
|
|
parent: string;
|
|
overrides: Keymap;
|
|
}
|
|
|
|
export type CustomKeymaps = Record<string, CustomKeymap>;
|
|
|
|
/** Base preset + overrides of the selected keymap (a bundled preset or a user keymap). */
|
|
export function activeDraft(selected: string, bundledOverrides: Keymap, customs: CustomKeymaps): KeymapDraft {
|
|
const custom = PRESETS[selected] ? undefined : customs[selected];
|
|
if (custom) return { preset: PRESETS[custom.parent] ? custom.parent : defaultPreset(), overrides: custom.overrides };
|
|
return { preset: PRESETS[selected] ? selected : defaultPreset(), overrides: bundledOverrides };
|
|
}
|
|
|
|
/** Display name of a keymap id. */
|
|
export function keymapName(id: string, customs: CustomKeymaps): string {
|
|
return customs[id]?.name ?? PRESET_TITLES[id] ?? id;
|
|
}
|
|
|
|
/** `"<name> copy"`, `"<name> copy 2"`, … not used by any keymap yet. */
|
|
export function copyName(name: string, customs: CustomKeymaps): string {
|
|
const taken = new Set([...Object.values(customs).map((k) => k.name), ...Object.values(PRESET_TITLES)]);
|
|
const base = `${name} copy`;
|
|
if (!taken.has(base)) return base;
|
|
for (let i = 2; ; i++) if (!taken.has(`${base} ${i}`)) return `${base} ${i}`;
|
|
}
|
|
|
|
/** Error text for a proposed keymap name, or null when valid. */
|
|
export function validateKeymapName(name: string, customs: CustomKeymaps, ownId?: string): string | null {
|
|
const n = name.trim();
|
|
if (!n) return "Keymap name cannot be empty";
|
|
const clash =
|
|
Object.values(PRESET_TITLES).includes(n) || Object.entries(customs).some(([id, k]) => id !== ownId && k.name === n);
|
|
return clash ? `Keymap '${n}' already exists` : null;
|
|
}
|
|
|
|
/** Copies the selected keymap (its effective overrides) under `name`; returns the new id. */
|
|
export function duplicateKeymap(
|
|
draft: KeymapDraft,
|
|
customs: CustomKeymaps,
|
|
name: string,
|
|
): { id: string; customs: CustomKeymaps } {
|
|
const id = customKeymapId(name, customs);
|
|
return {
|
|
id,
|
|
customs: { ...customs, [id]: { name: name.trim(), parent: draft.preset, overrides: { ...draft.overrides } } },
|
|
};
|
|
}
|
|
|
|
export function renameKeymap(customs: CustomKeymaps, id: string, name: string): CustomKeymaps {
|
|
const k = customs[id];
|
|
return k ? { ...customs, [id]: { ...k, name: name.trim() } } : customs;
|
|
}
|
|
|
|
/** Removes a user keymap; the selection falls back to its parent preset. */
|
|
export function deleteKeymap(customs: CustomKeymaps, id: string): { customs: CustomKeymaps; selected: string } {
|
|
const { [id]: removed, ...rest } = customs;
|
|
return { customs: rest, selected: removed && PRESETS[removed.parent] ? removed.parent : defaultPreset() };
|
|
}
|
|
|
|
/** Stores edited overrides on the user keymap `id`. */
|
|
export function withCustomOverrides(customs: CustomKeymaps, id: string, overrides: Keymap): CustomKeymaps {
|
|
const k = customs[id];
|
|
return k ? { ...customs, [id]: { ...k, overrides } } : customs;
|
|
}
|