add eventing and update command

This commit is contained in:
Johannes Rieken
2018-11-16 12:16:23 +01:00
parent c3fe2d0459
commit 7e8bb9e09b
6 changed files with 47 additions and 5 deletions

View File

@ -12,6 +12,11 @@ export function activate({ subscriptions }: vscode.ExtensionContext) {
// register a content provider for the cowsay-scheme
const myScheme = 'cowsay';
const myProvider = new class implements vscode.TextDocumentContentProvider {
// emitter and its event
onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
onDidChange = this.onDidChangeEmitter.event;
provideTextDocumentContent(uri: vscode.Uri): string {
// simply invoke cowsay, use uri-path as text
return cowsay.say({ text: uri.path });
@ -24,8 +29,24 @@ export function activate({ subscriptions }: vscode.ExtensionContext) {
let what = await vscode.window.showInputBox({ placeHolder: 'cowsay...' });
if (what) {
let uri = vscode.Uri.parse('cowsay:' + what);
let doc = await vscode.workspace.openTextDocument(uri);
let doc = await vscode.workspace.openTextDocument(uri); // calls back into the provider
await vscode.window.showTextDocument(doc, { preview: false });
}
}));
// register a command that updates the current cowsay
subscriptions.push(vscode.commands.registerCommand('cowsay.backwards', async () => {
if (!vscode.window.activeTextEditor) {
return; // no editor
}
let { document } = vscode.window.activeTextEditor;
if (document.uri.scheme !== myScheme) {
return; // not my scheme
}
// get path-components, reverse it, and create a new uri
let say = document.uri.path;
let newSay = say.split('').reverse().join('');
let newUri = document.uri.with({ path: newSay });
await vscode.window.showTextDocument(newUri, { preview: false });
}))
}