first version of 'Code Assist" template engine
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / java / CompilationUnitContext.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.template.java;
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 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15
16 /**
17  * A compilation unit context.
18  */
19 public class CompilationUnitContext extends DocumentTemplateContext {
20
21   /** The platform default line delimiter. */
22   private static final String PLATFORM_LINE_DELIMITER= System.getProperty("line.separator"); //$NON-NLS-1$
23
24         /** The compilation unit, may be <code>null</code>. */
25 //      private final ICompilationUnit fCompilationUnit;
26
27         /**
28          * Creates a compilation unit context.
29          * 
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>).
34          */
35         protected CompilationUnitContext(ContextType type, IDocument document, int completionPosition)
36         //,ICompilationUnit compilationUnit)
37         {
38                 super(type, document, completionPosition);
39         //      fCompilationUnit= compilationUnit;
40         }
41         
42     /*
43    * @see TemplateContext#canEvaluate(Template templates)
44    */
45   public boolean canEvaluate(Template template) {
46     // return fForceEvaluation || 
47     return template.matches(getKey(), getContextType().getName());
48   }
49   
50     /*
51    * @see TemplateContext#evaluate(Template template)
52    */
53   public TemplateBuffer evaluate(Template template) throws CoreException {
54     if (!canEvaluate(template))
55       return null;
56     
57     TemplateTranslator translator= new TemplateTranslator();
58     TemplateBuffer buffer= translator.translate(template.getPattern());
59
60     getContextType().edit(buffer, this);
61       
62     String lineDelimiter= null;
63     try {
64       lineDelimiter= getDocument().getLineDelimiter(0);
65     } catch (BadLocationException e) {
66     }
67
68     if (lineDelimiter == null)
69       lineDelimiter= PLATFORM_LINE_DELIMITER;
70     
71 //    ITemplateEditor formatter= new JavaFormatter(lineDelimiter);
72 //    formatter.edit(buffer, this);
73
74     return buffer;
75   }
76   
77   /*
78    * @see DocumentTemplateContext#getCompletionPosition();
79    */
80   public int getStart() {
81     IDocument document= getDocument();
82     try {
83       int start= getCompletionPosition();
84   
85       while ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
86         start--;
87         
88       if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
89         start--;
90   
91       return start;
92
93     } catch (BadLocationException e) {
94       return getCompletionPosition(); 
95     }
96   }
97
98   /**
99    * Returns the character before start position of completion.
100    */
101   public char getCharacterBeforeStart() {
102     int start= getStart();
103     
104     try {
105       return start == 0
106         ? ' '
107         : getDocument().getChar(start - 1);
108
109     } catch (BadLocationException e) {
110       return ' ';
111     }
112   }
113         /**
114          * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
115          */
116 //      public final ICompilationUnit getCompilationUnit() {
117 //              return fCompilationUnit;
118 //      }
119
120         /**
121          * Returns the enclosing element of a particular element type, <code>null</code>
122          * if no enclosing element of that type exists.
123          */
124 //      public IJavaElement findEnclosingElement(int elementType) {
125 //              if (fCompilationUnit == null)
126 //                      return null;
127 //
128 //              try {
129 //                      IJavaElement element= fCompilationUnit.getElementAt(getStart());
130 //                      while (element != null && element.getElementType() != elementType)
131 //                              element= element.getParent();
132 //                      
133 //                      return element;
134 //
135 //              } catch (JavaModelException e) {
136 //                      return null;
137 //              }       
138 //      }
139
140 }