Skip to content

Commit

Permalink
Save the fileHeader and typeComment prefs to the jdt template store
Browse files Browse the repository at this point in the history
  • Loading branch information
testforstephen authored and fbricon committed Sep 15, 2020
1 parent 6ca5602 commit c376625
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private static void setFields(CompletionItem ci, ICompilationUnit cu) {

private static String getSnippetContent(SnippetCompletionContext scc, CodeGenerationTemplate templateSetting, boolean snippetStringSupport) throws CoreException {
ICompilationUnit cu = scc.getCompilationUnit();
Template template = templateSetting.createTemplate(cu.getJavaProject());
Template template = templateSetting.createTemplate();
if (template == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static void configureFormatter(PreferenceManager preferenceManager, Proje
javaOptions.put(k, v);
});
JavaCore.setOptions(javaOptions);
JavaLanguageServerPlugin.getPreferencesManager().initialize();
JavaLanguageServerPlugin.getPreferencesManager().initializeJavaCoreOptions();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.preferences;

import org.eclipse.jdt.core.IJavaProject;
import java.util.Objects;

import org.eclipse.jdt.internal.core.manipulation.CodeTemplateContextType;
import org.eclipse.jface.text.templates.Template;

public enum CodeGenerationTemplate {
/**
* File comment template
*/
FILECOMMENT(CodeTemplatePreferences.CODETEMPLATE_FILECOMMENT, CodeTemplateContextType.FILECOMMENT_CONTEXTTYPE, ""),

/**
* Field comment template
Expand Down Expand Up @@ -173,185 +178,21 @@ private CodeGenerationTemplate(String preferenceId, String contextType, String d

}

public Template createTemplate(IJavaProject project) {
public Template createTemplate() {
return new Template(this.name(), this.preferenceId, this.contextType, this.defaultContent, false);
}
}

/**
* Preference key names, internal for now.
*/
class CodeTemplatePreferences {
private static final String CODETEMPLATES_PREFIX = "org.eclipse.jdt.ui.text.codetemplates."; //$NON-NLS-1$
public static final String COMMENT_SUFFIX = "comment"; //$NON-NLS-1$
public static final String BODY_SUFFIX = "body"; //$NON-NLS-1$
private static final String BLOCK_SUFFIX = "block"; //$NON-NLS-1$

/**
* A named preference that defines the template for field comments
*/
public static final String CODETEMPLATE_FIELDCOMMENT = CODETEMPLATES_PREFIX + "field" + COMMENT_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for constructor comments
*/
public static final String CODETEMPLATE_CONSTRUCTORCOMMENT = CODETEMPLATES_PREFIX + "constructor" + COMMENT_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for constructor body content
*/
public static final String CODETEMPLATE_CONSTRUCTORBODY = CODETEMPLATES_PREFIX + "constructor" + BODY_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for method body content
*/
public static final String CODETEMPLATE_METHODBODY = CODETEMPLATES_PREFIX + "method" + BODY_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for delegate method comments
*/
public static final String CODETEMPLATE_DELEGATECOMMENT = CODETEMPLATES_PREFIX + "delegate" + COMMENT_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for overridden method comments
*/
public static final String CODETEMPLATE_OVERRIDECOMMENT = CODETEMPLATES_PREFIX + "override" + COMMENT_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for method comments
*/
public static final String CODETEMPLATE_METHODCOMMENT = CODETEMPLATES_PREFIX + "method" + COMMENT_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for snippet body content
*/
public static final String CODETEMPLATE_CODESNIPPET = CODETEMPLATES_PREFIX + "snippet" + BODY_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for type comments
*/
public static final String CODETEMPLATE_TYPECOMMENT = CODETEMPLATES_PREFIX + "type" + COMMENT_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for getter comments
*/
public static final String CODETEMPLATE_GETTERCOMMENT = CODETEMPLATES_PREFIX + "getter" + COMMENT_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for setter comments
*/
public static final String CODETEMPLATE_SETTERCOMMENT = CODETEMPLATES_PREFIX + "setter" + COMMENT_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for getter method body content
*/
public static final String CODETEMPLATE_GETTERBODY = CODETEMPLATES_PREFIX + "getter" + BODY_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for setter method body content
*/
public static final String CODETEMPLATE_SETTERBODY = CODETEMPLATES_PREFIX + "setter" + BODY_SUFFIX; //$NON-NLS-1$

/**
* A named preference that defines the template for setter method body content
*/
public static final String CODETEMPLATE_CATCHBODY = CODETEMPLATES_PREFIX + "catch" + BLOCK_SUFFIX; //$NON-NLS-1$

public static final String CLASSSNIPPET_CONTEXTTYPE = "classsnippet_context"; //$NON-NLS-1$

public static final String INTERFACESNIPPET_CONTEXTTYPE = "interfacesnippet_context"; //$NON-NLS-1$

public static final String RECORDSNIPPET_CONTEXTTYPE = "recordsnippet_context"; //$NON-NLS-1$

/**
* Default value for field comments
*/
public static final String CODETEMPLATE_FIELDCOMMENT_DEFAULT = "/**\n" + " *\n" + " */";

/**
* Default value for constructor comments
*/
public static final String CODETEMPLATE_CONSTRUCTORCOMMENT_DEFAULT = "/**\n" + " * ${tags}\n" + " */";

/**
* Default value for delegate comments
*/
public static final String CODETEMPLATE_DELEGATECOMMENT_DEFAULT = "/**\n" + " * ${tags}\n" + " * ${see_to_target}\n" + " */\n";

/**
* Default value for override comments
*/
public static final String CODETEMPLATE_OVERRIDECOMMENT_DEFAULT = "/* (non-Javadoc)\n" + " * ${see_to_overridden}\n"
+ " */\n";

/**
* Default value for method comments
*/
public static final String CODETEMPLATE_METHODCOMMENT_DEFAULT = "/**\n" + " * ${tags}\n" + " */\n";

/**
* Default value for type comments
*/
public static final String CODETEMPLATE_TYPECOMMENT_DEFAULT = "/**\n" + " * ${tags}\n" + " */\n";

/**
* Default value for getter comments
*/
public static final String CODETEMPLATE_GETTERCOMMENT_DEFAULT = "/**\n" + " * @return the ${bare_field_name}\n" + " */";

/**
* Default value for setter comments
*/
public static final String CODETEMPLATE_SETTERCOMMENT_DEFAULT = "/**\n" + " * @param ${param} the ${bare_field_name} to set\n" + " */";

/**
* Default value for getter method body content
*/
public static final String CODETEMPLATE_GETTERBODY_DEFAULT = "return ${field};\n";

/**
* Default value for setter method body content
*/
public static final String CODETEMPLATE_SETTERBODY_DEFAULT = "${field} = ${param};\n";

/**
* Default value for constructor method body content
*/
public static final String CODETEMPLATE_CONSTRUCTORBODY_DEFAULT = "${body_statement}\n//${todo} Auto-generated constructor stub";

/**
* Default value from method body content
*/
public static final String CODETEMPLATE_METHODBODY_DEFAULT = "// ${todo} Auto-generated method stub\n${body_statement}";
/**
* Default value for catch body content
*/
public static final String CODETEMPLATE_CATCHBODY_DEFAULT = "// ${todo} Auto-generated catch block\n${exception_var}.printStackTrace();";

/**
* Default value for class snippet body content
*/
public static final String CODETEMPLATE_CLASSSNIPPET_DEFAULT = "${package_header}class ${type_name} {\n\n\t${cursor}\n}";
public Template createTemplate(String content) {
return new Template(this.name(), this.preferenceId, this.contextType, content, false);
}

/**
* Default value for public class snippet body content
*/
public static final String CODETEMPLATE_CLASSSNIPPET_PUBLIC = "${package_header}/**\n * ${type_name}\n */\npublic class ${type_name} {\n\n\t${cursor}\n}";
/**
* Default value for interface snippet body content
*/
public static final String CODETEMPLATE_INTERFACESNIPPET_DEFAULT = "${package_header}interface ${type_name} {\n\n\t${cursor}\n}";
/**
* Default value for public interface snippet body content
*/
public static final String CODETEMPLATE_INTERFACESNIPPET_PUBLIC = "${package_header}/**\n * ${type_name}\n */\npublic interface ${type_name} {\n\n\t${cursor}\n}";
/**
* Default value for record snippet body content
*/
public static final String CODETEMPLATE_RECORDSNIPPET_DEFAULT = "${package_header}record ${type_name}(${cursor}) {\n}";
/**
* Default value for public record snippet body content
*/
public static final String CODETEMPLATE_RECORDSNIPPET_PUBLIC = "${package_header}/**\n * ${type_name}\n */\npublic record ${type_name}(${cursor}) {\n}";
public static CodeGenerationTemplate getValueById(String preferenceId) {
for (CodeGenerationTemplate value : values()) {
if (Objects.equals(preferenceId, value.preferenceId)) {
return value;
}
}

return null;
}
}
Loading

0 comments on commit c376625

Please sign in to comment.