如何在Visual Studio代码中侦听特定文件上的事件



我是visual studio代码API的新手。

我想听听一些事件,比如onSave,onClose等等…

我当前正在使用此API
但我觉得这样的价格不一样。当您只需要侦听特定文件的事件时。

let { workspace } = require("vscode");
module.exports = {
activate(ctx) {
ctx.subscriptions.push(
workspace.onDidSaveTextDocument(({ fileName, version }) => {

})
)
},
deactivate() {}
};

这不是一个很好的解决方案。但对我来说很有效。尽管还有其他办法,更好的办法?。。。

我的解决方案是…

let onSaveCleaner = workspace.onDidSaveTextDocument((e) => {
// watch for all file save event ... yuck!
});
let onCloseCleaner = workspace.onDidCloseTextDocument(({ fileName, languageId }) => {
// some condition like once `specific` file closed 
if (languageId == "python" && fileName == "...") {
onSaveCleaner.dispose()
onCloseCleaner.dispose()
}
});

通过这样做,我们只在文件未关闭时运行昂贵的代码!一旦文件关闭。。。毁灭一切!

最新更新