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

📄 pylintvisitor.java

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * License: Common Public License v1.0
 * Created on Oct 25, 2004
 * 
 * @author Fabio Zadrozny
 */
package org.python.pydev.builder.pylint;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.eclipse.ui.console.MessageConsole;
import org.python.pydev.builder.PyDevBuilderVisitor;
import org.python.pydev.builder.PydevMarkerUtils;
import org.python.pydev.core.REF;
import org.python.pydev.core.Tuple;
import org.python.pydev.plugin.PydevPlugin;
import org.python.pydev.plugin.nature.PythonNature;
import org.python.pydev.runners.SimplePythonRunner;
import org.python.pydev.ui.UIConstants;

/**
 * 
 * Check lint.py for options.
 * 
 * @author Fabio Zadrozny
 */
public class PyLintVisitor extends PyDevBuilderVisitor {

    /* (non-Javadoc)
     * @see org.python.pydev.builder.PyDevBuilderVisitor#visitResource(org.eclipse.core.resources.IResource)
     */
    public static final String PYLINT_PROBLEM_MARKER = "org.python.pydev.pylintproblemmarker";

    public static final List<PyLintThread> pyLintThreads = new ArrayList<PyLintThread>();
    
    private static MessageConsole fConsole;
    
    /**
     * This class runs as a thread to get the markers, and only stops the IDE when the markers are being added.
     * 
     * @author Fabio Zadrozny
     */
    public static class PyLintThread extends Thread{
        
        IResource resource; 
        IDocument document; 
        IPath location;

        List<Object[]> markers = new ArrayList<Object[]>();
        
        public PyLintThread(IResource resource, IDocument document, IPath location){
            setName("PyLint thread");
            this.resource = resource;
            this.document = document;
            this.location = location;
        }
        
        /**
         * @return
         */
        private boolean canPassPyLint() {
            if(pyLintThreads.size() < PyLintPrefPage.getMaxPyLintDelta()){
                pyLintThreads.add(this);
                return true;
            }
            return false;
        }

        /**
         * @see java.lang.Thread#run()
         */
        public void run() {
            try {
                if(canPassPyLint()){
                	
                	IOConsoleOutputStream out=null;
                	try {
                		MessageConsole console = getConsole();
                		if(console != null){
                			out = console.newOutputStream();
                		}
        			} catch (MalformedURLException e3) {
        				throw new RuntimeException(e3);
        			}

	                passPyLint(resource, out);
	                
	                new Job("Adding markers"){
	                
	                    protected IStatus run(IProgressMonitor monitor) {
	                        try {
	                            resource.deleteMarkers(PYLINT_PROBLEM_MARKER, false, IResource.DEPTH_ZERO);
	                        } catch (CoreException e3) {
	                            PydevPlugin.log(e3);
	                        }
	
	                        for (Iterator<Object[]> iter = markers.iterator(); iter.hasNext();) {
	                            Object[] el = iter.next();
	                            
	                            String tok   = (String) el[0];
	                            String type  = (String) el[1];
	                            int priority = ((Integer)el[2]).intValue();
	                            String id    = (String) el[3];
	                            int line     = ((Integer)el[4]).intValue();
	        		            try {
                                    PydevMarkerUtils.createMarker(resource, document, "ID:"+id+" "+tok , 
                                    		line, 0,line, 0,  
                                    		type, priority);
                                } catch (BadLocationException e) {
                                    // ignore (the file may have changed during the time we were analyzing the file)
                                }
	                        }
	
	                        return PydevPlugin.makeStatus(Status.OK, "", null);
	                    }
	                }.schedule();
                }
                
            } catch (final CoreException e) {
                new Job("Error reporting"){
                    protected IStatus run(IProgressMonitor monitor) {
                        PydevPlugin.log(e);
                        return PydevPlugin.makeStatus(Status.OK, "", null);
                    }
                }.schedule();
            }finally{
                try {
                    pyLintThreads.remove(this);
                } catch (Exception e) {
                    PydevPlugin.log(e);
                }
            }
        }

		private MessageConsole getConsole() throws MalformedURLException {
			if(PyLintPrefPage.useConsole()){
				if (fConsole == null){
					fConsole = new MessageConsole("", PydevPlugin.getImageCache().getDescriptor(UIConstants.PY_ICON));
					ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{fConsole});
				}
			}else{
				return null;
			}
			return fConsole;
		}


        /**
         * @param tok
         * @param type
         * @param priority
         * @param id
         * @param line
         */
        private void addToMarkers(String tok, String type, int priority, String id, int line) {
            markers.add(new Object[]{tok, type, priority, id, line} );
        }
        
        /**
         * @param resource
         * @param out 
         * @param document
         * @param location
         * @throws CoreException
         */
        private void passPyLint(IResource resource, IOConsoleOutputStream out) throws CoreException {
            File script = new File(PyLintPrefPage.getPyLintLocation());
            File arg = new File(location.toOSString());

            ArrayList<String> list = new ArrayList<String>();
            list.add("--include-ids=y");
            
            //user args
            String userArgs = PyLintPrefPage.getPylintArgs().replaceAll("\r","").replaceAll("\n"," ");
            StringTokenizer tokenizer2 = new StringTokenizer(userArgs);
            while(tokenizer2.hasMoreTokens()){
                list.add(tokenizer2.nextToken());
            }
            list.add(REF.getFileAbsolutePath(arg));

⌨️ 快捷键说明

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