⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codecompletiontestsbase.java

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on Mar 9, 2005
 *
 * @author Fabio Zadrozny
 */
package org.python.pydev.editor.codecompletion.revisited;

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

import junit.framework.TestCase;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.python.pydev.core.IInterpreterManager;
import org.python.pydev.core.IPythonNature;
import org.python.pydev.core.REF;
import org.python.pydev.core.TestDependent;
import org.python.pydev.editor.codecompletion.CompletionRequest;
import org.python.pydev.editor.codecompletion.IPyCodeCompletion;
import org.python.pydev.editor.codecompletion.PyCodeCompletionUtils;
import org.python.pydev.plugin.PydevPlugin;
import org.python.pydev.plugin.nature.PythonNature;
import org.python.pydev.ui.BundleInfoStub;
import org.python.pydev.ui.pythonpathconf.InterpreterInfo;
import org.python.pydev.utils.PrintProgressMonitor;

/**
 * @author Fabio Zadrozny
 */
public class CodeCompletionTestsBase extends TestCase {

    public CodeCompletionTestsBase() {
        
    }
    
    public CodeCompletionTestsBase(String name) {
        super(name);
    }

    public static void main(String[] args) {
        //for single setup / teardown, check http://www.beust.com/weblog/archives/000082.html
        //(may be useful to get rid of the ThreadStreamReader threads)
        junit.textui.TestRunner.run(CodeCompletionTestsBase.class);
    }

    /**
     * We want to keep it initialized among runs from the same class.
     * Check the restorePythonPath function.
     */
	public static PythonNature nature;
	
	/**
	 * Nature for the second project. 
     * 
     * This nature has the other nature as a dependency.
	 */
	public static PythonNature nature2;
	
    /**
     * A map with the name of the project pointing to the last class that restored the
     * nature. This is done in this way because we don't want the nature to be recreated
     * all the time among tests from the same test case.
     */
	public static Map<String, Class> restoredClass = new HashMap<String, Class>();
	
    /**
     * Serves the same purpose that the restoredClass serves, but for the system 
     * python nature.
     */
	public static Class restoredSystem;
	public Preferences preferences;

	protected boolean ADD_MX_TO_FORCED_BUILTINS = true;
    
    /**
     * Whether we want to debug problems in this class.
     */
    protected static boolean DEBUG_TESTS_BASE = false;

	/*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        PydevPlugin.setBundleInfo(new BundleInfoStub());
        preferences = new Preferences();
        ProjectModulesManager.IN_TESTS = true;
        REF.IN_TESTS = true;
    }
    
    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();
        PydevPlugin.setBundleInfo(null);
        ProjectModulesManager.IN_TESTS = false;
        REF.IN_TESTS = false;
    }
    
    /**
     * Backwards-compatibility interface
     */
    protected boolean restoreProjectPythonPath(boolean force, String path){
        return restoreProjectPythonPath(force, path, "testProjectStub");
    }
    
    /**
     * Backwards-compatibility interface
     */
    protected boolean restoreProjectPythonPath2(boolean force, String path){
        return restoreProjectPythonPath2(force, path, "testProjectStub2");
    }
    
    /**
     * A method that creates the default nature
     * 
     * @param force whether the creation of the new nature should be forced
     * @param path the pythonpath for the new nature
     * @param name the name for the project
     * @return true if the creation was needed and false if it wasn't
     */
    protected boolean restoreProjectPythonPath(boolean force, String path, String name){
        PythonNature n = checkNewNature(name, force);
        if(n != null){
            nature = n;
            ProjectStub projectStub = new ProjectStub(name, path, new IProject[0], new IProject[0]);
            
            setAstManager(path, projectStub, nature);
            return true;
        }
        return false;
    }
    
    /**
     * A method that creates a project that references the project from the 'default' nature
     * (and adds itself as a reference in the other project). 
     * 
     * @param force whether the creation of the new nature should be forced
     * @param path the pythonpath for the new nature
     * @param name the name for the project
     * @return true if the creation was needed and false if it wasn't
     */
    protected boolean restoreProjectPythonPath2(boolean force, String path, String name){
        PythonNature n = checkNewNature(name, force);
        if(n != null){
    		nature2 = n;
            
    		ProjectStub projectFromNature1 = (ProjectStub) nature.getProject();
            //create a new project referencing the first one
            ProjectStub projectFromNature2 = new ProjectStub(name, path, new IProject[]{projectFromNature1}, new IProject[0]);
            
            //as we're adding a reference, we also have to set the referencing...
            projectFromNature1.referencingProjects = new IProject[]{projectFromNature2};
            
			setAstManager(path, projectFromNature2, nature2);
    		return true;
    	}
    	return false;
    }
    
    /**
     * Checks if we have to create a new nature for the given name
     * 
     * @param name the name of the project to be checked for the creation of the nature
     * @param force whether the creation of the new nature should be forced
     * @return the PythonNature created (if needed) or null if the creation was not needed
     */
    protected PythonNature checkNewNature(String name, boolean force){
        Class restored = CodeCompletionTestsBase.restoredClass.get(name);
        if(restored == null || restored != this.getClass() || force){
            //cache
            CodeCompletionTestsBase.restoredClass.put(name, this.getClass());
            return createNature();
        }        
        return null;
    }

    /**
     * This method sets the ast manager for a nature and restores the pythonpath
     * with the path passed
     * @param path the pythonpath that shoulb be set for this nature
     * @param projectStub the project where the nature should be set
     * @param pNature the nature we're interested in
     */
    protected void setAstManager(String path, ProjectStub projectStub, PythonNature pNature) {
        pNature.setProject(projectStub); //references the project 1
        projectStub.setNature(pNature);
        pNature.setAstManager(new ASTManager());
        
        ASTManager astManager = ((ASTManager)pNature.getAstManager());
        astManager.setNature(pNature);
        astManager.setProject(projectStub, false);
        astManager.changePythonPath(path, projectStub, getProgressMonitor(),null);
    }

    /**
     * @return the pydev interpreter manager we are testing
     */
    protected IInterpreterManager getInterpreterManager() {
        return PydevPlugin.getPythonInterpreterManager();
    }

    /**
     * @return a PythonNature that is regarded as a python nature with the latest grammar.
     */
    public static PythonNature createStaticNature() {
        return new PythonNature(){
            @Override
            public boolean isJython() throws CoreException {
                return false;
            }
            @Override
            public boolean isPython() throws CoreException {
                return true;
            }
            @Override
            public int getGrammarVersion() {
                return IPythonNature.LATEST_GRAMMAR_VERSION;
            }
        };
    }
    /**
     * @return a nature that is python-specific
     */
    protected PythonNature createNature() {
        return createStaticNature();
    }
    
    /**
     * @return whether is was actually restored (given the force parameter)
     */
    protected boolean restoreSystemPythonPath(boolean force, String path){
        if(restoredSystem == null || restoredSystem != this.getClass() || force){
            //restore manager and cache
            setInterpreterManager();
            restoredSystem = this.getClass();
            
            //get default and restore the pythonpath
            InterpreterInfo info = getDefaultInterpreterInfo();
            info.restoreCompiledLibs(getProgressMonitor());
            if(ADD_MX_TO_FORCED_BUILTINS){
            	info.addForcedLib("mx");
            }
            info.restorePythonpath(path, getProgressMonitor()); //here

            //postconditions
            afterRestorSystemPythonPath(info);
            return true;
        }
        return false;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -