This is an automated email from the ASF dual-hosted git repository. junichi11 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new 26ab50f13b Don't remove existing uses if they are the same as the created string for use statements new f6df5f3323 Merge pull request #6179 from junichi11/php-gh-5681-reimplement 26ab50f13b is described below commit 26ab50f13b7c223f6e8cfa6592961f9f57551055 Author: Junichi Yamamoto <junich...@apache.org> AuthorDate: Wed Jul 12 00:20:50 2023 +0900 Don't remove existing uses if they are the same as the created string for use statements - https://github.com/apache/netbeans/pull/5681 - Avoid changing the file state if the created string is the same as the existing use statements --- .../php/editor/actions/FixUsesPerformer.java | 37 +++++++++++++++++----- .../01/testIssue210093_01.php.fixUses | 4 +-- .../actions/testNoChanges/01/testNoChanges_01.php | 37 ++++++++++++++++++++++ .../testNoChanges/01/testNoChanges_01.php.fixUses | 37 ++++++++++++++++++++++ .../php/editor/actions/FixUsesPerformerTest.java | 7 ++++ 5 files changed, 112 insertions(+), 10 deletions(-) diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/actions/FixUsesPerformer.java b/php/php.editor/src/org/netbeans/modules/php/editor/actions/FixUsesPerformer.java index 678f6e372c..f294b47160 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/actions/FixUsesPerformer.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/actions/FixUsesPerformer.java @@ -114,7 +114,6 @@ public class FixUsesPerformer { if (document instanceof BaseDocument) { baseDocument = (BaseDocument) document; editList = new EditList(baseDocument); - processExistingUses(); processSelections(); editList.apply(); } @@ -157,10 +156,23 @@ public class FixUsesPerformer { } 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); } } @@ -493,13 +505,22 @@ public class FixUsesPerformer { 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 = EMPTY_STRING; + 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); } diff --git a/php/php.editor/test/unit/data/testfiles/actions/testIssue210093/01/testIssue210093_01.php.fixUses b/php/php.editor/test/unit/data/testfiles/actions/testIssue210093/01/testIssue210093_01.php.fixUses index c64e8084be..2eff6f58a1 100644 --- a/php/php.editor/test/unit/data/testfiles/actions/testIssue210093/01/testIssue210093_01.php.fixUses +++ b/php/php.editor/test/unit/data/testfiles/actions/testIssue210093/01/testIssue210093_01.php.fixUses @@ -8,11 +8,11 @@ namespace Issue\Martin { namespace { -use \Issue\Martin\Pondeli; + use \Issue\Martin\Pondeli; function testOk(Pondeli $param) {} function testFail(Pondeli $param) {} } -?> \ No newline at end of file +?> diff --git a/php/php.editor/test/unit/data/testfiles/actions/testNoChanges/01/testNoChanges_01.php b/php/php.editor/test/unit/data/testfiles/actions/testNoChanges/01/testNoChanges_01.php new file mode 100644 index 0000000000..dd568e55c9 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/actions/testNoChanges/01/testNoChanges_01.php @@ -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 {} +} diff --git a/php/php.editor/test/unit/data/testfiles/actions/testNoChanges/01/testNoChanges_01.php.fixUses b/php/php.editor/test/unit/data/testfiles/actions/testNoChanges/01/testNoChanges_01.php.fixUses new file mode 100644 index 0000000000..55521bed24 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/actions/testNoChanges/01/testNoChanges_01.php.fixUses @@ -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 {} +} diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/actions/FixUsesPerformerTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/actions/FixUsesPerformerTest.java index 0e89b69e31..c6801bbadb 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/actions/FixUsesPerformerTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/actions/FixUsesPerformerTest.java @@ -641,6 +641,13 @@ public void testGH4609PSR12_GroupUses() 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); --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists