Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4d50f60d5c2c7a38de2eb5f107b60bbab4539b83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*******************************************************************************
 * Copyright (c) 2007, 2011 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Anton Leherbauer (Wind River Systems) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.corext.template.c;

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.core.variables.IStringVariableManager;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.jface.text.templates.ContextTypeRegistry;
import org.eclipse.jface.text.templates.GlobalTemplateVariables;
import org.eclipse.jface.text.templates.SimpleTemplateVariableResolver;
import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.eclipse.jface.text.templates.TemplateException;
import org.eclipse.jface.text.templates.TemplateVariable;
import org.eclipse.jface.text.templates.TemplateVariableResolver;
import org.eclipse.jface.text.templates.TemplateVariableType;

import org.eclipse.cdt.internal.corext.util.Messages;

/**
 * A generic template context type for file resources based on content-type.
 *
 * @since 5.0
 */
public class FileTemplateContextType extends TemplateContextType {
	public static final String CONTEXTTYPE_SUFFIX= ".contenttype_context"; //$NON-NLS-1$

	public static final String CONTENTTYPE_TEXT= "org.eclipse.core.runtime.text"; //$NON-NLS-1$
	public static final String TEXTFILE_CONTEXTTYPE= CONTENTTYPE_TEXT + CONTEXTTYPE_SUFFIX;

	/* resolver types */
	public static final String FILENAME= "file_name"; //$NON-NLS-1$
	public static final String FILEBASE= "file_base"; //$NON-NLS-1$
	public static final String FILELOCATION= "file_loc"; //$NON-NLS-1$
	public static final String FILEPATH= "file_path"; //$NON-NLS-1$
	public static final String PROJECTNAME= "project_name"; //$NON-NLS-1$

	/**
	 * Resolver that resolves to the variable defined in the context.
	 */
	static class FileTemplateVariableResolver extends SimpleTemplateVariableResolver {
		public FileTemplateVariableResolver(String type, String description) {
			super(type, description);
		}
		
		@Override
		protected String resolve(TemplateContext context) {
			String value= context.getVariable(getType());
			return value != null ? value : ""; //$NON-NLS-1$
		}
	}

	/**
	 * This date variable evaluates to the current date in a specific format.
	 */
	static class DateVariableResolver extends SimpleTemplateVariableResolver {
		private String fFormat;

		public DateVariableResolver() {
			super("date", TemplateMessages.FileTemplateContextType_variable_description_date); //$NON-NLS-1$
		}
		
		/*
		 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
		 */
		@Override
		public void resolve(TemplateVariable variable, TemplateContext context) {
			fFormat= null;
			TemplateVariableType type= variable.getVariableType();
			List<?> params= type.getParams();
			if (params.size() == 1) {
				fFormat= params.get(0).toString();
			}
			super.resolve(variable, context);
		}
		
		/*
		 * @see org.eclipse.jface.text.templates.SimpleTemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateContext)
		 */
		@Override
		protected String resolve(TemplateContext context) {
			DateFormat f;
			if (fFormat == null) {
				f= DateFormat.getDateInstance();
			} else {
				f= new SimpleDateFormat(fFormat);
			}
			return f.format(new java.util.Date());
		}
	}

	/**
	 * Resolver that resolves to the value of a core variable.
	 */
	static class CoreVariableResolver extends SimpleTemplateVariableResolver {
		private String fVariableName;
		private String[] fArguments;

		public CoreVariableResolver(String type) {
			super(type, TemplateMessages.FileTemplateContextType__variable_description_eclipse);
		}

		/*
		 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
		 */
		@Override
		public void resolve(TemplateVariable variable, TemplateContext context) {
			fVariableName= variable.getName();
			TemplateVariableType type= variable.getVariableType();
			List<?> params= type.getParams();
			fArguments= params.toArray(new String[params.size()]);
			super.resolve(variable, context);
		}
		
		/*
		 * @see org.eclipse.jface.text.templates.SimpleTemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateContext)
		 */
		@Override
		protected String resolve(TemplateContext context) {
			StringBuffer expr= new StringBuffer("${"); //$NON-NLS-1$
			expr.append(fVariableName);
			for (int i = 0; i < fArguments.length; i++) {
				expr.append(':').append(fArguments[i]);
			}
			expr.append('}');
			IStringVariableManager mgr= VariablesPlugin.getDefault().getStringVariableManager();
			try {
				return mgr.performStringSubstitution(expr.toString(), false);
			} catch (CoreException exc) {
				return expr.toString();
			}
		}
	}

	public FileTemplateContextType(String contextTypeId) {
		this(contextTypeId, contextTypeId);
	}

	public FileTemplateContextType(String contextTypeId, String contextName) {
		super(contextTypeId, contextName);

		// global
		addResolver(new GlobalTemplateVariables.Dollar());
		addResolver(new DateVariableResolver());
		addResolver(new GlobalTemplateVariables.Year());
		addResolver(new GlobalTemplateVariables.Time());
		addResolver(new GlobalTemplateVariables.User());

//		addResolver(new CoreVariableResolver("eclipse")); //$NON-NLS-1$
		
		addResourceVariables();
	}
	
	protected void addResourceVariables() {
		addResolver(new FileTemplateVariableResolver(FILENAME, TemplateMessages.FileTemplateContextType_variable_description_filename));
		addResolver(new FileTemplateVariableResolver(FILEBASE, TemplateMessages.FileTemplateContextType_variable_description_filebase));
		addResolver(new FileTemplateVariableResolver(FILELOCATION, TemplateMessages.FileTemplateContextType_variable_description_fileloc));
		addResolver(new FileTemplateVariableResolver(FILEPATH, TemplateMessages.FileTemplateContextType_variable_description_filepath));
		addResolver(new FileTemplateVariableResolver(PROJECTNAME, TemplateMessages.FileTemplateContextType_variable_description_projectname));
	}

	@Override
	protected TemplateVariableResolver getResolver(String type) {
		// compatibility with editor template variables
		if ("file".equals(type)) { //$NON-NLS-1$
			type= FILENAME;
		} else if ("project".equals(type) || "enclosing_project".equals(type)) { //$NON-NLS-1$ //$NON-NLS-2$
			type= PROJECTNAME;
		}
		return super.getResolver(type);
	}

	@Override
	protected void validateVariables(TemplateVariable[] variables) throws TemplateException {
		ArrayList<String> required=  new ArrayList<String>(5);
		for (int i= 0; i < variables.length; i++) {
			String type= variables[i].getType();
			if (getResolver(type) == null) {
				throw new TemplateException(Messages.format(TemplateMessages.FileTemplateContextType_validate_unknownvariable, type));
			}
			required.remove(type);
		}
		if (!required.isEmpty()) {
			String missing= required.get(0);
			throw new TemplateException(Messages.format(TemplateMessages.FileTemplateContextType_validate_missingvariable, missing));
		}
		super.validateVariables(variables);
	}

	/*
	 * @see org.eclipse.jface.text.templates.TemplateContextType#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
	 */
	@Override
	public void resolve(TemplateVariable variable, TemplateContext context) {
		String type= variable.getType();
		TemplateVariableResolver resolver= getResolver(type);
		if (resolver == null) {
			resolver= new FileTemplateVariableResolver(type, ""); //$NON-NLS-1$
		}
		resolver.resolve(variable, context);
	}

	public static void registerContextTypes(ContextTypeRegistry registry) {
		IContentTypeManager contentTypeMgr= Platform.getContentTypeManager();
		IContentType[] contentTypes= contentTypeMgr.getAllContentTypes();
		for (int i = 0; i < contentTypes.length; i++) {
			IContentType contentType = contentTypes[i];
			if (isTextContentType(contentType) && contentType.getFileSpecs(IContentType.FILE_EXTENSION_SPEC).length > 0) {
				final String contextTypeId= contextTypeIdForContentType(contentType);
				if (registry.getContextType(contextTypeId) == null) {
					registry.addContextType(new FileTemplateContextType(contextTypeId, contentType.getName()));
				}
			}
		}
	}

	public static String contextTypeIdForContentType(IContentType contentType) {
		return contentType.getId() + CONTEXTTYPE_SUFFIX;
	}

	public static boolean isFileTemplateContextType(String contextTypeId) {
		return contextTypeId.endsWith(CONTEXTTYPE_SUFFIX);
	}

	public static boolean isContextTypeForContentType(String contextTypeId, String contentTypeId) {
		return contextTypeId.endsWith(CONTEXTTYPE_SUFFIX) && contextTypeId.startsWith(contentTypeId);
	}

	public static String contentTypeIdForContextType(String contextTypeId) {
		return contextTypeId.substring(0, contextTypeId.length() - CONTEXTTYPE_SUFFIX.length());
	}

	private static boolean isTextContentType(IContentType contentType) {
		if (contentType == null) {
			return false;
		}
		String id= contentType.getId();
		if (id.equals(CONTENTTYPE_TEXT)) {
			return true;
		}
		if (id.indexOf(".pde.") != -1 || id.indexOf(".jdt.") != -1) { //$NON-NLS-1$ //$NON-NLS-2$
			return false;
		}
		return isTextContentType(contentType.getBaseType());
	}
}

Back to the top