Search in sources :

Example 41 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class FixCorrectionProposal method createTextChange.

@Override
protected TextChange createTextChange() throws CoreException {
    CompilationUnitChange createChange = fFix.createChange(null);
    createChange.setSaveMode(TextFileChange.LEAVE_DIRTY);
    return createChange;
}
Also used : CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 42 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class NewCUProposal method constructNewCUChange.

private CompilationUnitChange constructNewCUChange(ICompilationUnit cu) throws CoreException {
    String lineDelimiter = StubUtility.getLineDelimiterUsed(fCompilationUnit.getJavaProject());
    String typeStub = constructTypeStub(cu, fTypeNameWithParameters, Flags.AccPublic, lineDelimiter);
    String cuContent = constructCUContent(cu, typeStub, lineDelimiter);
    CompilationUnitChange cuChange = new CompilationUnitChange("", cu);
    cuChange.setEdit(new InsertEdit(0, cuContent));
    return cuChange;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 43 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method getConvertToSwitchExpressionProposals.

private static boolean getConvertToSwitchExpressionProposals(IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> resultingCollections) {
    if (covering instanceof Block) {
        List<Statement> statements = ((Block) covering).statements();
        int startIndex = QuickAssistProcessorUtil.getIndex(context.getSelectionOffset(), statements);
        if (startIndex == -1 || startIndex >= statements.size()) {
            return false;
        }
        covering = statements.get(startIndex);
    } else {
        while (covering instanceof SwitchCase || covering instanceof SwitchExpression) {
            covering = covering.getParent();
        }
    }
    SwitchStatement switchStatement;
    if (covering instanceof SwitchStatement) {
        switchStatement = (SwitchStatement) covering;
    } else {
        return false;
    }
    SwitchExpressionsFixCore fix = SwitchExpressionsFixCore.createConvertToSwitchExpressionFix(switchStatement);
    if (fix == null) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    // add correction proposal
    try {
        CompilationUnitChange change = fix.createChange(null);
        ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(fix.getDisplayString(), JavaCodeActionKind.QUICK_ASSIST, change, IProposalRelevance.CONVERT_TO_SWITCH_EXPRESSION);
        resultingCollections.add(proposal);
    } catch (CoreException e) {
    // continue
    }
    return true;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) CoreException(org.eclipse.core.runtime.CoreException) SwitchExpressionsFixCore(org.eclipse.jdt.internal.corext.fix.SwitchExpressionsFixCore) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 44 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class ChangeUtilTest method testConvertCompilationUnitChange.

// Text Changes
@Test
public void testConvertCompilationUnitChange() throws CoreException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null);
    CompilationUnitChange change = new CompilationUnitChange("insertText", cu);
    String newText = "// some content";
    change.setEdit(new InsertEdit(0, newText));
    WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
    assertEquals(edit.getDocumentChanges().size(), 1);
    TextDocumentEdit textDocumentEdit = edit.getDocumentChanges().get(0).getLeft();
    assertNotNull(textDocumentEdit);
    assertEquals(textDocumentEdit.getEdits().size(), 1);
    TextEdit textEdit = textDocumentEdit.getEdits().get(0);
    assertEquals(textEdit.getNewText(), newText);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InsertEdit(org.eclipse.text.edits.InsertEdit) TextEdit(org.eclipse.lsp4j.TextEdit) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 45 with CompilationUnitChange

use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.

the class ChangeUtilTest method testConvertSimpleCompositeChange.

// Composite Changes
@Test
public void testConvertSimpleCompositeChange() throws CoreException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null);
    CompositeChange change = new CompositeChange("simple composite change");
    RenameCompilationUnitChange resourceChange = new RenameCompilationUnitChange(cu, "ENew.java");
    change.add(resourceChange);
    CompilationUnitChange textChange = new CompilationUnitChange("insertText", cu);
    textChange.setEdit(new InsertEdit(0, "// some content"));
    change.add(textChange);
    WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
    assertEquals(edit.getDocumentChanges().size(), 2);
    assertTrue(edit.getDocumentChanges().get(0).getRight() instanceof RenameFile);
    assertTrue(edit.getDocumentChanges().get(1).getLeft() instanceof TextDocumentEdit);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InsertEdit(org.eclipse.text.edits.InsertEdit) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) RenameFile(org.eclipse.lsp4j.RenameFile) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Aggregations

CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)45 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)14 TextEdit (org.eclipse.text.edits.TextEdit)14 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 CoreException (org.eclipse.core.runtime.CoreException)9 CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)8 TextEditGroup (org.eclipse.text.edits.TextEditGroup)8 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)4 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)4 IDocument (org.eclipse.jface.text.IDocument)4 TextChange (org.eclipse.ltk.core.refactoring.TextChange)4 HashMap (java.util.HashMap)3 IFile (org.eclipse.core.resources.IFile)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3