TheLQ
TheLQ

Reputation: 15008

Autoindent PHP Array in Notepad++?

I'm currently having difficulty with auto-indenting PHP arrays that span multiple lines. The standard TextFX > TextFX Edit > Reindent C++ Code fails here.

For example, take the following code snippet:

<?php
$something = array(
    "test" => $var,
    "nested" => array(
        "subnest" = array(
            "low" => "yes",
            "foo" => "bar",
            ),
        "bar" => "baz",
        ),
    "last" => "yes",
    );

Run "Reindent C++ Code" and get this:

<?php
$something = array(
"test" => $var,
"nested" => array(
"subnest" = array(
"low" => "yes",
"foo" => "bar",
),
"bar" => "baz",
),
"last" => "yes",
);

Not really what I was after.

Is there any tool I'm missing or plugin that can properly indent PHP arrays in Notepad++?

Upvotes: 1

Views: 4344

Answers (2)

Dan Lugg
Dan Lugg

Reputation: 20592

Unfortunately, still (at the time of this writing) Notepad++ doesn't have support for code indentation formatting of anything other than curly brace {} blocks, in PHP and most other languages it supports.

switch is another one:

switch ($value) {
    case 1:
        foo();
        break;
    case 2:
        bar();
        break;
    case 3:
        qux();
        break;
}

Becomes:

switch ($value) {
    case 1:
    foo();
    break;
    case 2:
    bar();
    break;
    case 3:
    qux();
    break;
}

The solution I've found (at least with PHP) is to use curly braces for formatting, as they are syntactically valid yet don't change the program structure:

switch ($value) {
    case 1: {
        foo();
        break;
    }
    case 2: {
        bar();
        break;
    }
    case 3: {
        qux();
        break;
    }
}

This has the added bonus of allowing you to code-fold arbitrary blocks of your script.

Unfortunately as you've discovered, square [] and round () brackets aren't recognized by the formatter, and arrays wouldn't be a syntactically valid case for curly brace wrapping.

Short answer being; sorry, I've tried exhaustively too, you'll need to find/write a plugin (I haven't; I just live with it)

Upvotes: 1

Kieran Andrews
Kieran Andrews

Reputation: 5885

There was an error in your code - that could have been causing it. Netbeans showed me this error and then I fixed it.

Try changing this line:

"nested" = array(

to

"nested" => array(

and see how Notepad++ handles it.

I used netbeans for this, even if I am writing in another application, I copy and paste it to netbeans to tidy it up.

Netbeans

Netbeans returns:

<?php

$something = array(
    "test" => $var,
    "nested" => array(
        "subnest" => array(
            "low" => "yes",
            "foo" => "bar",
        ), "bar" => "baz",),
    "last" => "yes",
);
?>

Upvotes: 0

Related Questions