use of org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore in project eclipse.jdt.ls by eclipse.
the class RefactorProposalUtility method getExtractFieldProposal.
private static CUCorrectionProposal getExtractFieldProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, String initializeIn, boolean returnAsCommand, boolean inferSelectionSupport) throws CoreException {
if (!supportsExtractVariable(context)) {
return null;
}
final ICompilationUnit cu = context.getCompilationUnit();
String label = CorrectionMessages.QuickAssistProcessor_extract_to_field_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 (context.getSelectionLength() == 0 && inferSelectionSupport) {
ASTNode parent = context.getCoveringNode();
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()) {
InitializeScope scope = InitializeScope.fromName(initializeIn);
if (scope != null) {
refactoring.setInitializeIn(scope.ordinal());
}
List<String> scopes = getInitializeScopes(refactoring);
if (!scopes.isEmpty()) {
return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_FIELD, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_FIELD_COMMAND, params));
}
}
parent = parent.getParent();
}
return null;
}
ExtractFieldRefactoring extractFieldRefactoringSelectedOnly = new ExtractFieldRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
extractFieldRefactoringSelectedOnly.setFormatterOptions(formatterOptions);
if (extractFieldRefactoringSelectedOnly.checkInitialConditions(new NullProgressMonitor()).isOK()) {
InitializeScope scope = InitializeScope.fromName(initializeIn);
if (scope != null) {
extractFieldRefactoringSelectedOnly.setInitializeIn(scope.ordinal());
}
if (returnAsCommand) {
List<String> scopes = getInitializeScopes(extractFieldRefactoringSelectedOnly);
return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_FIELD, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_FIELD_COMMAND, params, new ExtractFieldInfo(scopes)));
}
LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
extractFieldRefactoringSelectedOnly.setLinkedProposalModel(linkedProposalModel);
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_FIELD, cu, extractFieldRefactoringSelectedOnly, relevance) {
@Override
protected void init(Refactoring refactoring) throws CoreException {
ExtractFieldRefactoring etr = (ExtractFieldRefactoring) refactoring;
// expensive
etr.setFieldName(etr.guessFieldName());
}
};
proposal.setLinkedProposalModel(linkedProposalModel);
return proposal;
}
return null;
}
use of org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore 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