Sublime Forum

Bug: array parentheses indentation (build 2200)

#1

Upon typing:

$var = array(

Sublime automatically adds the closing parenthesis:

$var = array()

Your cursor will still be between both parentheses.
Now, if you hit enter at this point, Sublime makes:

$var = array( <cursor>)

This is wrong. It should put the closing parenthesis on another new line, at the same indentation as the original line. E.g.:

$var = array( <cursor> )

In other words, it should behave pretty much the same like it does with curly brackets (as for an if-statement for example).
Since I have to make a lot of associative arrays like this, it’s really a pain to have to re-indent it manually every time:

$var = array( 'blah' => 'blah', 'blah' => 'blah', 'blah' => 'blah', 'blah' => 'blah', );

1 Like

#2

Does anyone have a work around to “fix” this? Maybe via a custom plugin?
Is there anyone that knows where to find the code responsible for this behavior?

Since it already works correctly for curly brackets, it shouldn’t be too hard to fix it for regular brackets as well.

0 Likes

#3

If you search for ‘enter’ in your Default Key-Bindings, it’s probably this:

[code] { “keys”: “enter”], “command”: “run_macro_file”, “args”: {“file”: “Packages/Default/Add Line in Braces.sublime-macro”}, “context”:

		{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
		{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
		{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
		{ "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }[/code]

If you copy this section to Key-Bindings User, and modify “\{$” to “\($” and “^\}” to “^\)” it might work.

0 Likes

#4

Thanks, that was useful. I added this code to the user keybindings:

{ "keys": "enter"], "command": "run_macro_file", "args": {"file": "Packages/User/Macros/Add Line in Braces.sublime-macro"}, "context":
		
			{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
			{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
			{ "key": "preceding_text", "operator": "regex_contains", "operand": "array\\($", "match_all": true },
			{ "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
		]
	}

But this still had one flaw: the closing parenthesis “)” was placed one tab too far indented to the right. I am not sure what does this, as it only happens with parentheses, and not with for example “” or square brackets ].

Anyway, I fixed this odd behavior by modifying the “Add Line in Braces” macro (after copying to user folder), changing it to:


    {"command": "insert", "args": {"characters": "\n\n"} },
    {"command": "left_delete", "args": null},
    {"command": "move", "args": {"by": "lines", "forward": false} },
    {"command": "move_to", "args": {"to": "hardeol", "extend": false} },
    {"command": "reindent", "args": {"single_line": true} }
]

I added in the second line, and now it works like a charm.
Thanks again for helping me find this, I hope someone else finds the solution useful as well, or maybe Sublime will implement it in their default packages and keybindings.

0 Likes

#5

Although this may be a solution, it should still be the default. Why no love for php?

0 Likes

#6

bumping again :angry:

Yes for to be making this default behavior

0 Likes

#7

Bump, I would love this feature, I too am a PHP developer, and I am used to this behavior. :frowning:

Hope John notices this and fixes it.

0 Likes

#8

Bump. This is still not fixed in 2217 and really needs to be.

0 Likes

#9

Bump. This issue is so annoying.

0 Likes

#10

I would also like to see this as the default.

0 Likes

#11

I’m on build 3059. The instructions offered above are not working.

0 Likes

#12

Still no available fix? I’m on 3126 and this would be the last thing to code in peace!

The reindent works fine - except for this little thing! Every package out the required a mass of configuration and you have to keep it up2date & it’s slow! Yuck!

Hope this gets fixed!

The behaviour with PHP5.4+ short array syntax is also “wrong”:

$array = array(
    'foo',
    'bar',
    ); // 4 spaces too much before parenthesis

$array = [
'foo', // 4 spaces missing before first single quote
'bar',
];
0 Likes

#13

you could log this at https://github.com/sublimehq/Packages/issues, I suspect it is related to indentParens

0 Likes