package com.redhat.ceylon.eclipse.core.launch;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.ISourcePathComputerDelegate;
import org.eclipse.debug.core.sourcelookup.containers.ExternalArchiveSourceContainer;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer;

import com.redhat.ceylon.cmr.api.ArtifactContext;
import com.redhat.ceylon.eclipse.core.builder.CeylonBuilder;

public class CeylonSourcePathComputer implements ISourcePathComputerDelegate {

    @Override
    public ISourceContainer[] computeSourceContainers(
            ILaunchConfiguration configuration, IProgressMonitor monitor)
            throws CoreException {
        IRuntimeClasspathEntry[] entries = JavaRuntime.computeUnresolvedSourceLookupPath(configuration);
        IRuntimeClasspathEntry[] resolved = JavaRuntime.resolveSourceLookupPath(entries, configuration);
        List<IRuntimeClasspathEntry> resolvedEntries = new ArrayList<>(resolved.length);
        for (IRuntimeClasspathEntry entry : resolved) {
            // Don't add the exploded Ceylon classes directory to the source containers !
            if (! entry.getPath().lastSegment().equals(CeylonBuilder.CEYLON_CLASSES_FOLDER_NAME)) {
                resolvedEntries.add(entry);
            }
        }
        List<ISourceContainer> containers = new ArrayList<ISourceContainer>(resolvedEntries.size());

        // When it's a Ceylon CAR archive that has a SRC attachment, 
        // also add the SRC archive as an archive container not only a PackageFragmentRoot-based container
        
        for (ISourceContainer container : JavaRuntime.getSourceContainers(resolvedEntries.toArray(new IRuntimeClasspathEntry[0]))) {
            containers.add(container);
            if (container instanceof PackageFragmentRootSourceContainer) {
                PackageFragmentRootSourceContainer pfrSourceContainer = (PackageFragmentRootSourceContainer) container;
                IPackageFragmentRoot pfr = pfrSourceContainer.getPackageFragmentRoot();
                if (pfr != null) {
                    IPath sourceAttachment = pfr.getSourceAttachmentPath();
                    if (sourceAttachment != null) {
                        if (sourceAttachment.lastSegment().endsWith(ArtifactContext.SRC)) {
                            containers.add(new ExternalArchiveSourceContainer(sourceAttachment.toOSString(), true));
                        }
                    }
                }
            }
        }
        return containers.toArray(new ISourceContainer[containers.size()]);
    }
}

@@ -41,16 +41,37 @@
 aunch;%0A%0A
+import java.io.File;%0A
 import j
@@ -107,22 +107,95 @@
 va.util.
-List;%0A
+Collection;%0Aimport java.util.List;%0A%0Aimport org.eclipse.core.resources.IProject;
 %0Aimport 
@@ -586,32 +586,74 @@
 ourceContainer;%0A
+import org.eclipse.jdt.core.IJavaProject;%0A
 import org.eclip
@@ -993,16 +993,71 @@
 Builder;
+%0Aimport com.redhat.ceylon.eclipse.core.model.JDTModule;
 %0A%0Apublic
@@ -2209,16 +2209,308 @@
        %0A
+        IJavaProject javaProject = JavaRuntime.getJavaProject(configuration);%0A        IProject project = javaProject != null ? javaProject.getProject() : null;%0A        Collection%3CJDTModule%3E modules = project != null ? CeylonBuilder.getProjectExternalModules(project) : null;%0A                %0A
         
@@ -3328,16 +3328,992 @@
 true));%0A
+                        %7D else if (sourceAttachment.lastSegment().endsWith(%22javaSources.zip%22)) %7B%0A                            File archiveFile = pfr.getPath().toFile();%0A                            if (archiveFile != null %0A                                    && modules != null) %7B%0A                                for (JDTModule m : modules) %7B%0A                                    if (m.isCeylonBinaryArchive() %0A                                            && archiveFile.equals(m.getArtifact())) %7B%0A                                        String sourceArchivePath = m.getSourceArchivePath();%0A                                        if (sourceArchivePath != null) %7B%0A                                            containers.add(new ExternalArchiveSourceContainer(sourceArchivePath, true));%0A                                            break;%0A                                        %7D%0A                                    %7D%0A                                %7D%0A                            %7D%0A
         

Fix #1537