Sublime Forum

How to fix C++ unindent on access modifiers?

#1

Hello when I am programming in C++ I notice that after I type an access modifier (e.g. private or public) and I press the colon key, the line that has the access modifier is unindented. For example I type:

namespace cardGameAPI {

    class Card {

	    public

    }

}

When I type the colon key to adhere to the syntax of C++, the code formatting automatically changes to:

namespace cardGameAPI {

    class Card {

    public:

    }

}

Is there any way to stop this from happening?

I have read a solution for Sublime Text 2 on Stack Overflow but I cannot find a solution for Sublime Text 3.

0 Likes

#2

The access modifier are part of the normal indent/unindent chain like braces. Means ST auto-indents the next function declaration afterwards and therefore unindents the next access modifier keyword to ensure them to be one level less indented, then the declarations.

If you want to indent the access modifiers by 1 you’d need to indent the function declaration in front of it by 2 levels.


class Card {
        
        void _read();

    public:
        Card();
        ~Card();
}

You can modify the indention rules to exclude the access modifiers, but this results in


class Card {
        
    void _read();

    public:
    Card();
    ~Card();
}

To do so, you’d need to create a Pakages/C++/Indention Rules.tmPreferences as override for the default indention rules of the builtin C++ package with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
	<key>name</key>
	<string>Indentation Rules</string>
	<key>scope</key>
	<string>source.c, source.c++, source.objc, source.objc++</string>
	<key>settings</key>
	<dict>
		<key>decreaseIndentPattern</key>
		<string>(?x)
		^ (.*\*/)? \s* \} .* $
		# |   ^ \s* (public|private|protected): \s* $
		|   ^ \s* @(public|private|protected) \s* $
		</string>
		<key>increaseIndentPattern</key>
		<string>(?x)
		^ .* \{ [^}"']* $
		# |   ^ \s* (public|private|protected): \s* $
		|   ^ \s* @(public|private|protected) \s* $
		</string>

		<key>bracketIndentNextLinePattern</key>
		<string>(?x)
		^ \s* \b(if|while|else)\b [^;]* $
		| ^ \s* \b(for)\b .* $
		</string>

		<key>unIndentedLinePattern</key>
		<string><![CDATA[^\s*((/\*|.*\*/|//|#|template\b.*?>(?!\(.*\))|@protocol|@interface(?!.*\{)|@implementation|@end).*)?$]]></string>

		<key>indentSquareBrackets</key>
		<true/>
	</dict>
</dict>
</plist>

Note: The code block shows a copy of the builtin C++ indention rules with (public|private|protected) commented out.

0 Likes

#3

Is there any way to simply stop it from moving backwards while still allowing the following lines to autoindent?

0 Likes

#4

Does Sublime’s "smart_indent" setting control whether the stuff in Indention Rules.tmPreferences is respected?

I thought that was the case, but IIRC the last time I tried, they didn’t seem to be directly connected.

0 Likes

#5

Yes, by keeping the second line of the increaseIndentPattern commented in.

   	<key>increaseIndentPattern</key>
	<string>(?x)
	^ .* \{ [^}"']* $
	|   ^ \s* (public|private|protected): \s* $
	|   ^ \s* @(public|private|protected) \s* $
	</string>
0 Likes