diff options
| author | Tor Norbye <tnorbye@google.com> | 2011-11-30 10:53:20 -0800 |
|---|---|---|
| committer | Tor Norbye <tnorbye@google.com> | 2011-11-30 10:53:20 -0800 |
| commit | 9b5ee8cc697ab75826f7c53718b84042fa45cf74 (patch) | |
| tree | 7cce12e6c6ce761e876ae82e36f2d3f1e7d1ea4a | |
| parent | 27b2ac34506d7142e8b9fc5abe7aebcf1790e882 (diff) | |
| download | sdk-9b5ee8cc697ab75826f7c53718b84042fa45cf74.zip sdk-9b5ee8cc697ab75826f7c53718b84042fa45cf74.tar.gz sdk-9b5ee8cc697ab75826f7c53718b84042fa45cf74.tar.bz2 | |
Make Fix in Lint Warnings window work for closed files
When you click on the Fix icon in the Lint Warnings window, the marker
you're applying a fix for may be referencing a file that is not open
and visible. The apply fix code needs to account for this, and open
the document (such that getModelForEdit will succeed) and for
interactive fixes, also open the editor.
Change-Id: Ic9c232bd050e4d6cce8acfc41a8e24b66e21bb48
2 files changed, 47 insertions, 16 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintFix.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintFix.java index c132248..5bf0851 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintFix.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintFix.java @@ -77,6 +77,7 @@ import org.eclipse.wst.sse.core.StructuredModelManager; import org.eclipse.wst.sse.core.internal.provisional.IModelManager; import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; +import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; import org.w3c.dom.Attr; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; @@ -102,7 +103,8 @@ abstract class LintFix implements ICompletionProposal { /** * Returns true if this fix needs focus (which means that when the fix is - * performed from a {@link LintListDialog}'s Fix button + * performed from for example a {@link LintListDialog}'s Fix button) the + * editor needs to be given focus. * * @return true if this fix needs focus after being applied */ @@ -233,12 +235,17 @@ abstract class LintFix implements ICompletionProposal { int start, int end); public void apply(IDocument document) { + if (!(document instanceof IStructuredDocument)) { + AdtPlugin.log(null, "Unexpected document type: %1$s. Can't fix.", + document.getClass().getName()); + return; + } int start = mMarker.getAttribute(IMarker.CHAR_START, -1); int end = mMarker.getAttribute(IMarker.CHAR_END, -1); if (start != -1 && end != -1) { - Node node = DomUtilities.getNode(document, start); IModelManager manager = StructuredModelManager.getModelManager(); - IStructuredModel model = manager.getExistingModelForEdit(document); + IStructuredModel model = manager.getModelForEdit((IStructuredDocument) document); + Node node = DomUtilities.getNode(document, start); try { apply(document, model, node, start, end); } finally { diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintViewPart.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintViewPart.java index f9a78a5..6f42bf2 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintViewPart.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintViewPart.java @@ -16,16 +16,15 @@ package com.android.ide.eclipse.adt.internal.lint; import com.android.ide.eclipse.adt.AdtPlugin; -import com.android.ide.eclipse.adt.AdtUtils; -import com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor; import com.android.ide.eclipse.adt.internal.editors.IconFactory; import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper; +import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.jobs.IJobChangeEvent; import org.eclipse.core.runtime.jobs.IJobChangeListener; import org.eclipse.core.runtime.jobs.Job; @@ -33,6 +32,9 @@ import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.Region; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.SashForm; import org.eclipse.swt.events.SelectionEvent; @@ -43,7 +45,6 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; -import org.eclipse.ui.IEditorPart; import org.eclipse.ui.ISharedImages; import org.eclipse.ui.IViewPart; import org.eclipse.ui.IWorkbench; @@ -51,8 +52,9 @@ import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.editors.text.TextFileDocumentProvider; import org.eclipse.ui.part.ViewPart; -import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; +import org.eclipse.ui.texteditor.IDocumentProvider; import java.util.ArrayList; import java.util.Collections; @@ -360,14 +362,36 @@ public class LintViewPart extends ViewPart implements SelectionListener, IJobCha List<IMarker> markers = mLintView.getSelectedMarkers(); for (IMarker marker : markers) { LintFix fix = LintFix.getFix(EclipseLintClient.getId(marker), marker); - IEditorPart editor = AdtUtils.getActiveEditor(); - if (editor instanceof AndroidXmlEditor) { - IStructuredDocument doc = - ((AndroidXmlEditor) editor).getStructuredDocument(); - fix.apply(doc); - } else { - AdtPlugin.log(IStatus.ERROR, - "Did not find associated editor to apply fix"); + IResource resource = marker.getResource(); + if (fix.needsFocus() && resource instanceof IFile) { + IRegion region = null; + try { + int start = marker.getAttribute(IMarker.CHAR_START, -1); + int end = marker.getAttribute(IMarker.CHAR_END, -1); + if (start != -1) { + region = new Region(start, end - start); + } + AdtPlugin.openFile((IFile) resource, region); + } catch (PartInitException e) { + AdtPlugin.log(e, "Can't open file %1$s", resource); + } + } + IDocumentProvider provider = new TextFileDocumentProvider(); + try { + provider.connect(resource); + IDocument document = provider.getDocument(resource); + if (document != null) { + fix.apply(document); + if (!fix.needsFocus()) { + provider.saveDocument(new NullProgressMonitor(), resource, + document, true /*overwrite*/); + } + } + } catch (Exception e) { + AdtPlugin.log(e, "Did not find associated editor to apply fix: %1$s", + resource.getName()); + } finally { + provider.disconnect(resource); } } break; |
