public class TemplateEngine {
- private static final String $_LINE_SELECTION= "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
- private static final String $_WORD_SELECTION= "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
+ private static final String $_LINE_SELECTION = "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
+
+ private static final String $_WORD_SELECTION = "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
/** The context type. */
private TemplateContextType fContextType;
+
/** The result proposals. */
- private ArrayList fProposals= new ArrayList();
+ private ArrayList fProposals = new ArrayList();
/**
- * Creates the template engine for a particular context type.
- * See <code>TemplateContext</code> for supported context types.
+ * Creates the template engine for a particular context type. See
+ * <code>TemplateContext</code> for supported context types.
*/
public TemplateEngine(TemplateContextType contextType) {
Assert.isNotNull(contextType);
- fContextType= contextType;
+ fContextType = contextType;
}
/**
* Returns the array of matching templates.
*/
public TemplateProposal[] getResults() {
- return (TemplateProposal[]) fProposals.toArray(new TemplateProposal[fProposals.size()]);
+ return (TemplateProposal[]) fProposals
+ .toArray(new TemplateProposal[fProposals.size()]);
}
/**
- * Inspects the context of the compilation unit around <code>completionPosition</code>
- * and feeds the collector with proposals.
- * @param viewer the text viewer
- * @param completionPosition the context position in the document of the text viewer
- * @param compilationUnit the compilation unit (may be <code>null</code>)
+ * Inspects the context of the compilation unit around
+ * <code>completionPosition</code> and feeds the collector with proposals.
+ *
+ * @param viewer
+ * the text viewer
+ * @param completionPosition
+ * the context position in the document of the text viewer
+ * @param compilationUnit
+ * the compilation unit (may be <code>null</code>)
*/
- public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
- IDocument document= viewer.getDocument();
+ public void complete(ITextViewer viewer, int completionPosition,
+ ICompilationUnit compilationUnit) {
+ IDocument document = viewer.getDocument();
if (!(fContextType instanceof CompilationUnitContextType))
return;
- Point selection= viewer.getSelectedRange();
+ Point selection = viewer.getSelectedRange();
// remember selected text
- String selectedText= null;
+ String selectedText = null;
if (selection.y != 0) {
try {
- selectedText= document.get(selection.x, selection.y);
- } catch (BadLocationException e) {}
+ selectedText = document.get(selection.x, selection.y);
+ } catch (BadLocationException e) {
+ }
}
-
- CompilationUnitContext context= ((CompilationUnitContextType) fContextType).createContext(document, completionPosition, selection.y, compilationUnit);
+ CompilationUnitContext context = ((CompilationUnitContextType) fContextType)
+ .createContext(document, completionPosition, selection.y,
+ compilationUnit);
context.setVariable("selection", selectedText); //$NON-NLS-1$
- int start= context.getStart();
- int end= context.getEnd();
- IRegion region= new Region(start, end - start);
+ int start = context.getStart();
+ int end = context.getEnd();
+ IRegion region = new Region(start, end - start);
- Template[] templates= PHPeclipsePlugin.getDefault().getTemplateStore().getTemplates();
+ Template[] templates = PHPeclipsePlugin.getDefault().getTemplateStore()
+ .getTemplates();
if (selection.y == 0) {
- for (int i= 0; i != templates.length; i++)
+ for (int i = 0; i != templates.length; i++)
if (context.canEvaluate(templates[i]))
- fProposals.add(new TemplateProposal(templates[i], context, region, PHPUiImages.get(PHPUiImages.IMG_OBJS_TEMPLATE)));
+ fProposals.add(new TemplateProposal(templates[i], context,
+ region, PHPUiImages
+ .get(PHPUiImages.IMG_OBJS_TEMPLATE)));
} else {
if (context.getKey().length() == 0)
context.setForceEvaluation(true);
- boolean multipleLinesSelected= areMultipleLinesSelected(viewer);
-
- for (int i= 0; i != templates.length; i++) {
- Template template= templates[i];
- if (context.canEvaluate(template) &&
- template.getContextTypeId().equals(context.getContextType().getId()) &&
- (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1)))
- {
- fProposals.add(new TemplateProposal(templates[i], context, region, PHPUiImages.get(PHPUiImages.IMG_OBJS_TEMPLATE)));
+ boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
+
+ for (int i = 0; i != templates.length; i++) {
+ Template template = templates[i];
+ if (context.canEvaluate(template)
+ && template.getContextTypeId().equals(
+ context.getContextType().getId())
+ && (!multipleLinesSelected
+ && template.getPattern().indexOf(
+ $_WORD_SELECTION) != -1 || (multipleLinesSelected && template
+ .getPattern().indexOf($_LINE_SELECTION) != -1))) {
+ fProposals.add(new TemplateProposal(templates[i], context,
+ region, PHPUiImages
+ .get(PHPUiImages.IMG_OBJS_TEMPLATE)));
}
}
}
}
/**
- * Returns <code>true</code> if one line is completely selected or if multiple lines are selected.
- * Being completely selected means that all characters except the new line characters are
- * selected.
- *
+ * Returns <code>true</code> if one line is completely selected or if
+ * multiple lines are selected. Being completely selected means that all
+ * characters except the new line characters are selected.
+ *
* @return <code>true</code> if one or multiple lines are selected
* @since 2.1
*/
if (viewer == null)
return false;
- Point s= viewer.getSelectedRange();
+ Point s = viewer.getSelectedRange();
if (s.y == 0)
return false;
try {
- IDocument document= viewer.getDocument();
- int startLine= document.getLineOfOffset(s.x);
- int endLine= document.getLineOfOffset(s.x + s.y);
- IRegion line= document.getLineInformation(startLine);
- return startLine != endLine || (s.x == line.getOffset() && s.y == line.getLength());
+ IDocument document = viewer.getDocument();
+ int startLine = document.getLineOfOffset(s.x);
+ int endLine = document.getLineOfOffset(s.x + s.y);
+ IRegion line = document.getLineInformation(startLine);
+ return startLine != endLine
+ || (s.x == line.getOffset() && s.y == line.getLength());
} catch (BadLocationException x) {
return false;