Search in sources :

Example 46 with XtextDocument

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

the class SimpleEditorTest method testOpenFileReadModifyReadSecond.

@Test
public void testOpenFileReadModifyReadSecond() throws Exception {
    IFile file = createFile("foo/z.testlanguage", "/* multi line */\n" + "stuff foo\n" + "stuff bar\n" + "// end");
    XtextEditor openEditor = openEditor(file);
    assertNotNull(openEditor);
    XtextDocument document = (XtextDocument) openEditor.getDocument();
    Display.getDefault().readAndDispatch();
    document.readOnly(new IUnitOfWork.Void<XtextResource>() {

        @Override
        public void process(XtextResource resource) throws Exception {
            assertNotNull(resource);
            EList<EObject> contents = resource.getContents();
            EObject object = contents.get(0);
            assertEquals(2, object.eContents().size());
        }
    });
    document.replace(36, 0, "a");
    document.readOnly(new IUnitOfWork.Void<XtextResource>() {

        @Override
        public void process(XtextResource resource) throws Exception {
            assertNotNull(resource);
            EList<EObject> contents = resource.getContents();
            EObject object = contents.get(0);
            assertEquals(2, object.eContents().size());
            EObject object2 = object.eContents().get(0);
            assertEquals("foo", object2.eGet(object2.eClass().getEStructuralFeature("name")));
            EObject object3 = object.eContents().get(1);
            Object name = object3.eGet(object3.eClass().getEStructuralFeature("name"));
            assertEquals("bara", name);
        }
    });
    openEditor.doSave(null);
    openEditor.close(true);
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) IFile(org.eclipse.core.resources.IFile) EList(org.eclipse.emf.common.util.EList) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) EObject(org.eclipse.emf.ecore.EObject) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) EObject(org.eclipse.emf.ecore.EObject) Test(org.junit.Test) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest)

Example 47 with XtextDocument

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

the class ValidationJobSchedulerTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    referenceDescriptions = Lists.newArrayList();
    dirtyStateManager = new DirtyStateManager();
    testMe = new ValidationJobScheduler();
    testMe.setDirtyStateManager(dirtyStateManager);
    testMe.setDescriptionUtils(new DescriptionUtils());
    testMe.setResourceDescriptions(this);
    testMe.setResourceDescriptionManager(new DefaultResourceDescriptionManager());
    DocumentTokenSource nullSource = new DocumentTokenSource() {

        @Override
        protected IRegion computeDamageRegion(DocumentEvent e) {
            return new Region(0, 0);
        }
    };
    document = new XtextDocument(nullSource, null, outdatedStateManager, operationCanceledManager) {

        @Override
        public URI getResourceURI() {
            return documentURI;
        }

        @Override
        public void checkAndUpdateAnnotations() {
            validationScheduled = true;
        }
    };
    documentResource = new TestableDocumentResource();
    targetResource = new AbstractResourceDescription() {

        @Override
        public URI getURI() {
            return targetURI;
        }

        @Override
        public Iterable<IReferenceDescription> getReferenceDescriptions() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Iterable<QualifiedName> getImportedNames() {
            throw new UnsupportedOperationException();
        }

        @Override
        protected List<IEObjectDescription> computeExportedObjects() {
            if (documentResource.importedName == null)
                throw new UnsupportedOperationException();
            return Collections.emptyList();
        }
    };
}
Also used : DescriptionUtils(org.eclipse.xtext.resource.DescriptionUtils) AbstractResourceDescription(org.eclipse.xtext.resource.impl.AbstractResourceDescription) ValidationJobScheduler(org.eclipse.xtext.ui.editor.ValidationJobScheduler) DefaultResourceDescriptionManager(org.eclipse.xtext.resource.impl.DefaultResourceDescriptionManager) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) DocumentEvent(org.eclipse.jface.text.DocumentEvent) URI(org.eclipse.emf.common.util.URI) DirtyStateManager(org.eclipse.xtext.ui.editor.DirtyStateManager) DocumentTokenSource(org.eclipse.xtext.ui.editor.model.DocumentTokenSource) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) List(java.util.List)

Example 48 with XtextDocument

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

the class LexerTokenAndCharacterPairAwareStrategy method findWord.

@Override
protected IRegion findWord(IDocument document, int offset) {
    if (document instanceof XtextDocument) {
        Iterator<ILexerTokenRegion> tokenIterator = ((XtextDocument) document).getTokens().iterator();
        ILexerTokenRegion leadingToken = null;
        ILexerTokenRegion trailingToken = null;
        while (tokenIterator.hasNext()) {
            ILexerTokenRegion token = tokenIterator.next();
            if (token.getOffset() <= offset && token.getOffset() + token.getLength() >= offset) {
                if (leadingToken != null)
                    trailingToken = token;
                else
                    leadingToken = token;
            }
            if (token.getOffset() > offset)
                break;
        }
        if (leadingToken != null) {
            try {
                if (leadingToken.getLength() > 1 && (trailingToken == null || !Character.isLetter(document.getChar(trailingToken.getOffset())))) {
                    return new Region(leadingToken.getOffset(), leadingToken.getLength());
                } else if (trailingToken != null) {
                    return new Region(trailingToken.getOffset(), trailingToken.getLength());
                }
            } catch (BadLocationException ignore) {
            }
        }
    }
    return super.findWord(document, offset);
}
Also used : Region(org.eclipse.jface.text.Region) ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion) IRegion(org.eclipse.jface.text.IRegion) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 49 with XtextDocument

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

the class EmbeddedEditorModelAccess method updateModel.

public void updateModel(String model, final IUnitOfWork<ITextRegion, XtextResource> editablePartSelector) {
    XtextDocument document = (XtextDocument) this.viewer.getDocument();
    this.viewer.setRedraw(false);
    this.viewer.getUndoManager().disconnect();
    document.set(model);
    ITextRegion textRegion = document.readOnly(editablePartSelector);
    if (textRegion == null) {
        this.viewer.resetVisibleRegion();
    } else {
        this.viewer.setVisibleRegion(textRegion.getOffset(), textRegion.getLength());
    }
    this.viewer.getUndoManager().connect(this.viewer);
    this.viewer.setRedraw(true);
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument)

Example 50 with XtextDocument

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

the class XtextGrammarQuickfixProviderTest method validateInEditor.

private void validateInEditor(IXtextDocument document) {
    Job validationJob = ((XtextDocument) document).getValidationJob();
    validationJob.schedule();
    boolean wasInterrupted = false;
    do {
        try {
            Job.getJobManager().join(ValidationJob.XTEXT_VALIDATION_FAMILY, new NullProgressMonitor());
            wasInterrupted = false;
        } catch (OperationCanceledException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            wasInterrupted = true;
        }
    } while (wasInterrupted);
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) XtextDocument(org.eclipse.xtext.ui.editor.model.XtextDocument) ValidationJob(org.eclipse.xtext.ui.editor.validation.ValidationJob) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

XtextDocument (org.eclipse.xtext.ui.editor.model.XtextDocument)51 Test (org.junit.Test)22 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)15 XtextResource (org.eclipse.xtext.resource.XtextResource)14 DocumentTokenSource (org.eclipse.xtext.ui.editor.model.DocumentTokenSource)12 Lexer (org.eclipse.xtext.parser.antlr.Lexer)9 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)7 EList (org.eclipse.emf.common.util.EList)6 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)5 EObject (org.eclipse.emf.ecore.EObject)4 ITypedRegion (org.eclipse.jface.text.ITypedRegion)4 Region (org.eclipse.jface.text.Region)4 AntlrTokenDefProvider (org.eclipse.xtext.parser.antlr.AntlrTokenDefProvider)4 DocumentPartitioner (org.eclipse.xtext.ui.editor.model.DocumentPartitioner)4 IOutlineNode (org.eclipse.xtext.ui.editor.outline.IOutlineNode)4 AbstractXtextDocumentTest (org.eclipse.xtext.ui.tests.editor.model.AbstractXtextDocumentTest)4 IFile (org.eclipse.core.resources.IFile)3 Job (org.eclipse.core.runtime.jobs.Job)3 IRegion (org.eclipse.jface.text.IRegion)3 ITextEditComposer (org.eclipse.xtext.ui.editor.model.edit.ITextEditComposer)3