Search in sources :

Example 16 with ChangeCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal 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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IMarker(org.eclipse.core.resources.IMarker) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) IResource(org.eclipse.core.resources.IResource) ChangeCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Aggregations

ChangeCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)12 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)8 ArrayList (java.util.ArrayList)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)5 Test (org.junit.Test)5 CorrectPackageDeclarationProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectPackageDeclarationProposal)4 ICommandAccess (org.eclipse.jdt.ui.text.java.correction.ICommandAccess)4 Image (org.eclipse.swt.graphics.Image)4 IType (org.eclipse.jdt.core.IType)3 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)3 IDocument (org.eclipse.jface.text.IDocument)3 Hashtable (java.util.Hashtable)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CorrectMainTypeNameProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.CorrectMainTypeNameProposal)2 IMarker (org.eclipse.core.resources.IMarker)1 IResource (org.eclipse.core.resources.IResource)1 CoreException (org.eclipse.core.runtime.CoreException)1