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

5
  • 2
    Use Netbeans netbeans.com Commented Feb 3, 2011 at 3:08
  • 2
    How the heck is this off topic? IDE questions have long been asked on SO. Just look at the notepad++ tag. Commented Feb 3, 2011 at 3:38
  • 2
    @Petah While I love Netbeans there are many times when I need something... lighter. Commented Feb 3, 2011 at 3:38
  • what exact formatting are you after? As in your code snippet, or...? Commented Feb 3, 2011 at 5: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. Commented Feb 3, 2011 at 20:46

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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",
);
?>

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.