use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring 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.refactoring.code.ExtractMethodRefactoring in project eclipse.jdt.ls by eclipse.
the class InferSelectionHandler method inferSelectionsForRefactor.
public static List<SelectionInfo> inferSelectionsForRefactor(InferSelectionParams 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);
List<SelectionInfo> selectionCandidates = new ArrayList<SelectionInfo>();
ASTNode parent = context.getCoveringNode();
try {
if (RefactorProposalUtility.EXTRACT_METHOD_COMMAND.equals(params.command)) {
while (parent != null && parent instanceof Expression) {
if (parent instanceof ParenthesizedExpression) {
parent = parent.getParent();
continue;
}
ExtractMethodRefactoring refactoring = new ExtractMethodRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
selectionCandidates.add(new SelectionInfo(parent.toString(), parent.getStartPosition(), parent.getLength()));
}
parent = parent.getParent();
}
} else if (RefactorProposalUtility.EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND.equals(params.command) || RefactorProposalUtility.EXTRACT_VARIABLE_COMMAND.equals(params.command)) {
while (parent != null && parent instanceof Expression) {
if (parent instanceof ParenthesizedExpression) {
parent = parent.getParent();
continue;
}
ExtractTempRefactoring refactoring = new ExtractTempRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
selectionCandidates.add(new SelectionInfo(parent.toString(), parent.getStartPosition(), parent.getLength()));
}
parent = parent.getParent();
}
} else if (RefactorProposalUtility.EXTRACT_CONSTANT_COMMAND.equals(params.command)) {
while (parent != null && parent instanceof Expression) {
if (parent instanceof ParenthesizedExpression) {
parent = parent.getParent();
continue;
}
ExtractConstantRefactoring refactoring = new ExtractConstantRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
selectionCandidates.add(new SelectionInfo(parent.toString(), parent.getStartPosition(), parent.getLength()));
}
parent = parent.getParent();
}
} else if (RefactorProposalUtility.EXTRACT_FIELD_COMMAND.equals(params.command)) {
while (parent != null && parent instanceof Expression) {
if (parent instanceof ParenthesizedExpression) {
parent = parent.getParent();
continue;
}
ExtractFieldRefactoring refactoring = new ExtractFieldRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
List<String> scopes = RefactorProposalUtility.getInitializeScopes(refactoring);
if (!scopes.isEmpty()) {
selectionCandidates.add(new SelectionInfo(parent.toString(), parent.getStartPosition(), parent.getLength(), scopes));
}
}
parent = parent.getParent();
}
}
} catch (CoreException e) {
// do nothing.
}
if (selectionCandidates.size() == 0) {
return null;
}
return selectionCandidates;
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring in project eclipse.jdt.ls by eclipse.
the class RefactorProposalUtility method getExtractMethodProposal.
private static CUCorrectionProposal getExtractMethodProposal(CodeActionParams params, IInvocationContext context, ASTNode coveringNode, boolean problemsAtLocation, Map formattingOptions, boolean returnAsCommand, boolean inferSelectionSupport) throws CoreException {
if (!(coveringNode instanceof Expression) && !(coveringNode instanceof Statement) && !(coveringNode instanceof Block)) {
return null;
}
if (coveringNode instanceof Block) {
List<Statement> statements = ((Block) coveringNode).statements();
int startIndex = getIndex(context.getSelectionOffset(), statements);
if (startIndex == -1) {
return null;
}
int endIndex = getIndex(context.getSelectionOffset() + context.getSelectionLength(), statements);
if (endIndex == -1 || endIndex <= startIndex) {
return null;
}
}
final ICompilationUnit cu = context.getCompilationUnit();
final ExtractMethodRefactoring extractMethodRefactoring = new ExtractMethodRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength(), formattingOptions);
String uniqueMethodName = getUniqueMethodName(coveringNode, "extracted");
extractMethodRefactoring.setMethodName(uniqueMethodName);
String label = CorrectionMessages.QuickAssistProcessor_extractmethod_description;
int relevance = problemsAtLocation ? IProposalRelevance.EXTRACT_METHOD_ERROR : IProposalRelevance.EXTRACT_METHOD;
if (context.getSelectionLength() == 0) {
if (!inferSelectionSupport) {
return null;
}
ASTNode parent = coveringNode;
while (parent != null && parent instanceof Expression) {
if (parent instanceof ParenthesizedExpression) {
parent = parent.getParent();
continue;
}
ExtractMethodRefactoring refactoring = new ExtractMethodRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength(), formattingOptions);
if (refactoring.checkInferConditions(new NullProgressMonitor()).isOK()) {
return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_METHOD, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_METHOD_COMMAND, params));
}
parent = parent.getParent();
}
return null;
} else if (extractMethodRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
if (returnAsCommand) {
return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_METHOD, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_METHOD_COMMAND, params));
}
LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
extractMethodRefactoring.setLinkedProposalModel(linkedProposalModel);
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_METHOD, cu, extractMethodRefactoring, relevance);
proposal.setLinkedProposalModel(linkedProposalModel);
return proposal;
}
return null;
}
Aggregations