Sublime Forum

Coding folding does not work properly

#1

Hi, the addition of code folder is great! I just love it… :smile:

There are, however, some cases which make the folding of functions or if-clases fail.
Problem 1: ST2 does not recognize a folding element.
Problem 1: ST2 does not fold the whole code of a loop/if/for but is ‘interrupted’ by a comment line (or maybe because of intention)

Both problems are illustrated in the attached screenshots. Is that a general problem or related to the language definition (in this case, it’s R)?

Thanks!





1 Like

Dev Build 2136
BracketHighlighter foldbracket failing
Code folding not working
#2

I just confirmed: the indentation seems to be the problem.
I can see that code folding based on indentations works perfectly for Python but it does not for any other language that uses symbols to delineate loops etc. Perfect indentations is not required in these languages and code folding should be based on ‘{’ and ‘}’…

or again, can I solve this with the language definition?

0 Likes

#3

Yeah, it works great in Python, but I was having this problem in C files as well, so I coded up a simple function for use with BracketHighlighter to fold by brackets.

If you happen to use BracketHighlighter, you can create a file called foldbracket.py and drop in this code (I have not yet included it by default in the repo yet)

[code]import bracket_plugin

class fold_bracket(bracket_plugin.BracketPluginCommand):
def run(self, bracket, content, selection):
new_content = [content]
if content.size > 0:
if self.view.fold(content) == False:
new_content = self.view.unfold(content)
self.attr.set_selection(new_content)
[/code]

To map it to a shortcut you can just use this replacing the key sequence with whatever shortcut you would like:

{ "keys": "ctrl+shift+b","ctrl+shift+f"], "command": "bracket_highlighter_key", "args": { "plugin": {"type": "quote", "tag", "bracket"],"command" : "foldbracket.fold_bracket"} } }

But in general I think this should be fixed in the core ST2.

Edit: Removed an unnecessary parameter from the code.

0 Likes

#4

Thanks for posting this. I also hope that it gets addressed in the core ST2 considering that most popular languages don’t use indentation like Python does…

0 Likes

#5

No problem. I hope this gets addressed as well. I personally think folding by indentation should be the exception to the rule. Most languages are not indentation sensitive.

I work for a big cooperation, so a lot of people modify the same code, so we get some weird indentation in many places. It seems there are many people who are not sensitive to indentation as well.

I personally think folding by brackets should be the default with a setting that allows you to keep a list of languages you would like use folding by indentation. We have an internal scripting language that doesn’t really use brackets for function blocks, so I still think that indentation folding is still a needed option, just not the only needed option.

1 Like

#6

I see the same problem in ST3 but your explanatiion about rigth indentation cannot be the reason in my case.
I have correct indentation and I still cannot fold the code.
I can solve the issue by selecting the portion of code and hitting SHIFT+TAB, which will change the overall indentation. Wright after that I can fold the code as expected. But do notice that now I cannot fold the inner code anymore…
See the screenshots:

0 Likes

#7

Proper indentation is expected of every developer and it is frowned upon a developer to not properly indent their code as it just makes it illegible and ugly… However, when making an editor, it is just as bad to assume user-input will always abide by standards set by the community as a whole… One of the first major rules in programming is to never trust user-input… It’s disappointing ST makes this mistake.

I keep getting 500 error on post which is why this response has taken a lot of time…

1 Like