Search in sources :

Example 26 with IXtextDocument

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

the class SimpleEditorTest method testOpenFileReadModifyRead.

@Test
public void testOpenFileReadModifyRead() throws Exception {
    IFile file = createFile("foo/y.testlanguage", "/* multi line */\n" + "stuff foo\n" + "stuff bar\n" + "// end");
    XtextEditor openEditor = openEditor(file);
    assertNotNull(openEditor);
    IXtextDocument document = 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(23, 3, "honolulu");
    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("honolulu", object2.eGet(object2.eClass().getEStructuralFeature("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) XtextResource(org.eclipse.xtext.resource.XtextResource) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Test(org.junit.Test) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest)

Example 27 with IXtextDocument

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

the class IssueDataTest method testIssueData.

@Test
public void testIssueData() throws Exception {
    IFile dslFile = dslFile(getProjectName(), getFileName(), getFileExtension(), MODEL_WITH_LINKING_ERROR);
    XtextEditor xtextEditor = openEditor(dslFile);
    IXtextDocument document = xtextEditor.getDocument();
    IResource file = xtextEditor.getResource();
    List<Issue> issues = getAllValidationIssues(document);
    assertEquals(1, issues.size());
    Issue issue = issues.get(0);
    assertEquals(2, issue.getLineNumber().intValue());
    assertEquals(3, issue.getColumn().intValue());
    assertEquals(PREFIX.length(), issue.getOffset().intValue());
    assertEquals(QuickfixCrossrefTestLanguageValidator.TRIGGER_VALIDATION_ISSUE.length(), issue.getLength().intValue());
    String[] expectedIssueData = new String[] { QuickfixCrossrefTestLanguageValidator.ISSUE_DATA_0, QuickfixCrossrefTestLanguageValidator.ISSUE_DATA_1 };
    assertTrue(Arrays.equals(expectedIssueData, issue.getData()));
    Thread.sleep(1000);
    IAnnotationModel annotationModel = xtextEditor.getDocumentProvider().getAnnotationModel(xtextEditor.getEditorInput());
    AnnotationIssueProcessor annotationIssueProcessor = new AnnotationIssueProcessor(document, annotationModel, new IssueResolutionProvider.NullImpl());
    annotationIssueProcessor.processIssues(issues, new NullProgressMonitor());
    Iterator<?> annotationIterator = annotationModel.getAnnotationIterator();
    // filter QuickDiffAnnotations
    List<Object> allAnnotations = Lists.newArrayList(annotationIterator);
    List<XtextAnnotation> annotations = newArrayList(filter(allAnnotations, XtextAnnotation.class));
    assertEquals(annotations.toString(), 1, annotations.size());
    XtextAnnotation annotation = annotations.get(0);
    assertTrue(Arrays.equals(expectedIssueData, annotation.getIssueData()));
    IssueUtil issueUtil = new IssueUtil();
    Issue issueFromAnnotation = issueUtil.getIssueFromAnnotation(annotation);
    assertTrue(Arrays.equals(expectedIssueData, issueFromAnnotation.getData()));
    new MarkerCreator().createMarker(issue, file, MarkerTypes.FAST_VALIDATION);
    IMarker[] markers = file.findMarkers(MarkerTypes.FAST_VALIDATION, true, IResource.DEPTH_ZERO);
    String errorMessage = new AnnotatedTextToString().withFile(dslFile).withMarkers(markers).toString().trim();
    assertEquals(errorMessage, 1, markers.length);
    String attribute = (String) markers[0].getAttribute(Issue.DATA_KEY);
    assertNotNull(attribute);
    assertTrue(Arrays.equals(expectedIssueData, Strings.unpack(attribute)));
    Issue issueFromMarker = issueUtil.createIssue(markers[0]);
    assertEquals(issue.getColumn(), issueFromMarker.getColumn());
    assertEquals(issue.getLineNumber(), issueFromMarker.getLineNumber());
    assertEquals(issue.getOffset(), issueFromMarker.getOffset());
    assertEquals(issue.getLength(), issueFromMarker.getLength());
    assertTrue(Arrays.equals(expectedIssueData, issueFromMarker.getData()));
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) Issue(org.eclipse.xtext.validation.Issue) AnnotatedTextToString(org.eclipse.xtext.ui.testing.util.AnnotatedTextToString) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) AnnotatedTextToString(org.eclipse.xtext.ui.testing.util.AnnotatedTextToString) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IssueUtil(org.eclipse.xtext.ui.util.IssueUtil) AnnotationIssueProcessor(org.eclipse.xtext.ui.editor.validation.AnnotationIssueProcessor) XtextAnnotation(org.eclipse.xtext.ui.editor.validation.XtextAnnotation) IssueResolutionProvider(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionProvider) IMarker(org.eclipse.core.resources.IMarker) MarkerCreator(org.eclipse.xtext.ui.editor.validation.MarkerCreator) IResource(org.eclipse.core.resources.IResource) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) AbstractQuickfixTest(org.eclipse.xtext.ui.testing.AbstractQuickfixTest) Test(org.junit.Test)

Example 28 with IXtextDocument

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

the class OutlineTreeProviderTest method testCreateChildren.

@Test
public void testCreateChildren() throws Exception {
    final String modelAsText = "element1 { element11 {}} element2 {}";
    IXtextDocument document = createXtextDocument(modelAsText);
    final IOutlineNode rootNode = treeProvider.createRoot(document);
    document.readOnly(new IUnitOfWork.Void<XtextResource>() {

        @Override
        public void process(XtextResource state) throws Exception {
            treeProvider.createChildren(rootNode, state.getContents().get(0));
            assertEquals(1, rootNode.getChildren().size());
            IOutlineNode modelNode = rootNode.getChildren().get(0);
            assertEquals(state.getURI().trimFileExtension().lastSegment(), modelNode.getText());
            assertEquals(new TextRegion(0, modelAsText.length()), modelNode.getSignificantTextRegion());
            assertEquals(new TextRegion(0, modelAsText.length()), modelNode.getFullTextRegion());
            assertEquals(rootNode, modelNode.getParent());
            assertTrue(modelNode.hasChildren());
            assertEquals(2, modelNode.getChildren().size());
            IOutlineNode element1 = modelNode.getChildren().get(0);
            assertEquals("element1", element1.getText().toString());
            assertEquals(new TextRegion(0, 8), element1.getSignificantTextRegion());
            assertEquals(new TextRegion(0, 24), element1.getFullTextRegion());
            assertEquals(modelNode, element1.getParent());
            assertTrue(element1.hasChildren());
            IOutlineNode element2 = modelNode.getChildren().get(1);
            assertEquals("element2", element2.getText().toString());
            assertEquals(new TextRegion(25, 8), element2.getSignificantTextRegion());
            assertEquals(new TextRegion(25, 11), element2.getFullTextRegion());
            assertEquals(modelNode, element2.getParent());
            assertFalse(element2.hasChildren());
        }
    });
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) TextRegion(org.eclipse.xtext.util.TextRegion) XtextResource(org.eclipse.xtext.resource.XtextResource) IOutlineNode(org.eclipse.xtext.ui.editor.outline.IOutlineNode) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Test(org.junit.Test)

Example 29 with IXtextDocument

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

the class XtextInformationProvider method getSubject.

@Override
public IRegion getSubject(ITextViewer textViewer, final int offset) {
    if (textViewer instanceof XtextSourceViewer) {
        IXtextDocument document = ((XtextSourceViewer) textViewer).getXtextDocument();
        if (document != null) {
            Object resolvedObject = document.priorityReadOnly(new IUnitOfWork<Object, XtextResource>() {

                @Override
                public Object exec(XtextResource state) throws Exception {
                    return eObjectAtOffsetHelper.resolveElementAt(state, offset);
                }
            });
            if (resolvedObject != null && resolvedObject instanceof EObject) {
                this.contextObject = (EObject) resolvedObject;
            }
        }
    }
    this.contextRegion = computeRegion(textViewer, offset);
    this.textViewer = textViewer;
    return contextRegion;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 30 with IXtextDocument

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

the class DirtyStateEditorSupport method initializeDirtyStateSupport.

public void initializeDirtyStateSupport(IDirtyStateEditorSupportClient client) {
    if (this.currentClient != null)
        // $NON-NLS-1$
        throw new IllegalStateException("editor was already assigned");
    this.currentClient = client;
    this.state = State.CLEAN;
    IXtextDocument document = client.getDocument();
    initDirtyResource(document);
    stateChangeEventBroker.addListener(this);
    client.addVerifyListener(this);
    scheduleValidationJobIfNecessary();
}
Also used : IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

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