junichi11 commented on code in PR #5681:
URL: https://github.com/apache/netbeans/pull/5681#discussion_r1142814745


##########
php/php.editor/src/org/netbeans/modules/php/editor/actions/FixUsesPerformer.java:
##########
@@ -107,51 +107,192 @@ public void perform() {
         if (document instanceof BaseDocument) {
             baseDocument = (BaseDocument) document;
             editList = new EditList(baseDocument);
-            namespaceScope = ModelUtils.getNamespaceScope(parserResult, 
importData.caretPosition);
+            namespaceScope = 
ModelUtils.getNamespaceScope(parserResult.getModel().getFileScope(), 
importData.caretPosition);
             assert namespaceScope != null;
-            processSelections(processExistingUses(importData.caretPosition));
+            processSelections();
             editList.apply();
         }
     }
 
     @NbBundle.Messages("FixUsesPerformer.noChanges=Fix imports: No Changes")
-    private void processSelections(int startOffset) {
+    private void processSelections() {
         final List<ImportData.DataItem> dataItems = 
resolveDuplicateSelections();
-        List<UsePart> useParts = new ArrayList<>();
-        Collection<? extends GroupUseScope> declaredGroupUses = 
namespaceScope.getDeclaredGroupUses();
-        for (GroupUseScope groupUseElement : declaredGroupUses) {
-            for (UseScope useElement : groupUseElement.getUseScopes()) {
-                processUseElement(useElement, useParts);
-            }
-        }
-        Collection<? extends UseScope> declaredUses = 
namespaceScope.getDeclaredSingleUses();
-        for (UseScope useElement : declaredUses) {
-            assert !useElement.isPartOfGroupUse() : useElement;
-            processUseElement(useElement, useParts);
-        }
+        TreeMap<Integer, List<UsePart>> usePartsMap = new TreeMap<>();
+        
         for (int i = 0; i < selections.size(); i++) {
             ItemVariant itemVariant = selections.get(i);
-            if (itemVariant.canBeUsed()) {
+            // we shouldn't use itemVariant if there is no any real dataItem 
related to it
+            if (itemVariant.canBeUsed() && 
!dataItems.get(i).getUsedNamespaceNames().isEmpty()) {
+                NamespaceScope currentScope = 
dataItems.get(i).getUsedNamespaceNames().get(0).getInScope();
+                int mapKey = currentScope.getBlockRange().getStart();
+                if (usePartsMap.get(mapKey) == null) {
+                    usePartsMap.put(mapKey, 
processScopeDeclaredUses(currentScope, new ArrayList<>()));
+                }
+                
                 SanitizedUse sanitizedUse = new SanitizedUse(
                         new UsePart(modifyUseName(itemVariant.getName()), 
UsePart.Type.create(itemVariant.getType()), itemVariant.isFromAliasedElement()),
-                        useParts,
-                        createAliasStrategy(i, useParts, selections));
+                        usePartsMap.get(mapKey),
+                        createAliasStrategy(i, usePartsMap.get(mapKey), 
selections));
                 if (sanitizedUse.shouldBeUsed()) {
-                    useParts.add(sanitizedUse.getSanitizedUsePart());
+                    
usePartsMap.get(mapKey).add(sanitizedUse.getSanitizedUsePart());
                 }
                 for (UsedNamespaceName usedNamespaceName : 
dataItems.get(i).getUsedNamespaceNames()) {
                     editList.replace(usedNamespaceName.getOffset(), 
usedNamespaceName.getReplaceLength(), 
sanitizedUse.getReplaceName(usedNamespaceName), false, 0);
                 }
             }
         }
         replaceUnimportedItems();
-        String insertString = createInsertString(useParts, 
shouldAppendLineAfter(startOffset));
-        // avoid being recognized as a modified file
-        if (insertString.isEmpty()) {
+        
+        CheckVisitor visitor = new CheckVisitor();
+        Program program = parserResult.getProgram();
+        if (program != null) {
+            program.accept(visitor);
+        }
+        
+        boolean emptyString = true;
+        int lastUsedRangeIndex = 0;
+        int lastDeclareIndex = 0;
+        List<NamespaceScope> declaredNamespaces;
+        if (namespaceScope.isDefaultNamespace()) {
+            declaredNamespaces = new 
ArrayList(namespaceScope.getFileScope().getDeclaredNamespaces());
+            declaredNamespaces.sort((left, right) -> 
left.getBlockRange().getStart() - right.getBlockRange().getStart());
+        } else {
+            declaredNamespaces = new ArrayList();
+            declaredNamespaces.add(namespaceScope);
+        }
+        for (NamespaceScope currentScope : declaredNamespaces) {
+            int mapKey = currentScope.getBlockRange().getStart();
+            if (usePartsMap.get(mapKey) == null) {
+                usePartsMap.put(mapKey, processScopeDeclaredUses(currentScope, 
new ArrayList<>()));
+            }
+            
+            int startOffset = getNamespaceScopeOffset(currentScope);
+            int endOffset = currentScope.isDefaultNamespace() && 
visitor.getGlobalNamespaceEndOffset() > 0
+                    ? visitor.getGlobalNamespaceEndOffset()
+                    : currentScope.getBlockRange().getEnd();
+            
+            int compareStringOffsetStart = 0;
+            List <OffsetRange> replace = new ArrayList();
+            for (int i = lastUsedRangeIndex; i < 
visitor.getUsedRanges().size(); i++) {
+                OffsetRange offsetRange = visitor.getUsedRanges().get(i);
+                if (endOffset < offsetRange.getStart()) {
+                    break;
+                }
+                lastUsedRangeIndex = i;
+                if (startOffset > offsetRange.getStart()) {
+                    continue;
+                }
+                
+                compareStringOffsetStart = compareStringOffsetStart > 0 ? 
compareStringOffsetStart : offsetRange.getStart();
+                int useStartOffset = 
getOffsetWithoutLeadingWhitespaces(offsetRange.getStart());
+                replace.add(new OffsetRange(useStartOffset, 
offsetRange.getEnd()));
+                startOffset = offsetRange.getEnd();
+            }
+            
+            // because only declare(strict_types=1) should go first, but 
declare(ticks=1) could be everywhere
+            // we have to restrict declare start offset to prevent wrong USE 
placement after declare in cases such
+            // function { declare(ticks=1); } or class A { puclic function fn 
() { declare(ticks=1); } }
+            // in the feature it would be better to have DeclareStatmentScope 
for that       

Review Comment:
   Please remove trailing spaces.
   
   I suggest trailing whitespaces are highlighted.
   
![Options_001](https://user-images.githubusercontent.com/738383/226498293-add08c5d-baa7-4f4f-a91e-dd22ad64692a.png)
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to