Sublime Forum

C++ indenting fails in specific situations

#1

I have a problem with indenting in C++.
The following code gets indented incorrectly:

for (int i=0;
  i<1;
  ++i) {
  int x = 0;
for (int j=0; j<1; ++j) {
  x=i*j;
}
}

There is an unindent after the int x=0; line, which should not happen.
These versions work:

for (int i=0;i<1;++i) {
  int x = 0;
  for (int j=0; j<1; ++j) {
    x=i*j;
  }
}

for (int i=0;
  i<1;
  ++i) 
{
  int x = 0;
  for (int j=0; j<1; ++j) {
    x=i*j;
  }
}

for (int i=0;
  i<1;
  ++i) {int x = 0;
  for (int j=0; j<1; ++j) {
    x=i*j;
  }
}

This is simplified example to show the problem. I manage a large code base where this happens a lot.

Is this a known bug or feature? Could I have some setting wrong to mess this up? I am using version 3.2.2. build 3211.

0 Likes

#2

nope, this seems to be a result of the <key>bracketIndentNextLinePattern</key> in the C++ package’s Indentation Rules.tmPreferences file. Changing it to never match seems to fix this particular scenario - not sure how it would affect the others you showed though, didn’t check.

Not sure if it’s relevant here, but one thing to bear in mind though is that ST’s current indentation engine doesn’t support indenting multiple levels at once, and it’s a historical solution based on TextMate, which makes it a little hard to get it perfect in many situations.

--- Shipped Packages/C++/Indentation Rules.tmPreferences    2020-08-07 14:54:04
+++ Packages/C++/Indentation Rules.tmPreferences    2021-03-06 22:18:49
@@ -20,8 +20,9 @@
 
        <key>bracketIndentNextLinePattern</key>
        <string>(?x)
-       ^ \s* \b(if|while|else)\b [^;]* $
-       | ^ \s* \b(for)\b .* $
+       #^ \s* \b(if|while|else)\b [^;]* $
+       #| ^ \s* \b(for)\b .* $
+       (?!)
        </string>
 
        <key>unIndentedLinePattern</key>
0 Likes

#3

Thanks, that seems to do the trick. Took me a while to work out I had to unzip the exising .sublime-package, change it, zip it and put it back where I found it. Are you able to briefly explain the logic of what is happening here, or point me to relevant documentation so i can teach myself. I can see it is some sort of regular expression processing but can’t completely interpret it.

0 Likes

#4

@kingkeith wrote a guide to indentation and how it works which I linked to below.

This is a Bad Idea :tm: because when Sublime updates itself or when Package Control updates a package, they remove the existing sublime-package file and replace it with a newly updated one. So, if you modify a sublime-package file, your changes will be clobbered away silently on upgrade.

What you want to do is create an override on the package resource instead; overrides remain in place forever, even if the sublime-package file gets updated by an upgrade. It’s a simple process to create one; the video linked below shows how and explains more the why of why you should make modifications that way.

You may also be interested in OverrideAudit (I am the author of the package), which helps you to manage your overrides since they will remain in place forever, which can potentially mask changes that happen during updates.

0 Likes

#5

Thanks again. I’ve also found this page… https://jdhao.github.io/2019/02/28/sublime_text_regex_cheat_sheet/

0 Likes