Search in sources :

Example 26 with TextEditGroup

use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.

the class ConvertForLoopOperation method rewriteAST.

/**
	 * {@inheritDoc}
	 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
    TextEditGroup group = createTextEditGroup(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description, cuRewrite);
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    TightSourceRangeComputer rangeComputer;
    if (rewrite.getExtendedSourceRangeComputer() instanceof TightSourceRangeComputer) {
        rangeComputer = (TightSourceRangeComputer) rewrite.getExtendedSourceRangeComputer();
    } else {
        rangeComputer = new TightSourceRangeComputer();
    }
    rangeComputer.addTightSourceNode(getForStatement());
    rewrite.setTargetSourceRangeComputer(rangeComputer);
    Statement statement = convert(cuRewrite, group, positionGroups);
    rewrite.replace(getForStatement(), statement, group);
}
Also used : TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 27 with TextEditGroup

use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.

the class StringFix method createCleanUp.

private static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, boolean addNLSTag, boolean removeNLSTag, IProblemLocation[] problems) throws CoreException, JavaModelException {
    ICompilationUnit cu = (ICompilationUnit) compilationUnit.getJavaElement();
    if (!cu.isStructureKnown())
        //[clean up] 'Remove unnecessary $NLS-TAGS$' removes necessary ones in case of syntax errors: https://bugs.eclipse.org/bugs/show_bug.cgi?id=285814 : 
        return null;
    List<CategorizedTextEditGroup> result = new ArrayList<CategorizedTextEditGroup>();
    List<IProblemLocation> missingNLSProblems = new ArrayList<IProblemLocation>();
    for (int i = 0; i < problems.length; i++) {
        IProblemLocation problem = problems[i];
        if (addNLSTag && problem.getProblemId() == IProblem.NonExternalizedStringLiteral) {
            missingNLSProblems.add(problem);
        }
        if (removeNLSTag && problem.getProblemId() == IProblem.UnnecessaryNLSTag) {
            IBuffer buffer = cu.getBuffer();
            if (buffer != null) {
                TextEdit edit = StringFix.getReplace(problem.getOffset(), problem.getLength(), buffer, false);
                if (edit != null) {
                    String label = FixMessages.StringFix_RemoveNonNls_description;
                    result.add(new CategorizedTextEditGroup(label, edit, new GroupCategorySet(new GroupCategory(label, label, label))));
                }
            }
        }
    }
    if (!missingNLSProblems.isEmpty()) {
        int[] positions = new int[missingNLSProblems.size()];
        int i = 0;
        for (Iterator<IProblemLocation> iter = missingNLSProblems.iterator(); iter.hasNext(); ) {
            IProblemLocation problem = iter.next();
            positions[i] = problem.getOffset();
            i++;
        }
        //TODO nls
        //NLSUtil.createNLSEdits(cu, positions);
        TextEdit[] edits = null;
        if (edits != null) {
            for (int j = 0; j < edits.length; j++) {
                String label = FixMessages.StringFix_AddNonNls_description;
                result.add(new CategorizedTextEditGroup(label, edits[j], new GroupCategorySet(new GroupCategory(label, label, label))));
            }
        }
    }
    if (result.isEmpty())
        return null;
    //$NON-NLS-1$
    return new StringFix("", compilationUnit, result.toArray(new TextEditGroup[result.size()]));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) GroupCategorySet(org.eclipse.ltk.core.refactoring.GroupCategorySet) GroupCategory(org.eclipse.ltk.core.refactoring.GroupCategory) ArrayList(java.util.ArrayList) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IBuffer(org.eclipse.jdt.core.IBuffer) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup) TextEdit(org.eclipse.text.edits.TextEdit) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 28 with TextEditGroup

use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.

the class RenameLocalVariableProcessor method createEdits.

private void createEdits() {
    TextEdit declarationEdit = createRenameEdit(fTempDeclarationNode.getName().getStartPosition());
    TextEdit[] allRenameEdits = getAllRenameEdits(declarationEdit);
    TextEdit[] allUnparentedRenameEdits = new TextEdit[allRenameEdits.length];
    TextEdit unparentedDeclarationEdit = null;
    fChange = new CompilationUnitChange(RefactoringCoreMessages.RenameTempRefactoring_rename, fCu);
    MultiTextEdit rootEdit = new MultiTextEdit();
    fChange.setEdit(rootEdit);
    fChange.setKeepPreviewEdits(true);
    for (int i = 0; i < allRenameEdits.length; i++) {
        if (fIsComposite) {
            // Add a copy of the text edit (text edit may only have one
            // parent) to keep problem reporting code clean
            TextChangeCompatibility.addTextEdit(fChangeManager.get(fCu), RefactoringCoreMessages.RenameTempRefactoring_changeName, allRenameEdits[i].copy(), fCategorySet);
            // Add a separate copy for problem reporting
            allUnparentedRenameEdits[i] = allRenameEdits[i].copy();
            if (allRenameEdits[i].equals(declarationEdit))
                unparentedDeclarationEdit = allUnparentedRenameEdits[i];
        }
        rootEdit.addChild(allRenameEdits[i]);
        fChange.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.RenameTempRefactoring_changeName, allRenameEdits[i]));
    }
    // store information for analysis
    if (fIsComposite) {
        fLocalAnalyzePackage = new RenameAnalyzeUtil.LocalAnalyzePackage(unparentedDeclarationEdit, allUnparentedRenameEdits);
    } else
        fLocalAnalyzePackage = new RenameAnalyzeUtil.LocalAnalyzePackage(declarationEdit, allRenameEdits);
}
Also used : LocalAnalyzePackage(org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil.LocalAnalyzePackage) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) LocalAnalyzePackage(org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil.LocalAnalyzePackage) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 29 with TextEditGroup

use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.

the class IntroduceFactoryRefactoring method replaceConstructorCalls.

/**
	 * Use the given <code>ASTRewrite</code> to replace direct calls to the constructor
	 * with calls to the newly-created factory method.
	 * @param rg the <code>SearchResultGroup</code> indicating all of the constructor references
	 * @param unit the <code>CompilationUnit</code> to be rewritten
	 * @param unitRewriter the rewriter
	 * @param unitChange the compilation unit change
	 * @throws CoreException
	 * @return true iff at least one constructor call site was rewritten.
	 */
private boolean replaceConstructorCalls(SearchResultGroup rg, CompilationUnit unit, ASTRewrite unitRewriter, CompilationUnitChange unitChange) throws CoreException {
    Assert.isTrue(ASTCreator.getCu(unit).equals(rg.getCompilationUnit()));
    SearchMatch[] hits = rg.getSearchResults();
    Arrays.sort(hits, new Comparator<SearchMatch>() {

        /**
			 * Sort by descending offset, such that nested constructor calls are processed first.
			 * This is necessary, since they can only be moved into the factory method invocation
			 * after they have been rewritten.
			 */
        public int compare(SearchMatch m1, SearchMatch m2) {
            return m2.getOffset() - m1.getOffset();
        }
    });
    boolean someCallPatched = false;
    for (int i = 0; i < hits.length; i++) {
        ASTNode ctrCall = getCtorCallAt(hits[i].getOffset(), hits[i].getLength(), unit);
        if (ctrCall instanceof ClassInstanceCreation) {
            TextEditGroup gd = new TextEditGroup(RefactoringCoreMessages.IntroduceFactory_replaceCalls);
            rewriteFactoryMethodCall((ClassInstanceCreation) ctrCall, unitRewriter, gd);
            unitChange.addTextEditGroup(gd);
            someCallPatched = true;
        } else if (ctrCall instanceof MethodRef) {
            TextEditGroup gd = new TextEditGroup(RefactoringCoreMessages.IntroduceFactoryRefactoring_replaceJavadocReference);
            rewriteJavadocReference((MethodRef) ctrCall, unitRewriter, gd);
            unitChange.addTextEditGroup(gd);
            someCallPatched = true;
        }
    }
    return someCallPatched;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) MethodRef(org.eclipse.jdt.core.dom.MethodRef) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 30 with TextEditGroup

use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.

the class ChangeSignatureProcessor method addNewConstructorToSubclass.

private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) {
    AST ast = subclass.getAST();
    MethodDeclaration newConstructor = ast.newMethodDeclaration();
    newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier()));
    newConstructor.setConstructor(true);
    newConstructor.setJavadoc(null);
    newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass)));
    newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
    Block body = ast.newBlock();
    newConstructor.setBody(body);
    SuperConstructorInvocation superCall = ast.newSuperConstructorInvocation();
    addArgumentsToNewSuperConstructorCall(superCall, cuRewrite);
    body.statements().add(superCall);
    String msg = RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor;
    TextEditGroup description = cuRewrite.createGroupDescription(msg);
    cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description);
// TODO use AbstractTypeDeclaration
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

TextEditGroup (org.eclipse.text.edits.TextEditGroup)34 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)11 TextEdit (org.eclipse.text.edits.TextEdit)10 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 AST (org.eclipse.jdt.core.dom.AST)6 CategorizedTextEditGroup (org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)4 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)4 ArrayList (java.util.ArrayList)3 IFile (org.eclipse.core.resources.IFile)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)3