1 11 package org.eclipse.jdt.internal.ui.text.template.contentassist; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Map.Entry; 18 19 import org.eclipse.core.runtime.Assert; 20 21 import org.eclipse.swt.graphics.Point; 22 23 import org.eclipse.jface.text.BadLocationException; 24 import org.eclipse.jface.text.IDocument; 25 import org.eclipse.jface.text.IRegion; 26 import org.eclipse.jface.text.ITextViewer; 27 import org.eclipse.jface.text.Position; 28 import org.eclipse.jface.text.Region; 29 import org.eclipse.jface.text.templates.GlobalTemplateVariables; 30 import org.eclipse.jface.text.templates.Template; 31 import org.eclipse.jface.text.templates.TemplateContextType; 32 33 import org.eclipse.jdt.core.ICompilationUnit; 34 35 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext; 36 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType; 37 38 import org.eclipse.jdt.internal.ui.JavaPlugin; 39 import org.eclipse.jdt.internal.ui.JavaPluginImages; 40 41 public class TemplateEngine { 42 43 private static final String $_LINE_SELECTION= "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; private static final String $_WORD_SELECTION= "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; 46 47 private TemplateContextType fContextType; 48 49 private ArrayList fProposals= new ArrayList (); 50 51 private final Map fPositions= new HashMap (); 52 53 57 public TemplateEngine(TemplateContextType contextType) { 58 Assert.isNotNull(contextType); 59 fContextType= contextType; 60 } 61 62 65 public void reset() { 66 fProposals.clear(); 67 for (Iterator it= fPositions.entrySet().iterator(); it.hasNext();) { 68 Entry entry= (Entry) it.next(); 69 IDocument doc= (IDocument) entry.getKey(); 70 Position position= (Position) entry.getValue(); 71 doc.removePosition(position); 72 } 73 fPositions.clear(); 74 } 75 76 79 public TemplateProposal[] getResults() { 80 return (TemplateProposal[]) fProposals.toArray(new TemplateProposal[fProposals.size()]); 81 } 82 83 90 public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) { 91 IDocument document= viewer.getDocument(); 92 93 if (!(fContextType instanceof CompilationUnitContextType)) 94 return; 95 96 Point selection= viewer.getSelectedRange(); 97 Position position= new Position(completionPosition, selection.y); 98 99 String selectedText= null; 101 if (selection.y != 0) { 102 try { 103 selectedText= document.get(selection.x, selection.y); 104 document.addPosition(position); 105 fPositions.put(document, position); 106 } catch (BadLocationException e) {} 107 } 108 109 CompilationUnitContext context= ((CompilationUnitContextType) fContextType).createContext(document, position, compilationUnit); 110 context.setVariable("selection", selectedText); int start= context.getStart(); 112 int end= context.getEnd(); 113 IRegion region= new Region(start, end - start); 114 115 Template[] templates= JavaPlugin.getDefault().getTemplateStore().getTemplates(); 116 117 if (selection.y == 0) { 118 for (int i= 0; i != templates.length; i++) 119 if (context.canEvaluate(templates[i])) 120 fProposals.add(new TemplateProposal(templates[i], context, region, JavaPluginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE))); 121 122 } else { 123 124 if (context.getKey().length() == 0) 125 context.setForceEvaluation(true); 126 127 boolean multipleLinesSelected= areMultipleLinesSelected(viewer); 128 129 for (int i= 0; i != templates.length; i++) { 130 Template template= templates[i]; 131 if (context.canEvaluate(template) && 132 template.getContextTypeId().equals(context.getContextType().getId()) && 133 (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1))) 134 { 135 fProposals.add(new TemplateProposal(templates[i], context, region, JavaPluginImages.get(JavaPluginImages.IMG_OBJS_TEMPLATE))); 136 } 137 } 138 } 139 } 140 141 149 private boolean areMultipleLinesSelected(ITextViewer viewer) { 150 if (viewer == null) 151 return false; 152 153 Point s= viewer.getSelectedRange(); 154 if (s.y == 0) 155 return false; 156 157 try { 158 159 IDocument document= viewer.getDocument(); 160 int startLine= document.getLineOfOffset(s.x); 161 int endLine= document.getLineOfOffset(s.x + s.y); 162 IRegion line= document.getLineInformation(startLine); 163 return startLine != endLine || (s.x == line.getOffset() && s.y == line.getLength()); 164 165 } catch (BadLocationException x) { 166 return false; 167 } 168 } 169 } 170 | Popular Tags |