Search in sources :

Example 1 with ExtractMethodRefactoring

use of org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring in project che by eclipse.

the class QuickAssistProcessor method getExtractMethodProposal.

private static boolean getExtractMethodProposal(IInvocationContext context, ASTNode coveringNode, boolean problemsAtLocation, Collection<ICommandAccess> proposals) throws CoreException {
    if (!(coveringNode instanceof Expression) && !(coveringNode instanceof Statement) && !(coveringNode instanceof Block)) {
        return false;
    }
    if (coveringNode instanceof Block) {
        List<Statement> statements = ((Block) coveringNode).statements();
        int startIndex = getIndex(context.getSelectionOffset(), statements);
        if (startIndex == -1)
            return false;
        int endIndex = getIndex(context.getSelectionOffset() + context.getSelectionLength(), statements);
        if (endIndex == -1 || endIndex <= startIndex)
            return false;
    }
    if (proposals == null) {
        return true;
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    final ExtractMethodRefactoring extractMethodRefactoring = new ExtractMethodRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    //$NON-NLS-1$
    extractMethodRefactoring.setMethodName("extracted");
    if (extractMethodRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        String label = CorrectionMessages.QuickAssistProcessor_extractmethod_description;
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractMethodRefactoring.setLinkedProposalModel(linkedProposalModel);
        Image image = JavaPluginImages.get(JavaPluginImages.DESC_MISC_PUBLIC);
        int relevance = problemsAtLocation ? IProposalRelevance.EXTRACT_METHOD_ERROR : IProposalRelevance.EXTRACT_METHOD;
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractMethodRefactoring, relevance, image);
        proposal.setCommandId(EXTRACT_METHOD_INPLACE_ID);
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    return true;
}
Also used : ExtractMethodRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LinkedProposalModel(org.eclipse.jdt.internal.corext.fix.LinkedProposalModel) Image(org.eclipse.swt.graphics.Image) RefactoringCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.RefactoringCorrectionProposal)

Aggregations

NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 LinkedProposalModel (org.eclipse.jdt.internal.corext.fix.LinkedProposalModel)1 ExtractMethodRefactoring (org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring)1 RefactoringCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.RefactoringCorrectionProposal)1 Image (org.eclipse.swt.graphics.Image)1