Search in sources :

Example 11 with TemplateException

use of org.eclipse.jface.text.templates.TemplateException 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)

Example 12 with TemplateException

use of org.eclipse.jface.text.templates.TemplateException in project flux by eclipse.

the class StubUtility method getTypeComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getTypeComment(ICompilationUnit, String, String[], String)
	 */
public static String getTypeComment(ICompilationUnit cu, String typeQualifiedName, String[] typeParameterNames, String lineDelim) throws CoreException {
    Template template = getCodeTemplate(CodeTemplateContextType.TYPECOMMENT_ID, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, Signature.getQualifier(typeQualifiedName));
    context.setVariable(CodeTemplateContextType.TYPENAME, Signature.getSimpleName(typeQualifiedName));
    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);
    }
    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);
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(document, tagOffsets[i], position.getLength(), EMPTY, EMPTY, null, typeParameterNames, false, lineDelim);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return document.get();
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template)

Example 13 with TemplateException

use of org.eclipse.jface.text.templates.TemplateException in project flux 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)

Aggregations

TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)13 TemplateException (org.eclipse.jface.text.templates.TemplateException)13 BadLocationException (org.eclipse.jface.text.BadLocationException)12 CoreException (org.eclipse.core.runtime.CoreException)10 IDocument (org.eclipse.jface.text.IDocument)8 Document (org.eclipse.jface.text.Document)7 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 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)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 LinkedPositionGroupImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl)1 RegionImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1