CamelCase Expansion in Vim like Intellij Idea? -
in intellij idea, there's feature. let's have used variable mycamelcase
somewhere in code. if type mcc
, press ctrl-enter or such key combination, expands mycamelcase
. there similar in vim?
okay, forgive me answering twice, since first attempt missed point, i'll have go. more complicated thought, possibly not complicated have made (!).
this modified suggest matching variable names.
first of all, here's function generate 'mcc' abbreviation 'mycamelcase' string:
function! camel_initials(camel) let first_char = matchstr(a:camel,"^.") let other_char = substitute(a:camel,"\\u","","g") return first_char . other_char endfunction
now, here's function takes abbreviation ('mcc') , scans current buffer (backwards current line) "words" have abbreviation. a list of matches returned:
function! expand_camel_initials(abbrev) let winview=winsaveview() let candidate=a:abbrev let matches=[] try let resline = line(".") while resline >= 1 let sstr = '\<' . matchstr(a:abbrev,"^.") . '[a-za-z]*\>' keepjumps let resline=search(sstr,"bw") let candidate=expand("<cword>") if candidate != a:abbrev && camel_initials(candidate) == a:abbrev call add( matches, candidate ) endif endwhile call winrestview(winview) if len(matches) == 0 echo "no expansion found" endif return sort(candidate) endtry endfunction
next, here's custom-completion function reads word under cursor , suggests matches returned above functions:
function! camel_complete( findstart, base ) if a:findstart let line = getline('.') let start = col('.') - 1 while start > 0 && line[start - 1] =~ '[a-za-z_]' let start -= 1 endwhile return start else return expand_camel_initials( a:base ) endif endfunction
to make use of this, must define "completefunc":
setlocal completefunc=camel_complete
to use insert-mode completion, type ctrl-x ctrl-u, map ctrl-l:
inoremap <c-l> <c-x><c-u>
with code in vimrc should find typing mcc
followed ctrl-l make expected replacement. if no matching expansion found, abbreviation unchanged.
the code isn't water-tight, works in simple cases tested. hope helps. let me know if needs elucidating.
Comments
Post a Comment