<commit_before>"""
The Python parts of the Jedi library for VIM. It is mostly about communicating
with VIM.
"""

import traceback  # for exception output
import re
import os
from shlex import split as shsplit

import vim
import jedi
import jedi.keywords
from jedi._compatibility import unicode


def catch_and_print_exceptions(func):
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except (Exception, vim.error):
            print(traceback.format_exc())
            return None
    return wrapper


class VimError(Exception):
    def __init__(self, message, throwpoint, executing):
        super(type(self), self).__init__(message)
        self.throwpoint = throwpoint
        self.executing = executing

    def __str__(self):
        return self.message + '; created by: ' +  repr(self.executing)


def _catch_exception(string, is_eval):
    """
    Interface between vim and python calls back to it.
    Necessary, because the exact error message is not given by `vim.error`.
    """
    e = 'jedi#_vim_exceptions(%s, %s)'
    result = vim.eval(e % (repr(PythonToVimStr(string)), is_eval))
    if 'exception' in result:
        raise VimError(result['exception'], result['throwpoint'], string)
    return result['result']


def vim_eval(string):
    return _catch_exception(string, 1)


def vim_command(string):
    _catch_exception(string, 0)


def echo_highlight(msg):
    vim_command('echohl WarningMsg | echom "%s" | echohl None' % msg)


if not hasattr(jedi, '__version__') or jedi.__version__ < (0, 7, 0):
    echo_highlight('Please update your Jedi version, it is to old.')


class PythonToVimStr(unicode):
    """ Vim has a different string implementation of single quotes """
    __slots__ = []

    def __repr__(self):
        # this is totally stupid and makes no sense but vim/python unicode<commit_msg>fix problems with error reporting<commit_after>"""
The Python parts of the Jedi library for VIM. It is mostly about communicating
with VIM.
"""

import traceback  # for exception output
import re
import os
from shlex import split as shsplit

import vim
import jedi
import jedi.keywords
from jedi._compatibility import unicode, is_py3k


def catch_and_print_exceptions(func):
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except (Exception, vim.error):
            print(traceback.format_exc())
            return None
    return wrapper


class VimError(Exception):
    def __init__(self, message, throwpoint, executing):
        super(type(self), self).__init__(message)
        self.throwpoint = throwpoint
        self.executing = executing

    def __str__(self):
        return self.message + '; created by: ' +  repr(self.executing)


def _catch_exception(string, is_eval):
    """
    Interface between vim and python calls back to it.
    Necessary, because the exact error message is not given by `vim.error`.
    """
    e = 'jedi#_vim_exceptions(%s, %s)'
    result = vim.eval(e % (repr(PythonToVimStr(string, 'UTF-8')), is_eval))
    if 'exception' in result:
        raise VimError(result['exception'], result['throwpoint'], string)
    return result['result']


def vim_eval(string):
    return _catch_exception(string, 1)


def vim_command(string):
    _catch_exception(string, 0)


def echo_highlight(msg):
    vim_command('echohl WarningMsg | echom "%s" | echohl None' % msg)


if not hasattr(jedi, '__version__') or jedi.__version__ < (0, 7, 0):
    echo_highlight('Please update your Jedi version, it is to old.')


class PythonToVimStr(unicode):
    """ Vim has a different string implementation of single quotes """
    __slots__ = []
    def __new__(cls, obj, encoding='UTF-8'):
        if is_py3k or isinstance(obj, unicode):
            return unicode.__new__(cls, obj)
        else:
            return unicode.__new__(cls, obj, encoding)

    def __repr__(self):
        # this is totally stupid and makes no sense but vim/python unicode
{'fraction_non_alphanumeric': 0.10304096506659965, 'fraction_numerical': 0.0035184719778838906, 'mean_word_length': 3.3929359823399556, 'pattern_counts': {'":': 0, '<': 5, '<?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': '45178', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1179, 'n_words': 382}