improved auto trigger for html
[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   private static final String specialChars = "&<#";
26   /** The compilation unit, may be <code>null</code>. */
27   //    private final ICompilationUnit fCompilationUnit;
28
29   /**
30    * Creates a compilation unit context.
31    * 
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>).
36    */
37   protected HTMLUnitContext(ContextType type, IDocument document, int completionPosition)
38   //,ICompilationUnit compilationUnit)
39   {
40     super(type, document, completionPosition, 0);
41     //  fCompilationUnit= compilationUnit;
42   }
43
44   /*
45   * @see TemplateContext#canEvaluate(Template templates)
46   */
47   public boolean canEvaluate(Template template) {
48     // return fForceEvaluation || 
49     return template.matches(getKey(), getContextType().getName());
50   }
51
52   /**
53    * Returns <code>true</code> if template matches the prefix and context,
54    * <code>false</code> otherwise.
55    */
56   public boolean canEvaluate(String identifier) {
57     String prefix = getKey();
58     return
59     //      fEnabled &&
60     //      fContextTypeName.equals(contextTypeName) &&
61      (prefix.length() != 0) && identifier.toLowerCase().startsWith(prefix.toLowerCase());
62   }
63
64   /*
65   * @see TemplateContext#evaluate(Template template)
66   */
67   public TemplateBuffer evaluate(Template template) throws CoreException {
68     if (!canEvaluate(template))
69       return null;
70
71     TemplateTranslator translator = new TemplateTranslator();
72     TemplateBuffer buffer = translator.translate(template.getPattern());
73
74     getContextType().edit(buffer, this);
75
76     String lineDelimiter = null;
77     try {
78       lineDelimiter = getDocument().getLineDelimiter(0);
79     } catch (BadLocationException e) {
80     }
81
82     if (lineDelimiter == null)
83       lineDelimiter = PLATFORM_LINE_DELIMITER;
84
85     //    ITemplateEditor formatter= new JavaFormatter(lineDelimiter);
86     //    formatter.edit(buffer, this);
87
88     return buffer;
89   }
90
91   /*
92    * @see DocumentTemplateContext#getCompletionPosition();
93    */
94   public int getStart() {
95     IDocument document = getDocument();
96     try {
97       int start = getCompletionOffset();
98       char ch = ' ';
99       while (start != 0) {
100         ch = document.getChar(start - 1);
101         if (specialChars.indexOf(ch) != (-1)) {
102           return --start;
103         }
104         if (Character.isUnicodeIdentifierPart(ch)) {
105           start--;
106         } else {
107           break;
108         }
109       }
110       if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1))) {
111         start--;
112         if ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1)) {
113           start--;
114         }
115       }
116
117       //      while (((start != 0)
118       //        && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
119       //        || ((start != 0)
120       //          && specialChars.indexOf(document.getChar(start - 1)) != (-1))) {
121       //        start--;
122       //      }
123       //
124       //if (((start != 0)
125       //        && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
126       //        || ((start != 0)
127       //                && specialChars.indexOf(document.getChar(start - 1)) != (-1))) {
128       //        start--;
129       //}
130
131       return start;
132
133     } catch (BadLocationException e) {
134       return getCompletionOffset();
135     }
136   }
137
138   /**
139    * Returns the character before start position of completion.
140    */
141   public char getCharacterBeforeStart() {
142     int start = getStart();
143
144     try {
145       return start == 0 ? ' ' : getDocument().getChar(start - 1);
146
147     } catch (BadLocationException e) {
148       return ' ';
149     }
150   }
151   /**
152    * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
153    */
154   //    public final ICompilationUnit getCompilationUnit() {
155   //            return fCompilationUnit;
156   //    }
157
158   /**
159    * Returns the enclosing element of a particular element type, <code>null</code>
160    * if no enclosing element of that type exists.
161    */
162   //    public IJavaElement findEnclosingElement(int elementType) {
163   //            if (fCompilationUnit == null)
164   //                    return null;
165   //
166   //            try {
167   //                    IJavaElement element= fCompilationUnit.getElementAt(getStart());
168   //                    while (element != null && element.getElementType() != elementType)
169   //                            element= element.getParent();
170   //                    
171   //                    return element;
172   //
173   //            } catch (JavaModelException e) {
174   //                    return null;
175   //            }       
176   //    }
177
178 }