Search in sources :

Example 86 with Template

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

the class StubUtility method getMethodComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * This method should work with all AST levels.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, boolean, String, String[], String)
	 */
public static String getMethodComment(ICompilationUnit cu, String typeName, MethodDeclaration decl, boolean isDeprecated, String targetName, String targetMethodDeclaringTypeName, String[] targetMethodParameterTypeNames, boolean delegate, String lineDelimiter) throws CoreException {
    boolean needsTarget = targetMethodDeclaringTypeName != null && targetMethodParameterTypeNames != null;
    String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
    if (decl.isConstructor()) {
        templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
    } else if (needsTarget) {
        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, decl.getName().getIdentifier());
    if (!decl.isConstructor()) {
        context.setVariable(CodeTemplateContextType.RETURN_TYPE, ASTNodes.asString(getReturnType(decl)));
    }
    if (needsTarget) {
        if (delegate)
            context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
        else
            context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
    }
    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 textBuffer = new Document(str);
    List<TypeParameter> typeParams = shouldGenerateMethodTypeParameterTags(cu.getJavaProject()) ? decl.typeParameters() : Collections.emptyList();
    String[] typeParamNames = new String[typeParams.size()];
    for (int i = 0; i < typeParamNames.length; i++) {
        TypeParameter elem = typeParams.get(i);
        typeParamNames[i] = elem.getName().getIdentifier();
    }
    List<SingleVariableDeclaration> params = decl.parameters();
    String[] paramNames = new String[params.size()];
    for (int i = 0; i < paramNames.length; i++) {
        SingleVariableDeclaration elem = params.get(i);
        paramNames[i] = elem.getName().getIdentifier();
    }
    String[] exceptionNames = getExceptionNames(decl);
    String returnType = null;
    if (!decl.isConstructor()) {
        returnType = ASTNodes.asString(getReturnType(decl));
    }
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(textBuffer, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParamNames, isDeprecated, lineDelimiter);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return textBuffer.get();
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) TemplateException(org.eclipse.jface.text.templates.TemplateException) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) 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 87 with Template

use of org.eclipse.jface.text.templates.Template in project flux 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 88 with Template

use of org.eclipse.jface.text.templates.Template in project KaiZen-OpenAPI-Editor by RepreZen.

the class Activator method addNamedTemplates.

private void addNamedTemplates(String inlineContextId, String namedContextId, String key) {
    Template[] schemaTemplates = templateStore.getTemplates(inlineContextId);
    for (int i = 0; i < schemaTemplates.length; i++) {
        Template schemaTemplate = schemaTemplates[i];
        Template template = createNamedTemplate(schemaTemplate, namedContextId, key);
        templateStore.add(new TemplatePersistenceData(template, true));
    }
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) Template(org.eclipse.jface.text.templates.Template)

Example 89 with Template

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

the class TemplateReaderWriter method save.

/**
	 * Saves the templates as XML.
	 *
	 * @param templates the templates to save
	 * @param result the stream result to write to
	 * @throws IOException if writing the templates fails
	 */
private void save(TemplatePersistenceData[] templates, StreamResult result) throws IOException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
        Node root = document.createElement(TEMPLATE_ROOT);
        document.appendChild(root);
        for (int i = 0; i < templates.length; i++) {
            TemplatePersistenceData data = templates[i];
            Template template = data.getTemplate();
            Node node = document.createElement(TEMPLATE_ELEMENT);
            root.appendChild(node);
            NamedNodeMap attributes = node.getAttributes();
            String id = data.getId();
            if (id != null) {
                Attr idAttr = document.createAttribute(ID_ATTRIBUTE);
                idAttr.setValue(id);
                attributes.setNamedItem(idAttr);
            }
            if (template != null) {
                Attr name = document.createAttribute(NAME_ATTRIBUTE);
                name.setValue(validateXML(template.getName()));
                attributes.setNamedItem(name);
            }
            if (template != null) {
                Attr description = document.createAttribute(DESCRIPTION_ATTRIBUTE);
                description.setValue(validateXML(template.getDescription()));
                attributes.setNamedItem(description);
            }
            if (template != null) {
                Attr context = document.createAttribute(CONTEXT_ATTRIBUTE);
                context.setValue(validateXML(template.getContextTypeId()));
                attributes.setNamedItem(context);
            }
            Attr enabled = document.createAttribute(ENABLED_ATTRIBUTE);
            enabled.setValue(data.isEnabled() ? Boolean.toString(true) : Boolean.toString(false));
            attributes.setNamedItem(enabled);
            Attr deleted = document.createAttribute(DELETED_ATTRIBUTE);
            deleted.setValue(data.isDeleted() ? Boolean.toString(true) : Boolean.toString(false));
            attributes.setNamedItem(deleted);
            if (template != null) {
                Attr autoInsertable = document.createAttribute(AUTO_INSERTABLE_ATTRIBUTE);
                autoInsertable.setValue(template.isAutoInsertable() ? Boolean.toString(true) : Boolean.toString(false));
                attributes.setNamedItem(autoInsertable);
            }
            if (template != null) {
                Text pattern = document.createTextNode(validateXML(template.getPattern()));
                node.appendChild(pattern);
            }
        }
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        //$NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        DOMSource source = new DOMSource(document);
        transformer.transform(source, result);
    } catch (ParserConfigurationException e) {
        Assert.isTrue(false);
    } catch (TransformerException e) {
        if (e.getException() instanceof IOException)
            throw (IOException) e.getException();
        Assert.isTrue(false);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) Transformer(javax.xml.transform.Transformer) Node(org.w3c.dom.Node) Text(org.w3c.dom.Text) IOException(java.io.IOException) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) Template(org.eclipse.jface.text.templates.Template) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 90 with Template

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

the class TemplateStore method findTemplate.

/**
	 * Returns the first enabled template that matches both name and context type id.
	 *
	 * @param name the name of the template searched for
	 * @param contextTypeId the context type id to clip unwanted templates, or <code>null</code> if any context type is OK
	 * @return the first enabled template that matches both name and context type id, or <code>null</code> if none is found
	 */
public Template findTemplate(String name, String contextTypeId) {
    Assert.isNotNull(name);
    for (Iterator it = fTemplates.iterator(); it.hasNext(); ) {
        TemplatePersistenceData data = (TemplatePersistenceData) it.next();
        Template template = data.getTemplate();
        if (data.isEnabled() && !data.isDeleted() && (contextTypeId == null || contextTypeId.equals(template.getContextTypeId())) && name.equals(template.getName()))
            return template;
    }
    return null;
}
Also used : Iterator(java.util.Iterator) Template(org.eclipse.jface.text.templates.Template)

Aggregations

Template (org.eclipse.jface.text.templates.Template)158 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)27 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)22 TemplateException (org.eclipse.jface.text.templates.TemplateException)21 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)19 IDocument (org.eclipse.jface.text.IDocument)18 Document (org.eclipse.jface.text.Document)17 ArrayList (java.util.ArrayList)15 BadLocationException (org.eclipse.jface.text.BadLocationException)15 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)15 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)14 TemplatePersistenceData (org.eclipse.jface.text.templates.persistence.TemplatePersistenceData)13 TemplateStore (org.eclipse.jface.text.templates.persistence.TemplateStore)12 CoreException (org.eclipse.core.runtime.CoreException)11 Region (org.eclipse.jface.text.Region)11 DocumentTemplateContext (org.eclipse.jface.text.templates.DocumentTemplateContext)11 ISubReference (org.eclipse.titan.designer.AST.ISubReference)11 IRegion (org.eclipse.jface.text.IRegion)10 ITextSelection (org.eclipse.jface.text.ITextSelection)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)10