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.Template;
10 import net.sourceforge.phpdt.internal.corext.template.TemplateBuffer;
11 import net.sourceforge.phpdt.internal.corext.template.TemplateTranslator;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
18 * A compilation unit context.
20 public class HTMLUnitContext extends DocumentTemplateContext {
22 /** The platform default line delimiter. */
23 private static final String PLATFORM_LINE_DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$
25 private static final String specialChars = "&<#";
26 /** The compilation unit, may be <code>null</code>. */
27 // private final ICompilationUnit fCompilationUnit;
30 * Creates a compilation unit context.
32 * @param type the context type.
33 * @param document the document.
34 * @param completionPosition the completion position within the document.
35 * @param compilationUnit the compilation unit (may be <code>null</code>).
37 protected HTMLUnitContext(ContextType type, IDocument document, int completionPosition)
38 //,ICompilationUnit compilationUnit)
40 super(type, document, completionPosition, 0);
41 // fCompilationUnit= compilationUnit;
45 * @see TemplateContext#canEvaluate(Template templates)
47 public boolean canEvaluate(Template template) {
48 // return fForceEvaluation ||
49 return template.matches(getKey(), getContextType().getName());
53 * Returns <code>true</code> if template matches the prefix and context,
54 * <code>false</code> otherwise.
56 public boolean canEvaluate(String identifier) {
57 String prefix = getKey();
60 // fContextTypeName.equals(contextTypeName) &&
61 (prefix.length() != 0) && identifier.toLowerCase().startsWith(prefix.toLowerCase());
65 * @see TemplateContext#evaluate(Template template)
67 public TemplateBuffer evaluate(Template template) throws CoreException {
68 if (!canEvaluate(template))
71 TemplateTranslator translator = new TemplateTranslator();
72 TemplateBuffer buffer = translator.translate(template.getPattern());
74 getContextType().edit(buffer, this);
76 String lineDelimiter = null;
78 lineDelimiter = getDocument().getLineDelimiter(0);
79 } catch (BadLocationException e) {
82 if (lineDelimiter == null)
83 lineDelimiter = PLATFORM_LINE_DELIMITER;
85 // ITemplateEditor formatter= new JavaFormatter(lineDelimiter);
86 // formatter.edit(buffer, this);
92 * @see DocumentTemplateContext#getCompletionPosition();
94 public int getStart() {
95 IDocument document = getDocument();
97 int start = getCompletionOffset();
99 while (((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
100 || ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
104 if (((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
105 || ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
111 } catch (BadLocationException e) {
112 return getCompletionOffset();
117 * Returns the character before start position of completion.
119 public char getCharacterBeforeStart() {
120 int start = getStart();
123 return start == 0 ? ' ' : getDocument().getChar(start - 1);
125 } catch (BadLocationException e) {
130 * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
132 // public final ICompilationUnit getCompilationUnit() {
133 // return fCompilationUnit;
137 * Returns the enclosing element of a particular element type, <code>null</code>
138 * if no enclosing element of that type exists.
140 // public IJavaElement findEnclosingElement(int elementType) {
141 // if (fCompilationUnit == null)
145 // IJavaElement element= fCompilationUnit.getElementAt(getStart());
146 // while (element != null && element.getElementType() != elementType)
147 // element= element.getParent();
151 // } catch (JavaModelException e) {