如何让 Eclipse 控制台将文本超链接到源代码文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6469445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Eclipse Console to hyperlink text to source code files?
提问by salman.mirghasemi
In Code: System.out.println("myPackage.MyClass");
在代码中: System.out.println("myPackage.MyClass");
In Eclipse Console: myPackage.MyClass.myMethod
在 Eclipse 控制台中:myPackage.MyClass.myMethod
I want to click on the output (myPackage.MyClass.myMethod) in Console and it directly shows the corresponding method, similar to what happens for exception stack traces. Any Idea?
我想点击控制台中的输出(myPackage.MyClass.myMethod),它直接显示相应的方法,类似于异常堆栈跟踪发生的情况。任何的想法?
采纳答案by Tonny Madsen
The hyperlinking for exception stack traces is based on the file name and line number given at the end of the line. E.g.
异常堆栈跟踪的超链接基于行尾给出的文件名和行号。例如
Stack trace:
org.eclipse.jface.internal.databinding.provisional.BindingException: string property does not have a read method.
at org.eclipse.jface.internal.databinding.internal.beans.JavaBeanObservableValue.doGetValue(JavaBeanObservableValue.java:102)
at org.eclipse.jface.internal.databinding.internal.beans.JavaBeanObservableValue.setValue(JavaBeanObservableValue.java:83)
For the first stack trace, it is at line 102 in the file JavaBeanObservableValue.java
. The file is searched after in the current class path so if you have multiple classes with the same name, the first is always found...
对于第一个堆栈跟踪,它位于文件中的第 102 行JavaBeanObservableValue.java
。在当前类路径中搜索该文件,因此如果您有多个同名的类,则始终会找到第一个...
With other words, if you want to add extended hyperlinking based on your example, you need to extend the console view a bit...
换句话说,如果要根据示例添加扩展超链接,则需要稍微扩展控制台视图...
...which can be done with the org.eclipse.ui.console.consolePatternMatchListeners
extension point. It is pretty easy to use this extension point and by looking at the example from JDT, you should be able to get your example to work without too much work...
...这可以通过org.eclipse.ui.console.consolePatternMatchListeners
扩展点来完成。使用这个扩展点非常容易,通过查看 JDT 的示例,您应该能够让您的示例在没有太多工作的情况下工作......
回答by viking
Maybe this is clear to other people, but I found the other answers confusing, although correct. The eclipse console parses the pattern (FileName.java:lineNumber) to be a link to that line in that file:
也许这对其他人来说很清楚,但我发现其他答案令人困惑,尽管是正确的。eclipse 控制台将模式 (FileName.java:lineNumber) 解析为指向该文件中该行的链接:
(MyFile.java:2)
As the other answers point out, there are many ways to output this to the console. Location in the line does not matter, it is just a pattern match. As Colin Smith shows, log4j PatternLayout can use (%F:%L)
to get the file name and line number. To get the file name and line number programmatically see this question.
正如其他答案所指出的,有很多方法可以将其输出到控制台。行中的位置无关紧要,这只是模式匹配。正如 Colin Smith 所示,log4j PatternLayout 可用于(%F:%L)
获取文件名和行号。要以编程方式获取文件名和行号,请参阅此问题。
The question asks about linking to a method and I believe you could use the consolePatternMatchListeners method recommended by Tonny Madsen and described in more detail in this question.
该问题询问链接到方法,我相信您可以使用由 Tonny Madsen 推荐并在此问题中更详细描述的 consolePatternMatchListeners 方法。
回答by Peter Westmacott
A much easier method is to trick the console into creating a link for you. The format is simple:
一种更简单的方法是欺骗控制台为您创建链接。格式很简单:
System.out.println("(" + new TestBed().getClass().getSimpleName() + ".java:" + 18 + ")");
Obviously you can provide the class type and line number in code as you wish.
显然,您可以根据需要在代码中提供类类型和行号。
回答by Colin Smith
Or, if you use log4j, you can configure your appender like this:
或者,如果你使用 log4j,你可以像这样配置你的 appender:
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p %m (%F:%L) in %t%n"/>
</layout>
</appender>
If you want it to work in IntelliJ IDEA too, you can use:
如果您也希望它在 IntelliJ IDEA 中工作,您可以使用:
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p %m - at %c.%M(%F:%L) in %t%n"/>
回答by Stefan
I use a custom console view and I add an IPatternMatchListener to the MessageConsole with
我使用自定义控制台视图,并将 IPatternMatchListener 添加到 MessageConsole
console.addPatternMatchListener(...)
An important advantage of using the listener is that the event provides the offsetthat is required for creating the hyperlink.
使用侦听器的一个重要优点是事件提供了创建超链接所需的偏移量。
A starting point for the implementation of IPatternMatchListener that creates hyperlinks can be found here:
可以在此处找到实现创建超链接的 IPatternMatchListener 的起点:
The trickiest part seems to be to get the full file path from the file name (I don't use a logger).
最棘手的部分似乎是从文件名中获取完整的文件路径(我不使用记录器)。
A work around used by the sunshade plugin can be found here:
可以在此处找到遮阳插件使用的解决方法:
See method "dereferenceWindowsLink"
请参阅方法“取消引用WindowsLink”
I am still looking for an alternative.
我仍在寻找替代方案。
回答by David
Here's a simple wrapper method based on everyone else's answers that can be used anywhere to get a string formatted so that the Eclipse console will link to the line in any file where getSourceCodeLine()is called. I also discovered that printing to System.err in Eclipse will be shown in red.
这是一个基于其他人的答案的简单包装器方法,可以在任何地方使用它来获取格式化的字符串,以便 Eclipse 控制台将链接到调用getSourceCodeLine() 的任何文件中的行。我还发现在 Eclipse 中打印到 System.err 将显示为红色。
public static String getSourceCodeLine() {
// An index of 1 references the calling method
StackTraceElement ste = new Throwable().getStackTrace()[1];
return "(" + ste.getFileName() + ":" + ste.getLineNumber() + ")";
}
public static void main( String[] args )
System.out.println("Here's a link to " + getSourceCodeLine());
System.err.println("And a red link to " + getSourceCodeLine());
}