use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment in project eclipse.jdt.ls by eclipse.
the class ExtractConstantRefactoring method getFragmentsToReplace.
private IASTFragment[] getFragmentsToReplace() throws JavaModelException {
List<IASTFragment> toReplace = new ArrayList<>();
if (fReplaceAllOccurrences) {
Iterator<ASTNode> replacementScope = getReplacementScope();
while (replacementScope.hasNext()) {
ASTNode scope = replacementScope.next();
IASTFragment[] allMatches = ASTFragmentFactory.createFragmentForFullSubtree(scope).getSubFragmentsMatching(getSelectedExpression());
IASTFragment[] replaceableMatches = retainOnlyReplacableMatches(allMatches);
for (int i = 0; i < replaceableMatches.length; i++) {
toReplace.add(replaceableMatches[i]);
}
}
} else if (canReplace(getSelectedExpression())) {
toReplace.add(getSelectedExpression());
}
return toReplace.toArray(new IASTFragment[toReplace.size()]);
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method addReplaceExpressionWithTemp.
private void addReplaceExpressionWithTemp() throws JavaModelException {
IASTFragment[] fragmentsToReplace = retainOnlyReplacableMatches(getMatchingFragments());
// TODO: should not have to prune duplicates here...
ASTRewrite rewrite = fCURewrite.getASTRewrite();
HashSet<IASTFragment> seen = new HashSet<>();
for (int i = 0; i < fragmentsToReplace.length; i++) {
IASTFragment fragment = fragmentsToReplace[i];
if (!seen.add(fragment)) {
continue;
}
SimpleName tempName = fCURewrite.getAST().newSimpleName(fTempName);
TextEditGroup description = fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_replace);
fragment.replace(rewrite, tempName, description);
if (fLinkedProposalModel != null) {
fLinkedProposalModel.getPositionGroup(KEY_NAME, true).addPosition(rewrite.track(tempName), false);
}
}
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment in project eclipse.jdt.ls by eclipse.
the class ExtractConstantRefactoring method replaceExpressionsWithConstant.
private void replaceExpressionsWithConstant() throws JavaModelException {
ASTRewrite astRewrite = fCuRewrite.getASTRewrite();
AST ast = astRewrite.getAST();
IASTFragment[] fragmentsToReplace = getFragmentsToReplace();
for (int i = 0; i < fragmentsToReplace.length; i++) {
IASTFragment fragment = fragmentsToReplace[i];
ASTNode node = fragment.getAssociatedNode();
boolean inTypeDeclarationAnnotation = isInTypeDeclarationAnnotation(node);
if (inTypeDeclarationAnnotation && JdtFlags.VISIBILITY_STRING_PRIVATE == getVisibility()) {
continue;
}
SimpleName ref = ast.newSimpleName(fConstantName);
Name replacement = ref;
boolean qualifyReference = qualifyReferencesWithDeclaringClassName();
if (!qualifyReference) {
qualifyReference = inTypeDeclarationAnnotation;
}
if (qualifyReference) {
replacement = ast.newQualifiedName(ast.newSimpleName(getContainingTypeBinding().getName()), ref);
}
TextEditGroup description = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_replace);
fragment.replace(astRewrite, replacement, description);
if (fLinkedProposalModel != null) {
fLinkedProposalModel.getPositionGroup(KEY_NAME, true).addPosition(astRewrite.track(ref), false);
}
}
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method getSelectedExpression.
private IExpressionFragment getSelectedExpression() throws JavaModelException {
if (fSelectedExpression != null) {
return fSelectedExpression;
}
IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);
if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
fSelectedExpression = (IExpressionFragment) selectedFragment;
} else if (selectedFragment != null) {
if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
Expression expression = exprStatement.getExpression();
fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
} else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
}
}
if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
fSelectedExpression = null;
}
return fSelectedExpression;
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IASTFragment in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method createAndInsertTempDeclaration.
private void createAndInsertTempDeclaration() throws CoreException {
Expression initializer = getSelectedExpression().createCopyTarget(fCURewrite.getASTRewrite(), true);
VariableDeclarationStatement vds = createTempDeclaration(initializer);
boolean insertAtSelection;
if (!fReplaceAllOccurrences) {
insertAtSelection = true;
} else {
IASTFragment[] replacableMatches = retainOnlyReplacableMatches(getMatchingFragments());
insertAtSelection = replacableMatches.length == 0 || replacableMatches.length == 1 && replacableMatches[0].getAssociatedNode().equals(getSelectedExpression().getAssociatedExpression());
}
if (insertAtSelection) {
insertAt(getSelectedExpression().getAssociatedNode(), vds);
return;
}
ASTNode[] firstReplaceNodeParents = getParents(getFirstReplacedExpression().getAssociatedNode());
ASTNode[] commonPath = findDeepestCommonSuperNodePathForReplacedNodes();
Assert.isTrue(commonPath.length <= firstReplaceNodeParents.length);
ASTNode deepestCommonParent = firstReplaceNodeParents[commonPath.length - 1];
if (deepestCommonParent instanceof Block) {
insertAt(firstReplaceNodeParents[commonPath.length], vds);
} else {
insertAt(deepestCommonParent, vds);
}
}
Aggregations