Search in sources :

Example 11 with Template

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

the class ContributionTemplateStore method createTemplate.

//	private static IConfigurationElement[] getTemplateExtensions() {
//		return Platform.getExtensionRegistry().getConfigurationElementsFor(TEMPLATES_EXTENSION_POINT);
//	}
private void createTemplate(Collection map, IConfigurationElement element) {
    String contextTypeId = element.getAttribute(CONTEXT_TYPE_ID);
    // log failures since extension point id and name are mandatory
    if (contextExists(contextTypeId)) {
        String id = element.getAttribute(ID);
        if (isValidTemplateId(id)) {
            String name = element.getAttribute(NAME);
            if (name != null) {
                String pattern = element.getChildren(PATTERN)[0].getValue();
                if (pattern != null) {
                    String desc = element.getAttribute(DESCRIPTION);
                    if (desc == null)
                        //$NON-NLS-1$
                        desc = "";
                    String autoInsert = element.getAttribute(AUTO_INSERT);
                    boolean bAutoInsert;
                    if (autoInsert == null)
                        bAutoInsert = true;
                    else
                        bAutoInsert = Boolean.valueOf(autoInsert).booleanValue();
                    Template template = new Template(name, desc, contextTypeId, pattern, bAutoInsert);
                    TemplatePersistenceData data = new TemplatePersistenceData(template, true, id);
                    if (validateTemplate(template))
                        map.add(data);
                }
            }
        }
    }
}
Also used : TemplatePersistenceData(org.eclipse.che.jface.text.templates.persistence.TemplatePersistenceData) Template(org.eclipse.jface.text.templates.Template)

Example 12 with Template

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

the class TemplateEngine method complete.

/**
	 * Inspects the context of the compilation unit around <code>completionPosition</code>
	 * and feeds the collector with proposals.
	 * @param viewer the text viewer
	 * @param completionPosition the context position in the document of the text viewer
	 * @param compilationUnit the compilation unit (may be <code>null</code>)
	 */
public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
    IDocument document = viewer.getDocument();
    if (!(fContextType instanceof CompilationUnitContextType))
        return;
    Point selection = viewer.getSelectedRange();
    Position position = new Position(completionPosition, selection.y);
    // remember selected text
    String selectedText = null;
    if (selection.y != 0) {
        try {
            selectedText = document.get(selection.x, selection.y);
            document.addPosition(position);
            fPositions.put(document, position);
        } catch (BadLocationException e) {
        }
    }
    CompilationUnitContext context = ((CompilationUnitContextType) fContextType).createContext(document, position, compilationUnit);
    //$NON-NLS-1$
    context.setVariable("selection", selectedText);
    int start = context.getStart();
    int end = context.getEnd();
    IRegion region = new Region(start, end - start);
    Template[] templates = JavaPlugin.getDefault().getTemplateStore().getTemplates();
    if (selection.y == 0) {
        for (int i = 0; i != templates.length; i++) {
            Template template = templates[i];
            if (context.canEvaluate(template)) {
                fProposals.add(new TemplateProposal(template, context, region, getImage()));
            }
        }
    } else {
        if (context.getKey().length() == 0)
            context.setForceEvaluation(true);
        boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
        for (int i = 0; i != templates.length; i++) {
            Template template = templates[i];
            if (context.canEvaluate(template) && (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1))) {
                fProposals.add(new TemplateProposal(templates[i], context, region, getImage()));
            }
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) Point(org.eclipse.swt.graphics.Point) CompilationUnitContextType(org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) Template(org.eclipse.jface.text.templates.Template) CompilationUnitContext(org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 13 with Template

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

the class StubUtility method getCatchBodyContent.

public static String getCatchBodyContent(ICompilationUnit cu, String exceptionType, String variableName, String enclosingType, String enclosingMethod, String lineDelimiter) throws CoreException {
    Template template = getCodeTemplate(CodeTemplateContextType.CATCHBLOCK_ID, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, enclosingType);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, enclosingMethod);
    context.setVariable(CodeTemplateContextType.EXCEPTION_TYPE, exceptionType);
    context.setVariable(CodeTemplateContextType.EXCEPTION_VAR, variableName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 14 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 15 with Template

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

the class StubUtility method getGetterMethodBodyContent.

/*
	 * Don't use this method directly, use CodeGeneration.
	 */
public static String getGetterMethodBodyContent(IJavaProject project, String destTypeName, String methodName, String fieldName, String lineDelimiter) throws CoreException {
    String templateName = CodeTemplateContextType.GETTERSTUB_ID;
    Template template = getCodeTemplate(templateName, project);
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
    context.setVariable(CodeTemplateContextType.FIELD, fieldName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Aggregations

Template (org.eclipse.jface.text.templates.Template)37 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)28 BadLocationException (org.eclipse.jface.text.BadLocationException)7 IDocument (org.eclipse.jface.text.IDocument)7 CoreException (org.eclipse.core.runtime.CoreException)6 Document (org.eclipse.jface.text.Document)6 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)6 TemplateException (org.eclipse.jface.text.templates.TemplateException)6 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)6 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IOException (java.io.IOException)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TemplatePersistenceData (org.eclipse.che.jface.text.templates.persistence.TemplatePersistenceData)2 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)2 Position (org.eclipse.jface.text.Position)2 Region (org.eclipse.jface.text.Region)2