Set focus after quickfix [message #881260] |
Mon, 04 June 2012 04:42  |
Eclipse User |
|
|
|
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 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 09:55   |
Eclipse User |
|
|
|
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 07:55  |
Eclipse User |
|
|
|
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);
}
});
|
|
|
Powered by
FUDForum. Page generated in 0.03064 seconds