Syntax highlighting is changeable.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / php / HTMLUnitContext.java
index 136e438..0c4dfb3 100644 (file)
@@ -1,98 +1,60 @@
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
 package net.sourceforge.phpdt.internal.corext.template.php;
 
-import net.sourceforge.phpdt.internal.corext.template.ContextType;
-import net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext;
-import net.sourceforge.phpdt.internal.corext.template.Template;
-import net.sourceforge.phpdt.internal.corext.template.TemplateBuffer;
-import net.sourceforge.phpdt.internal.corext.template.TemplateTranslator;
-
-import org.eclipse.core.runtime.CoreException;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.templates.TemplateContextType;
+import org.eclipse.jface.text.templates.Template;
+import org.eclipse.jface.text.templates.TemplateBuffer;
+import org.eclipse.jface.text.templates.TemplateException;
+import org.eclipse.jface.text.templates.TemplateTranslator;
+
+import net.sourceforge.phpdt.core.ICompilationUnit;
 
 /**
- * A compilation unit context.
+ * A context for javadoc.
  */
-public class HTMLUnitContext extends DocumentTemplateContext {
-
-  /** The platform default line delimiter. */
-  private static final String PLATFORM_LINE_DELIMITER = System.getProperty("line.separator"); //$NON-NLS-1$
+public class HTMLUnitContext extends CompilationUnitContext {
 
-  /** special characters
-   * '&' for the start of HTML entities
-   * '<' for the start of HTML tags
-   * '#' for the start of colour attributes
+  // tags
+  //   private static final char HTML_TAG_BEGIN= '<';
+  //   private static final char HTML_TAG_END= '>';
+  //   private static final char JAVADOC_TAG_BEGIN= '@';
+  /**
+   * special characters '&' for the start of HTML entities ' <' for the start of HTML tags '#' for the start of colour attributes
    * '{' for the start of smarty partitions inside HTML code
    */
   private static final String specialChars = "&<#{";
 
-  /** The compilation unit, may be <code>null</code>. */
-  //   private final ICompilationUnit fCompilationUnit;
-  protected boolean fForceEvaluation;
   /**
-   * Creates a compilation unit context.
+   * Creates a javadoc template context.
    * 
-   * @param type   the context type.
-   * @param document the document.
-   * @param completionPosition the completion position within the document.
-   * @param compilationUnit the compilation unit (may be <code>null</code>).
+   * @param type
+   *          the context type.
+   * @param document
+   *          the document.
+   * @param completionOffset
+   *          the completion offset within the document.
+   * @param completionLength
+   *          the completion length within the document.
+   * @param compilationUnit
+   *          the compilation unit (may be <code>null</code>).
    */
-  protected HTMLUnitContext(ContextType type, IDocument document, int completionPosition)
-  //,ICompilationUnit compilationUnit)
-  {
-    super(type, document, completionPosition, 0);
-    // fCompilationUnit= compilationUnit;
+  public HTMLUnitContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength,
+      ICompilationUnit compilationUnit) {
+    super(type, document, completionOffset, completionLength, compilationUnit);
   }
 
   /*
-  * @see TemplateContext#canEvaluate(Template templates)
-  */
-  public boolean canEvaluate(Template template) {
-    // return fForceEvaluation || 
-    return template.matches(getKey(), getContextType().getName());
-  }
-
-  /**
-   * Returns <code>true</code> if template matches the prefix and context,
-   * <code>false</code> otherwise.
+   * @see TemplateContext#canEvaluate(Template templates)
    */
-  public boolean canEvaluate(String identifier) {
-    String prefix = getKey();
-    return
-    //      fEnabled &&
-    //      fContextTypeName.equals(contextTypeName) &&
-     (prefix.length() != 0) && identifier.toLowerCase().startsWith(prefix.toLowerCase());
-  }
-
-  /*
-  * @see TemplateContext#evaluate(Template template)
-  */
-  public TemplateBuffer evaluate(Template template) throws CoreException {
-    if (!canEvaluate(template))
-      return null;
-
-    TemplateTranslator translator = new TemplateTranslator();
-    TemplateBuffer buffer = translator.translate(template.getPattern());
-
-    getContextType().edit(buffer, this);
-
-    String lineDelimiter = null;
-    try {
-      lineDelimiter = getDocument().getLineDelimiter(0);
-    } catch (BadLocationException e) {
-    }
-
-    if (lineDelimiter == null)
-      lineDelimiter = PLATFORM_LINE_DELIMITER;
+  public boolean canEvaluate(Template template) {
+    String key = getKey();
 
-    //    ITemplateEditor formatter= new JavaFormatter(lineDelimiter);
-    //    formatter.edit(buffer, this);
+    if (fForceEvaluation)
+      return true;
 
-    return buffer;
+    return template.matches(key, getContextType().getId()) && (key.length() != 0)
+        && template.getName().toLowerCase().startsWith(key.toLowerCase());
   }
 
   /*
@@ -142,49 +104,61 @@ public class HTMLUnitContext extends DocumentTemplateContext {
     }
   }
 
-  /**
-   * Returns the character before start position of completion.
+  /*
+   * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getEnd()
    */
-  public char getCharacterBeforeStart() {
-    int start = getStart();
+  public int getEnd() {
+
+    if (getCompletionLength() == 0)
+      return super.getEnd();
 
     try {
-      return start == 0 ? ' ' : getDocument().getChar(start - 1);
+      IDocument document = getDocument();
+
+      int start = getCompletionOffset();
+      int end = getCompletionOffset() + getCompletionLength();
+
+      while (start != end && Character.isWhitespace(document.getChar(end - 1)))
+        end--;
+
+      return end;
 
     } catch (BadLocationException e) {
-      return ' ';
+      return super.getEnd();
     }
   }
-  /**
-   * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
-   */
-  //   public final ICompilationUnit getCompilationUnit() {
-  //           return fCompilationUnit;
-  //   }
 
-  /**
-   * Returns the enclosing element of a particular element type, <code>null</code>
-   * if no enclosing element of that type exists.
+  /*
+   * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getKey()
    */
-  //   public IJavaElement findEnclosingElement(int elementType) {
-  //           if (fCompilationUnit == null)
-  //                   return null;
-  //
-  //           try {
-  //                   IJavaElement element= fCompilationUnit.getElementAt(getStart());
-  //                   while (element != null && element.getElementType() != elementType)
-  //                           element= element.getParent();
-  //                   
-  //                   return element;
-  //
-  //           } catch (JavaModelException e) {
-  //                   return null;
-  //           }       
-  //   }
-  /**
-   * Forces evaluation.
+  public String getKey() {
+
+    if (getCompletionLength() == 0)
+      return super.getKey();
+
+    try {
+      IDocument document = getDocument();
+
+      int start = getStart();
+      int end = getCompletionOffset();
+      return start <= end ? document.get(start, end - start) : ""; //$NON-NLS-1$
+
+    } catch (BadLocationException e) {
+      return super.getKey();
+    }
+  }
+
+  /*
+   * @see TemplateContext#evaluate(Template)
    */
-  public void setForceEvaluation(boolean evaluate) {
-    fForceEvaluation = evaluate;
+  public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
+    TemplateTranslator translator = new TemplateTranslator();
+    TemplateBuffer buffer = translator.translate(template);
+
+    getContextType().resolve(buffer, this);
+
+    return buffer;
   }
+
 }
+