дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
feat(python): show package documentation in the Python Packages tool window (#6600)
When the pane has a package's PyPI details (a search result, or an installed package after Show PyPI Details), it also shows the project's links (its PyPI page, home_page and project_urls; http(s) only, duplicates dropped) and its long description: rendered Markdown (sanitized, as in hover docs) when description_content_type is Markdown, plain text otherwise. Links open in the browser. Everything comes from the JSON API response the pane already loads. Closes #6530 Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
1 джерело
0a8fea1876
коміт
3747a8a63f
11 files changed
+263
-2
No files matched your search
@@ -11,8 +11,10 @@ const mocks = vi.hoisted(() => ({
|
||||
container: vi.fn(),
|
||||
index: vi.fn(),
|
||||
cancel: vi.fn(),
|
||||
openUrl: vi.fn(),
|
||||
active: null as string | null,
|
||||
}));
|
||||
vi.mock("@/platform/openUrl", () => ({ openExternalUrl: mocks.openUrl }));
|
||||
vi.mock("@tauri-apps/api/core", () => ({ Channel: class {} }));
|
||||
vi.mock("@/platform/ipc", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("@/platform/ipc")>();
|
||||
@@ -50,6 +52,9 @@ const SIX = {
|
||||
summary: "Python 2 and 3 compatibility utilities",
|
||||
latest: "1.17.0",
|
||||
versions: ["1.17.0", "1.16.0"],
|
||||
description: "# Six\n\nRead the [documentation](https://six.readthedocs.io/).<script>alert(1)</script>",
|
||||
markdown: true,
|
||||
links: [{ label: "Source", url: "https://github.com/benjaminp/six" }],
|
||||
};
|
||||
|
||||
function open(interpreter = "") {
|
||||
@@ -112,6 +117,30 @@ describe("Python Packages", () => {
|
||||
await waitFor(() => expect(mocks.packages.mock.calls.filter((c) => c[1] === false)).toHaveLength(2));
|
||||
});
|
||||
|
||||
it("shows a PyPI project's links and its Markdown description, opening links in the browser", async () => {
|
||||
open();
|
||||
await screen.findByText("urllib3");
|
||||
search("six");
|
||||
select(await screen.findByText("six"));
|
||||
expect(await screen.findByRole("heading", { name: "Six" })).toBeInTheDocument();
|
||||
expect(document.querySelector("script")).toBeNull();
|
||||
const links = screen.getByRole("navigation", { name: "Project links" });
|
||||
expect([...links.querySelectorAll("a")].map((a) => a.textContent)).toEqual(["PyPI", "Source"]);
|
||||
fireEvent.click(screen.getByRole("link", { name: "PyPI" }));
|
||||
fireEvent.click(screen.getByRole("link", { name: "documentation" }));
|
||||
expect(mocks.openUrl.mock.calls).toEqual([["https://pypi.org/project/six/"], ["https://six.readthedocs.io/"]]);
|
||||
});
|
||||
|
||||
it("shows a description that is not Markdown as text", async () => {
|
||||
open();
|
||||
await screen.findByText("urllib3");
|
||||
mocks.pypi.mockResolvedValue(ok({ ...SIX, name: "docutils", description: "Docutils\n========", markdown: false }));
|
||||
search("docutils");
|
||||
select(await screen.findByText("docutils"));
|
||||
expect((await screen.findByLabelText("Package description")).textContent).toBe("Docutils\n========");
|
||||
expect(screen.queryByRole("heading")).toBeNull();
|
||||
});
|
||||
|
||||
it("sends an installed package's name to PyPI only when asked, once per session", async () => {
|
||||
open();
|
||||
select(await screen.findByText("urllib3"));
|
||||
@@ -120,6 +149,7 @@ describe("Python Packages", () => {
|
||||
mocks.pypi.mockResolvedValue(ok({ ...SIX, name: "urllib3", summary: "HTTP library" }));
|
||||
fireEvent.click(screen.getByRole("button", { name: "Show PyPI Details" }));
|
||||
expect(await screen.findByText("HTTP library")).toBeInTheDocument();
|
||||
expect(screen.getByRole("link", { name: "PyPI" })).toHaveAttribute("href", "https://pypi.org/project/urllib3/");
|
||||
select(screen.getByText("requests"));
|
||||
select(screen.getByText("urllib3"));
|
||||
expect(screen.getByText("HTTP library")).toBeInTheDocument();
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
* looked up by exact name, and install / upgrade / uninstall through pip or uv.
|
||||
*/
|
||||
import { Channel } from "@tauri-apps/api/core";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useEffect, useMemo, useState, type MouseEvent } from "react";
|
||||
import { useMarkupHtml } from "@/editor/lsp/markdown";
|
||||
import { executeAction } from "@/platform/actions";
|
||||
import { useTx } from "@/platform/i18n";
|
||||
import { call, commands, isCancelled, nextRequestId, type PypiPackage, type PythonPackage } from "@/platform/ipc";
|
||||
import { openExternalUrl } from "@/platform/openUrl";
|
||||
import { dirname, useProject } from "@/platform/project";
|
||||
import { usePythonPackages } from "@/platform/pythonPackages";
|
||||
import {
|
||||
@@ -576,6 +578,47 @@ function Details({
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{asked && info.found && <Docs found={info.found} />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Links open in the browser; a relative one has nothing to open and must not navigate the app. */
|
||||
function openLink(e: MouseEvent) {
|
||||
const a = (e.target as HTMLElement).closest("a");
|
||||
if (!a) return;
|
||||
e.preventDefault();
|
||||
void openExternalUrl(a.getAttribute("href") ?? "");
|
||||
}
|
||||
|
||||
/** The project's links and its description from PyPI: rendered when Markdown, else as text. */
|
||||
function Docs({ found }: { found: PypiPackage }) {
|
||||
const tx = useTx();
|
||||
const html = useMarkupHtml({ kind: "markdown", value: found.markdown ? found.description : "" });
|
||||
const links = [{ label: "PyPI", url: `https://pypi.org/project/${found.name}/` }, ...found.links];
|
||||
return (
|
||||
<>
|
||||
<nav className={styles.links} aria-label={tx("Project links")} onClick={openLink}>
|
||||
{links.map((l) => (
|
||||
<a key={l.url} href={l.url} title={l.url}>
|
||||
{l.label}
|
||||
</a>
|
||||
))}
|
||||
</nav>
|
||||
{found.description.trim() &&
|
||||
(found.markdown ? (
|
||||
<div
|
||||
className={styles.doc}
|
||||
role="document"
|
||||
aria-label={tx("Package description")}
|
||||
onClick={openLink}
|
||||
dangerouslySetInnerHTML={{ __html: html }}
|
||||
/>
|
||||
) : (
|
||||
<pre className={styles.plainDoc} aria-label={tx("Package description")}>
|
||||
{found.description}
|
||||
</pre>
|
||||
))}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -95,6 +95,101 @@
|
||||
min-width: 140px;
|
||||
}
|
||||
|
||||
.links {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: var(--space-1) var(--space-3);
|
||||
}
|
||||
.links a,
|
||||
.doc :global(a) {
|
||||
color: var(--link);
|
||||
text-decoration: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
.links a:hover,
|
||||
.doc :global(a:hover) {
|
||||
color: var(--link-hover);
|
||||
text-decoration: underline;
|
||||
}
|
||||
/* The project description, styled like the Markdown preview. */
|
||||
.doc,
|
||||
.plainDoc {
|
||||
padding-top: var(--space-2);
|
||||
border-top: 1px solid var(--border);
|
||||
line-height: 1.55;
|
||||
overflow-wrap: anywhere;
|
||||
user-select: text;
|
||||
-webkit-user-select: text;
|
||||
}
|
||||
.plainDoc {
|
||||
margin: 0;
|
||||
font-family: var(--font-mono);
|
||||
font-size: var(--fs-small);
|
||||
white-space: pre-wrap;
|
||||
color: var(--fg-secondary);
|
||||
}
|
||||
.doc :global(:is(h1, h2, h3, h4, h5, h6)) {
|
||||
margin: 1em 0 0.5em;
|
||||
color: var(--fg-strong);
|
||||
line-height: 1.25;
|
||||
}
|
||||
.doc :global(h1) {
|
||||
font-size: 1.5em;
|
||||
}
|
||||
.doc :global(h2) {
|
||||
font-size: 1.3em;
|
||||
}
|
||||
.doc :global(h3) {
|
||||
font-size: 1.15em;
|
||||
}
|
||||
.doc :global(:is(h1, h2)) {
|
||||
padding-bottom: 0.25em;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
.doc > :first-child {
|
||||
margin-top: 0;
|
||||
}
|
||||
.doc :global(:is(p, ul, ol, blockquote, pre, table)) {
|
||||
margin: 0 0 0.9em;
|
||||
}
|
||||
.doc :global(code) {
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.9em;
|
||||
padding: 0.1em 0.35em;
|
||||
border-radius: var(--radius-small);
|
||||
background: var(--bg-elev);
|
||||
}
|
||||
.doc :global(pre) {
|
||||
padding: var(--space-2);
|
||||
border-radius: var(--radius-control);
|
||||
background: var(--bg-elev);
|
||||
overflow: auto;
|
||||
}
|
||||
.doc :global(pre code) {
|
||||
padding: 0;
|
||||
background: none;
|
||||
}
|
||||
.doc :global(blockquote) {
|
||||
padding: 0 1em;
|
||||
border-left: 3px solid var(--border-strong);
|
||||
color: var(--fg-secondary);
|
||||
}
|
||||
.doc :global(table) {
|
||||
border-collapse: collapse;
|
||||
}
|
||||
.doc :global(:is(th, td)) {
|
||||
padding: var(--space-1) var(--space-2);
|
||||
border: 1px solid var(--border-strong);
|
||||
}
|
||||
.doc :global(img) {
|
||||
max-width: 100%;
|
||||
}
|
||||
.doc :global(hr) {
|
||||
border: none;
|
||||
border-top: 1px solid var(--border);
|
||||
margin: 1.2em 0;
|
||||
}
|
||||
|
||||
.note {
|
||||
color: var(--fg-subtle);
|
||||
font-size: var(--fs-small);
|
||||
|
||||
@@ -5,7 +5,15 @@ const INSTALLED = [
|
||||
{ name: "requests", version: "2.31.0" },
|
||||
{ name: "typing_extensions", version: "4.12.0" },
|
||||
];
|
||||
const found = (name: string) => ({ name, summary: "", latest: "1.0", versions: ["1.0"] });
|
||||
const found = (name: string) => ({
|
||||
name,
|
||||
summary: "",
|
||||
latest: "1.0",
|
||||
versions: ["1.0"],
|
||||
description: "",
|
||||
markdown: false,
|
||||
links: [],
|
||||
});
|
||||
|
||||
describe("package rows", () => {
|
||||
it("lists the installed packages with their newer release", () => {
|
||||
|
||||
Reference in new issue
Заблокувати користувача