Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/documentation/api/ApiNode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import NullLoggingProvider from "../../logging/NullLoggingProvider";
import { ActiveEditorProvider } from "../../editor/active-editor-hooks";
import FixedTranslationProvider from "../../messages/FixedTranslationProvider";
import ScrollablePanel from "../../common/ScrollablePanel";
import ApiNode, { getDragContext, classToInstanceMap } from "./ApiNode";
import ApiNode, { getDragContext } from "./ApiNode";

describe("ApiNode", () => {
const node: ApiDocsEntry = {
Expand Down Expand Up @@ -87,8 +87,13 @@ describe("getDragContext", () => {
it("creates the correct dragContext with a micro:bit class", () => {
const context = getDragContext("microbit.MicroBitTouchPin", "class");
expect(context.type).toEqual("example");
const classInstance = classToInstanceMap["MicroBitTouchPin"];
expect(context.code).toEqual(`from microbit import *\n${classInstance}`);
expect(context.code).toEqual(`from microbit import *\npin0`);
});

it("creates the correct dragContext for os.uname", () => {
const context = getDragContext("os.uname_result.version", "variable");
expect(context.type).toEqual("example");
expect(context.code).toEqual(`import os\nos.uname().version`);
});

it("creates the correct dragContext with __init__", () => {
Expand Down
26 changes: 14 additions & 12 deletions src/documentation/api/ApiNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ const kindToSpacing: Record<string, any> = {
function: 4,
};

export const classToInstanceMap: Record<string, string> = {
Button: "button_a",
MicroBitDigitalPin: "pin0",
MicroBitTouchPin: "pin0",
MicroBitAnalogDigitalPin: "pin0",
Image: "Image.HEART",
};

interface ApiDocEntryNodeProps extends BoxProps {
docs: ApiDocsEntry;
anchor?: Anchor;
Expand Down Expand Up @@ -327,11 +319,21 @@ const BaseClasses = ({ value }: { value: ApiDocsBaseClass[] }) => {
);
};

const classToInstanceMap: Record<string, string> = {
Button: "button_a",
MicroBitDigitalPin: "pin0",
MicroBitTouchPin: "pin0",
MicroBitAnalogDigitalPin: "pin0",
Image: "Image.HEART",
uname_result: "uname()",
};

export const getDragContext = (fullName: string, kind: string): DragContext => {
const parts = fullName
.split(".")
.map((p) => (kind === "variable" ? p : classToInstanceMap[p] ?? p))
.filter((p) => p !== "__init__");
let parts = fullName.split(".").filter((p) => p !== "__init__");
// Heuristic identification of e.g. Image.HEART. Sufficient for MicroPython API.
if (!parts[parts.length - 1].match(/^[A-Z0-9_]+$/)) {
parts = parts.map((p) => classToInstanceMap[p] ?? p);
}
const isMicrobit = parts[0] === "microbit";
const nameAsWeImportIt = isMicrobit ? parts.slice(1) : parts;
const code = nameAsWeImportIt.join(".") + (kind === "function" ? "()" : "");
Expand Down
Loading