Search in sources :

Example 1 with GenFunRefactoringMessage

use of org.erlide.wrangler.refactoring.backend.internal.GenFunRefactoringMessage in project erlide_eclipse by erlang.

the class RefactoringHandler method runGenFunRefactoring.

/**
 * Run the generalise function refactoring
 *
 * @param pages
 * @param activeShell
 * @return
 * @throws OtpErlangRangeException
 */
protected WranglerRefactoring runGenFunRefactoring(final List<WranglerPage> pages, final Shell activeShell) throws OtpErlangRangeException {
    WranglerRefactoring refactoring = null;
    final IErlMemberSelection sel = (IErlMemberSelection) GlobalParameters.getWranglerSelection();
    // Ask the user about a new name
    final NewParameterNameInputDialog dialog = new NewParameterNameInputDialog(activeShell, "New parameter name");
    dialog.open();
    if (!dialog.isFinished()) {
        return null;
    }
    final String newParamName = dialog.getData();
    dialog.close();
    // call initial RPC
    final GenFunRefactoringMessage m = (GenFunRefactoringMessage) WranglerBackendManager.getRefactoringBackend().callWithParser(new GenFunRefactoringMessage(), "generalise_eclipse", "sxxsxi", sel.getFilePath(), sel.getSelectionRange().getStartPos(), sel.getSelectionRange().getEndPos(), newParamName, sel.getSearchPath(), GlobalParameters.getTabWidth());
    // Examine the result of the refactoring: 4 cases
    if (m.getRefactoringState() == RefactoringState.OK) {
        refactoring = new GeneraliseFunctionRefactoring(State.ok, m);
    } else if (m.getRefactoringState() == RefactoringState.MULTI_INSTANCES) {
        refactoring = new GeneraliseFunctionRefactoring(State.multi_instance, m);
        pages.add(new SelectionInputPage("Generalise expression", "Please select which of them should be generalised!", "Select one of them, which should be folded!", (CostumWorkflowRefactoringWithPositionsSelection) refactoring));
    } else if (m.getRefactoringState() == RefactoringState.MORE_THAN_ONE_CLAUSE) {
        final boolean selectedClauseOnly = MessageDialog.openQuestion(activeShell, "Multiple clauses", "The function selected has multiple clauses, would you like" + " to generalise the function clause selected only?");
        refactoring = new GeneraliseFunctionRefactoring(State.more_than_one_clause, m, selectedClauseOnly);
        if (((OtpErlangList) m.getParameters().get(GenFunReturnParameterName.dupsInClause)).arity() > 0) {
            pages.add(new SelectionInputPage("Generalise expression", "Please select which of them should be generalised!", "Select one of them, which should be folded!", (CostumWorkflowRefactoringWithPositionsSelection) refactoring));
        }
    } else if (m.getRefactoringState() == RefactoringState.UNKNOWN_SIDE_EFFECT) {
        final boolean sideEffect = MessageDialog.openQuestion(activeShell, "Side effect", "Does the expression selected has side effect?");
        final OtpErlangObject noOfClausesPar = m.getParameters().get(GenFunReturnParameterName.noOfClauses);
        if (noOfClausesPar != null && ((OtpErlangLong) noOfClausesPar).intValue() > 1) {
            final boolean selectedClauseOnly = MessageDialog.openQuestion(activeShell, "Multiple clauses", "The function selected has multiple clauses, would you like" + " to generalise the function clause selected only?");
            refactoring = new GeneraliseFunctionRefactoring(State.unknown_side_effect, m, selectedClauseOnly, sideEffect);
            if (!selectedClauseOnly && ((OtpErlangList) m.getParameters().get(GenFunReturnParameterName.dupsInFun)).arity() > 0 || selectedClauseOnly && ((OtpErlangList) m.getParameters().get(GenFunReturnParameterName.dupsInClause)).arity() > 0) {
                pages.add(new SelectionInputPage("Generalise expression", "Please select which of them should be generalised!", "Select one of them, which should be folded!", (CostumWorkflowRefactoringWithPositionsSelection) refactoring));
            }
        } else {
            refactoring = new GeneraliseFunctionRefactoring(State.unknown_side_effect, m, true, sideEffect);
            pages.add(new SelectionInputPage("Generalise expression", "Please select which of them should be generalised!", "Select one of them, which should be folded!", (CostumWorkflowRefactoringWithPositionsSelection) refactoring));
        }
    } else if (m.getRefactoringState() == RefactoringState.ERROR) {
        refactoring = new GeneraliseFunctionRefactoring(State.error, m.getMessageString());
    } else {
        // error?
        return null;
    }
    return refactoring;
}
Also used : CostumWorkflowRefactoringWithPositionsSelection(org.erlide.wrangler.refactoring.core.CostumWorkflowRefactoringWithPositionsSelection) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) IErlMemberSelection(org.erlide.wrangler.refactoring.selection.IErlMemberSelection) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) GeneraliseFunctionRefactoring(org.erlide.wrangler.refactoring.core.internal.GeneraliseFunctionRefactoring) WranglerRefactoring(org.erlide.wrangler.refactoring.core.WranglerRefactoring) GenFunRefactoringMessage(org.erlide.wrangler.refactoring.backend.internal.GenFunRefactoringMessage) SelectionInputPage(org.erlide.wrangler.refactoring.ui.wizardpages.SelectionInputPage)

Aggregations

OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)1 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)1 GenFunRefactoringMessage (org.erlide.wrangler.refactoring.backend.internal.GenFunRefactoringMessage)1 CostumWorkflowRefactoringWithPositionsSelection (org.erlide.wrangler.refactoring.core.CostumWorkflowRefactoringWithPositionsSelection)1 WranglerRefactoring (org.erlide.wrangler.refactoring.core.WranglerRefactoring)1 GeneraliseFunctionRefactoring (org.erlide.wrangler.refactoring.core.internal.GeneraliseFunctionRefactoring)1 IErlMemberSelection (org.erlide.wrangler.refactoring.selection.IErlMemberSelection)1 SelectionInputPage (org.erlide.wrangler.refactoring.ui.wizardpages.SelectionInputPage)1