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;
16 import net.sourceforge.phpdt.internal.corext.Assert;
17 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContext;
18 import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
19 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
20 import net.sourceforge.phpeclipse.ui.WebUI;
21 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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.Region;
28 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
29 import org.eclipse.jface.text.templates.Template;
30 import org.eclipse.jface.text.templates.TemplateContextType;
31 import org.eclipse.swt.graphics.Point;
33 public class TemplateEngine {
35 private static final String $_LINE_SELECTION = "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
37 private static final String $_WORD_SELECTION = "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
39 /** The context type. */
40 private TemplateContextType fContextType;
42 /** The result proposals. */
43 private ArrayList fProposals = new ArrayList();
46 * Creates the template engine for a particular context type. See
47 * <code>TemplateContext</code> for supported context types.
49 public TemplateEngine(TemplateContextType contextType) {
50 Assert.isNotNull(contextType);
51 fContextType = contextType;
55 * Empties the collector.
62 * Returns the array of matching templates.
64 public TemplateProposal[] getResults() {
65 return (TemplateProposal[]) fProposals
66 .toArray(new TemplateProposal[fProposals.size()]);
70 * Inspects the context of the compilation unit around
71 * <code>completionPosition</code> and feeds the collector with proposals.
75 * @param completionPosition
76 * the context position in the document of the text viewer
77 * @param compilationUnit
78 * the compilation unit (may be <code>null</code>)
80 public void complete(ITextViewer viewer, int completionPosition,
81 ICompilationUnit compilationUnit) {
82 IDocument document = viewer.getDocument();
84 if (!(fContextType instanceof CompilationUnitContextType))
87 Point selection = viewer.getSelectedRange();
89 // remember selected text
90 String selectedText = null;
91 if (selection.y != 0) {
93 selectedText = document.get(selection.x, selection.y);
94 } catch (BadLocationException e) {
98 CompilationUnitContext context = ((CompilationUnitContextType) fContextType)
99 .createContext(document, completionPosition, selection.y,
101 context.setVariable("selection", selectedText); //$NON-NLS-1$
102 int start = context.getStart();
103 int end = context.getEnd();
104 IRegion region = new Region(start, end - start);
106 Template[] templates = WebUI.getDefault().getTemplateStore()
109 if (selection.y == 0) {
110 for (int i = 0; i != templates.length; i++)
111 if (context.canEvaluate(templates[i]))
112 fProposals.add(new TemplateProposal(templates[i], context,
114 .get(PHPUiImages.IMG_OBJS_TEMPLATE)));
118 if (context.getKey().length() == 0)
119 context.setForceEvaluation(true);
121 boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
123 for (int i = 0; i != templates.length; i++) {
124 Template template = templates[i];
125 if (context.canEvaluate(template)
126 && template.getContextTypeId().equals(
127 context.getContextType().getId())
128 && (!multipleLinesSelected
129 && template.getPattern().indexOf(
130 $_WORD_SELECTION) != -1 || (multipleLinesSelected && template
131 .getPattern().indexOf($_LINE_SELECTION) != -1))) {
132 fProposals.add(new TemplateProposal(templates[i], context,
134 .get(PHPUiImages.IMG_OBJS_TEMPLATE)));
141 * Returns <code>true</code> if one line is completely selected or if
142 * multiple lines are selected. Being completely selected means that all
143 * characters except the new line characters are selected.
145 * @return <code>true</code> if one or multiple lines are selected
148 private boolean areMultipleLinesSelected(ITextViewer viewer) {
152 Point s = viewer.getSelectedRange();
158 IDocument document = viewer.getDocument();
159 int startLine = document.getLineOfOffset(s.x);
160 int endLine = document.getLineOfOffset(s.x + s.y);
161 IRegion line = document.getLineInformation(startLine);
162 return startLine != endLine
163 || (s.x == line.getOffset() && s.y == line.getLength());
165 } catch (BadLocationException x) {