Skip to content

Commit

Permalink
Fix the formatting for the method call arguments alignment option apa…
Browse files Browse the repository at this point in the history
…che#6714

- apache#6714
- Consider the TAB size when a column size is got

Example:
```php
array_merge(
	$x,
	$y,
);
```

Before:

```php
array_merge(
	$x,
 $y,
);
```

After:

```php
array_merge(
        $x,
        $y,
);
```

- Keep the last anchor to a stack when a method calls are nested

Example:

```php
nestedCall(
	something(
				$arg1,
			$arg2,
		C::something(
					$x,
					$y,
				$z,
		)
	),
			$y,
		$z,
);
```

Before:

```php
nestedCall(
	something(
		$arg1,
  $arg2,
  C::something(
			$x,
   $y,
   $z,
		)
	),
   $y,
   $z,
);
```

After:

```php
nestedCall(
	something(
		$arg1,
		$arg2,
		C::something(
			$x,
			$y,
			$z,
		)
	),
	$y,
	$z,
);
```

- Add unit tests
  • Loading branch information
junichi11 committed Dec 24, 2023
1 parent 2141d37 commit 5f687eb
Show file tree
Hide file tree
Showing 12 changed files with 965 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.netbeans.modules.csl.spi.GsfUtilities;
import org.netbeans.modules.csl.spi.ParserResult;
import org.netbeans.modules.editor.indent.spi.Context;
import org.netbeans.modules.php.editor.CodeUtils;
import org.netbeans.modules.php.editor.lexer.LexUtilities;
import org.netbeans.modules.php.editor.lexer.PHPTokenId;
import org.netbeans.modules.php.editor.parser.PHPParseResult;
Expand Down Expand Up @@ -455,7 +456,9 @@ public void run() {
int extraLines;
int column = 0;
int indentOfOpenTag = 0;
int methodCallParenBalance = 0; // GH-6714 for nested arguments
final Deque<Integer> lastBracedBlockIndent = new ArrayDeque<>();
final Deque<FormatToken.AnchorToken> lastAnchorTokenStack = new ArrayDeque<>(); // GH-6714 for nested arguments

FormatToken formatToken;
String newText = null;
Expand Down Expand Up @@ -1455,12 +1458,17 @@ && countOfNewLines(formatTokens.get(index + 1).getOldText()) > 0) {
}
// NETBEANS-3391
if (isLeftParen(formatTokens.get(index - 1))) {
methodCallParenBalance++;
if (hasNewLineWithinParensForward(index, formatTokens, formatToken.getId())
&& docOptions.wrapMethodCallArgsAfterLeftParen) {
indentLine = true;
newLines = 1;
}
} else {
methodCallParenBalance--;
if (methodCallParenBalance > 0 && !lastAnchorTokenStack.isEmpty()) {
lastAnchor = lastAnchorTokenStack.pop();
}
if (hasNewLineWithinParensBackward(index, formatTokens, formatToken.getId())
&& docOptions.wrapMethodCallArgsRightParen) {
indentLine = true;
Expand Down Expand Up @@ -1741,6 +1749,9 @@ && countOfNewLines(formatTokens.get(index + 1).getOldText()) > 0) {
lastPHPIndent += indentDelta;
break;
case ANCHOR:
if (methodCallParenBalance > 0 && lastAnchor != null) {
lastAnchorTokenStack.push(lastAnchor);
}
lastAnchor = (FormatToken.AnchorToken) formatToken;
lastAnchor.setAnchorColumn(column + 1);
break;
Expand Down Expand Up @@ -2156,6 +2167,9 @@ && isAfterLineComment(formatTokens, index - 2)) {
}
break;
case ANCHOR:
if (methodCallParenBalance > 0 && lastAnchor != null) {
lastAnchorTokenStack.push(lastAnchor);
}
lastAnchor = (FormatToken.AnchorToken) formatToken;
lastAnchor.setAnchorColumn(column);
break;
Expand Down Expand Up @@ -2221,22 +2235,25 @@ && isAfterLineComment(formatTokens, index - 2)) {
}

delta = replaceString(doc, changeOffset, index, oldText, newText, delta, templateEdit);
// GH-6714 if text have TABs, get incorrect column
// so, use countOfSpaces() instead of newText.length()
if (newText == null) {
String formatTokenOldText = formatToken.getOldText() == null ? "" : formatToken.getOldText();
int formatTokenOldTextLength = formatTokenOldText.length();
String formatTokenOldText = formatToken.getOldText() == null ? CodeUtils.EMPTY_STRING : formatToken.getOldText();
int formatTokenOldTextLength = countOfSpaces(formatTokenOldText, docOptions.tabSize);
int lines = countOfNewLines(formatTokenOldText);
if (lines > 0) {
int lastNewLine = formatTokenOldText.lastIndexOf('\n'); //NOI18N
column = formatTokenOldText.substring(lastNewLine).length();
int lastNewLine = formatTokenOldText.lastIndexOf(CodeUtils.NEW_LINE);
String substring = formatTokenOldText.substring(lastNewLine);
column = countOfSpaces(substring, docOptions.tabSize);
} else {
column += formatTokenOldTextLength;
}
} else {
int lines = countOfNewLines(newText);
if (lines > 0) {
column = newText.length() - lines;
column = countOfSpaces(newText, docOptions.tabSize) - lines;
} else {
column += newText.length();
column += countOfSpaces(newText, docOptions.tabSize);
}
}
newText = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace NS\GH6714;

use Vendor\Package\TestClass1;
use function Vendor\Package\ {
function1,
function2,
};
use function Vendor\Package\ {
function3,
function4,
};
use const Vendor\Package\CONSTANT1;

class GH6714 implements Interface1,
Interface2,
Interface3
{
public array $x = [];
public array $y = [];

public function test1()
{
return something1(
$this->x,
$this->y,
);
}

public function test2(
$param1,
$param2,
$param3,
)
{
nestedCall(
$this->testMethod($arg1),
$this->x,
$this->y,
);
return nestedCall(
$this->testMethod($arg1),
$this->x,
$this->y,
);
}
}

array_merge(
$x,
$y,
);

nestedCall(
something(
$arg1,
$arg2,
C::something(
$x,
$y,
$z,
)
),
$y,
$z,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

namespace NS\GH6714;

use Vendor\Package\TestClass1;
use function Vendor\Package\{
function1,
function2,
};
use function Vendor\Package\{
function3,
function4,
};
use const Vendor\Package\CONSTANT1;

class GH6714 implements Interface1,
Interface2,
Interface3 {

public array $x = [];
public array $y = [];

public function test1() {
return something1(
$this->x,
$this->y,
);
}

public function test2(
$param1,
$param2,
$param3,
) {
nestedCall(
$this->testMethod($arg1),
$this->x,
$this->y,
);
return nestedCall(
$this->testMethod($arg1),
$this->x,
$this->y,
);
}
}

array_merge(
$x,
$y,
);

nestedCall(
something(
$arg1,
$arg2,
C::something(
$x,
$y,
$z,
)
),
$y,
$z,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

namespace NS\GH6714;

use Vendor\Package\TestClass1;
use function Vendor\Package\{
function1,
function2,
};
use function Vendor\Package\{
function3,
function4,
};
use const Vendor\Package\CONSTANT1;

class GH6714 implements Interface1,
Interface2,
Interface3 {

public array $x = [];
public array $y = [];

public function test1() {
return something1(
$this->x,
$this->y,
);
}

public function test2(
$param1,
$param2,
$param3,
) {
nestedCall(
$this->testMethod($arg1),
$this->x,
$this->y,
);
return nestedCall(
$this->testMethod($arg1),
$this->x,
$this->y,
);
}
}

array_merge(
$x,
$y,
);

nestedCall(
something(
$arg1,
$arg2,
C::something(
$x,
$y,
$z,
)
),
$y,
$z,
);

0 comments on commit 5f687eb

Please sign in to comment.