1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.templates.TemplateContextType;
16 import org.eclipse.jface.text.templates.Template;
17 import org.eclipse.jface.text.templates.TemplateBuffer;
18 import org.eclipse.jface.text.templates.TemplateException;
19 import org.eclipse.jface.text.templates.TemplateTranslator;
21 import net.sourceforge.phpdt.core.ICompilationUnit;
25 * A context for javadoc.
27 public class JavaDocContext extends CompilationUnitContext {
30 private static final char HTML_TAG_BEGIN= '<';
31 private static final char HTML_TAG_END= '>';
32 private static final char JAVADOC_TAG_BEGIN= '@';
35 * Creates a javadoc template context.
37 * @param type the context type.
38 * @param document the document.
39 * @param completionOffset the completion offset within the document.
40 * @param completionLength the completion length within the document.
41 * @param compilationUnit the compilation unit (may be <code>null</code>).
43 public JavaDocContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength,
44 ICompilationUnit compilationUnit)
46 super(type, document, completionOffset, completionLength, compilationUnit);
50 * @see TemplateContext#canEvaluate(Template templates)
52 public boolean canEvaluate(Template template) {
59 template.matches(key, getContextType().getId()) &&
60 (key.length() != 0) && template.getName().toLowerCase().startsWith(key.toLowerCase());
64 * @see DocumentTemplateContext#getStart()
66 public int getStart() {
68 IDocument document= getDocument();
70 if (getCompletionLength() == 0) {
71 int start= getCompletionOffset();
73 if ((start != 0) && (document.getChar(start - 1) == HTML_TAG_END))
76 while ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
79 if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
82 // include html and javadoc tags
84 (document.getChar(start - 1) == HTML_TAG_BEGIN) ||
85 (document.getChar(start - 1) == JAVADOC_TAG_BEGIN)))
94 int start= getCompletionOffset();
95 int end= getCompletionOffset() + getCompletionLength();
97 while (start != 0 && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
100 while (start != end && Character.isWhitespace(document.getChar(start)))
104 start= getCompletionOffset();
109 } catch (BadLocationException e) {
110 return getCompletionOffset();
115 * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getEnd()
117 public int getEnd() {
119 if (getCompletionLength() == 0)
120 return super.getEnd();
123 IDocument document= getDocument();
125 int start= getCompletionOffset();
126 int end= getCompletionOffset() + getCompletionLength();
128 while (start != end && Character.isWhitespace(document.getChar(end - 1)))
133 } catch (BadLocationException e) {
134 return super.getEnd();
139 * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getKey()
141 public String getKey() {
143 if (getCompletionLength() == 0)
144 return super.getKey();
147 IDocument document= getDocument();
149 int start= getStart();
150 int end= getCompletionOffset();
152 ? document.get(start, end - start)
155 } catch (BadLocationException e) {
156 return super.getKey();
161 * @see TemplateContext#evaluate(Template)
163 public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
164 TemplateTranslator translator= new TemplateTranslator();
165 TemplateBuffer buffer= translator.translate(template);
167 getContextType().resolve(buffer, this);