From b5ac4549c745df2253c1ac0c60035f619f1b0766 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Mon, 21 Mar 2005 14:31:22 +0000 Subject: [PATCH] Hooked up the parameter list validator. --- .../contentassist/CCompletionProcessor2.java | 5 +- .../DOMCompletionContributor.java | 79 ++++++++++++++++--- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java index 7403eb9222d..bbd7dfe4ab1 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java @@ -21,6 +21,7 @@ import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.ParserUtil; +import org.eclipse.cdt.internal.ui.text.CParameterListValidator; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; import org.eclipse.core.resources.IFile; @@ -43,6 +44,7 @@ public class CCompletionProcessor2 implements IContentAssistProcessor { private IEditorPart editor; private String errorMessage; + public CCompletionProcessor2(IEditorPart editor) { this.editor = editor; } @@ -152,8 +154,7 @@ public class CCompletionProcessor2 implements IContentAssistProcessor { * @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#getContextInformationValidator() */ public IContextInformationValidator getContextInformationValidator() { - // TODO Auto-generated method stub - return null; + return new CParameterListValidator(); } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java index cb7dd6a4673..34831e1d967 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java @@ -10,11 +10,14 @@ package org.eclipse.cdt.internal.ui.text.contentassist; import java.util.List; import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; +import org.eclipse.cdt.core.dom.ast.ASTUtil; import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.IFunction; +import org.eclipse.cdt.core.dom.ast.IParameter; +import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.IVariable; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; @@ -23,7 +26,6 @@ import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.text.ITextViewer; -import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.swt.graphics.Image; public class DOMCompletionContributor implements ICompletionContributor { @@ -33,20 +35,73 @@ public class DOMCompletionContributor implements ICompletionContributor { ASTCompletionNode completionNode, List proposals) { if (completionNode != null) { - int repLength = completionNode.getLength(); - int repOffset = offset - repLength; - IASTName[] names = completionNode.getNames(); for (int i = 0; i < names.length; ++i) { - IBinding [] bindings = names[i].resolvePrefix(); - if (bindings != null) - for (int j = 0; j < bindings.length; ++j) - proposals.add(createBindingCompletionProposal(bindings[j], repOffset, repLength)); + IBinding[] bindings = names[i].resolvePrefix(); + for (int j = 0; j < bindings.length; ++j) + handleBinding(names[i], bindings[j], completionNode, offset, viewer, proposals); } } } - private ICompletionProposal createBindingCompletionProposal(IBinding binding, int offset, int length) { + private void handleBinding(IASTName name, IBinding binding, ASTCompletionNode completionNode, int offset, ITextViewer viewer, List proposals) { + if (binding instanceof IFunction) + handleFunction(name, (IFunction)binding, completionNode, offset, viewer, proposals); + else + proposals.add(createProposal(binding.getName(), binding.getName(), getImage(binding), completionNode, offset, viewer)); + } + + private void handleFunction(IASTName name, IFunction function, ASTCompletionNode completionNode, int offset, ITextViewer viewer, List proposals) { + Image image = getImage(CElementImageProvider.getFunctionImageDescriptor()); + + String repString = new String(name.toCharArray()) + "("; //$NON-NLS-1$ + + StringBuffer args = new StringBuffer(); + try { + IParameter[] params = function.getParameters(); + if (params != null) + for (int i = 0; i < params.length; ++i) { + IType paramType = params[i].getType(); + if (i > 0) + args.append(','); + + args.append(ASTUtil.getType(paramType)); + String paramName = params[i].getName(); + if (paramName != null) { + args.append(' '); + args.append(paramName); + } + } + } catch (DOMException e) { + } + String argString = args.toString(); + + String descString = repString + argString + ")"; //$NON-NLS-1$ + repString = repString + ")"; //$NON-NLS-1$ + + CCompletionProposal proposal = createProposal(repString, descString, image, completionNode, offset, viewer); + proposal.setCursorPosition(repString.length() - 1); + + if (argString.length() > 0) { + CProposalContextInformation info = new CProposalContextInformation(repString, argString); + info.setContextInformationPosition(offset); + proposal.setContextInformation(info); + } + + proposals.add(proposal); + } + + private CCompletionProposal createProposal(String repString, String dispString, Image image, ASTCompletionNode completionNode, int offset, ITextViewer viewer) { + int repLength = completionNode.getLength(); + int repOffset = offset - repLength; + return new CCompletionProposal(repString, repOffset, repLength, image, dispString, 1, viewer); + } + + private Image getImage(ImageDescriptor desc) { + return desc != null ? CUIPlugin.getImageDescriptorRegistry().get(desc) : null; + } + + private Image getImage(IBinding binding) { ImageDescriptor imageDescriptor = null; try { @@ -67,11 +122,9 @@ public class DOMCompletionContributor implements ICompletionContributor { } catch (DOMException e) { } - Image image = imageDescriptor != null + return imageDescriptor != null ? CUIPlugin.getImageDescriptorRegistry().get( imageDescriptor ) : null; - - return new CCompletionProposal(binding.getName(), offset, length, image, binding.getName(), 1); } - + }