Файли
openidea/src/editor/refactorProviders.ts
T
LU XIANGRUIтаwildcatDownstairs 1880ac541c fix(python): fixture usages and rename follow pytest's files and Python's names (#6655)
Follow-up to #6520. Find Usages, Go to Declaration and Rename of a pytest fixture now search where pytest would look: the defining test module, the test files below a conftest.py, or the project's test files for a plugin or pytest_plugins module, with test files taken from python_files in the configuration file pytest reads. A fixture imported into a conftest or test module is that module's own. Rename merges the language server's rename of the fixture function, refuses names that clash with a parameter or body name, an override or another fixture in scope, and invalid identifiers, and reads of a parameter skip nested scopes that rebind it.

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-28 14:58:10 +08:00

60 рядки
2.3 KiB
TypeScript

/**
* Find Usages and Rename for names the language server doesn't connect (pytest fixtures and
* the tests that request them). A provider answers only for positions it owns; the first
* answer wins, and everywhere else the server's answer is used as before.
*/
import type { EditorView } from "@codemirror/view";
import type { Location, Pos, RequestId, WorkspaceEdit } from "@/platform/ipc";
export interface UsageQuery {
doc: number;
path: string;
pos: Pos;
}
/** Usages of the symbol at `query`, or null when the provider doesn't own that position. */
type UsageProvider = (query: UsageQuery, request: RequestId) => Promise<Location[] | null>;
export interface ContributedRename {
/** The name being renamed, in the editor's document. */
from: number;
to: number;
/** Why it can't be renamed (defined outside the project, …); the rename stops with this hint. */
refused?: string;
/** The edit renaming it to `newName`, and a warning to show with it (in the preview). */
edit: (newName: string, request: RequestId) => Promise<{ edit: WorkspaceEdit; note?: string | null }>;
}
type RenameProvider = (view: EditorView, offset: number) => Promise<ContributedRename | null>;
const usageProviders = new Set<UsageProvider>();
const renameProviders = new Set<RenameProvider>();
export function registerUsageProvider(provider: UsageProvider): () => void {
usageProviders.add(provider);
return () => void usageProviders.delete(provider);
}
export function registerRenameProvider(provider: RenameProvider): () => void {
renameProviders.add(provider);
return () => void renameProviders.delete(provider);
}
export async function contributedUsages(query: UsageQuery, request: RequestId): Promise<Location[] | null> {
for (const provider of usageProviders) {
// oxlint-disable-next-line no-await-in-loop -- first owner wins
const found = await provider(query, request).catch(() => null);
if (found) return found;
}
return null;
}
export async function contributedRename(view: EditorView, offset: number): Promise<ContributedRename | null> {
for (const provider of renameProviders) {
// oxlint-disable-next-line no-await-in-loop -- first owner wins
const found = await provider(view, offset).catch(() => null);
if (found) return found;
}
return null;
}