From d0edd5f5d296465d859b11edc914d4223e5843b0 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Thu, 31 Mar 2005 16:39:06 +0000 Subject: [PATCH] Added handling for macros. --- .../cdt/core/dom/ast/ASTCompletionNode.java | 17 ++++- .../parser/AbstractGNUSourceCodeParser.java | 2 +- .../DOMCompletionContributor.java | 64 ++++++++++++++++++- 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTCompletionNode.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTCompletionNode.java index 82a269a9938..3e56727e81b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTCompletionNode.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTCompletionNode.java @@ -30,15 +30,18 @@ public class ASTCompletionNode { private IToken completionToken; private List names = new ArrayList(); + + private IASTTranslationUnit translationUnit; /** * Only constructor. * - * @param completionToken - - * the completion token + * @param completionToken the completion token + * @param translationUnit the translation unit for this completion */ - public ASTCompletionNode(IToken completionToken) { + public ASTCompletionNode(IToken completionToken, IASTTranslationUnit translationUnit) { this.completionToken = completionToken; + this.translationUnit = translationUnit; } /** @@ -78,4 +81,12 @@ public class ASTCompletionNode { return (IASTName[]) names.toArray(new IASTName[names.size()]); } + /** + * Get the translation unit for this completion + * + * @return the translation unit + */ + public IASTTranslationUnit getTranslationUnit() { + return translationUnit; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java index a4bce321f86..efb5770e436 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java @@ -123,7 +123,7 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser { // Use to create the completion node protected ASTCompletionNode createCompletionNode(IToken token) { if (completionNode == null) - completionNode = new ASTCompletionNode(token); + completionNode = new ASTCompletionNode(token, getTranslationUnit()); return completionNode; } /** 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 bf2b75712c0..84283b6ff02 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 @@ -14,7 +14,10 @@ import java.util.List; import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; import org.eclipse.cdt.core.dom.ast.ASTTypeUtil; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTFunctionStyleMacroParameter; import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorFunctionStyleMacroDefinition; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.IFunction; @@ -41,8 +44,13 @@ public class DOMCompletionContributor implements ICompletionContributor { ASTCompletionNode completionNode, List proposals) { if (completionNode != null) { - List allBindings = new ArrayList(); IASTName[] names = completionNode.getNames(); + if (names == null || names.length == 0) + // No names, not much we can do here + return; + + // Find all bindings + List allBindings = new ArrayList(); for (int i = 0; i < names.length; ++i) { IBinding[] bindings = names[i].resolvePrefix(); if (bindings != null) @@ -58,6 +66,16 @@ public class DOMCompletionContributor implements ICompletionContributor { IBinding binding = (IBinding)iBinding.next(); handleBinding(binding, completionNode, offset, viewer, proposals); } + + // Find all macros if there is a prefix + String prefix = completionNode.getPrefix(); + if (prefix.length() > 0) { + IASTPreprocessorMacroDefinition[] macros = completionNode.getTranslationUnit().getMacroDefinitions(); + if (macros != null) + for (int i = 0; i < macros.length; ++i) + if (macros[i].getName().toString().startsWith(prefix)) + handleMacro(macros[i], completionNode, offset, viewer, proposals); + } } } @@ -125,6 +143,50 @@ public class DOMCompletionContributor implements ICompletionContributor { proposals.add(proposal); } + private void handleMacro(IASTPreprocessorMacroDefinition macro, ASTCompletionNode completionNode, int offset, ITextViewer viewer, List proposals) { + String macroName = macro.getName().toString(); + Image image = getImage(CElementImageProvider.getMacroImageDescriptor()); + + if (macro instanceof IASTPreprocessorFunctionStyleMacroDefinition) { + IASTPreprocessorFunctionStyleMacroDefinition functionMacro = (IASTPreprocessorFunctionStyleMacroDefinition)macro; + + StringBuffer repStringBuff = new StringBuffer(); + repStringBuff.append(macroName); + repStringBuff.append('('); + + StringBuffer args = new StringBuffer(); + + IASTFunctionStyleMacroParameter[] params = functionMacro.getParameters(); + if (params != null) + for (int i = 0; i < params.length; ++i) { + if (i > 0) + args.append(", "); + args.append(params[i].getParameter()); + } + String argString = args.toString(); + + StringBuffer descStringBuff = new StringBuffer(repStringBuff.toString()); + descStringBuff.append(argString); + descStringBuff.append(')'); + + repStringBuff.append(')'); + String repString = repStringBuff.toString(); + String descString = descStringBuff.toString(); + + 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); + } else + proposals.add(createProposal(macroName, macroName, image, completionNode, offset, viewer)); + } + private CCompletionProposal createProposal(String repString, String dispString, Image image, ASTCompletionNode completionNode, int offset, ITextViewer viewer) { int repLength = completionNode.getLength(); int repOffset = offset - repLength;