2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.template.php;
7 import net.sourceforge.phpdt.internal.corext.template.ContextType;
8 import net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext;
9 import net.sourceforge.phpdt.internal.corext.template.ITemplateEditor;
10 import net.sourceforge.phpdt.internal.corext.template.Template;
11 import net.sourceforge.phpdt.internal.corext.template.TemplateBuffer;
12 import net.sourceforge.phpdt.internal.corext.template.TemplateTranslator;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
19 * A compilation unit context.
21 public class PHPUnitContext extends DocumentTemplateContext {
23 /** The platform default line delimiter. */
24 private static final String PLATFORM_LINE_DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$
26 private static final String specialChars = "$";
27 /** The compilation unit, may be <code>null</code>. */
28 // private final ICompilationUnit fCompilationUnit;
29 protected boolean fForceEvaluation;
31 * Creates a compilation unit context.
33 * @param type the context type.
34 * @param document the document.
35 * @param completionPosition the completion position within the document.
36 * @param compilationUnit the compilation unit (may be <code>null</code>).
38 protected PHPUnitContext(ContextType type, IDocument document, int completionPosition)
39 //,ICompilationUnit compilationUnit)
41 super(type, document, completionPosition, 0);
42 // fCompilationUnit= compilationUnit;
45 protected PHPUnitContext(ContextType type, IDocument document, int completionPosition, int completionLength)
46 //,ICompilationUnit compilationUnit)
48 super(type, document, completionPosition, completionLength);
49 // fCompilationUnit= compilationUnit;
53 * @see TemplateContext#canEvaluate(Template templates)
55 public boolean canEvaluate(Template template) {
56 // return fForceEvaluation ||
57 return template.matches(getKey(), getContextType().getName());
61 * Returns <code>true</code> if template matches the prefix and context,
62 * <code>false</code> otherwise.
64 public boolean canEvaluate(String identifier) {
65 String prefix = getKey();
68 // fContextTypeName.equals(contextTypeName) &&
69 // (prefix.length() != 0) &&
70 identifier.toLowerCase().startsWith(prefix.toLowerCase());
74 * @see TemplateContext#evaluate(Template template)
76 public TemplateBuffer evaluate(Template template) throws CoreException {
77 if (!canEvaluate(template))
80 TemplateTranslator translator = new TemplateTranslator();
81 TemplateBuffer buffer = translator.translate(template.getPattern());
83 getContextType().edit(buffer, this);
85 String lineDelimiter = null;
87 lineDelimiter = getDocument().getLineDelimiter(0);
88 } catch (BadLocationException e) {
91 if (lineDelimiter == null)
92 lineDelimiter = PLATFORM_LINE_DELIMITER;
94 // ITemplateEditor formatter= new PHPFormatter(lineDelimiter);
95 // formatter.edit(buffer, this);
101 * @see DocumentTemplateContext#getCompletionPosition();
103 public int getStart() {
104 IDocument document = getDocument();
106 int start = getCompletionOffset();
108 if ( ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
112 while (((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
113 || ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
117 if (((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
118 || ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
124 } catch (BadLocationException e) {
125 return getCompletionOffset();
130 * Returns the character before start position of completion.
132 public char getCharacterBeforeStart() {
133 int start = getStart();
136 return start == 0 ? ' ' : getDocument().getChar(start - 1);
138 } catch (BadLocationException e) {
144 * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
146 // public final ICompilationUnit getCompilationUnit() {
147 // return fCompilationUnit;
151 * Returns the enclosing element of a particular element type, <code>null</code>
152 * if no enclosing element of that type exists.
154 // public IJavaElement findEnclosingElement(int elementType) {
155 // if (fCompilationUnit == null)
159 // IJavaElement element= fCompilationUnit.getElementAt(getStart());
160 // while (element != null && element.getElementType() != elementType)
161 // element= element.getParent();
165 // } catch (JavaModelException e) {
173 public void setForceEvaluation(boolean b) {
174 fForceEvaluation = b;