Search in sources :

Example 11 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project che by eclipse.

the class JavaContext method evaluateTemplate.

/**
	 * Evaluates a 'java' template in the context of a compilation unit
	 *
	 * @param template the template to be evaluated
	 * @param compilationUnit the compilation unit in which to evaluate the template
	 * @param position the position inside the compilation unit for which to evaluate the template
	 * @return the evaluated template
	 * @throws CoreException in case the template is of an unknown context type
	 * @throws BadLocationException in case the position is invalid in the compilation unit
	 * @throws TemplateException in case the evaluation fails
	 */
public static String evaluateTemplate(Template template, ICompilationUnit compilationUnit, int position) throws CoreException, BadLocationException, TemplateException {
    TemplateContextType contextType = JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(template.getContextTypeId());
    if (!(contextType instanceof CompilationUnitContextType))
        throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.ID_PLUGIN, IStatus.ERROR, JavaTemplateMessages.JavaContext_error_message, null));
    IDocument document = new Document();
    if (compilationUnit != null && compilationUnit.exists())
        document.set(compilationUnit.getSource());
    CompilationUnitContext context = ((CompilationUnitContextType) contextType).createContext(document, position, 0, compilationUnit);
    context.setForceEvaluation(true);
    TemplateBuffer buffer = context.evaluate(template);
    if (buffer == null)
        return null;
    return buffer.getString();
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) IDocument(org.eclipse.jface.text.IDocument)

Example 12 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project che by eclipse.

the class JavaDocContext method evaluate.

/*
	 * @see TemplateContext#evaluate(Template)
	 */
@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();
    //prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER);
    boolean useCodeFormatter = true;
    IJavaProject project = getJavaProject();
    JavaFormatter formatter = new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project);
    formatter.format(buffer, this);
    return buffer;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Example 13 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project che by eclipse.

the class StubUtility method evaluateTemplate.

private static String evaluateTemplate(CodeTemplateContext context, Template template) throws CoreException {
    TemplateBuffer buffer;
    try {
        buffer = context.evaluate(template);
    } catch (BadLocationException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    }
    if (buffer == null)
        return null;
    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
        return null;
    }
    return str;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 14 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project che by eclipse.

the class StubUtility method getMethodComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, String, String[], String[], String, String[], IMethod, String)
	 */
public static String getMethodComment(ICompilationUnit cu, String typeName, String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig, String[] typeParameterNames, IMethod target, boolean delegate, String lineDelimiter) throws CoreException {
    String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
    if (retTypeSig == null) {
        templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
    } else if (target != null) {
        if (delegate)
            templateName = CodeTemplateContextType.DELEGATECOMMENT_ID;
        else
            templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
    }
    Template template = getCodeTemplate(templateName, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
    if (retTypeSig != null) {
        context.setVariable(CodeTemplateContextType.RETURN_TYPE, Signature.toString(retTypeSig));
    }
    if (target != null) {
        String targetTypeName = target.getDeclaringType().getFullyQualifiedName('.');
        String[] targetParamTypeNames = getParameterTypeNamesForSeeTag(target);
        if (delegate)
            context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
        else
            context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
    }
    TemplateBuffer buffer;
    try {
        buffer = context.evaluate(template);
    } catch (BadLocationException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    }
    if (buffer == null) {
        return null;
    }
    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
        return null;
    }
    // look if Javadoc tags have to be added
    TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
    if (position == null) {
        return str;
    }
    IDocument document = new Document(str);
    String[] exceptionNames = new String[excTypeSig.length];
    for (int i = 0; i < excTypeSig.length; i++) {
        exceptionNames[i] = Signature.toString(excTypeSig[i]);
    }
    String returnType = retTypeSig != null ? Signature.toString(retTypeSig) : null;
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(document, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParameterNames, false, lineDelimiter);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return document.get();
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template) CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument)

Example 15 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project che by eclipse.

the class TemplateProposal method getAdditionalProposalInfo.

/*
	 * @see ICompletionProposal#getAdditionalProposalInfo()
	 */
public String getAdditionalProposalInfo() {
    try {
        fContext.setReadOnly(true);
        TemplateBuffer templateBuffer;
        try {
            templateBuffer = fContext.evaluate(fTemplate);
        } catch (TemplateException e) {
            return null;
        }
        IDocument document = new Document(templateBuffer.getString());
        IndentUtil.indentLines(document, new LineRange(0, document.getNumberOfLines()), null, null);
        StringBuffer buffer = new StringBuffer();
        HTMLPrinter.insertPageProlog(buffer, 0, JavadocFinder.getStyleSheet());
        HTMLPrinter.addParagraph(buffer, document.get());
        HTMLPrinter.addPageEpilog(buffer);
        return buffer.toString();
    } catch (BadLocationException e) {
        //			handleException(
        //					JavaPlugin.getActiveWorkbenchShell(), new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, "", e))); //$NON-NLS-1$
        JavaPlugin.log(e);
        return null;
    }
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) LineRange(org.eclipse.che.jface.text.source.LineRange) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)19 TemplateException (org.eclipse.jface.text.templates.TemplateException)13 BadLocationException (org.eclipse.jface.text.BadLocationException)12 CoreException (org.eclipse.core.runtime.CoreException)11 IDocument (org.eclipse.jface.text.IDocument)9 Document (org.eclipse.jface.text.Document)8 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)7 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 Template (org.eclipse.jface.text.templates.Template)6 TemplateTranslator (org.eclipse.jface.text.templates.TemplateTranslator)6 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)2 TemplateVariableResolver (org.eclipse.jface.text.templates.TemplateVariableResolver)2 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)1 ProposalPosition (org.eclipse.che.jface.text.link.ProposalPosition)1 LineRange (org.eclipse.che.jface.text.source.LineRange)1 LinkedDataImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedDataImpl)1 LinkedModeModelImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl)1