Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When setting date variables in typeComment and generate comment manually, some variables can't be parse. #2052

Closed
duskmont opened this issue Aug 5, 2021 · 5 comments · Fixed by eclipse-jdtls/eclipse.jdt.ls#1839

Comments

@duskmont
Copy link

duskmont commented Aug 5, 2021

When setting "${year}-${month}-${day} ${hour}:${minute}" in "java.templates.typeComment" and generate comment manually, some variables can't be parse.

Environment
  • Operating System: Windows
  • JDK version: java 8
  • Visual Studio Code version: 1.58.2
  • Java extension version: v0.80.0
Steps To Reproduce
  1. setting "java.templates.typeComment" as follow
  "java.templates.typeComment": [
    "/**",
    " * ",
    " * @author  username",
    " * @date    ${year}-${month}-${day} ${hour}:${minute}",
    " */"
  ]
  1. type "/**" above “class” line
  2. Select "javadoc comment" and Enter
  3. Comment generated as follow
/**
 *
 * @author  username
 * @date    2021-month-day hour:minute
 */
Current Result
/**
 *
 * @author  username
 * @date    2021-month-day hour:minute
 */
Expected Result
/**
 *
 * @author  username
 * @date    2021-08-05 09:00
 */
Additional Informations

Such case happened when generate class comment manually. Create java file is OK.

@testforstephen
Copy link
Collaborator

Currently only creating new file consumed these variable templates. Generating javadoc didn't.

@rgrunber
Copy link
Member

rgrunber commented Aug 9, 2021

As @testforstephen mentioned, it seems to work for newly created files for fileHeader templates, but the mechanism should be the same for typeComment. In fact, I remembered #1987 , which I really should merge.

Since ${year} is getting resolved (and not other placeholders, I wonder if there's some resolution happening on the language server side that we could potentially extend.

@rgrunber
Copy link
Member

Our docs are slightly inaccurate at https:/redhat-developer/vscode-java/wiki/Predefined-Variables-for-Java-Template-Snippets .

fileHeader is handled by vscode-java (which defines the terms directly in fileEventHandler.ts . However typeComment uses JDT-LS, and doesn't seem to support month, day, hour minute.

We could probably contribute these back into JDT at : https://git.eclipse.org/c/gerrit/jdt/eclipse.jdt.ui.git/tree/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/CodeTemplateContextType.java#n181 . The implementation is pretty easy : https://git.eclipse.org/c/platform/eclipse.platform.text.git/tree/org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java#n208

@rgrunber
Copy link
Member

We could also just make the change on our end until it finally makes it upstream.

diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java
index 9b2a855b..21337104 100644
--- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java
+++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java
@@ -15,6 +15,7 @@ package org.eclipse.jdt.ls.core.internal.preferences;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.util.Calendar;
 import java.util.Hashtable;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -46,7 +47,10 @@ import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
 import org.eclipse.jdt.ls.core.internal.IConstants;
 import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
 import org.eclipse.jdt.ls.core.internal.StatusFactory;
+import org.eclipse.jface.text.templates.SimpleTemplateVariableResolver;
 import org.eclipse.jface.text.templates.Template;
+import org.eclipse.jface.text.templates.TemplateContext;
+import org.eclipse.jface.text.templates.TemplateContextType;
 import org.eclipse.lsp4j.ClientCapabilities;
 import org.eclipse.text.templates.ContextTypeRegistry;
 import org.eclipse.text.templates.TemplatePersistenceData;
@@ -104,6 +108,9 @@ public class PreferenceManager {
 		registry.addContextType(new CodeTemplateContextType(CodeTemplatePreferences.INTERFACESNIPPET_CONTEXTTYPE));
 		registry.addContextType(new CodeTemplateContextType(CodeTemplatePreferences.RECORDSNIPPET_CONTEXTTYPE));
 
+		TemplateContextType tmp = registry.getContextType(CodeTemplateContextType.TYPECOMMENT_CONTEXTTYPE);
+		tmp.addResolver(new Month());
+
 		JavaManipulation.setCodeTemplateContextRegistry(registry);
 
 		// Initialize templates
@@ -123,6 +130,16 @@ public class PreferenceManager {
 		reloadTemplateStore();
 	}
 
+	public static class Month extends SimpleTemplateVariableResolver {
+		public Month() {
+			super("month", "Month Description"); //$NON-NLS-1$ //$NON-NLS-2$
+		}
+		@Override
+		protected String resolve(TemplateContext context) {
+			return Integer.toString(Calendar.getInstance().get(Calendar.MONTH) + 1);
+		}
+	}
+
 	public static void initializeJavaCoreOptions() {
 		Hashtable<String, String> javaCoreOptions = JavaCore.getOptions();
 		javaCoreOptions.put(JavaCore.CODEASSIST_VISIBILITY_CHECK, JavaCore.ENABLED);

@duskmont
Copy link
Author

Thanks for replay and support @rgrunber

rgrunber added a commit to rgrunber/eclipse.jdt.ls that referenced this issue Aug 18, 2021
- Fixes redhat-developer/vscode-java#2052
- Ensure that all single digit representations are 0-prefixed

Signed-off-by: Roland Grunberg <[email protected]>
rgrunber added a commit to eclipse-jdtls/eclipse.jdt.ls that referenced this issue Aug 19, 2021
- Fixes redhat-developer/vscode-java#2052
- Ensure that all single digit representations are 0-prefixed

Signed-off-by: Roland Grunberg <[email protected]>
@rgrunber rgrunber added this to the Mid September 2021 milestone Aug 19, 2021
rgrunber added a commit to rgrunber/eclipse.jdt.ls that referenced this issue Aug 20, 2021
rgrunber added a commit to eclipse-jdtls/eclipse.jdt.ls that referenced this issue Aug 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants