Search in sources :

Example 1 with CheckConstructorsResponse

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

the class SourceAssistProcessor method getGenerateConstructorsAction.

private Optional<Either<Command, CodeAction>> getGenerateConstructorsAction(CodeActionParams params, IInvocationContext context, IType type, String kind, IProgressMonitor monitor) {
    try {
        if (type == null || type.isAnnotation() || type.isInterface() || type.isAnonymous() || type.getCompilationUnit() == null) {
            return Optional.empty();
        }
    } catch (JavaModelException e) {
        return Optional.empty();
    }
    if (preferenceManager.getClientPreferences().isGenerateConstructorsPromptSupported()) {
        CheckConstructorsResponse status = GenerateConstructorsHandler.checkConstructorStatus(type, monitor);
        if (status.constructors.length == 0) {
            return Optional.empty();
        }
        if (status.constructors.length == 1 && status.fields.length == 0) {
            CodeActionProposal generateConstructorsProposal = (pm) -> {
                TextEdit edit = GenerateConstructorsHandler.generateConstructors(type, status.constructors, status.fields, params.getRange(), pm);
                return convertToWorkspaceEdit(type.getCompilationUnit(), edit);
            };
            return getCodeActionFromProposal(params.getContext(), type.getCompilationUnit(), ActionMessages.GenerateConstructorsAction_label, kind, generateConstructorsProposal);
        }
        Command command = new Command(ActionMessages.GenerateConstructorsAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT, Collections.singletonList(params));
        if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS)) {
            CodeAction codeAction = new CodeAction(ActionMessages.GenerateConstructorsAction_ellipsisLabel);
            codeAction.setKind(kind);
            codeAction.setCommand(command);
            codeAction.setDiagnostics(Collections.emptyList());
            return Optional.of(Either.forRight(codeAction));
        } else {
            return Optional.of(Either.forLeft(command));
        }
    }
    return Optional.empty();
}
Also used : CodeActionProposal(org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) CoreException(org.eclipse.core.runtime.CoreException) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) GenerateToStringHandler(org.eclipse.jdt.ls.core.internal.handlers.GenerateToStringHandler) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) Statement(org.eclipse.jdt.core.dom.Statement) CodeActionHandler(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandler) DiagnosticsHelper(org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) VariableDeclarationFixCore(org.eclipse.jdt.internal.corext.fix.VariableDeclarationFixCore) CodeActionProposal(org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal) OrganizeImportsHandler(org.eclipse.jdt.ls.core.internal.handlers.OrganizeImportsHandler) ASTNodes(org.eclipse.jdt.internal.corext.dom.ASTNodes) CodeAction(org.eclipse.lsp4j.CodeAction) ASTNode(org.eclipse.jdt.core.dom.ASTNode) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) IProposalRelevance(org.eclipse.jdt.ls.core.internal.corrections.proposals.IProposalRelevance) FixCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal) Set(java.util.Set) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TextEditConverter(org.eclipse.jdt.ls.core.internal.TextEditConverter) Sets(com.google.common.collect.Sets) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GenerateDelegateMethodsHandler(org.eclipse.jdt.ls.core.internal.handlers.GenerateDelegateMethodsHandler) IType(org.eclipse.jdt.core.IType) IProposableFix(org.eclipse.jdt.internal.corext.fix.IProposableFix) IInvocationContext(org.eclipse.jdt.ls.core.internal.corrections.IInvocationContext) IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore) List(java.util.List) Stream(java.util.stream.Stream) Command(org.eclipse.lsp4j.Command) Modifier(java.lang.reflect.Modifier) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) Optional(java.util.Optional) ChangeUtil(org.eclipse.jdt.ls.core.internal.ChangeUtil) GenerateGetterSetterOperation(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation) IField(org.eclipse.jdt.core.IField) JavaModelException(org.eclipse.jdt.core.JavaModelException) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) CodeGenerationUtils(org.eclipse.jdt.ls.core.internal.handlers.CodeGenerationUtils) CorrectionMessages(org.eclipse.jdt.ls.core.internal.corrections.CorrectionMessages) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ArrayList(java.util.ArrayList) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) GenerateConstructorsHandler(org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) CheckConstructorsResponse(org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse) AccessorField(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField) JdtFlags(org.eclipse.jdt.internal.corext.util.JdtFlags) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) LspVariableBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) TextEdit(org.eclipse.text.edits.TextEdit) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) IJavaElement(org.eclipse.jdt.core.IJavaElement) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Collections(java.util.Collections) JavaModelException(org.eclipse.jdt.core.JavaModelException) Command(org.eclipse.lsp4j.Command) TextEdit(org.eclipse.text.edits.TextEdit) CodeAction(org.eclipse.lsp4j.CodeAction) CheckConstructorsResponse(org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse)

Example 2 with CheckConstructorsResponse

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

the class GenerateConstructorsHandlerTest method testCheckConstructorStatus.

@Test
public void testCheckConstructorStatus() throws JavaModelException {
    // @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);
    assertEquals("B", response.constructors[0].name);
    assertTrue(Arrays.equals(new String[] { "String" }, response.constructors[0].parameters));
    assertEquals("B", response.constructors[1].name);
    assertTrue(Arrays.equals(new String[] { "String", "int" }, response.constructors[1].parameters));
    assertNotNull(response.fields);
    assertEquals(2, response.fields.length);
    assertEquals("instance", response.fields[0].name);
    assertEquals("String", response.fields[0].type);
    assertEquals("address", response.fields[1].name);
    assertEquals("String", response.fields[1].type);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CheckConstructorsResponse(org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse) Test(org.junit.Test)

Example 3 with CheckConstructorsResponse

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

the class GenerateConstructorsHandlerTest method testCheckConstructorStatus_enum.

@Test
public void testCheckConstructorStatus_enum() throws JavaModelException {
    // @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);
    assertEquals("Object", response.constructors[0].name);
    assertNotNull(response.constructors[0].parameters);
    assertEquals(0, response.constructors[0].parameters.length);
    assertNotNull(response.fields);
    assertEquals(0, response.fields.length);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CheckConstructorsResponse(org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse) Test(org.junit.Test)

Example 4 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.

@Test
public void testGenerateConstructors() throws ValidateEditException, CoreException, IOException {
    // @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;
    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 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" + "	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" + "}";
    /* @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 5 with CheckConstructorsResponse

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

the class GenerateConstructorsHandlerTest method testGenerateConstructorsBeforeCursorPosition.

@Test
public void testGenerateConstructorsBeforeCursorPosition() throws ValidateEditException, CoreException, IOException {
    String oldValue = preferences.getCodeGenerationInsertionLocation();
    try {
        preferences.setCodeGenerationInsertionLocation(CodeGenerationUtils.INSERT_BEFORE_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" + "	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 final String instance;/*|*/\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