Initial implementation of #2755

This commit is contained in:
Manindra Moharana
2014-08-07 12:58:27 +05:30
parent 24365e7dea
commit cd5a96785f
2 changed files with 111 additions and 8 deletions

View File

@@ -1045,13 +1045,14 @@ public class ASTGenerator {
if (sketchOutline.isVisible()) return;
Collections.sort(candidates);
// CompletionCandidate[][] candi = new CompletionCandidate[candidates.size()][1];
DefaultListModel<CompletionCandidate> defListModel = new DefaultListModel<CompletionCandidate>();
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<CompletionCandidate> defListModel = new DefaultListModel<CompletionCandidate>();
//
// 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<CompletionCandidate> 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<CompletionCandidate> filterPredictions(){
DefaultListModel<CompletionCandidate> defListModel = new DefaultListModel<CompletionCandidate>();
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
*

View File

@@ -63,7 +63,8 @@ public class CompletionCandidate implements Comparable<CompletionCandidate>{
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<CompletionCandidate>{
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<CompletionCandidate>{
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<CompletionCandidate>{
elementName = td.getName().toString();
label = elementName;
completionString = elementName;
wrappedObject = td;
}
public CompletionCandidate(Field f) {
@@ -148,6 +152,18 @@ public class CompletionCandidate implements Comparable<CompletionCandidate>{
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<CompletionCandidate>{
}
return (elementName.compareTo(cc.getElementName()));
}
public void regenerateCompletionString(){
if(wrappedObject instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration)wrappedObject;
List<ASTNode> params = (List<ASTNode>) 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();
}
}
}