1

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++?

TheLQ
  • 14,830
  • 14
  • 69
  • 107
  • 2
    Use Netbeans http://www.netbeans.com – Petah Feb 03 '11 at 03:08
  • 2
    How the heck is this off topic? IDE questions have long been asked on SO. Just look at the notepad++ tag. – TheLQ Feb 03 '11 at 03:38
  • 2
    @Petah While I love Netbeans there are many times when I need something... lighter. – TheLQ Feb 03 '11 at 03:38
  • what exact formatting are you after? As in your code snippet, or...? – bob-the-destroyer Feb 03 '11 at 05:29
  • @bob-the-destroyer I'm after having Np++ indent this code snippet correctly (The first one is actually what I'm after but Np++ reformats it like the second one). The issue is in large PHP files everything indents correctly except this, preventing me from using it all the time. – TheLQ Feb 03 '11 at 20:46

2 Answers2

1

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)

Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
0

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",
);
?>
Kieran Andrews
  • 5,845
  • 2
  • 33
  • 57
  • That was more of an example than actual code. But I did make the change, no difference. And as I said to @Petah, I use Np++ most of the time since I need something lighter. – TheLQ Feb 03 '11 at 03:39
  • Possibly the reason its lighter is due to the fact it doesn't have a powerful code formatter. I use Coda for most of my development and it has a horrible formatter plugin. – Kieran Andrews Feb 03 '11 at 03:53