Search in sources :

Example 1 with SelectionInfo

use of org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo in project eclipse.jdt.ls by eclipse.

the class InferSelectionHandlerTest method testInferSelectionWhenExtractVariable.

@Test
public void testInferSelectionWhenExtractVariable() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public int foo() {\n");
    buf.append("        boolean b1 = true;\n");
    buf.append("        boolean b2 = false;\n");
    buf.append("        boolean b3 = true && !b2;\n");
    buf.append("        boolean b4 = b3 || /*|*/b2 && b1;\n");
    buf.append("        if ((b1||b4) && (b2||b3))\n");
    buf.append("            return 1;\n");
    buf.append("        \n");
    buf.append("        return 0;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getVerticalBarRange(cu);
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
    InferSelectionParams inferParams = new InferSelectionParams(RefactorProposalUtility.EXTRACT_VARIABLE_COMMAND, params);
    List<SelectionInfo> infos = InferSelectionHandler.inferSelectionsForRefactor(inferParams);
    Assert.assertNotNull(infos);
    Assert.assertEquals(infos.size(), 3);
    Assert.assertEquals(infos.get(0).name, "b2");
    Assert.assertEquals(infos.get(0).length, 2);
    Assert.assertEquals(infos.get(1).name, "b2 && b1");
    Assert.assertEquals(infos.get(1).length, 8);
    Assert.assertEquals(infos.get(2).name, "b3 || b2 && b1");
    Assert.assertEquals(infos.get(2).length, 19);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InferSelectionParams(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams) SelectionInfo(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)

Example 2 with SelectionInfo

use of org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo in project eclipse.jdt.ls by eclipse.

the class InferSelectionHandlerTest method testInferSelectionWhenExtractField.

@Test
public void testInferSelectionWhenExtractField() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    private String test = \"test\";\n");
    buf.append("    public int foo() {\n");
    buf.append("        int hashCode = this./*|*/test.hashCode();\n");
    buf.append("        return 0;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getVerticalBarRange(cu);
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
    InferSelectionParams inferParams = new InferSelectionParams(RefactorProposalUtility.EXTRACT_FIELD_COMMAND, params);
    List<SelectionInfo> infos = InferSelectionHandler.inferSelectionsForRefactor(inferParams);
    Assert.assertNotNull(infos);
    Assert.assertEquals(infos.size(), 2);
    Assert.assertEquals(infos.get(0).name, "this.test");
    Assert.assertEquals(infos.get(0).length, 14);
    Assert.assertEquals(infos.get(1).name, "this.test.hashCode()");
    Assert.assertEquals(infos.get(1).length, 25);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InferSelectionParams(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams) SelectionInfo(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)

Example 3 with SelectionInfo

use of org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo in project eclipse.jdt.ls by eclipse.

the class InferSelectionHandlerTest method testInferSelectionWhenExtractConstant.

@Test
public void testInferSelectionWhenExtractConstant() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public int foo() {\n");
    buf.append("        boolean b1 = true;\n");
    buf.append("        boolean b2 = false;\n");
    buf.append("        boolean b3 = /*|*/true || false;\n");
    buf.append("        boolean b4 = b3 || b2 && b1;\n");
    buf.append("        if ((b1||b4) && (b2||b3))\n");
    buf.append("            return 1;\n");
    buf.append("        \n");
    buf.append("        return 0;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getVerticalBarRange(cu);
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
    InferSelectionParams inferParams = new InferSelectionParams(RefactorProposalUtility.EXTRACT_CONSTANT_COMMAND, params);
    List<SelectionInfo> infos = InferSelectionHandler.inferSelectionsForRefactor(inferParams);
    Assert.assertNotNull(infos);
    Assert.assertEquals(infos.size(), 2);
    Assert.assertEquals(infos.get(0).name, "true");
    Assert.assertEquals(infos.get(0).length, 4);
    Assert.assertEquals(infos.get(1).name, "true || false");
    Assert.assertEquals(infos.get(1).length, 13);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InferSelectionParams(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams) SelectionInfo(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)

Example 4 with SelectionInfo

use of org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo in project eclipse.jdt.ls by eclipse.

the class InferSelectionHandlerTest method testInferSelectionWhenExtractMethod.

@Test
public void testInferSelectionWhenExtractMethod() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public int foo() {\n");
    buf.append("        boolean b1 = true;\n");
    buf.append("        boolean b2 = false;\n");
    buf.append("        boolean b3 = true && !b2;\n");
    buf.append("        if (b1 && (/*|*/b2 || b3))\n");
    buf.append("            return 1;\n");
    buf.append("        \n");
    buf.append("        return 0;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getVerticalBarRange(cu);
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
    InferSelectionParams inferParams = new InferSelectionParams(RefactorProposalUtility.EXTRACT_METHOD_COMMAND, params);
    List<SelectionInfo> infos = InferSelectionHandler.inferSelectionsForRefactor(inferParams);
    Assert.assertNotNull(infos);
    Assert.assertEquals(infos.size(), 3);
    Assert.assertEquals(infos.get(0).name, "b2");
    Assert.assertEquals(infos.get(0).length, 2);
    Assert.assertEquals(infos.get(1).name, "b2 || b3");
    Assert.assertEquals(infos.get(1).length, 8);
    Assert.assertEquals(infos.get(2).name, "b1 && (b2 || b3)");
    Assert.assertEquals(infos.get(2).length, 21);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InferSelectionParams(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams) SelectionInfo(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)

Example 5 with SelectionInfo

use of org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo in project eclipse.jdt.ls by eclipse.

the class GetRefactorEditHandler method getEditsForRefactor.

public static RefactorWorkspaceEdit getEditsForRefactor(GetRefactorEditParams params) {
    final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.context.getTextDocument().getUri());
    if (unit == null) {
        return null;
    }
    int start = DiagnosticsHelper.getStartOffset(unit, params.context.getRange());
    int end = DiagnosticsHelper.getEndOffset(unit, params.context.getRange());
    InnovationContext context = new InnovationContext(unit, start, end - start);
    context.setASTRoot(CodeActionHandler.getASTRoot(unit));
    IProblemLocationCore[] locations = CodeActionHandler.getProblemLocationCores(unit, params.context.getContext().getDiagnostics());
    boolean problemsAtLocation = locations.length != 0;
    String positionKey = DEFAULT_POSITION_KEY;
    try {
        Map formatterOptions = params.options == null ? null : FormatterHandler.getOptions(params.options, unit);
        LinkedCorrectionProposal proposal = null;
        if (RefactorProposalUtility.EXTRACT_VARIABLE_COMMAND.equals(params.command) || RefactorProposalUtility.EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND.equals(params.command) || RefactorProposalUtility.EXTRACT_CONSTANT_COMMAND.equals(params.command)) {
            SelectionInfo info = (params.commandArguments != null && !params.commandArguments.isEmpty()) ? JSONUtility.toModel(params.commandArguments.get(0), SelectionInfo.class) : null;
            if (info != null) {
                context = new InnovationContext(unit, info.offset, info.length);
            }
            proposal = (LinkedCorrectionProposal) getExtractVariableProposal(params.context, context, problemsAtLocation, params.command, formatterOptions);
        } else if (RefactorProposalUtility.ASSIGN_VARIABLE_COMMAND.equals(params.command)) {
            proposal = (LinkedCorrectionProposal) getAssignVariableProposal(params, context, problemsAtLocation, params.command, formatterOptions, locations);
        } else if (RefactorProposalUtility.ASSIGN_FIELD_COMMAND.equals(params.command)) {
            proposal = (LinkedCorrectionProposal) RefactorProposalUtility.getAssignFieldProposal(params.context, context, problemsAtLocation, formatterOptions, false, locations);
        } else if (RefactorProposalUtility.EXTRACT_METHOD_COMMAND.equals(params.command)) {
            SelectionInfo info = (params.commandArguments != null && !params.commandArguments.isEmpty()) ? JSONUtility.toModel(params.commandArguments.get(0), SelectionInfo.class) : null;
            if (info != null) {
                context = new InnovationContext(unit, info.offset, info.length);
            }
            proposal = (LinkedCorrectionProposal) getExtractMethodProposal(params.context, context, context.getCoveringNode(), problemsAtLocation, formatterOptions);
        } else if (RefactorProposalUtility.CONVERT_VARIABLE_TO_FIELD_COMMAND.equals(params.command)) {
            String initializeIn = (params.commandArguments != null && !params.commandArguments.isEmpty()) ? JSONUtility.toModel(params.commandArguments.get(0), String.class) : null;
            proposal = (LinkedCorrectionProposal) RefactorProposalUtility.getConvertVariableToFieldProposal(params.context, context, problemsAtLocation, formatterOptions, initializeIn, false);
        } else if (RefactorProposalUtility.EXTRACT_FIELD_COMMAND.equals(params.command)) {
            String initializeIn = (params.commandArguments != null && !params.commandArguments.isEmpty()) ? JSONUtility.toModel(params.commandArguments.get(0), String.class) : null;
            SelectionInfo info = (params.commandArguments != null && params.commandArguments.size() > 1) ? JSONUtility.toModel(params.commandArguments.get(1), SelectionInfo.class) : null;
            if (info != null) {
                context = new InnovationContext(unit, info.offset, info.length);
            }
            proposal = (LinkedCorrectionProposal) RefactorProposalUtility.getExtractFieldProposal(params.context, context, problemsAtLocation, formatterOptions, initializeIn, false);
        } else if (InvertBooleanUtility.INVERT_VARIABLE_COMMAND.equals(params.command)) {
            proposal = (LinkedCorrectionProposal) InvertBooleanUtility.getInvertVariableProposal(params.context, context, context.getCoveringNode(), false);
        } else if (RefactorProcessor.CONVERT_ANONYMOUS_CLASS_TO_NESTED_COMMAND.equals(params.command)) {
            proposal = RefactorProcessor.getConvertAnonymousToNestedProposal(params.context, context, context.getCoveringNode(), false);
            positionKey = "type_name";
        } else if (RefactorProposalUtility.INTRODUCE_PARAMETER_COMMAND.equals(params.command)) {
            // String initializeIn = (params.commandArguments != null && !params.commandArguments.isEmpty()) ? JSONUtility.toModel(params.commandArguments.get(0), String.class) : null;
            proposal = (LinkedCorrectionProposal) RefactorProposalUtility.getIntroduceParameterRefactoringProposals(params.context, context, context.getCoveringNode(), false, locations);
            positionKey = null;
            if (proposal instanceof RefactoringCorrectionProposal) {
                RefactoringCorrectionProposal rcp = (RefactoringCorrectionProposal) proposal;
                IntroduceParameterRefactoring refactoring = (IntroduceParameterRefactoring) rcp.getRefactoring();
                ParameterInfo parameterInfo = refactoring.getAddedParameterInfo();
                if (parameterInfo != null) {
                    positionKey = parameterInfo.getNewName();
                }
            }
        }
        if (proposal == null) {
            return null;
        }
        Change change = proposal.getChange();
        WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
        LinkedProposalModelCore linkedProposalModel = proposal.getLinkedProposalModel();
        Command additionalCommand = null;
        if (linkedProposalModel != null) {
            LinkedProposalPositionGroupCore linkedPositionGroup = linkedProposalModel.getPositionGroup(positionKey, false);
            if (linkedPositionGroup == null) {
                Iterator<LinkedProposalPositionGroupCore> iter = linkedProposalModel.getPositionGroupCoreIterator();
                while (iter.hasNext()) {
                    LinkedProposalPositionGroupCore lppgc = iter.next();
                    if (lppgc.getGroupId().startsWith(positionKey)) {
                        linkedPositionGroup = lppgc;
                        break;
                    }
                }
            }
            PositionInformation highlightPosition = getFirstTrackedNodePositionBySequenceRank(linkedPositionGroup);
            if (highlightPosition != null) {
                int offset = highlightPosition.getOffset();
                int length = highlightPosition.getLength();
                RenamePosition renamePosition = new RenamePosition(JDTUtils.toURI(unit), offset, length);
                additionalCommand = new Command("Rename", RENAME_COMMAND, Arrays.asList(renamePosition));
            }
        }
        return new RefactorWorkspaceEdit(edit, additionalCommand);
    } catch (CoreException e) {
    // do nothing.
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IntroduceParameterRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.IntroduceParameterRefactoring) PositionInformation(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroupCore.PositionInformation) LinkedProposalPositionGroupCore(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroupCore) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) ParameterInfo(org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo) Change(org.eclipse.ltk.core.refactoring.Change) RefactoringCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal) LinkedProposalModelCore(org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore) IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore) SelectionInfo(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command) LinkedCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.LinkedCorrectionProposal) Map(java.util.Map)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 SelectionInfo (org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo)5 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 AbstractSelectionTest (org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)4 InferSelectionParams (org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams)4 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)4 Range (org.eclipse.lsp4j.Range)4 Test (org.junit.Test)4 Map (java.util.Map)1 CoreException (org.eclipse.core.runtime.CoreException)1 LinkedProposalModelCore (org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore)1 LinkedProposalPositionGroupCore (org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroupCore)1 PositionInformation (org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroupCore.PositionInformation)1 IProblemLocationCore (org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore)1 ParameterInfo (org.eclipse.jdt.ls.core.internal.corext.refactoring.ParameterInfo)1 IntroduceParameterRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.IntroduceParameterRefactoring)1 InnovationContext (org.eclipse.jdt.ls.core.internal.corrections.InnovationContext)1 LinkedCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.LinkedCorrectionProposal)1 RefactoringCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal)1 Command (org.eclipse.lsp4j.Command)1