Java Code Examples for org.eclipse.xtext.ui.editor.model.XtextDocument#setInput()

The following examples show how to use org.eclipse.xtext.ui.editor.model.XtextDocument#setInput() . 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: Bug297909Test.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testReconcileDocument() throws Exception {
	Injector injector = doGetSetup().createInjectorAndDoEMFRegistration();
	XtextDocument document = injector.getInstance(XtextDocument.class);
	document.setValidationJob(new Job("Job") {
		@Override
		protected IStatus run(IProgressMonitor monitor) {
			return Status.OK_STATUS;
		}
	});
	XtextResource resource = injector.getInstance(XtextResource.class);
	XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
	resourceSet.getResources().add(resource);
	resource.load(new StringInputStream(""), Collections.singletonMap(XtextResource.OPTION_ENCODING, Charset.defaultCharset().name()));
	document.setInput(resource);
	document.set("abstract rules firstRule");
	XtextDocumentReconcileStrategy strategy = injector.getInstance(XtextDocumentReconcileStrategy.class);
	strategy.setDocument(document);
	strategy.setResource(resource);
	try {
		strategy.reconcile(new ReconcilerReplaceRegion(0, document.getLength(), document.get()));
		fail("Expected ParseException");
	} catch(ParseException expected) {
		assertTrue(expected.getMessage().contains("Make sure the EPackage has been registered"));
	}
}
 
Example 2
Source File: ReplacingAppendableTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected XtextDocument insertListField(final String model, final String fieldName)
		throws Exception {
	final int cursorPosition = model.indexOf('|');
	String actualModel = model.replace("|", " ");
	final XtendFile file = testHelper.xtendFile("Foo", actualModel);
	final XtextDocument document = documentProvider.get();
	document.set(actualModel);
	XtextResource xtextResource = (XtextResource) file.eResource();
	document.setInput(xtextResource);
	final EObject context = eObjectAtOffsetHelper.resolveElementAt(xtextResource, cursorPosition);
	document.modify(new IUnitOfWork.Void<XtextResource>() {
		@Override
		public void process(XtextResource state) throws Exception {
			ReplacingAppendable a = appendableFactory.create(document, state, cursorPosition, 1);
			ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, context);
			LightweightTypeReference typeRef = owner.toLightweightTypeReference(services.getTypeReferences().getTypeForName(List.class, context, typesFactory.createJvmWildcardTypeReference()));
			a.append(typeRef);
			a.append(" ").append(fieldName);
			a.commitChanges();
		}
	});
	return document;
}
 
Example 3
Source File: EmbeddedEditorModelAccess.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void setModel(XtextDocument document, String prefix, String editablePart, String suffix) {
	if (this.insertLineBreaks) {
		String delimiter = document.getDefaultLineDelimiter();
		prefix = prefix + delimiter;
		suffix = delimiter + suffix;
	}
	String model = prefix + editablePart + suffix;
	document.set(model);
	XtextResource resource = createResource(model);
	document.setInput(resource);
	AnnotationModel annotationModel = new AnnotationModel();
	if (document instanceof ISynchronizable) {
		Object lock = ((ISynchronizable) document).getLockObject();
		if (lock == null) {
			lock = new Object();
			((ISynchronizable) document).setLockObject(lock);
		}
		((ISynchronizable) annotationModel).setLockObject(lock);
	}
	this.viewer.setDocument(document, annotationModel, prefix.length(), editablePart.length());
}
 
Example 4
Source File: ContentAssistProcessorTestBuilder.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public IXtextDocument getDocument(final XtextResource xtextResource, final String model) {
	XtextDocument document = get(XtextDocument.class);
	document.set(model);
	document.setInput(xtextResource);
	DocumentPartitioner partitioner = get(DocumentPartitioner.class);
	partitioner.connect(document);
	document.setDocumentPartitioner(partitioner);
	return document;
}
 
Example 5
Source File: AbstractSARLOutlineTreeProviderTest.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Create a new assertion tool on a specific code resource.
 *
 * @param script the script to consider for producing the outline.
 * @return the assertion tool.
 * @throws Exception
 * @throws CoreException
 */
protected OutlineAsserts newOutlineAsserts(SarlScript script) throws Exception, CoreException {
	XtextDocument document = this.documentProvider.get();
	document.setInput((XtextResource) script.eResource());
	IOutlineNode root = this.treeProvider.createRoot(document);
	OutlineAsserts assertBuilder = new OutlineAsserts(root);
	return assertBuilder;
}
 
Example 6
Source File: ModificationContextRegistry.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a new document with the contents of the given {@link XtextResource}.
 * 
 * @param resource
 *          the resource to be used as input to the document
 * @return the created document
 */
private ICoreXtextDocument createDocument(final XtextResource resource) {
  XtextDocument document = documentProvider.get();
  if (resource.getParseResult() != null && resource.getParseResult().getRootNode() != null) {
    document.set(resource.getParseResult().getRootNode().getText());
  }
  document.setInput(resource);
  return new XtextDocumentAdapter(document);
}
 
Example 7
Source File: DocumentRewriterTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected XtextDocument createDocument(final String content) throws Exception {
  XtextDocument _xblockexpression = null;
  {
    final IFile file = this._workbenchTestHelper.createFile("Foo", content);
    final Resource resource = this._workbenchTestHelper.getResourceSet().getResource(URI.createPlatformResourceURI(file.getFullPath().toString(), true), true);
    XtendDocumentTokenSource _xtendDocumentTokenSource = new XtendDocumentTokenSource();
    final Procedure1<XtendDocumentTokenSource> _function = (XtendDocumentTokenSource it) -> {
      AntlrTokenDefProvider _antlrTokenDefProvider = new AntlrTokenDefProvider();
      final Procedure1<AntlrTokenDefProvider> _function_1 = (AntlrTokenDefProvider it_1) -> {
        XtendAntlrTokenFileProvider _xtendAntlrTokenFileProvider = new XtendAntlrTokenFileProvider();
        it_1.setAntlrTokenFileProvider(_xtendAntlrTokenFileProvider);
      };
      AntlrTokenDefProvider _doubleArrow = ObjectExtensions.<AntlrTokenDefProvider>operator_doubleArrow(_antlrTokenDefProvider, _function_1);
      it.setTokenDefProvider(_doubleArrow);
      final Provider<Lexer> _function_2 = () -> {
        InternalXtendLexer _internalXtendLexer = new InternalXtendLexer();
        return ((Lexer) _internalXtendLexer);
      };
      it.setLexer(_function_2);
      it.setFlexerFactory(this.flexerFactory);
    };
    final XtendDocumentTokenSource source = ObjectExtensions.<XtendDocumentTokenSource>operator_doubleArrow(_xtendDocumentTokenSource, _function);
    final XtextDocument document = new XtextDocument(source, null, this.outdatedStateManager, this.operationCanceledManager);
    document.set(content);
    document.setInput(((XtextResource) resource));
    _xblockexpression = document;
  }
  return _xblockexpression;
}
 
Example 8
Source File: AbstractOutlineTests.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected AssertBuilder newAssertBuilder(XtendFile xtendFile) throws Exception, CoreException {
	XtextDocument document = documentProvider.get();
	document.setInput((XtextResource) xtendFile.eResource());
	IOutlineNode root = treeProvider.createRoot(document);
	AssertBuilder assertBuilder = new AssertBuilder(root);
	return assertBuilder;
}
 
Example 9
Source File: DotHtmlLabelQuickfixDelegator.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * The implementation of the following helper methods are taken from the
 * org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder class.
 */
private IXtextDocument getDocument(String model) {
	XtextResource xtextResource = getXtextResource(model);
	XtextDocument document = injector.getInstance(XtextDocument.class);
	document.set(model);
	document.setInput(xtextResource);
	return document;
}
 
Example 10
Source File: DotEditorUtils.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private static IXtextDocument getDocument(final Injector injector,
		final XtextResource xtextResource, final String model) {
	XtextDocument document = injector.getInstance(XtextDocument.class);
	document.set(model);
	document.setInput(xtextResource);
	DocumentPartitioner partitioner = injector
			.getInstance(DocumentPartitioner.class);
	partitioner.connect(document);
	document.setDocumentPartitioner(partitioner);
	return document;
}
 
Example 11
Source File: AbstractQuickfixTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * The implementation of the following helper methods are taken from the
 * {@code org.eclipse.xtext.ui.testing.ContentAssistProcessorTestBuilder} class.
 */
protected IXtextDocument getDocument(String model) {
	XtextResource xtextResource = getXtextResource(model);
	XtextDocument document = injector.getInstance(XtextDocument.class);
	document.set(model);
	document.setInput(xtextResource);
	return document;
}
 
Example 12
Source File: OutlineNodeTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected DocumentRootNode createRootNode() {
	XtextDocument document = get(XtextDocument.class);
	document.setInput(resource);
	IOutlineTreeStructureProvider treeStructureProvider = new IOutlineTreeStructureProvider() {
		@Override
		public void createChildren(IOutlineNode parentNode, EObject modelElement) {
			new EObjectNode(child0Element, parentNode, (ImageDescriptor) null, "child", false);
		}
	};
	DocumentRootNode rootNode = new DocumentRootNode((ImageDescriptor) null, "root", document, treeStructureProvider);
	return rootNode;
}
 
Example 13
Source File: ContentAssistProcessorTestBuilder.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public IXtextDocument getDocument(final XtextResource xtextResource, final String model) {
	XtextDocument document = get(XtextDocument.class);
	document.set(model);
	document.setInput(xtextResource);
	DocumentPartitioner partitioner = get(DocumentPartitioner.class);
	partitioner.connect(document);
	document.setDocumentPartitioner(partitioner);
	return document;
}
 
Example 14
Source File: XtextOutlineTreeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected IOutlineNode assertNoException(String model) throws Exception {
	try {
		XtextResource resource = getResourceAndExpect(new StringInputStream(model), UNKNOWN_EXPECTATION);
		XtextDocument document = documentProvider.get();
		document.setInput(resource);
		IOutlineNode root = treeProvider.createRoot(document);
		traverseChildren(root);
		return root;
	} catch (Exception exc) {
		exc.printStackTrace();
		fail("Exception in outline tree construction");
	}
	return null; // unreachable
}
 
Example 15
Source File: DocumentLockerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testReadOnlyDoesntCancelReaders() {
	XtextDocument document = new XtextDocument(createTokenSource(), null, outdatedStateManager, operationCanceledManager);
	XtextResource resource = new XtextResource();
	new XtextResourceSet().getResources().add(resource);
	document.setInput(resource);
	List<CancelIndicator> cancelIndicators = new ArrayList<>();
	addReaderCancelationListener(document, cancelIndicators);
	Assert.assertTrue(cancelIndicators.isEmpty());
	document.readOnly(r -> null);
	Assert.assertTrue(cancelIndicators.isEmpty());
	document.readOnly(r -> null);
	Assert.assertTrue(cancelIndicators.isEmpty());
}
 
Example 16
Source File: DocumentLockerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testPriorityReadOnlyCancelsReaders() throws Exception {
	Thread.interrupted(); // prevent random test failures: https://github.com/junit-team/junit4/issues/1365
	XtextDocument document = new XtextDocument(createTokenSource(), null, outdatedStateManager, operationCanceledManager);
	XtextResource resource = new XtextResource();
	new XtextResourceSet().getResources().add(resource);
	document.setInput(resource);
	CountDownLatch check = new CountDownLatch(1);
	Runnable runnable = new Runnable() {

		@Override
		public void run() {
			document.readOnly(new CancelableUnitOfWork<Object, XtextResource>() {
				@Override
				public Object exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
					check.countDown();
					int wait = 4000;
					int i = 0;
					while (!cancelIndicator.isCanceled()) {
						Thread.sleep(10L);
						if (i > wait) {
							throw new InterruptedException();
						}
						i = i + 1;
					}
					return null;
				}
			});
		}

	};
	Thread thread = new Thread(runnable);
	thread.start();
	check.await();
	document.priorityReadOnly(r -> null);
	Assert.assertFalse(thread.isInterrupted());
}
 
Example 17
Source File: DocumentLockerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testNoUpdateContentProcessOnReentrant() {
	List<String> s = new ArrayList<>();
	XtextDocument document = new XtextDocument(createTokenSource(), createTextEditComposer(), outdatedStateManager,
			operationCanceledManager) {
		@Override
		protected boolean updateContentBeforeRead() {
			return s.add("x");
		}
	};
	XtextResource resource = new XtextResource();
	new XtextResourceSet().getResources().add(resource);
	document.setInput(resource);
	Assert.assertEquals(0, s.size());
	document.readOnly(r1 -> {
		Assert.assertEquals(1, s.size());
		document.readOnly(r2 -> {
			Assert.assertEquals(1, s.size());
			document.readOnly(r3 -> {
				Assert.assertEquals(1, s.size());
				return null;
			});
			return null;
		});
		return null;
	});
	Assert.assertEquals(1, s.size());
}
 
Example 18
Source File: OutlineTreeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected XtextDocument createXtextDocument(String modelAsText) throws Exception {
	final XtextResource resource = getResource(modelAsText, "test.outlinetestlanguage");
	DocumentTokenSource tokenSource = new DocumentTokenSource();
	tokenSource.setLexer(new Provider<Lexer>(){
		@Override
		public Lexer get() {
			return new InternalXtextLexer();
		}});
	XtextDocument xtextDocument = new XtextDocument(tokenSource, null, new OutdatedStateManager(), new OperationCanceledManager());
	xtextDocument.setInput(resource);
	xtextDocument.set(modelAsText);
	return xtextDocument;	
}