Файли
openidea/src/editor/lsp/serverCodeLens.ts
T
l0ng-ai 2811a438a0 fix(editor): skip project-wide background requests in library sources (#4933)
Go to Declaration in a library file could wait 10-20 s behind usage lenses, server code lenses and inheritor markers, which asked references / implementation for every visible library declaration and made the server analyse the whole project.

Usage lenses and server code lenses now skip library sources (library roots, node_modules, excluded folders, virtual documents, files outside the project). Gutter markers stay: in library files ↓ is asked for classes and interfaces only, and in-flight marker requests are cancelled while a navigation gesture runs and asked again afterwards.
2026-11-21 13:38:13 +08:00

123 рядки
4.9 KiB
TypeScript

/** Language-server and plugin lenses in the shared editor code-vision renderer. */
import { MapMode, StateEffect, StateField, type Extension } from "@codemirror/state";
import { ViewPlugin, type ViewUpdate } from "@codemirror/view";
import {
codeLenses,
isLibrarySource,
shownAgain,
whenSynced,
type CodeLens,
type EditorDocContext,
} from "@/editor/api";
import { call, commands } from "@/platform/ipc";
import { onNavigationProvidersChanged } from "@/editor/navigationProviders";
import { posToOffset } from "./positions";
import { onLspRefresh, onServerSettled, anyServerBusy } from "./availability";
import { editorPrefs } from "@/platform/editorSettings";
import { useProject } from "@/platform/project";
/**
* A server's reference count ("3 references", csharp-ls's "3 Reference(s)"): Code Vision's
* Usages shows the same count above the declaration, under its own setting.
*/
const REFERENCE_COUNT = /^\s*\d+\s+references?(\(s\))?\s*$/i;
const set = StateEffect.define<CodeLens[]>();
const field = StateField.define<CodeLens[]>({
create: () => [],
update(value, tr) {
for (const e of tr.effects) if (e.is(set)) return e.value;
// Map through edits so block widgets don't vanish/reappear and thrash scroll (#2710).
if (!tr.docChanged || !value.length) return value;
const out: CodeLens[] = [];
for (const l of value) {
const pos = tr.changes.mapPos(l.pos, -1, MapMode.TrackDel);
if (pos !== null) out.push({ ...l, pos });
}
return out;
},
provide: (f) => codeLenses.from(f),
});
export function serverCodeLenses(ctx: EditorDocContext): Extension {
if (ctx.doc < 0) return [];
return [
field,
ViewPlugin.define(
(view) =>
new (class {
timer: ReturnType<typeof setTimeout> | undefined;
generation = 0;
skipped = false;
enabled = editorPrefs().inlay.codeVision;
off: () => void;
constructor() {
const offs = [
onNavigationProvidersChanged(() => this.schedule()),
onLspRefresh("codeLens", () => this.schedule()),
onServerSettled(() => this.schedule()),
// Turning Code Vision off hides these rows too, and turning it back on asks again.
useProject.subscribe((s, prev) => {
if (s.settings !== prev.settings && editorPrefs(s.settings).inlay.codeVision !== this.enabled)
this.schedule();
}),
];
this.off = () => offs.forEach((f) => f());
this.schedule();
}
schedule() {
this.generation++;
clearTimeout(this.timer);
this.timer = setTimeout(() => void this.run(), 400);
}
update(u: ViewUpdate) {
if (u.docChanged || (this.skipped && shownAgain(u))) this.schedule();
}
async run() {
const gen = this.generation;
const text = view.state.doc;
this.enabled = editorPrefs().inlay.codeVision;
// Resolving a server's lenses counts references / implementations over the whole project.
if (!this.enabled || isLibrarySource(ctx)) {
if (view.state.field(field).length) view.dispatch({ effects: set.of([]) });
return;
}
try {
await whenSynced(view);
const result = await call(commands.lspCodeLens(ctx.doc));
if (gen !== this.generation || text !== view.state.doc) return;
this.skipped = !view.dom.isConnected;
if (this.skipped) return;
const fresh = result.flatMap((l) =>
l.command && !REFERENCE_COUNT.test(l.command.title)
? [
{
pos: posToOffset(text, l.range.start),
title: l.command.title,
run: () => {
void call(commands.lspExecuteCommand(ctx.doc, l.command!)).catch(console.warn);
},
},
]
: [],
);
// A busy server (indexing, loading) can answer with nothing: keep the
// lenses shown until it settles, instead of blinking the rows away.
// A partial non-empty answer is applied as-is.
if (!fresh.length && view.state.field(field).length && anyServerBusy()) return;
view.dispatch({ effects: set.of(fresh) });
} catch {
// A failed request while a server is starting or busy: keep the lenses until it
// settles. Otherwise the server is gone and so are its lenses.
if (gen === this.generation && view.dom.isConnected && !anyServerBusy())
view.dispatch({ effects: set.of([]) });
}
}
destroy() {
this.generation++;
clearTimeout(this.timer);
this.off();
}
})(),
),
];
}