Compare commits

...
Author SHA1 Message Date
l0ng-ai 9734edcd0a fix(http): don't rescan Spring routes on every keystroke
Typing in a Java or Kotlin file rescanned the whole project's Spring mappings
once per keystroke. Feign navigation dropped the route index on every docChanged,
although it overlays unsaved text on the disk index anyway, and the editor's
endpoint icons asked every endpoint provider (Spring included) again on each
change, though they only show plugin endpoints.

Typing now keeps the disk index (saves and closes still refresh it), and the
editor icons ask only plugin providers, once typing pauses.

Fixes #4501
2026-11-19 21:07:19 +08:00
5 changed files with 76 additions and 12 deletions

No files matched your search

+32 -2
View File
@@ -1,9 +1,10 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { CompletionContext } from "@codemirror/autocomplete";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { useProject } from "@/platform/project";
import { registerEndpointProvider, searchEndpoints, type EndpointEntry } from "@/workbench/contributions";
import { completionTarget, endpointCompletion, renderEndpoint } from "./endpoints";
import { completionTarget, endpointCompletion, endpointEditorExtension, renderEndpoint } from "./endpoints";
const route: EndpointEntry = {
method: "GET",
@@ -70,4 +71,33 @@ describe("shared HTTP endpoints", () => {
useProject.setState(previous);
}
});
it("asks only plugin providers for editor icons, once typing pauses", async () => {
vi.useFakeTimers();
const previous = useProject.getState();
useProject.setState({ project: { root: "/p" } as NonNullable<typeof previous.project> });
const scan = vi.fn(async () => [route]);
const plugin = vi.fn(async () => [{ ...route, pluginId: "demo", path: "/p/Users.java" }]);
const disposeScan = registerEndpointProvider({ id: "http.spring", endpoints: scan });
const disposePlugin = registerEndpointProvider({ id: "plugin:demo", endpoints: plugin });
const ctx = { doc: 1, path: "/p/Users.java", language: "java", uiMode: null, readOnly: false };
const view = new EditorView({
state: EditorState.create({ doc: "class Users {}", extensions: endpointEditorExtension(ctx) }),
});
try {
await vi.runAllTimersAsync();
plugin.mockClear();
for (const ch of "abc") view.dispatch({ changes: { from: 0, insert: ch } });
expect(plugin).not.toHaveBeenCalled();
await vi.runAllTimersAsync();
expect(plugin).toHaveBeenCalledTimes(1);
expect(scan).not.toHaveBeenCalled();
} finally {
view.destroy();
disposeScan();
disposePlugin();
useProject.setState(previous);
vi.useRealTimers();
}
});
});
+14 -3
View File
@@ -72,6 +72,9 @@ export async function endpointCompletion(context: CompletionContext): Promise<Co
};
}
const REFRESH_DELAY = 300;
const isPluginProvider = (id: string) => id.startsWith("plugin:");
type Mark = { pos: number; endpoints: EndpointEntry[] };
const setMarks = StateEffect.define<Mark[]>();
const marks = StateField.define<Mark[]>({
@@ -112,18 +115,25 @@ export function endpointEditorExtension(ctx: EditorDocContext): Extension {
destroyed = false;
unsubscribe: () => void;
projectUnsubscribe: () => void;
timer: ReturnType<typeof setTimeout> | undefined;
constructor(readonly view: EditorView) {
this.unsubscribe = onEndpointsChanged(() => this.refresh());
this.projectUnsubscribe = useProject.subscribe(() => this.refresh());
this.projectUnsubscribe = useProject.subscribe((state, previous) => {
if (state.project !== previous.project) this.refresh();
});
this.refresh();
}
update(u: { docChanged: boolean }) {
if (u.docChanged) this.refresh();
if (!u.docChanged) return;
clearTimeout(this.timer);
this.timer = setTimeout(() => this.refresh(), REFRESH_DELAY);
}
refresh() {
clearTimeout(this.timer);
const seq = ++this.seq;
const root = useProject.getState().project?.root ?? null;
void searchEndpoints(root).then((entries) => {
// Only plugin endpoints get icons here; built-in providers (Spring) scan the project.
void searchEndpoints(root, isPluginProvider).then((entries) => {
if (this.destroyed || seq !== this.seq) return;
const byLine = new Map<number, EndpointEntry[]>();
for (const e of entries) {
@@ -136,6 +146,7 @@ export function endpointEditorExtension(ctx: EditorDocContext): Extension {
}
destroy() {
this.destroyed = true;
clearTimeout(this.timer);
this.unsubscribe();
this.projectUnsubscribe();
}
@@ -101,6 +101,15 @@ describe("core service-aware Feign navigation", () => {
expect(await resolve()).toEqual([]); // Current source revalidation rejects the cached target.
});
it("keeps the disk index while a controller is edited", async () => {
await resolve();
for (let version = 2; version < 6; version++) {
_emitEditorEvent({ type: "docChanged", doc: 9, path: users, baseVersion: version - 1, version, changes: [] });
await resolve();
}
expect(commands.springRouteSources).toHaveBeenCalledTimes(1);
});
it("overlays recently edited unsaved controllers absent from the disk index", async () => {
await resolve();
const path = "/p/users/src/main/kotlin/Added.kt";
@@ -39,12 +39,16 @@ function inside(project: ProjectInfo, path: string): boolean {
async function read(path: string): Promise<string> {
return editorsFor(path)[0]?.view.state.doc.toString() ?? (await call(commands.fsReadText(path)));
}
function invalidate() {
/** Forgets resolved targets and service names; the disk route index stays. */
function refresh() {
epoch++;
serviceNames.clear();
clearRouteIndex();
invalidateNavigationProviders();
}
function invalidate() {
refresh();
clearRouteIndex();
}
function named(module: string): Promise<string | null> {
let pending = serviceNames.get(module);
@@ -155,9 +159,13 @@ export function registerFeignNavigation(): () => void {
(!isRouteSource(event.path) && !isServiceConfiguration(event.path))
)
return;
if (event.type === "docChanged" && isRouteSource(event.path)) {
if (changedSources.size < 256) changedSources.add(event.path);
} else if (event.type === "docClosed" || event.type === "docSaved") changedSources.delete(event.path);
if (event.type === "docChanged") {
if (isRouteSource(event.path) && changedSources.size < 256) changedSources.add(event.path);
// Unsaved text is overlaid on the disk index: typing must not rescan the project.
refresh();
return;
}
changedSources.delete(event.path);
invalidate();
});
const projects = useProject.subscribe((state, previous) => {
+8 -2
View File
@@ -1883,9 +1883,15 @@ export function registerEndpointProvider(p: EndpointProvider): () => void {
};
}
export async function searchEndpoints(root: string | null): Promise<EndpointEntry[]> {
/** Endpoints of every provider, or of those `provider` accepts (by provider id). */
export async function searchEndpoints(
root: string | null,
provider: (id: string) => boolean = () => true,
): Promise<EndpointEntry[]> {
if (!root) return [];
const results = await Promise.allSettled([...endpointProviders.values()].map(async (p) => p.endpoints(root)));
const results = await Promise.allSettled(
[...endpointProviders.values()].filter((p) => provider(p.id)).map(async (p) => p.endpoints(root)),
);
return results.flatMap((result) => (result.status === "fulfilled" ? result.value : []));
}