1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 10:46:02 +02:00

Update code completion to include for functions and

methods and to position the cursor appropiately.
This commit is contained in:
Alain Magloire 2003-08-27 13:17:47 +00:00
parent 397cfb2121
commit f62e609968
2 changed files with 88 additions and 43 deletions

View file

@ -15,7 +15,6 @@ import java.util.Map;
import org.eclipse.cdt.core.index.TagFlags; import org.eclipse.cdt.core.index.TagFlags;
import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IField;
import org.eclipse.cdt.core.model.IFunction; import org.eclipse.cdt.core.model.IFunction;
import org.eclipse.cdt.core.model.IFunctionDeclaration; import org.eclipse.cdt.core.model.IFunctionDeclaration;
import org.eclipse.cdt.core.model.IMember; import org.eclipse.cdt.core.model.IMember;
@ -591,31 +590,36 @@ public class CCompletionProcessor implements IContentAssistProcessor {
Iterator i = elementsFound.iterator(); Iterator i = elementsFound.iterator();
while (i.hasNext()){ while (i.hasNext()){
CCompletionProposal proposal; CCompletionProposal proposal;
FunctionPrototypeSummary fproto = null; String replaceString = "";
String fname = "";
String displayString = ""; String displayString = "";
Image image = null; Image image = null;
StringBuffer infoString = new StringBuffer(); StringBuffer infoString = new StringBuffer();
BasicSearchMatch match = (BasicSearchMatch)i.next(); BasicSearchMatch match = (BasicSearchMatch)i.next();
fproto = getPrototype(match);
fname = (fproto == null) ? match.getName() : fproto.getName(); //Make sure we replace with the appropriate string for functions and methods
displayString = (fproto == null) ? fname : fproto.getPrototypeString(true); FunctionPrototypeSummary fproto = getPrototype(match);
if(fproto != null) {
replaceString = fproto.getName() + "()";
displayString = fproto.getPrototypeString(true);
} else {
replaceString =
displayString = match.getName();;
}
image = labelProvider.getImage(match); image = labelProvider.getImage(match);
infoString.append(displayString); infoString.append(displayString);
if(match.getParentName().length() > 0) { if(match.getParentName().length() > 0) {
infoString.append(" - Parent: "); infoString.append(" - Parent: ");
infoString.append(match.getParentName()); infoString.append(match.getParentName());
} }
proposal = new CCompletionProposal( proposal = new CCompletionProposal(
fname, // replacement string replaceString, // Replacement string
region.getOffset(), region.getOffset(),
region.getLength(), region.getLength(),
image, image,
displayString, // displayString displayString, // Display string
calculateRelevance(match) calculateRelevance(match)
); );
completions.add(proposal); completions.add(proposal);
@ -625,7 +629,7 @@ public class CCompletionProcessor implements IContentAssistProcessor {
if(fproto != null){ if(fproto != null){
String fargs = fproto.getArguments(); String fargs = fproto.getArguments();
if(fargs != null && fargs.length() > 0) { if(fargs != null && fargs.length() > 0) {
proposal.setContextInformation(new ContextInformation(fname, fargs)); proposal.setContextInformation(new ContextInformation(replaceString, fargs));
} }
} }
@ -642,42 +646,61 @@ public class CCompletionProcessor implements IContentAssistProcessor {
ITagEntry[] tags = model.query(project, frag + "*", false, false); ITagEntry[] tags = model.query(project, frag + "*", false, false);
if (tags != null && tags.length > 0) { if (tags != null && tags.length > 0) {
for (int i = 0; i < tags.length; i++) { for (int i = 0; i < tags.length; i++) {
String fname = tags[i].getTagName();
FunctionPrototypeSummary fproto = null; FunctionPrototypeSummary fproto = null;
String fargs = null;
String fdisplay = null;
String fdesc = null;
String fname = tags[i].getTagName();
int kind = tags[i].getKind(); int kind = tags[i].getKind();
//No member completion yet
if (kind == TagFlags.T_MEMBER) {
continue;
}
//This doesn't give you a nice "function" look to macros, but is safe
if (kind == TagFlags.T_FUNCTION || kind == TagFlags.T_PROTOTYPE) { if (kind == TagFlags.T_FUNCTION || kind == TagFlags.T_PROTOTYPE) {
fname = fname + "()"; fname = fname + "()";
String pattern = tags[i].getPattern();
if(pattern != null) {
fproto = new FunctionPrototypeSummary(pattern);
}
if(fproto == null) {
fproto = new FunctionPrototypeSummary(fname);
}
}
if(fproto != null) {
fargs = fproto.getArguments();
fdisplay = fproto.getPrototypeString(true);
} else {
fdisplay = fname;
}
//@@@ In the future something more usefull could go in here (ie Doxygen/JavaDoc)
fdesc = "<b>" + fname + "</b><br>" + "Defined in:<br> " + tags[i].getFileName();
if(tags[i].getClassName() != null) {
fdesc = fdesc + "<br>Class:<br> " + tags[i].getClassName();
}
//System.out.println("tagmatch " + fname + " proto " + proto + " type" + tags[i].getKind());
CCompletionProposal proposal;
proposal = new CCompletionProposal(fname,
region.getOffset(),
region.getLength(),
getTagImage(kind),
fdisplay,
3);
completions.add(proposal);
if(fdesc != null) {
proposal.setAdditionalProposalInfo(fdesc);
} }
if(tags[i].getPattern() != null) { if(fargs != null && fargs.length() > 0) {
try { proposal.setContextInformation(new ContextInformation(fname, fargs));
fproto = new FunctionPrototypeSummary(tags[i].getPattern());
} catch(Exception ex) {
fproto = null;
}
}
if(fproto == null) {
fproto = new FunctionPrototypeSummary(fname);
}
//System.out.println("tagmatch " + fname + " proto " + proto + " type" + tags[i].getKind());
if (kind != TagFlags.T_MEMBER) {
CCompletionProposal proposal;
proposal = new CCompletionProposal(fname,
region.getOffset(),
region.getLength(),
getTagImage(kind),
fproto.getPrototypeString(true),
3);
completions.add(proposal);
//No summary information available yet
String fargs = fproto.getArguments();
if(fargs != null && fargs.length() > 0) {
proposal.setContextInformation(new ContextInformation(fname, fargs));
}
} }
} }
} }

View file

@ -10,26 +10,48 @@ public class FunctionPrototypeSummary implements IFunctionSummary.IFunctionProto
String farguments; String farguments;
/** /**
* Creates a prototype which matches the format * Create a function prototype summary based on a prototype string.
* returntype function(arguments) * @param The string describing the prototype which is properly
* @param properProto * formed with following format -- returntype function(arguments)
* The following formats will be converted as follows:
* function(arguments) --> void function(arguments)
* returntype function --> returntype function()
* function --> void function()
*/ */
public FunctionPrototypeSummary(String proto) { public FunctionPrototypeSummary(String proto) {
int leftbracket = proto.indexOf('('); int leftbracket = proto.indexOf('(');
int rightbracket = proto.lastIndexOf(')'); int rightbracket = proto.lastIndexOf(')');
//If there are brackets missing, then assume void parameters
if(leftbracket == -1 || rightbracket == -1) {
if(leftbracket != -1) {
proto = proto.substring(leftbracket) + ")";
} else if(rightbracket != -1) {
proto = proto.substring(rightbracket - 1) + "()";
} else {
proto = proto + "()";
}
leftbracket = proto.indexOf('(');
rightbracket = proto.lastIndexOf(')');
}
farguments = proto.substring(leftbracket + 1, rightbracket); farguments = proto.substring(leftbracket + 1, rightbracket);
int nameend = leftbracket - 1; int nameend = leftbracket - 1;
while(proto.charAt(nameend) == ' ') { while(proto.charAt(nameend) == ' ') {
nameend--; nameend--;
} }
int namestart = nameend; int namestart = nameend;
while(namestart > 0 && proto.charAt(namestart) != ' ') { while(namestart > 0 && proto.charAt(namestart) != ' ') {
namestart--; namestart--;
} }
fname = proto.substring(namestart, nameend + 1).trim(); fname = proto.substring(namestart, nameend + 1).trim();
if(namestart == 0) { if(namestart == 0) {
//@@@ Should this be int instead?
freturn = "void"; freturn = "void";
} else { } else {
freturn = proto.substring(0, namestart).trim(); freturn = proto.substring(0, namestart).trim();