You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by db...@apache.org on 2022/06/29 10:21:21 UTC
[netbeans] branch master updated: LSP: Auto import of types in code completion fixed. (#4291)
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 4792fcf7cd LSP: Auto import of types in code completion fixed. (#4291)
4792fcf7cd is described below
commit 4792fcf7cd6bb4c18d460a1aabd761f981c1eab2
Author: Dusan Balek <du...@oracle.com>
AuthorDate: Wed Jun 29 12:21:14 2022 +0200
LSP: Auto import of types in code completion fixed. (#4291)
---
.../editor/java/JavaCompletionCollector.java | 27 ++++++++++++----------
.../org/netbeans/api/java/source/SourceUtils.java | 1 +
.../org/netbeans/api/java/source/WorkingCopy.java | 15 +++++++++---
3 files changed, 28 insertions(+), 15 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 b6228782c0..a115a62dc9 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
@@ -43,6 +43,7 @@ import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.lang.model.SourceVersion;
@@ -218,19 +219,20 @@ public class JavaCompletionCollector implements CompletionCollector {
public static Supplier<List<TextEdit>> addImport(Document doc, int offset, ElementHandle<?> handle) {
return () -> {
try {
+ AtomicReference<String> pkg = new AtomicReference<>();
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;
- }
+ String fqn = SourceUtils.resolveImport(copy, copy.getTreeUtilities().pathFor(offset), handle.getQualifiedName());
+ if (fqn != null) {
+ int idx = fqn.lastIndexOf('.');
+ if (idx >= 0) {
+ pkg.set(fqn.substring(0, idx + 1));
}
- copy.rewrite(copy.getCompilationUnit(), GeneratorUtilities.get(copy).addImports(copy.getCompilationUnit(), Collections.singleton(e)));
}
});
+ if (textEdit == null && pkg.get() != null) {
+ textEdit = new TextEdit(offset, offset, pkg.get());
+ }
return textEdit != null ? Collections.singletonList(textEdit) : null;
} catch (IOException ex) {
throw new RuntimeException(ex);
@@ -921,6 +923,7 @@ public class JavaCompletionCollector implements CompletionCollector {
}
private Completion createTypeItem(CompilationInfo info, String prefix, ElementHandle<TypeElement> handle, TypeElement elem, DeclaredType type, int substitutionOffset, ReferencesCount referencesCount, boolean isDeprecated, boolean insideNew, boolean addTypeVars, boolean addSimpleName, boolean smartType) {
+ int off = info.getSnapshot().getEmbeddedOffset(substitutionOffset);
String name = elem.getQualifiedName().toString();
int idx = name.lastIndexOf('.');
String pkgName = idx < 0 ? EMPTY : name.substring(0, idx);
@@ -942,7 +945,7 @@ public class JavaCompletionCollector implements CompletionCollector {
} else if (info.getTreeUtilities().isModuleInfo(info.getCompilationUnit())) {
insertText.append(elem.getQualifiedName());
} else {
- TreePath tp = info.getTreeUtilities().pathFor(info.getSnapshot().getEmbeddedOffset(substitutionOffset));
+ TreePath tp = info.getTreeUtilities().pathFor(off);
if (tp != null && tp.getLeaf().getKind() == Tree.Kind.IMPORT) {
insertText.append(elem.getQualifiedName());
inImport = true;
@@ -961,7 +964,7 @@ public class JavaCompletionCollector implements CompletionCollector {
insertText.append('<'); //NOI18N
if (!insideNew || elem.getModifiers().contains(Modifier.ABSTRACT)
|| info.getSourceVersion().compareTo(SourceVersion.RELEASE_7) < 0
- || !allowDiamond(info, substitutionOffset, type)) {
+ || !allowDiamond(info, off, type)) {
while (tas.hasNext()) {
TypeMirror ta = tas.next();
insertText.append("${").append(cnt++).append(":");
@@ -1013,9 +1016,9 @@ public class JavaCompletionCollector implements CompletionCollector {
.addCommitCharacter('.');
}
if (handle != null) {
- builder.documentation(getDocumentation(doc, offset, handle));
+ builder.documentation(getDocumentation(doc, off, handle));
if (!addSimpleName && !inImport) {
- builder.additionalTextEdits(addImport(doc, offset, handle));
+ builder.additionalTextEdits(addImport(doc, off, handle));
}
}
if (isDeprecated) {
diff --git a/java/java.source.base/src/org/netbeans/api/java/source/SourceUtils.java b/java/java.source.base/src/org/netbeans/api/java/source/SourceUtils.java
index 6cd772847c..2a02cdb1d3 100644
--- a/java/java.source.base/src/org/netbeans/api/java/source/SourceUtils.java
+++ b/java/java.source.base/src/org/netbeans/api/java/source/SourceUtils.java
@@ -407,6 +407,7 @@ public class SourceUtils {
if (info instanceof WorkingCopy) {
CompilationUnitTree nue = (CompilationUnitTree) ((WorkingCopy)info).resolveRewriteTarget(cut);
((WorkingCopy)info).rewrite(info.getCompilationUnit(), GeneratorUtilities.get((WorkingCopy)info).addImports(nue, elementsToImport));
+ ((WorkingCopy)info).invalidateSourceAfter = true;
} else {
final ElementHandle handle = ElementHandle.create(toImport);
SwingUtilities.invokeLater(new Runnable() {
diff --git a/java/java.source.base/src/org/netbeans/api/java/source/WorkingCopy.java b/java/java.source.base/src/org/netbeans/api/java/source/WorkingCopy.java
index ff8bc89c20..3a7aff6191 100644
--- a/java/java.source.base/src/org/netbeans/api/java/source/WorkingCopy.java
+++ b/java/java.source.base/src/org/netbeans/api/java/source/WorkingCopy.java
@@ -70,7 +70,6 @@ import com.sun.source.util.DocTrees;
import java.util.Collection;
import java.util.Comparator;
-import com.sun.tools.javac.parser.ParserFactory;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.annotations.common.NullAllowed;
import org.netbeans.api.annotations.common.NullUnknown;
@@ -99,7 +98,8 @@ import org.netbeans.modules.java.source.save.ElementOverlay.FQNComputer;
import org.netbeans.modules.java.source.transform.ImmutableDocTreeTranslator;
import org.netbeans.modules.java.source.transform.ImmutableTreeTranslator;
import org.netbeans.modules.java.source.transform.TreeDuplicator;
-import org.netbeans.modules.parsing.api.Snapshot;
+import org.netbeans.modules.parsing.api.Source;
+import org.netbeans.modules.parsing.impl.SourceAccessor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle;
@@ -1282,6 +1282,8 @@ public class WorkingCopy extends CompilationController {
return creator.create(template, scratchFolder, name);
}
+ boolean invalidateSourceAfter = false;
+
List<Difference> getChanges(Map<?, int[]> tag2Span) throws IOException, BadLocationException {
if (afterCommit)
throw new IllegalStateException("The commit method can be called only once on a WorkingCopy instance"); //NOI18N
@@ -1322,7 +1324,14 @@ public class WorkingCopy extends CompilationController {
result.addAll(processExternalCUs(tag2Span, syntheticTrees));
overlay.clearElementsCache();
-
+
+ if (invalidateSourceAfter) {
+ Source source = impl.getSnapshot() != null ? impl.getSnapshot().getSource() : null;
+ if (source != null) {
+ SourceAccessor.getINSTANCE().invalidate(source, true);
+ }
+ }
+
return result;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists