Load plugin after `atom.grammars` is loaded


#1

Hello,

I’m creating a plugin and it depends on atom.grammars.
On initial load of plugin atom.grammars.getGrammars() array has only NullGrammar.

Are there any way to load my plugin after all grammars are loaded to atom.grammars?


#2

Since Atom doesn’t know which packages contain grammars until they’re at least loaded you should probably use the atom.packages.onDidActivateInitialPackages event:

Add an event handler in your activate function to run when the the event fires and do all of your initialization in there.


#3

Thanks for your reply.

I tried this solution but it didn’t work. Maybe because my package provides atom.file-icons service.


#4

What is it you’re depending on atom.grammars for?


#5

I want to find file name or extension in atom.grammars and depending on scopeName to show icon.


#6

You could use atom.grammars.onDidAddGrammar to be notified when a grammar is added. There is also an onDidUpdateGrammar too. These would help you keep your list up-to-date if grammars are loaded after Atom first launches (which can happen).


#7

All these events are fired after my provider is consumed.


#8

I don’t understand why that is a problem?


#9

Let me describe this process in steps:

  1. Atom window loads
  2. My atom.file-icons provider is consumed by tree-view, tabs, etc. As there is no atom.grammars loaded, I see default icon icon-file-text for all files.
  3. Then atom.grammars loads.
  4. If I open any folder I can see correct icons.

#10

This question is still relevant and as temporary solution I saved source code file types to file.

Here is my package: https://atom.io/packages/simple-icons


#11

Atom’s events are to a large extent non-deterministic in their order. Your package should be able to handle the events in any order. So if you revise the steps above to:

  1. Atom window loads
  2. atom.file-icons provider is consumed
  3. atom.grammars.onDid(Add|Update)Grammar event is raised, your package should update its internal state and update any consumers that your package’s state has changed
  4. All consumers update themselves and the correct icons are displayed without an action (such as opening a folder) needing to be taken by the user

As a matter of fact, the order of 2 and 3 should be able to be reversed as well (let’s say someone installs a new package that consumes your service after Atom launches):

  1. Atom window loads
  2. atom.grammars.onDid(Add|Update)Grammar event is raised, your package should update its internal state (since there aren’t any consumers yet, no need to update them)
  3. atom.file-icons provider is consumed
  4. Consumer updates itself with the current state of things and displays proper icons

And this can go on and on … with more consumers being loaded and more grammars being added or updated long after Atom launches. To be a well-behaved package, it needs to handle events happening at any time and in any order.


#12

Sorry, can’t find any way to update atom.file-icons consumers if state of my provider changes.

Any way, thanks for your help! I found that I need little bit more than rely on grammars (I don’t think files like json, yml or sql are good candidates for icon-file-code icon).