Run command on file load

Hey guys, I’m created a atom package to add some decorations to my text editor. I would like this package to run the decoration command whenever a new file is loaded. I was looking through the docs and though that the onDidAddTextEditor event might be the subscription i was looking for. However, no events were ever fired, (at least nothing outputted in the console log).

Does anyone know how to listen for file load events within atom ?

this.subscriptions.add(
  atom.workspace.onDidAddTextEditor(editor => {
    // Memory leak - this `Disposable` retains `editor` until the package is
    // deactivated, even though `editor` may be destroyed long before then!
    const editorSubscription = editor.onDidStopChanging(() => {
      console.log("Editor changed", editor);
    });

    this.subscriptions.add(editorSubscription);
  })
);

You probably want observeTextEditors; this will call it for all current and future text editors (as opposed to onDidAdd…, which is only for editors opened after your package has been loaded). You should add the console log statement to this scope to confirm it is working properly, rather than inside another event handler.

And yes, a TextEditor generally represents an open file, though multiple TextEditors may point to the same file, and some may not have a backing file (e.g., new files).

1 Like

Hey, thanks for the suggestion. I tried subscribing to the observeTextEditors, event. But, i don’t see anything being dumped in my console when opening files (or at any time). Do you maybe have a working example i can peek at?

  activate(state) {
    // Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
    this.subscriptions = new CompositeDisposable();
    this.subscriptions.add(
      atom.workspace.observeTextEditors(editor => {
        console.log("Editor changed", editor);
      })
    );
  },

Try logging your package loading too; it’s possible the activate method is not being called. It’s difficult to say what the cause is without the full context.

Regarding observeTextEditors, try this in your init.js file (remove your init.coffee for now if it exists)

atom.workspace.observeTextEditors(editor => {
  console.log("Opened", editor)
})

Also make sure to reload Atom when testing new changes (Window: Reload in the command palette)

Hey,
Thanks for the help. I played around with it a bit, and found out it does actually print to the console when a file is opened. However, it only will do this, after i run a command. Do you happened to know why that is and/or how i can subscribe without running a command first.

Repo:
my-package

Your package only activates when you run better-comments:format, so it makes perfect sense it behaves this way (read more about activationCommands on the Flight Manual). You might want to use an activationHook instead or omit activation events altogether (which is not recommended!)

1 Like

@idleberg, you are correct. It was because i didn’t add the activationHooks. Thank you so much. Any suggestions for which activationHook fits this context ? I suppose i could load supported language grammars.

However, i see that the editor stores all the grammars used in the editor. Maybe i can hook it to a core function, and then search through the grammars.

That depends on how your package is meant to work. I’m not quite sure based on the README; is highlighting automatic, or done when a command is run? Is it for all languages, or a select few?

The only reason i have it running off a command, is because this is my first package, and i didn’t know where to start.

The package is meant to do highlighting (add decorations), automatically. I basically want to have a package that allows me to customize my comments. For example, if i add a ! to my comments. I would like that line highlighted with a color i specify. I have that logic worked out (for the most part). Now, i just have to hook up atom to run the script on the files, when they open in the text editor, not when a command is emitted.
If i can have this package highlight all files, that would be perfect.

@Aerijo, @idleberg
Thank you guys. I’ll very much appreciate your help and apologies for any dump questions i may have asked.

The language-todo package offers you an example to do what you aim for.
See that package here: https://atom.io/packages/language-todo

This is a package that runs on-top of other syntax highlighting to identify special words. New scope identifiers are used. In turn this can be used in LESS to colour the words.

My modified grammar package gives the result:
image

The problem with language-todo right now is that it only works on the older TextMate grammars. Writing a grammar is probably the best way of doing it, but Tree-sitter grammars need to be compiled, which is a whole can of worms.

@Aerijo: A grammar package would have the best user experience.
I would use that even if Tree-sitter was not handled.

Expanding the conversation:
As alternative a regular package could be used
but the highlighting is best updated by another trigger than discussed already.

Perhaps onDidChange.
This will eat up computer resources: regular updates and defining if the code words are in allowed scope sections.