1 11 package org.eclipse.jdt.internal.ui.text.java; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collections ; 16 import java.util.HashSet ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.core.runtime.IProgressMonitor; 21 22 23 import org.eclipse.jface.text.BadLocationException; 24 import org.eclipse.jface.text.TextUtilities; 25 import org.eclipse.jface.text.templates.TemplateContextType; 26 27 import org.eclipse.jdt.core.ICompilationUnit; 28 29 import org.eclipse.jdt.internal.corext.template.java.JavaContextType; 30 import org.eclipse.jdt.internal.corext.template.java.JavaDocContextType; 31 32 import org.eclipse.jdt.ui.text.IJavaPartitions; 33 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer; 34 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; 35 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext; 36 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext; 37 38 import org.eclipse.jdt.internal.ui.JavaPlugin; 39 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateEngine; 40 import org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal; 41 42 46 public final class TemplateCompletionProposalComputer implements IJavaCompletionProposalComputer { 47 48 private final TemplateEngine fJavaTemplateEngine; 49 private final TemplateEngine fJavadocTemplateEngine; 50 51 public TemplateCompletionProposalComputer() { 52 TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(JavaContextType.NAME); 53 if (contextType == null) { 54 contextType= new JavaContextType(); 55 JavaPlugin.getDefault().getTemplateContextRegistry().addContextType(contextType); 56 } 57 if (contextType != null) 58 fJavaTemplateEngine= new TemplateEngine(contextType); 59 else 60 fJavaTemplateEngine= null; 61 contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType("javadoc"); if (contextType == null) { 63 contextType= new JavaDocContextType(); 64 JavaPlugin.getDefault().getTemplateContextRegistry().addContextType(contextType); 65 } 66 if (contextType != null) 67 fJavadocTemplateEngine= new TemplateEngine(contextType); 68 else 69 fJavadocTemplateEngine= null; 70 } 71 72 75 public List computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) { 76 TemplateEngine engine; 77 try { 78 String partition= TextUtilities.getContentType(context.getDocument(), IJavaPartitions.JAVA_PARTITIONING, context.getInvocationOffset(), true); 79 if (partition.equals(IJavaPartitions.JAVA_DOC)) 80 engine= fJavadocTemplateEngine; 81 else 82 engine= fJavaTemplateEngine; 83 } catch (BadLocationException x) { 84 return Collections.EMPTY_LIST; 85 } 86 87 if (engine != null) { 88 if (!(context instanceof JavaContentAssistInvocationContext)) 89 return Collections.EMPTY_LIST; 90 91 JavaContentAssistInvocationContext javaContext= (JavaContentAssistInvocationContext) context; 92 ICompilationUnit unit= javaContext.getCompilationUnit(); 93 if (unit == null) 94 return Collections.EMPTY_LIST; 95 96 engine.reset(); 97 engine.complete(javaContext.getViewer(), javaContext.getInvocationOffset(), unit); 98 99 TemplateProposal[] templateProposals= engine.getResults(); 100 List result= new ArrayList (Arrays.asList(templateProposals)); 101 102 IJavaCompletionProposal[] keyWordResults= javaContext.getKeywordProposals(); 103 if (keyWordResults.length > 0) { 104 List removals= new ArrayList (); 105 106 if (keyWordResults.length > 0) { 112 outer: for (int k= 0; k < templateProposals.length; k++) { 113 TemplateProposal curr= templateProposals[k]; 114 String name= curr.getTemplate().getName(); 115 for (int i= 0; i < keyWordResults.length; i++) { 116 String keyword= keyWordResults[i].getDisplayString(); 117 if (name.startsWith(keyword)) { 118 curr.setRelevance(keyWordResults[i].getRelevance() + 1); 119 continue outer; 120 } 121 } 122 if (isKeyword(name)) 123 removals.add(curr); 124 } 125 } 126 127 result.removeAll(removals); 128 } 129 return result; 130 } 131 132 return Collections.EMPTY_LIST; 133 } 134 135 138 public List computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) { 139 return Collections.EMPTY_LIST; 140 } 141 142 private static final Set KEYWORDS; 143 static { 144 Set keywords= new HashSet (42); 145 keywords.add("abstract"); keywords.add("assert"); keywords.add("break"); keywords.add("case"); keywords.add("catch"); keywords.add("class"); keywords.add("continue"); keywords.add("default"); keywords.add("do"); keywords.add("else"); keywords.add("elseif"); keywords.add("extends"); keywords.add("final"); keywords.add("finally"); keywords.add("for"); keywords.add("if"); keywords.add("implements"); keywords.add("import"); keywords.add("instanceof"); keywords.add("interface"); keywords.add("native"); keywords.add("new"); keywords.add("package"); keywords.add("private"); keywords.add("protected"); keywords.add("public"); keywords.add("return"); keywords.add("static"); keywords.add("strictfp"); keywords.add("super"); keywords.add("switch"); keywords.add("synchronized"); keywords.add("this"); keywords.add("throw"); keywords.add("throws"); keywords.add("transient"); keywords.add("try"); keywords.add("volatile"); keywords.add("while"); keywords.add("true"); keywords.add("false"); keywords.add("null"); KEYWORDS= Collections.unmodifiableSet(keywords); 188 } 189 190 private boolean isKeyword(String name) { 191 return KEYWORDS.contains(name); 192 } 193 194 197 public String getErrorMessage() { 198 return null; 199 } 200 201 204 public void sessionStarted() { 205 } 206 207 210 public void sessionEnded() { 211 fJavadocTemplateEngine.reset(); 212 fJavaTemplateEngine.reset(); 213 } 214 } 215 | Popular Tags |