call-hierarchy-sample

This commit is contained in:
Jan Dolejsi
2019-11-11 00:47:36 +01:00
parent a8af0838d6
commit a1d64677ea
18 changed files with 824 additions and 28 deletions

View File

@ -2,41 +2,82 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import { ExtensionContext, StatusBarAlignment, window, StatusBarItem, Selection, workspace, TextEditor, commands, ProgressLocation } from 'vscode';
import { ExtensionContext, window, commands, ProgressLocation, CancellationToken, Progress } from 'vscode';
import { spawn, spawnSync } from 'child_process';
export function activate(context: ExtensionContext) {
context.subscriptions.push(commands.registerCommand('extension.startTask', () => {
context.subscriptions.push(commands.registerCommand('extension.askMode', () => {
return window.showQuickPick(['sync', 'async'], { placeHolder: 'Pick mode...' });
}));
context.subscriptions.push(commands.registerCommand('extension.startTask', async () => {
let mode = await commands.executeCommand('extension.askMode');
window.withProgress({
location: ProgressLocation.Notification,
title: "I am long running!",
title: "I am long running",
cancellable: true
}, (progress, token) => {
}, async (progress, token) => {
token.onCancellationRequested(() => {
console.log("User canceled the long running operation");
});
progress.report({ increment: 0 });
setTimeout(() => {
progress.report({ increment: 10, message: "I am long running! - still going..." });
}, 1000);
setTimeout(() => {
progress.report({ increment: 40, message: "I am long running! - still going even more..." });
}, 2000);
setTimeout(() => {
progress.report({ increment: 50, message: "I am long running! - almost there..." });
}, 3000);
var p = new Promise(resolve => {
setTimeout(() => {
resolve();
}, 5000);
});
return p;
switch (mode) {
case undefined:
return; // canceled by the user
case 'sync':
return spawnSomethingSync(token);
case 'async':
default:
return spawnSomethingAsync(progress, token);
}
});
}));
}
/**
* Synchronous approach
* @param _token cancellation token (unused in the sync approach)
*/
function spawnSomethingSync(_token: CancellationToken): Promise<void> {
return new Promise(resolve => {
console.log('Started...');
let child = spawnSync('cmd', ['/c', 'dir', '/S'], { cwd: 'c:\\', encoding: 'utf8' });
console.log(`stdout: ${child.stdout.slice(0, 1000)}`); // otherwise it is too big for the console
resolve();
});
}
/**
* Asynchronous approach
* @param token cancellation token (triggered by the cancel button on the UI)
*/
function spawnSomethingAsync(progress: Progress<{ message?: string; increment?: number }>, token: CancellationToken): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (token.isCancellationRequested) {
return;
}
var progressUpdate = 'Starting up...';
const interval = setInterval(() => progress.report({ message: progressUpdate }), 500);
let childProcess = spawn('cmd', ['/c', 'dir', '/S'], { cwd: 'C:\\Users\\jdolejsi\\AppData\\' })
.on("close", (code, signal) => {
console.log(`Closed: ${code} ${signal}`);
if (childProcess.killed) { console.log('KILLED'); }
resolve();
clearInterval(interval);
})
.on("error", err => {
reject(err);
});
childProcess.stdout
.on("data", (chunk: string | Buffer) => {
console.log(`stdout: ${chunk}`);
progressUpdate = chunk.toString('utf8', 0, 50).replace(/[\r\n]/g, '');
});
token.onCancellationRequested(_ => childProcess.kill());
});
}