How can I make a package act on editors with a certain grammar?


I’m making a pretty-printer for a language, and I want it to do the following:

On opening a file of my language, it should act on the editor.
Upon changing a line in an editor for a file of my language, it should act again.

How do I encode this behaviour?

At the moment, I use an activation hook,

  "activationHooks": [

along with the activation command

atom.workspace.observeTextEditors (editor) ->
editor.onDidStopChanging ->
view = atom.views.getView editor
return unless view

    for element in view.querySelectorAll '::shadow .line span:not(.prettyprinted)'
      <pretty print>

But this seems to be applying my package to all open editors once I have activated the package by opening a file of my language; for example, If I open a file of my language, and then a python file, both will be pretty-printed! This is undesirable.


You should match against editor.getGrammar() and return if the editor doesn’t have the active grammar you’re looking for.