Skip to content

Commit

Permalink
Don't remove existing uses if they are the same as the created string…
Browse files Browse the repository at this point in the history
… for use statements

- apache#5681
- Avoid changing the file state if the created string is the same as the
  existing use statements
  • Loading branch information
junichi11 committed Jul 11, 2023
1 parent 691f0b4 commit c762224
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public void perform() {
if (document instanceof BaseDocument) {
baseDocument = (BaseDocument) document;
editList = new EditList(baseDocument);
processExistingUses();
processSelections();
editList.apply();
}
Expand Down Expand Up @@ -157,10 +156,23 @@ private void processSelections() {
}
replaceUnimportedItems();
String insertString = createInsertString(useParts);
insertUses(startOffset, insertString);
}

private void insertUses(int startOffset, String insertString) {
ExistingUseStatementVisitor visitor = new ExistingUseStatementVisitor();
Program program = parserResult.getProgram();
if (program != null) {
program.accept(visitor);
}
List<OffsetRange> usedRanges = visitor.getUsedRanges();
String existingUses = getExistingUses(usedRanges);
// avoid being recognized as a modified file
if (insertString.isEmpty()) {
if (insertString.isEmpty()
|| existingUses.equals(insertString.trim())) {
StatusDisplayer.getDefault().setStatusText(Bundle.FixUsesPerformer_noChanges());
} else {
processExistingUses(usedRanges);
editList.replace(startOffset, 0, insertString, false, 0);
}
}
Expand Down Expand Up @@ -493,13 +505,22 @@ private String createStringForCommonUse(List<UsePart> useParts) {
return result.toString();
}

private void processExistingUses() {
ExistingUseStatementVisitor visitor = new ExistingUseStatementVisitor();
Program program = parserResult.getProgram();
if (program != null) {
program.accept(visitor);
private String getExistingUses(List<OffsetRange> usedRanges) {
String existingUses = ""; // NOI18N
if (!usedRanges.isEmpty()) {
int start = usedRanges.get(0).getStart();
int end = usedRanges.get(usedRanges.size() - 1).getEnd();
try {
existingUses = baseDocument.getText(start, end - start);
} catch (BadLocationException ex) {
LOGGER.log(Level.WARNING, "Invalid offset: {0}", ex.offsetRequested()); // NOI18N
}
}
for (OffsetRange offsetRange : visitor.getUsedRanges()) {
return existingUses;
}

private void processExistingUses(List<OffsetRange> usedRanges) {
for (OffsetRange offsetRange : usedRanges) {
int startOffset = getOffsetWithoutLeadingWhitespaces(offsetRange.getStart());
editList.replace(startOffset, offsetRange.getEnd() - startOffset, EMPTY_STRING, false, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace Issue\Martin {

namespace {

use \Issue\Martin\Pondeli;
use \Issue\Martin\Pondeli;

function testOk(Pondeli $param) {}

function testFail(Pondeli $param) {}

}
?>
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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 {

use NS1\TestClass;

class Test {
public function create(): TestClass {
return new TestClass();
}

public function something(\NS1\TestClass $testClass): TestClass {
return $testClass;
}
}
}

namespace NS1 {
class TestClass {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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 {

use NS1\TestClass;

class Test {
public function create(): TestClass {
return new TestClass();
}

public function something(TestClass $testClass): TestClass {
return $testClass;
}
}
}

namespace NS1 {
class TestClass {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,13 @@ public void testGH5578_declare05() throws Exception {
performTest("class DeclareTest1 ^{", selections, true, options);
}

public void testNoChanges_01() throws Exception {
List<Selection> selections = new ArrayList<>();
selections.add(new Selection("\\NS1\\TestClass", ItemVariant.Type.CLASS));
Options options = new Options.Builder(PhpVersion.PHP_81).build();
performTest(" class ^Test {", selections, true, options);
}

private String getTestResult(final String fileName, final String caretLine, final List<Selection> selections, final boolean removeUnusedUses, final Options options) throws Exception {
FileObject testFile = getTestFile(fileName);

Expand Down

0 comments on commit c762224

Please sign in to comment.