1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
" Mappings for changing the case type of a text object. These mappings are invoked with <C-c>.
"
" This way, to change the case format of a word to snake_case, one can run:
"
" <C-c>siw
"
" the format of this command is
"
" <C-c>[j]<target_casefmt><text_object>
"
" if the j is included the text object is joined using the case format.
"
" valid case formats are
"
" s - snake_case
" S - Upper_Snake_Case
" c - camelCase
" C - UpperCamelCase
" k - KONSTANT_CASE
" K - KONSTANT CASE (toupper(title case))
" t - title case
" T - Title Case
" - - dashed-case
if ! exists('g:casefmt_include_bindings')
let g:casefmt_include_bindings = 1
endif
if ! exists('g:casefmt_leader')
let g:casefmt_leader = '<C-c>'
endif
let s:case_fmts = {
\ 's': 'to_snake',
\ '_': 'to_snake',
\ 'S': 'to_Snake',
\ 'c': 'to_camel',
\ 'C': 'to_Camel',
\ 'k': 'to_CONSTANT',
\ 'K': 'to_CONSTANT_space',
\ 't': 'to_title',
\ 'T': 'to_Title',
\ '-': 'to_dashed'
\ }
function! CaseFmt_AddFormat(key, funcname) abort
exec printf("noremap <silent> <Plug>(casefmt-leader-no-set)%s "
\ . ":\<C-u>let g:CaseFmtFunction=function(\"\<SID>%s\")<cr>"
\ . ":\<C-u>set operatorfunc=\<SID>casefmt_do<cr>g@", a:key, a:funcname)
exec printf("vnoremap <silent> <Plug>(casefmt-leader-no-set)%s "
\ . ":\<C-u>let g:CaseFmtFunction=function(\"\<SID>%s\")<cr>"
\ . ":\<C-u>call \<SID>casefmt_do(visualmode(), 1)<cr>", a:key, a:funcname)
endfunction
nmap <silent> <Plug>(casefmt-leader) :<C-u>let g:CaseFmtProcessor=function("<SID>casefmt_default_processor")<cr><Plug>(casefmt-leader-no-set)
vmap <silent> <Plug>(casefmt-leader) :<C-u>let g:CaseFmtProcessor=function("<SID>casefmt_default_processor")<cr>gv<Plug>(casefmt-leader-no-set)
for [k, v] in items(s:case_fmts)
call CaseFmt_AddFormat(k, v)
endfor
if g:casefmt_include_bindings
exec printf("nmap <silent> %s <Plug>(casefmt-leader)", g:casefmt_leader)
exec printf("vmap <silent> %s <Plug>(casefmt-leader)", g:casefmt_leader)
endif
nmap <silent> <Plug>(casefmt-leader)j :<C-u>let g:CaseFmtProcessor=function("<SID>casefmt_joiner")<cr><Plug>(casefmt-leader-no-set)
vmap <silent> <Plug>(casefmt-leader)j :<C-u>let g:CaseFmtProcessor=function("<SID>casefmt_joiner")<cr>gv<Plug>(casefmt-leader-no-set)
function! s:casefmt_do(type, ...) abort
if a:0
silent exe "norm! gvy"
elseif a:type == 'line'
" yank the text described by the motion
silent exe "norm! '[V']y"
else
silent exe "norm! `[v`]y"
endif
let yanked = getreg('"', 1, v:true)
let yankedtype = getregtype('"')
if !exists('g:CaseFmtProcessor')
let g:CaseFmtProcessor = function("\<SID>casefmt_default_processor")
endif
let changed = g:CaseFmtProcessor(yanked, yankedtype)
call setreg('"', changed, yankedtype)
norm gvp
" Reset the yanked text to what it was originally.
call setreg('"', yanked, yankedtype)
endfunction
" Default processor. Calls change case fmt on each word.
function! s:casefmt_default_processor(yanked, type)
let changed = []
for n in a:yanked
let split = split(n, '\<\|\>') " Split by word boundaries
let new_split = []
for s in split " Iterate by words.
if s =~ "[a-zA-Z_0-9]*" " Is s an identifier?
call add(new_split, g:CaseFmtFunction(s:normalize(s)))
else
call add(new_split, s)
endif
endfor
call add(changed, join(new_split, ''))
endfor
return changed
endfunction
function! s:casefmt_joiner(yanked, type) abort
let changed = []
for n in a:yanked
call add(changed, g:CaseFmtFunction(tolower(substitute(n, '\s\+', '_', 'g'))))
endfor
return changed
endfunction
" snake_case is the platonic form that all other forms build from. This function
" tries to be SmartAboutDetecting_the current case format and converting it
" properly.
function! s:normalize(s) abort
if a:s =~ '[a-z][A-Z]'
" a:s has a camel case boundary in it.
return tolower(substitute(a:s, '\%([a-zA-Z]\)\zs\ze\([A-Z0-9]\)', '_', "g"))
endif
return tolower(a:s)
endfunction
function! s:to_snake(s) abort
return a:s
endfunction
function! s:to_Snake(s) abort
return substitute(a:s, '\%(_\|\<\)\zs\w\ze', '\u\0', 'g')
endfunction
function! s:to_camel(s) abort
return substitute(a:s, '_\([a-z]\)', '\u\1', "g")
endfunction
function! s:to_Camel(s) abort
return substitute(s:to_camel(a:s), '\<\w', '\u\0', 'g')
endfunction
function! s:to_CONSTANT(s) abort
return toupper(a:s)
endfunction
function! s:to_CONSTANT_space(s) abort
return toupper(s:to_title(a:s))
endfunction
function! s:to_title(s) abort
return substitute(a:s, '_', ' ', 'g')
endfunction
function! s:to_Title(s) abort
return substitute(s:to_title(a:s), '\<[a-z]', '\u\0', 'g')
endfunction
function! s:to_dashed(s) abort
return substitute(a:s, '_', '-', 'g')
endfunction
|