Files
openidea/e2e/inlayRequests.fixture.ts
T
LU XIANGRUIandwildcatDownstairs 9d94b00911 feat(python): add type annotations from the inferred types (#5357)
basedpyright shows inferred types as inlay hints, and each hint carries the `textEdits` that insert it, including the import it needs (`from typing import Any`). The IDE dropped those edits, so an inferred type could not become an annotation. Inlay hints now keep `textEdits` over IPC, and Alt+Enter in Python offers PyCharm's:
- **Add type annotation ': int'** on an unannotated `name = value` assignment in a function or at module level (not in class bodies, where it would turn a dataclass/NamedTuple attribute into a field, nor for `global`/`nonlocal` names);
- **Add return type annotation '-> str'** on a function header;
- **Add type hints for function 'f'** on a function header: the return type plus the types of defaulted parameters (`limit: int = 10`), taken from hover and kept only when they name builtins, which need no import.
Annotations Python evaluates (signatures, module variables) use `X | Y` or `list[X]` only when `requires-python` allows them (3.10 / 3.9) or the file has `from __future__ import annotations`; a local variable's annotation is never evaluated. The intention asks the server for the hints itself, so it works with type hints turned off in Settings; a variable the server shows no hint for falls back to its hover type under the same builtin rule.

Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
2026-11-24 00:40:35 +08:00

49 lines
1.7 KiB
TypeScript

import { EditorView } from "@codemirror/view";
import { StateEffect } from "@codemirror/state";
import type { InlayHint, Range } from "../src/bindings";
import { commands } from "../src/platform/ipc";
import { setInvokeRoute } from "../src/platform/tauriCore";
import { lspDoc } from "../src/editor/lsp/lsp";
import { inlayExtension } from "../src/editor/lsp/inlay";
import { lspTheme } from "../src/editor/lsp/theme";
import "../src/styles/tokens.css";
const requests: { range: Range; resolve: (hints: InlayHint[]) => void }[] = [];
setInvokeRoute(() => null);
commands.lspInlayHints = async (_doc, range) =>
new Promise((resolve) => requests.push({ range, resolve: (data) => resolve({ status: "ok", data }) }));
const view = new EditorView({
parent: document.body,
doc: Array.from({ length: 2000 }, (_, i) => `let item${i} = value;`).join("\n"),
extensions: [
EditorView.theme({ "&": { height: "400px" }, ".cm-scroller": { overflow: "auto" } }),
lspTheme,
lspDoc.of({ doc: 1, path: "demo.rs", language: "rust", uiMode: "Rust", readOnly: false }),
inlayExtension(),
],
});
Object.assign(window, {
inlayTest: {
count: () => requests.length,
resolve(index: number, label = `hint-${index}`) {
const request = requests[index]!;
request.resolve([
{
position: { line: request.range.start.line + 1, col: 4 },
label,
kind: "type",
paddingLeft: false,
paddingRight: false,
tooltip: null,
parts: [],
textEdits: [],
},
]);
},
wrap: () => view.dispatch({ effects: StateEffect.appendConfig.of(EditorView.lineWrapping) }),
destroy: () => view.destroy(),
},
});