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 |
} |