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 4eb78d72617c..788c3055aa8a 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 @@ -39,6 +39,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; @@ -824,6 +825,7 @@ public Completion createInitializeAllConstructorItem(CompilationInfo info, boole } labelDetail.append(") - generate"); sortParams.append(')'); + ElementHandle parentPath = ElementHandle.create(parent); return CompletionCollector.newBuilder(simpleName) .kind(Completion.Kind.Constructor) .labelDetail(labelDetail.toString()) @@ -834,7 +836,11 @@ public Completion createInitializeAllConstructorItem(CompilationInfo info, boole wc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); TreePath tp = wc.getTreeUtilities().pathFor(substitutionOffset); if (TreeUtilities.CLASS_TREE_KINDS.contains(tp.getLeaf().getKind())) { - if (parent == wc.getTrees().getElement(tp)) { + Element currentType = wc.getTrees().getElement(tp); + ElementHandle currentTypePath = + currentType != null ? ElementHandle.create(currentType) + : null; + if (Objects.equals(parentPath, currentTypePath)) { ArrayList fieldElements = new ArrayList<>(); for (VariableElement fieldElement : fields) { if (fieldElement != null && fieldElement.getKind().isField()) { diff --git a/java/java.editor/test/unit/src/org/netbeans/modules/editor/java/JavaCompletionCollectorTest.java b/java/java.editor/test/unit/src/org/netbeans/modules/editor/java/JavaCompletionCollectorTest.java index bc1e4bdb87cf..328a1b5bf62a 100644 --- a/java/java.editor/test/unit/src/org/netbeans/modules/editor/java/JavaCompletionCollectorTest.java +++ b/java/java.editor/test/unit/src/org/netbeans/modules/editor/java/JavaCompletionCollectorTest.java @@ -18,6 +18,7 @@ */ package org.netbeans.modules.editor.java; +import java.io.OutputStream; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -48,6 +49,8 @@ public class JavaCompletionCollectorTest extends NbTestCase { + private FileObject primaryTestFO; + public JavaCompletionCollectorTest(String name) { super(name); } @@ -299,21 +302,62 @@ public static Map test() { assertEquals(Set.of("()"), found); } + public void testAdditionalEditsGenerateConstructorAfterReparse() throws Exception { + AtomicBoolean found = new AtomicBoolean(); + runJavaCollector(List.of(new FileDescription("test/Test.java", + """ + package test; + public class Test { + private final int i; + | + } + """)), + completions -> { + for (Completion completion : completions) { + if (completion.getLabel().equals("Test") && + "(int i) - generate".equals(completion.getLabelDetail())) { + //force full reparse: + byte[] content = primaryTestFO.asBytes(); + try (OutputStream out = primaryTestFO.getOutputStream()) { + out.write(content); + } + assertEquals(null, + completion.getInsertText()); + assertEquals("63-63:", + textEdit2String(completion.getTextEdit())); + assertEquals(""" + 59-59: + public Test(int i) { + this.i = i; + } + """.replace("\n", "\\n"), + completion.getAdditionalTextEdits() + .get() + .stream() + .map(JavaCompletionCollectorTest::textEdit2String) + .collect(Collectors.joining(", "))); + found.set(true); + } + } + }); + assertTrue(found.get()); + } + private void runJavaCollector(List files, Validator> validator) throws Exception { SourceUtilsTestUtil.prepareTest(new String[]{"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object[]{new MIMEResolverImpl(), new MIMEDataProvider()}); FileObject scratch = SourceUtilsTestUtil.makeScratchDir(this); FileObject cache = scratch.createFolder("cache"); FileObject src = scratch.createFolder("src"); - FileObject mainFile = null; + primaryTestFO = null; int caretPosition = -1; for (FileDescription testFile : files) { FileObject testFO = FileUtil.createData(src, testFile.fileName); String code = testFile.code; - if (mainFile == null) { - mainFile = testFO; + if (primaryTestFO == null) { + primaryTestFO = testFO; caretPosition = code.indexOf('|'); assertTrue(caretPosition >= 0); @@ -324,16 +368,16 @@ private void runJavaCollector(List files, Validator