Search in sources :

Example 1 with CodeTemplateContext

use of org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext in project eclipse.jdt.ls 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());
    CodeGenerationTemplate templateSetting = CodeGenerationTemplate.CATCHBODY;
    Template template = templateSetting.createTemplate(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.ls.core.internal.corext.template.java.CodeTemplateContext) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Example 2 with CodeTemplateContext

use of org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext in project eclipse.jdt.ls by eclipse.

the class StubUtility method getSetterMethodBodyContent.

/*
	 * Don't use this method directly, use CodeGeneration.
	 */
public static String getSetterMethodBodyContent(IJavaProject project, String destTypeName, String methodName, String fieldName, String paramName, String lineDelimiter) throws CoreException {
    CodeGenerationTemplate templateSetting = CodeGenerationTemplate.SETTERBOY;
    Template template = templateSetting.createTemplate(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);
    context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldName);
    context.setVariable(CodeTemplateContextType.PARAM, paramName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Example 3 with CodeTemplateContext

use of org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext in project eclipse.jdt.ls by eclipse.

the class StubUtility method getFieldComment.

// 
// // remove lines for empty variables
// private static String fixEmptyVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException {
// IDocument doc= new Document(buffer.getString());
// int nLines= doc.getNumberOfLines();
// MultiTextEdit edit= new MultiTextEdit();
// HashSet<Integer> removedLines= new HashSet<>();
// for (int i= 0; i < variables.length; i++) {
// TemplateVariable position= findVariable(buffer, variables[i]); // look if Javadoc tags have to be added
// if (position == null || position.getLength() > 0) {
// continue;
// }
// int[] offsets= position.getOffsets();
// for (int k= 0; k < offsets.length; k++) {
// int line= doc.getLineOfOffset(offsets[k]);
// IRegion lineInfo= doc.getLineInformation(line);
// int offset= lineInfo.getOffset();
// String str= doc.get(offset, lineInfo.getLength());
// if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) {
// int nextStart= doc.getLineOffset(line + 1);
// edit.addChild(new DeleteEdit(offset, nextStart - offset));
// }
// }
// }
// edit.apply(doc, 0);
// return doc.get();
// }
// 
/*
	 * Don't use this method directly, use CodeGeneration.
	 */
public static String getFieldComment(ICompilationUnit cu, String typeName, String fieldName, String lineDelimiter) throws CoreException {
    Template template = CodeGenerationTemplate.FIELDCOMMENT.createTemplate(cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.FIELD_TYPE, typeName);
    context.setVariable(CodeTemplateContextType.FIELD, fieldName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Example 4 with CodeTemplateContext

use of org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext in project eclipse.jdt.ls by eclipse.

the class StubUtility method getSetterComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getSetterComment(ICompilationUnit, String, String, String, String, String, String, String)
	 */
public static String getSetterComment(ICompilationUnit cu, String typeName, String methodName, String fieldName, String fieldType, String paramName, String bareFieldName, String lineDelimiter) throws CoreException {
    CodeGenerationTemplate templateSetting = CodeGenerationTemplate.SETTERCOMMENT;
    Template template = templateSetting.createTemplate(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);
    context.setVariable(CodeTemplateContextType.FIELD, fieldName);
    context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldType);
    context.setVariable(CodeTemplateContextType.BARE_FIELD_NAME, bareFieldName);
    context.setVariable(CodeTemplateContextType.PARAM, paramName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Example 5 with CodeTemplateContext

use of org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext in project eclipse.jdt.ls by eclipse.

the class StubUtility method getGetterMethodBodyContent.

// 
// private static final Set<String> VALID_TYPE_BODY_TEMPLATES;
// static {
// VALID_TYPE_BODY_TEMPLATES= new HashSet<>();
// VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.CLASSBODY_ID);
// VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.INTERFACEBODY_ID);
// VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.ENUMBODY_ID);
// VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.ANNOTATIONBODY_ID);
// }
// 
// /*
// * Don't use this method directly, use CodeGeneration.
// */
// public static String getMethodBodyContent(boolean isConstructor, IJavaProject project, String destTypeName, String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
// String templateName= isConstructor ? CodeTemplateContextType.CONSTRUCTORSTUB_ID : CodeTemplateContextType.METHODSTUB_ID;
// Template template= getCodeTemplate(templateName, project);
// if (template == null) {
// return bodyStatement;
// }
// CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
// context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
// context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
// context.setVariable(CodeTemplateContextType.BODY_STATEMENT, bodyStatement);
// String str= evaluateTemplate(context, template, new String[] { CodeTemplateContextType.BODY_STATEMENT });
// if (str == null && !Strings.containsOnlyWhitespaces(bodyStatement)) {
// return bodyStatement;
// }
// return str;
// }
/*
	 * Don't use this method directly, use CodeGeneration.
	 */
public static String getGetterMethodBodyContent(IJavaProject project, String destTypeName, String methodName, String fieldName, String lineDelimiter) throws CoreException {
    CodeGenerationTemplate templateSetting = CodeGenerationTemplate.GETTERBODY;
    Template template = templateSetting.createTemplate(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.ls.core.internal.corext.template.java.CodeTemplateContext) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Aggregations

CodeTemplateContext (org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext)9 CodeGenerationTemplate (org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)9 Template (org.eclipse.jface.text.templates.Template)9 CoreException (org.eclipse.core.runtime.CoreException)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 Document (org.eclipse.jface.text.Document)3 IDocument (org.eclipse.jface.text.IDocument)3 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)3 TemplateException (org.eclipse.jface.text.templates.TemplateException)3 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)3 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)1 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)1 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)1