Created
January 2, 2018 23:55
-
-
Save PeterRincker/0b237accaf97b22705170b2df83ac6a0 to your computer and use it in GitHub Desktop.
Mimic Rails.vim's :Railsabbrev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Mimic part of Rails.vim's :Railsabbrev | |
" aka Abbreviations that will only expand on <tab> or <c-]> | |
" | |
" Example usage: | |
" :Railsabbrev iff if:<left> | |
" :Railsabbrev fun function!\ ()<cr>endfunction<up><end><left><left> | |
" Now you can type "iff<tab>" and it will expand to: "if:" with the cursor before the ":" | |
" | |
" Supports completion menu of :Railsabbrev via <Plug>(Railsabbrev-complete) mapping | |
" e.g. | |
" imap <c-x><c-x><c-a> <Plug>(Railsabbrev-complete) | |
function! RailsExpand(root, good, ...) | |
let c = nr2char(getchar(0)) | |
if c == "" || c =~ (a:0 ? a:1 : "\t") | |
return a:good | |
else | |
return a:root . c | |
endif | |
endfunction | |
let g:railsabbrev = [] | |
function! s:railsabbrev(root, good) abort | |
call add(g:railsabbrev, {'word': a:root, 'menu': a:good}) | |
let good = substitute(a:good, '[\"|]', '\\&', "g") | |
let good = substitute(good, '<', '\\<lt>', "g") | |
let root = substitute(a:root, '[\"|]', '\\&', "g") | |
let root = substitute(root, '<', '\\<lt>', "g") | |
execute "iabbr <buffer> " . a:root . " <c-r>=RailsExpand(\"" . root . "\", \"" . good . "\")<cr>" | |
endfunction | |
command! -nargs=* Railsabbrev call <SID>railsabbrev(<f-args>) | |
function! s:railsabbrev_complete() abort | |
let col = col('.') | |
let prefix = matchstr(getline('.')[0:col-2], '\k\+$') | |
let lst = get(g:, 'railsabbrev', []) | |
if len(prefix) | |
let lst = filter(lst, 'len(prefix) <= len(v:val.word) && v:val.word[0:len(prefix)-1] == prefix') | |
let col = col - len(prefix) | |
endif | |
call complete(col, lst) | |
return '' | |
endfunction | |
inoremap <script> <Plug>(Railsabbrev-complete) <c-r>=<SID>railsabbrev_complete()<cr> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment