Skip to content

Commit

Permalink
Fix incorrect indentation behavior apache#6731
Browse files Browse the repository at this point in the history
- apache#6731
- When a multi-line parameter of a method invocation has a string with a variable(e.g. `"example {$example}"`), ignore the variable within the string(i.e. {$example}) to avoid adding extra indentations

Example:
```php
$x = test(
    "test {$test}"
);// enter key here
```

Before:
```php
$x = test(
    "test {$test}"
);
    // one indentation is added
```

After:
```php
$x = test(
    "test {$test}"
);
// no indentation
```
  • Loading branch information
junichi11 committed Nov 22, 2023
1 parent d3ebc8b commit 22ec4c7
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ public static int findStartTokenOfExpression(TokenSequence ts) {
Token token;
int balance = 0;
int curlyBalance = 0;
boolean isInQuotes = false; // GH-6731 for checking a variable in string
do {
token = ts.token();
if (token.id() == PHPTokenId.PHP_TOKEN) {
Expand All @@ -520,6 +521,14 @@ public static int findStartTokenOfExpression(TokenSequence ts) {
default:
//no-op
}
} else if (token.id() == PHPTokenId.PHP_CONSTANT_ENCAPSED_STRING) {
// GH-6731 for checking a variable in string
// e.g. "example {$example}"
if ((token.text().length() == 1 && TokenUtilities.textEquals(token.text(), "\"")) // NOI18N
|| (!TokenUtilities.startsWith(token.text(), "\"") && TokenUtilities.endsWith(token.text(), "\"")) // NOI18N
|| (TokenUtilities.startsWith(token.text(), "\"") && !TokenUtilities.endsWith(token.text(), "\""))) { // NOI18N
isInQuotes = !isInQuotes;
}
} else if ((token.id() == PHPTokenId.PHP_SEMICOLON || token.id() == PHPTokenId.PHP_OPENTAG)
&& ts.moveNext()) {
// we found previous end of expression => find begin of the current.
Expand Down Expand Up @@ -584,7 +593,7 @@ public static int findStartTokenOfExpression(TokenSequence ts) {
break;
} else if (token.id() == PHPTokenId.PHP_CURLY_CLOSE) {
curlyBalance--;
if (curlyBalance == -1 && ts.moveNext()) {
if (!isInQuotes && curlyBalance == -1 && ts.moveNext()) {
// we are after previous blog close
LexUtilities.findNext(ts, Arrays.asList(
PHPTokenId.WHITESPACE,
Expand All @@ -600,7 +609,7 @@ public static int findStartTokenOfExpression(TokenSequence ts) {
}
} else if (token.id() == PHPTokenId.PHP_CURLY_OPEN) {
curlyBalance++;
if (curlyBalance == 1 && ts.moveNext()) {
if (!isInQuotes && curlyBalance == 1 && ts.moveNext()) {
// we are at the begining of a blog
LexUtilities.findNext(ts, Arrays.asList(
PHPTokenId.WHITESPACE,
Expand Down
23 changes: 23 additions & 0 deletions php/php.editor/test/unit/data/testfiles/indent/gh6731_01.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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.
*/

$result = test(
"test {$test}"
);^
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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.
*/

$result = test(
"test {$test}"
);
^
23 changes: 23 additions & 0 deletions php/php.editor/test/unit/data/testfiles/indent/gh6731_02.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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.
*/

$result = test(
"test {$test} test"
);^
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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.
*/

$result = test(
"test {$test} test"
);
^
21 changes: 21 additions & 0 deletions php/php.editor/test/unit/data/testfiles/indent/gh6731_03.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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.
*/

$result = test("test {$test} test");^
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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.
*/

$result = test("test {$test} test");
^
27 changes: 27 additions & 0 deletions php/php.editor/test/unit/data/testfiles/indent/gh6731_04.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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.
*/

$result = test(
"test"
. "test"
. "test"
. "test"
. "test {$test}"
);^
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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.
*/

$result = test(
"test"
. "test"
. "test"
. "test"
. "test {$test}"
);
^
28 changes: 28 additions & 0 deletions php/php.editor/test/unit/data/testfiles/indent/gh6731_05.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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.
*/

class GH6731 {

public function test() {
$result = test(
"test {$test}"
);^
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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.
*/

class GH6731 {

public function test() {
$result = test(
"test {$test}"
);
^
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,26 @@ public void testEnumerationsWithBackingType_03() throws Exception {
testIndentInFile("testfiles/indent/php81/enumerationsWithBackingType_03.php");
}

public void testGH6731_01() throws Exception {
testIndentInFile("testfiles/indent/gh6731_01.php");
}

public void testGH6731_02() throws Exception {
testIndentInFile("testfiles/indent/gh6731_02.php");
}

public void testGH6731_03() throws Exception {
testIndentInFile("testfiles/indent/gh6731_03.php");
}

public void testGH6731_04() throws Exception {
testIndentInFile("testfiles/indent/gh6731_04.php");
}

public void testGH6731_05() throws Exception {
testIndentInFile("testfiles/indent/gh6731_05.php");
}

@Override
protected boolean runInEQ() {
return true;
Expand Down

0 comments on commit 22ec4c7

Please sign in to comment.