From cd5a96785fa357f05cbd81fb598c2a802d3c5ddf Mon Sep 17 00:00:00 2001 From: Manindra Moharana Date: Thu, 7 Aug 2014 12:58:27 +0530 Subject: [PATCH] Initial implementation of #2755 --- .../mode/experimental/ASTGenerator.java | 53 +++++++++++++-- .../experimental/CompletionCandidate.java | 66 ++++++++++++++++++- 2 files changed, 111 insertions(+), 8 deletions(-) diff --git a/pdex/src/processing/mode/experimental/ASTGenerator.java b/pdex/src/processing/mode/experimental/ASTGenerator.java index 723ba302c..139436122 100644 --- a/pdex/src/processing/mode/experimental/ASTGenerator.java +++ b/pdex/src/processing/mode/experimental/ASTGenerator.java @@ -1045,13 +1045,14 @@ public class ASTGenerator { if (sketchOutline.isVisible()) return; Collections.sort(candidates); // CompletionCandidate[][] candi = new CompletionCandidate[candidates.size()][1]; - DefaultListModel defListModel = new DefaultListModel(); - - for (int i = 0; i < candidates.size(); i++) { -// candi[i][0] = candidates.get(i); - defListModel.addElement(candidates.get(i)); - } - log("Total preds = " + candidates.size()); +// DefaultListModel defListModel = new DefaultListModel(); +// +// for (int i = 0; i < candidates.size(); i++) { +//// candi[i][0] = candidates.get(i); +// defListModel.addElement(candidates.get(i)); +// } +// log("Total preds = " + candidates.size()); + DefaultListModel defListModel = filterPredictions(); // DefaultTableModel tm = new DefaultTableModel(candi, // new String[] { "Suggestions" }); // if (tableAuto.isVisible()) { @@ -1063,6 +1064,44 @@ public class ASTGenerator { .showSuggestion(defListModel, word); } + private DefaultListModel filterPredictions(){ + DefaultListModel defListModel = new DefaultListModel(); + if (candidates.isEmpty()) + return defListModel; + // check if first & last CompCandidate are the same methods, only then show all overloaded methods + if (candidates.get(0).getElementName() + .equals(candidates.get(candidates.size() - 1).getElementName())) { + log("All CC are methods only: " + candidates.get(0).getElementName()); + for (int i = 0; i < candidates.size(); i++) { + candidates.get(i).regenerateCompletionString(); + defListModel.addElement(candidates.get(i)); + } + } + else { + boolean ignoredSome = false; + for (int i = 0; i < candidates.size(); i++) { + if(i > 0 && (candidates.get(i).getElementName() + .equals(candidates.get(i - 1).getElementName()))){ + if (candidates.get(i).getType() == CompletionCandidate.LOCAL_METHOD + || candidates.get(i).getType() == CompletionCandidate.PREDEF_METHOD) { + CompletionCandidate cc = candidates.get(i - 1); + String label = cc.getLabel(); + int x = label.lastIndexOf(')'); + cc.setLabel(cc.getElementName() + "(...)" + label.substring(x + 1)); + cc.setCompletionString(cc.getElementName()); + ignoredSome = true; + continue; + } + } + defListModel.addElement(candidates.get(i)); + } + if (ignoredSome) { + log("Some suggestions hidden"); + } + } + return defListModel; + } + /** * Loads classes from .jar files in sketch classpath * diff --git a/pdex/src/processing/mode/experimental/CompletionCandidate.java b/pdex/src/processing/mode/experimental/CompletionCandidate.java index cf7ad5c40..44584319f 100644 --- a/pdex/src/processing/mode/experimental/CompletionCandidate.java +++ b/pdex/src/processing/mode/experimental/CompletionCandidate.java @@ -63,7 +63,8 @@ public class CompletionCandidate implements Comparable{ type = LOCAL_FIELD; else type = LOCAL_VAR; - label = svd.getName() + " : " + svd.getType(); + label = svd.getName() + " : " + svd.getType(); + wrappedObject = svd; } public CompletionCandidate(VariableDeclarationFragment vdf) { @@ -74,6 +75,7 @@ public class CompletionCandidate implements Comparable{ else type = LOCAL_VAR; label = vdf.getName() + " : " + ASTGenerator.extracTypeInfo2(vdf); + wrappedObject = vdf; } public CompletionCandidate(MethodDeclaration method) { @@ -100,6 +102,7 @@ public class CompletionCandidate implements Comparable{ cstr.append(")"); this.label = label.toString(); this.completionString = cstr.toString(); + wrappedObject = method; } public CompletionCandidate(TypeDeclaration td){ @@ -107,6 +110,7 @@ public class CompletionCandidate implements Comparable{ elementName = td.getName().toString(); label = elementName; completionString = elementName; + wrappedObject = td; } public CompletionCandidate(Field f) { @@ -148,6 +152,18 @@ public class CompletionCandidate implements Comparable{ public int getType() { return type; } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public void setCompletionString(String completionString) { + this.completionString = completionString; + } public int compareTo(CompletionCandidate cc) { if(type != cc.getType()){ @@ -155,5 +171,53 @@ public class CompletionCandidate implements Comparable{ } return (elementName.compareTo(cc.getElementName())); } + + public void regenerateCompletionString(){ + if(wrappedObject instanceof MethodDeclaration) { + MethodDeclaration method = (MethodDeclaration)wrappedObject; + List params = (List) method + .getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY); + StringBuffer label = new StringBuffer(elementName + "("); + StringBuffer cstr = new StringBuffer(method.getName() + "("); + for (int i = 0; i < params.size(); i++) { + label.append(params.get(i).toString()); + if (i < params.size() - 1) { + label.append(","); + cstr.append(","); + } + } + if (params.size() == 1) { + cstr.append(' '); + } + label.append(")"); + if (method.getReturnType2() != null) + label.append(" : " + method.getReturnType2()); + cstr.append(")"); + this.label = label.toString(); + this.completionString = cstr.toString(); + } + else if (wrappedObject instanceof Method) { + Method method = (Method)wrappedObject; + StringBuffer label = new StringBuffer(method.getName() + "("); + StringBuffer cstr = new StringBuffer(method.getName() + "("); + for (int i = 0; i < method.getParameterTypes().length; i++) { + label.append(method.getParameterTypes()[i].getSimpleName()); + if (i < method.getParameterTypes().length - 1) { + label.append(","); + cstr.append(","); + } + } + if(method.getParameterTypes().length == 1) { + cstr.append(' '); + } + label.append(")"); + if(method.getReturnType() != null) + label.append(" : " + method.getReturnType().getSimpleName()); + label.append(" - " + method.getDeclaringClass().getSimpleName()); + cstr.append(")"); + this.label = label.toString(); + this.completionString = cstr.toString(); + } + } }