org.eclipse.xtext.ui.editor.model.XtextDocument Java Examples

The following examples show how to use org.eclipse.xtext.ui.editor.model.XtextDocument. 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: XtextReconciler.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected IStatus run(final IProgressMonitor monitor) {
	if (monitor.isCanceled() || paused)
		return Status.CANCEL_STATUS;

	if (pendingChanges.isEmpty()) {
		return Status.OK_STATUS;
	}
	long start = System.currentTimeMillis();
	final IXtextDocument document = xtextDocumentUtil.getXtextDocument(textViewer);
	if (document instanceof XtextDocument) {
		((XtextDocument) document).internalModify(new IUnitOfWork.Void<XtextResource>() {
			@Override
			public void process(XtextResource state) throws Exception {
				doRun(state, monitor);
			}
		});
	}
	if (monitor.isCanceled()) {
		return Status.CANCEL_STATUS;
	}
	if (log.isDebugEnabled())
		log.debug("Reconciliation finished. Time required: " + (System.currentTimeMillis() - start)); //$NON-NLS-1$
	return Status.OK_STATUS;
}
 
Example #2
Source File: DotAutoEditStrategy.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
private String computeEndTag(IDocument document, DocumentCommand command) {

		IUnitOfWork<String, XtextResource> endTagComputationWork = new IUnitOfWork<String, XtextResource>() {

			@Override
			public String exec(XtextResource state) throws Exception {
				HtmlTag openTag = findOpenTag(state, command);
				if (openTag != null) {
					return "</" + openTag.getName() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
				} else {
					return ""; //$NON-NLS-1$
				}
			}

		};

		return ((XtextDocument) document).readOnly(endTagComputationWork);
	}
 
Example #3
Source File: AbstractXtextDocumentTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public XtextDocument getDocument(String s) {
	TerminalsTokenTypeToPartitionMapper mapper = new TerminalsTokenTypeToPartitionMapper() {{
		setTokenDefProvider(new AntlrTokenDefProvider() {
			{
				setAntlrTokenFileProvider(new XtextAntlrTokenFileProvider());
			}
		});
	}};
	PartitionTokenScanner scanner = new PartitionTokenScanner();
	scanner.setMapper(mapper);
	DocumentPartitioner partitioner = new DocumentPartitioner(scanner, mapper);
	DocumentTokenSource tokenSource = new DocumentTokenSource();
	tokenSource.setLexer(new Provider<Lexer>() {
		@Override
		public Lexer get() {
			return new org.eclipse.xtext.parser.antlr.internal.InternalXtextLexer();
		}
	});
	XtextDocument document = new XtextDocument(tokenSource, null, outdatedStateManager, operationCanceledManager);
	document.setDocumentPartitioner(partitioner);
	partitioner.connect(document);
	document.set(s);
	return document;
}
 
Example #4
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 #5
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 #6
Source File: PresentationDamagerTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Document createDocument(final String before) throws Exception {
  XtextDocument _xblockexpression = null;
  {
    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 = () -> {
        return this.createLexer();
      };
      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(before);
    _xblockexpression = document;
  }
  return _xblockexpression;
}
 
Example #7
Source File: DocumentRewriterTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected String rewrite(final CharSequence model, final Procedure2<? super DocumentRewriter, ? super XtextResource> test) {
  try {
    String _xblockexpression = null;
    {
      final XtextDocument document = this.createDocument(model.toString());
      final IUnitOfWork<TextEdit, XtextResource> _function = (XtextResource it) -> {
        TextEdit _xblockexpression_1 = null;
        {
          final DocumentRewriter rewriter = this.factory.create(document, it);
          test.apply(rewriter, it);
          _xblockexpression_1 = this._replaceConverter.convertToTextEdit(rewriter.getChanges());
        }
        return _xblockexpression_1;
      };
      document.<TextEdit>readOnly(_function).apply(document);
      _xblockexpression = document.get();
    }
    return _xblockexpression;
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #8
Source File: DocumentPartitionerTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public XtextDocument getDocument(String s) {
	TerminalsTokenTypeToPartitionMapper mapper = new TerminalsTokenTypeToPartitionMapper() {{
		setTokenDefProvider(new AntlrTokenDefProvider() {
			{
				setAntlrTokenFileProvider(new XtextAntlrTokenFileProvider());
			}
		});
	}};
	PartitionTokenScanner scanner = new PartitionTokenScanner();
	scanner.setMapper(mapper);
	DocumentPartitioner partitioner = new DocumentPartitioner(scanner, mapper);
	DocumentTokenSource tokenSource = new DocumentTokenSource();
	tokenSource.setLexer(new Provider<Lexer>() {
		@Override
		public Lexer get() {
			return new org.eclipse.xtext.parser.antlr.internal.InternalXtextLexer();
		}
	});
	XtextDocument document = new XtextDocument(tokenSource, null, new OutdatedStateManager(), new OperationCanceledManager());
	document.setDocumentPartitioner(partitioner);
	partitioner.connect(document);
	document.set(s);
	return document;
}
 
Example #9
Source File: DocumentPartitionerTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testSimple() throws Exception {
	XtextDocument document = getDocument("/* foo */ bar 345 grammar : so 'baz & so'");
	ITypedRegion partition = document.getPartition(0);
	assertEquals(0, partition.getOffset());
	assertEquals(9, partition.getLength());
	assertEquals(TerminalsTokenTypeToPartitionMapper.COMMENT_PARTITION, partition.getType());

	partition = document.getPartition(9);
	assertEquals(9, partition.getOffset());
	assertEquals(22, partition.getLength());
	assertEquals(IDocument.DEFAULT_CONTENT_TYPE, partition.getType());

	partition = document.getPartition(35);
	assertEquals(31, partition.getOffset());
	assertEquals(10, partition.getLength());
	assertEquals(TerminalsTokenTypeToPartitionMapper.STRING_LITERAL_PARTITION, partition.getType());
}
 
Example #10
Source File: XtextDocumentModifyTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private IXtextDocument createDocument(String model) throws Exception {
	resource = getResource(new StringInputStream(model));
	DocumentTokenSource tokenSource = new DocumentTokenSource();
	tokenSource.setLexer(new Provider<Lexer>(){
		@Override
		public Lexer get() {
			return new InternalXtextLexer();
		}});
	
	final XtextDocument document = new XtextDocument(tokenSource, get(ITextEditComposer.class), new OutdatedStateManager(), new OperationCanceledManager()) {
		@Override
		public <T> T internalModify(IUnitOfWork<T, XtextResource> work) {
			try {
				return work.exec((XtextResource) resource);
			}
			catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	};
	document.set(model);
	return document;
}
 
Example #11
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 #12
Source File: FixedHighlightingReconciler.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Uninstall this reconciler from the editor
 */
@Override
public void uninstall() {
  if (presenter != null) {
    presenter.setCanceled(true);
  }

  if (sourceViewer.getDocument() != null) {
    if (oldCalculator != null || newCalculator != null) {
      XtextDocument document = (XtextDocument) sourceViewer.getDocument();
      document.removeModelListener(this);
      sourceViewer.removeTextInputListener(this);
    }
  }
  synchronized (fReconcileLock) {
    if (reconciling) {
      cleanUpAfterReconciliation = true;
    } else {
      editor = null;
      sourceViewer = null;
      presenter = null;
    }
  }
}
 
Example #13
Source File: ReplacingAppendableTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testImports_4() throws Exception {
	final XtextDocument document = insertListField("package test\n" +
			"/**\n" +
			"*/\n" +
			"\n" + 
			"class Foo {|}", "foo");
	assertEqualsIgnoreWhitespace("package test\n" +
			"\n" +
			"import java.util.List\n" +
			"\n" +
			"/**\n" +
			"*/\n" +
			"\n" +
			"class Foo {List<?> foo}", document.get());
}
 
Example #14
Source File: ReplacingAppendableTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testImports_1() throws Exception {
	final XtextDocument document = insertListField(" \n" +
			"\t class Foo {|}", "foo");
	assertEqualsIgnoreWhitespace("import java.util.List\n" +
			"\n" +
			"class Foo {List<?> foo}", document.get());
}
 
Example #15
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 #16
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 #17
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 #18
Source File: ResponsiveXtextDocumentProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Performs (schedule) a "fast-only" validation job on preference changes.
 *
 * @param event
 *          the event
 */
@Override
public void propertyChange(final PropertyChangeEvent event) {
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug(NLS.bind("Preference Change: {0} => {1} -> {2}", new Object[] {event.getProperty(), event.getOldValue(), event.getNewValue()})); //$NON-NLS-1$
  }

  for (Iterator<?> i = getConnectedElements(); i.hasNext();) {
    ((XtextDocument) getDocument(i.next())).checkAndUpdateAnnotations();
  }
}
 
Example #19
Source File: FixedHighlightingReconciler.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Refreshes the highlighting.
 */
@Override
public void refresh() {
  if (oldCalculator != null || newCalculator != null) {
    IDocument document = editor != null ? editor.getDocument() : sourceViewer.getDocument();
    if (document instanceof IXtextDocument) {
      Job job = new Job("Calculating highlighting") { //$NON-NLS-1$
        @Override
        protected IStatus run(final IProgressMonitor monitor) {
          ((XtextDocument) document).readOnly(new CancelableUnitOfWork<Void, XtextResource>() {
            @Override
            public java.lang.Void exec(final XtextResource state, final CancelIndicator cancelIndicator) throws Exception {
              beforeRefresh(state, cancelIndicator);
              modelChanged(state, cancelIndicator);
              return null;
            }
          });
          return Status.OK_STATUS;
        }
      };
      job.setSystem(true);
      job.schedule();
    }
  } else {
    Display display = getDisplay();
    display.asyncExec(presenter.createSimpleUpdateRunnable());
  }
}
 
Example #20
Source File: GamlEditor.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void initializeDirtyStateSupport() {
	if (getDocument() != null) {
		fileURI = ((XtextDocument) getDocument()).getResourceURI();
		GamlResourceServices.addResourceListener(fileURI, GamlEditor.this);
		super.initializeDirtyStateSupport();
		scheduleValidationJob();
	}
}
 
Example #21
Source File: ImportAwareActionTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  this.document = this.<XtextDocument>get(XtextDocument.class);
  IDocumentPartitioner partitioner = this.<IDocumentPartitioner>get(IDocumentPartitioner.class);
  partitioner.connect(this.document);
  this.document.setDocumentPartitioner(partitioner);
}
 
Example #22
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 #23
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 #24
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 #25
Source File: XtextDocumentReconcileStrategy.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setDocument(IDocument document) {
	if (!(document instanceof XtextDocument)) {
		throw new IllegalArgumentException("Document must be an " + XtextDocument.class + " but was " + document.getClass().getName());
	}
	for (IReconcilingStrategy strategy: strategies) {
		strategy.setDocument(document);
	}
}
 
Example #26
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 #27
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 #28
Source File: AbstractDamagerRepairerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected Document createDocument(String before) throws Exception {
	DocumentTokenSource source = new DocumentTokenSource();
	source.setLexer(new Provider<Lexer>() {
		@Override
		public Lexer get() {
			return createLexer();
		}
	});
	XtextDocument document = new XtextDocument(source, null, outdatedStateManager, operationCanceledManager);
	document.set(before);
	return document;
}
 
Example #29
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 #30
Source File: AbstractDamagerRepairerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected Document createDocument(String before) throws Exception {
	DocumentTokenSource source = new DocumentTokenSource();
	source.setLexer(new Provider<Lexer>() {
		@Override
		public Lexer get() {
			return createLexer();
		}
	});
	XtextDocument document = new XtextDocument(source, null, outdatedStateManager, operationCanceledManager);
	document.set(before);
	return document;
}