From 8fcdbb9e320af1a16b32cf549e15af759902010f Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Thu, 18 Dec 2008 08:26:32 +0000 Subject: [PATCH] Bug 217043 - ContentAssist: Position cursor behind ')' if method has no arguments, patch by Jens Elementhaler --- .../eclipse/cdt/core/dom/ast/ASTTypeUtil.java | 28 +++++++++++++++++++ .../DOMCompletionProposalComputer.java | 22 ++++++++++----- .../HelpCompletionProposalComputer.java | 9 ++++-- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java index c7e84dad164..2a87d5e771a 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java @@ -44,6 +44,7 @@ import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalBinding; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; +import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil; /** * This is a utility class to help convert AST elements to Strings corresponding to the @@ -76,6 +77,33 @@ public class ASTTypeUtil { result.append(Keywords.cpRPAREN); return result.toString(); } + + /** + * @return Whether the function matching the given function binding takes + * parameters or not. + * @throws DOMException + * + * @since 5.1 + */ + public static boolean functionTakesParameters(IFunction function) + throws DOMException { + IParameter[] parameters = function.getParameters(); + + if (parameters.length == 0) { + return false; + } else if (parameters.length == 1) { + IType ultimateType = SemanticUtil + .getUltimateTypeViaTypedefs(parameters[0].getType()); + + if (ultimateType instanceof IBasicType) { + if (((IBasicType) ultimateType).getType() == IBasicType.t_void) { + return false; + } + } + } + + return true; + } /** * Returns a string representation for the type array. The representation is diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java index 31d2c4063a7..b283d536952 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionProposalComputer.java @@ -238,7 +238,11 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer CCompletionProposal proposal = createProposal(repString, descString, prefix.length(), image, baseRelevance + RelevanceConstants.MACRO_TYPE_RELEVANCE, context); if (!context.isContextInformationStyle()) { - proposal.setCursorPosition(repString.length() - 1); + if (argString.length() > 0) { + proposal.setCursorPosition(repString.length() - 1); + } else { + proposal.setCursorPosition(repString.length()); + } } if (argString.length() > 0) { @@ -336,8 +340,9 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer repStringBuff.append(function.getName()); repStringBuff.append('('); - StringBuilder dispargs = new StringBuilder(); // for the displayString - StringBuilder idargs = new StringBuilder(); // for the idString + StringBuilder dispargs = new StringBuilder(); // for the dispargString + StringBuilder idargs = new StringBuilder(); // for the idargString + boolean hasArgs = true; String returnTypeStr = null; try { IParameter[] params = function.getParameters(); @@ -376,12 +381,14 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer if (returnType != null) returnTypeStr = ASTTypeUtil.getType(returnType, false); } + + hasArgs = ASTTypeUtil.functionTakesParameters(function); } catch (DOMException e) { } String dispargString = dispargs.toString(); String idargString = idargs.toString(); - + String contextDispargString = hasArgs ? dispargString : null; StringBuilder dispStringBuff = new StringBuilder(repStringBuff.toString()); dispStringBuff.append(dispargString); dispStringBuff.append(')'); @@ -402,11 +409,12 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer final int relevance = function instanceof ICPPMethod ? RelevanceConstants.METHOD_TYPE_RELEVANCE : RelevanceConstants.FUNCTION_TYPE_RELEVANCE; CCompletionProposal proposal = createProposal(repString, dispString, idString, context.getCompletionNode().getLength(), image, baseRelevance + relevance, context); if (!context.isContextInformationStyle()) { - proposal.setCursorPosition(repString.length() - 1); + int cursorPosition = hasArgs ? (repString.length() - 1) : repString.length(); + proposal.setCursorPosition(cursorPosition); } - if (dispargString.length() > 0) { - CProposalContextInformation info = new CProposalContextInformation(image, dispString, dispargString); + if (contextDispargString != null) { + CProposalContextInformation info = new CProposalContextInformation(image, dispString, contextDispargString); info.setContextInformationPosition(context.getContextInformationOffset()); proposal.setContextInformation(info); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionProposalComputer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionProposalComputer.java index d31d4f61cfe..a80ef79fe50 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionProposalComputer.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionProposalComputer.java @@ -110,8 +110,13 @@ public class HelpCompletionProposalComputer extends ParsingBasedProposalComputer } if (!cContext.isContextInformationStyle()) { - // set the cursor before the closing bracket - proposal.setCursorPosition(fname.length() - 1); + if (fargs != null && fargs.length() > 0) { + // set the cursor before the closing bracket + proposal.setCursorPosition(fname.length() - 1); + } else { + // set the cursor behind the closing bracked + proposal.setCursorPosition(fname.length()); + } } if (fargs != null && fargs.length() > 0) {