Improved completion processor
[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    * Returns <code>true</code> if template matches the prefix and context,
52    * <code>false</code> otherwise.
53    */
54   public boolean canEvaluate(String identifier) {
55     String prefix = getKey();
56     return 
57 //      fEnabled &&
58 //      fContextTypeName.equals(contextTypeName) &&
59       (prefix.length() != 0) &&
60       identifier.toLowerCase().startsWith(prefix.toLowerCase());
61   }
62   
63     /*
64    * @see TemplateContext#evaluate(Template template)
65    */
66   public TemplateBuffer evaluate(Template template) throws CoreException {
67     if (!canEvaluate(template))
68       return null;
69     
70     TemplateTranslator translator= new TemplateTranslator();
71     TemplateBuffer buffer= translator.translate(template.getPattern());
72
73     getContextType().edit(buffer, this);
74       
75     String lineDelimiter= null;
76     try {
77       lineDelimiter= getDocument().getLineDelimiter(0);
78     } catch (BadLocationException e) {
79     }
80
81     if (lineDelimiter == null)
82       lineDelimiter= PLATFORM_LINE_DELIMITER;
83     
84 //    ITemplateEditor formatter= new JavaFormatter(lineDelimiter);
85 //    formatter.edit(buffer, this);
86
87     return buffer;
88   }
89   
90   /*
91    * @see DocumentTemplateContext#getCompletionPosition();
92    */
93   public int getStart() {
94     IDocument document= getDocument();
95     try {
96       int start= getCompletionPosition();
97   
98       while ( ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1))) ||
99                ((start != 0) && document.getChar(start - 1)=='$') ) {
100         start--;
101       }
102         
103       if ( ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1))) ||
104            ((start != 0) && document.getChar(start - 1)=='$')) {
105         start--;
106       }
107   
108       return start;
109
110     } catch (BadLocationException e) {
111       return getCompletionPosition(); 
112     }
113   }
114
115   /**
116    * Returns the character before start position of completion.
117    */
118   public char getCharacterBeforeStart() {
119     int start= getStart();
120     
121     try {
122       return start == 0
123         ? ' '
124         : getDocument().getChar(start - 1);
125
126     } catch (BadLocationException e) {
127       return ' ';
128     }
129   }
130         /**
131          * Returns the compilation unit if one is associated with this context, <code>null</code> otherwise.
132          */
133 //      public final ICompilationUnit getCompilationUnit() {
134 //              return fCompilationUnit;
135 //      }
136
137         /**
138          * Returns the enclosing element of a particular element type, <code>null</code>
139          * if no enclosing element of that type exists.
140          */
141 //      public IJavaElement findEnclosingElement(int elementType) {
142 //              if (fCompilationUnit == null)
143 //                      return null;
144 //
145 //              try {
146 //                      IJavaElement element= fCompilationUnit.getElementAt(getStart());
147 //                      while (element != null && element.getElementType() != elementType)
148 //                              element= element.getParent();
149 //                      
150 //                      return element;
151 //
152 //              } catch (JavaModelException e) {
153 //                      return null;
154 //              }       
155 //      }
156
157 }