Run oxfmt once over the whole repository (formatting only, no logic changes) and add `pnpm format:check` to the frontend CI job so unformatted code no longer builds up. `.oxfmtrc.json` now skips the generated theme docs, test fixtures and code blocks inside Markdown. Co-authored-by: jaw52 <53564781+jaw52@users.noreply.github.com>
60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
/**
|
|
* Temporary range highlight (styled in `theme.ts`) while a chooser moves its selection (the
|
|
* expression / occurrences an Introduce refactoring would take, a postfix
|
|
* template's target…), as in IDEA. Not part of the document state: any
|
|
* edit clears it, and callers clear it when their popup closes.
|
|
*/
|
|
import { StateEffect, StateField, type Extension } from "@codemirror/state";
|
|
import { Decoration, EditorView } from "@codemirror/view";
|
|
|
|
export interface HighlightRange {
|
|
from: number;
|
|
to: number;
|
|
}
|
|
|
|
const setTemporary = StateEffect.define<readonly HighlightRange[]>();
|
|
|
|
const mark = Decoration.mark({ class: "cm-lsp-chooser-highlight" });
|
|
|
|
const temporaryField = StateField.define<readonly HighlightRange[]>({
|
|
create: () => [],
|
|
update(value, tr) {
|
|
for (const e of tr.effects) if (e.is(setTemporary)) return e.value;
|
|
return tr.docChanged && value.length ? [] : value;
|
|
},
|
|
provide: (f) =>
|
|
EditorView.decorations.from(f, (ranges) =>
|
|
Decoration.set(
|
|
ranges.filter((r) => r.from < r.to).map((r) => mark.range(r.from, r.to)),
|
|
true,
|
|
),
|
|
),
|
|
});
|
|
|
|
/** Highlights `ranges` until the next call, `clearTemporaryHighlight` or an edit; scrolls the first into view. */
|
|
export function highlightRanges(view: EditorView, ranges: readonly HighlightRange[]): void {
|
|
const len = view.state.doc.length;
|
|
const clamped = ranges
|
|
.map((r) => ({ from: Math.max(0, Math.min(r.from, len)), to: Math.max(0, Math.min(r.to, len)) }))
|
|
.sort((a, b) => a.from - b.from);
|
|
const effects: StateEffect<unknown>[] = [setTemporary.of(clamped)];
|
|
if (!view.state.field(temporaryField, false))
|
|
effects.push(StateEffect.appendConfig.of(temporaryHighlightExtension()));
|
|
const first = clamped[0];
|
|
if (first) effects.push(EditorView.scrollIntoView(first.from, { y: "nearest" }));
|
|
view.dispatch({ effects });
|
|
}
|
|
|
|
export function clearTemporaryHighlight(view: EditorView): void {
|
|
const current = view.state.field(temporaryField, false);
|
|
if (current?.length) view.dispatch({ effects: setTemporary.of([]) });
|
|
}
|
|
|
|
export function temporaryHighlights(view: EditorView): readonly HighlightRange[] {
|
|
return view.state.field(temporaryField, false) ?? [];
|
|
}
|
|
|
|
export function temporaryHighlightExtension(): Extension {
|
|
return [temporaryField];
|
|
}
|