From a45e9f1513a76cd782c2b2448c5aaeadd51d677a Mon Sep 17 00:00:00 2001 From: liquidz Date: Thu, 3 Dec 2020 06:26:10 +0900 Subject: [PATCH] Fix get_current_top_object to support folded codes #295 --- autoload/iced/paredit.vim | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/autoload/iced/paredit.vim b/autoload/iced/paredit.vim index 97be2da7d..bba86343b 100644 --- a/autoload/iced/paredit.vim +++ b/autoload/iced/paredit.vim @@ -96,6 +96,21 @@ function! s:is_in_range(current_pos, start_pos, end_pos) abort \ || (a:start_pos[1] <= a:current_pos[1] && a:current_pos[1] <= a:end_pos[1]) endfunction +" NOTE: Use `getline` instead of visual mode and yank +" because visual mode and yank cannot support folded texts +" c.f. https://github.com/liquidz/vim-iced/issues/295 +function! s:getrange(start_pos, end_pos) abort + let start_lnum = a:start_pos[1] + let end_lnum = a:end_pos[1] + let len = end_lnum - start_lnum + + let lines = getline(start_lnum, end_lnum) + let lines[0] = strpart(lines[0], a:start_pos[2] - 1) + let lines[len] = strpart(lines[len], 0, a:end_pos[2]) + + return join(lines, "\n") +endfunction + function! iced#paredit#get_current_top_object_raw(...) abort let open_char = get(a:, 1, '(') let close_char = get(a:, 2, ')') @@ -118,15 +133,11 @@ function! iced#paredit#get_current_top_object_raw(...) abort let end_pos = getcurpos() if s:is_in_range(pos, start_pos, end_pos) - " select end_pos to start_pos - call setpos('.', end_pos) - silent exe 'normal! v' - call setpos('.', start_pos) " NOTE: `0` is to wrap top level tag literal - silent exe 'normal! 0y' - + let start_pos_head = copy(start_pos) + let start_pos_head[2] = 0 let result = { - \ 'code': @@, + \ 'code': s:getrange(start_pos_head, end_pos), \ 'curpos': start_pos, \ } endif