Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Added a feature to use visual placeholders. #40

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion autoload/minisnip.vim
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ function! s:SelectPlaceholder() abort
let @s=substitute(@s, '\V' . g:minisnip_finalenddelim, '', '')
catch /E486:/
" There's no placeholder at all, enter insert mode
call feedkeys('i', 'n')
call feedkeys('A', 'n')
return
finally
let &wrapscan = l:ws
unlet! s:minisnip_selected_text
endtry
finally
let &wrapscan = l:ws
Expand All @@ -143,6 +144,20 @@ function! s:SelectPlaceholder() abort
let l:skip = 0
endif

" is this placeholder marked as 'visual'?
if @s =~ '\V\^' . g:minisnip_visualmarker
" remove the marker
let @s=substitute(@s, '\V\^' . g:minisnip_visualmarker, '', '')
if exists('s:minisnip_selected_text')
" get the text
let @s=s:minisnip_selected_text
endif
let l:visualmarker = 1
let l:skip = 1
else
let l:visualmarker = 0
endif

" is this placeholder marked as 'evaluate'?
if @s =~ '\V\^' . g:minisnip_evalmarker
" remove the marker
Expand All @@ -163,6 +178,10 @@ function! s:SelectPlaceholder() abort
call feedkeys(col("'>") - l:slen >= col('$') - 1 ? 'a' : 'i', 'n')
elseif l:skip == 1
normal! gv"sp
if l:visualmarker == 1
" if the text came by replacing a visual marker, indent the same
normal! gv=`]
endif
let @s = l:old_s
call s:SelectPlaceholder()
else
Expand All @@ -174,6 +193,14 @@ function! s:SelectPlaceholder() abort
let @s = l:old_s
endfunction

" function to get the selected text in a variable
function! minisnip#CopyVisualSelection() abort
execute "'<,'>d"
let s:minisnip_selected_text = substitute(@", '\n\+$', '', '')
execute "normal! O\<Space>\<BS>\<Esc>"
startinsert!
endfunction

function! minisnip#complete() abort
" Locate the start of the word
let l:line = getline('.')
Expand Down
27 changes: 27 additions & 0 deletions doc/minisnip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,33 @@ of how it can be used: >
This will automatically fill the `#define` line with the value entered on the
`#ifndef` line upon jumping to it.

-------------------------------------------------------------------------------
*'g:minisnip_visualmarker'*
Default: '~#'

Marks a "visual" placeholder that gets replaced with the last visually
selected text; after selecting the text, |'g:minisnip_trigger'| needs to be
pressed before starting out with triggering a snippet.
Here is an example of how it can be used: >
if (cond) {
{{+~#+}}
}

Say the following text has to be surrounded by the above `if` snippet:
x = 10;
y = 20;

First select the text and press `Tab` (or whatever is the value of
|'g:minisnip_trigger'|), then trigger the `if` snippet as usual. Once the
control reaches the visual placeholder, the output will be the following:
if (cond) {
x = 10;
y = 20;
}

In case there is no text that was copied from the visual mode previously,
the placeholder will simply land you to the insert mode.

-------------------------------------------------------------------------------
*'g:minisnip_finalstartdelim'*
*'g:minisnip_finalenddelim'*
Expand Down
6 changes: 6 additions & 0 deletions plugin/minisnip.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let g:minisnip_enddelim = get(g:, 'minisnip_enddelim', '+}}')
let g:minisnip_finalstartdelim = get(g:, 'minisnip_finalstartdelim', '{{-')
let g:minisnip_finalenddelim = get(g:, 'minisnip_finalenddelim', '-}}')
let g:minisnip_evalmarker = get(g:, 'minisnip_evalmarker', '~')
let g:minisnip_visualmarker = get(g:, 'minisnip_visualmarker', '~#')
let g:minisnip_donotskipmarker = get(g:, 'minisnip_donotskipmarker', '`')
let g:minisnip_backrefmarker = get(g:, 'minisnip_backrefmarker', '\\~')

Expand All @@ -33,12 +34,17 @@ snoremap <script> <expr> <Plug>(minisnip) minisnip#ShouldTrigger() ?
\"\<esc>:call \minisnip#Minisnip()\<cr>" :
\eval('"' . escape(g:minisnip_trigger, '\"<') . '"')
inoremap <silent> <Plug>(minisnip-complete) <C-r>=minisnip#complete()<CR>
xnoremap <silent> <Plug>(minisnip-visual) :<C-u>call minisnip#CopyVisualSelection()<CR>

" add the default mappings if the user hasn't defined any
if !hasmapto('<Plug>(minisnip)')
execute 'imap <unique> ' . g:minisnip_trigger . ' <Plug>(minisnip)'
execute 'smap <unique> ' . g:minisnip_trigger . ' <Plug>(minisnip)'
endif
" visual selection
if !hasmapto('<Plug>(minisnip-visual)')
execute 'xmap <unique> ' . g:minisnip_trigger . ' <Plug>(minisnip-visual)'
endif
" Completion
if !hasmapto('<Plug>(minisnip-complete)')
imap <C-x><C-t> <Plug>(minisnip-complete)
Expand Down