1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
13 import java.util.ArrayList;
15 import org.eclipse.jface.text.templates.TemplateContextType;
16 import org.eclipse.jface.text.templates.ContextTypeRegistry;
17 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
18 import org.eclipse.jface.text.templates.TemplateContext;
19 import org.eclipse.jface.text.templates.TemplateException;
20 import org.eclipse.jface.text.templates.TemplateVariable;
21 import org.eclipse.jface.text.templates.TemplateVariableResolver;
23 import net.sourceforge.phpdt.core.ToolFactory;
24 import net.sourceforge.phpdt.core.compiler.IScanner;
25 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
26 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
28 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
32 public class CodeTemplateContextType extends TemplateContextType {
35 public static final String CATCHBLOCK_CONTEXTTYPE= "catchblock_context"; //$NON-NLS-1$
36 public static final String METHODBODY_CONTEXTTYPE= "methodbody_context"; //$NON-NLS-1$
37 public static final String CONSTRUCTORBODY_CONTEXTTYPE= "constructorbody_context"; //$NON-NLS-1$
38 public static final String GETTERBODY_CONTEXTTYPE= "getterbody_context"; //$NON-NLS-1$
39 public static final String SETTERBODY_CONTEXTTYPE= "setterbody_context"; //$NON-NLS-1$
40 public static final String NEWTYPE_CONTEXTTYPE= "newtype_context"; //$NON-NLS-1$
41 public static final String TYPECOMMENT_CONTEXTTYPE= "typecomment_context"; //$NON-NLS-1$
42 public static final String FIELDCOMMENT_CONTEXTTYPE= "fieldcomment_context"; //$NON-NLS-1$
43 public static final String METHODCOMMENT_CONTEXTTYPE= "methodcomment_context"; //$NON-NLS-1$
44 public static final String CONSTRUCTORCOMMENT_CONTEXTTYPE= "constructorcomment_context"; //$NON-NLS-1$
45 public static final String OVERRIDECOMMENT_CONTEXTTYPE= "overridecomment_context"; //$NON-NLS-1$
46 public static final String GETTERCOMMENT_CONTEXTTYPE= "gettercomment_context"; //$NON-NLS-1$
47 public static final String SETTERCOMMENT_CONTEXTTYPE= "settercomment_context"; //$NON-NLS-1$
50 public static final String COMMENT_SUFFIX= "comment"; //$NON-NLS-1$
51 public static final String CATCHBLOCK= "catchblock"; //$NON-NLS-1$
52 public static final String METHODSTUB= "methodbody"; //$NON-NLS-1$
53 public static final String NEWTYPE= "newtype"; //$NON-NLS-1$
54 public static final String CONSTRUCTORSTUB= "constructorbody"; //$NON-NLS-1$
55 public static final String GETTERSTUB= "getterbody"; //$NON-NLS-1$
56 public static final String SETTERSTUB= "setterbody"; //$NON-NLS-1$
57 public static final String TYPECOMMENT= "type" + COMMENT_SUFFIX; //$NON-NLS-1$
58 public static final String FIELDCOMMENT= "field" + COMMENT_SUFFIX; //$NON-NLS-1$
59 public static final String METHODCOMMENT= "method" + COMMENT_SUFFIX; //$NON-NLS-1$
60 public static final String CONSTRUCTORCOMMENT= "constructor" + COMMENT_SUFFIX; //$NON-NLS-1$
61 public static final String OVERRIDECOMMENT= "override" + COMMENT_SUFFIX; //$NON-NLS-1$
62 public static final String GETTERCOMMENT= "getter" + COMMENT_SUFFIX; //$NON-NLS-1$
63 public static final String SETTERCOMMENT= "setter" + COMMENT_SUFFIX; //$NON-NLS-1$
66 public static final String EXCEPTION_TYPE= "exception_type"; //$NON-NLS-1$
67 public static final String EXCEPTION_VAR= "exception_var"; //$NON-NLS-1$
68 public static final String ENCLOSING_METHOD= "enclosing_method"; //$NON-NLS-1$
69 public static final String ENCLOSING_TYPE= "enclosing_type"; //$NON-NLS-1$
70 public static final String BODY_STATEMENT= "body_statement"; //$NON-NLS-1$
71 public static final String FIELD= "field"; //$NON-NLS-1$
72 public static final String FIELD_TYPE= "field_type"; //$NON-NLS-1$
73 public static final String BARE_FIELD_NAME= "bare_field_name"; //$NON-NLS-1$
75 public static final String PARAM= "param"; //$NON-NLS-1$
76 public static final String RETURN_TYPE= "return_type"; //$NON-NLS-1$
77 public static final String SEE_TAG= "see_to_overridden"; //$NON-NLS-1$
79 public static final String TAGS= "tags"; //$NON-NLS-1$
81 public static final String TYPENAME= "type_name"; //$NON-NLS-1$
82 public static final String FILENAME= "file_name"; //$NON-NLS-1$
83 public static final String PACKAGENAME= "package_name"; //$NON-NLS-1$
84 public static final String PROJECTNAME= "project_name"; //$NON-NLS-1$
86 public static final String PACKAGE_DECLARATION= "package_declaration"; //$NON-NLS-1$
87 public static final String TYPE_DECLARATION= "type_declaration"; //$NON-NLS-1$
88 public static final String TYPE_COMMENT= "typecomment"; //$NON-NLS-1$
92 * Resolver that resolves to the variable defined in the context.
94 public static class CodeTemplateVariableResolver extends TemplateVariableResolver {
95 public CodeTemplateVariableResolver(String type, String description) {
96 super(type, description);
99 protected String resolve(TemplateContext context) {
100 return context.getVariable(getType());
105 * Resolver for javadoc tags.
107 public static class TagsVariableResolver extends TemplateVariableResolver {
108 public TagsVariableResolver() {
109 super(TAGS, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.tags")); //$NON-NLS-1$
112 protected String resolve(TemplateContext context) {
113 return "@"; //$NON-NLS-1$
118 * Resolver for todo tags.
120 protected static class Todo extends TemplateVariableResolver {
123 super("todo", JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.todo")); //$NON-NLS-1$ //$NON-NLS-2$
126 protected String resolve(TemplateContext context) {
127 String todoTaskTag= StubUtility.getTodoTaskTag(((CodeTemplateContext) context).getJavaProject());
128 if (todoTaskTag == null)
129 return "XXX"; //$NON-NLS-1$
135 private boolean fIsComment;
137 public CodeTemplateContextType(String contextName) {
143 addResolver(new GlobalTemplateVariables.Dollar());
144 addResolver(new GlobalTemplateVariables.Date());
145 addResolver(new GlobalTemplateVariables.Year());
146 addResolver(new GlobalTemplateVariables.Time());
147 addResolver(new GlobalTemplateVariables.User());
148 addResolver(new Todo());
150 if (CATCHBLOCK_CONTEXTTYPE.equals(contextName)) {
151 addResolver(new CodeTemplateVariableResolver(EXCEPTION_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.exceptiontype"))); //$NON-NLS-1$
152 addResolver(new CodeTemplateVariableResolver(EXCEPTION_VAR, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.exceptionvar"))); //$NON-NLS-1$
153 } else if (METHODBODY_CONTEXTTYPE.equals(contextName)) {
154 addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
155 addResolver(new CodeTemplateVariableResolver(ENCLOSING_METHOD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
156 addResolver(new CodeTemplateVariableResolver(BODY_STATEMENT, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.bodystatement"))); //$NON-NLS-1$
157 } else if (CONSTRUCTORBODY_CONTEXTTYPE.equals(contextName)) {
158 addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
159 addResolver(new CodeTemplateVariableResolver(BODY_STATEMENT, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.bodystatement"))); //$NON-NLS-1$
160 } else if (GETTERBODY_CONTEXTTYPE.equals(contextName)) {
161 addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
162 addResolver(new CodeTemplateVariableResolver(ENCLOSING_METHOD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
163 addResolver(new CodeTemplateVariableResolver(FIELD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
164 } else if (SETTERBODY_CONTEXTTYPE.equals(contextName)) {
165 addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
166 addResolver(new CodeTemplateVariableResolver(ENCLOSING_METHOD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
167 addResolver(new CodeTemplateVariableResolver(FIELD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
168 addResolver(new CodeTemplateVariableResolver(PARAM, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.param"))); //$NON-NLS-1$
169 } else if (NEWTYPE_CONTEXTTYPE.equals(contextName)) {
170 addResolver(new CodeTemplateVariableResolver(TYPENAME, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.typename"))); //$NON-NLS-1$
171 addResolver(new CodeTemplateVariableResolver(PACKAGE_DECLARATION, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.packdeclaration"))); //$NON-NLS-1$
172 addResolver(new CodeTemplateVariableResolver(TYPE_DECLARATION, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.typedeclaration"))); //$NON-NLS-1$
173 addResolver(new CodeTemplateVariableResolver(TYPE_COMMENT, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.typecomment"))); //$NON-NLS-1$
174 addCompilationUnitVariables();
175 } else if (TYPECOMMENT_CONTEXTTYPE.equals(contextName)) {
176 addResolver(new CodeTemplateVariableResolver(TYPENAME, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.typename"))); //$NON-NLS-1$
177 addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
178 addResolver(new TagsVariableResolver());
179 addCompilationUnitVariables();
181 } else if (FIELDCOMMENT_CONTEXTTYPE.equals(contextName)) {
182 addResolver(new CodeTemplateVariableResolver(FIELD_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.fieldtype"))); //$NON-NLS-1$
183 addResolver(new CodeTemplateVariableResolver(FIELD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.fieldname"))); //$NON-NLS-1$
184 addCompilationUnitVariables();
186 } else if (METHODCOMMENT_CONTEXTTYPE.equals(contextName)) {
187 addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
188 addResolver(new CodeTemplateVariableResolver(ENCLOSING_METHOD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
189 addResolver(new CodeTemplateVariableResolver(RETURN_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.returntype"))); //$NON-NLS-1$
190 addResolver(new TagsVariableResolver());
191 addCompilationUnitVariables();
193 } else if (OVERRIDECOMMENT_CONTEXTTYPE.equals(contextName)) {
194 addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
195 addResolver(new CodeTemplateVariableResolver(ENCLOSING_METHOD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
196 addResolver(new CodeTemplateVariableResolver(SEE_TAG, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.seetag"))); //$NON-NLS-1$
197 addResolver(new TagsVariableResolver());
198 addCompilationUnitVariables();
200 } else if (CONSTRUCTORCOMMENT_CONTEXTTYPE.equals(contextName)) {
201 addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
202 addResolver(new TagsVariableResolver());
203 addCompilationUnitVariables();
205 } else if (GETTERCOMMENT_CONTEXTTYPE.equals(contextName)) {
206 addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
207 addResolver(new CodeTemplateVariableResolver(FIELD_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.getterfieldtype"))); //$NON-NLS-1$
208 addResolver(new CodeTemplateVariableResolver(FIELD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
209 addResolver(new CodeTemplateVariableResolver(ENCLOSING_METHOD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
210 addResolver(new CodeTemplateVariableResolver(BARE_FIELD_NAME, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.barefieldname"))); //$NON-NLS-1$
211 addCompilationUnitVariables();
213 } else if (SETTERCOMMENT_CONTEXTTYPE.equals(contextName)) {
214 addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
215 addResolver(new CodeTemplateVariableResolver(FIELD_TYPE, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.getterfieldtype"))); //$NON-NLS-1$
216 addResolver(new CodeTemplateVariableResolver(FIELD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
217 addResolver(new CodeTemplateVariableResolver(ENCLOSING_METHOD, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
218 addResolver(new CodeTemplateVariableResolver(PARAM, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.param"))); //$NON-NLS-1$
219 addResolver(new CodeTemplateVariableResolver(BARE_FIELD_NAME, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.barefieldname"))); //$NON-NLS-1$
220 addCompilationUnitVariables();
225 private void addCompilationUnitVariables() {
226 addResolver(new CodeTemplateVariableResolver(FILENAME, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.filename"))); //$NON-NLS-1$
227 addResolver(new CodeTemplateVariableResolver(PACKAGENAME, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.packagename"))); //$NON-NLS-1$
228 addResolver(new CodeTemplateVariableResolver(PROJECTNAME, JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.projectname"))); //$NON-NLS-1$
232 * @see net.sourceforge.phpdt.internal.corext.template.ContextType#validateVariables(net.sourceforge.phpdt.internal.corext.template.TemplateVariable[])
234 protected void validateVariables(TemplateVariable[] variables) throws TemplateException {
235 ArrayList required= new ArrayList(5);
236 String contextName= getId();
237 if (NEWTYPE_CONTEXTTYPE.equals(contextName)) {
238 required.add(PACKAGE_DECLARATION);
239 required.add(TYPE_DECLARATION);
241 for (int i= 0; i < variables.length; i++) {
242 String type= variables[i].getType();
243 if (getResolver(type) == null) {
244 throw new TemplateException(JavaTemplateMessages.getFormattedString("CodeTemplateContextType.validate.unknownvariable", type)); //$NON-NLS-1$
246 required.remove(type);
248 if (!required.isEmpty()) {
249 String missing= (String) required.get(0);
250 throw new TemplateException(JavaTemplateMessages.getFormattedString("CodeTemplateContextType.validate.missingvariable", missing)); //$NON-NLS-1$
252 super.validateVariables(variables);
258 * @see net.sourceforge.phpdt.internal.corext.template.ContextType#createContext()
260 public TemplateContext createContext() {
264 public static void registerContextTypes(ContextTypeRegistry registry) {
265 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.CATCHBLOCK_CONTEXTTYPE));
266 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.METHODBODY_CONTEXTTYPE));
267 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.CONSTRUCTORBODY_CONTEXTTYPE));
268 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.GETTERBODY_CONTEXTTYPE));
269 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.SETTERBODY_CONTEXTTYPE));
270 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.NEWTYPE_CONTEXTTYPE));
272 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.TYPECOMMENT_CONTEXTTYPE));
273 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.FIELDCOMMENT_CONTEXTTYPE));
274 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.METHODCOMMENT_CONTEXTTYPE));
275 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.CONSTRUCTORCOMMENT_CONTEXTTYPE));
276 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.OVERRIDECOMMENT_CONTEXTTYPE));
277 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.GETTERCOMMENT_CONTEXTTYPE));
278 registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.SETTERCOMMENT_CONTEXTTYPE));
284 * @see net.sourceforge.phpdt.internal.corext.template.ContextType#validate(java.lang.String)
286 public void validate(String pattern) throws TemplateException {
287 super.validate(pattern);
289 if (!isValidComment(pattern)) {
290 throw new TemplateException(JavaTemplateMessages.getString("CodeTemplateContextType.validate.invalidcomment")); //$NON-NLS-1$
296 private boolean isValidComment(String template) {
297 IScanner scanner= ToolFactory.createScanner(true, false, false, true);// false);
298 scanner.setSource(template.toCharArray());
300 int next= scanner.getNextToken();
301 while (next == ITerminalSymbols.TokenNameCOMMENT_LINE || next == ITerminalSymbols.TokenNameCOMMENT_PHPDOC || next == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
302 next= scanner.getNextToken();
304 return next == ITerminalSymbols.TokenNameEOF;
305 } catch (InvalidInputException e) {