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