diff options
author | Alena Laskavaia | 2010-06-26 00:18:38 +0000 |
---|---|---|
committer | Alena Laskavaia | 2010-06-26 00:18:38 +0000 |
commit | 2dc367389e423481166b7ff73f9853612ab71461 (patch) | |
tree | e29457c1d1921e292445a580803b17593735641a /codan | |
parent | ba0437069d9708b6d423f05017a4db3584871db3 (diff) | |
download | org.eclipse.cdt-2dc367389e423481166b7ff73f9853612ab71461.tar.gz org.eclipse.cdt-2dc367389e423481166b7ff73f9853612ab71461.tar.xz org.eclipse.cdt-2dc367389e423481166b7ff73f9853612ab71461.zip |
Bug 317876 added utility methods required for more complex quick fixes
Diffstat (limited to 'codan')
-rw-r--r-- | codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/AbstractCodanCMarkerResolution.java | 120 |
1 files changed, 108 insertions, 12 deletions
diff --git a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/AbstractCodanCMarkerResolution.java b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/AbstractCodanCMarkerResolution.java index 540dab6b3ed..6c9574668eb 100644 --- a/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/AbstractCodanCMarkerResolution.java +++ b/codan/org.eclipse.cdt.codan.ui/src/org/eclipse/cdt/codan/ui/AbstractCodanCMarkerResolution.java @@ -1,18 +1,27 @@ /******************************************************************************* - * Copyright (c) 2009 Alena Laskavaia + * Copyright (c) 2009, 2010 Alena Laskavaia * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Alena Laskavaia - initial API and implementation + * Alena Laskavaia - initial API and implementation + * Tomasz Wesolowski - extension *******************************************************************************/ package org.eclipse.cdt.codan.ui; import org.eclipse.cdt.codan.internal.core.model.CodanProblemMarker; import org.eclipse.cdt.codan.internal.ui.CodanUIActivator; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.ui.CDTUITools; +import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.ui.IEditorPart; @@ -66,37 +75,124 @@ public abstract class AbstractCodanCMarkerResolution implements * the marker to resolve */ public void run(IMarker marker) { + IDocument doc = openDocument(marker); + if (doc != null) { + apply(marker, doc); + } + } + + /** + * Apply marker resolution for given marker in given open document. + * + * @param marker + * @param document + */ + public abstract void apply(IMarker marker, IDocument document); + + /** + * Override is extra checks is required to determine appicablity of marker + * resolution + * + * @param marker + * @return + */ + public boolean isApplicable(IMarker marker) { + return true; + } + + /** + * Opens an editor with the document corresponding to the given problem and + * returns the corresponding IEditorPart. + * + * @param marker + * the problem marker + * @return the opened document + */ + protected IEditorPart openEditor(IMarker marker) { IEditorPart editorPart; try { editorPart = CodanEditorUtility.openInEditor(marker); } catch (PartInitException e) { + e.printStackTrace(); CodanUIActivator.log(e); - return; + return null; } + return editorPart; + } + + /** + * Opens the editor and returns the document corresponding to a given + * marker. + * + * @param marker + * the marker to find the editor + * @return the corresponding document + */ + protected IDocument openDocument(IMarker marker) { + return openDocument(openEditor(marker)); + } + + /** + * Returns the document corresponding to a given editor part. + * + * @param editorPart + * an editor part + * @return the document of that part + */ + protected IDocument openDocument(IEditorPart editorPart) { if (editorPart instanceof ITextEditor) { ITextEditor editor = (ITextEditor) editorPart; IDocument doc = editor.getDocumentProvider().getDocument( editor.getEditorInput()); - apply(marker, doc); + return doc; } + return null; } /** - * Apply marker resolution for given marker in given open document. + * Receives a translation unit from a given marker. Opens the editor. * * @param marker - * @param document + * A marker in an editor to get the translation unit + * @return The translation unit */ - public abstract void apply(IMarker marker, IDocument document); + protected ITranslationUnit getTranslationUnitViaEditor(IMarker marker) { + ITranslationUnit tu = (ITranslationUnit) CDTUITools + .getEditorInputCElement(openEditor(marker).getEditorInput()); + return tu; + } /** - * Override is extra checks is required to determine appicablity of marker - * resolution + * Receives a translation unit from a given marker using the marker's path. * * @param marker - * @return + * A marker in a translation unit + * @return The translation unit */ - public boolean isApplicable(IMarker marker) { - return true; + protected ITranslationUnit getTranslationUnitViaWorkspace(IMarker marker) { + IPath path = marker.getResource().getFullPath(); + IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path); + ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault().create( + file); + return tu; + } + + /** + * Receives an ASTName enclosing a given IMarker + * + * @param marker + * The marker enclosing an ASTName + * @param ast + * The AST to check + * @return The enclosing ASTName or null + */ + protected IASTName getASTNameFromMarker(IMarker marker, + IASTTranslationUnit ast) { + final int charStart = marker.getAttribute(IMarker.CHAR_START, -1); + final int length = marker.getAttribute(IMarker.CHAR_END, -1) + - charStart; + IASTName name = ast.getNodeSelector(null).findEnclosingName(charStart, + length); + return name; } } |