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,17 +75,8 @@ public abstract class AbstractCodanCMarkerResolution implements * the marker to resolve */ public void run(IMarker marker) { - IEditorPart editorPart; - try { - editorPart = CodanEditorUtility.openInEditor(marker); - } catch (PartInitException e) { - CodanUIActivator.log(e); - return; - } - if (editorPart instanceof ITextEditor) { - ITextEditor editor = (ITextEditor) editorPart; - IDocument doc = editor.getDocumentProvider().getDocument( - editor.getEditorInput()); + IDocument doc = openDocument(marker); + if (doc != null) { apply(marker, doc); } } @@ -99,4 +99,100 @@ public abstract class AbstractCodanCMarkerResolution implements 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 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()); + return doc; + } + return null; + } + + /** + * Receives a translation unit from a given marker. Opens the editor. + * + * @param marker + * A marker in an editor to get the translation unit + * @return The translation unit + */ + protected ITranslationUnit getTranslationUnitViaEditor(IMarker marker) { + ITranslationUnit tu = (ITranslationUnit) CDTUITools + .getEditorInputCElement(openEditor(marker).getEditorInput()); + return tu; + } + + /** + * Receives a translation unit from a given marker using the marker's path. + * + * @param marker + * A marker in a translation unit + * @return The translation unit + */ + 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; + } }