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