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;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
17 * A compilation unit context.
19 public class HTMLUnitContext extends DocumentTemplateContext {
21 /** The platform default line delimiter. */
22 private static final String PLATFORM_LINE_DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$
24 private static final String specialChars = "$&<";
25 /** The compilation unit, may be <code>null</code>. */
26 // private final ICompilationUnit fCompilationUnit;
29 * Creates a compilation unit context.
31 * @param type the context type.
32 * @param document the document.
33 * @param completionPosition the completion position within the document.
34 * @param compilationUnit the compilation unit (may be <code>null</code>).
36 protected HTMLUnitContext(ContextType type, IDocument document, int completionPosition)
37 //,ICompilationUnit compilationUnit)
39 super(type, document, completionPosition);
40 // fCompilationUnit= compilationUnit;
44 * @see TemplateContext#canEvaluate(Template templates)
46 public boolean canEvaluate(Template template) {
47 // return fForceEvaluation ||
48 return template.matches(getKey(), getContextType().getName());
52 * Returns <code>true</code> if template matches the prefix and context,
53 * <code>false</code> otherwise.
55 public boolean canEvaluate(String identifier) {
56 String prefix = getKey();
59 // fContextTypeName.equals(contextTypeName) &&
60 (prefix.length() != 0) && identifier.toLowerCase().startsWith(prefix.toLowerCase());
64 * @see TemplateContext#evaluate(Template template)
66 public TemplateBuffer evaluate(Template template) throws CoreException {
67 if (!canEvaluate(template))
70 TemplateTranslator translator = new TemplateTranslator();
71 TemplateBuffer buffer = translator.translate(template.getPattern());
73 getContextType().edit(buffer, this);
75 String lineDelimiter = null;
77 lineDelimiter = getDocument().getLineDelimiter(0);
78 } catch (BadLocationException e) {
81 if (lineDelimiter == null)
82 lineDelimiter = PLATFORM_LINE_DELIMITER;
84 // ITemplateEditor formatter= new JavaFormatter(lineDelimiter);
85 // formatter.edit(buffer, this);
91 * @see DocumentTemplateContext#getCompletionPosition();
93 public int getStart() {
94 IDocument document = getDocument();
96 int start = getCompletionPosition();
98 while (((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
99 || ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
103 if (((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
104 || ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1) )) {
110 } catch (BadLocationException e) {
111 return getCompletionPosition();
116 * Returns the character before start position of completion.
118 public char getCharacterBeforeStart() {
119 int start = getStart();
122 return start == 0 ? ' ' : getDocument().getChar(start - 1);
124 } catch (BadLocationException e) {
129 * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
131 // public final ICompilationUnit getCompilationUnit() {
132 // return fCompilationUnit;
136 * Returns the enclosing element of a particular element type, <code>null</code>
137 * if no enclosing element of that type exists.
139 // public IJavaElement findEnclosingElement(int elementType) {
140 // if (fCompilationUnit == null)
144 // IJavaElement element= fCompilationUnit.getElementAt(getStart());
145 // while (element != null && element.getElementType() != elementType)
146 // element= element.getParent();
150 // } catch (JavaModelException e) {