<commit_before>/*
 * Created on Jul 1, 2006
 * @author Fabio
 */
package org.python.pydev.editor.codecompletion;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.python.pydev.core.docutils.ParsingUtils;
import org.python.pydev.plugin.PydevPlugin;

/**
 * Based on JavaParameterListValidator
 * 
 * @author Fabio
 */
public class PyContextInformationValidator implements IContextInformationValidator, IContextInformationPresenter {

    public PyCalltipsContextInformation fInformation;

    public IDocument doc;

    public int fCurrentParameter;

    public boolean returnedFalseOnce;

    private int fPosition;

    /**
     * Holds the comma positions for the context information to display
     */
    int[] commaPositions;

    /**
     * IContextInformationValidator
     */
    public void install(IContextInformation info, IDocument doc, int offset) {
        System.out.println("Install");
        this.returnedFalseOnce = false;
        this.fInformation = (PyCalltipsContextInformation) info;
        this.doc = doc;
        this.fPosition = fInformation.getReplacementOffset();
        this.fCurrentParameter = -1;
        this.commaPositions = null;
    }

    /**
     * IContextInformationPresenter
     */
    public void install(IContextInformation info, ITextViewer viewer, int offset) {
        install(info, viewer.getDocument(), offset);
    }

    /**
     * @see IContextInformationValidator#isContextInformationValid(int)
     */
    public boolean isContextInformationValid(int position) {
        System.out.println("isContextInformationValid:" + position);
        if (doc == null) {
            this.returnedFalseOnce = true;
            return false;
        }

        try {
            if (position < fPosition) {
                this.returnedFalseOnce = true;
                return false;
            }

            IDocument document = doc;
            IRegion line = document.getLineInformationOfOffset(fPosition);

            if (position < line.getOffset() || position >= document.getLength()) {
                this.returnedFalseOnce = true;
                return false;
            }

            boolean ret = getCurrentParameter(document, fPosition, position, "(", ")", false) >= 0; //$NON-NLS-1$ //$NON-NLS-2$
            if (ret == false) {
                returnedFalseOnce = true;
            }
            return ret;

        } catch (BadLocationException x) {
            this.returnedFalseOnce = true;
            return false;
        } catch (Exception x) {
            this.returnedFalseOnce = true;
            PydevPlugin.log(x);
            return false;
        }
    }

    //--- interface from IContextInformationPresenter

    /**
     * @see IContextInformationPresenter#updatePresentation(int, TextPresentation)
     */
    public boolean updatePresentation(int position, TextPresentation presentation) {
        System.out.println("updatePresentation: " + position);
        int currentParameter = -1;

        try {
            currentParameter = getCurrentParameter(doc, fPosition, position, ",", "", true);
        } catch (BadLocationException x) {
            return false;
        }

        System.out.println("updatePresentation: current param: " + currentParameter);
        if (fCurrentParameter != -1) {
            if (currentParameter == fCurrentParameter) {
                return false;
            }
        }

        presentation.clear();
        fCurrentParameter = currentParameter;

        String s = fInformation.getInformationDisplayString();
        int[] commas = computeCommaPositions();<commit_msg>removed prints<commit_after>/*
 * Created on Jul 1, 2006
 * @author Fabio
 */
package org.python.pydev.editor.codecompletion;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.python.pydev.core.docutils.ParsingUtils;
import org.python.pydev.plugin.PydevPlugin;

/**
 * Based on JavaParameterListValidator
 * 
 * @author Fabio
 */
public class PyContextInformationValidator implements IContextInformationValidator, IContextInformationPresenter {

    public PyCalltipsContextInformation fInformation;

    public IDocument doc;

    public int fCurrentParameter;

    public boolean returnedFalseOnce;

    private int fPosition;

    /**
     * Holds the comma positions for the context information to display
     */
    int[] commaPositions;

    /**
     * IContextInformationValidator
     */
    public void install(IContextInformation info, IDocument doc, int offset) {
        this.returnedFalseOnce = false;
        this.fInformation = (PyCalltipsContextInformation) info;
        this.doc = doc;
        this.fPosition = fInformation.getReplacementOffset();
        this.fCurrentParameter = -1;
        this.commaPositions = null;
    }

    /**
     * IContextInformationPresenter
     */
    public void install(IContextInformation info, ITextViewer viewer, int offset) {
        install(info, viewer.getDocument(), offset);
    }

    /**
     * @see IContextInformationValidator#isContextInformationValid(int)
     */
    public boolean isContextInformationValid(int position) {
        if (doc == null) {
            this.returnedFalseOnce = true;
            return false;
        }

        try {
            if (position < fPosition) {
                this.returnedFalseOnce = true;
                return false;
            }

            IDocument document = doc;
            IRegion line = document.getLineInformationOfOffset(fPosition);

            if (position < line.getOffset() || position >= document.getLength()) {
                this.returnedFalseOnce = true;
                return false;
            }

            boolean ret = getCurrentParameter(document, fPosition, position, "(", ")", false) >= 0; //$NON-NLS-1$ //$NON-NLS-2$
            if (ret == false) {
                returnedFalseOnce = true;
            }
            return ret;

        } catch (BadLocationException x) {
            this.returnedFalseOnce = true;
            return false;
        } catch (Exception x) {
            this.returnedFalseOnce = true;
            PydevPlugin.log(x);
            return false;
        }
    }

    //--- interface from IContextInformationPresenter

    /**
     * @see IContextInformationPresenter#updatePresentation(int, TextPresentation)
     */
    public boolean updatePresentation(int position, TextPresentation presentation) {
        int currentParameter = -1;

        try {
            currentParameter = getCurrentParameter(doc, fPosition, position, ",", "", true);
        } catch (BadLocationException x) {
            return false;
        }

        if (fCurrentParameter != -1) {
            if (currentParameter == fCurrentParameter) {
                return false;
            }
        }

        presentation.clear();
        fCurrentParameter = currentParameter;

        String s = fInformation.getInformationDisplayString();
        int[] commas = computeCommaPositions();
{'fraction_non_alphanumeric': 0.09503105590062112, 'fraction_numerical': 0.002732919254658385, 'mean_word_length': 2.540457343887423, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5941689', 'n_tokens_mistral': 2162, 'n_tokens_neox': 2092, 'n_words': 453}