1 11 package org.eclipse.jdt.ui.text.java; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jface.text.ITextViewer; 16 17 import org.eclipse.ui.IEditorPart; 18 19 import org.eclipse.jdt.core.CompletionContext; 20 import org.eclipse.jdt.core.CompletionProposal; 21 import org.eclipse.jdt.core.ICodeAssist; 22 import org.eclipse.jdt.core.ICompilationUnit; 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.IJavaProject; 25 import org.eclipse.jdt.core.IType; 26 import org.eclipse.jdt.core.JavaModelException; 27 28 import org.eclipse.jdt.internal.corext.template.java.SignatureUtil; 29 30 import org.eclipse.jdt.internal.ui.JavaPlugin; 31 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; 32 import org.eclipse.jdt.internal.ui.text.java.ContentAssistHistory.RHSHistory; 33 34 42 public class JavaContentAssistInvocationContext extends ContentAssistInvocationContext { 43 private final IEditorPart fEditor; 44 45 private ICompilationUnit fCU= null; 46 private boolean fCUComputed= false; 47 48 private CompletionProposalLabelProvider fLabelProvider; 49 private CompletionProposalCollector fCollector; 50 private RHSHistory fRHSHistory; 51 private IType fType; 52 53 private IJavaCompletionProposal[] fKeywordProposals= null; 54 private CompletionContext fCoreContext= null; 55 56 63 public JavaContentAssistInvocationContext(ITextViewer viewer, int offset, IEditorPart editor) { 64 super(viewer, offset); 65 Assert.isNotNull(editor); 66 fEditor= editor; 67 } 68 69 74 public JavaContentAssistInvocationContext(ICompilationUnit unit) { 75 super(); 76 fCU= unit; 77 fCUComputed= true; 78 fEditor= null; 79 } 80 81 87 public ICompilationUnit getCompilationUnit() { 88 if (!fCUComputed) { 89 fCUComputed= true; 90 if (fCollector != null) 91 fCU= fCollector.getCompilationUnit(); 92 else { 93 IJavaElement je= EditorUtility.getEditorInputJavaElement(fEditor, false); 94 if (je instanceof ICompilationUnit) 95 fCU= (ICompilationUnit)je; 96 } 97 } 98 return fCU; 99 } 100 101 107 public IJavaProject getProject() { 108 ICompilationUnit unit= getCompilationUnit(); 109 return unit == null ? null : unit.getJavaProject(); 110 } 111 112 122 public IJavaCompletionProposal[] getKeywordProposals() { 123 if (fKeywordProposals == null) { 124 if (fCollector != null && !fCollector.isIgnored(CompletionProposal.KEYWORD) && fCollector.getContext() != null) { 125 fKeywordProposals= fCollector.getKeywordCompletionProposals(); 127 } else { 128 computeKeywordsAndContext(); 130 } 131 } 132 133 return fKeywordProposals; 134 } 135 136 147 public CompletionContext getCoreContext() { 148 if (fCoreContext == null) { 149 if (fCollector != null) 151 fCoreContext= fCollector.getContext(); 152 if (fCoreContext == null) 153 computeKeywordsAndContext(); 154 } 155 return fCoreContext; 156 } 157 158 172 public float getHistoryRelevance(String qualifiedTypeName) { 173 return getRHSHistory().getRank(qualifiedTypeName); 174 } 175 176 181 private RHSHistory getRHSHistory() { 182 if (fRHSHistory == null) { 183 CompletionContext context= getCoreContext(); 184 if (context != null) { 185 char[][] expectedTypes= context.getExpectedTypesSignatures(); 186 if (expectedTypes != null && expectedTypes.length > 0) { 187 String expected= SignatureUtil.stripSignatureToFQN(String.valueOf(expectedTypes[0])); 188 fRHSHistory= JavaPlugin.getDefault().getContentAssistHistory().getHistory(expected); 189 } 190 } 191 if (fRHSHistory == null) 192 fRHSHistory= JavaPlugin.getDefault().getContentAssistHistory().getHistory(null); 193 } 194 return fRHSHistory; 195 } 196 197 207 public IType getExpectedType() { 208 if (fType == null && getCompilationUnit() != null) { 209 CompletionContext context= getCoreContext(); 210 if (context != null) { 211 char[][] expectedTypes= context.getExpectedTypesSignatures(); 212 if (expectedTypes != null && expectedTypes.length > 0) { 213 IJavaProject project= getCompilationUnit().getJavaProject(); 214 if (project != null) { 215 try { 216 fType= project.findType(SignatureUtil.stripSignatureToFQN(String.valueOf(expectedTypes[0]))); 217 } catch (JavaModelException x) { 218 JavaPlugin.log(x); 219 } 220 } 221 } 222 } 223 } 224 return fType; 225 } 226 227 232 public CompletionProposalLabelProvider getLabelProvider() { 233 if (fLabelProvider == null) { 234 if (fCollector != null) 235 fLabelProvider= fCollector.getLabelProvider(); 236 else 237 fLabelProvider= new CompletionProposalLabelProvider(); 238 } 239 240 return fLabelProvider; 241 } 242 243 254 void setCollector(CompletionProposalCollector collector) { 255 fCollector= collector; 256 } 257 258 265 private void computeKeywordsAndContext() { 266 ICompilationUnit cu= getCompilationUnit(); 267 if (cu == null) { 268 if (fKeywordProposals == null) 269 fKeywordProposals= new IJavaCompletionProposal[0]; 270 return; 271 } 272 273 CompletionProposalCollector collector= new CompletionProposalCollector(cu); 274 collector.setIgnored(CompletionProposal.KEYWORD, false); 275 collector.setIgnored(CompletionProposal.ANNOTATION_ATTRIBUTE_REF, true); 276 collector.setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, true); 277 collector.setIgnored(CompletionProposal.FIELD_REF, true); 278 collector.setIgnored(CompletionProposal.LABEL_REF, true); 279 collector.setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, true); 280 collector.setIgnored(CompletionProposal.METHOD_DECLARATION, true); 281 collector.setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, true); 282 collector.setIgnored(CompletionProposal.METHOD_REF, true); 283 collector.setIgnored(CompletionProposal.PACKAGE_REF, true); 284 collector.setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, true); 285 collector.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true); 286 collector.setIgnored(CompletionProposal.JAVADOC_BLOCK_TAG, true); 287 collector.setIgnored(CompletionProposal.JAVADOC_FIELD_REF, true); 288 collector.setIgnored(CompletionProposal.JAVADOC_INLINE_TAG, true); 289 collector.setIgnored(CompletionProposal.JAVADOC_METHOD_REF, true); 290 collector.setIgnored(CompletionProposal.JAVADOC_PARAM_REF, true); 291 collector.setIgnored(CompletionProposal.JAVADOC_TYPE_REF, true); 292 collector.setIgnored(CompletionProposal.JAVADOC_VALUE_REF, true); 293 collector.setIgnored(CompletionProposal.TYPE_REF, true); 294 295 try { 296 cu.codeComplete(getInvocationOffset(), collector); 297 if (fCoreContext == null) 298 fCoreContext= collector.getContext(); 299 if (fKeywordProposals == null) 300 fKeywordProposals= collector.getKeywordCompletionProposals(); 301 if (fLabelProvider == null) 302 fLabelProvider= collector.getLabelProvider(); 303 } catch (JavaModelException x) { 304 JavaPlugin.log(x); 305 if (fKeywordProposals == null) 306 fKeywordProposals= new IJavaCompletionProposal[0]; 307 } 308 } 309 310 314 } 315 | Popular Tags |