use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring 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.ExtractTempRefactoring 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;
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring in project eclipse.jdt.ls by eclipse.
the class RefactorProposalUtility method getExtractVariableProposal.
private static CUCorrectionProposal getExtractVariableProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand, boolean inferSelectionSupport) throws CoreException {
final ICompilationUnit cu = context.getCompilationUnit();
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;
}
if (inferSelectionSupport && context.getSelectionLength() == 0) {
ASTNode parent = context.getCoveringNode();
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()) {
return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_COMMAND, params));
}
parent = parent.getParent();
}
return null;
}
ExtractTempRefactoring extractTempRefactoringSelectedOnly = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength(), formatterOptions);
extractTempRefactoringSelectedOnly.setReplaceAllOccurrences(false);
if (extractTempRefactoringSelectedOnly.checkInitialConditions(new NullProgressMonitor()).isOK()) {
if (returnAsCommand) {
return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_COMMAND, params));
}
LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
extractTempRefactoringSelectedOnly.setLinkedProposalModel(linkedProposalModel);
extractTempRefactoringSelectedOnly.setCheckResultForCompileProblems(false);
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, extractTempRefactoringSelectedOnly, relevance) {
@Override
protected void init(Refactoring refactoring) throws CoreException {
ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
// expensive
etr.setTempName(etr.guessTempName());
}
};
proposal.setLinkedProposalModel(linkedProposalModel);
return proposal;
}
return null;
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring in project eclipse.jdt.ls by eclipse.
the class RefactorProposalUtility method getExtractVariableAllOccurrenceProposal.
private static CUCorrectionProposal getExtractVariableAllOccurrenceProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand, boolean inferSelectionSupport) throws CoreException {
final ICompilationUnit cu = context.getCompilationUnit();
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;
}
if (inferSelectionSupport && context.getSelectionLength() == 0) {
ASTNode parent = context.getCoveringNode();
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()) {
return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND, params));
}
parent = parent.getParent();
}
return null;
}
ExtractTempRefactoring extractTempRefactoring = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength(), formatterOptions);
if (extractTempRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
if (returnAsCommand) {
return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND, params));
}
extractTempRefactoring.setReplaceAllOccurrences(true);
LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
extractTempRefactoring.setLinkedProposalModel(linkedProposalModel);
extractTempRefactoring.setCheckResultForCompileProblems(false);
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, extractTempRefactoring, relevance) {
@Override
protected void init(Refactoring refactoring) throws CoreException {
ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
// expensive
etr.setTempName(etr.guessTempName());
}
};
proposal.setLinkedProposalModel(linkedProposalModel);
return proposal;
}
return null;
}
Aggregations