Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand All @@ -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<VariableElement> fieldElements = new ArrayList<>();
for (VariableElement fieldElement : fields) {
if (fieldElement != null && fieldElement.getKind().isField()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +49,8 @@

public class JavaCompletionCollectorTest extends NbTestCase {

private FileObject primaryTestFO;

public JavaCompletionCollectorTest(String name) {
super(name);
}
Expand Down Expand Up @@ -299,21 +302,62 @@ public static Map<Object, Object> 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<FileDescription> files, Validator<List<Completion>> 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);
Expand All @@ -324,16 +368,16 @@ private void runJavaCollector(List<FileDescription> files, Validator<List<Comple
TestUtilities.copyStringToFile(testFO, code);
}

assertNotNull(mainFile);
assertNotNull(primaryTestFO);

if (sourceLevel != null) {
SourceUtilsTestUtil.setSourceLevel(mainFile, sourceLevel);
SourceUtilsTestUtil.setSourceLevel(primaryTestFO, sourceLevel);
}

SourceUtilsTestUtil.prepareTest(src, FileUtil.createFolder(scratch, "test-build"), cache);
SourceUtilsTestUtil.compileRecursively(src);

EditorCookie ec = mainFile.getLookup().lookup(EditorCookie.class);
EditorCookie ec = primaryTestFO.getLookup().lookup(EditorCookie.class);
Document doc = ec.openDocument();
JavaCompletionCollector collector = new JavaCompletionCollector();
Context ctx = new Context(TriggerKind.Invoked, null);
Expand Down
Loading