Skip to content

Commit

Permalink
Add support for month, day, hour, minute variables in typeComment.
Browse files Browse the repository at this point in the history
- Fixes redhat-developer/vscode-java#2052
- Ensure that all single digit representations are 0-prefixed

Signed-off-by: Roland Grunberg <rgrunber@redhat.com>
  • Loading branch information
rgrunber committed Aug 19, 2021
1 parent 5c066c3 commit 63713d5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

package org.eclipse.jdt.ls.core.internal.preferences;

import java.util.Calendar;

import org.eclipse.jface.text.templates.SimpleTemplateVariableResolver;
import org.eclipse.jface.text.templates.TemplateContext;

public 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$
Expand Down Expand Up @@ -191,4 +196,47 @@ public class CodeTemplatePreferences {
*/
public static final String CODETEMPLATE_RECORDSNIPPET_PUBLIC = "${package_header}/**\n * ${type_name}\n */\npublic record ${type_name}(${cursor}) {\n}";

public static class Month extends SimpleTemplateVariableResolver {
public Month() {
super("month", "Current month"); //$NON-NLS-1$ //$NON-NLS-2$
}

@Override
protected String resolve(TemplateContext context) {
return String.format("%02d", Calendar.getInstance().get(Calendar.MONTH) + 1);
}
}

public static class Day extends SimpleTemplateVariableResolver {
public Day() {
super("day", "Current day of month"); //$NON-NLS-1$ //$NON-NLS-2$
}

@Override
protected String resolve(TemplateContext context) {
return String.format("%02d", Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
}
}

public static class Hour extends SimpleTemplateVariableResolver {
public Hour() {
super("hour", "Current hour of day"); //$NON-NLS-1$ //$NON-NLS-2$
}

@Override
protected String resolve(TemplateContext context) {
return String.format("%02d", Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
}
}

public static class Minute extends SimpleTemplateVariableResolver {
public Minute() {
super("minute", "Current minute"); //$NON-NLS-1$ //$NON-NLS-2$
}

@Override
protected String resolve(TemplateContext context) {
return String.format("%02d", Calendar.getInstance().get(Calendar.MINUTE));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.StatusFactory;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.text.templates.ContextTypeRegistry;
import org.eclipse.text.templates.TemplatePersistenceData;
Expand Down Expand Up @@ -104,6 +105,13 @@ public static void initialize() {
registry.addContextType(new CodeTemplateContextType(CodeTemplatePreferences.INTERFACESNIPPET_CONTEXTTYPE));
registry.addContextType(new CodeTemplateContextType(CodeTemplatePreferences.RECORDSNIPPET_CONTEXTTYPE));

// These should be upstreamed into CodeTemplateContextType & GlobalVariables
TemplateContextType tmp = registry.getContextType(CodeTemplateContextType.TYPECOMMENT_CONTEXTTYPE);
tmp.addResolver(new CodeTemplatePreferences.Month());
tmp.addResolver(new CodeTemplatePreferences.Day());
tmp.addResolver(new CodeTemplatePreferences.Hour());
tmp.addResolver(new CodeTemplatePreferences.Minute());

JavaManipulation.setCodeTemplateContextRegistry(registry);

// Initialize templates
Expand Down

0 comments on commit 63713d5

Please sign in to comment.