<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

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

temp_rename = None  # used for jedi#rename

encoding = str if is_py3k else unicode


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

    def __new__(cls, s):
        # VIM doesn't like UTF-8
        s = s.decode('latin-1')
        return super(PythonToVimStr, cls).__new__(cls, s)

    def __repr__(self):
        # this is totally stupid and makes no sense but vim/python unicode
        # support is pretty bad. don't ask how I came up with this... It just
        # works...
        # It seems to be related to that bug: http://bugs.python.org/issue5876
        s = self.encode('UTF-8')
        return '"%s"' % s.replace('\\', '\\\\').replace('"', r'\"')


def echo_highlight(msg):
    vim.command('echohl WarningMsg | echo "%s" | echohl None' % msg)


def get_script(source=None, column=None):
    jedi.settings.additional_dynamic_modules = [b.name for b in vim.buffers
                            if b.name is not None and b.name.endswith('.py')]
    if source is None:
        source = '\n'.join(vim.current.buffer)
    row = vim.current.window.cursor[0]
    if column is None:
        column = vim.current.window.cursor[1]
    buf_path = vim.current.buffer.name
    return jedi.Script(source, row, column, buf_path)


def complete():
    row, column = vim.current.window.cursor
    clear_func_def()
    if vim.eval('a:findstart') == '1':
        count = 0
        for char in reversed(vim.current.line[:column]):
            if not re.match('[\w\d]', char):
                break
            count += 1
        vim.command('return %i' % (column - count))
    else:
        base = vim.eval('a:base')
        source = ''
        for i, line in enumerate(vim.current.buffer):
            # enter this path again, otherwise source would be incomplete
            if i == row - 1:
                source += line[:column] + base + line[column:]
            else:
                source += line
            source += '\n'
        # here again hacks, because jedi has a different interface than vim
        column += len(base)
        try:
            script = get_script(source=source, column=column)
            completions = script.complete()
            call_def = script.get_in_function_call()

            out = []
            for c in completions:
                d = dict(word=c.word[:len(base)] + c.complete,
                         abbr=c.word,
                         # stuff directly behind the completion
                         menu=PythonToVimStr(c.description),
                         info=PythonToVimStr(c.doc),  # docstr
                         icase=1,  # case insensitive
                         dup=1  # allow duplicates (maybe later remove this)
                )
                out.append(d)

            strout = str(out)
        except Exception:
            # print to stdout, will be in :messages
            print(traceback.format_exc())
            strout = ''
            completions = []
            call_def = None<commit_msg>changed something that I absolutely don't get again. #48<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

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

temp_rename = None  # used for jedi#rename

encoding = str if is_py3k else unicode


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

    def __new__(cls, s):
        # Somehow I'm not getting anything unicode related in Python
        try:
            s = s.decode('utf-8')
        except UnicodeDecodeError:
            s = unicode(s, 'latin-1')
        return super(PythonToVimStr, cls).__new__(cls, s)

    def __repr__(self):
        # this is totally stupid and makes no sense but vim/python unicode
        # support is pretty bad. don't ask how I came up with this... It just
        # works...
        # It seems to be related to that bug: http://bugs.python.org/issue5876
        s = self.encode('UTF-8')
        return '"%s"' % s.replace('\\', '\\\\').replace('"', r'\"')


def echo_highlight(msg):
    vim.command('echohl WarningMsg | echo "%s" | echohl None' % msg)


def get_script(source=None, column=None):
    jedi.settings.additional_dynamic_modules = [b.name for b in vim.buffers
                            if b.name is not None and b.name.endswith('.py')]
    if source is None:
        source = '\n'.join(vim.current.buffer)
    row = vim.current.window.cursor[0]
    if column is None:
        column = vim.current.window.cursor[1]
    buf_path = vim.current.buffer.name
    return jedi.Script(source, row, column, buf_path)


def complete():
    row, column = vim.current.window.cursor
    clear_func_def()
    if vim.eval('a:findstart') == '1':
        count = 0
        for char in reversed(vim.current.line[:column]):
            if not re.match('[\w\d]', char):
                break
            count += 1
        vim.command('return %i' % (column - count))
    else:
        base = vim.eval('a:base')
        source = ''
        for i, line in enumerate(vim.current.buffer):
            # enter this path again, otherwise source would be incomplete
            if i == row - 1:
                source += line[:column] + base + line[column:]
            else:
                source += line
            source += '\n'
        # here again hacks, because jedi has a different interface than vim
        column += len(base)
        try:
            script = get_script(source=source, column=column)
            completions = script.complete()
            call_def = script.get_in_function_call()

            out = []
            for c in completions:
                d = dict(word=PythonToVimStr(c.word[:len(base)] + c.complete),
                         abbr=PythonToVimStr(c.word),
                         # stuff directly behind the completion
                         menu=PythonToVimStr(c.description),
                         info=PythonToVimStr(c.doc),  # docstr
                         icase=1,  # case insensitive
                         dup=1  # allow duplicates (maybe later remove this)
                )
                out.append(d)

            strout = str(out)
        except Exception:
            # print to stdout, will be in :messages
            print(traceback.format_exc())
            strout = ''
            completions = []
            call_def = None
{'fraction_non_alphanumeric': 0.10692748933666715, 'fraction_numerical': 0.00529489630828063, 'mean_word_length': 1.88135593220339, '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': '2712653', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1913, 'n_words': 586}