BitMagic The Complete Development Environment for the Commander X16.

Using the X16 Reference from Other Extensions

This page is for extension authors. The X16 Reference has an API that other extensions can use for hovers and look ups, and a few ways for them to add to it.

None of it needs your extension to depend on this one. If the X16 Reference isn’t installed, you carry on without it.

How to get the API

const ext = vscode.extensions.getExtension<X16DocumentationApi>("yazwh0.bitmagic-x16documentation");
const docs = ext ? await ext.activate() : undefined;

getExtension returns undefined if it isn’t installed. activate starts it if it isn’t running and returns the API; call it once and keep the result. vscode.extensions.onDidChange fires if it’s installed later.

How to add docs to a hover

const [entry] = await docs?.lookup({ name: "CHROUT", kind: "kernal" }) ?? [];
if (entry) {
    const md = new vscode.MarkdownString(docs!.hoverMarkdown(entry));
    md.isTrusted = { enabledCommands: ["x16docs.show"] };   // for its "Open in the X16 Reference" link
    return new vscode.Hover(md);
}

Pass kind when you know what the word is, so a label called LOAD doesn’t turn up the BASIC command. When your language’s names don’t match the guide’s (Prog8’s cx16.VERA_ADDR_L is the guide’s ADDRx_L), work out the address and look that up instead: lookup({ address: 0x9F20 }).

The API never downloads the guide by itself. Until the user has opened the X16 Reference once, lookup and the rest return nothing, so a hover can’t start a download. ready() says whether there are docs, and onDidChange fires when they arrive.

The API

Name Type Optional Description
version 1 No Goes up if the API changes in a way you’d need to check for.
onDidChange event No Fires when the docs are loaded or updated, or doc sets are added or removed.
ready(download?) Promise<boolean> No Loads the docs if they aren’t yet, and says whether there are any. With download, downloads them if need be.
lookup(query) Promise<DocEntry[]> No Entries matching { name, kind } exactly (names and their aliases), or { address }.
addressName(address) Promise<DocEntry> No The best name for an address: the routine or register at it, else the smallest memory range holding it.
search(text, limit?) Promise<...[]> No Free text search over the entries and every section.
hoverMarkdown(entry, options?) string No Hover-ready Markdown for an entry, ending with a link that opens it in the reader. { brief: true } leaves out the details.
showLink(target) string No A command: link that opens a target, for your own Markdown.
show(target, options?) Promise<boolean> No Opens the reader at a target, beside the editor with { beside: true }.
registerLookupProvider(languageId, provider) Disposable Yes Tells look up what’s under the cursor in your language. From version 0.1.3; see below.

A target is a place in the guide, or something to find in it: { name: "CHROUT" }, { address: 0xFFD2 } or { query: "sprites" }.

The kinds of entry are kernal, math, basic, vera, io, memory, opcode, and other for docs other extensions add. The full types, to copy into your extension, are in the extension’s README.

Without the API, the x16docs.show command takes the same targets: vscode.commands.executeCommand("x16docs.show", { name: "CHROUT" }).

How to add look up for your language

To have Look Up in the X16 Reference on the right-click menu in your language, declare it in your package.json:

"contributes": {
    "bitMagicX16Documentation.languages": ["prog8"]
}

It’s read from your package.json, so neither extension has to be running for it.

How to tell look up what’s under the cursor

Ctrl+F1 guesses the word from the text, but your extension knows your language better: BASIC’s LOAD isn’t the kernal’s LOAD. Register a provider and look up asks it first:

docs.registerLookupProvider?.("x16basic", (document, position) => {
    const keyword = keywordAt(document, position);   // your own parsing
    return keyword ? { name: keyword, kind: "basic" } : undefined;
});

Return any target; { address: 0x9F20 } suits names that don’t match the guide’s. Return undefined where there’s nothing, and look up falls back to its guess. The ?. is because it arrived in version 0.1.3, so your extension still works with older ones.

How to add your own docs

Your extension can add a book of its own, in Markdown. Its pages join the contents, the reader and the search:

"contributes": {
    "bitMagicX16Documentation.docs": [
        { "path": "docs/library", "title": "Prog8 Library", "entries": "docs/library/entries.json" }
    ]
}
Name Type Optional Description
path string No A folder of Markdown files, subfolders too. index.md or README.md is the first page, and the order it links to the others is the order of the chapters.
title string No The book’s name in the contents.
index string Yes The first page, if it isn’t index.md or README.md.
entries string Yes A JSON file of reference entries for look up and search.
id string Yes The book’s id. Your extension’s id by default.

Each entry in the entries file is an object:

[
    { "name": "txt.print", "kind": "other", "summary": "Print a string", "signature": "txt.print(str text)", "file": "textio.md", "anchor": "print" }
]

file is the page it’s on, relative to path, and anchor is the heading’s anchor, made the way GitHub makes them. address can be a number or a string like "$FFD2".

How to share the BitMagic X16 button

The BitMagic X16 button in the activity bar is shared by the BitMagic extensions, not owned by this one. An extension with views declares the same container and puts its views in it:

"contributes": {
    "viewsContainers": {
        "activitybar": [{ "id": "bitmagicX16", "title": "BitMagic X16", "icon": "media/icon.svg" }]
    },
    "views": {
        "bitmagicX16": [{ "id": "myext.things", "name": "Things" }]
    }
}

VSCode registers a container once and reuses it when another extension declares the same id. So the button is there with any of these extensions installed, and they share it when there are several. Whichever loads first supplies the title and icon, so keep them identical: BitMagic X16, and a copy of media/icon.svg from the X16 Reference’s repository.