<commit_before>"" parinfer.vim - a Parinfer implementation in Vimscript
"" v0.1.0+dev
"" https://github.com/oakmac/parinfer-viml
""
"" More information about Parinfer can be found here:
"" http://shaunlebron.github.io/parinfer/
""
"" Copyright (c) 2015, <NAME> and other contributors
"" Released under the ISC license
"" https://github.com/oakmac/parinfer-viml/blob/master/LICENSE.md

""------------------------------------------------------------------------------
"" Constants / Predicates
""------------------------------------------------------------------------------

let s:INDENT_MODE = 'INDENT_MODE'
let s:PAREN_MODE = 'PAREN_MODE'

let s:BACKSLASH = '\'
let s:BLANK_SPACE = ' '
let s:DOUBLE_SPACE = '  '
let s:DOUBLE_QUOTE = '"'
let s:NEWLINE = "\n"
let s:SEMICOLON = ';'
let s:TAB = "\t"

let s:PARENS = {
    '(': ')',
    '{': '}',
    '[': ']',
    ')': '(',
    '}': '{',
    ']': '[',
}

function! s:IsOpenParen(c)
    return c ==# '(' || c ==# '{' || c ==# '['
endfunction

function! s:IsCloseParen(c)
    return c ==# ')' || c ==# '}' || c ==# ']'
endfunction

""------------------------------------------------------------------------------
"" Result Structure
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Errors
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" String Operations
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Line operations
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Misc Util
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Character functions
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Cursor functions
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Paren Trail functions
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Indentation functions<commit_msg>saving state<commit_after>"" parinfer.vim - a Parinfer implementation in Vimscript
"" v0.1.0+dev
"" https://github.com/oakmac/parinfer-viml
""
"" More information about Parinfer can be found here:
"" http://shaunlebron.github.io/parinfer/
""
"" Copyright (c) 2015, <NAME> and other contributors
"" Released under the ISC license
"" https://github.com/oakmac/parinfer-viml/blob/master/LICENSE.md

""------------------------------------------------------------------------------
"" Constants / Predicates
""------------------------------------------------------------------------------

let s:SENTINEL_NULL = -999

let s:INDENT_MODE = 'INDENT_MODE'
let s:PAREN_MODE = 'PAREN_MODE'

let s:BACKSLASH = '\'
let s:BLANK_SPACE = ' '
let s:DOUBLE_SPACE = '  '
let s:DOUBLE_QUOTE = '"'
let s:NEWLINE = "\n"
let s:SEMICOLON = ';'
let s:TAB = "\t"

let s:PARENS = {
  \ '(': ')',
  \ '{': '}',
  \ '[': ']',
  \ ')': '(',
  \ '}': '{',
  \ ']': '[',
  \ }


function! s:IsOpenParen(ch)
    return a:ch ==# '(' || a:ch ==# '{' || a:ch ==# '['
endfunction


function! s:IsCloseParen(ch)
    return a:ch ==# ')' || a:ch ==# '}' || a:ch ==# ']'
endfunction

""------------------------------------------------------------------------------
"" Result Structure
""------------------------------------------------------------------------------

function! s:CreateInitialResult(text, mode, options)
    return {
      \ 'mode': a:mode,

      \ 'origText': a:text,
      \ 'origLines': split(a:text, s:NEWLINE),

      \ 'lines': [],
      \ 'lineNo': -1,
      \ 'ch': '',
      \ 'x': 0,

      \ 'parenStack': [],

      \ 'parenTrailLineNo': s:SENTINEL_NULL,
      \ 'parenTrailStartX': s:SENTINEL_NULL,
      \ 'parenTrailEndX': s:SENTINEL_NULL,
      \ 'parenTrailOpeners': [],

      \ 'cursorX': s:SENTINEL_NULL,
      \ 'cursorLine': s:SENTINEL_NULL,
      \ 'cursorDx': s:SENTINEL_NULL,

      \ 'isInCode': 1,
      \ 'isEscaping': 0,
      \ 'isInStr': 0,
      \ 'isInComment': 0,
      \ 'commentX': s:SENTINEL_NULL,

      \ 'quoteDanger': 0,
      \ 'trackingIndent': 0,
      \ 'skipChar': 0,
      \ 'success': 0,

      \ 'maxIndent': s:SENTINEL_NULL,
      \ 'indentDelta': 0,

      \ 'error': s:SENTINEL_NULL,
      \ 'errorPosCache': {},
      \ }
endfunction

""------------------------------------------------------------------------------
"" Errors
""------------------------------------------------------------------------------

let s:ERROR_QUOTE_DANGER = 'quote-danger'
let s:ERROR_EOL_BACKSLASH = 'eol-backslash'
let s:ERROR_UNCLOSED_QUOTE = 'unclosed-quote'
let s:ERROR_UNCLOSED_PAREN = 'unclosed-paren'

let s:ERROR_MESSAGES = {}
let s:ERROR_MESSAGES[s:ERROR_QUOTE_DANGER] = 'Quotes must balanced inside comment blocks.'
let s:ERROR_MESSAGES[s:ERROR_EOL_BACKSLASH] = 'Line cannot end in a hanging backslash.'
let s:ERROR_MESSAGES[s:ERROR_UNCLOSED_QUOTE] = 'String is missing a closing quote.'
let s:ERROR_MESSAGES[s:ERROR_UNCLOSED_PAREN] = 'Unmatched open-paren.'

"" TODO: write this section

""------------------------------------------------------------------------------
"" String Operations
""------------------------------------------------------------------------------


function! s:InsertWithinString(orig, idx, insert)
    return strpart(a:orig, 0, a:idx) . a:insert . strpart(a:orig, a:idx)
endfunction


function! s:ReplaceWithinString(orig, startIdx, endIdx, replace)
    return strpart(a:orig, 0, a:startIdx) . a:replace . strpart(a:orig, a:endIdx)
endfunction


function! s:RemoveWithinString(orig, startIdx, endIdx)
    return strpart(a:orig, 0, a:startIdx) . strpart(a:orig, a:endIdx)
endfunction


function! s:RepeatString(text, n)
    let l:result = ''
    let l:i = 0
    while l:i < a:n
        let l:result = l:result . a:text
        let l:i = (l:i + 1)
    endwhile
    return l:result
endfunction


""------------------------------------------------------------------------------
"" Line operations
""------------------------------------------------------------------------------


function! s:InsertWithinLine(result, lineNo, idx, insert)
    let l:line = a:result.lines[a:lineNo]
    let a:result.lines[a:lineNo] = s:InsertWithinString(l:line, a:idx, a:insert)
endfunction


function! s:ReplaceWithinLine(result, lineNo, startIdx, endIdx, replace)
    let l:line = a:result.lines[a:lineNo]
    let a:result.lines[a:lineNo] = s:ReplaceWithinString(l:line, a:startIdx, a:endIdx, a:replace)
endfunction


function! s:RemoveWithinLine(result, lineNo, startIdx, endIdx)
    let l:line = a:result.lines[a:lineNo]
    let a:result.lines[a:lineNo] = s:RemoveWithinString(l:line, a:startIdx, a:endIdx)
endfunction


function! s:InitLine(result, line)
    let a:result.x = 0
    let a:result.lineNo = a:result.lineNo + 1
    add(a:result.lines, line)

    "" reset line-specific state
    let a:result.commentX = s:SENTINEL_NULL
    let a:result.indentDelta = 0
endfunction


function! s:CommitChar(result, origCh)
    let l:ch = a:result.ch
    if a:origCh !=# l:ch
        s:ReplaceWithinLine(a:result, a:result.lineNo, a:result.x, a:result.x + strlen(a:origCh), l:ch)
    endif
    let a:result.x = a:result.x + strlen(l:ch)
endfunction


""------------------------------------------------------------------------------
"" Misc Util
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Character functions
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Cursor functions
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Paren Trail functions
""------------------------------------------------------------------------------

"" TODO: write this section

""------------------------------------------------------------------------------
"" Indentation functions
{'fraction_non_alphanumeric': 0.44643055401354503, 'fraction_numerical': 0.003996891306761408, 'mean_word_length': 1.1046728971962616, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '7076863', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2356, 'n_words': 481}