Skip to content

Commit 847f305

Browse files
hopehadfieldrgrunber
authored andcommitted
Correct constructor snippet for additional classes in a file
Signed-off-by: Hope Hadfield <hhadfiel@redhat.com>
1 parent e894e35 commit 847f305

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corext/template/java/CodeSnippetTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class TemplatePreferences {
110110
public static final String TRYCATCH_CONTENT = "try {\n" + "\t$$TM_SELECTED_TEXT$${1}\n" + "} catch ($${2:Exception} $${3:e}) {\n" + "\t$${0}// TODO: handle exception\n" + "}";
111111
public static final String TRYRESOURCES_CONTENT = "try ($${1}) {\n" + "\t$$TM_SELECTED_TEXT$${2}\n" + "} catch ($${3:Exception} $${4:e}) {\n" + "\t$${0}// TODO: handle exception\n" + "}";
112112
public static final String MAIN_CONTENT = "public static void main(String[] args) {\n" + "\t$${0}\n" + "}";
113-
public static final String CTOR_CONTENT = "$${1|public,protected,private|} $${2:$$TM_FILENAME_BASE}($${3}) {\n" + "\t$${4:super();}$${0}\n" + "}";
113+
public static final String CTOR_CONTENT = "$${1|public,protected,private|} ${enclosing_simple_type}($${2}) {\n" + "\t$${3:super();}$${0}\n" + "}";
114114
public static final String METHOD_CONTENT = "$${1|public,protected,private|}$${2| , static |}$${3:void} $${4:name}($${5}) {\n" + "\t$${0}\n" + "}";
115115
public static final String NEW_CONTENT = "$${1:Object} $${2:foo} = new $${1}($${3});\n" + "$${0}";
116116
public static final String FIELD_CONTENT = "$${1|public,protected,private|} $${2:String} $${3:name};";

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corext/template/java/JavaContextType.java

+40
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313

1414
package org.eclipse.jdt.ls.core.internal.corext.template.java;
1515

16+
import org.eclipse.jdt.core.IJavaElement;
17+
import org.eclipse.jdt.core.IType;
18+
import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelsCore;
1619
import org.eclipse.jdt.internal.corext.template.java.AbstractJavaContextTypeCore;
20+
import org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext;
1721
import org.eclipse.jdt.internal.corext.template.java.IJavaContext;
22+
import org.eclipse.jdt.internal.corext.template.java.JavaTemplateMessages;
1823
import org.eclipse.jdt.internal.corext.template.java.VarResolver;
24+
import org.eclipse.jface.text.templates.TemplateContext;
1925
import org.eclipse.jface.text.templates.TemplateVariableResolver;
2026

2127
/**
@@ -75,10 +81,44 @@ public void initializeContextTypeResolvers() {
7581
// TODO: Some of the resolvers are defined in org.eclipse.jdt.ui/plugin.xml, now we have to add them manually.
7682
// See: https://github.com/eclipse-jdt/eclipse.jdt.ui/blob/dc995e7a0069e1eca58b19a4bc365032c50b0201/org.eclipse.jdt.ui/plugin.xml#L5674-L5752
7783
addResolver("var", new VarResolver());
84+
addResolver(new SimpleType());
7885
}
7986

8087
public synchronized void addResolver(String type, TemplateVariableResolver resolver) {
8188
resolver.setType(type);
8289
addResolver(resolver);
8390
}
91+
92+
// Copied from org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType.EnclosingJavaElement
93+
protected static class EnclosingJavaElement extends TemplateVariableResolver {
94+
protected final int fElementType;
95+
96+
public EnclosingJavaElement(String name, String description, int elementType) {
97+
super(name, description);
98+
fElementType = elementType;
99+
}
100+
101+
@Override
102+
protected String resolve(TemplateContext context) {
103+
IJavaElement element = ((CompilationUnitContext) context).findEnclosingElement(fElementType);
104+
if (element instanceof IType) {
105+
// Flag was changed to permit non-qualified types
106+
return JavaElementLabelsCore.getElementLabel(element, 0);
107+
}
108+
return (element == null) ? null : element.getElementName();
109+
}
110+
111+
@Override
112+
protected boolean isUnambiguous(TemplateContext context) {
113+
return resolve(context) != null;
114+
}
115+
}
116+
117+
// Copied from org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType.Type
118+
protected static class SimpleType extends EnclosingJavaElement {
119+
public SimpleType() {
120+
super("enclosing_simple_type", JavaTemplateMessages.CompilationUnitContextType_variable_description_enclosing_type, IJavaElement.TYPE);
121+
}
122+
}
84123
}
124+

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,28 @@ public void testSnippet_multiline_string() throws JavaModelException {
12821282
assertTrue(items.size() > 0);
12831283
}
12841284

1285+
@Test
1286+
public void testSnippet_ctor() throws JavaModelException {
1287+
//@formatter:off
1288+
ICompilationUnit unit = getWorkingCopy(
1289+
"src/org/sample/Test.java",
1290+
"package org.sample;\n" +
1291+
"public class MainClass {\n"
1292+
+ "}" +
1293+
"class AnotherClass {\n"
1294+
+ "ctor\n"
1295+
+ "}"
1296+
);
1297+
//@formatter:on
1298+
CompletionList list = requestCompletions(unit, "ctor");
1299+
assertNotNull(list);
1300+
List<CompletionItem> items = new ArrayList<>(list.getItems());
1301+
CompletionItem item = items.get(0);
1302+
assertEquals("ctor", item.getLabel());
1303+
String newText = item.getTextEdit().getLeft().getNewText();
1304+
assertEquals("${1|public,protected,private|} AnotherClass(${2}) {\n" + "\t${3:super();}${0}\n" + "}", newText);
1305+
}
1306+
12851307
@Test
12861308
public void testSnippet_interface() throws JavaModelException {
12871309
ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", "");

0 commit comments

Comments
 (0)