use of org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalModel in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getExtractMethodProposal.
private static boolean getExtractMethodProposal(IInvocationContext context, ASTNode coveringNode, boolean problemsAtLocation, Collection<CUCorrectionProposal> 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());
String uniqueMethodName = getUniqueMethodName(coveringNode, "extracted");
extractMethodRefactoring.setMethodName(uniqueMethodName);
if (extractMethodRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
String label = CorrectionMessages.QuickAssistProcessor_extractmethod_description;
LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
extractMethodRefactoring.setLinkedProposalModel(linkedProposalModel);
int relevance = problemsAtLocation ? IProposalRelevance.EXTRACT_METHOD_ERROR : IProposalRelevance.EXTRACT_METHOD;
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractMethodRefactoring, relevance);
proposal.setLinkedProposalModel(linkedProposalModel);
proposals.add(proposal);
}
return true;
}
use of org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalModel in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getExtractVariableProposal.
private static boolean getExtractVariableProposal(IInvocationContext context, boolean problemsAtLocation, Collection<CUCorrectionProposal> proposals) throws CoreException {
ASTNode node = context.getCoveredNode();
if (!(node instanceof Expression)) {
if (context.getSelectionLength() != 0) {
return false;
}
node = context.getCoveringNode();
if (!(node instanceof Expression)) {
return false;
}
}
final Expression expression = (Expression) node;
ITypeBinding binding = expression.resolveTypeBinding();
if (binding == null || Bindings.isVoidType(binding)) {
return false;
}
if (proposals == null) {
return true;
}
final ICompilationUnit cu = context.getCompilationUnit();
ExtractTempRefactoring extractTempRefactoring = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
if (extractTempRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
extractTempRefactoring.setReplaceAllOccurrences(true);
LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
extractTempRefactoring.setLinkedProposalModel(linkedProposalModel);
extractTempRefactoring.setCheckResultForCompileProblems(false);
String label = CorrectionMessages.QuickAssistProcessor_extract_to_local_all_description;
int relevance;
if (context.getSelectionLength() == 0) {
relevance = IProposalRelevance.EXTRACT_LOCAL_ALL_ZERO_SELECTION;
} else if (problemsAtLocation) {
relevance = IProposalRelevance.EXTRACT_LOCAL_ALL_ERROR;
} else {
relevance = IProposalRelevance.EXTRACT_LOCAL_ALL;
}
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractTempRefactoring, relevance) {
@Override
protected void init(Refactoring refactoring) throws CoreException {
ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
// expensive
etr.setTempName(etr.guessTempName());
}
};
proposal.setLinkedProposalModel(linkedProposalModel);
proposals.add(proposal);
}
ExtractTempRefactoring extractTempRefactoringSelectedOnly = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
extractTempRefactoringSelectedOnly.setReplaceAllOccurrences(false);
if (extractTempRefactoringSelectedOnly.checkInitialConditions(new NullProgressMonitor()).isOK()) {
LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
extractTempRefactoringSelectedOnly.setLinkedProposalModel(linkedProposalModel);
extractTempRefactoringSelectedOnly.setCheckResultForCompileProblems(false);
String label = CorrectionMessages.QuickAssistProcessor_extract_to_local_description;
int relevance;
if (context.getSelectionLength() == 0) {
relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION;
} else if (problemsAtLocation) {
relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR;
} else {
relevance = IProposalRelevance.EXTRACT_LOCAL;
}
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractTempRefactoringSelectedOnly, relevance) {
@Override
protected void init(Refactoring refactoring) throws CoreException {
ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
// expensive
etr.setTempName(etr.guessTempName());
}
};
proposal.setLinkedProposalModel(linkedProposalModel);
proposals.add(proposal);
}
ExtractConstantRefactoring extractConstRefactoring = new ExtractConstantRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
if (extractConstRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
extractConstRefactoring.setLinkedProposalModel(linkedProposalModel);
extractConstRefactoring.setCheckResultForCompileProblems(false);
String label = CorrectionMessages.QuickAssistProcessor_extract_to_constant_description;
int relevance;
if (context.getSelectionLength() == 0) {
relevance = IProposalRelevance.EXTRACT_CONSTANT_ZERO_SELECTION;
} else if (problemsAtLocation) {
relevance = IProposalRelevance.EXTRACT_CONSTANT_ERROR;
} else {
relevance = IProposalRelevance.EXTRACT_CONSTANT;
}
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractConstRefactoring, relevance) {
@Override
protected void init(Refactoring refactoring) throws CoreException {
ExtractConstantRefactoring etr = (ExtractConstantRefactoring) refactoring;
// expensive
etr.setConstantName(etr.guessConstantName());
}
};
proposal.setLinkedProposalModel(linkedProposalModel);
proposals.add(proposal);
}
return false;
}
Aggregations