How to modify javascript grammar to support indent-level folding?


#1

I’d love to switch from Sublime to Atom, but unfortunately I’m missing one essential feature. Consider the following javascript code:

// This is section A

     var f1 = function() { }
     var f2 = function() { }

// This is section B
     
     var f3 = function() { }
     var f4 = function() { }

Sublime’s javascript grammar automatically gives me “fold points” at the section level, so that I can easily focus on an individual section of code. Alas, Atom’s grammar does not. Is this a relatively easy fix?

Any help will be greatly appreciated.

-Z


How does code folding work in Atom?
#2

Sadly, I don’t believe marker based folding has been implemented yet. See first-mate issue #41.


#3

Is marker based folding necessary for this? I’m confused, because indent-level folding seems to work fine for plain text files…


#4

Sublime Text grammars offer sectional based folding by allowing the grammar author to write rules for placing hidden fold markers. So what you were describing, if I understood it correctly, was marker based folding. Only you weren’t having to place the markers yourself. The grammar was doing that for you behind the scenes. In any case, if your desire is for the Atom Javascript grammar to automatically do sectional based folding then that is not currently possible. It should be possible at some point in the future though. For now there is only indent based folding.


#5

I guess your last line: “For now there is only indent based folding” leaves me a little confused. What I’m looking for is, actually, just indent based folding. In my case, sections are defined strictly by indentation. But for whatever reason, indent-based folding gets disabled when you switch Atom’s grammar from plain text to javascript. Once you are in javascript mode, Atom only wants to fold on things like {curly braces}. Make sense?


#6

I don’t use the javascript grammar often but testing some sample code just now is producing the result you’re describing. Here is some javascript code I pulled from github. As you can see there are indeed folds before indented sections. Now these ones are following { as you first discribed

However if I indent a line that does not end in a { I get the same effect as shown below

Have you tried disabling all installed packages?


#7

I appreciate your continuing interest and help. It seems that indents do, in fact, produce fold points, as long as the indentation is inside code. If the “anchor” of the indentation is a comment, it doesn’t work. See the attached screenshot:


I expected that I would get a fold point on line 1, allowing me to “swallow” lines 2 through 6. Would it be difficult to modify the javascript grammar to support this?


#8

No problem @zzaacchh. My area of expertise in this is with the Fortran language grammar. I just checked and it produces the same behavior that you’re seeing in javascript where folding seemingly ignores comment lines (I feel this must have been introduced recently because I don’t remember it doing this several months ago). That said, having written nearly the entire Fortran grammar, I don’t know how I could go about changing this behavior other than scoping comment lines as something other than comments (this would likely cause numerous other problems with your color-scheme). There may be a way around this but I don’t know how to do it. Maybe someone else on these forums can help.