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

📄 pydevplugin.java

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        log(IStatus.ERROR, e.getMessage() != null ? e.getMessage() : "No message gotten.", e, printToConsole);
    }

    public static void logInfo(String msg) {
        IStatus s = PydevPlugin.makeStatus(IStatus.INFO, msg, null);
        PydevPlugin plug = getDefault();
        if(plug == null){//testing mode
            System.out.println(msg);
        }else{
            plug.getLog().log(s);
        }
    }
    
    public static CoreException log(String msg) {
        IStatus s = PydevPlugin.makeStatus(IStatus.ERROR, msg, new RuntimeException(msg));
        CoreException e = new CoreException(s);
        PydevPlugin.log(e);
        return e;
    }

    public static IEditorPart doOpenEditor(IFile f, boolean activate) {
    	if (f == null)
    		return null;
    	
    	try {
    		FileEditorInput file = new FileEditorInput(f);
    		return openEditorInput(file);
    		
    	} catch (Exception e) {
    		log(IStatus.ERROR, "Unexpected error opening path " + f.toString(), e);
    		return null;
    	}
    }
    
    /**
     * Utility function that opens an editor on a given path.
     * 
     * @return part that is the editor
     */
    public static IEditorPart doOpenEditor(IPath path, boolean activate) {
        if (path == null)
            return null;

        try {
    		IEditorInput file = createEditorInput(path);
	        return openEditorInput(file);
            
        } catch (Exception e) {
            log(IStatus.ERROR, "Unexpected error opening path " + path.toString(), e);
            return null;
        }
    }
    
    
	private static IEditorPart openEditorInput(IEditorInput file) throws PartInitException {
		final IWorkbench workbench = plugin.getWorkbench();
		if(workbench == null){
			throw new RuntimeException("workbench cannot be null");
		}

		IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
		if(activeWorkbenchWindow == null){
			throw new RuntimeException("activeWorkbenchWindow cannot be null (we have to be in a ui thread for this to work)");
		}
		
		IWorkbenchPage wp = activeWorkbenchWindow.getActivePage();
      
		// File is inside the workspace
		return IDE.openEditor(wp, file, PyEdit.EDITOR_ID);
	}

    
    
    

//  =====================
//  ===================== ALL BELOW IS COPIED FROM org.eclipse.ui.internal.editors.text.OpenExternalFileAction
//  =====================

    public static IEditorInput createEditorInput(IPath path) {
        return createEditorInput(path, true);
    }
    /**
     * @param path
     * @return
     */
    private static IEditorInput createEditorInput(IPath path, boolean askIfDoesNotExist) {
        IEditorInput edInput = null;
        IWorkspace w = ResourcesPlugin.getWorkspace();      

        //let's start with the 'easy' way
    	IFile fileForLocation = w.getRoot().getFileForLocation(path);
    	if(fileForLocation != null){
    		return new FileEditorInput(fileForLocation);
    	}

        
        
        IFile files[] = w.getRoot().findFilesForLocation(path);
        if (files == null  || files.length == 0 || !files[0].exists()){
            //it is probably an external file
            File systemFile = path.toFile();
            if(systemFile.exists()){
                edInput = createEditorInput(systemFile);
                
            }else if(askIfDoesNotExist){
                //this is the last resort... First we'll try to check for a 'good' match,
                //and if there's more than one we'll ask it to the user
                List<IFile> likelyFiles = getLikelyFiles(path, w);
                IFile iFile = selectWorkspaceFile(likelyFiles.toArray(new IFile[0]));
                if(iFile != null){
                    return new FileEditorInput(iFile);
                }
                
                //ok, ask the user for any file in the computer
                IEditorInput input = selectFilesystemFileForPath(path);
                if(input != null){
                    return input;
                }
            }
        }else{ //file exists
            edInput = doFileEditorInput(selectWorkspaceFile(files));
        }
        return edInput;
    }

    private static IEditorInput doFileEditorInput(IFile file) {
        if(file == null){
            return null;
        }
        return new FileEditorInput(file);
    }
    /**
     * This is the last resort... pointing to some filesystem file to get the editor for some path.
     */
    private static IEditorInput selectFilesystemFileForPath(final IPath path) {
        final List<String> l = new ArrayList<String>();
        Runnable r = new Runnable(){

            public void run() {
                Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
                FileDialog dialog = new FileDialog(shell);
                dialog.setText(path+" - select correspondent filesystem file.");
                dialog.setFilterExtensions(new String[]{"*.py; *.pyw"});
                String string = dialog.open();
                if(string != null){
                    l.add(string);
                }
            }
        };
        if(Display.getCurrent() == null){ //not ui-thread
            Display.getDefault().syncExec(r);
        }else{
            r.run();
        }
        if(l.size() > 0){
            String fileAbsolutePath = REF.getFileAbsolutePath(l.get(0));
            return new PydevFileEditorInput(new File(fileAbsolutePath));
        }
        return null;
    }
    
    /**
     * This method will pass all the files in the workspace and check if there's a file that might
     * be a match to some path (use only as an almost 'last-resort').
     */
    private static List<IFile> getLikelyFiles(IPath path, IWorkspace w) {
        List<IFile> ret = new ArrayList<IFile>();
        try {
            IResource[] resources = w.getRoot().members();
            getLikelyFiles(path, ret, resources);
        } catch (CoreException e) {
            Log.log(e);
        }
        return ret;
    }
    
    /**
     * Used to recursively get the likely files given the first set of containers
     */
    private static void getLikelyFiles(IPath path, List<IFile> ret, IResource[] resources) throws CoreException {
        String strPath = path.removeFileExtension().lastSegment().toLowerCase(); //this will return something as 'foo'
        
        for (IResource resource : resources) {
            if(resource instanceof IFile){
                IFile f = (IFile) resource;
                
                if(PythonPathHelper.isValidSourceFile(f)){
                    if(resource.getFullPath().removeFileExtension().lastSegment().toLowerCase().equals(strPath)){ 
                        ret.add((IFile) resource);
                    }
                }
            }else if(resource instanceof IContainer){
                getLikelyFiles(path, ret, ((IContainer)resource).members());
            }
        }
    }
    
    private static IEditorInput createEditorInput(File file) {
        IFile[] workspaceFile= getWorkspaceFiles(file);
        if (workspaceFile != null && workspaceFile.length > 0){
            IFile file2 = selectWorkspaceFile(workspaceFile);
            if(file2 != null){
                return new FileEditorInput(file2);
            }else{
                return new FileEditorInput(workspaceFile[0]);
            }
        }
        return new PydevFileEditorInput(file);
    }

    
    public static IFile getWorkspaceFile(File file) {
        IFile[] files = getWorkspaceFiles(file);
        return selectWorkspaceFile(files);
    }
    
    public static IFile[] getWorkspaceFiles(File file) {
        IWorkspace workspace= ResourcesPlugin.getWorkspace();
        IPath location= Path.fromOSString(file.getAbsolutePath());
        IFile[] files= workspace.getRoot().findFilesForLocation(location);
        files= filterNonExistentFiles(files);
        if (files == null || files.length == 0){
            return null;
        }
        
        return files;
    }
    

    private static IFile[] filterNonExistentFiles(IFile[] files){
        if (files == null)
            return null;

        int length= files.length;
        ArrayList<IFile> existentFiles= new ArrayList<IFile>(length);
        for (int i= 0; i < length; i++) {
            if (files[i].exists())
                existentFiles.add(files[i]);
        }
        return (IFile[])existentFiles.toArray(new IFile[existentFiles.size()]);
    }
    
    private static IFile selectWorkspaceFile(final IFile[] files) {
        if(files == null || files.length == 0){
            return null;
        }
        if(files.length == 1){
            return files[0];
        }
        final List<IFile> selected = new ArrayList<IFile>();
        
        Runnable r = new Runnable(){
            public void run() {
                Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
                ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new PyFileLabelProvider());
                dialog.setElements(files);
                dialog.setTitle("Select Workspace File");
                dialog.setMessage("File may be matched to multiple files in the workspace.");
                if (dialog.open() == Window.OK){
                    selected.add((IFile) dialog.getFirstResult());
                }
            }
            
        };
        if(Display.getCurrent() == null){ //not ui-thread
            Display.getDefault().syncExec(r);
        }else{
            r.run();
        }
        if(selected.size() > 0){
            return selected.get(0);
        }
        return null;
    }

    
//  =====================
//  ===================== END COPY FROM org.eclipse.ui.internal.editors.text.OpenExternalFileAction
//  =====================


    /**
	 * Returns this plug-in's template store.
	 * 
	 * @return the template store of this plug-in instance
	 */
    public TemplateStore getTemplateStore() {
        if (fStore == null) {
            fStore = new ContributionTemplateStore(getContextTypeRegistry(), getPreferenceStore(), CUSTOM_TEMPLATES_PY_KEY);
            try {
                fStore.load();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return fStore;
    }

    /**
     * Returns this plug-in's context type registry.
     * 
     * @return the context type registry for this plug-in instance
     */
    public ContextTypeRegistry getContextTypeRegistry() {
        if (fRegistry == null) {
            // create an configure the contexts available in the template editor
            fRegistry = new ContributionContextTypeRegistry();
            fRegistry.addContextType(PyContextType.PY_CONTEXT_TYPE);
        }
        return fRegistry;
    }

    
    
    /**
     * 
     * @return the script to get the variables.
     * 
     * @throws CoreException
     */
    public static File getScriptWithinPySrc(String targetExec) throws CoreException {
        IPath relative = new Path("PySrc").addTrailingSeparator().append(targetExec);
        return getRelativePath(relative);
    }

    /**
     * @param relative
     * @return
     * @throws CoreException
     */
    public static File getRelativePath(IPath relative) throws CoreException {
        return PydevPlugin.getBundleInfo().getRelativePath(relative);
    }
    
    public static ImageCache getImageCache(){
        return PydevPlugin.getBundleInfo().getImageCache();
    }
    

    public static File getImageWithinIcons(String icon) throws CoreException {

        IPath relative = new Path("icons").addTrailingSeparator().append(icon);

        return getRelativePath(relative);

    }

    /**
     * @return All the IFiles below the current folder that are python files (does not check if it has an __init__ path)
     */
    public static List<IFile> getAllIFilesBelow(IFolder member) {
    	final ArrayList<IFile> ret = new ArrayList<IFile>();

⌨️ 快捷键说明

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