Grep Console How-to #5: Links

One feature I had been considering for Grep Console for a while was customisable links in the matched text lines. For example, HTTP hyperlinks could be rendered as links and open the browser when clicked. I abandoned the idea when I couldn’t figure out how to put links in the console and didn’t want to hold up the original release of version 3 even longer. Recently I got an email requesting a feature just like this, so I delved a little deeper into the Eclipse APIs, and the result is the newly release version 3.2 with link support.

Basics

Links, like styles, can be assigned to whole lines or capture groups. This is done in the style assignment table in the expression dialog, which now features a column for the links:

Edit expression dialog Double clicking a cell in the link column will open a dialogue that can be used to edit the link for the corresponding group. The dialogue shows a combo box allowing you to select between “no link” and various types of links – see below for details. The rest of the dialogue content changes depending on the selected link type:

Edit link dialog

Also, right clicking on a link cell in the style/link assignment table lets you copy and paste links.

Patterns

Links don’t make a lot of sense if they are static and always link to the same target regardless of the text they have been matched to, so most fields for the various types of links are so-called patterns which allow you to use parameters to insert portions of the matched text.

As an example, an expression that matches strings starting with “www” could use a link of type URL wih the following URL pattern parameter to open the corresponding website:

http://{match}

The following parameters are available in patterns:

{line} – The whole line text string of the matched line.
{match} – The whole matched text.
{group} – The text of the matched capture group.
{1}, {2},… – The text of the specified capture group.

Using links

In order to not interfere with normal cursor positioning in the console, Grep Console don’t automatically react when clicked on. They also don’t have a specific style of their own – each link is assigned to a group in its expression, so the same group can be assigned any style you like to highlight the link. To actually “open” a link, i.e. to execute the behaviour associated with it, hold down the CTRL key and click on the link. Hovering over a link with CTRL held down will turn the mouse cursor into a link cursor and show a tooltip for the link.

File links

A file link points to a file in the file system and opens it in an Eclipse editor when the link is clicked. An optional line number parameter can be used to position the cursor in a specific line.

For example, the following expression can be used to find text file references with line numbers like “/home/myuser/myfile.txt:42” in the console:

(\S+\.txt)\:(\d+)

The first group matches a string of non-whitespace characters ending with “.txt” directly before a colon, while the second group matches a series of digits directly after the same colon.

File link

For this expression, a file link can be configured to use {1} (the first capture group) as the file name and {2} (the second capture group) as the line number.

Java type links

Similar to file links, Java type links open any type from a Java project in an Eclipse editor. The types must be referenced by their fully qualified name and be in the same project that is associated with the launch configuration connected to the console.

If the classes in your project belong to the namespace com.myproject, the following expression can be used to find references like “com.myproject.MyClass:42” in the console output:

(com\.myproject\.\S+)\:(\d+)

Java type link

Like the example in the file link section, the first group captures the fully qualified class name, while the second group captures the line number. Use {1} for the Java type and {2} for the line number field in the link settings.

URL links

Links can be opened internally in an Eclipse browser, or externally in whatever application the operating system deems appropriate for the link. Usually this will be the system browser, but depending on the prefix and file extension, any kind of application (e.g. an FTP client or an image editor) may be associated with a link.

The following expression will match all links starting with “http://”:

(http\:\/\/\S+)

URL link

With this expression in place, a URL link with {match} as its URL pattern can be configured. Use the checkbox below the URL field to specify whether the link should be opened internally or externally.

Command links

If opening files and URLs is not enough for you, you can use command links to call system or shell commands. For example, to open image files with the image viewer Gwenview, you can use the following expression to capture all PNG file names in the console:

(\S+\.png)

Then a command link with the following command pattern will open the viewer when the link is clicked:

gwenview {match}

Command link

For commands where the working directory matters, you can control it with a separate field. If the working directory is not specified, Eclipse’s own current working directory is used.

Script links

With this link type, you can execute scripts directly in your Eclipse environment. The scripts are executed via the Java Scripting API and can be written in any language installed for the Java Scripting API in your Eclipse setup. Out of the box, the API supports Javascript.

A drop down box in the link dialog allows you to pick one of the available languages. This list is queried from the Scripting API, and since some language modules offer several alternative names for the language they support, the drop down list may include multiple entries for the same language. For example, “Javascript” will also show up as “js”, “rhino”, “javascript”, “ECMAScript” and “ecmascript”.

The code box lets you enter script code in the selected language. The script code doesn’t use pattern parameters, but the script can access the expression match information in a number of variables passed to it upon execution. These variables are listed a tooltip text which appears if you leave the mouse cursor hovering over the code box for a few seconds.

Scripts executed via the Java Scripting API have full access to the entire Java API and all libraries in the class path (see below). Here, I only provide a simple example that prints the matched line of text to stdout:

importPackage(java.lang);
java.lang.System.out.println(wholeLine);

Script linkA note on the class path: In Eclipse, each plug-in has its own class loader, and therefore its own class path. Scripts run from Grep Console therefore have access to all the classes that the Grep Console plug-in itself  has access to. Classes from other, unrelated plug-ins are not accessible, and neither are the classes from your project producing the console output.

This entry was posted in Grep Console and tagged , . Bookmark the permalink.

10 Responses to Grep Console How-to #5: Links

  1. Lutz Schaft says:

    Dear Marian,

    great work and a simple but very useful idea your grep console plugin for eclipse. I would like to use it for unittests with the googletest framework. Once it should mark the unit test failure, what it does. The second helpful thing for me would be the link to the line in the scource code file where the failure is caused. I my case in a C++ source code file. So i have tried also the file link and the Java type link. But with both I haven’t had success. May I ask you for a hint in which way I can solve my problem. I want to click on the mark area and would like to see the line in the corresponding C++-Source file in the editor above. Is this with your plugin possible?

    Greetings from small town near Dortmund in Germany
    Lutz

  2. Lutz Schaf says:

    I have found my mistake! I have to hold the strg-key to follow the link!

    Great tool, keep going on…

    Lutz

    • Yes, Grep Console uses modifier keys so as not to interfere with the console’s own internal link handling. You can change the modifier key in the configuration if you want to use a different one.

  3. Milla M says:

    I’m trying to match a link in an HTML anchor:

    href=[\'"]?([^\'" >]+) (which I know matches the link)
    and
    {match}

    But it’s not working. Any ideas where I’m going wrong? Thank you very much!

    • Use {group} instead of {match}. {match} will use the full text matched by your expression, and that includes the href bit and the quotes.

      • Milla M says:

        Hi and thanks for the reply! I*m getting the link now, however it complains that my URL file://C:\Users\workspace\ui-tests\test-output\screenshots-2014113\africaCountriesTest-2014113133755.png is not an invalid URL. Do file://.. urls not work?

        • Backslashes are not allowed in URLs. You actually can’t instantiate the URL class with a URL string that contains a backslash character in Java. Replace the backslashes in your URL with forward slashes and it should work fine.

  4. Fab says:

    Hello, thank you for your extraodinary idea and shraed effort.
    Is it possible to use a JAVA class link within a local server deploy (ex. Tomcat)?
    It says Action Error: No project selected.
    I understand that we must select the grep config from the application launcher config, but for the server launch it doesn’t appear.
    Thanks

Leave a Reply to Milla M Cancel reply

Your email address will not be published. Required fields are marked *