Polish VS Code extension: three toolbar actions and CLI checks.

Add Run/View/Debug title icons, logo cover art, version/stdlib install prompts, and LiveServer for view + debug hosts.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-05 17:13:58 +08:00
1 parent 3887205ebb
commit d1b75d88c5
10 files changed
+270 -72

No files matched your search

+16 -7
View File
@@ -129,28 +129,37 @@ export function probeMarqdo(
}
}
export class DebugServer {
export class LiveServer {
private proc: ChildProcessWithoutNullStreams | null = null;
private output: vscode.OutputChannel;
private label: string;
constructor(output: vscode.OutputChannel) {
constructor(output: vscode.OutputChannel, label: string) {
this.output = output;
this.label = label;
}
get running(): boolean {
return this.proc !== null && this.proc.exitCode === null;
}
async start(targetPath: string, host: string, port: number): Promise<string> {
async start(
subcommand: "view" | "debug",
targetPath: string,
host: string,
port: number
): Promise<string> {
if (this.running) {
await this.stop();
}
const cli = resolveMarqdoCli();
const cwd = workspaceCwd(vscode.Uri.file(targetPath));
this.output.appendLine(`$ ${cli} debug ${targetPath} --host ${host} --port ${port} --no-open`);
this.output.appendLine(
`$ ${cli} ${subcommand} ${targetPath} --host ${host} --port ${port} --no-open`
);
this.proc = spawn(
cli,
["debug", targetPath, "--host", host, "--port", String(port), "--no-open"],
[subcommand, targetPath, "--host", host, "--port", String(port), "--no-open"],
{
cwd,
shell: process.platform === "win32",
@@ -161,11 +170,11 @@ export class DebugServer {
this.proc.stdout.on("data", (d: Buffer) => this.output.append(d.toString()));
this.proc.stderr.on("data", (d: Buffer) => this.output.append(d.toString()));
this.proc.on("exit", (code) => {
this.output.appendLine(`[debug] exited (${code})`);
this.output.appendLine(`[${this.label}] exited (${code})`);
this.proc = null;
});
this.proc.on("error", (err) => {
this.output.appendLine(`[debug] ${err.message}`);
this.output.appendLine(`[${this.label}] ${err.message}`);
this.proc = null;
});
await new Promise((r) => setTimeout(r, 600));
+89 -28
View File
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
import { DebugServer, configureCliResolution, runMarqdo, workspaceCwd } from "./cli";
import { LiveServer, configureCliResolution, runMarqdo, workspaceCwd } from "./cli";
import { DiagnosticBag } from "./diagnostics";
import {
checkInstallStatus,
@@ -12,14 +12,16 @@ import {
let output: vscode.OutputChannel;
let diagnostics: DiagnosticBag;
let debugServer: DebugServer;
let viewServer: LiveServer;
let debugServer: LiveServer;
let extContext: vscode.ExtensionContext;
export function activate(context: vscode.ExtensionContext): void {
extContext = context;
output = vscode.window.createOutputChannel("Marqdo");
diagnostics = new DiagnosticBag();
debugServer = new DebugServer(output);
viewServer = new LiveServer(output, "view");
debugServer = new LiveServer(output, "debug");
configureCliResolution(
context,
@@ -31,12 +33,17 @@ export function activate(context: vscode.ExtensionContext): void {
output,
diagnostics.disposable,
vscode.commands.registerCommand("marqdo.run", () => runActiveFile()),
vscode.commands.registerCommand("marqdo.view", () => startView()),
vscode.commands.registerCommand("marqdo.debug", () => startDebug()),
vscode.commands.registerCommand("marqdo.debug.stop", () => stopDebug()),
vscode.commands.registerCommand("marqdo.view.stop", () => stopView()),
vscode.commands.registerCommand("marqdo.catalog", () => runCatalog()),
vscode.commands.registerCommand("marqdo.showOutput", () => output.show(true)),
vscode.commands.registerCommand("marqdo.installCli", () => installCli()),
vscode.commands.registerCommand("marqdo.checkCli", () => checkCli()),
vscode.commands.registerCommand("marqdo.checkCli", () => checkCli())
);
context.subscriptions.push(
vscode.workspace.onDidSaveTextDocument((doc) => {
if (doc.languageId === "marqdo" && autoDiagnose()) {
void diagnoseDocument(doc);
@@ -48,6 +55,7 @@ export function activate(context: vscode.ExtensionContext): void {
}
export function deactivate(): void {
void viewServer?.stop();
void debugServer?.stop();
}
@@ -59,11 +67,8 @@ async function installCli(): Promise<void> {
await resetSkipPrompt(extContext);
const status = checkInstallStatus(extContext);
try {
if (status.cliOk && !status.libOk) {
await installFromGitHub(extContext, output, "stdlib");
} else {
await installFromGitHub(extContext, output, "bundle");
}
const mode = status.suggestedMode ?? "bundle";
await installFromGitHub(extContext, output, mode);
} catch (e) {
const err = e instanceof Error ? e.message : String(e);
output.appendLine(`[install] ${err}`);
@@ -77,8 +82,10 @@ async function installCli(): Promise<void> {
async function checkCli(): Promise<void> {
const status = checkInstallStatus(extContext);
output.appendLine(`[check] ${status.detail}`);
if (status.cliOk && status.libOk) {
void vscode.window.showInformationMessage(`Marqdo ready: ${status.version}`);
if (status.cliOk && status.versionOk && status.libOk) {
void vscode.window.showInformationMessage(
`Marqdo ready: ${status.parsedVersion ?? status.version} (≥ ${status.requiredVersion}), stdlib OK`
);
return;
}
const choice = await vscode.window.showWarningMessage(
@@ -106,21 +113,53 @@ function activeMqDoc(): vscode.TextDocument | undefined {
async function ensureCliOrOfferInstall(): Promise<boolean> {
const status = checkInstallStatus(extContext);
if (status.cliOk) {
if (status.cliOk && status.versionOk && status.libOk) {
return true;
}
const choice = await vscode.window.showWarningMessage(
"Marqdo CLI not found. Install the official release (includes stdlib)?",
"Install",
"Cancel"
);
if (status.cliOk && status.versionOk && !status.libOk) {
const choice = await vscode.window.showWarningMessage(
"Standard library (lib/) is missing. Continue anyway, or install stdlib?",
"Install stdlib",
"Continue",
"Cancel"
);
if (choice === "Install stdlib") {
await installCli();
return true;
}
if (choice === "Continue") {
return true;
}
return false;
}
const label = !status.cliOk
? "Marqdo CLI not found. Install the official release (includes stdlib)?"
: `Marqdo CLI is below ${status.requiredVersion}. Upgrade now?`;
const choice = await vscode.window.showWarningMessage(label, "Install", "Cancel");
if (choice === "Install") {
await installCli();
return checkInstallStatus(extContext).cliOk;
const again = checkInstallStatus(extContext);
return again.cliOk && again.versionOk;
}
return false;
}
function resolveTargetPath(): string | undefined {
const doc = activeMqDoc();
const folder = vscode.workspace.workspaceFolders?.[0];
return doc?.uri.fsPath ?? folder?.uri.fsPath;
}
async function openUrl(url: string, settingKey: "viewOpen" | "debugOpen"): Promise<void> {
const open =
vscode.workspace.getConfiguration("marqdo").get<string>(settingKey) ?? "external";
if (open === "simpleBrowser") {
await vscode.commands.executeCommand("simpleBrowser.show", url);
} else {
await vscode.env.openExternal(vscode.Uri.parse(url));
}
}
async function runActiveFile(): Promise<void> {
if (!(await ensureCliOrOfferInstall())) {
return;
@@ -168,13 +207,35 @@ async function diagnoseDocument(doc: vscode.TextDocument): Promise<void> {
}
}
async function startView(): Promise<void> {
if (!(await ensureCliOrOfferInstall())) {
return;
}
const target = resolveTargetPath();
if (!target) {
void vscode.window.showWarningMessage("Open a folder or .mq.md file for view.");
return;
}
const cfg = vscode.workspace.getConfiguration("marqdo");
const host = cfg.get<string>("viewHost") ?? "127.0.0.1";
const port = cfg.get<number>("viewPort") ?? 7429;
output.show(true);
try {
const url = await viewServer.start("view", target, host, port);
await openUrl(url, "viewOpen");
void vscode.window.setStatusBarMessage(`Marqdo view: ${url}`, 5000);
} catch (e) {
void vscode.window.showErrorMessage(
`Marqdo view failed: ${e instanceof Error ? e.message : String(e)}`
);
}
}
async function startDebug(): Promise<void> {
if (!(await ensureCliOrOfferInstall())) {
return;
}
const doc = activeMqDoc();
const folder = vscode.workspace.workspaceFolders?.[0];
const target = doc?.uri.fsPath ?? folder?.uri.fsPath;
const target = resolveTargetPath();
if (!target) {
void vscode.window.showWarningMessage("Open a folder or .mq.md file to debug.");
return;
@@ -184,13 +245,8 @@ async function startDebug(): Promise<void> {
const port = cfg.get<number>("debugPort") ?? 7430;
output.show(true);
try {
const url = await debugServer.start(target, host, port);
const open = cfg.get<string>("debugOpen") ?? "external";
if (open === "simpleBrowser") {
await vscode.commands.executeCommand("simpleBrowser.show", url);
} else {
await vscode.env.openExternal(vscode.Uri.parse(url));
}
const url = await debugServer.start("debug", target, host, port);
await openUrl(url, "debugOpen");
void vscode.window.setStatusBarMessage(`Marqdo debug: ${url}`, 5000);
} catch (e) {
void vscode.window.showErrorMessage(
@@ -199,6 +255,11 @@ async function startDebug(): Promise<void> {
}
}
async function stopView(): Promise<void> {
await viewServer.stop();
void vscode.window.setStatusBarMessage("Marqdo view stopped", 3000);
}
async function stopDebug(): Promise<void> {
await debugServer.stop();
void vscode.window.setStatusBarMessage("Marqdo debug stopped", 3000);
+77 -11
View File
@@ -12,12 +12,40 @@ const MANAGED_DIR = "cli";
export type InstallStatus = {
cliOk: boolean;
libOk: boolean;
versionOk: boolean;
version?: string;
parsedVersion?: string;
requiredVersion: string;
cliPath?: string;
libRoot?: string;
detail: string;
/** Suggested install mode when not fully ready */
suggestedMode?: "bundle" | "stdlib";
};
/** Parse first semver-like token from `marqdo --version` output. */
export function parseCliVersion(raw: string): string | undefined {
const m = raw.match(/(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)/);
return m?.[1];
}
/** Compare a.b.c semver (pre-release suffix ignored for ordering). Returns <0 if a<b. */
export function compareSemver(a: string, b: string): number {
const pa = a.split("-")[0].split(".").map((x) => Number.parseInt(x, 10) || 0);
const pb = b.split("-")[0].split(".").map((x) => Number.parseInt(x, 10) || 0);
for (let i = 0; i < 3; i++) {
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
if (d !== 0) {
return d;
}
}
return 0;
}
function requiredVersion(): string {
return cfg().get<string>("minCliVersion")?.trim() || "0.1.0";
}
type GhAsset = { name: string; browser_download_url: string };
type GhRelease = {
tag_name: string;
@@ -131,26 +159,52 @@ function stdlibPresent(cliPath: string, context?: vscode.ExtensionContext): { ok
}
export function checkInstallStatus(context: vscode.ExtensionContext): InstallStatus {
const need = requiredVersion();
const cliPath = resolveCliPath(context);
const probe = probeMarqdo(cliPath);
if (!probe.ok) {
return {
cliOk: false,
libOk: false,
versionOk: false,
requiredVersion: need,
cliPath,
detail: probe.detail,
detail: `Marqdo CLI not found (${probe.detail}). Need ≥ ${need} with stdlib.`,
suggestedMode: "bundle",
};
}
const parsed = parseCliVersion(probe.version ?? "");
const versionOk = parsed !== undefined && compareSemver(parsed, need) >= 0;
const lib = stdlibPresent(cliPath, context);
let suggestedMode: "bundle" | "stdlib" | undefined;
if (!versionOk) {
suggestedMode = "bundle";
} else if (!lib.ok) {
suggestedMode = "stdlib";
}
const parts: string[] = [`CLI ${probe.version}`];
if (!versionOk) {
parts.push(`version too old (need ≥ ${need}${parsed ? `, found ${parsed}` : ""})`);
}
if (lib.ok) {
parts.push(`stdlib at ${lib.root}`);
} else {
parts.push("stdlib missing (no lib/ next to binary / MARQDO_LIB)");
}
return {
cliOk: true,
libOk: lib.ok,
versionOk,
version: probe.version,
parsedVersion: parsed,
requiredVersion: need,
cliPath,
libRoot: lib.root,
detail: lib.ok
? `CLI ${probe.version}; stdlib at ${lib.root}`
: `CLI ${probe.version}; stdlib missing (no lib/ next to binary, no MARQDO_LIB)`,
detail: parts.join("; "),
suggestedMode,
};
}
@@ -325,9 +379,13 @@ export async function installFromGitHub(
const status = checkInstallStatus(context);
output.appendLine(status.detail);
if (status.cliOk && status.libOk) {
if (status.cliOk && status.versionOk && status.libOk) {
void vscode.window.showInformationMessage(
`Marqdo installed (${status.version}). Stdlib ready.`
`Marqdo ready (${status.parsedVersion ?? status.version}). Stdlib OK.`
);
} else if (status.cliOk && !status.versionOk) {
void vscode.window.showWarningMessage(
`Marqdo CLI still below ${status.requiredVersion}. ${status.detail}`
);
} else if (status.cliOk && !status.libOk) {
void vscode.window.showWarningMessage(
@@ -378,24 +436,32 @@ export async function promptIfNeeded(
const status = checkInstallStatus(context);
output.appendLine(`[install] ${status.detail}`);
if (status.cliOk && status.libOk) {
if (status.cliOk && status.versionOk && status.libOk) {
output.appendLine(`[install] ready (CLI ≥ ${status.requiredVersion}, stdlib present)`);
return;
}
let message: string;
let primary: string;
const mode = status.suggestedMode ?? "bundle";
if (!status.cliOk) {
message =
"Marqdo CLI not found. Download the official release (includes standard library) into the extension?";
message = `Marqdo CLI not detected. Install CLI ≥ ${status.requiredVersion} with the standard library?`;
primary = "Install CLI + stdlib";
} else if (!status.versionOk) {
message = `Marqdo CLI version is too old (need ≥ ${status.requiredVersion}${
status.parsedVersion ? `, found ${status.parsedVersion}` : `, raw: ${status.version}`
}). Upgrade now (includes stdlib)?`;
primary = "Upgrade CLI + stdlib";
} else {
message =
"Marqdo CLI found, but the standard library (lib/) is missing. Download stdlib into the extension?";
"Marqdo CLI is OK, but the standard library (lib/) is missing. Download stdlib into the extension?";
primary = "Install stdlib";
}
const choice = await vscode.window.showWarningMessage(
message,
{ modal: false },
primary,
"Open Releases",
"Don't ask again"
@@ -403,7 +469,7 @@ export async function promptIfNeeded(
if (choice === primary) {
try {
await installFromGitHub(context, output, status.cliOk ? "stdlib" : "bundle");
await installFromGitHub(context, output, mode);
} catch (e) {
const err = e instanceof Error ? e.message : String(e);
output.appendLine(`[install] ${err}`);