View | Details | Raw Unified | Return to bug 175283 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/cdt/internal/ui/text/contentassist/KeywordCompletionContributor.java (-207 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * IBM - Initial API and implementation
10
 * Anton Leherbauer (Wind River Systems)
11
 *******************************************************************************/
12
package org.eclipse.cdt.internal.ui.text.contentassist;
13
14
import java.util.List;
15
16
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
17
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
18
import org.eclipse.cdt.core.dom.ast.IASTName;
19
import org.eclipse.cdt.core.model.IWorkingCopy;
20
import org.eclipse.cdt.core.parser.Directives;
21
import org.eclipse.cdt.core.parser.Keywords;
22
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
23
import org.eclipse.cdt.ui.CUIPlugin;
24
import org.eclipse.cdt.ui.text.ICPartitions;
25
import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor;
26
import org.eclipse.jface.resource.ImageDescriptor;
27
import org.eclipse.jface.text.BadLocationException;
28
import org.eclipse.jface.text.IDocument;
29
import org.eclipse.jface.text.ITextViewer;
30
import org.eclipse.jface.text.TextUtilities;
31
import org.eclipse.swt.graphics.Image;
32
33
public class KeywordCompletionContributor implements ICompletionContributor {
34
35
    public void contributeCompletionProposals(ITextViewer viewer, int offset,
36
            IWorkingCopy workingCopy, ASTCompletionNode completionNode,
37
            String prefix, List proposals) {
38
39
        // No prefix, no completions
40
        if (prefix.length() == 0)
41
            return;
42
43
        String[] keywords;
44
		if(inPreprocessorDirective(viewer.getDocument(), offset)) {
45
			keywords= preprocessorKeywords;
46
		} else {
47
	        if (!validContext(completionNode))
48
	            return;
49
	        
50
	        keywords = cppkeywords; // default to C++
51
	        if (workingCopy != null && workingCopy.isCLanguage())
52
	            keywords = ckeywords;
53
	        
54
		}
55
		// add matching keyword proposals
56
        ImageDescriptor imagedesc = CElementImageProvider.getKeywordImageDescriptor();
57
        Image image = imagedesc != null ? CUIPlugin.getImageDescriptorRegistry().get(imagedesc) : null;
58
        for (int i = 0; i < keywords.length; ++i) {
59
            if (keywords[i].startsWith(prefix)) {
60
                int repLength = prefix.length();
61
                int repOffset = offset - repLength;
62
                proposals.add(new CCompletionProposal(keywords[i], repOffset, repLength, image, keywords[i], 1, viewer));
63
            }
64
        }
65
    }
66
67
    // TODO This is copied from the search completion contributor
68
    // We should make this common
69
    private boolean validContext(ASTCompletionNode completionNode) {
70
        if (completionNode == null)
71
            // No completion node, assume true
72
            return true;
73
74
        boolean valid = true;
75
        IASTName[] names = completionNode.getNames();
76
        for (int i = 0; i < names.length; i++) {
77
            IASTName name = names[i];
78
            
79
            // not hooked up, not a valid name, ignore
80
            if (name.getTranslationUnit() == null)
81
                continue;
82
            
83
            // member access currently isn't valid
84
            if (name.getParent() instanceof IASTFieldReference) {
85
                valid = false;
86
                continue;
87
            }
88
            
89
            // found one that was valid
90
            return true;
91
        }
92
93
        // Couldn't find a valid context
94
        return valid;
95
    }
96
97
	/**
98
	 * Check if given offset is inside a preprocessor directive.
99
	 * 
100
	 * @param doc  the document
101
	 * @param offset  the offset to check
102
	 * @return <code>true</code> if offset is inside a preprocessor directive
103
	 */
104
	private boolean inPreprocessorDirective(IDocument doc, int offset) {
105
		if (offset > 0 && offset == doc.getLength()) {
106
		--offset;
107
		}
108
		try {
109
			return ICPartitions.C_PREPROCESSOR
110
					.equals(TextUtilities.getContentType(doc, ICPartitions.C_PARTITIONING, offset, false));
111
		} catch (BadLocationException exc) {
112
		}
113
		return false;
114
	}
115
116
    // These are the keywords we complete
117
    // We only do the ones that are >= 5 characters long
118
    private static String [] ckeywords = {
119
        Keywords.BREAK,
120
        Keywords.CONST,
121
        Keywords.CONTINUE,
122
        Keywords.DEFAULT,
123
        Keywords.DOUBLE,
124
        Keywords.EXTERN,
125
        Keywords.FLOAT,
126
        Keywords.INLINE,
127
        Keywords.REGISTER,
128
        Keywords.RESTRICT,
129
        Keywords.RETURN,
130
        Keywords.SHORT,
131
        Keywords.SIGNED,
132
        Keywords.SIZEOF,
133
        Keywords.STATIC,
134
        Keywords.STRUCT,
135
        Keywords.SWITCH,
136
        Keywords.TYPEDEF,
137
        Keywords.UNION,
138
        Keywords.UNSIGNED,
139
        Keywords.VOLATILE,
140
        Keywords.WHILE,
141
        Keywords._BOOL,
142
        Keywords._COMPLEX,
143
        Keywords._IMAGINARY
144
    };
145
146
    private static String [] cppkeywords = {
147
        Keywords.BREAK,
148
        Keywords.CATCH,
149
        Keywords.CLASS,
150
        Keywords.CONST,
151
        Keywords.CONST_CAST,
152
        Keywords.CONTINUE,
153
        Keywords.DEFAULT,
154
        Keywords.DELETE,
155
        Keywords.DOUBLE,
156
        Keywords.DYNAMIC_CAST,
157
        Keywords.EXPLICIT,
158
        Keywords.EXPORT,
159
        Keywords.EXTERN,
160
        Keywords.FALSE,
161
        Keywords.FLOAT,
162
        Keywords.FRIEND,
163
        Keywords.INLINE,
164
        Keywords.MUTABLE,
165
        Keywords.NAMESPACE,
166
        Keywords.OPERATOR,
167
        Keywords.PRIVATE,
168
        Keywords.PROTECTED,
169
        Keywords.PUBLIC,
170
        Keywords.REGISTER,
171
        Keywords.REINTERPRET_CAST,
172
        Keywords.RETURN,
173
        Keywords.SHORT,
174
        Keywords.SIGNED,
175
        Keywords.SIZEOF,
176
        Keywords.STATIC,
177
        Keywords.STATIC_CAST,
178
        Keywords.STRUCT,
179
        Keywords.SWITCH,
180
        Keywords.TEMPLATE,
181
        Keywords.THROW,
182
        Keywords.TYPEDEF,
183
        Keywords.TYPEID,
184
        Keywords.TYPENAME,
185
        Keywords.UNION,
186
        Keywords.UNSIGNED,
187
        Keywords.USING,
188
        Keywords.VIRTUAL,
189
        Keywords.VOLATILE,
190
        Keywords.WCHAR_T,
191
        Keywords.WHILE
192
    };
193
194
    private static String [] preprocessorKeywords = {
195
        Directives.POUND_DEFINE,
196
        Directives.POUND_ELIF,
197
        Directives.POUND_ELSE,
198
        Directives.POUND_ENDIF,
199
        Directives.POUND_ERROR,
200
        Directives.POUND_IF,
201
        Directives.POUND_IFDEF,
202
        Directives.POUND_IFNDEF,
203
        Directives.POUND_INCLUDE,
204
        Directives.POUND_PRAGMA,
205
        Directives.POUND_UNDEF,
206
    };
207
}
(-)src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionContributor.java (-104 lines)
Removed Link Here
1
/**********************************************************************
2
 * Copyright (c) 2004, 2005 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.cdt.internal.ui.text.contentassist;
12
13
import java.util.List;
14
15
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
16
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
17
import org.eclipse.cdt.core.dom.ast.IASTName;
18
import org.eclipse.cdt.core.model.ITranslationUnit;
19
import org.eclipse.cdt.core.model.IWorkingCopy;
20
import org.eclipse.cdt.internal.ui.CHelpProviderManager;
21
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
22
import org.eclipse.cdt.ui.CUIPlugin;
23
import org.eclipse.cdt.ui.IFunctionSummary;
24
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
25
import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor;
26
import org.eclipse.core.resources.IProject;
27
import org.eclipse.jface.text.ITextViewer;
28
import org.eclipse.jface.text.contentassist.ContextInformation;
29
import org.eclipse.swt.graphics.Image;
30
31
public class HelpCompletionContributor implements ICompletionContributor {
32
33
	public void contributeCompletionProposals(ITextViewer viewer, int offset,
34
			IWorkingCopy workingCopy, ASTCompletionNode completionNode, String prefix,
35
			List proposals)
36
	{
37
		final IWorkingCopy fWorkingCopy = workingCopy;
38
		if (completionNode != null) {
39
			// Find matching functions
40
			ICHelpInvocationContext context = new ICHelpInvocationContext() {
41
42
				public IProject getProject() {
43
					return fWorkingCopy.getCProject().getProject();
44
				}
45
46
				public ITranslationUnit getTranslationUnit() {
47
					return fWorkingCopy.getTranslationUnit();
48
				}	
49
			};
50
			
51
			IASTName[] names = completionNode.getNames();
52
			for (int i = 0; i < names.length; ++i) {
53
				IASTName name = names[i];
54
				
55
				if (name.getTranslationUnit() == null)
56
					// Not connected
57
					continue;
58
			
59
				// ignore if this is a member access
60
				if (name.getParent() instanceof IASTFieldReference)
61
					continue;
62
63
				IFunctionSummary[] summaries = CHelpProviderManager.getDefault().getMatchingFunctions(context, prefix);
64
				if (summaries == null )
65
					continue;
66
				
67
				int repOffset = offset - prefix.length();
68
				int repLength = prefix.length();
69
				Image image = CUIPlugin.getImageDescriptorRegistry().get(CElementImageProvider.getFunctionImageDescriptor());
70
71
				for (int j = 0; j < summaries.length; j++) {
72
					IFunctionSummary summary = summaries[j];
73
					String fname = summary.getName() + "()"; //$NON-NLS-1$
74
					String fdesc = summary.getDescription();
75
					IFunctionSummary.IFunctionPrototypeSummary fproto = summary.getPrototype();
76
					String fargs = fproto.getArguments();
77
					
78
					CCompletionProposal proposal;
79
					proposal = new CCompletionProposal(fname, 
80
													   repOffset, 
81
													   repLength,
82
													   image, 
83
													   fproto.getPrototypeString(true),
84
													   2,
85
													   viewer);
86
87
					if (fdesc != null) {
88
						proposal.setAdditionalProposalInfo(fdesc);
89
					}
90
					
91
					if (fargs != null && fargs.length() > 0) {
92
						proposal.setContextInformation(new ContextInformation(fname, fargs));
93
						// set the cursor before the closing bracket
94
						proposal.setCursorPosition(fname.length() - 1);
95
					}
96
97
					proposals.add(proposal);
98
				}
99
100
			}
101
		}
102
	}
103
104
}
(-)plugin.xml (-15 / +25 lines)
Lines 1582-1602 Link Here
1582
            name="%CDTIndexer.domsourceindexer"/>
1582
            name="%CDTIndexer.domsourceindexer"/>
1583
1583
1584
   </extension>
1584
   </extension>
1585
	<extension
1586
         point="org.eclipse.cdt.ui.completionContributors">
1587
      <contributor
1588
            class="org.eclipse.cdt.internal.ui.text.contentassist.KeywordCompletionContributor"
1589
            id="Keywords"
1590
            priority="10"/>
1591
      <contributor
1592
            class="org.eclipse.cdt.internal.ui.text.contentassist.HelpCompletionContributor"
1593
            id="Help"
1594
            priority="99"/>
1595
      <!--contributor
1596
            class="org.eclipse.cdt.internal.ui.text.contentassist.PDOMCompletionContributor"
1597
            id="PDOM"
1598
            priority="3"/-->
1599
   	</extension>
1600
1585
1601
    <extension
1586
    <extension
1602
        point="org.eclipse.cdt.ui.completionProposalComputer"
1587
        point="org.eclipse.cdt.ui.completionProposalComputer"
Lines 1651-1656 Link Here
1651
            <partition type="__c_preprocessor"/>
1636
            <partition type="__c_preprocessor"/>
1652
        </completionProposalComputer>
1637
        </completionProposalComputer>
1653
    </extension>
1638
    </extension>
1639
    <extension
1640
          id="KeywordCompletionProposalComputer"
1641
          point="org.eclipse.cdt.ui.completionProposalComputer">
1642
       <completionProposalComputer
1643
             categoryId="org.eclipse.cdt.ui.parserProposalCategory"
1644
             class="org.eclipse.cdt.internal.ui.text.contentassist.KeywordCompletionProposalComputer">
1645
          <partition
1646
                type="__dftl_partition_content_type">
1647
          </partition>
1648
          <partition
1649
                type="__c_preprocessor">
1650
          </partition>
1651
       </completionProposalComputer>
1652
    </extension>
1653
    <extension
1654
          id="HelpCompletionProposalComputer"
1655
          point="org.eclipse.cdt.ui.completionProposalComputer">
1656
       <completionProposalComputer
1657
             categoryId="org.eclipse.cdt.ui.parserProposalCategory"
1658
             class="org.eclipse.cdt.internal.ui.text.contentassist.HelpCompletionProposalComputer">
1659
          <partition
1660
                type="__dftl_partition_content_type">
1661
          </partition>
1662
       </completionProposalComputer>
1663
    </extension>
1654
    <!-- template proposals -->
1664
    <!-- template proposals -->
1655
    <extension
1665
    <extension
1656
        point="org.eclipse.cdt.ui.completionProposalComputer"
1666
        point="org.eclipse.cdt.ui.completionProposalComputer"
(-)src/org/eclipse/cdt/internal/ui/text/contentassist/KeywordCompletionProposalComputer.java (+214 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 QNX Software Systems and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * Bryan Wilkinson (QNX) - Initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.cdt.internal.ui.text.contentassist;
12
13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.List;
16
17
import org.eclipse.core.runtime.CoreException;
18
import org.eclipse.jface.resource.ImageDescriptor;
19
import org.eclipse.jface.text.BadLocationException;
20
import org.eclipse.jface.text.IDocument;
21
import org.eclipse.jface.text.TextUtilities;
22
import org.eclipse.swt.graphics.Image;
23
24
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
25
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
26
import org.eclipse.cdt.core.dom.ast.IASTName;
27
import org.eclipse.cdt.core.model.ITranslationUnit;
28
import org.eclipse.cdt.core.parser.Directives;
29
import org.eclipse.cdt.core.parser.Keywords;
30
import org.eclipse.cdt.ui.CUIPlugin;
31
import org.eclipse.cdt.ui.text.ICPartitions;
32
import org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer;
33
34
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
35
36
public class KeywordCompletionProposalComputer extends ParsingBasedProposalComputer implements ICompletionProposalComputer {
37
38
	protected List computeCompletionProposals(
39
			CContentAssistInvocationContext context,
40
			ASTCompletionNode completionNode, String prefix)
41
			throws CoreException {
42
43
		// No prefix, no completions
44
        if (prefix.length() == 0 || context.isContextInformationStyle())
45
            return Collections.EMPTY_LIST;
46
47
        String[] keywords;
48
		if(inPreprocessorDirective(context.getDocument(), context.getInvocationOffset())) {
49
			keywords= preprocessorKeywords;
50
		} else {
51
	        if (!isValidContext(completionNode))
52
	            return Collections.EMPTY_LIST;
53
	        
54
	        ITranslationUnit tu = context.getTranslationUnit();
55
	        
56
	        keywords = cppkeywords; // default to C++
57
	        if (tu != null && tu.isCLanguage())
58
	            keywords = ckeywords;
59
	        
60
		}
61
		
62
		List proposals = new ArrayList();
63
		
64
		// add matching keyword proposals
65
        ImageDescriptor imagedesc = CElementImageProvider.getKeywordImageDescriptor();
66
        Image image = imagedesc != null ? CUIPlugin.getImageDescriptorRegistry().get(imagedesc) : null;
67
        for (int i = 0; i < keywords.length; ++i) {
68
            if (keywords[i].startsWith(prefix)) {
69
                int repLength = prefix.length();
70
                int repOffset = context.getInvocationOffset() - repLength;
71
                proposals.add(new CCompletionProposal(keywords[i], repOffset,
72
						repLength, image, keywords[i], 1, context.getViewer()));
73
            }
74
        }
75
        
76
        return proposals;
77
	}
78
79
	/**
80
	 * Checks whether the given invocation context looks valid for template completion.
81
	 * 
82
	 * @param context  the content assist invocation context
83
	 * @return <code>false</code> if the given invocation context looks like a field reference
84
	 */
85
	private boolean isValidContext(ASTCompletionNode completionNode) {
86
		IASTName[] names = completionNode.getNames();
87
		for (int i = 0; i < names.length; ++i) {
88
			IASTName name = names[i];
89
			
90
			// ignore if not connected
91
			if (name.getTranslationUnit() == null)
92
				continue;
93
		
94
			// ignore if this is a member access
95
			if (name.getParent() instanceof IASTFieldReference)
96
				continue;
97
			
98
			return true;
99
		}
100
		
101
		return false;
102
	}
103
	
104
	/**
105
	 * Check if given offset is inside a preprocessor directive.
106
	 * 
107
	 * @param doc  the document
108
	 * @param offset  the offset to check
109
	 * @return <code>true</code> if offset is inside a preprocessor directive
110
	 */
111
	private boolean inPreprocessorDirective(IDocument doc, int offset) {
112
		if (offset > 0 && offset == doc.getLength()) {
113
		--offset;
114
		}
115
		try {
116
			return ICPartitions.C_PREPROCESSOR
117
					.equals(TextUtilities.getContentType(doc, ICPartitions.C_PARTITIONING, offset, false));
118
		} catch (BadLocationException exc) {
119
		}
120
		return false;
121
	}
122
	
123
    // These are the keywords we complete
124
    // We only do the ones that are >= 5 characters long
125
    private static String [] ckeywords = {
126
        Keywords.BREAK,
127
        Keywords.CONST,
128
        Keywords.CONTINUE,
129
        Keywords.DEFAULT,
130
        Keywords.DOUBLE,
131
        Keywords.EXTERN,
132
        Keywords.FLOAT,
133
        Keywords.INLINE,
134
        Keywords.REGISTER,
135
        Keywords.RESTRICT,
136
        Keywords.RETURN,
137
        Keywords.SHORT,
138
        Keywords.SIGNED,
139
        Keywords.SIZEOF,
140
        Keywords.STATIC,
141
        Keywords.STRUCT,
142
        Keywords.SWITCH,
143
        Keywords.TYPEDEF,
144
        Keywords.UNION,
145
        Keywords.UNSIGNED,
146
        Keywords.VOLATILE,
147
        Keywords.WHILE,
148
        Keywords._BOOL,
149
        Keywords._COMPLEX,
150
        Keywords._IMAGINARY
151
    };
152
153
    private static String [] cppkeywords = {
154
        Keywords.BREAK,
155
        Keywords.CATCH,
156
        Keywords.CLASS,
157
        Keywords.CONST,
158
        Keywords.CONST_CAST,
159
        Keywords.CONTINUE,
160
        Keywords.DEFAULT,
161
        Keywords.DELETE,
162
        Keywords.DOUBLE,
163
        Keywords.DYNAMIC_CAST,
164
        Keywords.EXPLICIT,
165
        Keywords.EXPORT,
166
        Keywords.EXTERN,
167
        Keywords.FALSE,
168
        Keywords.FLOAT,
169
        Keywords.FRIEND,
170
        Keywords.INLINE,
171
        Keywords.MUTABLE,
172
        Keywords.NAMESPACE,
173
        Keywords.OPERATOR,
174
        Keywords.PRIVATE,
175
        Keywords.PROTECTED,
176
        Keywords.PUBLIC,
177
        Keywords.REGISTER,
178
        Keywords.REINTERPRET_CAST,
179
        Keywords.RETURN,
180
        Keywords.SHORT,
181
        Keywords.SIGNED,
182
        Keywords.SIZEOF,
183
        Keywords.STATIC,
184
        Keywords.STATIC_CAST,
185
        Keywords.STRUCT,
186
        Keywords.SWITCH,
187
        Keywords.TEMPLATE,
188
        Keywords.THROW,
189
        Keywords.TYPEDEF,
190
        Keywords.TYPEID,
191
        Keywords.TYPENAME,
192
        Keywords.UNION,
193
        Keywords.UNSIGNED,
194
        Keywords.USING,
195
        Keywords.VIRTUAL,
196
        Keywords.VOLATILE,
197
        Keywords.WCHAR_T,
198
        Keywords.WHILE
199
    };
200
201
    private static String [] preprocessorKeywords = {
202
        Directives.POUND_DEFINE,
203
        Directives.POUND_ELIF,
204
        Directives.POUND_ELSE,
205
        Directives.POUND_ENDIF,
206
        Directives.POUND_ERROR,
207
        Directives.POUND_IF,
208
        Directives.POUND_IFDEF,
209
        Directives.POUND_IFNDEF,
210
        Directives.POUND_INCLUDE,
211
        Directives.POUND_PRAGMA,
212
        Directives.POUND_UNDEF,
213
    };
214
}
(-)src/org/eclipse/cdt/internal/ui/text/contentassist/HelpCompletionProposalComputer.java (+126 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 QNX Software Systems and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * Bryan Wilkinson (QNX) - Initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.cdt.internal.ui.text.contentassist;
12
13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.List;
16
17
import org.eclipse.core.resources.IProject;
18
import org.eclipse.core.runtime.CoreException;
19
import org.eclipse.swt.graphics.Image;
20
21
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
22
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
23
import org.eclipse.cdt.core.dom.ast.IASTName;
24
import org.eclipse.cdt.core.model.ITranslationUnit;
25
import org.eclipse.cdt.ui.CUIPlugin;
26
import org.eclipse.cdt.ui.IFunctionSummary;
27
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
28
29
import org.eclipse.cdt.internal.ui.CHelpProviderManager;
30
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
31
32
public class HelpCompletionProposalComputer extends ParsingBasedProposalComputer {
33
34
	protected List computeCompletionProposals(
35
			CContentAssistInvocationContext cContext,
36
			ASTCompletionNode completionNode, String prefix)
37
			throws CoreException {
38
		
39
		boolean handleHelp = false;
40
		if (completionNode != null) {
41
			IASTName[] names = completionNode.getNames();
42
			for (int i = 0; i < names.length; ++i) {
43
				IASTName name = names[i];
44
				
45
				// ignore if not connected
46
				if (name.getTranslationUnit() == null)
47
					continue;
48
			
49
				// ignore if this is a member access
50
				if (name.getParent() instanceof IASTFieldReference)
51
					continue;
52
				
53
				handleHelp = true;
54
				break;
55
			}
56
		}
57
		
58
		if (!handleHelp) {
59
			return Collections.EMPTY_LIST;
60
		}
61
		
62
		final ITranslationUnit tu = cContext.getTranslationUnit();
63
		// Find matching functions
64
		ICHelpInvocationContext helpContext = new ICHelpInvocationContext() {
65
66
			public IProject getProject() {
67
				return tu.getCProject().getProject();
68
			}
69
70
			public ITranslationUnit getTranslationUnit() {
71
				return tu;
72
			}
73
		};
74
75
		IFunctionSummary[] summaries = CHelpProviderManager.getDefault()
76
				.getMatchingFunctions(helpContext, prefix);
77
		if (summaries == null)
78
			return Collections.EMPTY_LIST;
79
80
		int repOffset = cContext.getInvocationOffset() - prefix.length();
81
		int repLength = prefix.length();
82
		Image image = CUIPlugin.getImageDescriptorRegistry().get(
83
				CElementImageProvider.getFunctionImageDescriptor());
84
85
		List proposals = new ArrayList();
86
87
		for (int j = 0; j < summaries.length; j++) {
88
			IFunctionSummary summary = summaries[j];
89
			String fname = summary.getName() + "()"; //$NON-NLS-1$
90
			String fdesc = summary.getDescription();
91
			IFunctionSummary.IFunctionPrototypeSummary fproto = summary
92
					.getPrototype();
93
			String fargs = fproto.getArguments();
94
95
			CCompletionProposal proposal;
96
			proposal = new CCompletionProposal(
97
					fname,
98
					repOffset,
99
					repLength,
100
					image,
101
					fproto.getPrototypeString(true),
102
					2,
103
					cContext.getViewer());
104
105
			if (fdesc != null) {
106
				proposal.setAdditionalProposalInfo(fdesc);
107
			}
108
109
			if (!cContext.isContextInformationStyle()) {
110
				// set the cursor before the closing bracket
111
				proposal.setCursorPosition(fname.length() - 1);
112
			}
113
			
114
			if (fargs != null && fargs.length() > 0) {
115
				CProposalContextInformation info = new CProposalContextInformation(image, fname, fargs);
116
				info.setContextInformationPosition(cContext.getContextInformationOffset());
117
				proposal.setContextInformation(info);
118
119
			}
120
121
			proposals.add(proposal);
122
		}
123
124
		return proposals;
125
	}
126
}

Return to bug 175283