<commit_before>from vial import vim, register_function
from vial.utils import vimfunction

BRACKETS = (
    ('(', ')'),
    ('[', ']'),
    ('{', '}'),
    ('\'', '\''),
    ('"', '"'),
)

OPEN2CLOSE = dict(BRACKETS)

tail = []

def init():
    register_function('<SID>VialCrampClose(what)',  close)
    register_function('<SID>VialCrampLeave()', leave)
    register_function('<SID>VialCrampSkip()', skip)
    register_function('<SID>VialCrampBS()', backspace)

    vim.command('inoremap <Plug>VialCrampLeave <c-r>=<SID>VialCrampLeave()<cr><esc>')
    vim.command('inoremap <Plug>VialCrampSkip <c-r>=<SID>VialCrampSkip()<cr>')

    vim.command('inoremap <bs> <c-r>=<SID>VialCrampBS()<cr><bs>')

    for s, e in BRACKETS:
        if e == '"':
            e = '\\"'

        vim.command('inoremap {0} {0}<c-r>=<SID>VialCrampClose("{1}")<cr>'.format(s, e))


@vimfunction
def close(what):
    tail.insert(0, what)
    col = vim.current.window.cursor[1]
    vim.current.line = vim.current.line[:col] + what + vim.current.line[col:]
    return ''

@vimfunction
def leave():
    if not tail:
        return ''

    col = vim.current.window.cursor[1]
    vim.current.line = vim.current.line[:col] + vim.current.line[col+len(tail):]

    result = ''.join(tail)
    tail[:] = []

    return result

@vimfunction
def skip():
    col = vim.current.window.cursor[1]
    char = vim.current.line[col]
    vim.current.line = vim.current.line[:col] + vim.current.line[col+1:]

    if tail and tail[0] == char:
        tail.pop(0)

    return char

@vimfunction
def backspace():
    col = vim.current.window.cursor[1]
    lchar = vim.current.line[col-1]
    rchar = vim.current.line[col]

    if tail and lchar in OPEN2CLOSE and OPEN2CLOSE[lchar] == rchar and rchar == tail[0]:
        vim.current.line = vim.current.line[:col] + vim.current.line[col+1:]
        tail.pop(0)

    return ''<commit_msg>backspace: fix corner cases<commit_after>from vial import vim, register_function
from vial.utils import vimfunction

BRACKETS = (
    ('(', ')'),
    ('[', ']'),
    ('{', '}'),
    ('\'', '\''),
    ('"', '"'),
)

OPEN2CLOSE = dict(BRACKETS)

tail = []

def init():
    register_function('<SID>VialCrampClose(what)',  close)
    register_function('<SID>VialCrampLeave()', leave)
    register_function('<SID>VialCrampSkip()', skip)
    register_function('<SID>VialCrampBS()', backspace)

    vim.command('inoremap <Plug>VialCrampLeave <c-r>=<SID>VialCrampLeave()<cr><esc>')
    vim.command('inoremap <Plug>VialCrampSkip <c-r>=<SID>VialCrampSkip()<cr>')

    vim.command('inoremap <bs> <c-r>=<SID>VialCrampBS()<cr><bs>')

    for s, e in BRACKETS:
        if e == '"':
            e = '\\"'

        vim.command('inoremap {0} {0}<c-r>=<SID>VialCrampClose("{1}")<cr>'.format(s, e))


@vimfunction
def close(what):
    tail.insert(0, what)
    col = vim.current.window.cursor[1]
    vim.current.line = vim.current.line[:col] + what + vim.current.line[col:]
    return ''

@vimfunction
def leave():
    if not tail:
        return ''

    col = vim.current.window.cursor[1]
    vim.current.line = vim.current.line[:col] + vim.current.line[col+len(tail):]

    result = ''.join(tail)
    tail[:] = []

    return result

@vimfunction
def skip():
    col = vim.current.window.cursor[1]
    char = vim.current.line[col]
    vim.current.line = vim.current.line[:col] + vim.current.line[col+1:]

    if tail and tail[0] == char:
        tail.pop(0)

    return char

@vimfunction
def backspace():
    col = vim.current.window.cursor[1]

    try:
        lchar = vim.current.line[col-1]
        rchar = vim.current.line[col]
    except IndexError:
        return ''

    if tail and lchar in OPEN2CLOSE and OPEN2CLOSE[lchar] == rchar and rchar == tail[0]:
        vim.current.line = vim.current.line[:col] + vim.current.line[col+1:]
        tail.pop(0)

    return ''
{'fraction_non_alphanumeric': 0.18979432439468888, 'fraction_numerical': 0.009372559229367352, 'mean_word_length': 3.25, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, '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': '2464210', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1447, 'n_words': 241}