Java Code Examples for org.eclipse.jface.text.templates.TemplateTranslator#translate()

The following examples show how to use org.eclipse.jface.text.templates.TemplateTranslator#translate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentSnippetTemplateContext.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException
{
	if (!canEvaluate(template))
		return null;

	try
	{
		this.template = template;

		TemplateTranslator translator = new SnippetTemplateTranslator();
		TemplateBuffer buffer = translator.translate(template);

		getContextType().resolve(buffer, this);

		return buffer;
	}
	finally
	{
		this.template = null;
	}
}
 
Example 2
Source File: CodeTemplateContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
	// test that all variables are defined
	Iterator<TemplateVariableResolver> iterator= getContextType().resolvers();
	while (iterator.hasNext()) {
		TemplateVariableResolver var= iterator.next();
		if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) {
			Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	if (!canEvaluate(template))
		return null;

	String pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter);

	TemplateTranslator translator= new TemplateTranslator();
	TemplateBuffer buffer= translator.translate(pattern);
	getContextType().resolve(buffer, this);
	return buffer;
}
 
Example 3
Source File: JavaDocContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
	TemplateTranslator translator= new TemplateTranslator();
	TemplateBuffer buffer= translator.translate(template);

	getContextType().resolve(buffer, this);

	IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore();
	boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER);

	IJavaProject project= getJavaProject();
	JavaFormatter formatter= new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project);
	formatter.format(buffer, this);

	return buffer;
}
 
Example 4
Source File: TemplateValidator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkParameterSyntax(Variable variable) {
	//worst case check
	TemplateTranslator templateTranslator = new TemplateTranslator();
	String text = NodeModelUtils.getNode(variable).getText();
	try {
		templateTranslator.translate(text);
	} catch (TemplateException e) {
		error(templateTranslator.getErrorMessage(), variable, TemplatesPackage.Literals.VARIABLE__NAME);
	}
}
 
Example 5
Source File: XtextTemplateContext.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
	if (!canEvaluate(template))
		return null;

	TemplateTranslator translator = createTemplateTranslator();
	TemplateBuffer buffer = translator.translate(template);

	getContextType().resolve(buffer, this);

	return buffer;
}
 
Example 6
Source File: XtextTemplateContext.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.3
 */
public TemplateBuffer evaluateForDisplay(Template template) throws BadLocationException, TemplateException {
	if (!canEvaluate(template))
		return null;

	TemplateTranslator translator = new TemplateTranslator();
	TemplateBuffer buffer = translator.translate(template);

	getContextType().resolve(buffer, this);

	return buffer;
}
 
Example 7
Source File: SourceCodeTemplateContext.java    From xds-ide with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {

    TemplateTranslator translator= new TemplateTranslator();
       TemplateBuffer buffer= translator.translate(template);
       
       templateBody = buffer.getString(); // required to process multiline 'line_selection' 

       getContextType().resolve(buffer, this);

	getIndentation();

	///* Indents the variables */
	//TemplateVariable[] variables = indentVariableOffsets(buffer, indentation.length());
	TemplateVariable[] variables = buffer.getVariables();
	
	/* Indents the template */
	String formattedTemplate = doIndent(buffer.getString(), variables, indents); 
	
	if (cutTemplateCRLF) {
	    if (formattedTemplate.endsWith("\r\n")) {
	        formattedTemplate = formattedTemplate.substring(0, formattedTemplate.length()-2);
	    } else if (formattedTemplate.endsWith("\n")) {
               formattedTemplate = formattedTemplate.substring(0, formattedTemplate.length()-1);
	    }
	}
	
	buffer.setContent(formattedTemplate, variables);
	
	return buffer;
}
 
Example 8
Source File: SwaggerTemplateContext.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    if (!canEvaluate(template))
        return null;

    TemplateTranslator translator = createTemplateTranslator();
    TemplateBuffer buffer = translator.translate(template);

    getContextType().resolve(buffer, this);

    return buffer;
}
 
Example 9
Source File: SwaggerTemplateContext.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @since 2.3
 */
public TemplateBuffer evaluateForDisplay(Template template) throws BadLocationException, TemplateException {
    if (!canEvaluate(template))
        return null;

    TemplateTranslator translator = new TemplateTranslator();
    TemplateBuffer buffer = translator.translate(template);

    getContextType().resolve(buffer, this);

    return buffer;
}
 
Example 10
Source File: JavaContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
	public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
		clear();

		if (!canEvaluate(template))
			throw new TemplateException(JavaTemplateMessages.Context_error_cannot_evaluate);

		TemplateTranslator translator= new TemplateTranslator() {
			@Override
			protected TemplateVariable createVariable(TemplateVariableType type, String name, int[] offsets) {
//				TemplateVariableResolver resolver= getContextType().getResolver(type.getName());
//				return resolver.createVariable();

				MultiVariable variable= new JavaVariable(type, name, offsets);
				fVariables.put(name, variable);
				return variable;
			}
		};
		TemplateBuffer buffer= translator.translate(template);

		getContextType().resolve(buffer, this);

		rewriteImports();

		IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore();
		boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER);

		IJavaProject project= getJavaProject();
		JavaFormatter formatter= new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project);
		formatter.format(buffer, this);

		clear();

		return buffer;
	}
 
Example 11
Source File: LangContext.java    From goclipse with Eclipse Public License 1.0 5 votes vote down vote up
public TemplateBuffer evaluate(Template template, boolean fixIndentation) 
			throws BadLocationException, TemplateException {
		if (!canEvaluate(template))
			return null;
		
		TemplateTranslator translator= new TemplateTranslator();
		
		String pattern = template.getPattern();
//		if(fixIndentation) {
//			pattern = fixIndentation(pattern);
//		}
		TemplateBuffer buffer = translator.translate(pattern);
		
		getContextType().resolve(buffer, this);
		
		if(fixIndentation) {
			String delimiter = TextUtilities.getDefaultLineDelimiter(getDocument());
			JavaFormatter formatter = new JavaFormatter(delimiter) {
				@Override
				protected void indent(IDocument document) throws BadLocationException, MalformedTreeException {
					simpleIndent(document);
				}
			};
			formatter.format(buffer, this);
		}
		
		return buffer;
	}