<commit_before>package org.eclipselabs.recommenders.codesearchquery.rcp.popup.actions;

import java.io.File;
import java.util.List;

import org.apache.lucene.store.SimpleFSDirectory;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipselabs.recommenders.codesearchquery.rcp.Activator;
import org.eclipselabs.recommenders.codesearchquery.rcp.indexer.CodeIndexerIndex;

import com.google.common.collect.Lists;

public class IndexAction implements IObjectActionDelegate {

    //private Shell shell;
    private List<IProject> projects = null;

    /**
     * Constructor for Action1.
     */
    public IndexAction() {
        super();
    }

    /**
     * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
     */
    @Override
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        //shell = targetPart.getSite().getShell();
    }

    /**
     * @see IActionDelegate#run(IAction)
     */
    @Override
    public void run(IAction action) {
    	try {
	        final Long start = System.currentTimeMillis();
	        String path = Platform.getLocation().toString() + "/index.l";
	        final CodeIndexerIndex index = new CodeIndexerIndex(new SimpleFSDirectory(new File(path)));
	                
	        final WorkspaceJob job = new WorkspaceJob("Indexing sources...") {
	            
	            @Override
	            public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
	   
	                try {
	                    index.printStats();
	                    
	                    for (IProject p : projects) {
	                        try {
	                            
	                            IPackageFragment[] packages = JavaCore.create(p).getPackageFragments();
	        
	                            for (IPackageFragment mypackage : packages) {
	                                if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
	                                    for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
	                                        // Now create the AST for the ICompilationUnits
	                                        try {
	                                            Activator.logInfo("Indexing ICompilationUnit %1$s ...", unit.getPath());
	                                            
	                                            CompilationUnit cu = parse(unit);
	                                            
	                                            index.index(cu);
	                                        }catch(Exception e) {
	                                            Activator.logWarning(e, "ICompilationUnit %1$s has not been indexed successfully", unit.getPath());
	                                        }
	                                    }
	                                }
	                            }
	                        } catch(Exception e) {
	                            /* Do nothing */
	                        }
	                    }
	                    
	                    return Status.OK_STATUS;
	                } catch(Exception e) {
	                    return Status.OK_STATUS;
	                }
	            }
	        };
	        job.addJobChangeListener(new JobChangeAdapter() {
	           <commit_msg>zeige fortschritt beim indizieren in progress-view<commit_after>package org.eclipselabs.recommenders.codesearchquery.rcp.popup.actions;

import java.io.File;
import java.util.List;

import org.apache.lucene.store.SimpleFSDirectory;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipselabs.recommenders.codesearchquery.rcp.Activator;
import org.eclipselabs.recommenders.codesearchquery.rcp.indexer.CodeIndexerIndex;

import com.google.common.collect.Lists;

public class IndexAction implements IObjectActionDelegate {

    //private Shell shell;
    private List<IProject> projects = null;

    /**
     * Constructor for Action1.
     */
    public IndexAction() {
        super();
    }

    /**
     * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
     */
    @Override
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        //shell = targetPart.getSite().getShell();
    }

    /**
     * @see IActionDelegate#run(IAction)
     */
    @Override
    public void run(IAction action) {
    	try {
	        final Long start = System.currentTimeMillis();
	        String path = Platform.getLocation().toString() + "/index.l";
	        final CodeIndexerIndex index = new CodeIndexerIndex(new SimpleFSDirectory(new File(path)));
	                
	        final WorkspaceJob job = new WorkspaceJob("Indexing sources...") {
	            
	            @Override
	            public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreException {
	   
	                try {
	                    index.printStats();
	                    monitor.beginTask("Indexing source files", projects.size());
	                    
	                    for (IProject p : projects) {
	                        try {
	                            
	                            IPackageFragment[] packages = JavaCore.create(p).getPackageFragments();
	        
	                            for (IPackageFragment mypackage : packages) {
	                                if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE) {
	                                    for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
	                                        // Now create the AST for the ICompilationUnits
	                                        try {
	                                            Activator.logInfo("Indexing ICompilationUnit %1$s ...", unit.getPath());
	                                            
	                                            CompilationUnit cu = parse(unit);
	                                            
	                                            index.index(cu);
	                                        }catch(Exception e) {
	                                            Activator.logWarning(e, "ICompilationUnit %1$s has not been indexed successfully", unit.getPath());
	                                        }
	                                    }
	                                }
	                            }
	                        } catch(Exception e) {
	                            /* Do nothing */
	                        }
	                        
	                        monitor.worked(1);
	                    }
	                    
	                    return Status.OK_STATUS;
	                } catch(Exception e) {
	                    return Status.OK_STATUS;
	                }
	            }
	        };
	        job.addJobChangeListener(new JobChangeAdapter() {
	           
{'fraction_non_alphanumeric': 0.08633011911514464, 'fraction_numerical': 0.0007941009642654566, 'mean_word_length': 1.5800409716125257, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '426286', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2105, 'n_words': 384}