дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 10:07:11 +02:00
Editing and folding
- Move Caret to Code Block Start / End with Selection, Auto-Indent Lines,
Move Element Left / Right (swaps call arguments, parameters and list
entries).
- Fold Code Block folds the {...} block around the caret; Expand to Level
and Expand All to Level 1-5.
- Custom folding regions (//region, #region, <editor-fold>) fold to their
description; Navigate > Custom Folding lists them.
- Error Description shows the problems at the caret; Next / Previous
Highlighted Usage steps through usage highlights.
- Editor tooltips stay below the editor's top edge, so hints on the first
lines no longer hide under the tab bar.
Navigation, project and run
- Navigate > File Path lists the file and its folders to reveal in the file
manager.
- Copy... (F5) opens the Copy dialog for the selection or the open file; the
project view's Refactor submenu gets Copy File.
- Debug... chooses a configuration to debug; Quick Evaluate Expression shows
the selection's value in an expandable hint.
- Inspect Current File runs the inspection without the scope dialog.
- Surround with Live Template lists only $SELECTION$ templates; Java gets
Callable, read / write lock and iterate templates.
Windows
- Stretch the active tool window left / right / up / down, Restore Default
Layout, Go to Next / Previous Splitter, Close Active Tab on a second key,
Stop Background Processes.
All actions have default shortcuts in both keymaps, VS Code preset entries,
main menu items and Chinese translations.
Project view and Keymap settings
- Project view Options: Sort by Name / Type / Modification Time (also in
Find Action).
- Settings > Keymap: Add Mouse Shortcut records a side-button click with
modifiers and checks conflicts like keyboard shortcuts.
- Folding: Expand to Level / Expand All to Level 1-5.
142 рядки
4.8 KiB
TypeScript
142 рядки
4.8 KiB
TypeScript
/**
|
|
* Move Element Left / Right (⌥⇧⌘← / →): swaps the element at the caret with its
|
|
* neighbour in the enclosing comma-separated list — call arguments, parameters,
|
|
* array and object literal entries.
|
|
*/
|
|
import { EditorSelection, type EditorState } from "@codemirror/state";
|
|
import type { Command } from "@codemirror/view";
|
|
import { syntaxTree } from "@codemirror/language";
|
|
|
|
const OPEN = "([{";
|
|
const CLOSE = ")]}";
|
|
const NOT_CODE = /String|Comment|Regexp|Char|Template/;
|
|
/** Lists longer than this are not scanned. */
|
|
const MAX_SCAN = 20_000;
|
|
|
|
function inCode(state: EditorState, pos: number): boolean {
|
|
const tree = syntaxTree(state);
|
|
return !tree.length || !NOT_CODE.test(tree.resolveInner(pos, 1).name);
|
|
}
|
|
|
|
/** `[open, close]` offsets of the innermost bracket pair around `pos` that holds a comma list. */
|
|
function enclosingList(state: EditorState, pos: number): [number, number] | null {
|
|
const doc = state.doc;
|
|
const min = Math.max(0, pos - MAX_SCAN);
|
|
const stack: string[] = [];
|
|
for (let p = pos - 1; p >= min; p--) {
|
|
const ch = doc.sliceString(p, p + 1);
|
|
const close = CLOSE.indexOf(ch);
|
|
const open = OPEN.indexOf(ch);
|
|
if (close < 0 && open < 0) continue;
|
|
if (!inCode(state, p)) continue;
|
|
if (close >= 0) stack.push(OPEN[close]!);
|
|
else if (stack.length) {
|
|
if (stack.pop() !== ch) return null;
|
|
} else {
|
|
const end = matchingClose(state, p, ch);
|
|
if (end != null && end >= pos) return [p, end];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function matchingClose(state: EditorState, open: number, ch: string): number | null {
|
|
const doc = state.doc;
|
|
const want = CLOSE[OPEN.indexOf(ch)]!;
|
|
const max = Math.min(doc.length, open + MAX_SCAN);
|
|
let depth = 0;
|
|
for (let p = open + 1; p < max; p++) {
|
|
const c = doc.sliceString(p, p + 1);
|
|
if (c === ch && inCode(state, p)) depth++;
|
|
else if (c === want && inCode(state, p)) {
|
|
if (depth === 0) return p;
|
|
depth--;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** The trimmed top-level elements between `from` and `to`. */
|
|
export function listElements(state: EditorState, from: number, to: number): { from: number; to: number }[] {
|
|
const text = state.sliceDoc(from, to);
|
|
const out: { from: number; to: number }[] = [];
|
|
let depth = 0;
|
|
let quote: string | null = null;
|
|
let start = 0;
|
|
const push = (end: number) => {
|
|
const raw = text.slice(start, end);
|
|
const lead = raw.length - raw.trimStart().length;
|
|
const trimmed = raw.trim();
|
|
if (trimmed) out.push({ from: from + start + lead, to: from + start + lead + trimmed.length });
|
|
};
|
|
for (let i = 0; i < text.length; i++) {
|
|
const c = text[i]!;
|
|
if (quote) {
|
|
if (c === "\\") i++;
|
|
else if (c === quote) quote = null;
|
|
continue;
|
|
}
|
|
if (c === '"' || c === "'" || c === "`") quote = c;
|
|
else if ("([{".includes(c)) depth++;
|
|
else if (")]}".includes(c)) depth--;
|
|
else if (c === "," && depth === 0) {
|
|
push(i);
|
|
start = i + 1;
|
|
}
|
|
}
|
|
push(text.length);
|
|
return out;
|
|
}
|
|
|
|
/** The swap Move Element would make at `pos`, or null when there is no neighbour. */
|
|
export function elementSwap(
|
|
state: EditorState,
|
|
pos: number,
|
|
dir: -1 | 1,
|
|
): { a: { from: number; to: number }; b: { from: number; to: number }; caret: number } | null {
|
|
// The innermost list with more than one element: in `f(g(x), y)` at `x`, `g(x)` moves.
|
|
let items: { from: number; to: number }[] = [];
|
|
for (let at = pos, depth = 0; items.length < 2; depth++) {
|
|
const list = depth < 16 ? enclosingList(state, at) : null;
|
|
if (!list) return null;
|
|
items = listElements(state, list[0] + 1, list[1]);
|
|
at = list[0];
|
|
}
|
|
// The last element starting at or before the caret (the one before a comma it follows).
|
|
let i = 0;
|
|
items.forEach((it, k) => {
|
|
if (it.from <= pos) i = k;
|
|
});
|
|
const j = i + dir;
|
|
if (j < 0 || j >= items.length) return null;
|
|
const cur = items[i]!;
|
|
const other = items[j]!;
|
|
const offset = Math.max(0, Math.min(pos - cur.from, cur.to - cur.from));
|
|
// Where the moved element starts once both are swapped.
|
|
const start = dir === 1 ? other.to - (cur.to - cur.from) : other.from;
|
|
return { a: cur, b: other, caret: start + offset };
|
|
}
|
|
|
|
function moveElement(dir: -1 | 1): Command {
|
|
return (view) => {
|
|
const { state } = view;
|
|
if (state.readOnly || state.selection.ranges.length > 1) return false;
|
|
const swap = elementSwap(state, state.selection.main.head, dir);
|
|
if (!swap) return false;
|
|
const { a, b } = swap;
|
|
view.dispatch({
|
|
changes: [
|
|
{ from: a.from, to: a.to, insert: state.sliceDoc(b.from, b.to) },
|
|
{ from: b.from, to: b.to, insert: state.sliceDoc(a.from, a.to) },
|
|
],
|
|
selection: EditorSelection.cursor(swap.caret),
|
|
scrollIntoView: true,
|
|
userEvent: "move.element",
|
|
});
|
|
return true;
|
|
};
|
|
}
|
|
|
|
export const moveElementLeft = moveElement(-1);
|
|
export const moveElementRight = moveElement(1);
|