дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 01:31:41 +02:00
The indent guide in the caret's column, within the block the caret line belongs to, takes the selected indent guide colour, and guides sit slightly left of their column so a caret there is to the right of the line. With the caret at a brace of a multi-line block, that block's guide takes the matched-brace colour. A double-click that puts the caret next to { or } (also past the end of a line ending in {) selects the block, both braces included; a double-click on a word still selects the word.
Co-authored-by: PLZY <[email protected]>
54 рядки
2.4 KiB
TypeScript
54 рядки
2.4 KiB
TypeScript
/**
|
|
* A double-click that puts the caret next to a curly brace selects the block it opens or
|
|
* closes, both braces included, as IDEA does: past the end of a line ending in `{` too.
|
|
* A double-click on a word still selects the word.
|
|
*/
|
|
import { matchBrackets } from "@codemirror/language";
|
|
import { EditorSelection, Prec, type EditorState } from "@codemirror/state";
|
|
import { EditorView } from "@codemirror/view";
|
|
|
|
/** The range from the brace at `at` to its match, both included, or null when it has none. */
|
|
export function braceBlock(state: EditorState, at: number): { from: number; to: number } | null {
|
|
const ch = state.sliceDoc(at, at + 1);
|
|
if (ch !== "{" && ch !== "}") return null;
|
|
const match = ch === "{" ? matchBrackets(state, at, 1) : matchBrackets(state, at + 1, -1);
|
|
if (!match?.matched || !match.end) return null;
|
|
return { from: Math.min(match.start.from, match.end.from), to: Math.max(match.start.to, match.end.to) };
|
|
}
|
|
|
|
const isBrace = (ch: string) => ch === "{" || ch === "}";
|
|
|
|
/**
|
|
* The brace next to the caret position `pos` a click at `x` gives, unless the click is on a
|
|
* word character (that double-click selects the word).
|
|
*/
|
|
export function braceNextTo(state: EditorState, pos: number, onWord: boolean): number | null {
|
|
if (onWord) return null;
|
|
if (pos > 0 && isBrace(state.sliceDoc(pos - 1, pos))) return pos - 1;
|
|
if (isBrace(state.sliceDoc(pos, pos + 1))) return pos;
|
|
return null;
|
|
}
|
|
|
|
/** Whether the click at `x` is on a word character around `pos`. */
|
|
function clickOnWord(view: EditorView, pos: number, x: number): boolean {
|
|
for (const at of [pos - 1, pos]) {
|
|
if (at < 0 || at >= view.state.doc.length) continue;
|
|
const box = view.coordsForChar(at);
|
|
if (box && x >= box.left && x <= box.right) return /[\p{L}\p{N}_$]/u.test(view.state.sliceDoc(at, at + 1));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
export const bracketBlockSelect = Prec.high(
|
|
EditorView.mouseSelectionStyle.of((view, event) => {
|
|
if (event.detail !== 2 || event.button !== 0 || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey)
|
|
return null;
|
|
const pos = view.posAtCoords({ x: event.clientX, y: event.clientY });
|
|
const at = pos == null ? null : braceNextTo(view.state, pos, clickOnWord(view, pos, event.clientX));
|
|
const block = at == null ? null : braceBlock(view.state, at);
|
|
if (!block) return null;
|
|
const selection = EditorSelection.single(block.from, block.to);
|
|
return { get: () => selection, update: () => false };
|
|
}),
|
|
);
|