Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / php / HTMLUnitContext.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.template.php;
6
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
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16
17 /**
18  * A compilation unit context.
19  */
20 public class HTMLUnitContext extends DocumentTemplateContext {
21
22   /** The platform default line delimiter. */
23   private static final String PLATFORM_LINE_DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$
24
25   /** special characters
26    * '&' for the start of HTML entities
27    * '<' for the start of HTML tags
28    * '#' for the start of colour attributes
29    * '{' for the start of smarty partitions inside HTML code
30    */
31   private static final String specialChars = "&<#{";
32
33   /** The compilation unit, may be <code>null</code>. */
34   //    private final ICompilationUnit fCompilationUnit;
35   protected boolean fForceEvaluation;
36   /**
37    * Creates a compilation unit context.
38    * 
39    * @param type   the context type.
40    * @param document the document.
41    * @param completionPosition the completion position within the document.
42    * @param compilationUnit the compilation unit (may be <code>null</code>).
43    */
44   protected HTMLUnitContext(ContextType type, IDocument document, int completionPosition)
45   //,ICompilationUnit compilationUnit)
46   {
47     super(type, document, completionPosition, 0);
48     //  fCompilationUnit= compilationUnit;
49   }
50
51   /*
52   * @see TemplateContext#canEvaluate(Template templates)
53   */
54   public boolean canEvaluate(Template template) {
55     // return fForceEvaluation || 
56     return template.matches(getKey(), getContextType().getName());
57   }
58
59   /**
60    * Returns <code>true</code> if template matches the prefix and context,
61    * <code>false</code> otherwise.
62    */
63   public boolean canEvaluate(String identifier) {
64     String prefix = getKey();
65     return
66     //      fEnabled &&
67     //      fContextTypeName.equals(contextTypeName) &&
68      (prefix.length() != 0) && identifier.toLowerCase().startsWith(prefix.toLowerCase());
69   }
70
71   /*
72   * @see TemplateContext#evaluate(Template template)
73   */
74   public TemplateBuffer evaluate(Template template) throws CoreException {
75     if (!canEvaluate(template))
76       return null;
77
78     TemplateTranslator translator = new TemplateTranslator();
79     TemplateBuffer buffer = translator.translate(template.getPattern());
80
81     getContextType().edit(buffer, this);
82
83     String lineDelimiter = null;
84     try {
85       lineDelimiter = getDocument().getLineDelimiter(0);
86     } catch (BadLocationException e) {
87     }
88
89     if (lineDelimiter == null)
90       lineDelimiter = PLATFORM_LINE_DELIMITER;
91
92     //    ITemplateEditor formatter= new JavaFormatter(lineDelimiter);
93     //    formatter.edit(buffer, this);
94
95     return buffer;
96   }
97
98   /*
99    * @see DocumentTemplateContext#getCompletionPosition();
100    */
101   public int getStart() {
102     IDocument document = getDocument();
103     try {
104       int start = getCompletionOffset();
105       char ch = ' ';
106       while (start != 0) {
107         ch = document.getChar(start - 1);
108         if (specialChars.indexOf(ch) != (-1)) {
109           return --start;
110         }
111         if (Character.isUnicodeIdentifierPart(ch)) {
112           start--;
113         } else {
114           break;
115         }
116       }
117       if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1))) {
118         start--;
119         if ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1)) {
120           start--;
121         }
122       }
123
124       //      while (((start != 0)
125       //        && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
126       //        || ((start != 0)
127       //          && specialChars.indexOf(document.getChar(start - 1)) != (-1))) {
128       //        start--;
129       //      }
130       //
131       //if (((start != 0)
132       //        && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
133       //        || ((start != 0)
134       //                && specialChars.indexOf(document.getChar(start - 1)) != (-1))) {
135       //        start--;
136       //}
137
138       return start;
139
140     } catch (BadLocationException e) {
141       return getCompletionOffset();
142     }
143   }
144
145   /**
146    * Returns the character before start position of completion.
147    */
148   public char getCharacterBeforeStart() {
149     int start = getStart();
150
151     try {
152       return start == 0 ? ' ' : getDocument().getChar(start - 1);
153
154     } catch (BadLocationException e) {
155       return ' ';
156     }
157   }
158   /**
159    * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
160    */
161   //    public final ICompilationUnit getCompilationUnit() {
162   //            return fCompilationUnit;
163   //    }
164
165   /**
166    * Returns the enclosing element of a particular element type, <code>null</code>
167    * if no enclosing element of that type exists.
168    */
169   //    public IJavaElement findEnclosingElement(int elementType) {
170   //            if (fCompilationUnit == null)
171   //                    return null;
172   //
173   //            try {
174   //                    IJavaElement element= fCompilationUnit.getElementAt(getStart());
175   //                    while (element != null && element.getElementType() != elementType)
176   //                            element= element.getParent();
177   //                    
178   //                    return element;
179   //
180   //            } catch (JavaModelException e) {
181   //                    return null;
182   //            }       
183   //    }
184   /**
185    * Forces evaluation.
186    */
187   public void setForceEvaluation(boolean evaluate) {
188     fForceEvaluation = evaluate;
189   }
190 }