Search in sources :

Example 6 with IUnitOfWork

use of org.eclipse.xtext.util.concurrent.IUnitOfWork in project xtext-xtend by eclipse.

the class Declarators method getDeclaratorData.

public Declarators.DeclaratorsData getDeclaratorData(final TargetURIs targetURIs, final IReferenceFinder.IResourceAccess resourceAccess) {
    Declarators.DeclaratorsData result = targetURIs.<Declarators.DeclaratorsData>getUserData(Declarators.KEY);
    if ((result != null)) {
        return result;
    }
    final HashSet<QualifiedName> declaratorNames = CollectionLiterals.<QualifiedName>newHashSet();
    final Consumer<URI> _function = (URI uri) -> {
        final IUnitOfWork<Object, ResourceSet> _function_1 = (ResourceSet it) -> {
            Object _xblockexpression = null;
            {
                final Consumer<URI> _function_2 = (URI objectURI) -> {
                    final EObject object = it.getEObject(objectURI, true);
                    if ((object != null)) {
                        final JvmType type = EcoreUtil2.<JvmType>getContainerOfType(object, JvmType.class);
                        if ((type != null)) {
                            QualifiedName _lowerCase = this.nameConverter.toQualifiedName(type.getIdentifier()).toLowerCase();
                            declaratorNames.add(_lowerCase);
                            QualifiedName _lowerCase_1 = this.nameConverter.toQualifiedName(type.getQualifiedName('.')).toLowerCase();
                            declaratorNames.add(_lowerCase_1);
                        }
                    }
                };
                targetURIs.getEObjectURIs(uri).forEach(_function_2);
                _xblockexpression = null;
            }
            return _xblockexpression;
        };
        resourceAccess.<Object>readOnly(uri, _function_1);
    };
    targetURIs.getTargetResourceURIs().forEach(_function);
    Declarators.DeclaratorsData _declaratorsData = new Declarators.DeclaratorsData(declaratorNames);
    result = _declaratorsData;
    targetURIs.<Declarators.DeclaratorsData>putUserData(Declarators.KEY, result);
    return result;
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) JvmType(org.eclipse.xtext.common.types.JvmType) URI(org.eclipse.emf.common.util.URI) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) Consumer(java.util.function.Consumer) EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject)

Example 7 with IUnitOfWork

use of org.eclipse.xtext.util.concurrent.IUnitOfWork in project xtext-xtend by eclipse.

the class CodeBuilderQuickfix method getXtendModification.

protected IModification getXtendModification(final ICodeBuilder.Xtend builder) {
    final IModification _function = (IModificationContext it) -> {
        final XtendTypeDeclaration xtendClass = builder.getXtendType();
        final IEditorPart editor = this.editorOpener.open(EcoreUtil.getURI(xtendClass), false);
        if ((!(editor instanceof XtextEditor))) {
            return;
        }
        final XtextEditor xtextEditor = ((XtextEditor) editor);
        final IXtextDocument document = xtextEditor.getDocument();
        final Wrapper<Integer> wrapper = Wrapper.<Integer>forType(Integer.class);
        final IUnitOfWork<ReplacingAppendable, XtextResource> _function_1 = (XtextResource resource) -> {
            ReplacingAppendable _xblockexpression = null;
            {
                int offset = builder.getInsertOffset(resource);
                wrapper.set(Integer.valueOf(offset));
                final int typeIndentation = this.getTypeIndentation(resource, document, xtendClass);
                DocumentSourceAppender.Factory.OptionalParameters _optionalParameters = new DocumentSourceAppender.Factory.OptionalParameters();
                final Procedure1<DocumentSourceAppender.Factory.OptionalParameters> _function_2 = (DocumentSourceAppender.Factory.OptionalParameters it_1) -> {
                    int _indentationLevel = builder.getIndentationLevel();
                    int _plus = (_indentationLevel + typeIndentation);
                    it_1.baseIndentationLevel = _plus;
                    it_1.ensureEmptyLinesAround = true;
                };
                DocumentSourceAppender.Factory.OptionalParameters _doubleArrow = ObjectExtensions.<DocumentSourceAppender.Factory.OptionalParameters>operator_doubleArrow(_optionalParameters, _function_2);
                _xblockexpression = this.appendableFactory.create(document, resource, offset, 0, _doubleArrow);
            }
            return _xblockexpression;
        };
        final ReplacingAppendable appendable = document.<ReplacingAppendable>readOnly(_function_1);
        Integer offset = wrapper.get();
        builder.build(appendable);
        appendable.commitChanges();
        xtextEditor.setHighlightRange(((offset).intValue() + 1), appendable.length(), true);
    };
    return _function;
}
Also used : Wrapper(org.eclipse.xtext.util.Wrapper) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) IEditorPart(org.eclipse.ui.IEditorPart) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) DocumentSourceAppender(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 8 with IUnitOfWork

use of org.eclipse.xtext.util.concurrent.IUnitOfWork in project xtext-eclipse by eclipse.

the class ImportURINavigationTest method doTestNavigation.

protected void doTestNavigation(IUnitOfWork<URI, IFile> uriComputation, boolean expectFQN) throws Exception {
    IJavaProject project = JavaProjectSetupUtil.createJavaProject("importuriuitestlanguage.project");
    try {
        IFile first = project.getProject().getFile("src/first.importuriuitestlanguage");
        first.create(new StringInputStream("type ASimpleType"), true, null);
        ResourceSet resourceSet = resourceSetProvider.get(project.getProject());
        Resource resource = resourceFactory.createResource(URI.createURI("synthetic://second.importuriuitestlanguage"));
        resourceSet.getResources().add(resource);
        String model = "import '" + uriComputation.exec(first) + "' type MyType extends ASimpleType";
        resource.load(new StringInputStream(model), null);
        EcoreUtil.resolveAll(resource);
        Assert.assertTrue(resource.getErrors().isEmpty());
        IHyperlink[] hyperlinks = helper.createHyperlinksByOffset((XtextResource) resource, model.indexOf("SimpleType"), false);
        Assert.assertEquals(1, hyperlinks.length);
        IWorkbenchPage activePage = workbench.getActiveWorkbenchWindow().getActivePage();
        Assert.assertNull(activePage.getActiveEditor());
        if (expectFQN) {
            Assert.assertEquals(URI.createURI(first.getLocationURI().toString()), ((XtextHyperlink) hyperlinks[0]).getURI().trimFragment());
        } else {
            Assert.assertEquals(URI.createPlatformResourceURI(first.getFullPath().toString(), true), ((XtextHyperlink) hyperlinks[0]).getURI().trimFragment());
        }
        hyperlinks[0].open();
        IEditorPart editor = activePage.getActiveEditor();
        Assert.assertNotNull(editor);
        IXtextDocument document = xtextDocumentUtil.getXtextDocument(editor);
        document.readOnly(new IUnitOfWork.Void<XtextResource>() {

            @Override
            public void process(XtextResource state) throws Exception {
                Assert.assertEquals("platform:/resource/importuriuitestlanguage.project/src/first.importuriuitestlanguage", state.getURI().toString());
            }
        });
        Assert.assertEquals("type ASimpleType", document.get());
        IEditorPart newPart = IDE.openEditor(activePage, first);
        Assert.assertEquals(1, activePage.getEditorReferences().length);
        Assert.assertEquals(editor, newPart);
    } finally {
        project.getProject().delete(true, null);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) XtextHyperlink(org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IEditorPart(org.eclipse.ui.IEditorPart) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) StringInputStream(org.eclipse.xtext.util.StringInputStream) IJavaProject(org.eclipse.jdt.core.IJavaProject) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 9 with IUnitOfWork

use of org.eclipse.xtext.util.concurrent.IUnitOfWork in project xtext-eclipse by eclipse.

the class EditorResourceAccessTest method testAccess.

@Test
public void testAccess() throws Exception {
    EditorResourceAccess access = TestsActivator.getInstance().getInjector(getEditorId()).getInstance(EditorResourceAccess.class);
    IUnitOfWork<Boolean, ResourceSet> isEmpty = new IUnitOfWork<Boolean, ResourceSet>() {

        @Override
        public Boolean exec(ResourceSet state) throws Exception {
            return state.getResources().get(0).getContents().isEmpty();
        }
    };
    assertTrue(access.readOnly(uri, isEmpty));
    XtextEditor editor = openEditor(file);
    runAsyncJobs();
    assertTrue(access.readOnly(uri, isEmpty));
    editor.getDocument().set("A");
    runAsyncJobs();
    waitForReconciler(editor);
    runAsyncJobs();
    assertFalse(access.readOnly(uri, isEmpty));
    editor.close(false);
    runAsyncJobs();
    assertTrue(access.readOnly(uri, isEmpty));
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) EditorResourceAccess(org.eclipse.xtext.ui.editor.findrefs.EditorResourceAccess) Test(org.junit.Test) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest)

Example 10 with IUnitOfWork

use of org.eclipse.xtext.util.concurrent.IUnitOfWork in project xtext-eclipse by eclipse.

the class SelectionUtil method getSelectedFile.

public static IFile getSelectedFile(ISelection selection) {
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection ssel = (IStructuredSelection) selection;
        Object firstElement = ssel.getFirstElement();
        IFile file = Adapters.adapt(firstElement, IFile.class);
        if (file != null) {
            return file;
        } else if (firstElement instanceof IOutlineNode) {
            IOutlineNode outlineNode = (IOutlineNode) firstElement;
            return outlineNode.tryReadOnly(new IUnitOfWork<IFile, EObject>() {

                @Override
                public IFile exec(EObject state) throws Exception {
                    Resource resource = state.eResource();
                    URI uri = resource.getURI();
                    IPath path = new Path(uri.toPlatformString(true));
                    return ResourcesPlugin.getWorkspace().getRoot().getFile(path);
                }
            });
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) EObject(org.eclipse.emf.ecore.EObject) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) URI(org.eclipse.emf.common.util.URI) IOutlineNode(org.eclipse.xtext.ui.editor.outline.IOutlineNode)

Aggregations

IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)14 XtextResource (org.eclipse.xtext.resource.XtextResource)7 EObject (org.eclipse.emf.ecore.EObject)5 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)5 IModification (org.eclipse.xtext.ui.editor.model.edit.IModification)4 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)4 IFile (org.eclipse.core.resources.IFile)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)3 Rule (com.avaloq.tools.ddk.xtext.format.format.Rule)2 URI (org.eclipse.emf.common.util.URI)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 IEditorPart (org.eclipse.ui.IEditorPart)2 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Consumer (java.util.function.Consumer)1 IPath (org.eclipse.core.runtime.IPath)1