From 7809c5e1716b1a04ef33286f0a43762b914def61 Mon Sep 17 00:00:00 2001 From: Israel Chauca Fuentes Date: Sat, 11 Feb 2017 11:11:17 -0500 Subject: [PATCH] Add expand_cr --- autoload/delimitMate.vim | 53 +++++++++++++++++++++++++++++++++++----- test/expand_cr.vim | 28 +++++++++------------ 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/autoload/delimitMate.vim b/autoload/delimitMate.vim index 75a84d7..c7a8816 100644 --- a/autoload/delimitMate.vim +++ b/autoload/delimitMate.vim @@ -5,6 +5,7 @@ let s:defaults.delimitMate_quotes = ['"', "'", '`'] let s:defaults.delimitMate_enabled = 1 let s:defaults.delimitMate_autoclose = 1 let s:defaults.delimitMate_expand_space = 0 +let s:defaults.delimitMate_expand_cr = 0 let s:defaults.delimitMate_jump_expansion = 0 let s:defaults.delimitMate_expand_inside_quotes = 0 let s:defaults.delimitMate_smart_pairs = 1 @@ -104,6 +105,8 @@ function! s:get_info(...) "{{{1 let d.line = getline('.') let d.col = col('.') let d.lnum = line('.') + let d.prev_line = line('.') == 1 ? '' : getline(line('.') - 1) + let d.next_line = line('.') == line('$') ? '' : getline(line('.') + 1) endif let d.ahead = len(d.line) >= d.col ? d.line[d.col - 1 : ] : '' let d.behind = d.col >= 2 ? d.line[: d.col - 2] : '' @@ -168,29 +171,53 @@ function! delimitMate#TextChangedI(...) "{{{1 echom 21 return endif - if s:info.cur.lnum != s:info.prev.lnum + if s:info.cur.lnum == s:info.prev.lnum + 1 + \&& s:info.prev.behind ==# s:info.cur.prev_line + \&& s:info.prev.ahead ==# s:info.cur.ahead + \&& s:info.cur.behind =~ '^\s*$' + " CR echom 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 + if s:option('expand_cr') && !empty(pair) + echom "23.1" + return feedkeys("\") + 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" + return feedkeys("\") + endif + return + endif + if s:info.cur.lnum != s:info.prev.lnum + echom 24 return endif if s:info.prev.col - s:info.cur.col != len(s:info.prev.p_char) - echom 23 + echom 25 return endif if len(s:info.prev.line) == len(s:info.cur.line) - echom 24 + echom 26 return endif echom 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 25 + echom 27 return feedkeys("\", '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 26 + echom 28 return endif echom 29 @@ -233,8 +260,10 @@ function! s:handle_vchar(str) "{{{1 echom 13 let keys = s:keys4space(s:info, opts) elseif a:str == "\" + let prev_line = line('.') == 1 ? '' : getline(line('.') - 1) + let next_line = line('.') == line('$') ? '' : getline(line('.') + 1) echom 14 - return 0 + let keys = s:keys4cr(prev_line, next_line, s:info, opts) elseif !empty(filter(copy(opts.quotes), 'v:val ==# a:str')) echom 15 let keys = s:keys4quote(a:str, s:info, opts) @@ -352,6 +381,18 @@ function! s:keys4quote(char, info, opts) "{{{1 endfunction function! s:keys4cr(info, opts) "{{{1 + if a:opts.expand_cr + \&& !empty(filter(copy(a:opts.pairs), 'v:val ==# a:info.prev.around')) + " Empty pair + echom 71 + return "\\\" + 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 + return "\\\" + endif echom 79 return '' endfunction diff --git a/test/expand_cr.vim b/test/expand_cr.vim index 14748b5..2c3c0d4 100644 --- a/test/expand_cr.vim +++ b/test/expand_cr.vim @@ -14,27 +14,25 @@ call vimtest#StartTap() call vimtap#Plan(25) let g:delimitMate_expand_cr = 1 -DelimitMateReload "let g:delimitMate_eol_marker = ';' filetype indent on set bs=2 et sts=4 sw=4 ft=javascript call DMTest_single('$(document).ready(function() {})', - \ "\31|i\x\", + \ "31|i\x", \ ["$(document).ready(function() {", " x", "})"]) " Issue #95 new let b:delimitMate_jump_expansion = 1 -DelimitMateReload -call DMTest_single('', "(\test)x", +call DMTest_single('', "i(\test)x", \ ['(', 'test', ')x']) " Remove CR expansion on BS -call DMTest_single('', "(\\x", +call DMTest_single('', "i(\\x", \ ['(x)']) " Consider indentation with BS inside an empty CR expansion. -call DMTest_single('', "( \\\x", '(x)') +call DMTest_single('', "i( \\\x", '(x)') " Conflict with indentation settings (cindent). Issue #95 se cindent @@ -44,7 +42,7 @@ call DMTest_single( \ ' ', \ ' }', \ '}'], - \ "\3G8|a}x", + \ "i\3G8|a}x", \ ['sub foo {', \ ' while (1) {', \ ' ', @@ -62,33 +60,31 @@ set foldmethod=marker set foldmarker={,} set foldlevel=0 set backspace=2 -call DMTest_single('', "abc {\x", +call DMTest_single('', "iabc {\x", \['abc {', \ ' x', \ '}']) " expand_cr != 2 -call DMTest_single('abc(def)', "\$i\x", +call DMTest_single('abc(def)', "i\$i\x", \ ['abc(def', \ ' x)']) " expand_cr == 2 let delimitMate_expand_cr = 2 -DelimitMateReload -call DMTest_single('abc(def)', "\$i\x", ['abc(def', ' x', ' )']) +call DMTest_single('abc(def)', "i\$i\x", ['abc(def', ' x', ' )']) " Play nice with smartindent set all& set smartindent -call DMTest_single('', "\$i{\x", ['{', ' x', '}']) +call DMTest_single('', "i\$i{\x", ['{', ' x', '}']) -call DMTest_quotes('', "' x", "' x'") +call DMTest_quotes('', "i' x", "' x'") -call DMTest_quotes('', "'\x", ["'", "x'"]) +call DMTest_quotes('', "i'\x", ["'", "x'"]) let delimitMate_expand_inside_quotes = 1 -DelimitMateReload -call DMTest_quotes('', "'\x", ["'", "x", "'"]) +call DMTest_quotes('', "i'\x", ["'", "x", "'"]) call vimtest#Quit()