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 context for phpdoc.
20 public class PHPDocContext extends DocumentTemplateContext {
23 private static final char HTML_TAG_BEGIN= '<';
24 private static final char HTML_TAG_END= '>';
25 private static final char JAVADOC_TAG_BEGIN= '@';
28 * Creates a phpdoc template context.
30 * @param type the context type.
31 * @param document the document.
32 * @param completionPosition the completion position within the document.
33 * @param unit the compilation unit (may be <code>null</code>).
35 public PHPDocContext(ContextType type, IDocument document, int completionOffset, int completionLength)
36 //, int completionOffset, int completionLength, ICompilationUnit compilationUnit)
38 super(type, document, completionOffset, completionLength);
39 // super(type, document, completionOffset, completionLength, compilationUnit);
43 * @see TemplateContext#canEvaluate(Template templates)
45 public boolean canEvaluate(Template template) {
48 // if (fForceEvaluation)
50 return template.matches(getKey(), getContextType().getName());
52 // template.matches(key, getContextType().getName()) &&
53 // (key.length() != 0) && template.getName().toLowerCase().startsWith(key.toLowerCase());
57 * @see DocumentTemplateContext#getStart()
59 public int getStart() {
61 IDocument document= getDocument();
63 if (getCompletionLength() == 0) {
64 int start= getCompletionOffset();
66 if ((start != 0) && (document.getChar(start - 1) == HTML_TAG_END))
69 while ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
72 if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
75 // include html and phpdoc tags
77 (document.getChar(start - 1) == HTML_TAG_BEGIN) ||
78 (document.getChar(start - 1) == JAVADOC_TAG_BEGIN)))
87 int start= getCompletionOffset();
88 int end= getCompletionOffset() + getCompletionLength();
90 while (start != 0 && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
93 while (start != end && Character.isWhitespace(document.getChar(start)))
97 start= getCompletionOffset();
102 } catch (BadLocationException e) {
103 return getCompletionOffset();
108 * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getEnd()
110 public int getEnd() {
112 if (getCompletionLength() == 0)
113 return super.getEnd();
116 IDocument document= getDocument();
118 int start= getCompletionOffset();
119 int end= getCompletionOffset() + getCompletionLength();
121 while (start != end && Character.isWhitespace(document.getChar(end - 1)))
126 } catch (BadLocationException e) {
127 return super.getEnd();
132 * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getKey()
134 public String getKey() {
136 if (getCompletionLength() == 0)
137 return super.getKey();
140 IDocument document= getDocument();
142 int start= getStart();
143 int end= getCompletionOffset();
145 ? document.get(start, end - start)
148 } catch (BadLocationException e) {
149 return super.getKey();
154 * @see TemplateContext#evaluate(Template)
156 public TemplateBuffer evaluate(Template template) throws CoreException {
157 TemplateTranslator translator= new TemplateTranslator();
158 TemplateBuffer buffer= translator.translate(template.getPattern());
160 getContextType().edit(buffer, this);