use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring 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.ExtractFieldRefactoring in project eclipse.jdt.ls by eclipse.
the class ExtractFieldTest method failHelper.
protected void failHelper(ICompilationUnit cu) throws OperationCanceledException, CoreException {
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();
Range range = getRange(cu, problems);
int start = DiagnosticsHelper.getStartOffset(cu, range);
int end = DiagnosticsHelper.getEndOffset(cu, range);
ExtractFieldRefactoring refactoring = new ExtractFieldRefactoring(astRoot, start, end - start);
RefactoringStatus result = refactoring.checkInitialConditions(new NullProgressMonitor());
assertNotNull("precondition was supposed to fail", result);
assertEquals("precondition was supposed to fail", false, result.isOK());
}
use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring 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.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring in project eclipse.jdt.ls by eclipse.
the class ExtractFieldTest method helper.
protected boolean helper(ICompilationUnit cu, InitializeScope initializeIn, String expected) throws Exception {
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
IProblem[] problems = astRoot.getProblems();
Range range = getRange(cu, problems);
int start = DiagnosticsHelper.getStartOffset(cu, range);
int end = DiagnosticsHelper.getEndOffset(cu, range);
ExtractFieldRefactoring refactoring = new ExtractFieldRefactoring(astRoot, start, end - start);
refactoring.setFieldName(refactoring.guessFieldName());
RefactoringStatus activationResult = refactoring.checkInitialConditions(new NullProgressMonitor());
assertTrue("activation was supposed to be successful", activationResult.isOK());
if (initializeIn != null) {
if (initializeIn == InitializeScope.CURRENT_METHOD && !refactoring.canEnableSettingDeclareInMethod()) {
return false;
} else if (initializeIn == InitializeScope.CLASS_CONSTRUCTORS && !refactoring.canEnableSettingDeclareInConstructors()) {
return false;
} else if (initializeIn == InitializeScope.FIELD_DECLARATION && !refactoring.canEnableSettingDeclareInFieldDeclaration()) {
return false;
}
refactoring.setInitializeIn(initializeIn.ordinal());
}
RefactoringStatus checkInputResult = refactoring.checkFinalConditions(new NullProgressMonitor());
assertTrue("precondition was supposed to pass but was " + checkInputResult.toString(), checkInputResult.isOK());
Change change = refactoring.createChange(new NullProgressMonitor());
WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
assertNotNull(change);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(edit);
assertEquals(expected, actual);
return true;
}
Aggregations