Java Code Examples for org.eclipse.xtext.ui.editor.model.IXtextDocument#tryReadOnly()

The following examples show how to use org.eclipse.xtext.ui.editor.model.IXtextDocument#tryReadOnly() . 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: RailroadSynchronizer.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private void updateView(IWorkbenchPart part) {
	if (part instanceof XtextEditor) {
		XtextEditor xtextEditor = (XtextEditor) part;
		IXtextDocument xtextDocument = xtextEditor.getDocument();
		if (xtextDocument != lastActiveDocument) {
			selectionLinker.setXtextEditor(xtextEditor);
			final IFigure contents = xtextDocument.tryReadOnly(new IUnitOfWork<IFigure, XtextResource>() {
				@Override
				public IFigure exec(XtextResource resource) throws Exception {
					return createFigure(resource);
				}
			});
			if (contents != null) {
				view.setContents(contents);
				if (lastActiveDocument != null) {
					lastActiveDocument.removeModelListener(this);
				}
				lastActiveDocument = xtextDocument;
				lastActiveDocument.addModelListener(this);
			}
		}
	}
}
 
Example 2
Source File: DocumentBasedDirtyResource.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public synchronized void connect(IXtextDocument document) {
	if (document == null)
		throw new IllegalArgumentException("document may not be null");
	if (this.document == document)
		return;
	if (this.document != null)
		throw new IllegalStateException("Dirty resource was already connected to another document");
	this.document = document;
	document.tryReadOnly(new IUnitOfWork.Void<XtextResource>() {
		@Override
		public void process(XtextResource resource) {
			DocumentBasedDirtyResource.this.normalizedUri = EcoreUtil2.getNormalizedURI(resource);
			initiallyProcessResource(resource);
		}
	});
}
 
Example 3
Source File: EditorResourceAccess.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public <R> R readOnly(final URI targetURI, final IUnitOfWork<R, ResourceSet> work) {
	IXtextDocument document = openDocumentTracker.getOpenDocument(targetURI.trimFragment());
	if (document != null) {
		return document.tryReadOnly(new IUnitOfWork<R, XtextResource>() {
			@Override
			public R exec(XtextResource state) throws Exception {
				ResourceSet localContext = state.getResourceSet();
				if (localContext != null)
					return work.exec(localContext);
				return null;
			}
		}, () -> { return null; });
	} else {
		return delegate.readOnly(targetURI, work);
	}
}
 
Example 4
Source File: ReplAutoEdit.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void customizeDocumentCommand(final IDocument document, final DocumentCommand command) {
	if (!isLineDelimiter(document, command)) {
		return;
	}
	try {
		IXtextDocument doc = xtextDocumentUtil.getXtextDocument(document);
		String result = doc.tryReadOnly(new IUnitOfWork<String, XtextResource>() {
			@Override
			public String exec(XtextResource resource) throws Exception {
				return computeResultText(document, command, resource);
			}
		});
		if (result == null)
			return;
		command.text = result;
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: MultiModificationWrapper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void apply(IModificationContext ctx) throws Exception {
	IXtextDocument document = ctx.getXtextDocument();
	BatchModification batch = document.tryReadOnly(r -> r.getResourceServiceProvider().get(BatchModification.class));
	if (batch != null) {
		batch.setDocument(document);
		batch.apply(Collections.singleton(this), new NullProgressMonitor());
	}
}
 
Example 6
Source File: CompositeModificationWrapper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void apply(IModificationContext ctx) throws Exception {
	IXtextDocument document = ctx.getXtextDocument();
	BatchModification batch = document.tryReadOnly(r -> r.getResourceServiceProvider().get(BatchModification.class));
	if (batch != null) {
		batch.setDocument(document);
		batch.apply(Collections.singleton(this), new NullProgressMonitor());
	}
}
 
Example 7
Source File: DefaultFoldingRegionProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Collection<FoldedPosition> getFoldingRegions(final IXtextDocument xtextDocument) {
	return xtextDocument.tryReadOnly(new CancelableUnitOfWork<Collection<FoldedPosition>, XtextResource>() {
		@Override
		public Collection<FoldedPosition> exec(XtextResource xtextResource, CancelIndicator cancelIndicator)
				throws Exception {
			try {
				DefaultFoldingRegionProvider.this.cancelIndicator = cancelIndicator;
				return doGetFoldingRegions(xtextDocument, xtextResource);
			} finally {
				DefaultFoldingRegionProvider.this.cancelIndicator = null;
			}
		}
	}, () -> Collections.emptyList());
}
 
Example 8
Source File: ImportsAwareClipboardAction.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private XbaseClipboardData createClipboardData() {
	try {
		IEditorInput editorInput = getTextEditor().getEditorInput();
		final String sourceIdentifier = editorInput != null ? editorInput.toString() : "nullEditorInput";
		IXtextDocument document = getXtextDocument();
		final ISelection selection = getTextEditor().getSelectionProvider().getSelection();
		if (selection instanceof ITextSelection && !selection.isEmpty()) {
			final ITextSelection textSelection = (ITextSelection) selection;
			return document.tryReadOnly(new IUnitOfWork<XbaseClipboardData, XtextResource>() {
				@Override
				public XbaseClipboardData exec(XtextResource state) throws Exception {
					ITextRegion region = new TextRegion(textSelection.getOffset(), textSelection.getLength() - 1);
					Triple<Set<String>, Set<String>, Set<String>> imports = importsUtil.collectImports(state,
							region);
					XbaseClipboardData clipboardData = new XbaseClipboardData(sourceIdentifier,
							Iterables.toArray(imports.getFirst(), String.class),
							Iterables.toArray(imports.getSecond(), String.class),
							Iterables.toArray(imports.getThird(), String.class));
					return clipboardData;
				}
			});
		}
	} catch (Exception e) {
		//TODO Log exception
		return null;
	}
	return null;
}
 
Example 9
Source File: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Filter quickfixes for types and constructors.
 */
@Override
public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
	final IModificationContext modificationContext = getModificationContextFactory().createModificationContext(
			issue);
	final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
	if (xtextDocument != null) {
		xtextDocument.tryReadOnly(new CancelableUnitOfWork<Void, XtextResource>() {
			@Override
			public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
				try {
					EObject target = state.getEObject(issue.getUriToProblem().fragment());
					EReference reference = getUnresolvedEReference(issue, target);
					if (reference != null && reference.getEReferenceType() != null) {
						createLinkingIssueQuickfixes(issue,
								getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator), xtextDocument,
								state, target, reference);
					}
				} catch (WrappedException e) {
					// issue information seems to be out of sync, e.g. there is no
					// EObject with the given fragment
				}
				return null;
			}
		});
	}
}