<commit_before>/*
 * Created on 23/07/2005
 */
package com.python.pydev.analysis.builder;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.python.pydev.builder.PyDevBuilderVisitor;
import org.python.pydev.core.log.Log;
import org.python.pydev.editor.codecompletion.revisited.modules.AbstractModule;
import org.python.pydev.editor.codecompletion.revisited.modules.SourceModule;
import org.python.pydev.plugin.nature.PythonNature;

import com.python.pydev.analysis.AnalysisPreferences;
import com.python.pydev.analysis.IAnalysisPreferences;
import com.python.pydev.analysis.OcurrencesAnalyzer;
import com.python.pydev.analysis.messages.IMessage;

public class AnalysisBuilderVisitor extends PyDevBuilderVisitor{

    private static final String PYDEV_ANALYSIS_PROBLEM_MARKER = "com.python.pydev.analysis.pydev_analysis_problemmarker";
    
    /**
     * here we have to detect errors / warnings from the code analysis
     *  
     * @see org.python.pydev.builder.PyDevBuilderVisitor#visitChangedResource(org.eclipse.core.resources.IResource, org.eclipse.jface.text.IDocument)
     */
    @Override
    public boolean visitChangedResource(IResource resource, IDocument document) {
        try {
            resource.deleteMarkers(PYDEV_ANALYSIS_PROBLEM_MARKER, false, IResource.DEPTH_ZERO);
        } catch (CoreException e3) {
            Log.log(e3);
        }
        if(document.get().indexOf("#@PydevCodeAnalysisIgnore") != -1){
            return true;
        }
        
        if(isInPythonPath(resource)){ //just get problems in resources that are in the pythonpath
            IAnalysisPreferences analysisPreferences = AnalysisPreferences.getAnalysisPreferences();
            analysisPreferences.clearCaches();
            
            //let's see if we should do code analysis
            if(analysisPreferences.makeCodeAnalysis() == false){
                return true;
            }
    
            OcurrencesAnalyzer analyzer = new OcurrencesAnalyzer();
            PythonNature nature = PythonNature.getPythonNature(resource.getProject());
            IFile f = (IFile) resource;
            String file = f.getRawLocation().toOSString();
            
            String moduleName = nature.getAstManager().getProjectModulesManager().resolveModule(file);
            
            AbstractModule module = AbstractModule.createModuleFromDoc(moduleName, new File(file), document, nature, 0);
            
            //ok, let's do it
            IMessage[] messages = analyzer.analyzeDocument(nature, (SourceModule) module, analysisPreferences);
            try {
                
                //add the markers
                for (IMessage m : messages) {
                    String msg = "ID:" + m.getType() + " " + m.getMessage();
                    createMarker(resource, document, msg, 
                            m.getStartLine(document) - 1, m.getStartCol(document) - 1, m.getEndLine(document) - 1, m.getEndCol(document) - 1, 
                            PYDEV_ANALYSIS_PROBLEM_MARKER, m.getSeverity());
                }
            } catch (Exception e) {
                Log.log(e);
            }
        }
        return true;
    }

    @Override
    public boolean visitRemovedResource(IResource resource, IDocument document) {
        return false;
    }

}<commit_msg>*** empty log message ***<commit_after>/*
 * Created on 23/07/2005
 */
package com.python.pydev.analysis.builder;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.python.pydev.builder.PyDevBuilderVisitor;
import org.python.pydev.builder.PydevMarkerUtils;
import org.python.pydev.core.log.Log;
import org.python.pydev.editor.codecompletion.revisited.modules.AbstractModule;
import org.python.pydev.editor.codecompletion.revisited.modules.SourceModule;
import org.python.pydev.plugin.nature.PythonNature;

import com.python.pydev.analysis.AnalysisPreferences;
import com.python.pydev.analysis.IAnalysisPreferences;
import com.python.pydev.analysis.OcurrencesAnalyzer;
import com.python.pydev.analysis.messages.IMessage;

public class AnalysisBuilderVisitor extends PyDevBuilderVisitor{

    private static final String PYDEV_ANALYSIS_PROBLEM_MARKER = "com.python.pydev.analysis.pydev_analysis_problemmarker";
    
    /**
     * here we have to detect errors / warnings from the code analysis
     *  
     * @see org.python.pydev.builder.PyDevBuilderVisitor#visitChangedResource(org.eclipse.core.resources.IResource, org.eclipse.jface.text.IDocument)
     */
    @Override
    public boolean visitChangedResource(IResource resource, IDocument document) {
        try {
            resource.deleteMarkers(PYDEV_ANALYSIS_PROBLEM_MARKER, false, IResource.DEPTH_ZERO);
        } catch (CoreException e3) {
            Log.log(e3);
        }
        if(document.get().indexOf("#@PydevCodeAnalysisIgnore") != -1){
            return true;
        }
        
        if(isInPythonPath(resource)){ //just get problems in resources that are in the pythonpath
            IAnalysisPreferences analysisPreferences = AnalysisPreferences.getAnalysisPreferences();
            analysisPreferences.clearCaches();
            
            //let's see if we should do code analysis
            if(analysisPreferences.makeCodeAnalysis() == false){
                return true;
            }
    
            OcurrencesAnalyzer analyzer = new OcurrencesAnalyzer();
            PythonNature nature = PythonNature.getPythonNature(resource.getProject());
            IFile f = (IFile) resource;
            String file = f.getRawLocation().toOSString();
            
            String moduleName = nature.getAstManager().getProjectModulesManager().resolveModule(file);
            
            AbstractModule module = AbstractModule.createModuleFromDoc(moduleName, new File(file), document, nature, 0);
            
            //ok, let's do it
            IMessage[] messages = analyzer.analyzeDocument(nature, (SourceModule) module, analysisPreferences);
            try {
                
                //add the markers
                for (IMessage m : messages) {
                    String msg = "ID:" + m.getType() + " " + m.getMessage();
                    PydevMarkerUtils.createMarker(resource, document, msg, 
                            m.getStartLine(document) - 1, m.getStartCol(document) - 1, m.getEndLine(document) - 1, m.getEndCol(document) - 1, 
                            PYDEV_ANALYSIS_PROBLEM_MARKER, m.getSeverity());
                }
            } catch (Exception e) {
                Log.log(e);
            }
        }
        return true;
    }

    @Override
    public boolean visitRemovedResource(IResource resource, IDocument document) {
        return false;
    }

}
{'fraction_non_alphanumeric': 0.09618952476095333, 'fraction_numerical': 0.0045668617097188524, 'mean_word_length': 2.535822401614531, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5048408', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1880, 'n_words': 368}