1 11 package org.eclipse.jface.text.templates; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 import java.util.List ; 17 18 import org.eclipse.swt.graphics.Image; 19 20 import org.eclipse.jface.text.BadLocationException; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.IRegion; 23 import org.eclipse.jface.text.ITextSelection; 24 import org.eclipse.jface.text.ITextViewer; 25 import org.eclipse.jface.text.Region; 26 import org.eclipse.jface.text.contentassist.ICompletionProposal; 27 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 28 import org.eclipse.jface.text.contentassist.IContextInformation; 29 import org.eclipse.jface.text.contentassist.IContextInformationValidator; 30 31 32 39 public abstract class TemplateCompletionProcessor implements IContentAssistProcessor { 40 41 private static final class ProposalComparator implements Comparator { 42 public int compare(Object o1, Object o2) { 43 return ((TemplateProposal) o2).getRelevance() - ((TemplateProposal) o1).getRelevance(); 44 } 45 } 46 47 private static final Comparator fgProposalComparator= new ProposalComparator(); 48 49 53 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { 54 55 ITextSelection selection= (ITextSelection) viewer.getSelectionProvider().getSelection(); 56 57 if (selection.getOffset() == offset) 59 offset= selection.getOffset() + selection.getLength(); 60 61 String prefix= extractPrefix(viewer, offset); 62 Region region= new Region(offset - prefix.length(), prefix.length()); 63 TemplateContext context= createContext(viewer, region); 64 if (context == null) 65 return new ICompletionProposal[0]; 66 67 context.setVariable("selection", selection.getText()); 69 Template[] templates= getTemplates(context.getContextType().getId()); 70 71 List matches= new ArrayList (); 72 for (int i= 0; i < templates.length; i++) { 73 Template template= templates[i]; 74 try { 75 context.getContextType().validate(template.getPattern()); 76 } catch (TemplateException e) { 77 continue; 78 } 79 if (template.matches(prefix, context.getContextType().getId())) 80 matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix))); 81 } 82 83 Collections.sort(matches, fgProposalComparator); 84 85 return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]); 86 } 87 88 104 protected ICompletionProposal createProposal(Template template, TemplateContext context, Region region, int relevance) { 105 return createProposal(template, context, (IRegion) region, relevance); 106 } 107 108 123 protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region, int relevance) { 124 return new TemplateProposal(template, context, region, getImage(template), relevance); 125 } 126 127 133 protected abstract Template[] getTemplates(String contextTypeId); 134 135 144 protected TemplateContext createContext(ITextViewer viewer, IRegion region) { 145 TemplateContextType contextType= getContextType(viewer, region); 146 if (contextType != null) { 147 IDocument document= viewer.getDocument(); 148 return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength()); 149 } 150 return null; 151 } 152 153 161 protected abstract TemplateContextType getContextType(ITextViewer viewer, IRegion region); 162 163 173 protected int getRelevance(Template template, String prefix) { 174 if (template.getName().startsWith(prefix)) 175 return 90; 176 return 0; 177 } 178 179 189 protected String extractPrefix(ITextViewer viewer, int offset) { 190 int i= offset; 191 IDocument document= viewer.getDocument(); 192 if (i > document.getLength()) 193 return ""; 195 try { 196 while (i > 0) { 197 char ch= document.getChar(i - 1); 198 if (!Character.isJavaIdentifierPart(ch)) 199 break; 200 i--; 201 } 202 203 return document.get(i, offset - i); 204 } catch (BadLocationException e) { 205 return ""; } 207 } 208 209 215 protected abstract Image getImage(Template template); 216 217 220 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { 221 return null; 222 } 223 224 227 public char[] getCompletionProposalAutoActivationCharacters() { 228 return null; 229 } 230 231 234 public char[] getContextInformationAutoActivationCharacters() { 235 return null; 236 } 237 238 241 public String getErrorMessage() { 242 return null; 243 } 244 245 248 public IContextInformationValidator getContextInformationValidator() { 249 return null; 250 } 251 } 252 | Popular Tags |