Search in sources :

Example 6 with CheckConstructorsResponse

use of org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse in project eclipse.jdt.ls by eclipse.

the class GenerateConstructorsHandlerTest method testGenerateConstructors_enum.

@Test
public void testGenerateConstructors_enum() throws ValidateEditException, CoreException, IOException {
    // @formatter:off
    ICompilationUnit unit = fPackageP.createCompilationUnit("B.java", "package p;\r\n" + "\r\n" + "public enum B {\r\n" + "}", true, null);
    // @formatter:on
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "enum B");
    CheckConstructorsResponse response = GenerateConstructorsHandler.checkConstructorsStatus(params);
    assertNotNull(response.constructors);
    assertEquals(1, response.constructors.length);
    assertNotNull(response.fields);
    assertEquals(0, response.fields.length);
    CodeGenerationSettings settings = new CodeGenerationSettings();
    settings.createComments = false;
    TextEdit edit = GenerateConstructorsHandler.generateConstructors(unit.findPrimaryType(), response.constructors, response.fields, settings, null, new NullProgressMonitor());
    assertNotNull(edit);
    JavaModelUtil.applyEdit(unit, edit, true, null);
    /* @formatter:off */
    String expected = "package p;\r\n" + "\r\n" + "public enum B {\r\n" + "	;\r\n" + "\r\n" + "	private B() {\r\n" + "	}\r\n" + "}";
    /* @formatter:on */
    compareSource(expected, unit.getSource());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) TextEdit(org.eclipse.text.edits.TextEdit) CheckConstructorsResponse(org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse) Test(org.junit.Test)

Example 7 with CheckConstructorsResponse

use of org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse in project eclipse.jdt.ls by eclipse.

the class GenerateConstructorsHandlerTest method testGenerateConstructorsAfterCursorPosition.

@Test
public void testGenerateConstructorsAfterCursorPosition() throws ValidateEditException, CoreException, IOException {
    String oldValue = preferences.getCodeGenerationInsertionLocation();
    try {
        preferences.setCodeGenerationInsertionLocation(CodeGenerationUtils.INSERT_AFTER_CURSOR);
        // @formatter:off
        fPackageP.createCompilationUnit("B.java", "package p;\r\n" + "\r\n" + "public class B {\r\n" + "	public B(String name) {\r\n" + "	}\r\n" + "	public B(String name, int id) {\r\n" + "	}\r\n" + "	private B() {\r\n" + "	}\r\n" + "}", true, null);
        ICompilationUnit unit = fPackageP.createCompilationUnit("C.java", "package p;\r\n" + "\r\n" + "public class C extends B {\r\n" + "	private static String logger;\r\n" + "	private final String uuid = \"123\";\r\n" + "	private final String instance;/*|*/\r\n" + "	private String address;\r\n" + "}", true, null);
        // @formatter:on
        CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String address");
        CheckConstructorsResponse response = GenerateConstructorsHandler.checkConstructorsStatus(params);
        assertNotNull(response.constructors);
        assertEquals(2, response.constructors.length);
        assertNotNull(response.fields);
        assertEquals(2, response.fields.length);
        CodeGenerationSettings settings = new CodeGenerationSettings();
        settings.createComments = false;
        Range cursor = CodeActionUtil.getRange(unit, "/*|*/");
        TextEdit edit = GenerateConstructorsHandler.generateConstructors(unit.findPrimaryType(), response.constructors, response.fields, settings, cursor, new NullProgressMonitor());
        assertNotNull(edit);
        JavaModelUtil.applyEdit(unit, edit, true, null);
        /* @formatter:off */
        String expected = "package p;\r\n" + "\r\n" + "public class C extends B {\r\n" + "	private static String logger;\r\n" + "	private final String uuid = \"123\";\r\n" + "	private final String instance;/*|*/\r\n" + "	public C(String name, String instance, String address) {\r\n" + "		super(name);\r\n" + "		this.instance = instance;\r\n" + "		this.address = address;\r\n" + "	}\r\n" + "	public C(String name, int id, String instance, String address) {\r\n" + "		super(name, id);\r\n" + "		this.instance = instance;\r\n" + "		this.address = address;\r\n" + "	}\r\n" + "	private String address;\r\n" + "}";
        /* @formatter:on */
        compareSource(expected, unit.getSource());
    } finally {
        preferences.setCodeGenerationInsertionLocation(oldValue);
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) TextEdit(org.eclipse.text.edits.TextEdit) CheckConstructorsResponse(org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 CheckConstructorsResponse (org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse)7 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)7 Test (org.junit.Test)6 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 TextEdit (org.eclipse.text.edits.TextEdit)5 CodeGenerationSettings (org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings)4 Range (org.eclipse.lsp4j.Range)2 Sets (com.google.common.collect.Sets)1 Modifier (java.lang.reflect.Modifier)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Stream (java.util.stream.Stream)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1