use of org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal 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.corrections.proposals.RefactoringCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getInlineProposal.
private boolean getInlineProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
if (resultingCollections == null) {
return false;
}
if (!(node instanceof SimpleName)) {
return false;
}
SimpleName name = (SimpleName) node;
IBinding binding = name.resolveBinding();
try {
if (binding instanceof IVariableBinding) {
IVariableBinding varBinding = (IVariableBinding) binding;
if (varBinding.isParameter()) {
return false;
}
if (varBinding.isField()) {
// Inline Constant (static final field)
if (RefactoringAvailabilityTesterCore.isInlineConstantAvailable((IField) varBinding.getJavaElement())) {
InlineConstantRefactoring refactoring = new InlineConstantRefactoring(context.getCompilationUnit(), context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
if (refactoring != null && refactoring.checkInitialConditions(new NullProgressMonitor()).isOK() && refactoring.getReferences(new NullProgressMonitor(), new RefactoringStatus()).length > 0) {
refactoring.setRemoveDeclaration(refactoring.isDeclarationSelected());
refactoring.setReplaceAllReferences(refactoring.isDeclarationSelected());
CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS);
final CreateChangeOperation create = new CreateChangeOperation(check, RefactoringStatus.FATAL);
create.run(new NullProgressMonitor());
String label = ActionMessages.InlineConstantRefactoringAction_label;
int relevance = IProposalRelevance.INLINE_LOCAL;
ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(label, CodeActionKind.RefactorInline, create.getChange(), relevance);
resultingCollections.add(proposal);
return true;
}
}
return false;
}
ASTNode decl = context.getASTRoot().findDeclaringNode(varBinding);
if (!(decl instanceof VariableDeclarationFragment) || decl.getLocationInParent() != VariableDeclarationStatement.FRAGMENTS_PROPERTY) {
return false;
}
// Inline Local Variable
if (binding.getJavaElement() instanceof ILocalVariable && RefactoringAvailabilityTesterCore.isInlineTempAvailable((ILocalVariable) binding.getJavaElement())) {
InlineTempRefactoring refactoring = new InlineTempRefactoring((VariableDeclaration) decl);
boolean status;
try {
status = refactoring.checkAllConditions(new NullProgressMonitor()).isOK();
} catch (Exception e) {
// ignore
status = false;
}
if (status && refactoring.getReferences().length > 0) {
String label = CorrectionMessages.QuickAssistProcessor_inline_local_description;
int relevance = IProposalRelevance.INLINE_LOCAL;
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, CodeActionKind.RefactorInline, context.getCompilationUnit(), refactoring, relevance);
resultingCollections.add(proposal);
return true;
}
}
} else if (binding instanceof IMethodBinding) {
// Inline Method
if (RefactoringAvailabilityTesterCore.isInlineMethodAvailable((IMethod) binding.getJavaElement())) {
InlineMethodRefactoring refactoring = InlineMethodRefactoring.create(context.getCompilationUnit(), context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
if (refactoring != null && refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS);
final CreateChangeOperation create = new CreateChangeOperation(check, RefactoringStatus.FATAL);
create.run(new NullProgressMonitor());
String label = ActionMessages.InlineMethodRefactoringAction_label;
int relevance = IProposalRelevance.INLINE_LOCAL;
ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(label, CodeActionKind.RefactorInline, create.getChange(), relevance);
resultingCollections.add(proposal);
return true;
}
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
}
return false;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getConvertAnonymousToNestedProposal.
public static RefactoringCorrectionProposal getConvertAnonymousToNestedProposal(CodeActionParams params, IInvocationContext context, final ASTNode node, boolean returnAsCommand) throws CoreException {
String label = CorrectionMessages.QuickAssistProcessor_convert_anonym_to_nested;
ClassInstanceCreation cic = getClassInstanceCreation(node);
if (cic == null) {
return null;
}
final AnonymousClassDeclaration anonymTypeDecl = cic.getAnonymousClassDeclaration();
if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null) {
return null;
}
final ConvertAnonymousToNestedRefactoring refactoring = new ConvertAnonymousToNestedRefactoring(anonymTypeDecl);
if (!refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
return null;
}
if (returnAsCommand) {
return new RefactoringCorrectionCommandProposal(label, CodeActionKind.Refactor, context.getCompilationUnit(), IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED, RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, Arrays.asList(CONVERT_ANONYMOUS_CLASS_TO_NESTED_COMMAND, params));
}
String extTypeName = ASTNodes.getTypeName(cic.getType());
ITypeBinding anonymTypeBinding = anonymTypeDecl.resolveBinding();
String className;
if (anonymTypeBinding.getInterfaces().length == 0) {
className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_interface, extTypeName);
} else {
className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_class, extTypeName);
}
String[][] existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className);
int i = 1;
while (existingTypes != null) {
i++;
existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className + i);
}
refactoring.setClassName(i == 1 ? className : className + i);
LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
refactoring.setLinkedProposalModel(linkedProposalModel);
final ICompilationUnit cu = context.getCompilationUnit();
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, CodeActionKind.Refactor, cu, refactoring, IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED);
proposal.setLinkedProposalModel(linkedProposalModel);
return proposal;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal 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;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProposalUtility method getExtractConstantProposal.
private static CUCorrectionProposal getExtractConstantProposal(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_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;
}
if (inferSelectionSupport && context.getSelectionLength() == 0) {
ASTNode parent = context.getCoveringNode();
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()) {
return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_CONSTANT, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_CONSTANT_COMMAND, params));
}
parent = parent.getParent();
}
return null;
}
ExtractConstantRefactoring extractConstRefactoring = new ExtractConstantRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength(), formatterOptions);
if (extractConstRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
if (returnAsCommand) {
return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_CONSTANT, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_CONSTANT_COMMAND, params));
}
LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
extractConstRefactoring.setLinkedProposalModel(linkedProposalModel);
extractConstRefactoring.setCheckResultForCompileProblems(false);
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_CONSTANT, cu, extractConstRefactoring, relevance) {
@Override
protected void init(Refactoring refactoring) throws CoreException {
ExtractConstantRefactoring etr = (ExtractConstantRefactoring) refactoring;
// expensive
etr.setConstantName(etr.guessConstantName());
}
};
proposal.setLinkedProposalModel(linkedProposalModel);
return proposal;
}
return null;
}
Aggregations