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 net.sourceforge.phpdt.core.ToolFactory;
16 import net.sourceforge.phpdt.core.compiler.IScanner;
17 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
18 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
19 import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility;
21 import org.eclipse.jface.text.templates.ContextTypeRegistry;
22 import org.eclipse.jface.text.templates.GlobalTemplateVariables;
23 import org.eclipse.jface.text.templates.TemplateContext;
24 import org.eclipse.jface.text.templates.TemplateContextType;
25 import org.eclipse.jface.text.templates.TemplateException;
26 import org.eclipse.jface.text.templates.TemplateVariable;
27 import org.eclipse.jface.text.templates.TemplateVariableResolver;
31 public class CodeTemplateContextType extends TemplateContextType {
34 public static final String CATCHBLOCK_CONTEXTTYPE = "php_catchblock_context"; //$NON-NLS-1$
36 public static final String METHODBODY_CONTEXTTYPE = "php_methodbody_context"; //$NON-NLS-1$
38 public static final String CONSTRUCTORBODY_CONTEXTTYPE = "php_constructorbody_context"; //$NON-NLS-1$
40 public static final String GETTERBODY_CONTEXTTYPE = "php_getterbody_context"; //$NON-NLS-1$
42 public static final String SETTERBODY_CONTEXTTYPE = "php_setterbody_context"; //$NON-NLS-1$
44 public static final String NEWTYPE_CONTEXTTYPE = "php_newtype_context"; //$NON-NLS-1$
46 public static final String NEWHTML_CONTEXTTYPE = "php_newhtml_context"; //$NON-NLS-1$
48 public static final String TYPECOMMENT_CONTEXTTYPE = "php_typecomment_context"; //$NON-NLS-1$
50 public static final String FIELDCOMMENT_CONTEXTTYPE = "php_fieldcomment_context"; //$NON-NLS-1$
52 public static final String METHODCOMMENT_CONTEXTTYPE = "php_methodcomment_context"; //$NON-NLS-1$
54 public static final String CONSTRUCTORCOMMENT_CONTEXTTYPE = "php_constructorcomment_context"; //$NON-NLS-1$
56 public static final String OVERRIDECOMMENT_CONTEXTTYPE = "php_overridecomment_context"; //$NON-NLS-1$
58 public static final String GETTERCOMMENT_CONTEXTTYPE = "php_gettercomment_context"; //$NON-NLS-1$
60 public static final String SETTERCOMMENT_CONTEXTTYPE = "php_settercomment_context"; //$NON-NLS-1$
63 public static final String COMMENT_SUFFIX = "comment"; //$NON-NLS-1$
65 public static final String CATCHBLOCK = "catchblock"; //$NON-NLS-1$
67 public static final String METHODSTUB = "methodbody"; //$NON-NLS-1$
69 public static final String NEWTYPE = "newtype"; //$NON-NLS-1$
71 public static final String NEWHTML = "newhtml"; //$NON-NLS-1$
73 public static final String CONSTRUCTORSTUB = "constructorbody"; //$NON-NLS-1$
75 public static final String GETTERSTUB = "getterbody"; //$NON-NLS-1$
77 public static final String SETTERSTUB = "setterbody"; //$NON-NLS-1$
79 public static final String TYPECOMMENT = "type" + COMMENT_SUFFIX; //$NON-NLS-1$
81 public static final String FIELDCOMMENT = "field" + COMMENT_SUFFIX; //$NON-NLS-1$
83 public static final String METHODCOMMENT = "method" + COMMENT_SUFFIX; //$NON-NLS-1$
85 public static final String CONSTRUCTORCOMMENT = "constructor" + COMMENT_SUFFIX; //$NON-NLS-1$
87 public static final String OVERRIDECOMMENT = "override" + COMMENT_SUFFIX; //$NON-NLS-1$
89 public static final String GETTERCOMMENT = "getter" + COMMENT_SUFFIX; //$NON-NLS-1$
91 public static final String SETTERCOMMENT = "setter" + COMMENT_SUFFIX; //$NON-NLS-1$
94 public static final String EXCEPTION_TYPE = "exception_type"; //$NON-NLS-1$
96 public static final String EXCEPTION_VAR = "exception_var"; //$NON-NLS-1$
98 public static final String ENCLOSING_METHOD = "enclosing_method"; //$NON-NLS-1$
100 public static final String ENCLOSING_TYPE = "enclosing_type"; //$NON-NLS-1$
102 public static final String BODY_STATEMENT = "body_statement"; //$NON-NLS-1$
104 public static final String FIELD = "field"; //$NON-NLS-1$
106 public static final String FIELD_TYPE = "field_type"; //$NON-NLS-1$
108 public static final String BARE_FIELD_NAME = "bare_field_name"; //$NON-NLS-1$
110 public static final String PARAM = "param"; //$NON-NLS-1$
112 public static final String RETURN_TYPE = "return_type"; //$NON-NLS-1$
114 public static final String SEE_TAG = "see_to_overridden"; //$NON-NLS-1$
116 public static final String TAGS = "tags"; //$NON-NLS-1$
118 public static final String TYPENAME = "type_name"; //$NON-NLS-1$
120 public static final String FILENAME = "file_name"; //$NON-NLS-1$
122 public static final String PACKAGENAME = "package_name"; //$NON-NLS-1$
124 public static final String PROJECTNAME = "project_name"; //$NON-NLS-1$
126 public static final String PACKAGE_DECLARATION = "package_declaration"; //$NON-NLS-1$
128 public static final String TYPE_DECLARATION = "type_declaration"; //$NON-NLS-1$
130 public static final String TYPE_COMMENT = "typecomment"; //$NON-NLS-1$
133 * Resolver that resolves to the variable defined in the context.
135 public static class CodeTemplateVariableResolver extends
136 TemplateVariableResolver {
137 public CodeTemplateVariableResolver(String type, String description) {
138 super(type, description);
141 protected String resolve(TemplateContext context) {
142 return context.getVariable(getType());
147 * Resolver for javadoc tags.
149 public static class TagsVariableResolver extends TemplateVariableResolver {
150 public TagsVariableResolver() {
154 .getString("CodeTemplateContextType.variable.description.tags")); //$NON-NLS-1$
157 protected String resolve(TemplateContext context) {
158 return "@"; //$NON-NLS-1$
163 * Resolver for todo tags.
165 protected static class Todo extends TemplateVariableResolver {
169 "todo", JavaTemplateMessages.getString("CodeTemplateContextType.variable.description.todo")); //$NON-NLS-1$ //$NON-NLS-2$
172 protected String resolve(TemplateContext context) {
173 String todoTaskTag = StubUtility
174 .getTodoTaskTag(((CodeTemplateContext) context)
176 if (todoTaskTag == null)
177 return "XXX"; //$NON-NLS-1$
183 private boolean fIsComment;
185 public CodeTemplateContextType(String contextName) {
191 addResolver(new GlobalTemplateVariables.Dollar());
192 addResolver(new GlobalTemplateVariables.Date());
193 addResolver(new GlobalTemplateVariables.Year());
194 addResolver(new GlobalTemplateVariables.Time());
195 addResolver(new GlobalTemplateVariables.User());
196 addResolver(new Todo());
198 if (CATCHBLOCK_CONTEXTTYPE.equals(contextName)) {
199 addResolver(new CodeTemplateVariableResolver(
202 .getString("CodeTemplateContextType.variable.description.exceptiontype"))); //$NON-NLS-1$
203 addResolver(new CodeTemplateVariableResolver(
206 .getString("CodeTemplateContextType.variable.description.exceptionvar"))); //$NON-NLS-1$
207 } else if (METHODBODY_CONTEXTTYPE.equals(contextName)) {
208 addResolver(new CodeTemplateVariableResolver(
211 .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
212 addResolver(new CodeTemplateVariableResolver(
215 .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
216 addResolver(new CodeTemplateVariableResolver(
219 .getString("CodeTemplateContextType.variable.description.bodystatement"))); //$NON-NLS-1$
220 } else if (CONSTRUCTORBODY_CONTEXTTYPE.equals(contextName)) {
221 addResolver(new CodeTemplateVariableResolver(
224 .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
225 addResolver(new CodeTemplateVariableResolver(
228 .getString("CodeTemplateContextType.variable.description.bodystatement"))); //$NON-NLS-1$
229 } else if (GETTERBODY_CONTEXTTYPE.equals(contextName)) {
230 addResolver(new CodeTemplateVariableResolver(
233 .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
234 addResolver(new CodeTemplateVariableResolver(
237 .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
238 addResolver(new CodeTemplateVariableResolver(
241 .getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
242 } else if (SETTERBODY_CONTEXTTYPE.equals(contextName)) {
243 addResolver(new CodeTemplateVariableResolver(
246 .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
247 addResolver(new CodeTemplateVariableResolver(
250 .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
251 addResolver(new CodeTemplateVariableResolver(
254 .getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
255 addResolver(new CodeTemplateVariableResolver(
258 .getString("CodeTemplateContextType.variable.description.param"))); //$NON-NLS-1$
259 } else if (NEWTYPE_CONTEXTTYPE.equals(contextName)) {
260 addResolver(new CodeTemplateVariableResolver(
263 .getString("CodeTemplateContextType.variable.description.typename"))); //$NON-NLS-1$
264 addResolver(new CodeTemplateVariableResolver(
267 .getString("CodeTemplateContextType.variable.description.packdeclaration"))); //$NON-NLS-1$
268 addResolver(new CodeTemplateVariableResolver(
271 .getString("CodeTemplateContextType.variable.description.typedeclaration"))); //$NON-NLS-1$
272 addResolver(new CodeTemplateVariableResolver(
275 .getString("CodeTemplateContextType.variable.description.typecomment"))); //$NON-NLS-1$
276 addCompilationUnitVariables();
277 } else if (NEWHTML_CONTEXTTYPE.equals(contextName)) {
278 addResolver(new CodeTemplateVariableResolver(
281 .getString("CodeTemplateContextType.variable.description.typename"))); //$NON-NLS-1$
282 addResolver(new CodeTemplateVariableResolver(
285 .getString("CodeTemplateContextType.variable.description.packdeclaration"))); //$NON-NLS-1$
286 addResolver(new CodeTemplateVariableResolver(
289 .getString("CodeTemplateContextType.variable.description.typedeclaration"))); //$NON-NLS-1$
290 addResolver(new CodeTemplateVariableResolver(
293 .getString("CodeTemplateContextType.variable.description.typecomment"))); //$NON-NLS-1$
294 addCompilationUnitVariables();
295 } else if (TYPECOMMENT_CONTEXTTYPE.equals(contextName)) {
296 addResolver(new CodeTemplateVariableResolver(
299 .getString("CodeTemplateContextType.variable.description.typename"))); //$NON-NLS-1$
300 addResolver(new CodeTemplateVariableResolver(
303 .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
304 addResolver(new TagsVariableResolver());
305 addCompilationUnitVariables();
307 } else if (FIELDCOMMENT_CONTEXTTYPE.equals(contextName)) {
308 addResolver(new CodeTemplateVariableResolver(
311 .getString("CodeTemplateContextType.variable.description.fieldtype"))); //$NON-NLS-1$
312 addResolver(new CodeTemplateVariableResolver(
315 .getString("CodeTemplateContextType.variable.description.fieldname"))); //$NON-NLS-1$
316 addCompilationUnitVariables();
318 } else if (METHODCOMMENT_CONTEXTTYPE.equals(contextName)) {
319 addResolver(new CodeTemplateVariableResolver(
322 .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
323 addResolver(new CodeTemplateVariableResolver(
326 .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
327 addResolver(new CodeTemplateVariableResolver(
330 .getString("CodeTemplateContextType.variable.description.returntype"))); //$NON-NLS-1$
331 addResolver(new TagsVariableResolver());
332 addCompilationUnitVariables();
334 } else if (OVERRIDECOMMENT_CONTEXTTYPE.equals(contextName)) {
335 addResolver(new CodeTemplateVariableResolver(
338 .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
339 addResolver(new CodeTemplateVariableResolver(
342 .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
343 addResolver(new CodeTemplateVariableResolver(
346 .getString("CodeTemplateContextType.variable.description.seetag"))); //$NON-NLS-1$
347 addResolver(new TagsVariableResolver());
348 addCompilationUnitVariables();
350 } else if (CONSTRUCTORCOMMENT_CONTEXTTYPE.equals(contextName)) {
351 addResolver(new CodeTemplateVariableResolver(
354 .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
355 addResolver(new TagsVariableResolver());
356 addCompilationUnitVariables();
358 } else if (GETTERCOMMENT_CONTEXTTYPE.equals(contextName)) {
359 addResolver(new CodeTemplateVariableResolver(
362 .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
363 addResolver(new CodeTemplateVariableResolver(
366 .getString("CodeTemplateContextType.variable.description.getterfieldtype"))); //$NON-NLS-1$
367 addResolver(new CodeTemplateVariableResolver(
370 .getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
371 addResolver(new CodeTemplateVariableResolver(
374 .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
375 addResolver(new CodeTemplateVariableResolver(
378 .getString("CodeTemplateContextType.variable.description.barefieldname"))); //$NON-NLS-1$
379 addCompilationUnitVariables();
381 } else if (SETTERCOMMENT_CONTEXTTYPE.equals(contextName)) {
382 addResolver(new CodeTemplateVariableResolver(
385 .getString("CodeTemplateContextType.variable.description.enclosingtype"))); //$NON-NLS-1$
386 addResolver(new CodeTemplateVariableResolver(
389 .getString("CodeTemplateContextType.variable.description.getterfieldtype"))); //$NON-NLS-1$
390 addResolver(new CodeTemplateVariableResolver(
393 .getString("CodeTemplateContextType.variable.description.getterfieldname"))); //$NON-NLS-1$
394 addResolver(new CodeTemplateVariableResolver(
397 .getString("CodeTemplateContextType.variable.description.enclosingmethod"))); //$NON-NLS-1$
398 addResolver(new CodeTemplateVariableResolver(
401 .getString("CodeTemplateContextType.variable.description.param"))); //$NON-NLS-1$
402 addResolver(new CodeTemplateVariableResolver(
405 .getString("CodeTemplateContextType.variable.description.barefieldname"))); //$NON-NLS-1$
406 addCompilationUnitVariables();
411 private void addCompilationUnitVariables() {
412 addResolver(new CodeTemplateVariableResolver(
415 .getString("CodeTemplateContextType.variable.description.filename"))); //$NON-NLS-1$
416 addResolver(new CodeTemplateVariableResolver(
419 .getString("CodeTemplateContextType.variable.description.packagename"))); //$NON-NLS-1$
420 addResolver(new CodeTemplateVariableResolver(
423 .getString("CodeTemplateContextType.variable.description.projectname"))); //$NON-NLS-1$
427 * @see net.sourceforge.phpdt.internal.corext.template.ContextType#validateVariables(net.sourceforge.phpdt.internal.corext.template.TemplateVariable[])
429 protected void validateVariables(TemplateVariable[] variables)
430 throws TemplateException {
431 ArrayList required = new ArrayList(5);
432 String contextName = getId();
433 // if (NEWTYPE_CONTEXTTYPE.equals(contextName)) {
434 // required.add(PACKAGE_DECLARATION);
435 // required.add(TYPE_DECLARATION);
437 for (int i = 0; i < variables.length; i++) {
438 String type = variables[i].getType();
439 if (getResolver(type) == null) {
440 throw new TemplateException(
443 "CodeTemplateContextType.validate.unknownvariable", type)); //$NON-NLS-1$
445 required.remove(type);
447 if (!required.isEmpty()) {
448 String missing = (String) required.get(0);
449 throw new TemplateException(
452 "CodeTemplateContextType.validate.missingvariable", missing)); //$NON-NLS-1$
454 super.validateVariables(variables);
460 * @see net.sourceforge.phpdt.internal.corext.template.ContextType#createContext()
462 public TemplateContext createContext() {
466 public static void registerContextTypes(ContextTypeRegistry registry) {
467 registry.addContextType(new CodeTemplateContextType(
468 CodeTemplateContextType.CATCHBLOCK_CONTEXTTYPE));
469 registry.addContextType(new CodeTemplateContextType(
470 CodeTemplateContextType.METHODBODY_CONTEXTTYPE));
471 registry.addContextType(new CodeTemplateContextType(
472 CodeTemplateContextType.CONSTRUCTORBODY_CONTEXTTYPE));
473 registry.addContextType(new CodeTemplateContextType(
474 CodeTemplateContextType.GETTERBODY_CONTEXTTYPE));
475 registry.addContextType(new CodeTemplateContextType(
476 CodeTemplateContextType.SETTERBODY_CONTEXTTYPE));
477 registry.addContextType(new CodeTemplateContextType(
478 CodeTemplateContextType.NEWTYPE_CONTEXTTYPE));
479 registry.addContextType(new CodeTemplateContextType(
480 CodeTemplateContextType.NEWHTML_CONTEXTTYPE));
482 registry.addContextType(new CodeTemplateContextType(
483 CodeTemplateContextType.TYPECOMMENT_CONTEXTTYPE));
484 registry.addContextType(new CodeTemplateContextType(
485 CodeTemplateContextType.FIELDCOMMENT_CONTEXTTYPE));
486 registry.addContextType(new CodeTemplateContextType(
487 CodeTemplateContextType.METHODCOMMENT_CONTEXTTYPE));
488 registry.addContextType(new CodeTemplateContextType(
489 CodeTemplateContextType.CONSTRUCTORCOMMENT_CONTEXTTYPE));
490 registry.addContextType(new CodeTemplateContextType(
491 CodeTemplateContextType.OVERRIDECOMMENT_CONTEXTTYPE));
492 registry.addContextType(new CodeTemplateContextType(
493 CodeTemplateContextType.GETTERCOMMENT_CONTEXTTYPE));
494 registry.addContextType(new CodeTemplateContextType(
495 CodeTemplateContextType.SETTERCOMMENT_CONTEXTTYPE));
499 * @see net.sourceforge.phpdt.internal.corext.template.ContextType#validate(java.lang.String)
501 public void validate(String pattern) throws TemplateException {
502 super.validate(pattern);
504 if (!isValidComment(pattern)) {
505 throw new TemplateException(
507 .getString("CodeTemplateContextType.validate.invalidcomment")); //$NON-NLS-1$
512 private boolean isValidComment(String template) {
513 IScanner scanner = ToolFactory.createScanner(true, false, false, true);// false);
514 scanner.setSource(template.toCharArray());
516 int next = scanner.getNextToken();
517 while (next == ITerminalSymbols.TokenNameCOMMENT_LINE
518 || next == ITerminalSymbols.TokenNameCOMMENT_PHPDOC
519 || next == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
520 next = scanner.getNextToken();
522 return next == ITerminalSymbols.TokenNameEOF;
523 } catch (InvalidInputException e) {