forked from oracle/javavscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8242.diff
More file actions
140 lines (128 loc) · 7.54 KB
/
8242.diff
File metadata and controls
140 lines (128 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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<VariableElement> 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<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);
@@ -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);