Help with a package for folding comments


Hi guys!

I’d very much like to work on a package that can toggle folding or unfolding all comments in a document. I work on a few projects that have extensive inline docs, and when working on the code myself I like to have them folded away to be able to navigate the source more easily.

I’ve spent a little time looking through the API docs, but really am not sure where to start as far as making this happen. I know that you have access to document and can select all elements that have a .comment class on them, which does correctly return everything I need to fold, but it seems like the fold method operates on a selection which requires a Range to set where it is.

Based this, it doesn’t seem like selecting the already comments from the DOM will interop with the machinery for folding them (which is too bad), which I suppose means that you’d need to do a manual text search to discern comments somehow (maybe there’s a way I can get a comment syntax regex from the current syntax highlighter?), build ranges from where they are, then select, fold, and move on to the next one. At the same time this all sounds very difficult.

Can anyone that has a good knowledge of Atom and/or plugins help push me in the right direction of what would be the most efficient way to build a plugin like this?

Folding all except comments

I don’t have much knowledge at all but you should find there’s a method on Editor or TextBuffer that allows you to get a Range (or array of Ranges) from a scope. The scope you’re looking for is obviously ‘comment’. That should get you the Ranges you need to make the folding happen.


Hey! So this is super late, but I still really would like to try out atom, but I need a comment folding package in order to make it viable for me to navigate heavily commented code. It appears as if nobody else has made one still, sadly, so back to the drawing board.

Your tip here helped a lot @alexrussell - thank you! I’ve spent a bit more time with the docs and settled on a rough plan of action as for making this plugin work. It’s rather awkward, but these were the best methods I could find for getting the job done. If there’s a better way to do any of this and anyone has recommendations, that would be wonderful :smile: (gist because I can’t post more than 2 links)


I’ve posted a comment on your Gist that basically explains my thinking, and then I go on to try to get something working. In the end I succeeded, which is something of a bonus, so now all you need to do is wrap it up into a command.

However, I have no idea if the code is actually any good (and/or consistently reliable). I kinda made it up based on the API I could find, but it’s quite possible there’s a couple of methods I’ve missed that could simplify the whole thing right up.

If anyone knows of a way to get either an array of Ranges or Points (start points) for given scope selector for a given document, that would save the line-by-line traversal and basically wrap it all up into a few lines.


So good - thanks so much! Replied with what seems to be a functional solution, yay!


No worries. Glad I could help. It’s certainly fair to say that while Atom’s API is well documented, it’s not overly easy to find everything that it offers. That said, things do seem to be getting better (methods in more places you’d expect, etc.).

It would, however, be great to have a core dev look at what we’ve come up with and suggest different/better methods to use.


Absolutely – do you think there’s any way to do this easily? I put the code in a repo, and am planning on publishing soon, but would love to get it touched up as well :smile:


I guess maybe just see if @ProbablyCorey, @kevinsawicki or @AbeEstrada wouldn’t mind passing their eyes over it.