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
33 changes: 20 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { ManimCell } from './manimCell';
import { ManimCellRanges } from './manimCellRanges';
import { previewCode } from './previewCode';
import { startScene, exitScene } from './startStopScene';
import { loggerName } from './logger';
import Logger from './logger';
import { Logger, Window, loggerName } from './logger';

export function activate(context: vscode.ExtensionContext) {

Expand All @@ -15,35 +14,41 @@ export function activate(context: vscode.ExtensionContext) {

const previewManimCellCommand = vscode.commands.registerCommand(
'manim-notebook.previewManimCell', (cellCode?: string, startLine?: number) => {
Logger.info(`💠 Command requested: Preview Manim Cell, startLine=${startLine}`);
previewManimCell(cellCode, startLine);
});

const previewSelectionCommand = vscode.commands.registerCommand(
'manim-notebook.previewSelection', () => {
Logger.info("💠 Command requested: Preview Selection");
previewSelection();
}
);

const startSceneCommand = vscode.commands.registerCommand(
'manim-notebook.startScene', () => {
Logger.info("💠 Command requested: Start Scene");
startScene();
}
);

const exitSceneCommand = vscode.commands.registerCommand(
'manim-notebook.exitScene', () => {
Logger.info("💠 Command requested: Exit Scene");
exitScene();
}
);

const clearSceneCommand = vscode.commands.registerCommand(
'manim-notebook.clearScene', () => {
Logger.info("💠 Command requested: Clear Scene");
clearScene();
}
);

const openLogFileCommand = vscode.commands.registerCommand(
'manim-notebook.openLogFile', async () => {
Logger.info("💠 Command requested: Open Log File");
openLogFile(context);
});

Expand All @@ -60,7 +65,9 @@ export function activate(context: vscode.ExtensionContext) {
Logger.info("Manim Notebook activated");
}

export function deactivate() { }
export function deactivate() {
Logger.info("💠 Manim Notebook extension deactivated");
}

/**
* Previews the Manim code of the cell where the cursor is placed
Expand All @@ -74,7 +81,7 @@ async function previewManimCell(cellCode?: string, startLine?: number) {
if (cellCode === undefined) {
const editor = window.activeTextEditor;
if (!editor) {
window.showErrorMessage(
Window.showErrorMessage(
'No opened file found. Place your cursor in a Manim cell.');
return;
}
Expand All @@ -84,15 +91,15 @@ async function previewManimCell(cellCode?: string, startLine?: number) {
const cursorLine = editor.selection.active.line;
const range = ManimCellRanges.getCellRangeAtLine(document, cursorLine);
if (!range) {
window.showErrorMessage('Place your cursor in a Manim cell.');
Window.showErrorMessage('Place your cursor in a Manim cell.');
return;
}
cellCode = document.getText(range);
startLineFinal = range.start.line;
}

if (startLineFinal === undefined) {
window.showErrorMessage('Internal error: Line number not found in `previewManimCell()`.');
Window.showErrorMessage('Internal error: Line number not found in `previewManimCell()`.');
return;
}

Expand All @@ -105,7 +112,7 @@ async function previewManimCell(cellCode?: string, startLine?: number) {
async function previewSelection() {
const editor = window.activeTextEditor;
if (!editor) {
window.showErrorMessage('Select some code to preview.');
Window.showErrorMessage('Select some code to preview.');
return;
}

Expand All @@ -125,7 +132,7 @@ async function previewSelection() {
}

if (!selectedText) {
window.showErrorMessage('Select some code to preview.');
Window.showErrorMessage('Select some code to preview.');
return;
}

Expand All @@ -140,7 +147,7 @@ async function clearScene() {
try {
await ManimShell.instance.executeCommandErrorOnNoActiveSession("clear()");
} catch (NoActiveSessionError) {
window.showErrorMessage('No active Manim session found to remove objects from.');
Window.showErrorMessage('No active Manim session found to remove objects from.');
}
}

Expand Down Expand Up @@ -185,17 +192,17 @@ function registerManimCellProviders(context: vscode.ExtensionContext) {
*/
function openLogFile(context: vscode.ExtensionContext) {
const logFilePath = vscode.Uri.joinPath(context.logUri, `${loggerName}.log`);
vscode.window.withProgress({
window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "Opening Manim Notebook log file...",
cancellable: false
}, async (progressIndicator, token) => {
await new Promise<void>(async (resolve) => {
try {
const doc = await vscode.workspace.openTextDocument(logFilePath);
await vscode.window.showTextDocument(doc);
await window.showTextDocument(doc);
} catch {
vscode.window.showErrorMessage("Could not open Manim Notebook log file");
Window.showErrorMessage("Could not open Manim Notebook log file");
} finally {
resolve();
}
Expand All @@ -207,4 +214,4 @@ function openLogFile(context: vscode.ExtensionContext) {
// https://github.com/microsoft/vscode/blob/9de080f7cbcec77de4ef3e0d27fbf9fd335d3fba/extensions/typescript-language-features/src/typescriptServiceClient.ts#L580-L586
});
});
}
}
24 changes: 23 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from 'path';
export const loggerName = 'Manim Notebook';
const logger = window.createOutputChannel(loggerName, { log: true });

export default class Logger {
export class Logger {

public static trace(message: string) {
logger.trace(`${Logger.getFormattedCallerInformation()} ${message}`);
Expand Down Expand Up @@ -87,3 +87,25 @@ export default class Logger {
return `[${fileName}] [${methodName}]`;
}
}

/**
* Class that wraps some VSCode window methods to log the messages before
* displaying them to the user as a notification.
*/
export class Window {

public static showInformationMessage(message: string) {
Logger.info(`💡 ${message}`);
window.showInformationMessage(message);
}

public static showWarningMessage(message: string, ...items: string[]) {
Logger.warn(`💡 ${message}`);
window.showWarningMessage(message, ...items);
}

public static showErrorMessage(message: string) {
Logger.error(`💡 ${message}`);
window.showErrorMessage(message);
}
}
4 changes: 2 additions & 2 deletions src/manimCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export class ManimCell implements vscode.CodeLensProvider, vscode.FoldingRangePr
}

public resolveCodeLens(codeLens: vscode.CodeLens, token: vscode.CancellationToken): vscode.CodeLens {
if (!vscode.window.activeTextEditor) {
if (!window.activeTextEditor) {
return codeLens;
}

const document = vscode.window.activeTextEditor?.document;
const document = window.activeTextEditor?.document;
const range = new vscode.Range(codeLens.range.start, codeLens.range.end);
const cellCode = document.getText(range);

Expand Down
Loading