<commit_before>package com.python.pydev.analysis;

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

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.python.pydev.core.ICodeCompletionASTManager;
import org.python.pydev.core.ICompletionCache;
import org.python.pydev.core.IDefinition;
import org.python.pydev.core.IModule;
import org.python.pydev.core.IPythonNature;
import org.python.pydev.core.bundle.ImageCache;
import org.python.pydev.editor.codecompletion.revisited.CompletionStateFactory;
import org.python.pydev.editor.codecompletion.revisited.visitors.Definition;
import org.python.pydev.editor.model.ItemPointer;
import org.python.pydev.editor.model.Location;
import org.python.pydev.ui.UIConstants;

import com.python.pydev.PydevPlugin;
import com.python.pydev.analysis.additionalinfo.IInfo;

/**
 * The main plugin class to be used in the desktop.
 */
public class AnalysisPlugin extends AbstractUIPlugin {

	//The shared instance.
	private static AnalysisPlugin plugin;
    private static ImageCache imageCache;
	
	/**
	 * The constructor.
	 */
	public AnalysisPlugin() {
		plugin = this;
	}

	/**
	 * This method is called upon plug-in activation
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		PydevPlugin.getDefault().checkValid();
	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
	}


	/**
     * @param pointers the list where the pointers will be added
     * @param manager the manager to be used to get the definition
     * @param nature the nature to be used
     * @param info the info that we are looking for
     */
    public static void getDefinitionFromIInfo(List<ItemPointer> pointers, ICodeCompletionASTManager manager, IPythonNature nature, 
            IInfo info, ICompletionCache completionCache) {
        IModule mod;
        String tok;
        mod = manager.getModule(info.getDeclaringModuleName(), nature, true);
        if(mod != null){
            //ok, now that we found the module, we have to get the actual definition
            tok = "";
            String path = info.getPath();
            
            if(path != null && path.length() > 0){
                tok = path+".";
            }
            tok += info.getName();
            try {
                IDefinition[] definitions = mod.findDefinition(
                        CompletionStateFactory.getEmptyCompletionState(tok, nature, completionCache), -1, -1, nature);

                if((definitions == null || definitions.length == 0) && path != null && path.length() > 0){
                    //this can happen if we have something as an attribute in the path:
                    
                    //class Bar(object):
                    //    def __init__(self):
                    //        self.xxx = 10
                    //
                    //so, we'de get a find definition for Bar.__init__.xxx which is something we won't find
                    //for now, let's simply return a match in the correct context (although the correct way of doing
                    //it would be analyzing that context to find the match)
                    definitions = mod.findDefinition(
                            CompletionStateFactory.getEmptyCompletionState(path, nature, completionCache), -1, -1, nature);
                    
                }
                getAsPointers(pointers, (Definition[]) definitions);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * @param pointers: OUT: list where the pointers will be added
     * @param definitions the definitions that will be gotten as pointers
     */
    public static void getAsPointers(List<ItemPointer> pointers, Definition[] definitions) {
        for (Definition definition : definitions) {
            File file = definition.module.getFile();
            int line = definition.line;
            int col = definition.col;
            
            pointers.add(new ItemPointer(file,
                    new Location(line-1, col-1),
                    new Location(line-1, col-1), 
                    definition,
                    definition.module.getZipFilePath())
                    );
        }
    }

    /**
	 * Returns the shared instance.
	 */
	public static AnalysisPlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path.
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin("com.python.pydev.analysis", path);
	}


    public static ImageCache getImageCache() {
        if(plugin != null){
            if (imageCache == null) {
                imageCache = new ImageCache(plugin.getBundle().getEntry("/"));
            }
            return imageCache;
        }else{
            // we don't have it on tests
            org.python.pydev.plugin.PydevPlugin.log("unable to get image cache");
            
            //return one that always return null
            return new ImageCache(){
                @Override
                public Image get(String key) {
                    return null;
                }
            };
        }
    }
    
    public static final String CLASS_WITH_IMPORT_ICON = "icons/class_obj_imp.gif";
    public static final String METHOD_WITH_IMPORT_ICON = "icons/method_obj_imp.gif";
    public static final String ATTR_WITH_IMPORT_ICON = "icons/attr_obj_imp.gif";
    
    public static Image getImageForAutoImportTypeInfo(IInfo info){
        ImageCache imageCache = getImageCache();
        switch(info.getType()){
            case IInfo.CLASS_WITH_IMPORT_TYPE:
                return imageCache.get(AnalysisPlugin.CLASS_WITH_IMPORT_ICON); 
            case IInfo.METHOD_WITH_IMPORT_TYPE:
                return imageCache.get(AnalysisPlugin.METHOD_WITH_IMPORT_ICON);
            case IInfo.ATTRIBUTE_WITH_IMPORT_TYPE:
                return imageCache.get(AnalysisPlugin.ATTR_WITH_IMPORT_ICON);
            default:                  
                throw new RuntimeException("Undefined type.");
<commit_msg>Fixed problem while creating jython shell.<commit_after>package com.python.pydev.analysis;

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

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.python.pydev.core.ICodeCompletionASTManager;
import org.python.pydev.core.ICompletionCache;
import org.python.pydev.core.IDefinition;
import org.python.pydev.core.IModule;
import org.python.pydev.core.IPythonNature;
import org.python.pydev.core.bundle.ImageCache;
import org.python.pydev.editor.codecompletion.revisited.CompletionStateFactory;
import org.python.pydev.editor.codecompletion.revisited.visitors.Definition;
import org.python.pydev.editor.model.ItemPointer;
import org.python.pydev.editor.model.Location;
import org.python.pydev.ui.UIConstants;

import com.python.pydev.PydevPlugin;
import com.python.pydev.analysis.additionalinfo.IInfo;

/**
 * The main plugin class to be used in the desktop.
 */
public class AnalysisPlugin extends AbstractUIPlugin {

	//The shared instance.
	private static AnalysisPlugin plugin;
    private static ImageCache imageCache;
	
	/**
	 * The constructor.
	 */
	public AnalysisPlugin() {
		plugin = this;
	}

	/**
	 * This method is called upon plug-in activation
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		PydevPlugin.getDefault().checkValid();
	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
	}


	/**
     * @param pointers the list where the pointers will be added
     * @param manager the manager to be used to get the definition
     * @param nature the nature to be used
     * @param info the info that we are looking for
     */
    public static void getDefinitionFromIInfo(List<ItemPointer> pointers, ICodeCompletionASTManager manager, IPythonNature nature, 
            IInfo info, ICompletionCache completionCache) {
        IModule mod;
        String tok;
        mod = manager.getModule(info.getDeclaringModuleName(), nature, true);
        if(mod != null){
            //ok, now that we found the module, we have to get the actual definition
            tok = "";
            String path = info.getPath();
            
            if(path != null && path.length() > 0){
                tok = path+".";
            }
            tok += info.getName();
            try {
                IDefinition[] definitions = mod.findDefinition(
                        CompletionStateFactory.getEmptyCompletionState(tok, nature, completionCache), -1, -1, nature);

                if((definitions == null || definitions.length == 0) && path != null && path.length() > 0){
                    //this can happen if we have something as an attribute in the path:
                    
                    //class Bar(object):
                    //    def __init__(self):
                    //        self.xxx = 10
                    //
                    //so, we'de get a find definition for Bar.__init__.xxx which is something we won't find
                    //for now, let's simply return a match in the correct context (although the correct way of doing
                    //it would be analyzing that context to find the match)
                    definitions = mod.findDefinition(
                            CompletionStateFactory.getEmptyCompletionState(path, nature, completionCache), -1, -1, nature);
                    
                }
                getAsPointers(pointers, (Definition[]) definitions);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * @param pointers: OUT: list where the pointers will be added
     * @param definitions the definitions that will be gotten as pointers
     */
    public static void getAsPointers(List<ItemPointer> pointers, Definition[] definitions) {
        for (Definition definition : definitions) {
            File file = definition.module.getFile();
            int line = definition.line;
            int col = definition.col;
            
            pointers.add(new ItemPointer(file,
                    new Location(line-1, col-1),
                    new Location(line-1, col-1), 
                    definition,
                    definition.module.getZipFilePath())
                    );
        }
    }

    /**
	 * Returns the shared instance.
	 */
	public static AnalysisPlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path.
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin("com.python.pydev.analysis", path);
	}


    public static ImageCache getImageCache() {
        if (imageCache != null) {
            return imageCache;
        }
            
        if(plugin != null){
            imageCache = new ImageCache(plugin.getBundle().getEntry("/"));
        }else{
            // we don't have it on tests
            org.python.pydev.plugin.PydevPlugin.log("unable to get image cache");
            
            //return one that always return null
            imageCache = new ImageCache(){
                @Override
                public Image get(String key) {
                    return null;
                }
            };
        }
        return imageCache;
    }
    
    public static final String CLASS_WITH_IMPORT_ICON = "icons/class_obj_imp.gif";
    public static final String METHOD_WITH_IMPORT_ICON = "icons/method_obj_imp.gif";
    public static final String ATTR_WITH_IMPORT_ICON = "icons/attr_obj_imp.gif";
    
    public static Image getImageForAutoImportTypeInfo(IInfo info){
        ImageCache imageCache = getImageCache();
        switch(info.getType()){
            case IInfo.CLASS_WITH_IMPORT_TYPE:
                return imageCache.get(AnalysisPlugin.CLASS_WITH_IMPORT_ICON); 
            case IInfo.METHOD_WITH_IMPORT_TYPE:
                return imageCache.get(AnalysisPlugin.METHOD_WITH_IMPORT_ICON);
            case IInfo.ATTRIBUTE_WITH_IMPORT_TYPE:
                return imageCache.get(AnalysisPlugin.ATTR_WITH_IMPORT_ICON);
            default:                  
                throw new RuntimeException("Undefined type.");

{'fraction_non_alphanumeric': 0.08922700512271581, 'fraction_numerical': 0.0019879195657160334, 'mean_word_length': 2.183256266731565, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '681482', 'n_tokens_mistral': 3491, 'n_tokens_neox': 3341, 'n_words': 939}