VSCode 分机 API - "text document became dirty/unsaved" 的活动

VSCode Extension API - event for "text document became dirty/unsaved"

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

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

但是,如果此更改使文档变为 dirty/unsaved,我只想触发我的代码。每次更改都会触发 onDidChangeTextDocument 事件。似乎没有 onWillChangeTextDocument 事件。

有没有办法只检测将文档状态从 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;