Search in sources :

Example 1 with OccurrencesVisitor

use of com.python.pydev.analysis.visitors.OccurrencesVisitor in project Pydev by fabioz.

the class OccurrencesAnalyzer method analyzeDocument.

public IMessage[] analyzeDocument(IPythonNature nature, final SourceModule module, final IAnalysisPreferences prefs, final IDocument document, final IProgressMonitor monitor, IIndentPrefs indentPrefs) {
    // Do pep8 in a thread.
    final List<IMessage> pep8Messages = new ArrayList<>();
    Thread t = new Thread() {

        @Override
        public void run() {
            pep8Messages.addAll(new Pep8Visitor().getMessages(module, document, monitor, prefs));
        }
    };
    t.start();
    OccurrencesVisitor visitor = new OccurrencesVisitor(nature, module.getName(), module, prefs, document, monitor);
    try {
        SimpleNode ast = module.getAst();
        if (ast != null) {
            if (nature.startRequests()) {
                try {
                    ast.accept(visitor);
                } finally {
                    nature.endRequests();
                }
            }
        }
    } catch (OperationCanceledException e) {
        throw e;
    } catch (Exception e) {
        Log.log(IStatus.ERROR, ("Error while visiting " + module.getName() + " (" + module.getFile() + ")"), e);
    }
    List<IMessage> messages = new ArrayList<IMessage>();
    if (!monitor.isCanceled()) {
        messages.addAll(visitor.getMessages());
        try {
            FileUtils.getPythonFileEncoding(document, module.getName());
        } catch (PyUnsupportedEncodingException e) {
            Message m = new Message(IAnalysisPreferences.TYPE_INVALID_ENCODING, e.getMessage(), e.getLine(), e.getLine(), e.getColumn(), e.getColumn() + e.getMessage().length(), prefs);
            messages.add(m);
        }
        try {
            messages.addAll(TabNanny.analyzeDoc(document, prefs, module.getName(), indentPrefs, monitor));
        } catch (Exception e) {
            // just to be safe... (could happen if the document changes during the process).
            Log.log(e);
        }
    }
    if (!monitor.isCanceled()) {
        try {
            t.join();
            messages.addAll(pep8Messages);
        } catch (InterruptedException e) {
        // If interrupted keep on going as it is.
        }
    }
    return messages.toArray(new IMessage[messages.size()]);
}
Also used : Message(org.python.pydev.ast.analysis.messages.Message) IMessage(org.python.pydev.ast.analysis.messages.IMessage) PyUnsupportedEncodingException(org.python.pydev.shared_core.io.PyUnsupportedEncodingException) IMessage(org.python.pydev.ast.analysis.messages.IMessage) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) OccurrencesVisitor(com.python.pydev.analysis.visitors.OccurrencesVisitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) PyUnsupportedEncodingException(org.python.pydev.shared_core.io.PyUnsupportedEncodingException) Pep8Visitor(com.python.pydev.analysis.pep8.Pep8Visitor) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Aggregations

Pep8Visitor (com.python.pydev.analysis.pep8.Pep8Visitor)1 OccurrencesVisitor (com.python.pydev.analysis.visitors.OccurrencesVisitor)1 ArrayList (java.util.ArrayList)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IMessage (org.python.pydev.ast.analysis.messages.IMessage)1 Message (org.python.pydev.ast.analysis.messages.Message)1 SimpleNode (org.python.pydev.parser.jython.SimpleNode)1 PyUnsupportedEncodingException (org.python.pydev.shared_core.io.PyUnsupportedEncodingException)1