Search in sources :

Example 46 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class XtextDocumentModifyTest method createDocument.

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;
}
Also used : OutdatedStateManager(org.eclipse.xtext.resource.OutdatedStateManager) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) ITextEditComposer(org.eclipse.xtext.ui.editor.model.edit.ITextEditComposer) OperationCanceledManager(org.eclipse.xtext.service.OperationCanceledManager) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) StringInputStream(org.eclipse.xtext.util.StringInputStream) InternalXtextLexer(org.eclipse.xtext.parser.antlr.internal.InternalXtextLexer) Lexer(org.eclipse.xtext.parser.antlr.Lexer) DocumentTokenSource(org.eclipse.xtext.ui.editor.model.DocumentTokenSource) InternalXtextLexer(org.eclipse.xtext.parser.antlr.internal.InternalXtextLexer)

Example 47 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class XtextDocumentModifyTest method testSemanticModification.

// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=406811
@Test
public void testSemanticModification() throws Exception {
    // @formatter:off
    String grammar = text("grammar foo.Foo", "generate foo \"http://foo.net/foo\"", "Foo: 'foo';");
    // @formatter:on
    IXtextDocument document = createDocument(grammar);
    document.modify(new IUnitOfWork.Void<XtextResource>() {

        @Override
        public void process(XtextResource state) throws Exception {
            Grammar grammar = (Grammar) state.getContents().get(0);
            grammar.setName("foo.Bar");
        }
    });
    assertEquals(grammar.replace("foo.Foo", "foo.Bar"), document.get());
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Test(org.junit.Test)

Example 48 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class XtextDocumentModifyTest method testTextualModification.

// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=406811
@Test
public void testTextualModification() throws Exception {
    // @formatter:off
    final String grammar = text("grammar foo.Foo", "generate foo \"http://foo.net/foo\"", "Foo: 'foo';");
    // @formatter:on
    final IXtextDocument document = createDocument(grammar);
    document.modify(new IUnitOfWork.Void<XtextResource>() {

        @Override
        public void process(XtextResource state) throws Exception {
            document.replace(grammar.indexOf("Foo"), 3, "Bar");
        }
    });
    assertEquals(grammar.replace("foo.Foo", "foo.Bar"), document.get());
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) XtextResource(org.eclipse.xtext.resource.XtextResource) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Test(org.junit.Test)

Example 49 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class DefaultFoldingRegionProviderTest method openFileAndReturnDocument.

protected IXtextDocument openFileAndReturnDocument(IFile iFile) throws Exception {
    XtextEditor editor = openEditor(iFile);
    IXtextDocument document = editor.getDocument();
    return document;
}
Also used : XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 50 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class DefaultFoldingRegionProviderTest method testShouldProcessContent.

@SuppressWarnings("deprecation")
@Test
public void testShouldProcessContent() throws Exception {
    IFile iFile = createFile("foo/bar.foldingtestlanguage", "element foo \n" + "element bar \n" + "end \n" + "end");
    IXtextDocument document = openFileAndReturnDocument(iFile);
    DefaultFoldingRegionProvider reg = new DefaultFoldingRegionProvider(new DefaultLocationInFileProvider()) {

        @Override
        protected boolean shouldProcessContent(EObject object) {
            if (object instanceof Element) {
                return !"foo".equals(((Element) object).getName());
            }
            return super.shouldProcessContent(object);
        }
    };
    Collection<FoldedPosition> regions = reg.getFoldingRegions(document);
    assertEquals(1, regions.size());
    assertEquals(0, regions.iterator().next().getOffset());
}
Also used : DefaultLocationInFileProvider(org.eclipse.xtext.resource.DefaultLocationInFileProvider) IFile(org.eclipse.core.resources.IFile) DefaultFoldingRegionProvider(org.eclipse.xtext.ui.editor.folding.DefaultFoldingRegionProvider) EObject(org.eclipse.emf.ecore.EObject) Element(org.eclipse.xtext.ui.tests.folding.Element) FoldedPosition(org.eclipse.xtext.ui.editor.folding.FoldedPosition) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Test(org.junit.Test) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest)

Aggregations

IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)147 XtextResource (org.eclipse.xtext.resource.XtextResource)51 Test (org.junit.Test)46 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)38 IFile (org.eclipse.core.resources.IFile)37 BadLocationException (org.eclipse.jface.text.BadLocationException)26 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)26 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)25 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)23 EObject (org.eclipse.emf.ecore.EObject)22 DefaultFoldingRegionProvider (org.eclipse.xtext.ui.editor.folding.DefaultFoldingRegionProvider)18 FoldedPosition (org.eclipse.xtext.ui.editor.folding.FoldedPosition)18 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)16 List (java.util.List)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 ArrayList (java.util.ArrayList)11 Issue (org.eclipse.xtext.validation.Issue)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)10 URI (org.eclipse.emf.common.util.URI)9 IRegion (org.eclipse.jface.text.IRegion)9