Targeting a specific language for buffer changed event


#1

Currently I am using the following code to bind a buffer changed event for all Editors with grammar type ‘HTML’:

atom.workspaceView.eachEditorView(function(editorView){
    editorView.command('editor:grammar-changed', {}, function() {
        var grammar = editorView.editor.getGrammar();
        if(grammar.name === 'HTML') {
            editorView.editor.buffer.on('changed', fcn);
        } else {
            editorView.editor.buffer.off('changed', fcn);
        }
    }).trigger('editor:grammar-changed');
});

It work’s well enough, however I see some situations it might break down. Specifically if I were editing HTML in a PHP file, which would assumedly be assigned a grammar type PHP instead of HTML.

I could solve the issue by allowing the user to define grammar names to support, however that’s kind of annoying for users. It would be nice if I could do something like the following so that the event would fire anytime an .html line is edited rather than relying on the selected grammar

atom.workspaceView.eachEditorView(function(editorView){
    editorView.command('buffer:changed', '.html', fcn);
});

I’ve tried different events: ‘buffer:changed’, ‘changed’, ‘editor:contents-modifed’, ‘content-modified’ but nothing seems to get captured.


#2

The buffer events aren’t bubbled up to the editorView. But contents-modified is triggered on Editor instances.