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.PHPeclipsePlugin;
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.jface.text.ITextViewer;
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 import org.eclipse.swt.graphics.Point;
34 public class TemplateEngine {
36 private static final String $_LINE_SELECTION = "${" + GlobalTemplateVariables.LineSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
38 private static final String $_WORD_SELECTION = "${" + GlobalTemplateVariables.WordSelection.NAME + "}"; //$NON-NLS-1$ //$NON-NLS-2$
40 /** The context type. */
41 private TemplateContextType fContextType;
43 /** The result proposals. */
44 private ArrayList fProposals = new ArrayList();
47 * Creates the template engine for a particular context type. See
48 * <code>TemplateContext</code> for supported context types.
50 public TemplateEngine(TemplateContextType contextType) {
51 Assert.isNotNull(contextType);
52 fContextType = contextType;
56 * Empties the collector.
63 * Returns the array of matching templates.
65 public TemplateProposal[] getResults() {
66 return (TemplateProposal[]) fProposals
67 .toArray(new TemplateProposal[fProposals.size()]);
71 * Inspects the context of the compilation unit around
72 * <code>completionPosition</code> and feeds the collector with proposals.
76 * @param completionPosition
77 * the context position in the document of the text viewer
78 * @param compilationUnit
79 * the compilation unit (may be <code>null</code>)
81 public void complete(ITextViewer viewer, int completionPosition,
82 ICompilationUnit compilationUnit) {
83 IDocument document = viewer.getDocument();
85 if (!(fContextType instanceof CompilationUnitContextType))
88 Point selection = viewer.getSelectedRange();
90 // remember selected text
91 String selectedText = null;
92 if (selection.y != 0) {
94 selectedText = document.get(selection.x, selection.y);
95 } catch (BadLocationException e) {
99 CompilationUnitContext context = ((CompilationUnitContextType) fContextType)
100 .createContext(document, completionPosition, selection.y,
102 context.setVariable("selection", selectedText); //$NON-NLS-1$
103 int start = context.getStart();
104 int end = context.getEnd();
105 IRegion region = new Region(start, end - start);
107 Template[] templates = PHPeclipsePlugin.getDefault().getTemplateStore()
110 if (selection.y == 0) {
111 for (int i = 0; i != templates.length; i++)
112 if (context.canEvaluate(templates[i]))
113 fProposals.add(new TemplateProposal(templates[i], context,
115 .get(PHPUiImages.IMG_OBJS_TEMPLATE)));
119 if (context.getKey().length() == 0)
120 context.setForceEvaluation(true);
122 boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
124 for (int i = 0; i != templates.length; i++) {
125 Template template = templates[i];
126 if (context.canEvaluate(template)
127 && template.getContextTypeId().equals(
128 context.getContextType().getId())
129 && (!multipleLinesSelected
130 && template.getPattern().indexOf(
131 $_WORD_SELECTION) != -1 || (multipleLinesSelected && template
132 .getPattern().indexOf($_LINE_SELECTION) != -1))) {
133 fProposals.add(new TemplateProposal(templates[i], context,
135 .get(PHPUiImages.IMG_OBJS_TEMPLATE)));
142 * Returns <code>true</code> if one line is completely selected or if
143 * multiple lines are selected. Being completely selected means that all
144 * characters except the new line characters are selected.
146 * @return <code>true</code> if one or multiple lines are selected
149 private boolean areMultipleLinesSelected(ITextViewer viewer) {
153 Point s = viewer.getSelectedRange();
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
164 || (s.x == line.getOffset() && s.y == line.getLength());
166 } catch (BadLocationException x) {