Strange behavior of editor.onDidSave


#1

I use the following code in my plugin:

activate: (state) ->
    atom.config.observe 'php-cs-fixer.executeOnSave', =>
        @executeOnSave = atom.config.get 'php-cs-fixer.executeOnSave'

    ...

    @subscriptions.add atom.workspace.observeTextEditors (editor) =>
        @subscriptions.add editor.onDidSave =>
            if editor.getGrammar().name == "PHP" and @executeOnSave
                @fix() # if @executeOnSave

The onDidSave only works if I once run the code with the shortcut. Perhaps I am understanding something wrong… I hope someone has a tip for me.


#2

here is the complete code https://github.com/pfefferle/atom-php-cs-fixer/blob/master/lib/php-cs-fixer.coffee#L51


#3

Your package.json contains activationCommands, which means that your code (and the subscription to onDidSave) only runs after you execute php-cs-fixer:fix.
Remove activationCommands if you want to execute your code and subscribe to events when atom starts.


#4

After removing that command, it works like a charm! Thanks a lot, you saved my day!