Search in sources :

Example 61 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject 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 62 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class BuildOrderComputer method getBuildOrder.

/**
 * Convenience method for {@link #getBuildOrder(N4JSProject[])}
 */
public VertexOrder<IN4JSProject> getBuildOrder(N4JSExternalProject[] projects) {
    N4JSProject[] n4jsPrjs = new N4JSProject[projects.length];
    for (int i = 0; i < projects.length; i++) {
        N4JSExternalProject project = projects[i];
        URI locationURI = URIUtils.convert(project);
        N4JSProject prjDependency = (N4JSProject) core.findProject(locationURI).orNull();
        n4jsPrjs[i] = prjDependency;
    }
    return getBuildOrder(n4jsPrjs);
}
Also used : IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) N4JSProject(org.eclipse.n4js.internal.N4JSProject) N4JSExternalProject(org.eclipse.n4js.external.N4JSExternalProject) URI(org.eclipse.emf.common.util.URI)

Example 63 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class BuildOrderComputer method getDependencies.

private Set<N4JSProject> getDependencies(IN4JSProject project) {
    Set<N4JSProject> prjDependencies = new HashSet<>();
    ImmutableList<? extends IN4JSSourceContainerAware> deps = project.getAllDirectDependencies();
    for (IN4JSSourceContainerAware dep : deps) {
        IN4JSProject pDep = core.findProject(dep.getLocation()).orNull();
        boolean isValidDep = true;
        isValidDep &= pDep != null && pDep.exists();
        isValidDep &= !(pDep instanceof N4JSEclipseProject) || ((N4JSEclipseProject) pDep).getProject().isOpen();
        if (isValidDep) {
            prjDependencies.add((N4JSProject) pDep);
        }
    }
    return prjDependencies;
}
Also used : IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) N4JSProject(org.eclipse.n4js.internal.N4JSProject) N4JSEclipseProject(org.eclipse.n4js.ui.internal.N4JSEclipseProject) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) IN4JSSourceContainerAware(org.eclipse.n4js.projectModel.IN4JSSourceContainerAware) HashSet(java.util.HashSet)

Example 64 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class BuildOrderComputer method computeVertexOrder.

private VertexOrder<String> computeVertexOrder(String[] requestedProjectURIs) {
    // init worklist with existing projects
    TreeSet<String> worklist = new TreeSet<>();
    for (String projectURI : requestedProjectURIs) {
        IN4JSProject project = uri2PrjMapper.get(projectURI);
        if (project != null && project.exists()) {
            worklist.add(projectURI);
        }
    }
    // find dependency edges and project closure
    Multimap<String, String> edges = HashMultimap.create();
    TreeSet<String> projectsClosure = new TreeSet<>();
    while (!worklist.isEmpty()) {
        String projectURI = worklist.pollFirst();
        projectsClosure.add(projectURI);
        IN4JSProject project = uri2PrjMapper.get(projectURI);
        Set<N4JSProject> prjDependencies = getDependencies(project);
        Set<String> prjDependencyURIs = new HashSet<>();
        // add edges
        for (N4JSProject prjDependency : prjDependencies) {
            String depURI = prjDependency.getLocation().toString();
            edges.put(projectURI, depURI);
            prjDependencyURIs.add(depURI);
        }
        // add only new dependencies to the worklist
        prjDependencyURIs.removeAll(projectsClosure);
        worklist.addAll(prjDependencyURIs);
    }
    // transform dependency edges
    List<String[]> edges2 = new ArrayList<>();
    for (Map.Entry<String, String> dependency : edges.entries()) {
        String prj = dependency.getKey();
        String depOn = dependency.getValue();
        edges2.add(new String[] { prj, depOn });
    }
    VertexOrder<Object> orderedObjects = ComputeProjectOrder.computeVertexOrder(projectsClosure, edges2);
    VertexOrder<String> orderedURIs = ComputeProjectOrder.castVertexOrder(orderedObjects, String.class);
    return orderedURIs;
}
Also used : IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) N4JSProject(org.eclipse.n4js.internal.N4JSProject) ArrayList(java.util.ArrayList) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) TreeSet(java.util.TreeSet) Map(java.util.Map) HashSet(java.util.HashSet)

Example 65 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class N4JSProjectsStateHelper method initVisibleHandles.

public List<String> initVisibleHandles(String handle) {
    if (handle.startsWith(PROJECT_CONTAINER_PREFIX)) {
        // similar to source-container-prefix but we are only interested in the project/archive and
        // don't have an actual file of the source-locations.
        URI uri = URI.createURI(handle.substring(PROJECT_CONTAINER_PREFIX.length()));
        List<String> result = Lists.newArrayList();
        // Project.
        Optional<? extends IN4JSEclipseProject> containerProjectOpt = core.findProject(uri);
        if (containerProjectOpt.isPresent()) {
            fullCollectLocationHandles(result, containerProjectOpt.get());
        } else {
            // archive
            Optional<? extends IN4JSArchive> containerArchiveOpt = core.findArchive(uri);
            if (containerArchiveOpt.isPresent()) {
                // out of archive
                IN4JSArchive archive = containerArchiveOpt.get();
                fullCollectLocationHandles(result, archive);
            } else {
                // Nothing found.
                return Collections.emptyList();
            }
        }
        return result;
    }
    URI uri = URI.createURI(handle.substring(SOURCE_CONTAINER_PREFIX.length()));
    Optional<? extends IN4JSSourceContainer> containerOpt = core.findN4JSSourceContainer(uri);
    List<String> result = Lists.newArrayList();
    if (containerOpt.isPresent()) {
        IN4JSSourceContainer container = containerOpt.get();
        if (container.isLibrary()) {
            IN4JSArchive archive = container.getLibrary();
            fullCollectLocationHandles(result, archive);
        } else {
            IN4JSProject project = container.getProject();
            fullCollectLocationHandles(result, project);
        }
        return result;
    }
    return Collections.emptyList();
}
Also used : IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) IN4JSArchive(org.eclipse.n4js.projectModel.IN4JSArchive) URI(org.eclipse.emf.common.util.URI) IN4JSSourceContainer(org.eclipse.n4js.projectModel.IN4JSSourceContainer)

Aggregations

IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)93 URI (org.eclipse.emf.common.util.URI)32 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)13 List (java.util.List)11 Inject (com.google.inject.Inject)9 Map (java.util.Map)9 IProject (org.eclipse.core.resources.IProject)9 IN4JSCore (org.eclipse.n4js.projectModel.IN4JSCore)9 Optional (com.google.common.base.Optional)7 File (java.io.File)7 Collection (java.util.Collection)7 HashSet (java.util.HashSet)7 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 IOException (java.io.IOException)5 IN4JSArchive (org.eclipse.n4js.projectModel.IN4JSArchive)5 IN4JSSourceContainer (org.eclipse.n4js.projectModel.IN4JSSourceContainer)5 FluentIterable.from (com.google.common.collect.FluentIterable.from)4