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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations