mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-06-13 07:10:26 +08:00
add eventing and update command
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
|
||||
This is a sample extension that shows how to add virtual documents to the editor.
|
||||
|
||||

|
||||

|
||||
|
||||
|
||||
## VS Code API
|
||||
|
||||
BIN
virtual-document-sample/cowsay-bwd.png
Normal file
BIN
virtual-document-sample/cowsay-bwd.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
BIN
virtual-document-sample/cowsay.gif
Normal file
BIN
virtual-document-sample/cowsay.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 MiB |
@ -22,9 +22,30 @@
|
||||
],
|
||||
"main": "./out/extension",
|
||||
"contributes": {
|
||||
"commands": {
|
||||
"command": "cowsay.say",
|
||||
"title": "cowsay"
|
||||
"commands": [
|
||||
{
|
||||
"command": "cowsay.say",
|
||||
"title": "cowsay"
|
||||
},
|
||||
{
|
||||
"command": "cowsay.backwards",
|
||||
"title": "cowsay (↹)"
|
||||
}
|
||||
],
|
||||
"menus": {
|
||||
"editor/title": [
|
||||
{
|
||||
"command": "cowsay.backwards",
|
||||
"group": "navigation",
|
||||
"when": "resourceScheme == cowsay"
|
||||
}
|
||||
],
|
||||
"commandPalette": [
|
||||
{
|
||||
"command": "cowsay.backwards",
|
||||
"when": "resourceScheme == cowsay"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 35 KiB |
@ -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 });
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user