Skip to content

Commit d9b7811

Browse files
authored
Fix Missing Markdown Template contexts and MD Type comment generation (#3063)
This commit includes adding the missing Markdown code template for overrides and fixes unwanted "///" generating on types with empty params
1 parent c4e7056 commit d9b7811

7 files changed

Lines changed: 128 additions & 4 deletions

File tree

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/CodeTemplateContextType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class CodeTemplateContextType extends TemplateContextType {
6666
public static final String MARKDOWNGETTERCOMMENT_CONTEXTTYPE= "markdowngettercomment_context"; //$NON-NLS-1$
6767
public static final String MARKDOWNSETTERCOMMENT_CONTEXTTYPE= "markdownsettercomment_context"; //$NON-NLS-1$
6868
public static final String MARKDOWNMODULECOMMENT_CONTEXTTYPE= "markdownmodulecomment_context"; //$NON-NLS-1$
69+
public static final String MARKDOWNOVERRIDECOMMENT_CONTEXTTYPE= "markdownoverridecomment_context"; //$NON-NLS-1$
6970

7071
/* templates */
7172

@@ -104,6 +105,7 @@ public class CodeTemplateContextType extends TemplateContextType {
104105
public static final String MARKDOWNGETTERCOMMENT_ID= CODETEMPLATES_PREFIX + "markdowngetter" + COMMENT_SUFFIX; //$NON-NLS-1$
105106
public static final String MARKDOWNSETTERCOMMENT_ID= CODETEMPLATES_PREFIX + "markdownsetter" + COMMENT_SUFFIX; //$NON-NLS-1$
106107
public static final String MARKDOWNMODULECOMMENT_ID= CODETEMPLATES_PREFIX + "markdownmodule" + COMMENT_SUFFIX; //$NON-NLS-1$
108+
public static final String MARKDOWNOVERRIDECOMMENT_ID= CODETEMPLATES_PREFIX + "markdownoverride" + COMMENT_SUFFIX; //$NON-NLS-1$
107109

108110
/* resolver types */
109111
public static final String EXCEPTION_TYPE= "exception_type"; //$NON-NLS-1$
@@ -285,6 +287,7 @@ public CodeTemplateContextType(String contextName) {
285287
addCompilationUnitVariables();
286288
fIsComment= true;
287289
break;
290+
case MARKDOWNOVERRIDECOMMENT_CONTEXTTYPE:
288291
case OVERRIDECOMMENT_CONTEXTTYPE:
289292
addResolver(new CodeTemplateVariableResolver(ENCLOSING_TYPE, JavaManipulationMessages.CodeTemplateContextType_variable_description_enclosingtype));
290293
addResolver(new CodeTemplateVariableResolver(ENCLOSING_METHOD, JavaManipulationMessages.CodeTemplateContextType_variable_description_enclosingmethod));
@@ -403,6 +406,7 @@ public static void registerContextTypes(ContextTypeRegistry registry) {
403406
registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.MARKDOWNDELEGATECOMMENT_CONTEXTTYPE));
404407
registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.MARKDOWNGETTERCOMMENT_CONTEXTTYPE));
405408
registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.MARKDOWNSETTERCOMMENT_CONTEXTTYPE));
409+
registry.addContextType(new CodeTemplateContextType(CodeTemplateContextType.MARKDOWNOVERRIDECOMMENT_CONTEXTTYPE));
406410
}
407411

408412
/*

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/core/manipulation/StubUtility.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ public static String getTypeComment(ICompilationUnit cu, String typeQualifiedNam
326326
throw new CoreException(new Status(IStatus.ERROR, JavaManipulation.ID_PLUGIN, IStatus.ERROR, e.getMessage(), e));
327327
}
328328
}
329+
if (useMarkdown && typeParameterNames.length == 0 && params.length == 0 ) {
330+
str = document.get();
331+
str = str.replace("\n/// ", ""); //$NON-NLS-1$ //$NON-NLS-2$
332+
return str;
333+
}
329334
return document.get();
330335
}
331336

@@ -444,7 +449,7 @@ public static String getMethodComment(ICompilationUnit cu, String typeName, Stri
444449
if (delegate)
445450
templateName= useMarkdown ? CodeTemplateContextType.MARKDOWNDELEGATECOMMENT_ID : CodeTemplateContextType.DELEGATECOMMENT_ID;
446451
else
447-
templateName= CodeTemplateContextType.OVERRIDECOMMENT_ID;
452+
templateName= useMarkdown ? CodeTemplateContextType.MARKDOWNOVERRIDECOMMENT_ID : CodeTemplateContextType.OVERRIDECOMMENT_ID;
448453
}
449454
Template template= getCodeTemplate(templateName, cu.getJavaProject());
450455
if (template == null) {
@@ -720,7 +725,7 @@ public static String getMethodComment(ICompilationUnit cu, String typeName, Meth
720725
if (delegate)
721726
templateName= useMarkdown ? CodeTemplateContextType.MARKDOWNDELEGATECOMMENT_ID : CodeTemplateContextType.DELEGATECOMMENT_ID;
722727
else
723-
templateName= CodeTemplateContextType.OVERRIDECOMMENT_ID;
728+
templateName= useMarkdown ? CodeTemplateContextType.MARKDOWNOVERRIDECOMMENT_ID : CodeTemplateContextType.OVERRIDECOMMENT_ID;
724729
}
725730
Template template= getCodeTemplate(templateName, cu.getJavaProject());
726731
if (template == null) {

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/TemplateStoreTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2020 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -73,6 +73,7 @@ public class TemplateStoreTest extends CoreTests {
7373
CodeTemplateContextType.MARKDOWNGETTERCOMMENT_ID,
7474
CodeTemplateContextType.MARKDOWNSETTERCOMMENT_ID,
7575
CodeTemplateContextType.MARKDOWNMODULECOMMENT_ID,
76+
CodeTemplateContextType.MARKDOWNOVERRIDECOMMENT_ID,
7677
};
7778

7879
private TemplatePersistenceData find(String id, TemplatePersistenceData[] templateData) {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2026 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jdt.ui.tests.core.source;
15+
16+
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertTrue;
18+
19+
import org.junit.After;
20+
import org.junit.Before;
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
24+
import org.eclipse.jdt.testplugin.JavaProjectHelper;
25+
26+
import org.eclipse.core.runtime.CoreException;
27+
28+
import org.eclipse.jface.preference.IPreferenceStore;
29+
30+
import org.eclipse.jdt.core.ICompilationUnit;
31+
import org.eclipse.jdt.core.IJavaProject;
32+
import org.eclipse.jdt.core.IPackageFragment;
33+
import org.eclipse.jdt.core.IPackageFragmentRoot;
34+
35+
import org.eclipse.jdt.internal.core.manipulation.CodeTemplateContextType;
36+
import org.eclipse.jdt.internal.core.manipulation.StubUtility;
37+
38+
import org.eclipse.jdt.ui.PreferenceConstants;
39+
import org.eclipse.jdt.ui.tests.core.rules.Java23ProjectTestSetup;
40+
import org.eclipse.jdt.ui.tests.core.rules.ProjectTestSetup;
41+
42+
import org.eclipse.jdt.internal.ui.JavaPlugin;
43+
44+
public class GetTypeCommentMarkdownTest {
45+
46+
private static final String MARKDOWN_TYPECOMMENT_TEMPLATE= "/// ${type_name}" + "\n/// ${tags}";
47+
@Rule
48+
public ProjectTestSetup pts= new Java23ProjectTestSetup(false);
49+
50+
private IJavaProject fJavaProject;
51+
private IPackageFragmentRoot fSourceFolder;
52+
private IPackageFragment fPackageP;
53+
54+
@Before
55+
public void setUp() throws CoreException {
56+
fJavaProject= pts.getProject();
57+
fSourceFolder= JavaProjectHelper.addSourceContainer(fJavaProject, "src");
58+
fPackageP= fSourceFolder.createPackageFragment("p", true, null);
59+
60+
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
61+
store.setValue(PreferenceConstants.CODEGEN_USE_MARKDOWN, true);
62+
store.setValue(PreferenceConstants.CODEGEN_ADD_COMMENTS, true);
63+
64+
StubUtility.setCodeTemplate(CodeTemplateContextType.MARKDOWNTYPECOMMENT_ID, MARKDOWN_TYPECOMMENT_TEMPLATE, null);
65+
}
66+
67+
@After
68+
public void tearDown() {
69+
IPreferenceStore store= JavaPlugin.getDefault().getPreferenceStore();
70+
store.setValue(PreferenceConstants.CODEGEN_USE_MARKDOWN, false);
71+
store.setValue(PreferenceConstants.CODEGEN_ADD_COMMENTS, false);
72+
fJavaProject= null;
73+
fPackageP= null;
74+
}
75+
76+
@Test
77+
public void testNoTagsStripsTrailingMarkdownLine() throws Exception {
78+
ICompilationUnit cu= fPackageP.createCompilationUnit("A.java",
79+
"package p;\n\npublic class A {\n}\n", true, null);
80+
81+
String comment= StubUtility.getTypeComment(cu, "p.A", new String[0], new String[0], "\n");
82+
83+
assertEquals("/// A", comment);
84+
}
85+
86+
@Test
87+
public void testTypeParameterTagsNotStripped() throws Exception {
88+
ICompilationUnit cu= fPackageP.createCompilationUnit("B.java",
89+
"package p;\n\npublic class B<T> {\n}\n", true, null);
90+
91+
String comment= StubUtility.getTypeComment(cu, "p.B", new String[] { "T" }, new String[0], "\n");
92+
93+
assertTrue("expected type-name line", comment.startsWith("/// B\n"));
94+
assertTrue("expected @param <T> tag to be preserved", comment.contains("@param <T>"));
95+
}
96+
97+
@Test
98+
public void testRecordComponentParamsNotStripped() throws Exception {
99+
ICompilationUnit cu= fPackageP.createCompilationUnit("R.java",
100+
"package p;\n\npublic record R(String x, int y) {\n}\n", true, null);
101+
102+
String comment= StubUtility.getTypeComment(cu, "p.R", new String[0], new String[] { "x", "y" }, "\n");
103+
104+
assertTrue("expected type-name line", comment.startsWith("/// R\n"));
105+
assertTrue("missing @param tag for record component x", comment.contains("@param x"));
106+
assertTrue("missing @param tag for record component y", comment.contains("@param y"));
107+
}
108+
}

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/source/SourceActionTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
GenerateHashCodeEqualsTest.class,
3434
GenerateToStringTest.class,
3535
GenerateRecordConstructorTest.class,
36-
AddJavaDocStubOperationTest.class
36+
AddJavaDocStubOperationTest.class,
37+
GetTypeCommentMarkdownTest.class
3738
})
3839
public class SourceActionTests {
3940
}

org.eclipse.jdt.ui/templates/default-codetemplates.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
<template name="markdowndelegatecomment" id="org.eclipse.jdt.ui.text.codetemplates.markdowndelegatecomment" description="%CodeTemplates.markdowndelegatecomment" context="markdowndelegatecomment_context" enabled="true">/// ${tags}
7777
/// ${see_to_target}</template>
7878

79+
<template name="markdownoverridecomment" id="org.eclipse.jdt.ui.text.codetemplates.markdownoverridecomment" description="%CodeTemplates.markdownoverridecomment" context="markdownoverridecomment_context" enabled="true">/// ${see_to_overridden}</template>
80+
7981
<template name="newtype" id="org.eclipse.jdt.ui.text.codetemplates.newtype" description="%CodeTemplates.newfile" context="newtype_context" enabled="true">${filecomment}
8082
${package_declaration}
8183

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/CodeTemplateBlock.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ public int category(Object element) {
230230
return 14;
231231
case CodeTemplateContextType.MARKDOWNMETHODCOMMENT_ID:
232232
return 15;
233+
case CodeTemplateContextType.MARKDOWNOVERRIDECOMMENT_ID:
234+
return 16;
233235
case CodeTemplateContextType.MARKDOWNDELEGATECOMMENT_ID:
234236
return 17;
235237
case CodeTemplateContextType.MARKDOWNGETTERCOMMENT_ID:
@@ -298,6 +300,7 @@ public String getText(Object element) {
298300
case CodeTemplateContextType.MARKDOWNMETHODCOMMENT_ID:
299301
return PreferencesMessages.CodeTemplateBlock_methodcomment_label;
300302
case CodeTemplateContextType.OVERRIDECOMMENT_ID:
303+
case CodeTemplateContextType.MARKDOWNOVERRIDECOMMENT_ID:
301304
return PreferencesMessages.CodeTemplateBlock_overridecomment_label;
302305
case CodeTemplateContextType.DELEGATECOMMENT_ID:
303306
case CodeTemplateContextType.MARKDOWNDELEGATECOMMENT_ID:

0 commit comments

Comments
 (0)