Файли
openidea/src/editor/lsp/semantic/python/index.ts
T
Bert Verhelstтаbertyhell e1a1becadd feat(python): offer imports of names from installed packages in Alt+Enter (#6676)
basedpyright offers import fixes only for project and standard-library symbols. A library symbol index now reads each public module of the interpreter's site-packages once (its __all__, otherwise public top-level definitions, preferring stubs), rebuilt when a folder changes, and Alt+Enter on an unresolved name lists Import rows from it when the server has no import fix for that name.

Closes #6521

Co-authored-by: bertyhell <[email protected]>
2026-11-29 15:55:16 +08:00

79 рядки
3.0 KiB
TypeScript

/**
* Python's semantic highlighting beyond the LSP tokens (PyCharm's category
* colours): symbol kinds from the parse tree, syntax ranges the server never
* reports, and fallbacks while the server is pending.
*/
import type { LanguageSemantics } from "../languages";
import { registerLanguageSemantics } from "../languages";
import {
pythonFallbackRanges,
pythonSymbolClasses,
pythonSymbolKinds,
pythonSyntaxRanges,
type PythonSymbolKind,
} from "./highlight";
import { implementPythonAbstractMethods, overridePythonMethods, pythonMemberIntentions } from "./members";
import { pythonGenerateEntries } from "./generate";
import { pythonCreateFixes } from "./createFromUsage";
import { pythonTypeHintIntentions } from "./typeHints";
import { pythonStringIntentions } from "./stringFormat";
import { pythonDocstringIntentions } from "./docstring";
import { pythonLibraryImports } from "./libraryImports";
/** Server tokens these kinds override: the kind's own classes replace the generic ones. */
const VARIABLE_KINDS = new Set<PythonSymbolKind>([
"localVariable",
"globalVariable",
"instanceAttribute",
"classAttribute",
"constant",
"property",
"typeParameter",
"unusedParameter",
]);
const SYMBOL_BASES = new Set([
"cm-sem-variable",
"cm-sem-global",
"cm-sem-property",
"cm-sem-constant",
"cm-sem-type",
"cm-sem-typeParameter",
"cm-sem-parameter",
"cm-sem-builtin",
]);
const pythonSemantics: LanguageSemantics = {
symbolKinds: pythonSymbolKinds,
syntaxRanges: (tree, doc, symbols, format) =>
pythonSyntaxRanges(tree, doc, symbols as Map<string, PythonSymbolKind>, format),
fallbackRanges: (tree, doc, rainbow, symbols) =>
pythonFallbackRanges(tree, doc, rainbow, symbols as Map<string, PythonSymbolKind>),
tokenClasses({ type, modifiers, kind, classes }) {
if (modifiers.includes("static")) classes.push("cm-sem-python-static");
if (type === "method") classes.push("cm-sem-python-method-call");
if (modifiers.includes("defaultLibrary")) classes.push("cm-sem-python-builtin");
if (kind && VARIABLE_KINDS.has(kind as PythonSymbolKind)) {
for (let j = classes.length - 1; j >= 0; j--) if (SYMBOL_BASES.has(classes[j]!)) classes.splice(j, 1);
classes.push(...pythonSymbolClasses(kind as PythonSymbolKind).split(" "));
}
},
isLocal: ({ type, kind }) => (kind ? kind === "localVariable" || kind === "parameter" : type === "parameter"),
intentions: (view) => {
const { from, to } = view.state.selection.main;
return [
...pythonCreateFixes(view, from, to),
...pythonMemberIntentions(view),
...pythonStringIntentions(view),
...pythonDocstringIntentions(view),
];
},
quickFixes: pythonCreateFixes,
asyncIntentions: async (view, signal) =>
(await Promise.all([pythonTypeHintIntentions(view, signal), pythonLibraryImports(view, signal)])).flat(),
implementMethods: implementPythonAbstractMethods,
overrideMethods: overridePythonMethods,
generate: pythonGenerateEntries,
};
registerLanguageSemantics("python", pythonSemantics);