From 165d07b17540ac9f660472d20076ae3102768006 Mon Sep 17 00:00:00 2001 From: Jinbo Wang Date: Mon, 23 Oct 2023 13:46:00 +0800 Subject: [PATCH] Add new alias 'public static void main(String[] args)' for public main method' Signed-off-by: Jinbo Wang --- .../template/java/CodeSnippetTemplate.java | 15 ++++++++++-- .../handlers/CompletionHandlerTest.java | 24 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corext/template/java/CodeSnippetTemplate.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corext/template/java/CodeSnippetTemplate.java index ba01202656..0736819719 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corext/template/java/CodeSnippetTemplate.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corext/template/java/CodeSnippetTemplate.java @@ -13,6 +13,7 @@ package org.eclipse.jdt.ls.core.internal.corext.template.java; +import org.apache.commons.lang3.StringUtils; import org.eclipse.jface.text.templates.Template; public enum CodeSnippetTemplate { @@ -45,13 +46,16 @@ public enum CodeSnippetTemplate { SOUTM(TemplatePreferences.SOUTM_ID, JavaContextType.ID_STATEMENTS, TemplatePreferences.SYSTRACE_CONTENT, TemplatePreferences.SYSTRACE_DESCRIPTION), ITER(TemplatePreferences.ITER_ID, JavaContextType.ID_STATEMENTS, TemplatePreferences.FOREACH_CONTENT, TemplatePreferences.FOREACH_DESCRIPTION), PSVM(TemplatePreferences.PSVM_ID, JavaContextType.ID_MEMBERS, TemplatePreferences.MAIN_CONTENT, TemplatePreferences.MAIN_DESCRIPTION), - PRINT(TemplatePreferences.PRINT_ID, JavaContextType.ID_STATEMENTS, TemplatePreferences.SYSOUT_CONTENT, TemplatePreferences.SYSOUT_DESCRIPTION); + PRINT(TemplatePreferences.PRINT_ID, JavaContextType.ID_STATEMENTS, TemplatePreferences.SYSOUT_CONTENT, TemplatePreferences.SYSOUT_DESCRIPTION), + PUBLIC_MAIN(TemplatePreferences.PUBLIC_MAIN_ID, "public static void main(String[] args)", JavaContextType.ID_MEMBERS, TemplatePreferences.MAIN_CONTENT, TemplatePreferences.MAIN_DESCRIPTION); + //@formatter:on private final String templateId; private final String contextType; private final String defaultContent; private final String description; + private String displayName = null; private CodeSnippetTemplate(String templatesId, String contextType, String defaultContent, String description) { this.templateId = templatesId; @@ -60,8 +64,14 @@ private CodeSnippetTemplate(String templatesId, String contextType, String defau this.description = description; } + private CodeSnippetTemplate(String templatesId, String displayName, String contextType, String defaultContent, String description) { + this(templatesId, contextType, defaultContent, description); + this.displayName = displayName; + } + public Template createTemplate() { - return new Template(this.name().toLowerCase(), this.description, this.contextType, this.defaultContent, false); + return new Template(StringUtils.isNotBlank(this.displayName) ? this.displayName : this.name().toLowerCase(), + this.description, this.contextType, this.defaultContent, false); } public String getId() { @@ -97,6 +107,7 @@ class TemplatePreferences { public static final String NEW_ID = "org.eclipse.jdt.ls.templates.new"; public static final String FIELD_ID = "org.eclipse.jdt.ls.templates.field"; public static final String PRINT_ID = "org.eclipse.jdt.ls.templates.print"; + public static final String PUBLIC_MAIN_ID = "org.eclipse.jdt.ls.templates.publicmain"; // DefaultContents public static final String SYSOUT_CONTENT = "System.out.println($${0});"; diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java index b7750b2ba9..18387127df 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java @@ -1383,7 +1383,7 @@ public void testSnippet_nested_inner_interface() throws JavaModelException { assertFalse(items.isEmpty()); items.sort((i1, i2) -> (i1.getSortText().compareTo(i2.getSortText()))); - CompletionItem item = items.get(14); + CompletionItem item = items.get(15); assertEquals("interface", item.getLabel()); String te = item.getInsertText(); assertEquals("/**\n * ${1:InnerTest_1}\n */\npublic interface ${1:InnerTest_1} {\n\n\t${0}\n}", te); @@ -1549,7 +1549,7 @@ public void testSnippet_nested_inner_class() throws JavaModelException { assertFalse(items.isEmpty()); items.sort((i1, i2) -> (i1.getSortText().compareTo(i2.getSortText()))); - CompletionItem item = items.get(13); + CompletionItem item = items.get(14); assertEquals("class", item.getLabel()); String te = item.getInsertText(); assertNotNull(te); @@ -3852,6 +3852,26 @@ void f() { assertEquals(new Range(new Position(2, 2), new Position(2, 6)), item.getTextEdit().map(TextEdit::getRange, InsertReplaceEdit::getReplace)); } + @Test + public void testCompletion_publicmainSnippet() throws JavaModelException { + preferenceManager.getPreferences().setCompletionLazyResolveTextEditEnabled(false); + preferenceManager.getPreferences().setCompletionMatchCaseMode(CompletionMatchCaseMode.FIRSTLETTER); + ICompilationUnit unit = getWorkingCopy( + //@formatter:off + "src/java/Foo.java", + """ + class Foo { + public + }; + """); + //@formatter:on + CompletionList list = requestCompletions(unit, "public"); + assertNotNull(list); + assertEquals(2, list.getItems().size()); + CompletionItem item = list.getItems().get(1); + assertEquals("public static void main(String[] args)", item.getLabel()); + } + @Test public void testCompletion_forNonPrimitiveArrayTypeReceivers() throws Exception { ICompilationUnit unit = getWorkingCopy("src/java/Arr.java", """