add better sample for virtual documents

This commit is contained in:
Johannes Rieken
2018-11-15 18:42:11 +01:00
parent 8e8001f9c5
commit 8724ad4806
12 changed files with 2628 additions and 0 deletions

14
virtual-document-sample/src/cowsay.d.ts vendored Normal file
View File

@ -0,0 +1,14 @@
declare module 'cowsay' {
export interface CowsayOptions {
text: string;
cow?: string;
eyes?: string;
tongue?: string;
wrap?: boolean;
wrapLength?: number;
mode?: 'b' | 'd' | 'g' | 'p' | 's' | 't' | 'w' | 'y'
}
export function say(options: CowsayOptions): string;
}

View File

@ -0,0 +1,31 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import * as cowsay from 'cowsay';
export function activate({ subscriptions }: vscode.ExtensionContext) {
// register a content provider for the cowsay-scheme
const myScheme = 'cowsay';
const myProvider = new class implements vscode.TextDocumentContentProvider {
provideTextDocumentContent(uri: vscode.Uri): string {
// simply invoke cowsay, use uri-path as text
return cowsay.say({ text: uri.path });
}
}
subscriptions.push(vscode.workspace.registerTextDocumentContentProvider(myScheme, myProvider));
// register a command that opens a cowsay-document
subscriptions.push(vscode.commands.registerCommand('cowsay.say', async () => {
let what = await vscode.window.showInputBox({ placeHolder: 'cowsay...' });
if (what) {
let uri = vscode.Uri.parse('cowsay:' + what);
let doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc, { preview: false });
}
}));
}