Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext hover support
Xtext hover support [message #632437] Tue, 12 October 2010 22:18 Go to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
Does Xtext support any hovering? I want to show some documentation about function calls in my DSL. Is there something some hover class I can extend or do I have to extend XtextSourceViewerConfigurationa nd register a ITextHover?

Thank you.
Re: Xtext hover support [message #632477 is a reply to message #632437] Wed, 13 October 2010 06:35 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Please override the XtextSourceViewerConfiguration and therein the
method getTextHover(ISourceViewer, String).


Make sure you register a composite hover consisting of your custom hover
as well as Xtext's problem hover.

Sven


Am 10/13/10 12:18 AM, schrieb pgbackup@yahoo.com:
> Does Xtext support any hovering? I want to show some documentation about
> function calls in my DSL. Is there something some hover class I can
> extend or do I have to extend XtextSourceViewerConfigurationa nd
> register a ITextHover?
>
> Thank you.


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: Xtext hover support [message #632490 is a reply to message #632477] Wed, 13 October 2010 07:33 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi Sven

Thanks. It's so easy you that didn't document it.

Perhaps it might be worth adding a 'Finishing Touches' section to the
documentation.

Regards

Ed Willink

On 13/10/2010 07:35, Sven Efftinge wrote:
> Please override the XtextSourceViewerConfiguration and therein the
> method getTextHover(ISourceViewer, String).
>
>
> Make sure you register a composite hover consisting of your custom hover
> as well as Xtext's problem hover.
>
> Sven
>
>
> Am 10/13/10 12:18 AM, schrieb pgbackup@yahoo.com:
>> Does Xtext support any hovering? I want to show some documentation about
>> function calls in my DSL. Is there something some hover class I can
>> extend or do I have to extend XtextSourceViewerConfigurationa nd
>> register a ITextHover?
>>
>> Thank you.
>
>
Re: Xtext hover support [message #632701 is a reply to message #632437] Wed, 13 October 2010 23:54 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
Hi,

Thank you for the help. One thing I'm confused is the following:

Quote:

Make sure you register a composite hover consisting of your custom hover as well as Xtext's problem hover.



How do I do that? So far I have something like this:

1. In MyHLMUiModule.java


public Class<? extends XtextSourceViewerConfiguration> bindSourceViewerConfiguration() {
return MyHLMSourceViewerConfiguration.class;
}


2. MyHLMSourceViewerConfiguration.java

public class MyHLMSourceViewerConfiguration extends
		XtextSourceViewerConfiguration {
	
	@Override
	public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
		return new MyHLMTextHover(sourceViewer);
	}
}


3. MyHLMTextHover.java

public class MyHLMTextHover implements ITextHover, ITextHoverExtension2 {
	
	public MyHLMTextHover(ISourceViewer sourceViewer) {
		
	}

	@Override
	public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
		// Deprecated by Eclipse 3.4. Do not implement!
		return null;
	}

	@Override
	public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
		return new Region(offset, 0);
	}

	@Override
	public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
		return getHoverText(textViewer, hoverRegion);
	}

	private String getHoverText(ITextViewer textViewer, IRegion hoverRegion) {
		// Do the processing here.
		return "";
	}
}


How do I create a "composite" hover? It seems like setSourceViewConfiguration() will override the existing Xtext's sourceViewConfiguration. I see the 'new ProblemHover' in the getTextHover() of XtextSourceViewerConfiguration.

Thanks!
Re: Xtext hover support [message #632711 is a reply to message #632701] Thu, 14 October 2010 03:17 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Am 10/14/10 1:54 AM, schrieb pgbackup@yahoo.com:
> How do I create a "composite" hover?

I meant an implementation of org.eclipse.jface.text.ITextHover which
delegates to a list of "composed" ITextHover implementations.
The ProblemHover should come first and in case it returns something you
should use that, otherwise ask the next (your) ITextHover implementation.

Sven

P.S.: And in case you wrote something nice (incl. unit tests), please
file a bugzilla and attach a patch to it ;-)

--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de
Re: Xtext hover support [message #633291 is a reply to message #632437] Sat, 16 October 2010 04:13 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 101
Registered: August 2010
Senior Member
Here is some code I wrote that does what I want:

public class MyHLMSourceViewerConfiguration extends
		XtextSourceViewerConfiguration {
	
	@Override
	public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
		return new IInformationControlCreator() {
			
			@Override
			public IInformationControl createInformationControl(Shell parent) {
				return new DefaultInformationControl(parent, false);
			}
		};
	}

	@Override
	public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
			return new MyHLMBestMatchHover(sourceViewer);
	}
}


public class MyHLMBestMatchHover implements ITextHover, ITextHoverExtension2 {

	private List<ITextHoverExtension2> instantiatedTextHovers;
	
	public MyHLMBestMatchHover(ISourceViewer sourceViewer) {
		addTextHovers(sourceViewer);
	}

	private void addTextHovers(ISourceViewer sourceViewer) {
		// Add in order of precedence. Higher precedence first.
		instantiatedTextHovers = new ArrayList<ITextHoverExtension2>(2);
		// Xtext's problem hover (for syntax errors).
		instantiatedTextHovers.add(new ProblemHover(sourceViewer));
		// Our BBB information hover.
		instantiatedTextHovers.add(new MyHLMBBBHover(sourceViewer));
	}
	
	@Override
	public String getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
		if (instantiatedTextHovers == null) {
			return null;
		}
		for (Iterator<ITextHoverExtension2> it = instantiatedTextHovers.iterator(); it.hasNext(); ) {
			ITextHoverExtension2 hover = it.next();
			String s = (String) hover.getHoverInfo2(textViewer, hoverRegion);
			if (s != null && !s.trim().isEmpty()) {
				return s;
			}
		}
		return null;
	}
	
	@Override
	public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
		return getHoverInfo2(textViewer, hoverRegion);
	}

	@Override
	public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
		Point selection = textViewer.getSelectedRange();
		if (selection.x <= offset && offset < selection.x + selection.y) {
			return new Region(selection.x, selection.y);
		}
		return new Region(offset, 0);
	}
}


public class MyHLMBBBHover extends AbstractHover {
	
	private final MyHLMBBBFormatter bbbFormatter;
	
	/**
	 * Constructor.
	 * @param sourceViewer - The source viewer.
	 */
	public MyHLMBBBHover(ISourceViewer sourceViewer) {
		super(sourceViewer);
		bbbFormatter = new MyHLMBBBFormatter();
	}

	@Override
	protected String getHoverInfoInternal(final int lineNumber, final int offset) {
		try {
			IDocument doc = sourceViewer.getDocument();
			if (doc.get(offset, 1).trim().isEmpty()) {
				return null;
			}
			
			IRegion lineRegion = doc.getLineInformation(lineNumber);
			String line = doc.get(lineRegion.getOffset(), lineRegion.getLength());	
			int diff = offset - lineRegion.getOffset(); 
			int	firstSpace = 0, lastSpace = 0;
			for (int i = diff; i >= 0; i--) {
				if (!Character.isJavaIdentifierStart(line.charAt(i)) && !Character.isDigit(line.charAt(i))) {
					firstSpace = i;
					break;
				}
					
			}
			for (int i = diff; i < line.length(); i++) {
				if (!Character.isJavaIdentifierPart(line.charAt(i))) {
					lastSpace = i;
					break;
				}
			}
			if (firstSpace == lastSpace) {
				return null;
			}
			String word = line.substring(firstSpace + 1, (lastSpace > 0 ? lastSpace : line.length()));
			return bbbFormatter.getBBBDocumentation(word);
		} catch (BadLocationException e) {
			return null;
		}
	}
}


I had one question about getHoverInfoInternal. Given the line number/offset, i have to go character by character to extrract the function name(). Is it possible to use the Xtext parse model here somehow to get the element that matched at that particular offset?

Thanks.
Re: Xtext hover support [message #633347 is a reply to message #633291] Sat, 16 October 2010 19:12 Go to previous message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

the document you process may be an XtextDocument. Together with a "read only unit of work" and the utility class NodeUtil you should be able to get at the parse tree element/model element at the given offset.

Alex
Previous Topic:scoping ...union lists
Next Topic:[Xtext] programmatically create a new Xtetxt project
Goto Forum:
  


Current Time: Sat Apr 20 02:51:03 GMT 2024

Powered by FUDForum. Page generated in 0.03580 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top