From c7622247d63d0438977501f3c923bb26c0ddd37e Mon Sep 17 00:00:00 2001 From: Junichi Yamamoto Date: Wed, 12 Jul 2023 00:20:50 +0900 Subject: [PATCH] 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 +- .../testNoChanges/01/testNoChanges_01.php | 37 +++++++++++++++++++ .../01/testNoChanges_01.php.fixUses | 37 +++++++++++++++++++ .../editor/actions/FixUsesPerformerTest.java | 7 ++++ 5 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 php/php.editor/test/unit/data/testfiles/actions/testNoChanges/01/testNoChanges_01.php create mode 100644 php/php.editor/test/unit/data/testfiles/actions/testNoChanges/01/testNoChanges_01.php.fixUses 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 678f6e372c6c..3cd8056cec4b 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 void perform() { if (document instanceof BaseDocument) { baseDocument = (BaseDocument) document; editList = new EditList(baseDocument); - processExistingUses(); processSelections(); editList.apply(); } @@ -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 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 @@ private String createStringForCommonUse(List 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 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 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 c64e8084be42..2eff6f58a14d 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 000000000000..dd568e55c95c --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/actions/testNoChanges/01/testNoChanges_01.php @@ -0,0 +1,37 @@ + 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 selections, final boolean removeUnusedUses, final Options options) throws Exception { FileObject testFile = getTestFile(fileName);