дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
Settings › Keymap gains Visual Studio and ReSharper presets for Windows/Linux and macOS, generated from JetBrains' Visual Studio and ReSharper keymaps with the same parent chain (Visual Studio on the OS's IntelliJ preset, ReSharper on Visual Studio). Inherited bindings of context-free actions are dropped where a preset gives their key to another action. IDEA_ACTIONS gains 64 standard IDEA action ids, which also helps imported IDEA keymaps and IdeaVim's :action, and imported keymaps based on Visual Studio, ReSharper or NetBeans pick that preset as their parent.
167 рядки
6.3 KiB
TypeScript
167 рядки
6.3 KiB
TypeScript
/**
|
|
* Import of an IntelliJ IDEA keymap file (`keymaps/<name>.xml` in the IDE's
|
|
* config directory). An IDEA keymap lists only the actions it changes over its
|
|
* parent; each listed action's shortcuts replace the parent's.
|
|
*/
|
|
import { IDEA_ACTIONS } from "@/platform/ideaActions";
|
|
import type { CustomKeymap } from "@/bindings";
|
|
import {
|
|
customKeymapId,
|
|
defaultPreset,
|
|
isValidBinding,
|
|
normalizeBinding,
|
|
PRESET_TITLES,
|
|
withBindings,
|
|
type Keymap,
|
|
} from "@/platform/keymap";
|
|
|
|
export interface ImportedIdeaKeymap {
|
|
name: string;
|
|
/** OpenIDEA preset the keymap is based on. */
|
|
parent: string;
|
|
overrides: Keymap;
|
|
/** IDEA action ids applied. */
|
|
applied: number;
|
|
/** IDEA action ids without an OpenIDEA counterpart. */
|
|
unknownActions: string[];
|
|
/** Shortcuts that cannot be expressed here (double click, unknown keys). */
|
|
skippedShortcuts: number;
|
|
}
|
|
|
|
/** IDEA's bundled keymap names (`<keymap name=…>`) → our presets. */
|
|
function presetOf(parent: string | null): string {
|
|
const p = parent ?? "";
|
|
// "Mac OS X 10.5+", "Eclipse (Mac OS X)", "VSCode (macOS)", "Visual Studio OSX"; not the "mac" inside "Emacs".
|
|
const mac = /\bmac(?: ?os)?\b|\bosx\b/i.test(p);
|
|
if (/^eclipse/i.test(p)) return mac ? "eclipse-macos" : "eclipse-windows";
|
|
if (/^vs ?code/i.test(p)) return mac ? "vscode-macos" : "vscode-windows";
|
|
if (/^netbeans/i.test(p)) return mac ? "netbeans-macos" : "netbeans-windows";
|
|
if (/^visual studio/i.test(p)) return mac ? "visualstudio-macos" : "visualstudio-windows";
|
|
if (/^resharper/i.test(p)) return mac ? "resharper-macos" : "resharper-windows";
|
|
if (mac) return "intellij-macos";
|
|
// IDEA's Emacs keymap (and copies of it) inherits $default.
|
|
if (p === "$default" || /^default for/i.test(p) || /^emacs\b/i.test(p)) return "intellij-windows";
|
|
return defaultPreset();
|
|
}
|
|
|
|
/** Swing `KeyEvent.VK_*` names (as IDEA writes them, without `VK_`) → our key names. */
|
|
const SWING_KEYS: Record<string, string> = {
|
|
ENTER: "Enter",
|
|
ESCAPE: "Escape",
|
|
TAB: "Tab",
|
|
SPACE: "Space",
|
|
BACK_SPACE: "Backspace",
|
|
DELETE: "Delete",
|
|
INSERT: "Insert",
|
|
HOME: "Home",
|
|
END: "End",
|
|
PAGE_UP: "PageUp",
|
|
PAGE_DOWN: "PageDown",
|
|
UP: "Up",
|
|
DOWN: "Down",
|
|
LEFT: "Left",
|
|
RIGHT: "Right",
|
|
OPEN_BRACKET: "[",
|
|
CLOSE_BRACKET: "]",
|
|
COMMA: ",",
|
|
PERIOD: ".",
|
|
SLASH: "/",
|
|
BACK_SLASH: "\\",
|
|
SEMICOLON: ";",
|
|
QUOTE: "'",
|
|
BACK_QUOTE: "`",
|
|
MINUS: "-",
|
|
EQUALS: "=",
|
|
ADD: "NumpadAdd",
|
|
SUBTRACT: "NumpadSubtract",
|
|
MULTIPLY: "NumpadMultiply",
|
|
DIVIDE: "NumpadDivide",
|
|
};
|
|
|
|
const SWING_MODS: Record<string, string> = { shift: "Shift", ctrl: "Ctrl", control: "Ctrl", alt: "Alt", meta: "Meta" };
|
|
|
|
/**
|
|
* `"shift meta O"` / `"ctrl pressed OPEN_BRACKET"` → `"Meta+Shift+O"`, or null when not expressible.
|
|
* Bundled keymaps name keys in upper case; the keymaps IDEA saves for the user are all lower case (`"ctrl right"`).
|
|
*/
|
|
export function fromSwingStroke(stroke: string): string | null {
|
|
const words = stroke
|
|
.trim()
|
|
.split(/\s+/)
|
|
.filter((w) => !/^(pressed|released|typed)$/i.test(w));
|
|
const key = words.pop()?.toUpperCase();
|
|
if (!key || words.some((w) => !SWING_MODS[w.toLowerCase()])) return null;
|
|
const name = /^([A-Z0-9]|F\d{1,2})$/.test(key) ? key : SWING_KEYS[key];
|
|
return name ? [...words.map((w) => SWING_MODS[w.toLowerCase()]), name].join("+") : null;
|
|
}
|
|
|
|
/** Converts Swing mouse buttons and wheel directions. */
|
|
function fromMouseStroke(stroke: string): string | null {
|
|
const words = stroke.trim().split(/\s+/);
|
|
const button = words.pop()?.toLowerCase();
|
|
if (!button || !/^button(?:\d|14[23])$/.test(button) || words.some((w) => !SWING_MODS[w.toLowerCase()])) return null;
|
|
const key = button === "button143" ? "WheelUp" : button === "button142" ? "WheelDown" : button;
|
|
return [...words.map((w) => SWING_MODS[w.toLowerCase()]), key].join("+");
|
|
}
|
|
|
|
/** Parses an IDEA keymap XML; throws when the file is not a keymap. */
|
|
export function parseIdeaKeymap(xml: string): ImportedIdeaKeymap {
|
|
const doc = new DOMParser().parseFromString(xml, "application/xml");
|
|
const root = doc.documentElement;
|
|
if (doc.getElementsByTagName("parsererror").length || root.tagName !== "keymap")
|
|
throw new Error("Not an IntelliJ IDEA keymap file");
|
|
const parent = presetOf(root.getAttribute("parent"));
|
|
const collected = new Map<string, string[]>();
|
|
const unknownActions: string[] = [];
|
|
let applied = 0;
|
|
let skippedShortcuts = 0;
|
|
for (const action of Array.from(root.getElementsByTagName("action"))) {
|
|
const ideaId = action.getAttribute("id") ?? "";
|
|
const id = IDEA_ACTIONS[ideaId];
|
|
if (!id) {
|
|
if (ideaId) unknownActions.push(ideaId);
|
|
continue;
|
|
}
|
|
applied++;
|
|
const keys = collected.get(id) ?? [];
|
|
for (const s of Array.from(action.children)) {
|
|
let binding: string | null = null;
|
|
if (s.tagName === "keyboard-shortcut") {
|
|
const first = fromSwingStroke(s.getAttribute("first-keystroke") ?? "");
|
|
const raw = s.getAttribute("second-keystroke");
|
|
const second = raw == null ? "" : fromSwingStroke(raw);
|
|
binding = first && second !== null ? `${first} ${second}`.trim() : null;
|
|
} else if (s.tagName === "mouse-shortcut") {
|
|
binding = fromMouseStroke(s.getAttribute("keystroke") ?? "");
|
|
} else continue;
|
|
if (binding && isValidBinding(binding)) keys.push(normalizeBinding(binding));
|
|
else skippedShortcuts++;
|
|
}
|
|
// An `<action id=…/>` without shortcuts removes the parent's.
|
|
collected.set(id, keys);
|
|
}
|
|
let overrides: Keymap = {};
|
|
for (const [id, keys] of collected) overrides = withBindings({ preset: parent, overrides }, id, keys);
|
|
return {
|
|
name: root.getAttribute("name")?.trim() || "Imported keymap",
|
|
parent,
|
|
overrides,
|
|
applied,
|
|
unknownActions,
|
|
skippedShortcuts,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Adds a parsed keymap to the user keymaps under its own name, replacing an
|
|
* earlier import of the same name (a name a bundled keymap has gets a suffix).
|
|
*/
|
|
export function addIdeaKeymap(
|
|
customs: Record<string, CustomKeymap>,
|
|
parsed: ImportedIdeaKeymap,
|
|
): { id: string; customs: Record<string, CustomKeymap> } {
|
|
const name = Object.values(PRESET_TITLES).includes(parsed.name) ? `${parsed.name} (JetBrains)` : parsed.name;
|
|
const id = Object.keys(customs).find((k) => customs[k]?.name === name) ?? customKeymapId(name, customs);
|
|
return { id, customs: { ...customs, [id]: { name, parent: parsed.parent, overrides: parsed.overrides } } };
|
|
}
|