mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
add better sample for virtual documents
This commit is contained in:
14
virtual-document-sample/src/cowsay.d.ts
vendored
Normal file
14
virtual-document-sample/src/cowsay.d.ts
vendored
Normal 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;
|
||||
}
|
||||
31
virtual-document-sample/src/extension.ts
Normal file
31
virtual-document-sample/src/extension.ts
Normal 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 });
|
||||
}
|
||||
}));
|
||||
}
|
||||
Reference in New Issue
Block a user