Search in sources :

Example 31 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project dsl-devkit by dsldevkit.

the class AbstractXtextTestUtil method getResource.

/**
 * Creates a resource with the given URI and content.
 *
 * @param uri
 *          to associate the model with
 * @param content
 *          String representation of the create a resource from
 * @return {@link XtextResource} created
 * @throws IOException
 *           may be thrown when trying to load the given content
 */
protected final XtextResource getResource(final URI uri, final String content) throws IOException {
    StringInputStream instanceStream = new StringInputStream(content);
    XtextResourceSet rs = getResourceSet();
    XtextResource resource = (XtextResource) rs.createResource(uri);
    rs.getResources().add(resource);
    resource.load(instanceStream, null);
    EcoreUtil.resolveAll(resource);
    return resource;
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 32 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project dsl-devkit by dsldevkit.

the class ScopeLinkingService method getIncludedModel.

/**
 * Returns the {@link ScopeModel#getIncludes() included scope model} corresponding to the given node from the node model.
 *
 * @param context
 *          context object
 * @param node
 *          parser node corresponding to include reference
 * @return referenced {@link ScopeModel} to include
 */
private List<EObject> getIncludedModel(final ScopeModel context, final INode node) {
    // based on XtextLinkingService#getUsedGrammar()
    try {
        String scopeModelName = (String) valueConverterService.toValue("", grammarAccess.getDottedIDRule().getName(), node);
        if (scopeModelName != null) {
            final ResourceSet resourceSet = context.eResource().getResourceSet();
            List<Resource> resources = resourceSet.getResources();
            for (int i = 0; i < resources.size(); i++) {
                Resource resource = resources.get(i);
                EObject rootElement = null;
                if (resource instanceof XtextResource && ((XtextResource) resource).getLanguageName().equals(languageName) && !resource.getContents().isEmpty()) {
                    rootElement = resource.getContents().get(0);
                }
                if (rootElement instanceof ScopeModel) {
                    ScopeModel otherModel = (ScopeModel) rootElement;
                    if (scopeModelName.equals(otherModel.getName())) {
                        return Collections.<EObject>singletonList(otherModel);
                    }
                }
            }
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + scopeModelName.replace('.', '/') + "." + fileExtensionProvider.getPrimaryFileExtension());
            final Resource resource = resourceSet.getResource(classpathURI, true);
            if (!resource.getContents().isEmpty()) {
                final ScopeModel otherModel = (ScopeModel) resource.getContents().get(0);
                if (scopeModelName.equals(otherModel.getName())) {
                    return Collections.<EObject>singletonList(otherModel);
                }
            }
        }
        return Collections.emptyList();
    } catch (ClasspathUriResolutionException e) {
        LOG.warn("Cannot load included scope.", e);
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        LOG.warn("Cannot convert included scope name.", e);
        return Collections.emptyList();
    }
}
Also used : ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ScopeModel(com.avaloq.tools.ddk.xtext.scope.scope.ScopeModel) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) URI(org.eclipse.emf.common.util.URI)

Example 33 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project dsl-devkit by dsldevkit.

the class AbstractFastLinkingService method getUsedGrammar.

/**
 * Tries to find a grammar.
 *
 * @param resourceSet
 *          to use for loading
 * @param grammarName
 *          qualified grammar name
 * @return A singleton list containing the grammar, or an empty list if not found.
 */
protected List<EObject> getUsedGrammar(final ResourceSet resourceSet, final String grammarName) {
    // copied from XtextLinkingService#getUsedGrammar()
    try {
        if (grammarName != null) {
            List<Resource> resources = resourceSet.getResources();
            for (int i = 0; i < resources.size(); i++) {
                Resource resource = resources.get(i);
                EObject rootElement = null;
                if (resource instanceof XtextResource) {
                    IParseResult parseResult = ((XtextResource) resource).getParseResult();
                    if (parseResult != null) {
                        rootElement = parseResult.getRootASTElement();
                    }
                } else if (!resource.getContents().isEmpty()) {
                    rootElement = resource.getContents().get(0);
                }
                if (rootElement instanceof Grammar) {
                    Grammar otherGrammar = (Grammar) rootElement;
                    if (grammarName.equals(otherGrammar.getName())) {
                        if (resource instanceof DerivedStateAwareResource) {
                            resource.getContents();
                        }
                        return Collections.<EObject>singletonList(otherGrammar);
                    }
                }
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + ".xtext");
            URI normalizedURI = null;
            if (resourceSet instanceof XtextResourceSet) {
                XtextResourceSet set = (XtextResourceSet) resourceSet;
                normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
            } else {
                normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
            }
            final Resource resource = resourceSet.getResource(normalizedURI, true);
            if (!resource.getContents().isEmpty()) {
                final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
                if (grammarName.equals(usedGrammar.getName())) {
                    return Collections.<EObject>singletonList(usedGrammar);
                }
            }
        }
        return Collections.emptyList();
    } catch (ClasspathUriResolutionException e) {
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        return Collections.emptyList();
    }
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult) Grammar(org.eclipse.xtext.Grammar) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) URI(org.eclipse.emf.common.util.URI)

Example 34 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project dsl-devkit by dsldevkit.

the class EObjectContentProvider method valuesForAttributes.

/**
 * Retrieve the object's values for the given EAttributes.
 *
 * @param attributes
 *          the EAttributes
 * @return List<Pair<EAttribute, Object>> the attribute+values - possibly containing null entries
 */
private List<AttributeValuePair> valuesForAttributes(final EList<EAttribute> attributes) {
    final URI elementUri = xtextElementSelectionListener.getSelectedElementUri();
    XtextEditor editor = xtextElementSelectionListener.getEditor();
    if (editor != null && elementUri != null) {
        return editor.getDocument().readOnly(new IUnitOfWork<List<AttributeValuePair>, XtextResource>() {

            @SuppressWarnings("PMD.SignatureDeclareThrowsException")
            public List<AttributeValuePair> exec(final XtextResource state) throws Exception {
                final EObject eObject = state.getEObject(elementUri.fragment());
                List<AttributeValuePair> pairs = Lists.transform(attributes, new Function<EAttribute, AttributeValuePair>() {

                    public AttributeValuePair apply(final EAttribute from) {
                        return new AttributeValuePair(from, eObject.eGet(from));
                    }
                });
                return pairs;
            }
        });
    }
    return newArrayList();
}
Also used : Function(com.google.common.base.Function) EAttribute(org.eclipse.emf.ecore.EAttribute) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) EObject(org.eclipse.emf.ecore.EObject) EList(org.eclipse.emf.common.util.EList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) XtextResource(org.eclipse.xtext.resource.XtextResource) URI(org.eclipse.emf.common.util.URI)

Example 35 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project xtext-xtend by eclipse.

the class JavaDocTypeReferenceProviderTest method testComputation_3.

@Test
public void testComputation_3() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("package foo");
        _builder.newLine();
        _builder.newLine();
        _builder.append("/**");
        _builder.newLine();
        _builder.append("* {@link java.util.ArrayList");
        _builder.newLine();
        _builder.append("*/");
        _builder.newLine();
        _builder.append("class Foo{}");
        _builder.newLine();
        final String input = _builder.toString();
        Resource _eResource = this.clazz(input).eResource();
        final XtextResource resource = ((XtextResource) _eResource);
        final ICompositeNode rootNode = resource.getParseResult().getRootNode();
        final List<ReplaceRegion> regions = this.javaDocTypeReferenceProvider.computeTypeRefRegions(rootNode);
        Assert.assertEquals(0, regions.size());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Aggregations

XtextResource (org.eclipse.xtext.resource.XtextResource)627 Test (org.junit.Test)367 Resource (org.eclipse.emf.ecore.resource.Resource)107 EObject (org.eclipse.emf.ecore.EObject)99 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)67 StringInputStream (org.eclipse.xtext.util.StringInputStream)67 URI (org.eclipse.emf.common.util.URI)62 Diagnostic (org.eclipse.emf.common.util.Diagnostic)55 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)55 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)46 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)40 Grammar (org.eclipse.xtext.Grammar)32 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)31 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)30 IFile (org.eclipse.core.resources.IFile)29 Issue (org.eclipse.xtext.validation.Issue)29 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)28 List (java.util.List)26 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)26 INode (org.eclipse.xtext.nodemodel.INode)23