дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 10:07:11 +02:00
fix(editor): match WebStorm's JS/TS inlay hint defaults and cut long hints (#7338)
JS/TS inlay hints default as in WebStorm: inferred types for variables and fields, enum values, and parameter names for literal and anonymous-function arguments. The Editor toggle now reaches the typescript-language-server fallback too. Long hints are cut with the full label in the tooltip, which also refreshes when the server changes it. Closes #7326 Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
1 джерело
f853ccf544
коміт
2f799aede9
21 files changed
+513
-53
No files matched your search
@@ -10788,6 +10788,8 @@ export type InlayHint = {
|
||||
/** Editor › Inlay Hints. */
|
||||
export type InlayHintsSettings = {
|
||||
types?: boolean,
|
||||
/** Inferred variable and field types from the JavaScript and TypeScript servers (on, as in WebStorm). */
|
||||
jsTsTypes?: boolean,
|
||||
parameters?: boolean,
|
||||
/** Hints of other kinds (chaining, closing labels…). */
|
||||
other?: boolean,
|
||||
|
||||
@@ -19,7 +19,7 @@ vi.mock("@/platform/ipc", async (orig) => {
|
||||
return { ...real, isTauri: () => true, commands: { ...real.commands, ...ipc } };
|
||||
});
|
||||
|
||||
const { inlayDecorations, inlayExtension, inlayMenuItems } = await import("./inlay");
|
||||
const { inlayDecorations, inlayExtension, inlayMenuItems, truncateParts, MAX_LABEL_LENGTH } = await import("./inlay");
|
||||
const { lspDoc } = await import("./lsp");
|
||||
const { usePlatform } = await import("@/platform/platformInfo");
|
||||
const { resetNavigationModifier } = await import("@/platform/navigationModifier");
|
||||
@@ -86,6 +86,29 @@ describe("background inlay hints", () => {
|
||||
expect(shown(view)).toBe(1);
|
||||
});
|
||||
|
||||
it("updates hint and part tooltips while keeping the full truncated label", async () => {
|
||||
const label = ": " + "InferredType".repeat(5);
|
||||
const withTooltips = (tooltip: string, partTooltip: string) => ({
|
||||
...hint,
|
||||
label,
|
||||
tooltip,
|
||||
parts: [{ value: label, tooltip: partTooltip, location: null }],
|
||||
});
|
||||
ipc.lspInlayHints.mockResolvedValue(ok([withTooltips("Old hint", "Old part")]));
|
||||
const view = editor();
|
||||
await afterPass();
|
||||
const widget = () => view.dom.querySelector<HTMLElement>(".cm-lsp-inlay")!;
|
||||
expect(widget().title).toBe(`${label}\n\nOld hint`);
|
||||
expect(widget().querySelector("span")!.title).toBe(`${label}\n\nOld part`);
|
||||
|
||||
ipc.lspInlayHints.mockResolvedValue(ok([withTooltips("New hint", "New part")]));
|
||||
view.dispatch({ changes: { from: view.state.doc.length, insert: " " } });
|
||||
await afterPass();
|
||||
expect(widget().textContent).toHaveLength(MAX_LABEL_LENGTH);
|
||||
expect(widget().title).toBe(`${label}\n\nNew hint`);
|
||||
expect(widget().querySelector("span")!.title).toBe(`${label}\n\nNew part`);
|
||||
});
|
||||
|
||||
it("keeps the shown hints while the server is busy answering empty, and clears once it settles", async () => {
|
||||
ipc.lspInlayHints.mockResolvedValue(ok([hint]));
|
||||
const view = editor();
|
||||
@@ -185,6 +208,45 @@ describe("inlay hints", () => {
|
||||
expect(openFile).toHaveBeenCalledWith("/p/foo.rs", { selection: { line: 4, col: 7 }, focus: true });
|
||||
});
|
||||
|
||||
it("cuts a long label to the maximum length, with the full text on hover", () => {
|
||||
// vtsls' type of an optional-chained object: `{ id: number; title: string; … }`.
|
||||
const long = `: { ${Array.from({ length: 12 }, (_, i) => `field${i}: string;`).join(" ")} }`;
|
||||
const view = editorWith([{ ...hint, label: long, parts: [] }]);
|
||||
const el = view.dom.querySelector<HTMLElement>(".cm-lsp-inlay")!;
|
||||
expect(el.textContent).toHaveLength(MAX_LABEL_LENGTH);
|
||||
expect(el.textContent).toBe(`${long.slice(0, MAX_LABEL_LENGTH - 1)}…`);
|
||||
expect(el.title).toBe(long);
|
||||
// The server's tooltip stays, below the full label.
|
||||
const withTooltip = editorWith([{ ...hint, label: long, parts: [], tooltip: "Inferred type" }]);
|
||||
expect(withTooltip.dom.querySelector<HTMLElement>(".cm-lsp-inlay")!.title).toBe(`${long}\n\nInferred type`);
|
||||
// A short label is shown whole, with no hover text of its own.
|
||||
const short = editorWith([{ ...hint, label: ": number", parts: [] }]).dom.querySelector<HTMLElement>(
|
||||
".cm-lsp-inlay",
|
||||
)!;
|
||||
expect(short.textContent).toBe(": number");
|
||||
expect(short.title).toBe("");
|
||||
});
|
||||
|
||||
it("cuts long label parts, keeping the navigable parts before the cut", () => {
|
||||
const tail = "x".repeat(80);
|
||||
const parts = [...hint.parts, { value: tail, tooltip: null, location: null }];
|
||||
const view = editorWith([{ ...hint, label: parts.map((p) => p.value).join(""), parts }]);
|
||||
const el = view.dom.querySelector<HTMLElement>(".cm-lsp-inlay")!;
|
||||
expect(el.textContent).toHaveLength(MAX_LABEL_LENGTH);
|
||||
expect(el.querySelector(".cm-lsp-inlay-part")!.textContent).toBe("Foo");
|
||||
expect(el.title).toBe(`: Foo${tail}`);
|
||||
// The navigable part keeps its tooltip, below the full label.
|
||||
expect(el.querySelector<HTMLElement>(".cm-lsp-inlay-part")!.title).toBe(`: Foo${tail}\n\nstruct Foo`);
|
||||
});
|
||||
|
||||
it("truncates across parts at exactly the maximum length", () => {
|
||||
expect(truncateParts(["abc", "def"], 6)).toEqual(["abc", "def"]);
|
||||
expect(truncateParts(["abc", "def"], 5)).toEqual(["abc", "d…"]);
|
||||
expect(truncateParts(["abcd", "ef"], 5)).toEqual(["abcd…"]);
|
||||
expect(truncateParts(["abcdef"], 3)).toEqual(["ab…"]);
|
||||
expect(truncateParts([], 3)).toEqual([]);
|
||||
});
|
||||
|
||||
it("offers to disable hints of the clicked kind", () => {
|
||||
const view = editorWith([hint]);
|
||||
view.dom
|
||||
|
||||
@@ -60,6 +60,25 @@ export function inlayMenuItems(kind: InlayHint["kind"]): MenuItem[] {
|
||||
];
|
||||
}
|
||||
|
||||
/** The longest label shown in full; longer ones end in "…" with the full text on hover, as in IDEA. */
|
||||
export const MAX_LABEL_LENGTH = 50;
|
||||
|
||||
/** `values` cut to `max` characters in all, the last one kept ending in "…"; parts past it dropped. */
|
||||
export function truncateParts(values: readonly string[], max = MAX_LABEL_LENGTH): string[] {
|
||||
if (values.reduce((n, v) => n + v.length, 0) <= max) return [...values];
|
||||
const out: string[] = [];
|
||||
let left = max - 1;
|
||||
for (const value of values) {
|
||||
if (value.length >= left) {
|
||||
out.push(`${value.slice(0, left)}…`);
|
||||
break;
|
||||
}
|
||||
out.push(value);
|
||||
left -= value.length;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
class InlayWidget extends WidgetType {
|
||||
constructor(readonly hint: InlayHint) {
|
||||
super();
|
||||
@@ -70,10 +89,16 @@ class InlayWidget extends WidgetType {
|
||||
return (
|
||||
a.label === b.label &&
|
||||
a.kind === b.kind &&
|
||||
a.tooltip === b.tooltip &&
|
||||
a.paddingLeft === b.paddingLeft &&
|
||||
a.paddingRight === b.paddingRight &&
|
||||
(a.parts?.length ?? 0) === (b.parts?.length ?? 0) &&
|
||||
(a.parts ?? []).every((p, i) => p.location?.path === b.parts[i]?.location?.path && p.value === b.parts[i]?.value)
|
||||
(a.parts ?? []).every(
|
||||
(p, i) =>
|
||||
p.location?.path === b.parts[i]?.location?.path &&
|
||||
p.value === b.parts[i]?.value &&
|
||||
p.tooltip === b.parts[i]?.tooltip,
|
||||
)
|
||||
);
|
||||
}
|
||||
toDOM(view: EditorView) {
|
||||
@@ -82,11 +107,15 @@ class InlayWidget extends WidgetType {
|
||||
if (this.hint.paddingLeft) el.classList.add("cm-lsp-inlay-pl");
|
||||
if (this.hint.paddingRight) el.classList.add("cm-lsp-inlay-pr");
|
||||
const parts = this.hint.parts ?? [];
|
||||
const cut = this.hint.label.length > MAX_LABEL_LENGTH;
|
||||
if (parts.some((p) => p.location || p.tooltip)) {
|
||||
for (const part of parts) {
|
||||
const shown = truncateParts(parts.map((p) => p.value));
|
||||
for (const [i, value] of shown.entries()) {
|
||||
const part = parts[i]!;
|
||||
const span = document.createElement("span");
|
||||
span.textContent = part.value;
|
||||
if (part.tooltip) span.title = part.tooltip;
|
||||
span.textContent = value;
|
||||
// A part's own tooltip hides the hint's: put the full label above it when the label is cut.
|
||||
if (part.tooltip) span.title = cut ? `${this.hint.label}\n\n${part.tooltip}` : part.tooltip;
|
||||
if (part.location) {
|
||||
span.className = "cm-lsp-inlay-part";
|
||||
const loc = part.location;
|
||||
@@ -100,7 +129,7 @@ class InlayWidget extends WidgetType {
|
||||
}
|
||||
el.appendChild(span);
|
||||
}
|
||||
} else el.textContent = this.hint.label;
|
||||
} else el.textContent = truncateParts([this.hint.label])[0] ?? "";
|
||||
// A plain type hint (basedpyright's `: Series`) names no location: ⌘-click opens the preceding identifier's type.
|
||||
if (
|
||||
this.hint.kind === "type" &&
|
||||
@@ -117,7 +146,9 @@ class InlayWidget extends WidgetType {
|
||||
void gotoTargets(view, "typeDefinition", offset);
|
||||
});
|
||||
}
|
||||
if (this.hint.tooltip) el.title = this.hint.tooltip;
|
||||
// A cut label shows in full on hover, above the server's own tooltip.
|
||||
const title = [cut ? this.hint.label : "", this.hint.tooltip ?? ""].filter(Boolean).join("\n\n");
|
||||
if (title) el.title = title;
|
||||
el.setAttribute("aria-hidden", "true");
|
||||
el.addEventListener("contextmenu", (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
@@ -15,6 +15,13 @@ const EXAMPLES = [
|
||||
file: "total.rs",
|
||||
parts: ["fn checkout() {\n let total", { annotation: ": f64" }, ' = 42.0 * 1.05;\n println!("{total}");\n}'],
|
||||
},
|
||||
{
|
||||
id: "jsTsTypes",
|
||||
label: "JavaScript and TypeScript types",
|
||||
language: "typescript",
|
||||
file: "total.ts",
|
||||
parts: ["const total", { annotation: ": number" }, " = 42 * 1.05;\nconsole.log(total);"],
|
||||
},
|
||||
{
|
||||
id: "parameters",
|
||||
label: "Parameter names",
|
||||
@@ -56,12 +63,15 @@ export function InlayHintsPanel() {
|
||||
const example = EXAMPLES[index]!;
|
||||
const [enabled] = useSetting<boolean>("editor.inlayHints");
|
||||
const [shown, setShown] = useSetting<boolean>(`editor.inlay.${example.id}`);
|
||||
const [types] = useSetting<boolean>("editor.inlay.types");
|
||||
const [disabledLanguages] = useSetting<string[]>("editor.inlay.disabledLanguages");
|
||||
const [family] = useSetting<string>("editor.fontFamily");
|
||||
const [fallback] = useSetting<string>("editor.fallbackFont");
|
||||
const [size] = useSetting<number>("editor.fontSize");
|
||||
const [lineHeight] = useSetting<number>("editor.lineHeight");
|
||||
const showHint = enabled && shown && !disabledLanguages.includes(example.language);
|
||||
// JavaScript and TypeScript types are type hints: the Types switch has to be on too.
|
||||
const showHint =
|
||||
enabled && shown && (example.id !== "jsTsTypes" || types) && !disabledLanguages.includes(example.language);
|
||||
|
||||
return (
|
||||
<div className={styles.split}>
|
||||
|
||||
@@ -248,6 +248,7 @@ export const EDITOR_EXTRA_DEFAULTS = {
|
||||
},
|
||||
inlay: {
|
||||
types: true,
|
||||
jsTsTypes: true,
|
||||
parameters: true,
|
||||
other: true,
|
||||
closingLabels: true,
|
||||
|
||||
@@ -4303,6 +4303,7 @@
|
||||
"Copy Image": "Copy Image",
|
||||
"Couldn't copy the image": "Couldn't copy the image",
|
||||
"Closing labels": "Closing labels",
|
||||
"JavaScript and TypeScript types": "JavaScript and TypeScript types",
|
||||
"Roll Back to Here": "Roll Back to Here",
|
||||
"Couldn't roll back": "Couldn't roll back",
|
||||
"This message and the ones after it are removed, and the files the agent changed since go back to how they were before it was sent. The message returns to the composer.": "This message and the ones after it are removed, and the files the agent changed since go back to how they were before it was sent. The message returns to the composer.",
|
||||
|
||||
@@ -10883,6 +10883,7 @@
|
||||
"Copy Image": "复制图片",
|
||||
"Couldn't copy the image": "无法复制图片",
|
||||
"Closing labels": "结束标签",
|
||||
"JavaScript and TypeScript types": "JavaScript 和 TypeScript 类型",
|
||||
"Roll Back to Here": "回滚到这里",
|
||||
"Couldn't roll back": "无法回滚",
|
||||
"This message and the ones after it are removed, and the files the agent changed since go back to how they were before it was sent. The message returns to the composer.": "这条消息及之后的消息会被移除,Agent 在此之后改动的文件会恢复到这条消息发送前的状态。消息会放回输入框。",
|
||||
|
||||
Reference in new issue
Заблокувати користувача