use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.
the class InlineTempRefactoring method removeTemp.
private void removeTemp(CompilationUnitRewrite cuRewrite) {
VariableDeclaration variableDeclaration = getVariableDeclaration();
TextEditGroup groupDesc = cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_remove_edit_name);
ASTNode parent = variableDeclaration.getParent();
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
if (parent instanceof VariableDeclarationStatement && ((VariableDeclarationStatement) parent).fragments().size() == 1) {
sourceRangeComputer.addTightSourceNode(parent);
rewrite.remove(parent, groupDesc);
} else {
sourceRangeComputer.addTightSourceNode(variableDeclaration);
rewrite.remove(variableDeclaration, groupDesc);
}
}
use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.
the class ExtractTempRefactoring method insertAt.
private void insertAt(ASTNode target, Statement declaration) {
ASTRewrite rewrite = fCURewrite.getASTRewrite();
TextEditGroup groupDescription = fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable);
ASTNode parent = target.getParent();
StructuralPropertyDescriptor locationInParent = target.getLocationInParent();
while (locationInParent != Block.STATEMENTS_PROPERTY && locationInParent != SwitchStatement.STATEMENTS_PROPERTY) {
if (locationInParent == IfStatement.THEN_STATEMENT_PROPERTY || locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY || locationInParent == ForStatement.BODY_PROPERTY || locationInParent == EnhancedForStatement.BODY_PROPERTY || locationInParent == DoStatement.BODY_PROPERTY || locationInParent == WhileStatement.BODY_PROPERTY) {
// create intermediate block if target was the body property of a control statement:
Block replacement = rewrite.getAST().newBlock();
ListRewrite replacementRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
replacementRewrite.insertFirst(declaration, null);
replacementRewrite.insertLast(rewrite.createMoveTarget(target), null);
rewrite.replace(target, replacement, groupDescription);
return;
}
target = parent;
parent = parent.getParent();
locationInParent = target.getLocationInParent();
}
ListRewrite listRewrite = rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) locationInParent);
listRewrite.insertBefore(declaration, target, groupDescription);
}
use of org.eclipse.text.edits.TextEditGroup in project che 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<IASTFragment>();
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.text.edits.TextEditGroup in project che 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.text.edits.TextEditGroup in project che by eclipse.
the class AbstractSerialVersionOperation method rewriteAST.
/**
* {@inheritDoc}
*/
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
final ASTRewrite rewrite = cuRewrite.getASTRewrite();
VariableDeclarationFragment fragment = null;
for (int i = 0; i < fNodes.length; i++) {
final ASTNode node = fNodes[i];
final AST ast = node.getAST();
fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(NAME_FIELD));
final FieldDeclaration declaration = ast.newFieldDeclaration(fragment);
declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG));
declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL));
if (!addInitializer(fragment, node))
continue;
if (fragment.getInitializer() != null) {
final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite);
if (node instanceof AbstractTypeDeclaration)
rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup);
else if (node instanceof AnonymousClassDeclaration)
rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
else if (node instanceof ParameterizedType) {
final ParameterizedType type = (ParameterizedType) node;
final ASTNode parent = type.getParent();
if (parent instanceof ClassInstanceCreation) {
final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration();
if (anonymous != null)
rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
}
} else
Assert.isTrue(false);
addLinkedPositions(rewrite, fragment, positionGroups);
}
final String comment = CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, StubUtility.getLineDelimiterUsed(fUnit));
if (comment != null && comment.length() > 0) {
final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
declaration.setJavadoc(doc);
}
}
if (fragment == null)
return;
positionGroups.setEndPosition(rewrite.track(fragment));
}
Aggregations