From 19fdb7e535c57b495c2bc9bb121c7d83b59c0f9b Mon Sep 17 00:00:00 2001 From: Kana Natsuno Date: Sat, 23 Sep 2017 10:07:52 +0900 Subject: [PATCH 1/3] Write tests --- t/keep-marks.vim | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 t/keep-marks.vim diff --git a/t/keep-marks.vim b/t/keep-marks.vim new file mode 100644 index 0000000..2f21b42 --- /dev/null +++ b/t/keep-marks.vim @@ -0,0 +1,72 @@ +call textobj#user#plugin('line', { +\ '-': { +\ 'select-a': 'al', +\ 'select-a-function': 'SelectA', +\ 'select': 'il', +\ 'pattern': '^\s*\zs.\{-}\ze\s*$', +\ }, +\ }) + +function! SelectA() + if empty(getline('.')) + return 0 + endif + + normal! 0 + let head_pos = getpos('.') + + normal! $ + let tail_pos = getpos('.') + + return ['v', head_pos, tail_pos] +endfunction + +describe 'Custom text object' + before + new + + silent 1 put! =[ + \ 'if (!foo) {', + \ ' bar = ''baz''', + \ ' qux()', + \ '}', + \ ] + let @0 = '*nothing yanked*' + execute 'normal!' "1G2|vj\" + end + + after + close! + end + + context 'defined by a function' + it 'keeps ''< and ''> marks' + Expect @0 ==# '*nothing yanked*' + Expect [line("'<"), col("'<")] == [1, 2] + Expect [line("'>"), col("'>")] == [2, 2] + + normal 3Gyal + + Expect @0 ==# ' qux()' + Expect [line("'<"), col("'<")] == [1, 2] + Expect [line("'>"), col("'>")] == [2, 2] + end + end + + context 'defined by a pattern' + it 'keeps ''< and ''> marks' + Expect @0 ==# '*nothing yanked*' + Expect [line("'<"), col("'<")] == [1, 2] + Expect [line("'>"), col("'>")] == [2, 2] + + normal 3Gyil + + Expect @0 ==# 'qux()' + Expect [line("'<"), col("'<")] == [1, 2] + Expect [line("'>"), col("'>")] == [2, 2] + end + end +end + +" __END__ "{{{1 +" vim: foldmethod=marker From eb9870596136dd10d3d1ccd59127fcba9be9de6f Mon Sep 17 00:00:00 2001 From: Kana Natsuno Date: Sat, 23 Sep 2017 10:30:07 +0900 Subject: [PATCH 2/3] Keep built-in marks before and after selection --- autoload/textobj/user.vim | 41 +++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/autoload/textobj/user.vim b/autoload/textobj/user.vim index bf21064..b6f530f 100644 --- a/autoload/textobj/user.vim +++ b/autoload/textobj/user.vim @@ -443,29 +443,41 @@ endfunction function! s:plugin.define_interface_key_mappings() "{{{3 let RHS_PATTERN = - \ ':call g:__textobj_' . self.name . '.do_by_pattern(' + \ '%s' + \ . ':call g:__textobj_' . self.name . '.do_by_pattern(' \ . '"%s",' \ . '"%s",' \ . '""' \ . ')' + \ . '%s' let RHS_FUNCTION = - \ ':call g:__textobj_' . self.name . '.do_by_function(' + \ '%s' + \ . ':call g:__textobj_' . self.name . '.do_by_function(' \ . '"%s",' \ . '"%s",' \ . '""' \ . ')' + \ . '%s' for [obj_name, specs] in items(self.obj_specs) for spec_name in filter(keys(specs), 's:is_ui_property_name(v:val)') + if spec_name =~# '^move' + let save = '' + let restore = '' + else " spec_name =~# '^select' + let save = '(save-marks)' + let restore = '(restore-marks)' + endif + " lhs - let lhs = ' ' . self.interface_mapping_name(obj_name, spec_name) + let lhs = self.interface_mapping_name(obj_name, spec_name) " rhs let _ = spec_name . '-function' if has_key(specs, _) - let rhs = printf(RHS_FUNCTION, spec_name, obj_name) + let rhs = printf(RHS_FUNCTION, save, spec_name, obj_name, restore) elseif has_key(specs, 'pattern') - let rhs = printf(RHS_PATTERN, spec_name, obj_name) + let rhs = printf(RHS_PATTERN, save, spec_name, obj_name, restore) else " skip to allow to define user's own {rhs} of the interface mapping. continue @@ -477,7 +489,7 @@ function! s:plugin.define_interface_key_mappings() "{{{3 else " spec_name =~# '^select' let MapFunction = function('s:objnoremap') endif - call MapFunction(1, lhs, rhs) + call MapFunction(1, '