Sublime Forum

Auto indentation acting weird (Perl)

#1

When creating an if/else block in Perl, the automatic indentation moves the else block one level too far.

When I type:

if (1) {
}

Then with the cursor just after the closing bracket, I press and then begin typing “else”, the closing bracket moves to the right 1 indentation level instead of staying in place. I then:

if (1) {
    } else {
    }

Instead of getting:

if (1) {
} else {
}

This does not happen in the PHP editor. Is this related to a setting or have I found a bug?

0 Likes

#2

This isn’t happening for me. When I type:

if (1) { and press , I’m left with:

if (1){ | }
where the pipe represents the cursor.

If I try what you described, hitting space after the close brace, the same thing happens but for the else:

if (1) { } else { | }

Perhaps a plugin?

0 Likes

#3

I am suffering from this too. It might be one of my plugins, I’ll report back if I find it.

0 Likes

#4

Tested on ST3 with no plugins on Windows.

I always write out the skeleton of an if-else including the else before I start filling in the parts. This causes indentation on the } else { } part to pop out.

I think this is due to it believing that the close brace is a part of the inside of the if () { expression once we type the “e” in else, if even one character is typed inside the if brackets this does not happen.

0 Likes

#5

is there actually a solution for this? same thing’s happening for me in C# and it drives me crazy.

0 Likes

#6

why not create a snippet so that when you type ife and press Tab, it will automatically enter the if and else with braces in the right places and leave the cursor inside the parenthesis of the if?

0 Likes

#7

So, I don’t know about the original poster, & it seems to be a little different from language to language, but even that wouldn’t work 100%. I don’t always know when I first write the code if I’m going to need an else block. In these cases, sometimes (but not always?) adding the “} el…” will indent the else block, regardless of whether or not there’s code in the if block.

Really, it seems the solution would be to just not auto-indent lines that already have text on them. But I want to keep the rest of the auto-indent & smart-indent behavior, at least for new lines. But for all the indentation preferences I can’t find something like that.

0 Likes

#9

It appears that Perl doesn’t come with language-specific indentation settings like many others do. To fix this issue, I created a Perl-specific version of “Indentation Rules.tmPreferences” with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
	<key>name</key>
	<string>Indentation Rules</string>
	<key>scope</key>
	<string>source.perl</string>
	<key>settings</key>
	<dict>
		<key>decreaseIndentPattern</key>
		<string>^(.*\*/)?\s*\}.*$</string>
	</dict>
</dict>
</plist>

The key difference compared to the default decreaseIndentPattern is the “.” before the $ anchor. The file path on my install is C:\Users\<user>\AppData\Roaming\Sublime Text 3\Packages\Perl\Indentation Rules.tmPreferences.

0 Likes

#10

You should make a pull request !

0 Likes