Skip to content

Commit

Permalink
Fix get_current_top_object to support folded codes #295
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Dec 3, 2020
1 parent e3083be commit a45e9f1
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions autoload/iced/paredit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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, ')')
Expand All @@ -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
Expand Down

0 comments on commit a45e9f1

Please sign in to comment.