Help needed with custom grammar -- sometimes it works, sometimes it doesn't


#1

I’m thinking of dropping Sublime and using Atom. One of the things that would prevent me from doing this would be the lack of a custom grammar that I have in Sublime, so I converted it to Atom, and it works great. Until it doesn’t.

I have absolutely no idea why, but sometimes it simply stops working for a specific file. Once it does, I haven’t been able to figure out how to “restart” it again with that file. I can copy and paste the content into a new file, though, and it works fine. The files are identical (I checked with md5sum). And then at some point…the new file stops working.

I’ve tried:

  • many, many restarts of Atom (quit and restart)
  • switching the grammar to plain text and then back to the custom grammar
  • remove all but a handful of lines from the file
  • taking the grammar down to a single, simple/simplistic match
  • checking the Dev Tools console for errors (there were none)

I’m about ready to rip my hair out. I don’t even know how to begin to figure this out. I really, really like Atom, but I can’t use it without this custom grammar. Any help would be greatly appreciated – thanks!


#2

Is it one specific file, or is this happening for multiple files?


#3

Multiple files. And like I said, once it stops working for a given file, I can’t figure out how to get it to start working again. It is completely bizarre.

I forgot to include version info in my original post, sorry:

Atom : 1.23.3
Electron: 1.6.15
Chrome : 56.0.2924.87
Node : 7.4.0

apm 1.18.12
npm 3.10.10
node 6.9.5 x64
atom 1.23.3
python 2.7.10
git 2.15.1


#4

Also, I did try to use safe mode to test this, but I couldn’t then figure out how to load my grammar in safe mode, so that wasn’t helpful. I manually disabled all other extensions (I had three, including my grammar), and that didn’t help.


#5

You can use API calls like this to manually add the active file as a grammar (the file has to be saved first). Note that you have to paste this code into the dev tools console, because safe mode prevents init.coffee from loading.

editor = atom.workspace.getActiveTextEditor();
atom.grammars.loadGrammarSync(editor.getPath());

And the CoffeeScript version, which I keep in my init.coffee for testing grammars without having to reload Atom:

atom.commands.add 'atom-text-editor', 'custom:load-active-editor-as-grammar', ->
    editor = atom.workspace.getActiveTextEditor()
    atom.grammars.loadGrammarSync(editor.getPath())

I’m interested in seeing if it happens with safe mode on, as well.


#6

I tried it in safe mode with no change. I even did a screen cast if you want to see it (quite boring, but you can verify that I’m not doing anything stupid, perhaps):

atom-grammar.mov

Any other ideas? Thank you very much for the help!


#7

Also, I’ve tried the following:

  • replicated it on another machine (also a Mac)
  • validated the CSON file with a lint package, both linter-coffeelint and linter-coffeescript; one said it was OK, the other complained about lines over 80 characters (my regex’s are long, but no line is longer than 145 characters)

#8

There’s something about the particular file in question: it’s very, very long. When Atom sees an especially long file, it chooses to not perform the same highlighting because keeping all of that code highlighted slows Atom way down.


#9

I didn’t realize that was a limitation. In looking into it from that angle, I noticed:

Syntax highlighting conditionally works on super-long HTML file #11232

But that seems to say that the 2 MB limited is no longer there. I did have a single very long line in the file (6000+ characters) but even removing that, it doesn’t work. I also tried removing lines until it was well under 2 MB and that didn’t help. Reverting to the original, I deleted every line > 280 characters (mentioned as another limitation in the above issue) with no luck. My last attempt was to do all of those things: the resulting file was under 2 MB (1.9 MB), with the longest line being 263 characters, and the total # of lines being 15571, still with no syntax highlighting.

Is this a bug that should be reported? Or is this just a known limitation with the current state?

Thanks!


#10

It’s a known limitation. I don’t know what the current limit is, but that’s controlled by the dev team and I imagine has been tweaked over time to figure out the best measure of performance versus attractiveness.

I suspect that if Atom has decided to ignore a file, it has to be prompted to make that decision again. If you delete almost all of the lines in a big file, Atom isn’t going to suddenly start highlighting that file because it has decided that it shouldn’t monitor changes in that file due to the size of the file.


#11

I did quit and restart after every change. Do I need to switch to “plain text” and back again in addition to quitting and restarting?

Thanks for the help with this! I’ll check back with Atom in a few releases.


#12

Atom remembers open files when you close it, as well as many things that it has determined about those files (like how far you’ve scrolled down). I believe that closing and opening a file should do the job. When Atom loads a “new” file below its internal limit, it should behave correctly regardless of whether the file used to be bigger.