Skip to content
Open
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
3 changes: 3 additions & 0 deletions editors/vscode/src/extension.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ export async function tinymistActivate(
await tinymistDeactivate(trait);
await tinymistActivate(context, trait);
}),
commands.registerCommand("tinymist.getLogText", (options?: { maxChars?: number }) => {
return tinymist.getLogText(options?.maxChars);
}),
Comment thread
Myriad-Dreamin marked this conversation as resolved.
commands.registerCommand("tinymist.showLog", () => tinymist.showLog()),
);
// Activates platform-dependent features
Expand Down
98 changes: 97 additions & 1 deletion editors/vscode/src/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,103 @@ interface JumpInfo {
end: [number, number] | null;
}

class BufferedOutputChannel implements vscode.OutputChannel {
private readonly channel: vscode.OutputChannel;
private buffer = "";

constructor(
name: string,
languageId?: string,
private readonly maxChars: number = 200_000,
) {
this.channel = languageId
? vscode.window.createOutputChannel(name, languageId)
: vscode.window.createOutputChannel(name);
}

get name(): string {
return this.channel.name;
}

append(value: string): void {
this.push(value);
this.channel.append(value);
}

appendLine(value: string): void {
this.push(`${value}\n`);
this.channel.appendLine(value);
}

clear(): void {
this.buffer = "";
this.channel.clear();
}

show(preserveFocus?: boolean): void;
show(column?: vscode.ViewColumn, preserveFocus?: boolean): void;
show(
columnOrPreserveFocus?: vscode.ViewColumn | boolean,
preserveFocus?: boolean,
): void {
if (typeof columnOrPreserveFocus === "boolean" || columnOrPreserveFocus === undefined) {
this.channel.show(columnOrPreserveFocus);
return;
}

this.channel.show(columnOrPreserveFocus, preserveFocus);
}

hide(): void {
this.channel.hide();
}

replace(value: string): void {
this.buffer = "";
this.push(value);

if ("replace" in this.channel && typeof this.channel.replace === "function") {
this.channel.replace(value);
return;
}

this.channel.clear();
this.channel.append(value);
}

dispose(): void {
this.channel.dispose();
}

getText(maxChars?: number): string {
if (maxChars === 0) {
return "";
}

if (maxChars === undefined || maxChars < 0 || this.buffer.length <= maxChars) {
return this.buffer;
}

return this.buffer.slice(-maxChars);
}
Comment thread
Myriad-Dreamin marked this conversation as resolved.

private push(value: string): void {
if (!value || this.maxChars <= 0) {
return;
}

this.buffer += value;
if (this.buffer.length > this.maxChars) {
this.buffer = this.buffer.slice(-this.maxChars);
}
}
Comment thread
Myriad-Dreamin marked this conversation as resolved.
}

export class LanguageState {
static Client: typeof createSystemLanguageClient = undefined!;
static HoverTmpStorage?: typeof HoverTmpStorage = undefined;

outputChannel: vscode.OutputChannel = vscode.window.createOutputChannel("Tinymist Typst", "log");
outputChannel = new BufferedOutputChannel("Tinymist Typst", "log");
context: vscode.ExtensionContext = undefined!;
client: LanguageClient | undefined = undefined;
_watcher: vscode.FileSystemWatcher | undefined = undefined;
Expand Down Expand Up @@ -389,6 +481,10 @@ export class LanguageState {
}
}

getLogText(maxChars?: number) {
return this.outputChannel.getText(maxChars);
}

/**
* The commands group for the *Document Preview* feature. This feature is used to preview multiple
* documents at the same time.
Expand Down
19 changes: 19 additions & 0 deletions editors/vscode/src/test/e2e/simple-docs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from "vscode";
import { tinymist } from "../../lsp";
import type { Context } from ".";

export async function getTests(ctx: Context) {
Expand Down Expand Up @@ -77,5 +78,23 @@ export async function getTests(ctx: Context) {
// close the editor
await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
});

suite.addTest("get recent log text", async () => {
const marker = `agent-log-marker-${Date.now()}`;
tinymist.outputChannel.append(marker);

const logs = await vscode.commands.executeCommand<string>("tinymist.getLogText");
ctx.expect(logs).to.include(marker);

const none = await vscode.commands.executeCommand<string>("tinymist.getLogText", {
maxChars: 0,
});
ctx.expect(none).to.equal("");

const tail = await vscode.commands.executeCommand<string>("tinymist.getLogText", {
maxChars: 16,
});
ctx.expect(tail.length).to.be.lessThanOrEqual(16);
});
});
}
Loading