0

I want to print hyperlinks to the console in an eclipse plugin.

I saw How to write a hyperlink to an eclipse console from a plugin, but get BadLocationException when calling myconsole.addHyperlink(fileLink, 10, 5). I discovered that the class PatternMatchEvent has getLength() and getOffset() I need for MessageConsole.addHyperlink().

Is using the approach in above link still the way to do this (the question was asked almost 12 years ago) and if so, how do I proceed to get access to these methods?
Any help is appreciated!

SebastianWilke
  • 470
  • 1
  • 4
  • 15
  • `PatternMatcherEvent` is only available in the the events generated for an `IPatternMatchListener` - are you using a pattern match listener? Otherwise you have to work out the position by looking at the console text. – greg-449 Jan 23 '21 at 12:28
  • Thank you for your quick response! I am not using a pattern match listener. Could you please go a little more into detail on what you mean by 'work out the position by looking at the console text'? Do you mean determining the length of the hyperlink to be printed? What about the offset? – SebastianWilke Jan 23 '21 at 13:33
  • The addHyperlink call adds a link to existing text in the console (it does not add new text). You must specify an offset and length in that existing text for the link. Looking through the existing Eclipse source every usage of this call is actually in a pattern match listener. – greg-449 Jan 23 '21 at 13:43
  • @greg-449 Do you have any link where I can find more information about the offset (where it starts from for example, ...) and length? I am desperate. All I want to do is print a hyperlink to a file I know exists and starts from `L/`. Why do I need a `IPatternMatchListener` in the first place? What am I listening for? – SebastianWilke Jan 26 '21 at 17:51

1 Answers1

1

You can only use PatternMatcherEvent in a class implementing IPatternMatchListener which has been added to the console as a pattern match listener.

If you are not using a listener, then you have to find the offset of where you want to put the hyperlink by searching the console text.

You should be able to get the console text using:

IDocument document = myConsole.getDocument();

String text = document.get();

Find the text you want to use for the link:

String hyperlinkText = .... text you want to add the hyperlink to ...

int offset = text.indexOf(hyperlinkText);

Add the link if the text was found:

if (offset >= 0) {
   myconsole.addHyperlink(fileLink, offset, hyperlinkText.length());
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • I printed to my console via `MessageConsoleStream.println()`. The text appears in the plugin console but when I call `String text = myConsole.getDocument().get();` right after, I get back an empty string. – SebastianWilke Jan 27 '21 at 16:40
  • I believe the console is only updated a few times a second rather than immediately. – greg-449 Jan 27 '21 at 17:36
  • Thank you so much! I got it working now. I want to print ~10 lines and then hyperlink all of them. Do you have any recommendation as to how long to wait before I can be sure all of these lines have been updated? Ideally I want to hyperlink them as soon as possible after writing them. I tried `MessageConsoleStream.flush()` but this didn't work either. I could block with a while loop for as long as the document is empty, but I don't think this would be a good idea. – SebastianWilke Jan 27 '21 at 19:21