Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Set focus after quickfix
Set focus after quickfix [message #881260] Mon, 04 June 2012 08:42 Go to next message
Michael Ehrmann is currently offline Michael EhrmannFriend
Messages: 2
Registered: June 2012
Junior Member
Hi,

We're trying to implement a quickfix using a ISemanticModification, which introduces a new object in the model. Now we want to set the focus to this model, to let the user modify (refactor) the newly introduced random name of the object.

acceptor.accept(issue, "introduce prototype", "Introduce a new prototype", null, new ISemanticModification() {					
   public void apply(EObject element, IModificationContext context) throws Exception {
						
      Prototype newPrototype = ExamplePackage.eINSTANCE.getExampleFactory().createPrototype();
      model.getPrototypes().add(newPrototype);
							


Now we want to set the focus.
I understand that this would be possible by using

EditorUtils.getActiveXtextEditor().setHighlightRange(offset, length, true);



But we struggle determining offset/length.

I tried
locationInFileProvider.getFullTextRegion(newPrototype);
as well as
getSignificantTextRegion
but this seems to return bogus values.

NodeModelUtils.getNode(newPrototype);
seems also not possible.

It seems to me that the underlying model still has not yet been updated.
Anyone can give a hint?


Re: Set focus after quickfix [message #881411 is a reply to message #881260] Mon, 04 June 2012 13:55 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Michael,

I'm afraid you'll have to schedule a UI job that performs that task.
Alternatively you may want to use the underlying JFace API that allows
to access the ITextViewer.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 04.06.12 10:43, schrieb Michael Ehrmann:
> Hi,
>
> We're trying to implement a quickfix using a ISemanticModification,
> which introduces a new object in the model. Now we want to set the focus
> to this model, to let the user modify (refactor) the newly introduced
> random name of the object.
>
>
> acceptor.accept(issue, "introduce prototype", "Introduce a new
> prototype", null, new ISemanticModification() {
> public void apply(EObject element, IModificationContext context) throws
> Exception {
>
> Prototype newPrototype =
> ExamplePackage.eINSTANCE.getExampleFactory().createPrototype();
> model.getPrototypes().add(newPrototype);
>
>
>
> Now we want to set the focus.
> I understand that this would be possible by using
>
>
> EditorUtils.getActiveXtextEditor().setHighlightRange(offset, length, true);
>
>
>
> But we struggle determining offset/length.
>
> I tried locationInFileProvider.getFullTextRegion(newPrototype); as well
> as getSignificantTextRegion but this seems to return bogus values.
>
> NodeModelUtils.getNode(newPrototype); seems also not possible.
>
> It seems to me that the underlying model still has not yet been updated.
> Anyone can give a hint?
>
>
>
Re: Set focus after quickfix [message #881864 is a reply to message #881260] Tue, 05 June 2012 11:55 Go to previous message
Michael Ehrmann is currently offline Michael EhrmannFriend
Messages: 2
Registered: June 2012
Junior Member
This worked for me:

final String uriFragment = model.eResource().getURIFragment(newPrototype);
final IXtextDocument doc = context.getXtextDocument();

doc.addModelListener(new IXtextModelListener() {
	
	@Override
	public void modelChanged(XtextResource resource) {
		
		EObject eObject = resource.getEObject(uriFragment); //fetch our newly introduced prototype object
		if (eObject != null) {
			final ICompositeNode node = NodeModelUtils.getNode(eObject); //fetch the node, so we can get the offset										
			if (node != null) {										
				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
					@Override
					public void run() {
						EditorUtils.getActiveXtextEditor().setHighlightRange(node.getOffset(), node.getLength(), true);
					}
				});
			}									
		}
		doc.removeModelListener(this);
	}
});


Previous Topic:Problems since recent update (2.3 RC1)?
Next Topic:Please help with simple grammar
Goto Forum:
  


Current Time: Fri Sep 20 21:22:58 GMT 2024

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

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

Back to the top