use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.
the class CompilationUnitRewrite method createChange.
/**
* Creates a compilation unit change based on the events recorded by this compilation unit rewrite.
* @param name the name of the change to create
* @param generateGroups <code>true</code> to generate text edit groups, <code>false</code> otherwise
* @param monitor the progress monitor or <code>null</code>
* @return a {@link CompilationUnitChange}, or <code>null</code> for an empty change
* @throws CoreException when text buffer acquisition or import rewrite text edit creation fails
* @throws IllegalArgumentException when the AST rewrite encounters problems
*/
public CompilationUnitChange createChange(String name, boolean generateGroups, IProgressMonitor monitor) throws CoreException {
CompilationUnitChange cuChange = new CompilationUnitChange(name, fCu);
MultiTextEdit multiEdit = new MultiTextEdit();
cuChange.setEdit(multiEdit);
return attachChange(cuChange, generateGroups, monitor);
}
use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method addUnusedMemberProposal.
public static void addUnusedMemberProposal(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
int problemId = problem.getProblemId();
UnusedCodeFix fix = UnusedCodeFix.createUnusedMemberFix(context.getASTRoot(), problem, false);
if (fix != null) {
try {
CompilationUnitChange change = fix.createChange(null);
CUCorrectionProposal proposal = new CUCorrectionProposal(change.getName(), change.getCompilationUnit(), change, IProposalRelevance.UNUSED_MEMBER);
proposals.add(proposal);
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
}
}
if (problemId == IProblem.ArgumentIsNeverUsed) {
JavadocTagsSubProcessor.getUnusedAndUndocumentedParameterOrExceptionProposals(context, problem, proposals);
}
if (problemId == IProblem.UnusedPrivateField) {
GetterSetterCorrectionSubProcessor.addGetterSetterProposal(context, problem, proposals);
}
}
use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project bndtools by bndtools.
the class PackageInfoBaselineQuickFixProcessor method getCorrections.
@Override
public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
ICompilationUnit compUnit = context.getCompilationUnit();
IResource resource = compUnit.getResource();
IMarker[] markers = resource.findMarkers(BndtoolsConstants.MARKER_JAVA_BASELINE, false, 1);
for (IProblemLocation location : locations) {
for (IMarker marker : markers) {
int markerStart = marker.getAttribute(IMarker.CHAR_START, -1);
int markerEnd = marker.getAttribute(IMarker.CHAR_END, -1);
int markerLength = markerEnd - markerStart;
if (location.getOffset() <= markerStart && markerStart < location.getOffset() + location.getLength()) {
String newVersion = marker.getAttribute("suggestedVersion", null);
if (newVersion != null) {
StringBuilder quotedVersion = new StringBuilder(newVersion.trim());
if (quotedVersion.charAt(0) != '"')
quotedVersion.insert(0, '"');
if (quotedVersion.charAt(quotedVersion.length() - 1) != '"')
quotedVersion.append('"');
CompilationUnitChange change = new CompilationUnitChange("Change package-info.java", compUnit);
change.setEdit(new ReplaceEdit(markerStart, markerLength, quotedVersion.toString()));
return new IJavaCompletionProposal[] { new ChangeCorrectionProposal("Change package version to " + newVersion, change, 1000) };
}
}
}
}
return null;
}
use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls by eclipse.
the class RenameNonVirtualMethodProcessor method addDeclarationUpdate.
private void addDeclarationUpdate(TextChangeManager manager) throws CoreException {
if (getDelegateUpdating()) {
// create the delegate
CompilationUnitRewrite rewrite = new CompilationUnitRewrite(getDeclaringCU());
rewrite.setResolveBindings(true);
MethodDeclaration methodDeclaration = ASTNodeSearchUtil.getMethodDeclarationNode(getMethod(), rewrite.getRoot());
DelegateMethodCreator creator = new DelegateMethodCreator();
creator.setDeclaration(methodDeclaration);
creator.setDeclareDeprecated(getDeprecateDelegates());
creator.setSourceRewrite(rewrite);
creator.setCopy(true);
creator.setNewElementName(getNewElementName());
creator.prepareDelegate();
creator.createEdit();
CompilationUnitChange cuChange = rewrite.createChange(true);
if (cuChange != null) {
cuChange.setKeepPreviewEdits(true);
manager.manage(getDeclaringCU(), cuChange);
}
}
String editName = RefactoringCoreMessages.RenameMethodRefactoring_update_declaration;
ISourceRange nameRange = getMethod().getNameRange();
ReplaceEdit replaceEdit = new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName());
addTextEdit(manager.get(getDeclaringCU()), editName, replaceEdit);
}
use of org.eclipse.jdt.core.refactoring.CompilationUnitChange in project eclipse.jdt.ls 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);
}
}
Aggregations