Sublime Forum

[Solved] 3114: Auto indent (C syntax) when pressing enter

#1

I noticed this behavior in the latest build (3114), editing “C” files.
In “normal” operation, Enter will go to the beginning of next line. Therefore, for instance, if I write:

#define TEST 123
|

(where | is the position of the cursor after pressing Enter). Now, if this is preceded by a multi-line comment, pressing Enter will not go to the beginning of the line, but indents the cursor, like this:

/* This is a long comment
   which continues on next line
*/
#define TEST 123
    |

Is there a way to avoid this behavior?

0 Likes

#2

Based on some playing around, it would appear that it’s pulling the indent level from the indentation of the last text of the comment text as you have noticed. which definitely seems wrong to me.

Workarounds to defeat this include using C++ style comments instead:

// This is a long comment
// which continues on next line
#define TEST 123
|

or adding an extra line at the end of your comments that makes the indent jump to the first column in this case:

/* This is a long comment
   which continues on next line
.
*/
#define TEST 123
|

However, I am fiercely against tools dictating what my commenting style should look like, so I dug a little deeper.

According to this doc page and the current contents of the indentation rules for C++ (and sister languages like C), a modification to the decreastIndentPattern can be made to tweak this behavior.

What I did was use PackageResourceViewer to open the C++ Indentation Rules.tmPreferences file, and modify the decreaseIndentPattern key as follows, then save the file.

        <key>decreaseIndentPattern</key>
        <string>(?x)
        ^ (.*\*/)? \s* \} .* $
        |   ^ \s* (public|private|protected): \s* $
        |   ^ \s* @(public|private|protected) \s* $
        |   ^ \s* #
        </string>

That last line is what I added, which matches any line that starts with a # character and decreases the indent level, which is (to me) the desired behavior in this particular case.

This creates a file in your Packages/C++ directory that overrides the default settings, so note that if you do this and the default package changes something in this file, you’ll still be overriding it and thus not see those changes.

Note however that I am by no means an expert in how the sublime indenting system works and I didn’t do any super extensive testing, so while this works for fixing this particular issue, I don’t know if it might cause other issues or inconsistencies. For example, if you’re editing code with conditional defines in it, this will (probably) break any auto-indenting in the surrounding code, which is probably something you want to have happen.

1 Like

#3

I’ve fixed this with https://github.com/sublimehq/Packages/commit/3e0faa01dca88b8076bcb3aea879a9d2617d3c66, which will be part of the next dev build.

You can use the instructions in the readme of that repo to try the fix out until the new version ships.

2 Likes

#4

Great, thank you, !!!

0 Likes

#5

I haven’t checked the fix but just a note that in this case indentation should be preserved:

  int i = 0;
#ifdef FOO
  |
0 Likes

#6

I just tested, and it is preserved in that case.

0 Likes

#7

Will, when will realese next bulid

0 Likes