Sublime Forum

Strange indentation behavior with golang syntax

#1

Hi all,

With go syntax enabled, when I type something on the end of a } brace, it automatically indents the line over for some reason. For instance, trying to type

func() {
}()

becomes

func() {
    }()

This happens when trying to type anything on the end of a } with a matching {. Sublime console doesn’t show any methods being called. Any thoughts on how to fix this?

0 Likes

#2

you’ll probably want to take a look at the indentation regex rules here:

0 Likes

#3

How do I edit that file? All I can find for the language settings are binary files.

0 Likes

#4

The binary files are probably sublime-package files, which are just renamed zip files that contain the contents of the package. That said, modifying that file is a bad idea because the entire package file will get overwritten the next time Sublime updates.

If you want to edit the file you should make an override, which you can do with PackageResourceViewer. Once it’s installed you can select Package Resource Viewer: Open Resource from the command palette (Ctrl+Shift+P or Shift+⌘+P), then select the Go package and the file you want to modify.

If you modify the file and save it, it will become an override file in your Package directory and take precedence over the version that ships with sublime. Note however that if a future update of sublime modifies that file in it’s shipped package, your version will still override it.

2 Likes

#5

Ho, I need to have a look at this, it does it for python too:

call_function_with_dict({
    |
    })
0 Likes

#6

seems like it could be related to the Packages/Default/Indentation Rules.tmPreferences file - notice how it is much more likely to indent than unindent:

    <key>decreaseIndentPattern</key>
    <string>^(.*\*/)?\s*\}[;\s]*$</string>
    <key>increaseIndentPattern</key>
    <string>^.*(\{[^}"']*)$</string>
1 Like

#7

@kingkeith Are you talking to me? I think so.

What I don’t understand is why this with the [] it works…

call_function_with_dict({
    |
    })

arr = [{
    |
}]

test = ({
    |
    })

Just had a look at the Python’s Miscellaneous.tmPreferences, and I saw the key TM_LINE_TERMINATOR, which is set to :. Do you know what’s the use of it?

In the Packages/Default/Indentation Rules.tmPreferences, here’s the entire content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>scope</key>
	<string>source</string>
	<key>settings</key>
	<dict>
		<key>decreaseIndentPattern</key>
		<string>^(.*\*/)?\s*\}[;\s]*$</string>
		<key>increaseIndentPattern</key>
		<string>^.*(\{[^}"']*)$</string>
		<key>disableIndentNextLinePattern</key>
		<string>^\s*\{[\]})]*\s*$</string>
		<key>indentParens</key>
		<true/>
	</dict>
</dict>
</plist>

Isn’t indentParens an error? Shouldn’t it be indentParents? Or it means something?

0 Likes

#8

default keybindings when you press Enter, I suspect:

I suspect it is just meta info for plugins to make use of

parens is short for parenthesis. I’m not entirely sure what that key value pair affects in reality though.

1 Like

#9

okay so about Go:

in decreaseIndentPattern of Packages/Go/Indentation Rules.tmPreferences, you can:

  1. change the line (?: \) (?&lt;! \( ) ) # closing braces not preceded by opening braces to (?: \}?\) (?&lt;! \( ) ) # closing braces not preceded by opening braces
  2. change the line [;\s]*? # any whitespace or semicolons to [;\s]*?(\(\))? # any whitespace or semicolons

and it will unindent the } as expected and, not re-indent it when you type ) or ()

1 Like

Bracket problem with golang
#10

about Python:

setting indentParens to false in Packages/Default/Indentation Rules.tmPreferences prevents indentation when typing call_function_with_dict({ Enter

The decreaseIndentPattern, increaseIndentPattern and disableIndentNextLinePattern values are ignored in this file, as they are overridden by Packages/Python/Miscellaneous.tmPreferences.

Therefore, one needs to change the decreaseIndentPattern in Packages/Python/Miscellaneous.tmPreferences from ^\s*(elif|else|except|finally)\b.*: to something like ^((\s*(elif|else|except|finally)\b.*:)|\s*[})]+$) to achieve the desired behavior.

2 Likes