1 11 package org.eclipse.ui.texteditor.templates; 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 38 final class TemplateVariableProcessor implements IContentAssistProcessor { 39 40 private static Comparator fgTemplateVariableProposalComparator= new Comparator () { 41 public int compare(Object arg0, Object arg1) { 42 TemplateVariableProposal proposal0= (TemplateVariableProposal) arg0; 43 TemplateVariableProposal proposal1= (TemplateVariableProposal) arg1; 44 45 return proposal0.getDisplayString().compareTo(proposal1.getDisplayString()); 46 } 47 48 51 public boolean equals(Object arg0) { 52 return false; 53 } 54 55 59 public int hashCode() { 60 return super.hashCode(); 61 } 62 }; 63 64 65 66 private TemplateContextType fContextType; 67 68 73 public void setContextType(TemplateContextType contextType) { 74 fContextType= contextType; 75 } 76 77 82 public TemplateContextType getContextType() { 83 return fContextType; 84 } 85 86 89 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { 90 91 if (fContextType == null) 92 return null; 93 94 List proposals= new ArrayList (); 95 96 String text= viewer.getDocument().get(); 97 int start= getStart(text, documentOffset); 98 int end= documentOffset; 99 100 String string= text.substring(start, end); 101 String prefix= (string.length() >= 2) 102 ? string.substring(2) 103 : null; 104 105 int offset= start; 106 int length= end - start; 107 108 for (Iterator iterator= fContextType.resolvers(); iterator.hasNext(); ) { 109 TemplateVariableResolver variable= (TemplateVariableResolver) iterator.next(); 110 111 if (prefix == null || variable.getType().startsWith(prefix)) 112 proposals.add(new TemplateVariableProposal(variable, offset, length, viewer)); 113 } 114 115 Collections.sort(proposals, fgTemplateVariableProposalComparator); 116 return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]); 117 } 118 119 120 private int getStart(String string, int end) { 121 int start= end; 122 123 if (start >= 1 && string.charAt(start - 1) == '$') 124 return start - 1; 125 126 while ((start != 0) && Character.isUnicodeIdentifierPart(string.charAt(start - 1))) 127 start--; 128 129 if (start >= 2 && string.charAt(start - 1) == '{' && string.charAt(start - 2) == '$') 130 return start - 2; 131 132 return end; 133 } 134 135 138 public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) { 139 return null; 140 } 141 142 145 public char[] getCompletionProposalAutoActivationCharacters() { 146 return new char[] {'$'}; 147 } 148 149 152 public char[] getContextInformationAutoActivationCharacters() { 153 return null; 154 } 155 156 159 public String getErrorMessage() { 160 return null; 161 } 162 163 166 public IContextInformationValidator getContextInformationValidator() { 167 return null; 168 } 169 170 } 171 172 | Popular Tags |