private URI getSelectionURI(ISelection currentSelection) { if (currentSelection instanceof IStructuredSelection) { IStructuredSelection iss = (IStructuredSelection) currentSelection; if (iss.size() == 1) { EObjectNode node = (EObjectNode) iss.getFirstElement(); return node.getEObjectURI(); } } else if (currentSelection instanceof TextSelection) { // Selection may be stale, get latest from editor XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(); TextSelection ts = (TextSelection) xtextEditor.getSelectionProvider().getSelection(); return xtextEditor.getDocument().readOnly(resource -> { EObject e = new EObjectAtOffsetHelper().resolveContainedElementAt(resource, ts.getOffset()); return EcoreUtil2.getURI(e); }); } return null; }
private static EObject getSelectedElement(final XtextEditor editor, final ITextSelection textSelection) { return editor.getDocument().modify( resource -> { final IResourceServiceProvider serviceProvider = resource.getResourceServiceProvider(); final EObjectAtOffsetHelper offsetHelper = serviceProvider.get(EObjectAtOffsetHelper.class); return offsetHelper.resolveElementAt(resource, textSelection.getOffset()); }); }
protected Language getSelectedLanguage(XtextResource resource, int offset){ final EObjectAtOffsetHelper eObjectAtOffsetHelper = resource.getResourceServiceProvider().get(EObjectAtOffsetHelper.class); EObject selectedElement = eObjectAtOffsetHelper.resolveContainedElementAt(resource, offset); if (selectedElement != null) { EObject currentElem = selectedElement; while(currentElem != null){ if(currentElem instanceof Language){ return (Language)currentElem; } currentElem = currentElem.eContainer(); } } return null; }
/** * Given the current position in the editing area, tries * to recover the corresponding actual node. * * @return the actual node, <code>null</code> if not possible */ public ICompositeNode getActualNode(){ if(xtextAdapter.getXtextParseResult()!=null){ int caretOffset = textArea.getCaretOffset(); EObjectAtOffsetHelper eobjHelper=new EObjectAtOffsetHelper(); EObject resolvedEObj = eobjHelper.resolveElementAt(xtextAdapter.getFakeResourceContext().getFakeResource(), caretOffset); return NodeModelUtils.findActualNodeFor(resolvedEObj); } return null; }
public static Optional<EObject> findEObjectAtPosition(XtextEditor editor, ITextSelection selection) { Optional<EObjectAtOffsetHelper> helper = determineEPackage(editor) .flatMap(InjectingFactoryRegistry.getInstance()::getFactory) .map(f -> f.inject(EObjectAtOffsetHelper.class)).map(EObjectAtOffsetHelper.class::cast); if (!helper.isPresent()) { return Optional.empty(); } return Optional.ofNullable(editor.getDocument().priorityReadOnly(new IUnitOfWork<EObject, XtextResource>() { @Override public EObject exec(XtextResource resource) throws Exception { return helper.get().resolveElementAt(resource, selection.getOffset()); } })); }
@Override public Object execute(ExecutionEvent event) throws ExecutionException { try { final XtextEditor editor = EditorUtils.getActiveXtextEditor(event); Optional<EPackage> ePackage = EditorHandlingUtils.determineEPackage(editor); Optional<EObjectAtOffsetHelper> eObjectAtOffsetHelper = ePackage .flatMap(InjectingFactoryRegistry.INSTANCE::getFactory) .map(f -> f.inject(EObjectAtOffsetHelper.class)).map(EObjectAtOffsetHelper.class::cast); if (!eObjectAtOffsetHelper.isPresent()) { throw new IllegalStateException("Unable to create required helper classes."); } if (editor != null) { final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); EObject selectedEObject = editor.getDocument() .priorityReadOnly(new IUnitOfWork<EObject, XtextResource>() { @Override public EObject exec(XtextResource resource) throws Exception { return eObjectAtOffsetHelper.get().resolveElementAt(resource, selection.getOffset()); } }); startRename(selectedEObject, editor, editor.getShell()); } } catch (OperationCanceledException e) { LOGGER.debug("Refactoring operation has been canceled.", e); } catch (Exception exc) { LOGGER.error("Error initializing refactoring", exc); MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error initializing refactoring", exc.getMessage() + "\nSee log for details"); } return null; }
@Override public void process(XtextResource state) throws Exception { EObjectAtOffsetHelper helper = new EObjectAtOffsetHelper(); if (offset != -1) { focusedElement = helper.resolveContainedElementAt(state, offset); } }
public Class<? extends EObjectAtOffsetHelper> bindEObjectAtOffsetHelper() { return BrokenConstructorCallAwareEObjectAtOffsetHelper.class; }
protected EObjectAtOffsetHelper getOffsetHelper() { if(offsetHelper == null){ offsetHelper = injector.getInstance(EObjectAtOffsetHelper.class); } return offsetHelper; }