### Eclipse Workspace Patch 1.0 #P org.eclipse.cdt.ui Index: src/org/eclipse/cdt/internal/ui/text/contentassist/KeywordCompletionContributor.java =================================================================== RCS file: src/org/eclipse/cdt/internal/ui/text/contentassist/KeywordCompletionContributor.java diff -N src/org/eclipse/cdt/internal/ui/text/contentassist/KeywordCompletionContributor.java --- src/org/eclipse/cdt/internal/ui/text/contentassist/KeywordCompletionContributor.java 31 Jan 2007 13:09:21 -0000 1.4 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,207 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2005, 2007 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM - Initial API and implementation - * Anton Leherbauer (Wind River Systems) - *******************************************************************************/ -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.IASTFieldReference; -import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.model.IWorkingCopy; -import org.eclipse.cdt.core.parser.Directives; -import org.eclipse.cdt.core.parser.Keywords; -import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider; -import org.eclipse.cdt.ui.CUIPlugin; -import org.eclipse.cdt.ui.text.ICPartitions; -import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; -import org.eclipse.jface.resource.ImageDescriptor; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.ITextViewer; -import org.eclipse.jface.text.TextUtilities; -import org.eclipse.swt.graphics.Image; - -public class KeywordCompletionContributor implements ICompletionContributor { - - public void contributeCompletionProposals(ITextViewer viewer, int offset, - IWorkingCopy workingCopy, ASTCompletionNode completionNode, - String prefix, List proposals) { - - // No prefix, no completions - if (prefix.length() == 0) - return; - - String[] keywords; - if(inPreprocessorDirective(viewer.getDocument(), offset)) { - keywords= preprocessorKeywords; - } else { - if (!validContext(completionNode)) - return; - - keywords = cppkeywords; // default to C++ - if (workingCopy != null && workingCopy.isCLanguage()) - keywords = ckeywords; - - } - // add matching keyword proposals - ImageDescriptor imagedesc = CElementImageProvider.getKeywordImageDescriptor(); - Image image = imagedesc != null ? CUIPlugin.getImageDescriptorRegistry().get(imagedesc) : null; - for (int i = 0; i < keywords.length; ++i) { - if (keywords[i].startsWith(prefix)) { - int repLength = prefix.length(); - int repOffset = offset - repLength; - proposals.add(new CCompletionProposal(keywords[i], repOffset, repLength, image, keywords[i], 1, viewer)); - } - } - } - - // TODO This is copied from the search completion contributor - // We should make this common - private boolean validContext(ASTCompletionNode completionNode) { - if (completionNode == null) - // No completion node, assume true - return true; - - boolean valid = true; - IASTName[] names = completionNode.getNames(); - for (int i = 0; i < names.length; i++) { - IASTName name = names[i]; - - // not hooked up, not a valid name, ignore - if (name.getTranslationUnit() == null) - continue; - - // member access currently isn't valid - if (name.getParent() instanceof IASTFieldReference) { - valid = false; - continue; - } - - // found one that was valid - return true; - } - - // Couldn't find a valid context - return valid; - } - - /** - * Check if given offset is inside a preprocessor directive. - * - * @param doc the document - * @param offset the offset to check - * @return true if offset is inside a preprocessor directive - */ - private boolean inPreprocessorDirective(IDocument doc, int offset) { - if (offset > 0 && offset == doc.getLength()) { - --offset; - } - try { - return ICPartitions.C_PREPROCESSOR - .equals(TextUtilities.getContentType(doc, ICPartitions.C_PARTITIONING, offset, false)); - } catch (BadLocationException exc) { - } - return false; - } - - // These are the keywords we complete - // We only do the ones that are >= 5 characters long - private static String [] ckeywords = { - Keywords.BREAK, - Keywords.CONST, - Keywords.CONTINUE, - Keywords.DEFAULT, - Keywords.DOUBLE, - Keywords.EXTERN, - Keywords.FLOAT, - Keywords.INLINE, - Keywords.REGISTER, - Keywords.RESTRICT, - Keywords.RETURN, - Keywords.SHORT, - Keywords.SIGNED, - Keywords.SIZEOF, - Keywords.STATIC, - Keywords.STRUCT, - Keywords.SWITCH, - Keywords.TYPEDEF, - Keywords.UNION, - Keywords.UNSIGNED, - Keywords.VOLATILE, - Keywords.WHILE, - Keywords._BOOL, - Keywords._COMPLEX, - Keywords._IMAGINARY - }; - - private static String [] cppkeywords = { - Keywords.BREAK, - Keywords.CATCH, - Keywords.CLASS, - Keywords.CONST, - Keywords.CONST_CAST, - Keywords.CONTINUE, - Keywords.DEFAULT, - Keywords.DELETE, - Keywords.DOUBLE, - Keywords.DYNAMIC_CAST, - Keywords.EXPLICIT, - Keywords.EXPORT, - Keywords.EXTERN, - Keywords.FALSE, - Keywords.FLOAT, - Keywords.FRIEND, - Keywords.INLINE, - Keywords.MUTABLE, - Keywords.NAMESPACE, - Keywords.OPERATOR, - Keywords.PRIVATE, - Keywords.PROTECTED, - Keywords.PUBLIC, - Keywords.REGISTER, - Keywords.REINTERPRET_CAST, - Keywords.RETURN, - Keywords.SHORT, - Keywords.SIGNED, - Keywords.SIZEOF, - Keywords.STATIC, - Keywords.STATIC_CAST, - Keywords.STRUCT, - Keywords.SWITCH, - Keywords.TEMPLATE, - Keywords.THROW, - Keywords.TYPEDEF, - Keywords.TYPEID, - Keywords.TYPENAME, - Keywords.UNION, - Keywords.UNSIGNED, - Keywords.USING, - Keywords.VIRTUAL, - Keywords.VOLATILE, - Keywords.WCHAR_T, - Keywords.WHILE - }; - - private static String [] preprocessorKeywords = { - Directives.POUND_DEFINE, - Directives.POUND_ELIF, - Directives.POUND_ELSE, - Directives.POUND_ENDIF, - Directives.POUND_ERROR, - Directives.POUND_IF, - Directives.POUND_IFDEF, - Directives.POUND_IFNDEF, - Directives.POUND_INCLUDE, - Directives.POUND_PRAGMA, - Directives.POUND_UNDEF, - }; -} Index: src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionContributor.java =================================================================== RCS file: src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionContributor.java diff -N src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionContributor.java --- src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionContributor.java 5 Jul 2005 20:32:07 -0000 1.4 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,104 +0,0 @@ -/********************************************************************** - * Copyright (c) 2004, 2005 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -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.IASTFieldReference; -import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.core.model.IWorkingCopy; -import org.eclipse.cdt.internal.ui.CHelpProviderManager; -import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider; -import org.eclipse.cdt.ui.CUIPlugin; -import org.eclipse.cdt.ui.IFunctionSummary; -import org.eclipse.cdt.ui.text.ICHelpInvocationContext; -import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; -import org.eclipse.core.resources.IProject; -import org.eclipse.jface.text.ITextViewer; -import org.eclipse.jface.text.contentassist.ContextInformation; -import org.eclipse.swt.graphics.Image; - -public class HelpCompletionContributor implements ICompletionContributor { - - public void contributeCompletionProposals(ITextViewer viewer, int offset, - IWorkingCopy workingCopy, ASTCompletionNode completionNode, String prefix, - List proposals) - { - final IWorkingCopy fWorkingCopy = workingCopy; - if (completionNode != null) { - // Find matching functions - ICHelpInvocationContext context = new ICHelpInvocationContext() { - - public IProject getProject() { - return fWorkingCopy.getCProject().getProject(); - } - - public ITranslationUnit getTranslationUnit() { - return fWorkingCopy.getTranslationUnit(); - } - }; - - IASTName[] names = completionNode.getNames(); - for (int i = 0; i < names.length; ++i) { - IASTName name = names[i]; - - if (name.getTranslationUnit() == null) - // Not connected - continue; - - // ignore if this is a member access - if (name.getParent() instanceof IASTFieldReference) - continue; - - IFunctionSummary[] summaries = CHelpProviderManager.getDefault().getMatchingFunctions(context, prefix); - if (summaries == null ) - continue; - - int repOffset = offset - prefix.length(); - int repLength = prefix.length(); - Image image = CUIPlugin.getImageDescriptorRegistry().get(CElementImageProvider.getFunctionImageDescriptor()); - - for (int j = 0; j < summaries.length; j++) { - IFunctionSummary summary = summaries[j]; - String fname = summary.getName() + "()"; //$NON-NLS-1$ - String fdesc = summary.getDescription(); - IFunctionSummary.IFunctionPrototypeSummary fproto = summary.getPrototype(); - String fargs = fproto.getArguments(); - - CCompletionProposal proposal; - proposal = new CCompletionProposal(fname, - repOffset, - repLength, - image, - fproto.getPrototypeString(true), - 2, - viewer); - - if (fdesc != null) { - proposal.setAdditionalProposalInfo(fdesc); - } - - if (fargs != null && fargs.length() > 0) { - proposal.setContextInformation(new ContextInformation(fname, fargs)); - // set the cursor before the closing bracket - proposal.setCursorPosition(fname.length() - 1); - } - - proposals.add(proposal); - } - - } - } - } - -} Index: plugin.xml =================================================================== RCS file: /cvsroot/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui/plugin.xml,v retrieving revision 1.236 diff -u -r1.236 plugin.xml --- plugin.xml 21 Feb 2007 13:03:58 -0000 1.236 +++ plugin.xml 23 Feb 2007 14:32:49 -0000 @@ -1582,21 +1582,6 @@ name="%CDTIndexer.domsourceindexer"/> - - - - - + + + + + + + + + + + + + + doc.getLength()) + return null; + + int length= 0; + while (--offset >= 0 && Character.isJavaIdentifierPart(doc.getChar(offset))) + length++; + + return doc.get(offset + 1, length); + } + + /** + * Checks whether the given invocation context looks valid for template completion. + * + * @param context the content assist invocation context + * @return false if the given invocation context looks like a field reference + */ + private boolean isValidContext(ContentAssistInvocationContext context) { + CHeuristicScanner scanner= new CHeuristicScanner(context.getDocument()); + int start= context.getInvocationOffset(); + return !scanner.looksLikeFieldReferenceBackward(start, Math.max(0, start-100)); + } + + /** + * Check if given offset is inside a preprocessor directive. + * + * @param doc the document + * @param offset the offset to check + * @return true if offset is inside a preprocessor directive + */ + private boolean inPreprocessorDirective(IDocument doc, int offset) { + if (offset > 0 && offset == doc.getLength()) { + --offset; + } + try { + return ICPartitions.C_PREPROCESSOR + .equals(TextUtilities.getContentType(doc, ICPartitions.C_PARTITIONING, offset, false)); + } catch (BadLocationException exc) { + } + return false; + } + + public List computeContextInformation( + ContentAssistInvocationContext context, IProgressMonitor monitor) { + return Collections.EMPTY_LIST; + } + + public String getErrorMessage() { + return null; + } + + public void sessionStarted() { + } + + public void sessionEnded() { + } + + // These are the keywords we complete + // We only do the ones that are >= 5 characters long + private static String [] ckeywords = { + Keywords.BREAK, + Keywords.CONST, + Keywords.CONTINUE, + Keywords.DEFAULT, + Keywords.DOUBLE, + Keywords.EXTERN, + Keywords.FLOAT, + Keywords.INLINE, + Keywords.REGISTER, + Keywords.RESTRICT, + Keywords.RETURN, + Keywords.SHORT, + Keywords.SIGNED, + Keywords.SIZEOF, + Keywords.STATIC, + Keywords.STRUCT, + Keywords.SWITCH, + Keywords.TYPEDEF, + Keywords.UNION, + Keywords.UNSIGNED, + Keywords.VOLATILE, + Keywords.WHILE, + Keywords._BOOL, + Keywords._COMPLEX, + Keywords._IMAGINARY + }; + + private static String [] cppkeywords = { + Keywords.BREAK, + Keywords.CATCH, + Keywords.CLASS, + Keywords.CONST, + Keywords.CONST_CAST, + Keywords.CONTINUE, + Keywords.DEFAULT, + Keywords.DELETE, + Keywords.DOUBLE, + Keywords.DYNAMIC_CAST, + Keywords.EXPLICIT, + Keywords.EXPORT, + Keywords.EXTERN, + Keywords.FALSE, + Keywords.FLOAT, + Keywords.FRIEND, + Keywords.INLINE, + Keywords.MUTABLE, + Keywords.NAMESPACE, + Keywords.OPERATOR, + Keywords.PRIVATE, + Keywords.PROTECTED, + Keywords.PUBLIC, + Keywords.REGISTER, + Keywords.REINTERPRET_CAST, + Keywords.RETURN, + Keywords.SHORT, + Keywords.SIGNED, + Keywords.SIZEOF, + Keywords.STATIC, + Keywords.STATIC_CAST, + Keywords.STRUCT, + Keywords.SWITCH, + Keywords.TEMPLATE, + Keywords.THROW, + Keywords.TYPEDEF, + Keywords.TYPEID, + Keywords.TYPENAME, + Keywords.UNION, + Keywords.UNSIGNED, + Keywords.USING, + Keywords.VIRTUAL, + Keywords.VOLATILE, + Keywords.WCHAR_T, + Keywords.WHILE + }; + + private static String [] preprocessorKeywords = { + Directives.POUND_DEFINE, + Directives.POUND_ELIF, + Directives.POUND_ELSE, + Directives.POUND_ENDIF, + Directives.POUND_ERROR, + Directives.POUND_IF, + Directives.POUND_IFDEF, + Directives.POUND_IFNDEF, + Directives.POUND_INCLUDE, + Directives.POUND_PRAGMA, + Directives.POUND_UNDEF, + }; +} Index: src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionProposalComputer.java =================================================================== RCS file: src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionProposalComputer.java diff -N src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionProposalComputer.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionProposalComputer.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,126 @@ +/******************************************************************************* + * Copyright (c) 2007 QNX Software Systems and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Bryan Wilkinson (QNX) - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.text.contentassist; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.swt.graphics.Image; + +import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; +import org.eclipse.cdt.core.dom.ast.IASTFieldReference; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.IFunctionSummary; +import org.eclipse.cdt.ui.text.ICHelpInvocationContext; + +import org.eclipse.cdt.internal.ui.CHelpProviderManager; +import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider; + +public class HelpCompletionProposalComputer extends ParsingBasedProposalComputer { + + protected List computeCompletionProposals( + CContentAssistInvocationContext cContext, + ASTCompletionNode completionNode, String prefix) + throws CoreException { + + boolean handleHelp = false; + if (completionNode != null) { + IASTName[] names = completionNode.getNames(); + for (int i = 0; i < names.length; ++i) { + IASTName name = names[i]; + + // ignore if not connected + if (name.getTranslationUnit() == null) + continue; + + // ignore if this is a member access + if (name.getParent() instanceof IASTFieldReference) + continue; + + handleHelp = true; + break; + } + } + + if (!handleHelp) { + return Collections.EMPTY_LIST; + } + + final ITranslationUnit tu = cContext.getTranslationUnit(); + // Find matching functions + ICHelpInvocationContext helpContext = new ICHelpInvocationContext() { + + public IProject getProject() { + return tu.getCProject().getProject(); + } + + public ITranslationUnit getTranslationUnit() { + return tu; + } + }; + + IFunctionSummary[] summaries = CHelpProviderManager.getDefault() + .getMatchingFunctions(helpContext, prefix); + if (summaries == null) + return Collections.EMPTY_LIST; + + int repOffset = cContext.getInvocationOffset() - prefix.length(); + int repLength = prefix.length(); + Image image = CUIPlugin.getImageDescriptorRegistry().get( + CElementImageProvider.getFunctionImageDescriptor()); + + List proposals = new ArrayList(); + + for (int j = 0; j < summaries.length; j++) { + IFunctionSummary summary = summaries[j]; + String fname = summary.getName() + "()"; //$NON-NLS-1$ + String fdesc = summary.getDescription(); + IFunctionSummary.IFunctionPrototypeSummary fproto = summary + .getPrototype(); + String fargs = fproto.getArguments(); + + CCompletionProposal proposal; + proposal = new CCompletionProposal( + fname, + repOffset, + repLength, + image, + fproto.getPrototypeString(true), + 2, + cContext.getViewer()); + + if (fdesc != null) { + proposal.setAdditionalProposalInfo(fdesc); + } + + if (!cContext.isContextInformationStyle()) { + // set the cursor before the closing bracket + proposal.setCursorPosition(fname.length() - 1); + } + + if (fargs != null && fargs.length() > 0) { + CProposalContextInformation info = new CProposalContextInformation(image, fname, fargs); + info.setContextInformationPosition(cContext.getContextInformationOffset()); + proposal.setContextInformation(info); + + } + + proposals.add(proposal); + } + + return proposals; + } +}