Add :DMDebug

This commit is contained in:
Israel Chauca Fuentes
2017-02-13 19:11:51 -05:00
parent b1ff7f7942
commit d882e342f7

View File

@@ -1,8 +1,9 @@
" Script variables {{{1
let s:defaults = {}
let s:defaults.delimitMate_enabled = 1
let s:defaults.delimitMate_pairs = ['()', '[]', '{}']
let s:defaults.delimitMate_quotes = ['"', "'", '`']
let s:defaults.delimitMate_enabled = 1
let s:defaults.delimitMate_debug = 4
let s:defaults.delimitMate_autoclose = 1
let s:defaults.delimitMate_expand_space = 0
let s:defaults.delimitMate_expand_cr = 0
@@ -45,6 +46,17 @@ let s:info.char = ''
let s:info.nesting = 0
let s:info.template = {}
function! s:debug(debug_level, ...) "{{{
if s:option('debug') >= a:debug_level
let trail = expand('<sfile>')
let trail = substitute(trail, '\.\.<SNR>\d\+_debug$', '', '')
let trail = substitute(trail, '^function\s\+\%(delimitMate\)\?', '', '')
let message = get(a:, 1, '')
echom printf('%s: %s', trail, message)
endif
endfunction
command! -nargs=* -count=3 DMDebug call s:debug(<count>, <args>)
function! s:defaults.consolidate() "{{{1
let g = filter(copy(g:), 'v:key =~# "^delimitMate_"')
let b = filter(copy(b:), 'v:key =~# "^delimitMate_"')
@@ -117,7 +129,7 @@ function! s:get_info(...) "{{{1
let d.n_char = strcharpart(d.ahead, 0, 1)
let d.around = d.p_char . d.n_char
call extend(d, s:info.template, 'keep')
echom string(d)
3DMDebug string(d)
return d
endfunction
@@ -134,7 +146,7 @@ function! s:any_is_true(expressions, info, options) "{{{1
let next_char = info.cur.n_char
let exprs = copy(a:expressions)
call filter(exprs, 'eval(v:val)')
echom 'any_is_true: ' . string(exprs)
3DMDebug string(exprs)
return !empty(exprs)
endfunction
@@ -157,25 +169,24 @@ function! delimitMate#CursorMovedI(...) "{{{1
let s:info.prev = s:info.cur
let s:info.cur = call('s:get_info', a:000)
let s:info.skip_icp = 0
echom 'INFO: ' . string(s:info)
echom 'CMI: ' . s:info.prev.line
3DMDebug 'INFO: ' . string(s:info)
endfunction
function! delimitMate#InsertEnter(...) "{{{1
let s:info.cur = call('s:get_info', a:000)
let s:info.prev = {}
let s:info.skip_icp = 0
echom 'IE: ' . s:info.cur.line
3DMDebug
endfunction
function! delimitMate#TextChangedI(...) "{{{1
echom 'TCI: ' . s:info.cur.line
3DMDebug s:info.cur.line
if pumvisible()
echom 20
3DMDebug "20"
return 0
endif
if !s:option('enabled')
echom 21
3DMDebug "21"
return
endif
if s:info.cur.lnum == s:info.prev.lnum + 1
@@ -183,106 +194,106 @@ function! delimitMate#TextChangedI(...) "{{{1
\&& s:info.prev.ahead ==# s:info.cur.ahead
\&& s:info.cur.behind =~ '^\s*$'
" CR
echom 22
3DMDebug "22"
return feedkeys(s:keys4cr(s:info, s:defaults.consolidate()), 'tni')
endif
if s:info.cur.lnum == s:info.prev.lnum - 1
\&& s:info.prev.prev_line ==# s:info.cur.line
\&& s:info.prev.next_line ==# s:info.cur.next_line
let pair = filter(s:option('pairs'), 's:info.cur.p_char . matchstr(s:info.cur.next_line, "^\\s*\\zs\\S") ==# v:val')
echom 23
3DMDebug "23"
if s:option('expand_cr') && !empty(pair)
echom "23.1"
3DMDebug "23.1"
let spaces = strchars(s:info.cur.next_line, '^\s*')
return feedkeys(repeat("\<Del>", spaces), 'nti')
endif
let quote = filter(s:option('quotes'), 's:info.cur.p_char . matchstr(s:info.cur.next_line, "^\\s*\\zs\\S") ==# v:val.v:val')
if s:option('expand_cr') && s:option('expand_inside_quotes') && !empty(quote)
echom "23.2"
3DMDebug "23.2"
return feedkeys("\<Del>")
endif
return
endif
if s:info.cur.lnum != s:info.prev.lnum
echom 24
3DMDebug "24"
return
endif
if s:info.prev.col - s:info.cur.col != len(s:info.prev.p_char)
echom 25
3DMDebug "25"
return
endif
if len(s:info.prev.line) == len(s:info.cur.line)
echom 26
3DMDebug "26"
return
endif
echom s:info.prev.around
3DMDebug s:info.prev.around
let pair = filter(s:option('pairs'), 'v:val ==# (s:info.cur.p_char . matchstr(s:info.cur.ahead, "^\\s\\zs\\S"))')
let quotes = filter(s:option('quotes'), 'v:val . v:val ==# (s:info.cur.p_char . matchstr(s:info.cur.ahead, "^\\s\\zs\\S"))')
if s:option('expand_space') && (!empty(pair) || (s:option('expand_inside_quotes') && !empty(quotes)))
echom 27
3DMDebug "27"
return feedkeys("\<Del>", 'tni')
endif
let pair = filter(s:option('pairs'), 'v:val ==# s:info.prev.around')
let quote = filter(s:option('quotes'), 'v:val . v:val ==# s:info.prev.around')
if empty(pair) && empty(quote)
echom 28
3DMDebug "28"
return
endif
echom 29
3DMDebug "29"
let keys = "\<Del>"
call feedkeys(keys, 'tni')
endfunction
" vim: sw=2 et
function! delimitMate#InsertCharPre(str) "{{{1
echom 'ICP ' . string(a:str) . ': ' . get(s:info, 'cur', {'line': ''}).line
3DMDebug string(a:str) . ': ' . get(s:info, 'cur', {'line': ''}).line
if s:info.skip_icp
" iabbrev fires this event for every char and the trigger
echom 01
3DMDebug "01"
return 0
endif
if pumvisible()
echom 2
3DMDebug "2"
return 0
endif
if s:info.nesting
echom 03
3DMDebug "03"
let s:info.nesting -= 1
return 0
endif
let s:info.skip_icp = 1
if !s:option('enabled')
echom 04
3DMDebug "04"
return 0
endif
let synstack = join(map(synstack(line('.'), col('.')), 'tolower(synIDattr(v:val, "name"))'), ',')
let s:info.is_ignored_syn = !empty(filter(s:option('excluded_regions'), 'stridx(synstack, tolower(v:val)) >= 0'))
echom 9
3DMDebug "9"
for char in split(a:str, '\zs')
let keys = ''
let s:info.char = char
let opts = s:defaults.consolidate()
if s:info.cur.is_escaped()
echom 12
3DMDebug "12"
return
elseif !empty(filter(copy(opts.quotes), 'v:val ==# char'))
echom 15
3DMDebug "15"
let keys = s:keys4quote(char, s:info, opts)
let s:info.nesting = strchars(matchstr(keys, '^[^[:cntrl:]]*'))
let s:info.nesting = s:info.nesting < 3 ? 0 : s:info.nesting
elseif s:info.is_ignored_syn
echom 9
3DMDebug "9"
return
elseif char == ' '
echom 13
3DMDebug "13"
let keys = s:keys4space(s:info, opts)
elseif char == "\<C-]>"
let prev_line = line('.') == 1 ? '' : getline(line('.') - 1)
let next_line = line('.') == line('$') ? '' : getline(line('.') + 1)
echom 14
3DMDebug "14"
let keys = s:keys4cr(prev_line, next_line, s:info, opts)
elseif !empty(filter(copy(opts.pairs), 'strcharpart(v:val, 0, 1) ==# char'))
echom 16
3DMDebug "16"
let pair = get(filter(copy(opts.pairs), 'strcharpart(v:val, 0, 1) ==# char'), 0, '')
let keys = s:keys4left(char, pair, s:info, opts)
"echom strtrans(keys)
@@ -290,99 +301,99 @@ function! delimitMate#InsertCharPre(str) "{{{1
elseif !empty(filter(copy(opts.pairs), 'strcharpart(v:val, 1, 1) ==# char'))
let pair = get(filter(copy(opts.pairs), 'strcharpart(v:val, 1, 1) ==# char'), 0, '')
let keys = s:keys4right(char, pair, s:info, opts)
echom 17
echom keys
3DMDebug "17"
else
echom 18
3DMDebug "18"
return 0
endif
3DMDebug keys
call feedkeys(keys, 'mti')
endfor
endfunction
function! s:keys4space(info, opts) "{{{1
echom string(a:opts)
3DMDebug string(a:opts)
let empty_pair = !empty(filter(copy(a:opts.pairs), 'v:val ==# a:info.cur.around'))
if a:opts.expand_space && empty_pair
echom 61
3DMDebug "61"
return " \<C-G>U\<Left>"
endif
let empty_quotes = !empty(filter(copy(a:opts.quotes), 'v:val.v:val ==# a:info.cur.around'))
if a:opts.expand_space && a:opts.expand_inside_quotes && empty_quotes
echom 62
3DMDebug "62"
return " \<C-G>U\<Left>"
endif
echom 69
3DMDebug "69"
return ''
endfunction
function! s:keys4left(char, pair, info, opts) "{{{1
if !a:opts.autoclose
echom 31
3DMDebug "31"
return ''
endif
let exprs = a:opts.smart_pairs_base + a:opts.smart_pairs_extra
if a:opts.smart_pairs && s:any_is_true(exprs, a:info, a:opts)
echom 32
3DMDebug "32"
return ''
endif
if a:opts.balance_pairs && s:balance_pairs(a:pair, a:info, a:opts) < 0
echom 33
3DMDebug "33"
return ''
endif
let eol_marker = a:opts.insert_eol_marker == 1 && empty(a:info.cur.ahead) ? a:opts.eol_marker . "\<C-G>U\<Left>" : ''
echom 34
3DMDebug "34"
return strcharpart(a:pair, 1, 1) . eol_marker . "\<C-G>U\<Left>"
endfunction
function! s:keys4right(char, pair, info, opts) "{{{1
if a:opts.jump_expansion
echom 40
3DMDebug "40"
let around = matchstr(a:info.cur.prev_line, "\\S$") . matchstr(a:info.cur.next_line, "^\\s*\zs\\S")
if empty(a:info.cur.ahead) && a:char ==# matchstr(a:info.cur.next_line, "^\\s*\\zs\\S")
let rights = strchars(matchstr(a:info.cur.next_line, '^\s*')) + 2
echom "40.1"
3DMDebug "40.1"
return "\<Esc>s" . repeat("\<Right>", rights)
endif
endif
if !a:opts.autoclose
if s:info.cur.around == a:pair
echom 41
3DMDebug "41"
return "\<Del>"
elseif s:info.cur.p_char == strcharpart(a:pair, 0, 1)
echom 42
3DMDebug "42"
return "\<C-G>U\<Left>"
endif
echom 43
3DMDebug "43"
return ""
endif
if strcharpart(a:info.cur.line[a:info.cur.col - 1 :], 0, 1) ==# a:char
echom 44
3DMDebug "44"
return "\<Del>"
endif
if a:opts.expand_space && a:opts.jump_expansion
\ && matchstr(a:info.cur.ahead, '^ ['.escape(a:char, '\^[]').']') ==# ' ' . a:char
echom 45
3DMDebug "45"
return "\<Del>\<Del>\<C-G>U\<Left> \<C-G>U\<Right>"
endif
echom 49
3DMDebug "49"
return ''
endfunction
function! s:keys4quote(char, info, opts) "{{{1
let quotes_behind = strchars(matchstr(a:info.cur.behind, '['.escape(a:char, '\^[]').']*$'))
let quotes_ahead = strchars(matchstr(a:info.cur.ahead, '^['.escape(a:char, '\^[]').']*'))
echom 'k4q: ' quotes_behind . ' - ' . quotes_ahead
echom string(a:opts.nesting_quotes)
3DMDebug quotes_behind . ' - ' . quotes_ahead
3DMDebug string(a:opts.nesting_quotes)
if a:opts.autoclose && index(a:opts.nesting_quotes, a:char) >= 0
\&& quotes_behind > 1
let add2right = quotes_ahead > quotes_behind + 1 ? 0 : quotes_behind - quotes_ahead + 1
echom 51
echom add2right
3DMDebug "51"
3DMDebug add2right
return repeat(a:char, add2right) . repeat("\<C-G>U\<Left>", add2right)
endif
if a:info.cur.n_char ==# a:char
echom 53
3DMDebug "53"
return "\<Del>"
endif
if a:info.is_ignored_syn
@@ -391,18 +402,18 @@ function! s:keys4quote(char, info, opts) "{{{1
let exprs = a:opts.smart_quotes_base + a:opts.smart_quotes_extra
if a:opts.autoclose && a:opts.smart_quotes
\&& s:any_is_true(exprs, a:info, a:opts)
echom 52
3DMDebug "52"
return ''
endif
if !a:opts.autoclose && quotes_behind
echom 54
3DMDebug "54"
return "\<Left>"
endif
if !a:opts.autoclose
echom 55
3DMDebug "55"
return ''
endif
echom 59
3DMDebug "59"
return a:char . "\<C-G>U\<Left>"
endfunction
@@ -411,17 +422,17 @@ function! s:keys4cr(info, opts) "{{{1
\&& !empty(filter(copy(a:opts.pairs), 'v:val ==# a:info.prev.around'))
\|| (a:opts.expand_cr == 2 && !empty(filter(copy(a:opts.pairs), 'strcharpart(v:val, 1, 1) == a:info.cur.n_char')))
" Empty pair
echom 71
3DMDebug "71"
let eol_marker = a:opts.insert_eol_marker == 2 && strchars(a:info.cur.ahead) == 1 ? a:opts.eol_marker : ''
return "0\<C-D>\<Del>x\<C-G>U\<Left>\<BS>\<CR>" . a:info.cur.n_char . "\<Del>" . eol_marker . "\<Up>\<End>\<CR>"
endif
if a:opts.expand_cr && a:opts.expand_inside_quotes
\&& !empty(filter(copy(a:opts.quotes), 'v:val.v:val ==# a:info.prev.around'))
" Empty pair
echom 72
3DMDebug "72"
return "\<Up>\<End>\<CR>"
endif
echom 79
3DMDebug "79"
return ''
endfunction
" vim: sw=2 et