1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.template.contentassist;
13 import java.util.ArrayList;
15 import net.sourceforge.phpdt.core.ICompilationUnit;
17 //import net.sourceforge.phpdt.internal.corext.Assert;
18 import org.eclipse.core.runtime.Assert;
19 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContext;
20 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
21 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
22 import net.sourceforge.phpeclipse.ui.WebUI;
23 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.IRegion;
28 import org.eclipse.jface.text.ITextViewer;
29 import org.eclipse.jface.text.Region;
30 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
31 import org.eclipse.jface.text.templates.Template;
32 import org.eclipse.jface.text.templates.TemplateContextType;
33 import org.eclipse.swt.graphics.Point;
35 public class TemplateEngine {
37 private static final String $_LINE_SELECTION = "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
39 private static final String $_WORD_SELECTION = "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
41 /** The context type. */
42 private TemplateContextType fContextType;
44 /** The result proposals. */
45 private ArrayList fProposals = new ArrayList();
48 * Creates the template engine for a particular context type. See
49 * <code>TemplateContext</code> for supported context types.
51 public TemplateEngine(TemplateContextType contextType) {
52 Assert.isNotNull(contextType);
53 fContextType = contextType;
57 * Empties the collector.
64 * Returns the array of matching templates.
66 public TemplateProposal[] getResults() {
67 return (TemplateProposal[]) fProposals
68 .toArray(new TemplateProposal[fProposals.size()]);
72 * Inspects the context of the compilation unit around
73 * <code>completionPosition</code> and feeds the collector with proposals.
77 * @param completionPosition
78 * the context position in the document of the text viewer
79 * @param compilationUnit
80 * the compilation unit (may be <code>null</code>)
82 public void complete(ITextViewer viewer, int completionPosition,
83 ICompilationUnit compilationUnit) {
84 IDocument document = viewer.getDocument();
86 if (!(fContextType instanceof CompilationUnitContextType))
89 Point selection = viewer.getSelectedRange();
91 // remember selected text
92 String selectedText = null;
93 if (selection.y != 0) {
95 selectedText = document.get(selection.x, selection.y);
96 } catch (BadLocationException e) {
100 CompilationUnitContext context = ((CompilationUnitContextType) fContextType)
101 .createContext(document, completionPosition, selection.y,
103 context.setVariable("selection", selectedText); //$NON-NLS-1$
104 int start = context.getStart();
105 int end = context.getEnd();
106 IRegion region = new Region(start, end - start);
108 Template[] templates = WebUI.getDefault().getTemplateStore()
111 if (selection.y == 0) {
112 for (int i = 0; i != templates.length; i++)
113 if (context.canEvaluate(templates[i]))
114 fProposals.add(new TemplateProposal(templates[i], context,
116 .get(PHPUiImages.IMG_OBJS_TEMPLATE)));
120 if (context.getKey().length() == 0)
121 context.setForceEvaluation(true);
123 boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
125 for (int i = 0; i != templates.length; i++) {
126 Template template = templates[i];
127 if (context.canEvaluate(template)
128 && template.getContextTypeId().equals(
129 context.getContextType().getId())
130 && (!multipleLinesSelected
131 && template.getPattern().indexOf(
132 $_WORD_SELECTION) != -1 || (multipleLinesSelected && template
133 .getPattern().indexOf($_LINE_SELECTION) != -1))) {
134 fProposals.add(new TemplateProposal(templates[i], context,
136 .get(PHPUiImages.IMG_OBJS_TEMPLATE)));
143 * Returns <code>true</code> if one line is completely selected or if
144 * multiple lines are selected. Being completely selected means that all
145 * characters except the new line characters are selected.
147 * @return <code>true</code> if one or multiple lines are selected
150 private boolean areMultipleLinesSelected(ITextViewer viewer) {
154 Point s = viewer.getSelectedRange();
160 IDocument document = viewer.getDocument();
161 int startLine = document.getLineOfOffset(s.x);
162 int endLine = document.getLineOfOffset(s.x + s.y);
163 IRegion line = document.getLineInformation(startLine);
164 return startLine != endLine
165 || (s.x == line.getOffset() && s.y == line.getLength());
167 } catch (BadLocationException x) {