I just began learning about vscode extensions and I'm wondering if there is a simple way to programmatically close the information message box that is generated via vscode.window.showInformationMessage()
. If you want to reproduce, I started with the WordCount demo here, and after copy/pasting the body of extension.ts
, as outlined in the tutorial, I made some modifications to activate()
like so...
export function activate(context: ExtensionContext) {
let wordCounter = new WordCounter();
let wcTimeout = null;
let setWCTimeout = function() {
clearWCTimeout();
wcTimeout = setTimeout(clearWCTimeout, 1000);
};
let clearWCTimeout = function() {
if (wcTimeout !== null) {
clearTimeout(wcTimeout);
wcTimeout = null;
// clear/hide the message box here, but how?
}
};
let disposable = commands.registerCommand('extension.sayHello', () => {
wordCounter.updateWordCount();
setWCTimeout();
vscode.window
.showInformationMessage(wordCounter._getWordCount(window.activeTextEditor.document).toString())
.then(clearWCTimeout);
});
// Add to a list of disposables which are disposed when this extension is deactivated.
context.subscriptions.push(wordCounter);
context.subscriptions.push(wcTimeout);
context.subscriptions.push(disposable);
}
What I've tried, or considered:
vscode.window.showInformationMessage()
with both null
and empty string - does nothing, anything besides null
|empty string results in a new information message box appearingSide note: I am interested in best practices within this paradigm. For instance, is there a popular node lib that wraps js timers that I might want to consider using? However, that is not my primary concern with this post. If you're going to comment on the delay mechanism (setTimeout()/clearTimeout()
), please make it constructive in terms of what is best practice within this environment/paradigm (beyond "that's ugly", or "that's not how [you would personally] do it).
Although it seems that the message does not have an explicit close API (https://github.com/Microsoft/vscode/issues/2732), my workaround is to use a progress to simulate an automatically-closed notification:
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: 'Finding ...',
cancellable: false,
},
async (progress, token) => {
for (let i = 0; i < 10; i++) {
setTimeout(() => {
progress.report({ increment: i*10, message: title })
}, 10000)
}
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With