The file i want to open has a simple encryption (it is from a software, but i have the license to encrypt it). If I open it as plain text it is not human readable. For simplicity just assume, that every char is shifted by one bit.
I want to change that, if the extension is right.
Openinig should decrypt the file, so it can be edited. Closing should encrypt it again, so the program reading the file is able to do so.
I have the logic running as a Notepad++ plugin, but I do not know how to implement it in VSCode.
Currently a language server is setup running for the decrypted files. This seems to work fine. Can I implement the logic anywhere in it, without to much performance penalty?
You can use the onDidOpenTextDocument
and onDidChangeActiveTextEditor
events to run code when the text editor has been changed to a newly opened file. Then in the onDidChangeActiveTextEditor
event, read the contents of the document, run the decrypt function, then use the TextEditor.edit
function to replace the text with decrypt text.
To encrypt after the file has been saved, use the onDidSaveTextDocument
function to read the saved file, encrypt the contents, then write the encrypted text to the saved file.
import * as vscode from 'vscode';
import * as path from 'path';
let steveChannel: vscode.OutputChannel | undefined;
let lastOpenFile = '' ;
// ----------------------------------- activate -----------------------------------
export function activate(context: vscode.ExtensionContext)
{
steveChannel_writeLine(`activate`);
vscode.window.onDidChangeActiveTextEditor((editor) =>
{
// text editor has been changed to a newly opened text file.
if ( lastOpenFile == editor?.document.fileName )
{
let document = editor.document;
const doc_text = editor.document.getText();
const uc_text = doc_text.toUpperCase();
// update contents of text editor to uppercase text.
editor.edit(editBuilder =>
{
const range = doc_entireDocRange(document) ;
editBuilder.replace( range, uc_text);
});
}
});
vscode.workspace.onDidOpenTextDocument((d) =>
{
const ext = path.extname(d.fileName) ;
if ( ext == '.txt')
{
steveChannel_writeLine(`file opened ${d.fileName}`);
// save name of most recently opened .txt file.
// Use in change text editor event to detect when the text editor
// contains the contents of a newly opened text file.
lastOpenFile = d.fileName ;
}
});
vscode.workspace.onDidSaveTextDocument((d) =>
{
const ext = path.extname(d.fileName);
if (ext == '.txt')
{
steveChannel_writeLine(`file saved ${d.fileName}`);
}
});
}
// ---------------------------------- deactivate ----------------------------------
// this method is called when your extension is deactivated
export function deactivate() {}
// ---------------------------- steveChannel_writeLine ----------------------------
function steveChannel_writeLine(line: string)
{
if (!steveChannel)
{
steveChannel = vscode.window.createOutputChannel('steve');
}
steveChannel.appendLine(line);
}
// ----------------------------- doc_entireDocRange -----------------------------
function doc_entireDocRange( doc: vscode.TextDocument )
{
let invalidRange = new vscode.Range(0, 0, doc.lineCount /*intentionally missing the '-1' */, 0);
let docRange = doc.validateRange(invalidRange);
return docRange ;
}