1 11 package org.eclipse.jdt.internal.ui.text.template.preferences; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.jface.text.ITextViewer; 20 import org.eclipse.jface.text.contentassist.ICompletionProposal; 21 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 22 import org.eclipse.jface.text.contentassist.IContextInformation; 23 import org.eclipse.jface.text.contentassist.IContextInformationValidator; 24 import org.eclipse.jface.text.templates.TemplateContextType; 25 import org.eclipse.jface.text.templates.TemplateVariableResolver; 26 27 28 29 public class TemplateVariableProcessor implements IContentAssistProcessor { 30 31 private static Comparator fgTemplateVariableProposalComparator= new Comparator () { 32 public int compare(Object arg0, Object arg1) { 33 TemplateVariableProposal proposal0= (TemplateVariableProposal) arg0; 34 TemplateVariableProposal proposal1= (TemplateVariableProposal) arg1; 35 36 return proposal0.getDisplayString().compareTo(proposal1.getDisplayString()); 37 } 38 39 public boolean equals(Object arg0) { 40 return false; 41 } 42 }; 43 44 45 46 private TemplateContextType fContextType; 47 48 51 public void setContextType(TemplateContextType contextType) { 52 fContextType= contextType; 53 } 54 55 58 public TemplateContextType getContextType() { 59 return fContextType; 60 } 61 62 65 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 66 67 if (fContextType == null) 68 return null; 69 70 List proposals= new ArrayList (); 71 72 String text= viewer.getDocument().get(); 73 int start= getStart(text, documentOffset); 74 int end= documentOffset; 75 76 String string= text.substring(start, end); 77 int colon= string.indexOf(':'); 78 boolean includeBrace= true; 79 int offset= start; 80 String prefix= string; 81 if (colon != -1) { 82 includeBrace= false; 83 offset= start + colon + 1; 84 prefix= string.substring(colon + 1); 85 } else { 86 int escape= string.indexOf("${"); if (escape != -1) { 88 offset= start + escape + 2; 89 includeBrace= false; 90 prefix= string.substring(escape + 2); 91 } 92 } 93 if (prefix.equals("$")) prefix= ""; 96 int length= end - offset; 97 98 for (Iterator iterator= fContextType.resolvers(); iterator.hasNext(); ) { 99 TemplateVariableResolver variable= (TemplateVariableResolver) iterator.next(); 100 101 if (variable.getType().startsWith(prefix)) 102 proposals.add(new TemplateVariableProposal(variable, offset, length, viewer, includeBrace)); 103 } 104 105 Collections.sort(proposals, fgTemplateVariableProposalComparator); 106 return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]); 107 } 108 109 110 private int getStart(String string, int end) { 111 int start= end; 112 113 if (start >= 1 && string.charAt(start - 1) == '$') 114 return start - 1; 115 116 while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1))) 117 start--; 118 119 if (start >= 1 && string.charAt(start - 1) == ':') { 120 start--; 121 while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1))) 122 start--; 123 } 124 125 if (start >= 2 && string.charAt(start - 1) == '{' && string.charAt(start - 2) == '$') 126 return start - 2; 127 128 return end; 129 } 130 131 134 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { 135 return null; 136 } 137 138 141 public char[] getCompletionProposalAutoActivationCharacters() { 142 return new char[] {'$'}; 143 } 144 145 148 public char[] getContextInformationAutoActivationCharacters() { 149 return null; 150 } 151 152 155 public String getErrorMessage() { 156 return null; 157 } 158 159 162 public IContextInformationValidator getContextInformationValidator() { 163 return null; 164 } 165 166 } 167 168 | Popular Tags |