Skip to content
Merged

Pr 5 #10

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
95 changes: 65 additions & 30 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,83 @@ import * as vscode from 'vscode';
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "vscode-manim" is now active!');

// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
const disposable1 = vscode.commands.registerCommand('vscode-manim.helloData', () => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
// terminal.show();
/** Example command
* The command has been defined in the package.json file
* The commandId parameter must match the command field in package.json
*/
const helloData = vscode.commands.registerCommand('vscode-manim.helloData', () => {
vscode.window.showInformationMessage('Hello Data from vscode-manim!');
});

const disposable2 = vscode.commands.registerCommand('vscode-manim.checkpointPaste', async () => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
const terminal = vscode.window.activeTerminal || vscode.window.createTerminal();
const editor = vscode.window.activeTextEditor;
if (editor) {
// Get the selected text
const selection = editor.selection;
const selectedText = editor.document.getText(selection);

/**
* checkpoint_paste() functionality in a VSCode extension
*/
let isExecuting = false; // Flag: to prevent several commands executing at the same time (because clipboard saving would become uncontrollable in this case)
const checkpointPaste = vscode.commands.registerCommand('vscode-manim.checkpointPaste', async () => {
if (isExecuting) {
vscode.window.showInformationMessage('Please wait until the current command finishes executing.');
return;
}

isExecuting = true;
try {
// Editor must be found:
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('Editor not found');
return;
}
let selectedText;
if (editor.selection.isEmpty) {
// If nothing is selected - select the whole line (for convenience):
const line = editor.document.lineAt(editor.selection.start.line);
selectedText = editor.document.getText(line.range);
} else {
// If selected - extend selection to start and end of lines (for convenience):
const range = new vscode.Range(
editor.selection.start.with(undefined, 0),
editor.selection.end.with(undefined, Number.MAX_SAFE_INTEGER)
);
selectedText = editor.document.getText(range);
}
// Selected text must not be empty:
if (!selectedText) {
vscode.window.showErrorMessage('No text selected in the editor');
return;
}

// Save current clipboard content
const clipboardBuffer = await vscode.env.clipboard.readText();

// Copy the selected text to the clipboard
await vscode.env.clipboard.writeText(selectedText);

// Create or show the terminal
const terminal = vscode.window.activeTerminal || vscode.window.createTerminal();

// Send the checkpoint_paste() command
// terminal.sendText('checkpoint_paste()', false);
terminal.sendText('checkpoint_paste()');

vscode.window.showInformationMessage('Copied selected code and sent checkpoint_paste() to manim terminal');
} else {
vscode.window.showErrorMessage('No text is selected');
terminal.sendText(
'\x0C' + // to center the terminal (Command + l)
'checkpoint_paste()'
);

// move 1 line down in the text editor (to get ready to execute next line)
await vscode.commands.executeCommand('cursorDown');

// Restore original clipboard content
await new Promise(resolve => setTimeout(resolve, 500)); // must wait a bit (so that checkpoint_paste() above doesn't capture the next clipboard)
await vscode.env.clipboard.writeText(clipboardBuffer);

} catch (error) {
vscode.window.showErrorMessage(`Error: ${error}`);
} finally {
isExecuting = false;
}
});

context.subscriptions.push(disposable1, disposable2);

context.subscriptions.push(helloData, checkpointPaste);
}

// This method is called when your extension is deactivated
Expand Down