This is an automated email from the ASF dual-hosted git repository.

dbalek 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 c2a87c6b52 LSP: Do not autoimport nested classes when not necessary. 
(#4256)
c2a87c6b52 is described below

commit c2a87c6b52ea98f08e5276f3e8897843f1b954c3
Author: Dusan Balek <[email protected]>
AuthorDate: Thu Jun 23 11:02:00 2022 +0200

    LSP: Do not autoimport nested classes when not necessary. (#4256)
---
 .../netbeans/modules/editor/java/JavaCompletionCollector.java  | 10 ++++++++--
 .../java/editor/javadoc/JavadocCompletionCollector.java        |  2 +-
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git 
a/java/java.editor/src/org/netbeans/modules/editor/java/JavaCompletionCollector.java
 
b/java/java.editor/src/org/netbeans/modules/editor/java/JavaCompletionCollector.java
index 0418229640..b6228782c0 100644
--- 
a/java/java.editor/src/org/netbeans/modules/editor/java/JavaCompletionCollector.java
+++ 
b/java/java.editor/src/org/netbeans/modules/editor/java/JavaCompletionCollector.java
@@ -215,13 +215,19 @@ public class JavaCompletionCollector implements 
CompletionCollector {
         }
     }
 
-    public static Supplier<List<TextEdit>> addImport(Document doc, 
ElementHandle<?> handle) {
+    public static Supplier<List<TextEdit>> addImport(Document doc, int offset, 
ElementHandle<?> handle) {
         return () -> {
             try {
                 TextEdit textEdit = 
modify2TextEdit(JavaSource.forDocument(doc), copy -> {
                     copy.toPhase(JavaSource.Phase.RESOLVED);
                     Element e = handle.resolve(copy);
                     if (e != null) {
+                        Scope scope = copy.getTreeUtilities().scopeFor(offset);
+                        for (Element el : 
copy.getElementUtilities().getLocalMembersAndVars(scope, (el, type) -> 
el.getKind().isClass() || el.getKind().isInterface())) {
+                            if (el == e) {
+                                return;
+                            }
+                        }
                         copy.rewrite(copy.getCompilationUnit(), 
GeneratorUtilities.get(copy).addImports(copy.getCompilationUnit(), 
Collections.singleton(e)));
                     }
                 });
@@ -1009,7 +1015,7 @@ public class JavaCompletionCollector implements 
CompletionCollector {
             if (handle != null) {
                 builder.documentation(getDocumentation(doc, offset, handle));
                 if (!addSimpleName && !inImport) {
-                    builder.additionalTextEdits(addImport(doc, handle));
+                    builder.additionalTextEdits(addImport(doc, offset, 
handle));
                 }
             }
             if (isDeprecated) {
diff --git 
a/java/java.editor/src/org/netbeans/modules/java/editor/javadoc/JavadocCompletionCollector.java
 
b/java/java.editor/src/org/netbeans/modules/java/editor/javadoc/JavadocCompletionCollector.java
index 177e5c4a0b..0289b2dcb1 100644
--- 
a/java/java.editor/src/org/netbeans/modules/java/editor/javadoc/JavadocCompletionCollector.java
+++ 
b/java/java.editor/src/org/netbeans/modules/java/editor/javadoc/JavadocCompletionCollector.java
@@ -276,7 +276,7 @@ public class JavadocCompletionCollector implements 
CompletionCollector {
             }
             if (handle != null) {
                 
builder.documentation(JavaCompletionCollector.getDocumentation(doc, offset, 
handle));
-                
builder.additionalTextEdits(JavaCompletionCollector.addImport(doc, handle));
+                
builder.additionalTextEdits(JavaCompletionCollector.addImport(doc, offset, 
handle));
             }
             if (isDeprecated) {
                 builder.addTag(Completion.Tag.Deprecated);


---------------------------------------------------------------------
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