Skip to content

Commit 28e3b4f

Browse files
committed
Make CompletionCandidate immutable
This one goes from ASTGenerator on a background thread to the JList which displays code suggestions. Until refactored, I'm making it immutable with convenience methods returning mutated copies to prevent possible threading issues.
1 parent 95e04cd commit 28e3b4f

2 files changed

Lines changed: 38 additions & 26 deletions

File tree

java/src/processing/mode/java/pdex/ASTGenerator.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,9 +1015,10 @@ protected static DefaultListModel<CompletionCandidate> filterPredictions(List<Co
10151015
if (candidates.get(0).getElementName()
10161016
.equals(candidates.get(candidates.size() - 1).getElementName())) {
10171017
log("All CC are methods only: " + candidates.get(0).getElementName());
1018-
for (CompletionCandidate candidate : candidates) {
1019-
candidate.regenerateCompletionString();
1020-
defListModel.addElement(candidate);
1018+
for (int i = 0; i < candidates.size(); i++) {
1019+
CompletionCandidate cc = candidates.get(i).withRegeneratedCompString();
1020+
candidates.set(i, cc);
1021+
defListModel.addElement(cc);
10211022
}
10221023
}
10231024
else {
@@ -1030,14 +1031,15 @@ protected static DefaultListModel<CompletionCandidate> filterPredictions(List<Co
10301031
CompletionCandidate cc = candidates.get(i - 1);
10311032
String label = cc.getLabel();
10321033
int x = label.lastIndexOf(')');
1033-
if(candidates.get(i).getType() == CompletionCandidate.PREDEF_METHOD) {
1034-
cc.setLabel((cc.getLabel().contains("<html>") ? "<html>" : "")
1035-
+ cc.getElementName() + "(...)" + label.substring(x + 1));
1036-
}
1037-
else {
1038-
cc.setLabel(cc.getElementName() + "(...)" + label.substring(x + 1));
1034+
String newLabel;
1035+
if (candidates.get(i).getType() == CompletionCandidate.PREDEF_METHOD) {
1036+
newLabel = (cc.getLabel().contains("<html>") ? "<html>" : "")
1037+
+ cc.getElementName() + "(...)" + label.substring(x + 1);
1038+
} else {
1039+
newLabel = cc.getElementName() + "(...)" + label.substring(x + 1);
10391040
}
1040-
cc.setCompletionString(cc.getElementName() + "(");
1041+
String newCompString = cc.getElementName() + "(";
1042+
candidates.set(i - 1, cc.withLabelAndCompString(newLabel, newCompString));
10411043
ignoredSome = true;
10421044
continue;
10431045
}

java/src/processing/mode/java/pdex/CompletionCandidate.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333

3434

3535
public class CompletionCandidate implements Comparable<CompletionCandidate>{
36-
private String elementName;
37-
private String label; // the toString value
38-
private String completionString;
39-
private Object wrappedObject;
40-
private int type;
36+
private final String elementName;
37+
private final String label; // the toString value
38+
private final String completionString;
39+
private final Object wrappedObject;
40+
private final int type;
4141

4242
static final int PREDEF_CLASS = 0;
4343
static final int PREDEF_FIELD = 1;
@@ -158,13 +158,25 @@ public CompletionCandidate(String name, String labelStr, String completionStr, i
158158
label = labelStr;
159159
completionString = completionStr;
160160
this.type = type;
161+
wrappedObject = null;
161162
}
162163

163164
public CompletionCandidate(String name, int type) {
164165
elementName = name;
165166
label = name;
166167
completionString = name;
167168
this.type = type;
169+
wrappedObject = null;
170+
}
171+
172+
private CompletionCandidate(String elementName, String label,
173+
String completionString, int type,
174+
Object wrappedObject) {
175+
this.elementName = elementName;
176+
this.label = label;
177+
this.completionString = completionString;
178+
this.type = type;
179+
this.wrappedObject = wrappedObject;
168180
}
169181

170182
public String getElementName() {
@@ -204,22 +216,21 @@ public String getNoHtmlLabel(){
204216
}
205217
}
206218

207-
public void setLabel(String label) {
208-
this.label = label;
209-
}
210-
211-
public void setCompletionString(String completionString) {
212-
this.completionString = completionString;
219+
public CompletionCandidate withLabelAndCompString(String label,
220+
String completionString) {
221+
return new CompletionCandidate(this.elementName, label, completionString,
222+
this.type, this.wrappedObject);
213223
}
214224

225+
@Override
215226
public int compareTo(CompletionCandidate cc) {
216227
if(type != cc.getType()){
217228
return cc.getType() - type;
218229
}
219230
return (elementName.compareTo(cc.getElementName()));
220231
}
221232

222-
public void regenerateCompletionString(){
233+
public CompletionCandidate withRegeneratedCompString() {
223234
if (wrappedObject instanceof MethodDeclaration) {
224235
MethodDeclaration method = (MethodDeclaration)wrappedObject;
225236

@@ -243,8 +254,7 @@ public void regenerateCompletionString(){
243254
if (method.getReturnType2() != null)
244255
label.append(" : " + method.getReturnType2());
245256
cstr.append(")");
246-
this.label = label.toString();
247-
this.completionString = cstr.toString();
257+
return this.withLabelAndCompString(label.toString(), cstr.toString());
248258
}
249259
else if (wrappedObject instanceof Method) {
250260
Method method = (Method)wrappedObject;
@@ -265,8 +275,7 @@ else if (wrappedObject instanceof Method) {
265275
label.append(" : " + method.getReturnType().getSimpleName());
266276
label.append(" - <font color=#777777>" + method.getDeclaringClass().getSimpleName() + "</font></html>");
267277
cstr.append(")");
268-
this.label = label.toString();
269-
this.completionString = cstr.toString();
278+
return this.withLabelAndCompString(label.toString(), cstr.toString());
270279
/*
271280
* StringBuilder label = new StringBuilder("<html>"+method.getName() + "(");
272281
StringBuilder cstr = new StringBuilder(method.getName() + "(");
@@ -286,6 +295,7 @@ else if (wrappedObject instanceof Method) {
286295
label.append(" - <font color=#777777>" + method.getDeclaringClass().getSimpleName() + "</font></html>");
287296
* */
288297
}
298+
return this;
289299
}
290300

291301
}

0 commit comments

Comments
 (0)