VSCode Extension API - "text document became dirty/unsaved"事件



我正在创建一个小的VSCode扩展。我发现当文档被更改时发出一个事件:

vscode.workspace.onDidChangeTextDocument(function(e) {
console.log('changed.');
console.log(e.document.isDirty);
});

然而,我只想在此更改使文档变脏/未保存时触发我的代码。每次更改都会触发onDidChangeTextDocument事件。似乎没有onWillChangeTextDocument事件。

是否有一种方法可以只检测"first">

将文档状态从isDirty: false更改为isDirty: true?

我通过保持文件数组的变化来实现这个功能:

var isDirty = [];
function activate(context) {
console.log('activated.');

vscode.workspace.onDidChangeTextDocument(function(e) {
console.log('Changed.');

if (!isDirty.includes(e.document.uri.path)) {
console.log('This is the change that made this file "dirty".');

isDirty.push(e.document.uri.path);

// Place code that you only want to run on the first change (that makes a document dirty) here...
}
});

vscode.workspace.onDidSaveTextDocument(function(e) {
console.log('Saved!');

const index = isDirty.indexOf(e.uri.path);

if (index > -1) {
isDirty.splice(index, 1);
}
});

}
exports.activate = activate;

相关内容

  • 没有找到相关文章

最新更新