Sublime Forum

Disable syntax-based indentation

#1

I have been struggling with indentation in sublime text for years now. I really just want to disable syntax-based indentation. I even tried creating custom copies of the C/C++ language syntax files and removing all increaseIndent (etc) tags. It uses my custom syntax, but it still indents.

Bad behaviour:

if (inconsistent)
{
    |

then, typing ‘}’ randomly gives either of these:

if (inconsistent)
{
    }|

if (inconsistent)
{
}|

I’ve long ago given up on having this bug fixed. I just want syntax-based indentation disabled. When I edit a plain text file, the indentation behaviour is exactly what I want.

Is there anyway we can get this done?

Many thanks for anyone who can assist.

Reference:

other users complaining about the same thing

0 Likes

#2

This is not a system I’m super familiar with, so tweak as necessary.

I think you can go back to the default system and only override one part, by placing a file named something like Disable C Indentation.tmPreferences in your Packages/User with (nearly) the same content as the original. You want to keep the keys so that they override the existing ones.

Then, for decreaseIndentPattern and increaseIndentPattern, try putting in a regex that cannot match, e.g. $^.

Original C(++) indentation settings:

<?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>
0 Likes

#3

Thank-you for the suggestion. I did have to tweak it a little, but I got it working.

I believe it will override any file in the default packages, provided they have the same name.

I wound up using the PackageResourceViewer and finding the indentation settings file that you posted. I did have to use a regex that would never match, but for some reason, $^ didn’t work. That did not have any effect on the behaviour. I think it took the settings to be an error, then loaded the original ones.

But, ^ (?!x)x $ seemed to work. So, my new file is:

<?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* (public|private|protected): \s* $
        |   ^ \s* @(public|private|protected) \s* $
        </string>
        <key>increaseIndentPattern</key>
        <string>(?x)
        ^ (?!x)x $
        </string>

        <key>bracketIndentNextLinePattern</key>
        <string>(?x)
        ^ (?!x)x $
        </string>

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

        <key>indentSquareBrackets</key>
        <false/>
    </dict>
</dict>
</plist>

and I finally get the indentation behaviour I’ve been wanting.

Thanks again!

-Dave

0 Likes