Java Code Examples for org.eclipse.xtext.ui.editor.utils.EditorUtils#getXtextEditor()

The following examples show how to use org.eclipse.xtext.ui.editor.utils.EditorUtils#getXtextEditor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LanguageSpecificURIEditorOpener.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public IEditorPart open(final URI uri, final EReference crossReference, final int indexInList, final boolean select) {
	Iterator<Pair<IStorage, IProject>> storages = mapper.getStorages(uri.trimFragment()).iterator();
	if (storages != null && storages.hasNext()) {
		try {
			IStorage storage = storages.next().getFirst();
			IEditorInput editorInput = EditorUtils.createEditorInput(storage);
			IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage();
			final IEditorPart editor = IDE.openEditor(activePage, editorInput, getEditorId());
			selectAndReveal(editor, uri, crossReference, indexInList, select);
			return EditorUtils.getXtextEditor(editor);
		} catch (WrappedException e) {
			logger.error("Error while opening editor part for EMF URI '" + uri + "'", e.getCause());
		} catch (PartInitException partInitException) {
			logger.error("Error while opening editor part for EMF URI '" + uri + "'", partInitException);
		}
	}
	return null;
}
 
Example 2
Source File: ImportsAwareClipboardAction.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private void doPasteWithImportsOperation() {
	XbaseClipboardData xbaseClipboardData = ClipboardUtil
			.clipboardOperation(new Function<Clipboard, XbaseClipboardData>() {
				@Override
				public XbaseClipboardData apply(Clipboard input) {
					Object content = input.getContents(TRANSFER_INSTANCE);
					if (content instanceof XbaseClipboardData) {
						return (XbaseClipboardData) content;
					}
					return null;
				}
			});
	JavaImportData javaImportsContent = ClipboardUtil.getJavaImportsContent();
	String textFromClipboard = ClipboardUtil.getTextFromClipboard();
	XtextEditor xtextEditor = EditorUtils.getXtextEditor(getTextEditor());
	boolean addImports = shouldAddImports(xtextEditor.getDocument(), caretOffset(xtextEditor));
	if (xbaseClipboardData != null && !sameTarget(xbaseClipboardData)) {
		doPasteXbaseCode(xbaseClipboardData, addImports);
	} else if (javaImportsContent != null) {
		doPasteJavaCode(textFromClipboard, javaImportsContent, addImports);
	} else {
		textOperationTarget.doOperation(operationCode);
	}
}
 
Example 3
Source File: PlatformPluginAwareEditorOpener.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * If a platform plugin URI is given, a read-only Xtext editor is opened and returned. {@inheritDoc}
 *
 * @see {@link org.eclipse.emf.common.util.URI#isPlatformPlugin()}
 */
@Override
public IEditorPart open(final URI uri, final EReference crossReference, final int indexInList, final boolean select) {
  IEditorPart result = super.open(uri, crossReference, indexInList, select);
  if (result == null && (uri.isPlatformPlugin() || OSGI_RESOURCE_URL_PROTOCOL.equals(uri.scheme()))) {
    final IModelLocation modelLocation = getModelLocation(uri.trimFragment());
    if (modelLocation != null) {
      PlatformPluginStorage storage = new PlatformPluginStorage(modelLocation);
      IEditorInput editorInput = new XtextReadonlyEditorInput(storage);
      IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage();
      try {
        IEditorPart editor = IDE.openEditor(activePage, editorInput, editorID);
        selectAndReveal(editor, uri, crossReference, indexInList, select);
        return EditorUtils.getXtextEditor(editor);
      } catch (WrappedException e) {
        LOG.error("Error while opening editor part for EMF URI '" + uri + "'", e.getCause()); //$NON-NLS-1$ //$NON-NLS-2$
      } catch (PartInitException partInitException) {
        LOG.error("Error while opening editor part for EMF URI '" + uri + "'", partInitException); //$NON-NLS-1$ //$NON-NLS-2$
      }
    }
  }
  return result;
}
 
Example 4
Source File: AbstractEditorTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private XtextEditor getXtextEditor(IEditorPart openEditor) throws NoSuchFieldException, IllegalAccessException {
	XtextEditor xtextEditor = EditorUtils.getXtextEditor(openEditor);
	if (xtextEditor != null) {
		ISourceViewer sourceViewer = xtextEditor.getInternalSourceViewer();
		((ProjectionViewer) sourceViewer).doOperation(ProjectionViewer.EXPAND_ALL);
		return xtextEditor;
	} else if (openEditor instanceof ErrorEditorPart) {
		Field field = openEditor.getClass().getDeclaredField("error");
		field.setAccessible(true);
		throw new IllegalStateException("Couldn't open the editor.", ((Status) field.get(openEditor)).getException());
	} else {
		fail("Opened Editor with id:" + getEditorId() + ", is not an XtextEditor");
	}
	return null;
}
 
Example 5
Source File: AbstractSarlLaunchShortcut.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public IResource getLaunchableResource(IEditorPart editorpart) {
	final XtextEditor xtextEditor = EditorUtils.getXtextEditor(editorpart);
	if (xtextEditor != null) {
		return xtextEditor.getResource();
	}
	return null;
}
 
Example 6
Source File: AbstractSarlLaunchShortcut.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void launch(IEditorPart editor, String mode) {
	final XtextEditor xtextEditor = EditorUtils.getXtextEditor(editor);
	final ISelection selection = xtextEditor.getSelectionProvider().getSelection();
	if (selection instanceof ITextSelection) {
		final ITextSelection sel = (ITextSelection) selection;
		final EObject obj = xtextEditor.getDocument().readOnly(resource -> {
			final IParseResult parseRes = resource.getParseResult();
			if (parseRes == null) {
				return null;
			}
			final ICompositeNode rootNode = parseRes.getRootNode();
			final ILeafNode node = NodeModelUtils.findLeafNodeAtOffset(rootNode, sel.getOffset());
			return NodeModelUtils.findActualSemanticObjectFor(node);
		});
		if (obj != null) {
			final EObject elt = EcoreUtil2.getContainerOfType(obj, getValidEObjectType());
			if (elt != null) {
				searchAndLaunch(mode, elt);
				return;
			}
		}
	} else if (selection != null) {
		launch(selection, mode);
		return;
	}
	// Default launching
	searchAndLaunch(mode, xtextEditor.getResource());
}
 
Example 7
Source File: ImportsAwareClipboardAction.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
private IXtextDocument getXtextDocument() {
	XtextEditor xtextEditor = EditorUtils.getXtextEditor(getTextEditor());
	IXtextDocument document = xtextEditor.getDocument();
	return document;
}
 
Example 8
Source File: IEditorUtils.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public XtextEditor getXtextEditor(final IEditorPart openEditor) {
  return EditorUtils.getXtextEditor(openEditor);
}