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

📄 pythonnature.java

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    try {
						astManager = (ICodeCompletionASTManager) ASTManager.loadFromFile(getAstOutputFile());
						if (astManager != null) {
							synchronized (astManager) {
								astManager.setProject(getProject(), true); // this is the project related to it, restore the deltas (we may have some crash)

								//just a little validation so that we restore the needed info if we did not get the modules
								if (astManager.getModulesManager().getOnlyDirectModules().length < 5) {
									astManager = null;
								}

								if (astManager != null) {
									List<IInterpreterObserver> participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_INTERPRETER_OBSERVER);
									for (IInterpreterObserver observer : participants) {
										try {
											observer.notifyNatureRecreated(nature, jobProgressComunicator);
										} catch (Exception e) {
											//let's not fail because of other plugins
											PydevPlugin.log(e);
										}
									}
								}
							}
						}
					} catch (Exception e) {
						PydevPlugin.log(e);
						astManager = null;
					}
                    
                    //errors can happen when restoring it
                    if(astManager == null){
                        try {
                            rebuildPath();
                        } catch (Exception e) {
                            PydevPlugin.log(e);
                        }
                    }
                    initializationFinished = true;
                    jobProgressComunicator.done();
                    return Status.OK_STATUS;
                }
            };
            
            codeCompletionLoadJob.schedule();
                        
        }
    }


	/**
     * Returns the directory that should store completions.
     * 
     * @param p
     * @return
     */
    public static File getCompletionsCacheDir(IProject p) {
        IPath location = p.getWorkingLocation(PydevPlugin.getPluginID());
        IPath path = location;
    
        File file = new File(path.toOSString());
        return file;
    }
    
    
    public File getCompletionsCacheDir() {
        return getCompletionsCacheDir(getProject());
    }

    /**
     * @param dir: parent directory where file should be.
     * @return the file where the python path helper should be saved.
     */
    private File getAstOutputFile() {
        return new File(getCompletionsCacheDir(), "asthelper.completions");
    }

    /**
     * Can be called to refresh internal info (or after changing the path in the preferences).
     */
    public void rebuildPath() {
		this.rebuildPath(null, new NullProgressMonitor());
    }
    
    
	public void rebuildPath(String defaultSelectedInterpreter, IProgressMonitor monitor) {
		try {
			clearCaches();
			String paths = this.pythonPathNature.getOnlyProjectPythonPathStr();
			this.rebuildPath(defaultSelectedInterpreter, paths);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}


    private volatile Job rebuildJob;
    
    /**
     * This method is called whenever the pythonpath for the project with this nature is changed. 
     */
    private synchronized void rebuildPath(final String defaultSelectedInterpreter, final String paths) {
        if(rebuildJob != null){
            return;//already in rebuild
        }
        final PythonNature nature = this;
        rebuildJob = new RebuildPythonNatureModules("Pydev code completion: rebuilding modules", paths, defaultSelectedInterpreter, nature);
        rebuildJob.schedule();
    }
    
    /**
     * @return Returns the completionsCache.
     */
    public ICodeCompletionASTManager getAstManager() {
        if(astManager == null){
        	//this is needed because it may not be restarted already...
        	//also, this will only happen when initializing eclipse with some editors already open
        	
        	for(int i=0; i<10 && astManager == null && !initializationFinished; i++){ //we will wait 10 seconds for it
        		try {
					Thread.sleep(1000);
        		} catch (InterruptedException e) {
                    //ignore
				} catch (Exception e) {
					e.printStackTrace();
				}
        	}
        	//next time we won't wait as long.
        	initializationFinished = true;
        }

        return astManager;
    }
    
    public void setAstManager(ICodeCompletionASTManager astManager){
        this.astManager = astManager;
    }

    public IPythonPathNature getPythonPathNature() {
        return pythonPathNature;
    }
    
    public static IPythonPathNature getPythonPathNature(IProject project) {
        PythonNature pythonNature = getPythonNature(project);
        if(pythonNature != null){
            return pythonNature.pythonPathNature;
        }
        return null;
    }

    /**
     * @return all the python natures available in the workspace 
     */
    public static List<IPythonNature> getAllPythonNatures() {
    	List<IPythonNature> natures = new ArrayList<IPythonNature>();
    	IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    	IProject[] projects = root.getProjects();
    	for (IProject project : projects) {
			PythonNature nature = getPythonNature(project);
			if(nature != null){
				natures.add(nature);
			}
		}
    	return natures;
    }
    
    public static PythonNature getPythonNature(IResource resource) {
    	if(resource == null){
    		return null;
    	}
    	return getPythonNature(resource.getProject());
    	
    }
    /**
     * @param project the project we want to know about (if it is null, null is returned)
     * @return the python nature for a project (or null if it does not exist for the project)
     */
    public static PythonNature getPythonNature(IProject project) {
        if(project != null && project.isOpen()){
            try {
                IProjectNature n = project.getNature(PYTHON_NATURE_ID);
                if(n instanceof PythonNature){
                    return (PythonNature) n;
                }
            } catch (CoreException e) {
                PydevPlugin.logInfo(e);
            }
        }
        return null;
    }

    /**
     * Stores the version as a cache (the actual version is set in the xml file).
     * This is so that we don't have a runtime penalty for it.
     */
    private String versionPropertyCache = null;
    
    /**
     * Returns the Python version of the Project. 
     * because it might have changed on disk (e.g. a repository update).
     * @return the python version for the project
     * @throws CoreException 
     */
    public String getVersion() throws CoreException {
        if(project != null){
        	if (versionPropertyCache == null) {
        	    String storeVersion = getStore().getPropertyFromXml(getPythonProjectVersionQualifiedName());
	            if(storeVersion == null){ //there is no such property set (let's set it to the default)
	                setVersion(getDefaultVersion()); //will set the versionPropertyCache too
	            }else{
	                versionPropertyCache = storeVersion;   
                }
        	} 
        }
        return versionPropertyCache;
    }
    
    /**
     * set the project version given the constants provided
     * @throws CoreException 
     */
    public void setVersion(String version) throws CoreException{
		clearCaches();
        if(project != null){
        	this.versionPropertyCache = version;
            getStore().setPropertyToXml(getPythonProjectVersionQualifiedName(), version, true);
        }
    }

    public String getDefaultVersion(){
        return PYTHON_VERSION_2_4;
    }

    
    public boolean isJython() throws CoreException {
        if(isJython == null){
            isJython = getVersion().equals(JYTHON_VERSION_2_1);
        }
        return isJython;
    }

    public boolean isPython() throws CoreException {
        return !isJython();
    }
    
    public boolean acceptsDecorators() throws CoreException {
        return getVersion().equals(PYTHON_VERSION_2_4);
    }
    
    public void saveAstManager() {
    	if(astManager == null){
    		REF.writeToFile(null, getAstOutputFile());
    		
    	}else{
	    	synchronized(astManager){
	    		REF.writeToFile(astManager, getAstOutputFile());
	    	}
    	}
    }

    public int getRelatedId() throws CoreException {
    	return getRelatedId(this);
    }
    
    public static int getRelatedId(IPythonNature nature) throws CoreException {
        if(nature.isPython()){
            return PYTHON_RELATED;
        }else if(nature.isJython()){
            return JYTHON_RELATED;
        }
        throw new RuntimeException("Unable to get the id to which this nature is related");
    }


    /**
     * Resolve the module given the absolute path of the file in the filesystem.
     * 
     * @param fileAbsolutePath the absolute file path
     * @return the module name
     */
    public String resolveModule(String fileAbsolutePath) {
        String moduleName = null;
        
        if(astManager != null){
            moduleName = astManager.getModulesManager().resolveModule(fileAbsolutePath);
        }
        return moduleName;
        
    }
    
    public static String[] getStrAsStrItems(String str){
        return str.split("\\|");
    }

    public IInterpreterManager getRelatedInterpreterManager() {
        try {
            if (isPython()) {
                return PydevPlugin.getPythonInterpreterManager();
            } else if (isJython()) {
                return PydevPlugin.getJythonInterpreterManager();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        throw new RuntimeException("Unable to find the related interpreter manager.");
        
    }

    
    // ------------------------------------------------------------------------------------------ LOCAL CACHES
    public void clearCaches() {
        this.isJython = null;
        this.versionPropertyCache = null;
    }
    
    Boolean isJython = null; //cache
    
	public void setBuiltinCompletions(IToken[] comps) {
		this.getRelatedInterpreterManager().setBuiltinCompletions(comps);
	}

	public IToken[] getBuiltinCompletions() {
		return this.getRelatedInterpreterManager().getBuiltinCompletions();
	}

	public IModule getBuiltinMod() {
		return this.getRelatedInterpreterManager().getBuiltinMod();
	}

	public void setBuiltinMod(IModule mod) {
		this.getRelatedInterpreterManager().setBuiltinMod(mod);
	}

    public static List<IPythonNature> getPythonNaturesRelatedTo(int relatedTo) {
        ArrayList<IPythonNature> ret = new ArrayList<IPythonNature>();
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IProject[] projects = root.getProjects();
        for (IProject project : projects) {
            PythonNature nature = getPythonNature(project);
            try {
                if(nature != null){
                    if(nature.getRelatedId() == relatedTo){
                        ret.add(nature);
                    }
                }
            } catch (CoreException e) {
                throw new RuntimeException(e);
            }
        }
        
        return ret;
    }

    /**
     * @return the version of the grammar as defined in IPythonNature.GRAMMAR_PYTHON...
     */
    public int getGrammarVersion() {
        try {
            if(getVersion().equals(PYTHON_VERSION_2_5)){
                return GRAMMAR_PYTHON_VERSION_2_5;
            }else{
                return GRAMMAR_PYTHON_VERSION_2_4;
            }
        } catch (CoreException e) {
            throw new RuntimeException(e);
        }
    }
    
    protected IPythonNatureStore getStore(){
    	return pythonNatureStore;
    }


}


⌨️ 快捷键说明

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