Sublime Forum

PHP Array indentation

#1

Hello

How/where can I change PHP array indentation settings?

Right now when I start an array and hit enter, it places the closing bracket on the same column as the opening one.
I want to move closing bracket to the beginning of new line.

Thanks!

1 Like

#2

I would also love to know how to do this. auto-indenting works fine when I use curly braces but when I use parentheses with array syntax in PHP I have to manually fix the indents every time. I’m using spaces for indents, 4 spaces per indent.

It ends up looking like this:

function foo() {
    $my_super_long_array = array(
                                ) <-- super far indent
   
}

terms for google searchers: parenthesis, Sublime Text 2, auto indenting

Edit: Possible workaround found by using a snippet.
Create a new file named array(-).sublime-snippet inside /home/username/.config/sublime-text-2/Packages/PHP (on Linux)
Paste this contents, modify as needed:

<snippet> <content><![CDATA[array( 0 => '', 1 => '' );]]></content> <tabTrigger>array</tabTrigger> <scope>source.php - variable.other.php</scope> <description>array (…)</description> </snippet>

Next time you start typing something like $my_array_name = array SublimeText will bring up autocomplete with array selected, hit enter, and it will paste in your array syntax from the snippet, and it will smartly indent it.

0 Likes

#3

Good idea - thanks :smile:

Hopefully this will be fixed in the next release.

0 Likes

#4

Super annoying bug, and it does not seem to be fixed in the latest build.

How does one report bugs to Sublime HQ?

0 Likes

#5

pretty sure this can be fixed yourself in the sublime configuration for regexes buried deep in the package folders, but I just can’t figure out the syntax. It’s almost completely fixed in the latest version (for me) except that the last closing parenthesis is indented 1 too far.

0 Likes

#6

This issue is still bugging me, and like others, I don’t know how to fix the regex. Has anyone done this and can share the regex?

0 Likes

#7

This issue is not fixed, at least not for Mac. In browsing the forum, I see there’s another issue where the end parenthesis stays on the same line as the cursor. That seems to no longer be a problem. But this is:

Incorrect Behavior
Upon creating a new array, this is what I get:

And my cursor is all the way to the right of all those spaces.

Expected Behavior

  1. The cursor should be on the newly created line with one extra indent level from the line above.
  2. The indent should respect my tab/space setting instead of always using spaces to create the space.

0 Likes

#8

are you sure you have indent_to_bracket set to false in your preferences?

0 Likes

#9

Ah, actually I didn’t know about that. I’ve just set indent_to_brackets to false, and now I’m having the same complaint as everyone else, that the closing parenthesis should be on the next line after the cursor, not the same line as the cursor. But at least now I now it’s not some special bug to me. Thanks @kingkeith

0 Likes

#10

probably y’all want something like this in your keybindings:

{ "keys": ["enter"], "command": "insert_snippet", "args": { "contents": "\n\t$0\n" }, "context":
    [
        { "key": "setting.auto_indent", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "selector", "operator": "equal", "operand": "punctuation.section.array.end.php", "match_all": true },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "array\\($", "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true },
    ]
},
0 Likes