2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.template.java;
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 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
17 * A compilation unit context.
19 public class CompilationUnitContext extends DocumentTemplateContext {
21 /** The platform default line delimiter. */
22 private static final String PLATFORM_LINE_DELIMITER= System.getProperty("line.separator"); //$NON-NLS-1$
24 /** The compilation unit, may be <code>null</code>. */
25 // private final ICompilationUnit fCompilationUnit;
28 * Creates a compilation unit context.
30 * @param type the context type.
31 * @param document the document.
32 * @param completionPosition the completion position within the document.
33 * @param compilationUnit the compilation unit (may be <code>null</code>).
35 protected CompilationUnitContext(ContextType type, IDocument document, int completionPosition)
36 //,ICompilationUnit compilationUnit)
38 super(type, document, completionPosition);
39 // fCompilationUnit= compilationUnit;
43 * @see TemplateContext#canEvaluate(Template templates)
45 public boolean canEvaluate(Template template) {
46 // return fForceEvaluation ||
47 return template.matches(getKey(), getContextType().getName());
51 * @see TemplateContext#evaluate(Template template)
53 public TemplateBuffer evaluate(Template template) throws CoreException {
54 if (!canEvaluate(template))
57 TemplateTranslator translator= new TemplateTranslator();
58 TemplateBuffer buffer= translator.translate(template.getPattern());
60 getContextType().edit(buffer, this);
62 String lineDelimiter= null;
64 lineDelimiter= getDocument().getLineDelimiter(0);
65 } catch (BadLocationException e) {
68 if (lineDelimiter == null)
69 lineDelimiter= PLATFORM_LINE_DELIMITER;
71 // ITemplateEditor formatter= new JavaFormatter(lineDelimiter);
72 // formatter.edit(buffer, this);
78 * @see DocumentTemplateContext#getCompletionPosition();
80 public int getStart() {
81 IDocument document= getDocument();
83 int start= getCompletionPosition();
85 while ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
88 if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
93 } catch (BadLocationException e) {
94 return getCompletionPosition();
99 * Returns the character before start position of completion.
101 public char getCharacterBeforeStart() {
102 int start= getStart();
107 : getDocument().getChar(start - 1);
109 } catch (BadLocationException e) {
114 * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
116 // public final ICompilationUnit getCompilationUnit() {
117 // return fCompilationUnit;
121 * Returns the enclosing element of a particular element type, <code>null</code>
122 * if no enclosing element of that type exists.
124 // public IJavaElement findEnclosingElement(int elementType) {
125 // if (fCompilationUnit == null)
129 // IJavaElement element= fCompilationUnit.getElementAt(getStart());
130 // while (element != null && element.getElementType() != elementType)
131 // element= element.getParent();
135 // } catch (JavaModelException e) {