mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
- support for navigatable "URL"s containing file: syntax - would open editor in eclipse
This commit is contained in:
parent
aa8fb216e6
commit
b168348b96
4 changed files with 74 additions and 49 deletions
|
@ -12,7 +12,9 @@ Require-Bundle: org.eclipse.ui,
|
|||
org.eclipse.ui.editors,
|
||||
org.eclipse.cdt.codan.core,
|
||||
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-ActivationPolicy: lazy
|
||||
Export-Package: org.eclipse.cdt.codan.internal.ui;x-friends:="org.eclipse.cdt.codan.ui.cxx",
|
||||
|
|
|
@ -12,9 +12,18 @@ package org.eclipse.cdt.codan.internal.ui.views;
|
|||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.eclipse.cdt.codan.internal.ui.CodanUIActivator;
|
||||
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.IResource;
|
||||
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.IStructuredSelection;
|
||||
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.widgets.Composite;
|
||||
import org.eclipse.swt.widgets.Link;
|
||||
import org.eclipse.ui.IEditorPart;
|
||||
import org.eclipse.ui.ISelectionListener;
|
||||
import org.eclipse.ui.ISelectionService;
|
||||
import org.eclipse.ui.IWorkbenchPart;
|
||||
import org.eclipse.ui.PartInitException;
|
||||
import org.eclipse.ui.part.ViewPart;
|
||||
import org.eclipse.ui.texteditor.ITextEditor;
|
||||
|
||||
/**
|
||||
* Problems Details view show details for selected problem marker.
|
||||
|
@ -49,6 +61,7 @@ public class ProblemDetails extends ViewPart {
|
|||
*/
|
||||
private Link description;
|
||||
private GenericCodanProblemDetailsProvider genProvider = new GenericCodanProblemDetailsProvider();
|
||||
private AbstractCodanProblemDetailsProvider curProvider = genProvider;
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
|
@ -68,13 +81,15 @@ public class ProblemDetails extends ViewPart {
|
|||
@Override
|
||||
public void widgetSelected(SelectionEvent e) {
|
||||
String link = e.text;
|
||||
if (link==null) return;
|
||||
if (link == null)
|
||||
return;
|
||||
if (link.startsWith("http")) { //$NON-NLS-1$
|
||||
org.eclipse.swt.program.Program.launch(e.text);
|
||||
return;
|
||||
}
|
||||
if (link.startsWith("source:")) { //$NON-NLS-1$
|
||||
// open in eclipse editor TODO
|
||||
// link file format example "file:/tmp/file.c#42", 42 is the line number
|
||||
if (link.startsWith("file:")) { //$NON-NLS-1$
|
||||
openFile(link);
|
||||
return;
|
||||
}
|
||||
if (link.startsWith("help:")) { //$NON-NLS-1$
|
||||
|
@ -136,6 +151,7 @@ public class ProblemDetails extends ViewPart {
|
|||
}
|
||||
|
||||
private void applyProvider(AbstractCodanProblemDetailsProvider provider) {
|
||||
curProvider = provider;
|
||||
setTextSafe(message, provider, provider.getStyledProblemMessage());
|
||||
setTextSafe(description, provider, provider.getStyledProblemDescription());
|
||||
}
|
||||
|
@ -155,4 +171,37 @@ public class ProblemDetails extends ViewPart {
|
|||
public void 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,20 +10,14 @@
|
|||
*******************************************************************************/
|
||||
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.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.ui.IEditorPart;
|
||||
import org.eclipse.ui.IFileEditorInput;
|
||||
import org.eclipse.ui.IMarkerResolution;
|
||||
import org.eclipse.ui.IMarkerResolution2;
|
||||
import org.eclipse.ui.IWorkbenchPage;
|
||||
import org.eclipse.ui.IWorkbenchWindow;
|
||||
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;
|
||||
|
||||
/**
|
||||
|
@ -32,8 +26,7 @@ import org.eclipse.ui.texteditor.ITextEditor;
|
|||
* description client class should additionally implement
|
||||
* {@link IMarkerResolution2}
|
||||
*/
|
||||
public abstract class AbstarctCodanCMarkerResolution implements
|
||||
IMarkerResolution {
|
||||
public abstract class AbstarctCodanCMarkerResolution implements IMarkerResolution {
|
||||
/**
|
||||
* Get position offset from marker. If CHAR_START attribute is not set for
|
||||
* marker, line and document would be used.
|
||||
|
@ -65,35 +58,16 @@ public abstract class AbstarctCodanCMarkerResolution implements
|
|||
* the marker to resolve
|
||||
*/
|
||||
public void run(IMarker marker) {
|
||||
// See if there is an open editor on the file containing the marker
|
||||
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 {
|
||||
editorPart = IDE.openEditor(page, (IFile) marker.getResource(),
|
||||
true);
|
||||
} catch (PartInitException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (editorPart == null) {
|
||||
return;
|
||||
}
|
||||
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 = editor.getDocumentProvider().getDocument(editor.getEditorInput());
|
||||
apply(marker, doc);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@ public abstract class AbstractCodanProblemDetailsProvider {
|
|||
* @return
|
||||
*/
|
||||
protected String getProblemId() {
|
||||
String id = marker.getAttribute(IMarker.PROBLEM, (String) null);
|
||||
String id = marker.getAttribute(IMarker.PROBLEM, (String) null);
|
||||
return id;
|
||||
}
|
||||
|
||||
|
@ -77,14 +77,14 @@ public abstract class AbstractCodanProblemDetailsProvider {
|
|||
*/
|
||||
public String getStyledProblemMessage() {
|
||||
String message = escapeForLink(getProblemMessage());
|
||||
String loc = marker.getResource().getFullPath().toOSString();
|
||||
String loc2 = marker.getAttribute(IMarker.LOCATION, ""); //$NON-NLS-1$
|
||||
if (loc2.length()>0)
|
||||
loc=loc2;
|
||||
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
|
||||
return message + "\n" + loc + ":" + line; //$NON-NLS-1$//$NON-NLS-2$
|
||||
String href = getLocationHRef();
|
||||
String link = href.replaceFirst("^file:", ""); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
link = link.replaceFirst("#(\\d+)$", ":$1"); //$NON-NLS-1$//$NON-NLS-2$
|
||||
return "<a href=\"" + href + "\">" + link + "</a>\n" + message; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
||||
}
|
||||
protected String getLocationHRef() {
|
||||
return CodanEditorUtility.getLocationHRef(marker);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return styled problem description. This text would be used in Link widget.
|
||||
* String can include <a> tags to which would be
|
||||
|
@ -109,6 +109,6 @@ public abstract class AbstractCodanProblemDetailsProvider {
|
|||
* such as & (mnemonic)
|
||||
*/
|
||||
protected String escapeForLink(String text) {
|
||||
return text.replaceAll("&", "&&"); //$NON-NLS-1$//$NON-NLS-2$
|
||||
return text.replaceAll("&", "&&"); //$NON-NLS-1$//$NON-NLS-2$
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue