1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 02:06:01 +02:00

- support for navigatable "URL"s containing file: syntax - would open editor in eclipse

This commit is contained in:
Alena Laskavaia 2010-05-05 20:00:36 +00:00
parent aa8fb216e6
commit b168348b96
4 changed files with 74 additions and 49 deletions

View file

@ -12,7 +12,9 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.ui.editors, org.eclipse.ui.editors,
org.eclipse.cdt.codan.core, org.eclipse.cdt.codan.core,
org.eclipse.jface.text, org.eclipse.jface.text,
org.eclipse.ui.ide org.eclipse.ui.ide,
org.eclipse.cdt.ui,
org.eclipse.cdt.core
Bundle-RequiredExecutionEnvironment: J2SE-1.5 Bundle-RequiredExecutionEnvironment: J2SE-1.5
Bundle-ActivationPolicy: lazy Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.cdt.codan.internal.ui;x-friends:="org.eclipse.cdt.codan.ui.cxx", Export-Package: org.eclipse.cdt.codan.internal.ui;x-friends:="org.eclipse.cdt.codan.ui.cxx",

View file

@ -12,9 +12,18 @@ package org.eclipse.cdt.codan.internal.ui.views;
import java.util.Collection; import java.util.Collection;
import org.eclipse.cdt.codan.internal.ui.CodanUIActivator;
import org.eclipse.cdt.codan.ui.AbstractCodanProblemDetailsProvider; import org.eclipse.cdt.codan.ui.AbstractCodanProblemDetailsProvider;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
@ -24,10 +33,13 @@ import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Link; import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISelectionListener; import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService; import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart; import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.texteditor.ITextEditor;
/** /**
* Problems Details view show details for selected problem marker. * Problems Details view show details for selected problem marker.
@ -49,6 +61,7 @@ public class ProblemDetails extends ViewPart {
*/ */
private Link description; private Link description;
private GenericCodanProblemDetailsProvider genProvider = new GenericCodanProblemDetailsProvider(); private GenericCodanProblemDetailsProvider genProvider = new GenericCodanProblemDetailsProvider();
private AbstractCodanProblemDetailsProvider curProvider = genProvider;
/** /**
* The constructor. * The constructor.
@ -68,13 +81,15 @@ public class ProblemDetails extends ViewPart {
@Override @Override
public void widgetSelected(SelectionEvent e) { public void widgetSelected(SelectionEvent e) {
String link = e.text; String link = e.text;
if (link==null) return; if (link == null)
return;
if (link.startsWith("http")) { //$NON-NLS-1$ if (link.startsWith("http")) { //$NON-NLS-1$
org.eclipse.swt.program.Program.launch(e.text); org.eclipse.swt.program.Program.launch(e.text);
return; return;
} }
if (link.startsWith("source:")) { //$NON-NLS-1$ // link file format example "file:/tmp/file.c#42", 42 is the line number
// open in eclipse editor TODO if (link.startsWith("file:")) { //$NON-NLS-1$
openFile(link);
return; return;
} }
if (link.startsWith("help:")) { //$NON-NLS-1$ if (link.startsWith("help:")) { //$NON-NLS-1$
@ -136,6 +151,7 @@ public class ProblemDetails extends ViewPart {
} }
private void applyProvider(AbstractCodanProblemDetailsProvider provider) { private void applyProvider(AbstractCodanProblemDetailsProvider provider) {
curProvider = provider;
setTextSafe(message, provider, provider.getStyledProblemMessage()); setTextSafe(message, provider, provider.getStyledProblemMessage());
setTextSafe(description, provider, provider.getStyledProblemDescription()); setTextSafe(description, provider, provider.getStyledProblemDescription());
} }
@ -155,4 +171,37 @@ public class ProblemDetails extends ViewPart {
public void setFocus() { public void setFocus() {
message.setFocus(); message.setFocus();
} }
@SuppressWarnings("restriction")
public void openFile(String link) {
String file = link.replaceFirst("^file:", ""); //$NON-NLS-1$ //$NON-NLS-2$
file = file.replaceAll("#\\d+$", ""); //$NON-NLS-1$ //$NON-NLS-2$
String sline = link.replaceAll(".*#(\\d+)$", "$1"); //$NON-NLS-1$ //$NON-NLS-2$
try {
IPath pfile = new Path(file);
IResource markerResource = curProvider.getMarker().getResource();
ICElement element = CoreModel.getDefault().create(markerResource);
IEditorPart part = EditorUtility.openInEditor(pfile, element);
int line = 0;
try {
line = Integer.parseInt(sline);
} catch (NumberFormatException e2) {
// no line
}
if (line > 0) {
if (part instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) part;
IDocument document = textEditor.getDocumentProvider().getDocument(
part.getEditorInput());
try {
textEditor.selectAndReveal(document.getLineOffset(line-1), 0);
} catch (BadLocationException e1) {
return;
}
}
}
} catch (PartInitException e1) {
CodanUIActivator.log(e1);
}
}
} }

View file

@ -10,20 +10,14 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.ui; package org.eclipse.cdt.codan.ui;
import org.eclipse.core.resources.IFile; import org.eclipse.cdt.codan.internal.ui.CodanUIActivator;
import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IMarkerResolution; import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolution2; import org.eclipse.ui.IMarkerResolution2;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException; import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.texteditor.ITextEditor;
/** /**
@ -32,8 +26,7 @@ import org.eclipse.ui.texteditor.ITextEditor;
* description client class should additionally implement * description client class should additionally implement
* {@link IMarkerResolution2} * {@link IMarkerResolution2}
*/ */
public abstract class AbstarctCodanCMarkerResolution implements public abstract class AbstarctCodanCMarkerResolution implements IMarkerResolution {
IMarkerResolution {
/** /**
* Get position offset from marker. If CHAR_START attribute is not set for * Get position offset from marker. If CHAR_START attribute is not set for
* marker, line and document would be used. * marker, line and document would be used.
@ -65,35 +58,16 @@ public abstract class AbstarctCodanCMarkerResolution implements
* the marker to resolve * the marker to resolve
*/ */
public void run(IMarker marker) { public void run(IMarker marker) {
// See if there is an open editor on the file containing the marker IEditorPart editorPart;
IWorkbenchWindow w = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
if (w == null) {
return;
}
IWorkbenchPage page = w.getActivePage();
if (page == null) {
return;
}
IFileEditorInput input = new FileEditorInput((IFile) marker
.getResource());
IEditorPart editorPart = page.findEditor(input);
if (editorPart == null) {
// open an editor
try { try {
editorPart = IDE.openEditor(page, (IFile) marker.getResource(), editorPart = CodanEditorUtility.openInEditor(marker);
true);
} catch (PartInitException e) { } catch (PartInitException e) {
e.printStackTrace(); CodanUIActivator.log(e);
}
}
if (editorPart == null) {
return; return;
} }
if (editorPart instanceof ITextEditor) { if (editorPart instanceof ITextEditor) {
ITextEditor editor = (ITextEditor) editorPart; ITextEditor editor = (ITextEditor) editorPart;
IDocument doc = editor.getDocumentProvider().getDocument( IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
editor.getEditorInput());
apply(marker, doc); apply(marker, doc);
} }
} }

View file

@ -77,14 +77,14 @@ public abstract class AbstractCodanProblemDetailsProvider {
*/ */
public String getStyledProblemMessage() { public String getStyledProblemMessage() {
String message = escapeForLink(getProblemMessage()); String message = escapeForLink(getProblemMessage());
String loc = marker.getResource().getFullPath().toOSString(); String href = getLocationHRef();
String loc2 = marker.getAttribute(IMarker.LOCATION, ""); //$NON-NLS-1$ String link = href.replaceFirst("^file:", ""); //$NON-NLS-1$ //$NON-NLS-2$
if (loc2.length()>0) link = link.replaceFirst("#(\\d+)$", ":$1"); //$NON-NLS-1$//$NON-NLS-2$
loc=loc2; return "<a href=\"" + href + "\">" + link + "</a>\n" + message; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0); }
return message + "\n" + loc + ":" + line; //$NON-NLS-1$//$NON-NLS-2$ protected String getLocationHRef() {
return CodanEditorUtility.getLocationHRef(marker);
} }
/** /**
* Return styled problem description. This text would be used in Link widget. * Return styled problem description. This text would be used in Link widget.
* String can include <a> tags to which would be * String can include <a> tags to which would be