Search in sources :

Example 26 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project xtext-eclipse by eclipse.

the class JdtTypesProposalProvider method getDirtyTypeNames.

/**
 * @since 2.8
 */
protected Set<String> getDirtyTypeNames() {
    Iterable<IEObjectDescription> dirtyTypes = dirtyStateManager.getExportedObjectsByType(TypesPackage.Literals.JVM_TYPE);
    final Set<String> dirtyNames = new HashSet<String>();
    for (IEObjectDescription description : dirtyTypes) {
        dirtyNames.add(description.getQualifiedName().toString());
    }
    for (URI dirtyURI : ((IDirtyStateManagerExtension) dirtyStateManager).getDirtyResourceURIs()) {
        IResourceDescriptions index = resourceDescriptionsProvider.createPersistedResourceDescriptions();
        IResourceDescription indexedResourceDescription = index.getResourceDescription(dirtyURI);
        if (indexedResourceDescription != null)
            for (IEObjectDescription desc : indexedResourceDescription.getExportedObjectsByType(TypesPackage.Literals.JVM_TYPE)) {
                dirtyNames.add(desc.getQualifiedName().toString());
            }
    }
    return dirtyNames;
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IDirtyStateManagerExtension(org.eclipse.xtext.ui.editor.IDirtyStateManagerExtension) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) StyledString(org.eclipse.jface.viewers.StyledString) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) HashSet(java.util.HashSet)

Example 27 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.

the class TestDiscoveryHelper method isTestable.

/**
 * Checks if the resource at the given location can be executed as a test. The location may point to an N4JS project
 * or a folder or file within an N4JS project.
 * <p>
 * This method is intended as a very first check to decide if the user should be given the option to launch a test
 * or not; for example, to enable or disable a "Run as test" context menu item). This method does not check all
 * details because many checks are better handled at later stages when meaningful error messages can be provided to
 * the user.
 */
public boolean isTestable(final URI location) {
    if (null == location) {
        return false;
    }
    if (isProject(location)) {
        // location points an N4JS project
        // --> must contain at least 1 source container of type "test"
        // (do *not* check if the folder contains test classes (for performance reasons and
        // to show better error messages; same behavior as in JUnit support in Eclipse))
        final IN4JSProject p = n4jsCore.create(location);
        return p.getSourceContainers().stream().anyMatch(IN4JSSourceContainer::isTest);
    } else {
        // then extract the fragment and check the location for the module.
        if (location.hasFragment()) {
            return isTestable(location.trimFragment());
        }
        // (2) location must lie in a source container of type "test"
        final IN4JSSourceContainer c = n4jsCore.findN4JSSourceContainer(location).orNull();
        if (c == null || !c.isTest())
            return false;
        // (3) if the location points to an n4js-file, it must contain at least one test class
        final ResourceSet resourceSet = n4jsCore.createResourceSet(Optional.of(c.getProject()));
        final IResourceDescriptions index = n4jsCore.getXtextIndex(resourceSet);
        final IResourceDescription rdesc = index.getResourceDescription(location);
        if (rdesc != null) {
            return stream(rdesc.getExportedObjectsByType(T_CLASS)).anyMatch(desc -> hasTestMethods(resourceSet, desc));
        } else {
            // to show better error messages; same behavior as in JUnit support in Eclipse))
            return true;
        }
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IN4JSSourceContainer(org.eclipse.n4js.projectModel.IN4JSSourceContainer)

Example 28 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.

the class TestDiscoveryHelper method collectTests.

/**
 * Collects all test methods available at the given location and returns a {@link TestTree} in which each test
 * method is represented by a {@link TestCase}. The location may point to an N4JS project or a folder or file within
 * an N4JS project.
 *
 * @param locations
 *            the locations referencing to a resource. Can be an N4JS project, or a folder or a file within an N4JS
 *            project.
 * @return The test tree representing one or more test cases. Never returns with {@code null}, but the returned test
 *         tree may be empty.
 */
public TestTree collectTests(final URI... locations) {
    final ResourceSet resSet = n4jsCore.createResourceSet(Optional.absent());
    final IResourceDescriptions index = n4jsCore.getXtextIndex(resSet);
    return collectTests(resSet, index, locations).sort();
}
Also used : IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Example 29 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.

the class EditorContentExtractor method getDescriptorForSemanticElement.

/**
 * Optionally returns with the semantic AST node element (given as the element URI) as a {@link StyledTextDescriptor
 * styled text descriptor}. If the element cannot be resolved or the styled text cannot be computed this method
 * returns with and {@link Optional#absent() absent} instance but never {@code null}.
 *
 * @param uri
 *            the URI of the semantic element in the AST.
 * @return a styled text descriptor representing the extracted code for the semantic AST node given with its unique
 *         URI.
 */
public Optional<StyledTextDescriptor> getDescriptorForSemanticElement(final URI uri) {
    if (null == uri) {
        return absent();
    }
    final URI trimmedUri = uri.hasFragment() ? uri.trimFragment() : uri;
    final IN4JSProject project = core.findProject(trimmedUri).orNull();
    if (project == null) {
        return absent();
    }
    final ResourceSet resSet = core.createResourceSet(Optional.of(project));
    final IResourceDescriptions index = core.getXtextIndex(resSet);
    final IResourceDescription resDesc = index.getResourceDescription(trimmedUri);
    if (null == resDesc) {
        return absent();
    }
    final TModule module = core.loadModuleFromIndex(resSet, resDesc, false);
    if (null == module || null == module.eResource() || null == module.eResource().getResourceSet()) {
        return absent();
    }
    final URI moduleUri = module.eResource().getURI();
    final IFile file = getWorkspace().getRoot().getFile(new Path(moduleUri.toPlatformString(true)));
    if (null == file || !file.exists()) {
        return absent();
    }
    final FileEditorInput editorInput = new FileEditorInput(file);
    try {
        docProvider.connect(editorInput);
    } catch (final CoreException e) {
        LOGGER.error("Error while connecting editor input with document provider: " + e);
        return absent();
    }
    final IDocument doc = docProvider.getDocument(editorInput);
    if (null == doc) {
        return absent();
    }
    final XtextResource xtextResource = (XtextResource) module.eResource();
    final ResourceSet resourceSet = xtextResource.getResourceSet();
    final EObject object = resourceSet.getEObject(uri, true);
    if (null == object) {
        return absent();
    }
    final ITextRegion textRegion = locationInFileProvider.getFullTextRegion(object);
    if (null == textRegion) {
        return absent();
    }
    try {
        final int lineOfOffset = doc.getLineOfOffset(textRegion.getOffset());
        final int lineOffset = doc.getLineOffset(lineOfOffset);
        final int offset = lineOffset;
        final int length = textRegion.getLength() + (textRegion.getOffset() - lineOffset);
        final String text = doc.get(offset, length);
        final IPresentationRepairer repairer = repairerProvider.get();
        final IPresentationDamager damager = damagerProvider.get();
        for (final String contentType : partitionTypeMapper.getSupportedPartitionTypes()) {
            reconciler.setRepairer(repairer, contentType);
            repairer.setDocument(doc);
            reconciler.setDamager(damager, contentType);
            damager.setDocument(doc);
        }
        final Region region = new Region(offset, length);
        final TextPresentation textPresentation = reconciler.createRepairDescription(region, doc);
        final Iterator<?> rangeItr = textPresentation.getAllStyleRangeIterator();
        final Collection<StyleRange> ranges = newLinkedList();
        while (rangeItr.hasNext()) {
            final Object next = rangeItr.next();
            if (next instanceof StyleRange) {
                ranges.add((StyleRange) next);
            }
        }
        final Range<Integer> textRange = Range.closed(offset, offset + length);
        for (final Iterator<StyleRange> itr = ranges.iterator(); itr.hasNext(); ) /* nothing */
        {
            final StyleRange range = itr.next();
            if (!textRange.contains(range.start) || !textRange.contains(range.start + range.length)) {
                itr.remove();
            } else {
                range.start = range.start - offset;
            }
        }
        return fromNullable(new StyledTextDescriptorImpl(text, ranges));
    } catch (final BadLocationException e) {
        LOGGER.error("Error while trying to extract text from document.", e);
        return absent();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) StyleRange(org.eclipse.swt.custom.StyleRange) XtextResource(org.eclipse.xtext.resource.XtextResource) URI(org.eclipse.emf.common.util.URI) IPresentationRepairer(org.eclipse.jface.text.presentation.IPresentationRepairer) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) EObject(org.eclipse.emf.ecore.EObject) TModule(org.eclipse.n4js.ts.types.TModule) IPresentationDamager(org.eclipse.jface.text.presentation.IPresentationDamager) TextPresentation(org.eclipse.jface.text.TextPresentation) Path(org.eclipse.core.runtime.Path) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) CoreException(org.eclipse.core.runtime.CoreException) ITextRegion(org.eclipse.xtext.util.ITextRegion) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) FileEditorInput(org.eclipse.ui.part.FileEditorInput) Region(org.eclipse.jface.text.Region) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 30 with IResourceDescriptions

use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.

the class ProjectCompareHelper method createComparison.

/**
 * First step in comparing API and implementation projects. This method will collect all API projects, the types
 * declared there and their members and relate them to the corresponding types and members in all available
 * implementation projects.
 * <p>
 * The second step of comparison is to compare an individual type/member to a corresponding type/member of a
 * particular implementation via method {@link #compareApiImpl(ProjectComparisonEntry, int)}.
 * <p>
 * Usually, this second step is performed lazily when method <code>#compareApiImpl()</code> is invoked; however, by
 * setting <code>fullCompare</code> to <code>true</code>, this is performed up-front here causing all results to be
 * stored in a cache in the entries, and then subsequent calls to <code>#compareApiImpl()</code> will just read from
 * that cache.
 * <p>
 * Returns <code>null</code> in case of error and adds human-readable error messages to the given list.
 */
public ProjectComparison createComparison(boolean fullCompare, List<String> addErrorMessagesHere) {
    final ResourceSet resourceSet = n4jsCore.createResourceSet(Optional.absent());
    final IResourceDescriptions index = n4jsCore.getXtextIndex(resourceSet);
    final ApiImplMapping mapping = ApiImplMapping.of(n4jsCore);
    if (mapping.hasErrors()) {
        if (addErrorMessagesHere != null)
            addErrorMessagesHere.addAll(mapping.getErrorMessages());
        return null;
    }
    final List<String> allImplIds = mapping.getAllImplIds();
    final int implCount = allImplIds.size();
    final ProjectComparison root = new ProjectComparison(allImplIds.toArray(new String[implCount]));
    for (String currApiId : mapping.getApiIds()) {
        final IN4JSProject currApi = mapping.getApi(currApiId);
        final IN4JSProject[] currImpls = new IN4JSProject[implCount];
        for (int idx = 0; idx < implCount; idx++) {
            final String currImplId = allImplIds.get(idx);
            currImpls[idx] = mapping.getImpl(currApiId, currImplId);
        }
        createEntries(root, currApi, currImpls, resourceSet, index);
    }
    // compare all implementations in all entries and store in cache (if requested)
    if (fullCompare) {
        root.getAllEntries().forEach(currE -> {
            for (int implIdx = 0; implIdx < implCount; implIdx++) compareApiImpl(currE, implIdx);
        });
    }
    return root;
}
Also used : IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Aggregations

IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)46 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)23 URI (org.eclipse.emf.common.util.URI)18 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)15 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)13 Resource (org.eclipse.emf.ecore.resource.Resource)12 EObject (org.eclipse.emf.ecore.EObject)11 QualifiedName (org.eclipse.xtext.naming.QualifiedName)7 IContainer (org.eclipse.xtext.resource.IContainer)7 List (java.util.List)6 Test (org.junit.Test)6 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)5 IScope (org.eclipse.xtext.scoping.IScope)5 IProject (org.eclipse.core.resources.IProject)4 EClass (org.eclipse.emf.ecore.EClass)4 XtextResource (org.eclipse.xtext.resource.XtextResource)4 IFile (org.eclipse.core.resources.IFile)3 CoreException (org.eclipse.core.runtime.CoreException)3 EPackage (org.eclipse.emf.ecore.EPackage)3 TModule (org.eclipse.n4js.ts.types.TModule)3