-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
rename.vim
140 lines (124 loc) · 4.27 KB
/
rename.vim
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
" rename.vim: Implement :GoRename.
" TODO: There is command-line support for 'gopls rename'
"
" $ gopls rename './site.go:#1029' XXX
" admin.go:
" package foo
" [..]
" init.go:
" package foo
" [..]
"
" Can also use -w to write immediately:
"
" $ gopls rename -w './site.go:#1029' XXX
" /home/martin/code/goatcounter/admin.go
" /home/martin/code/goatcounter/init.go
" /home/martin/code/goatcounter/site.go
" /home/martin/code/goatcounter/site_test.go
" /home/martin/code/goatcounter/test.go
" /home/martin/code/goatcounter/user.go
"
" Note: will clobber work tree with .orig files.
" Limitation: only current package, not e.g. ./handlers subpackage.
"
" Commandline completion: original, unexported camelCase, and exported
" CamelCase.
fun! gopher#rename#complete(lead, cmdline, cursor) abort
let l:word = expand('<cword>')
return gopher#compl#filter(a:lead, uniq(sort([l:word, s:unexport(l:word), s:export(l:word)])))
endfun
" Rename the identifier under the cursor to the identifier in the first
" argument.
fun! gopher#rename#do(...) abort
if !gopher#go#in_gopath()
return gopher#error(
\ 'gorename does not work with modules yet. https://github.com/golang/go/issues/27571')
endif
" No argument; try to make a sane decision:
" - ALLCAPS -> Allcaps
" - snake_case -> snakeCase (Convert snake_case while keeping export status)
" - Snake_case -> SnakeCase
" - Otherwise toggle export status.
if a:0 is 0
let l:to = gopher#rename#_auto_to_(expand('<cword>'))
else
let l:to = a:1
endif
call gopher#buf#write_all()
"call setqflist([])
" Make sure the buffer can't be modified since gorename will write stuff to
" disk, and overwrite the user's changes.
" Set this for *all* buffers since gorename can modify multiple files.
call gopher#buf#doall('set nomodifiable')
let l:autoread = &autoread
set autoread
try
call gopher#system#tool_job({e, o -> s:done(l:e, l:o, l:autoread)}, [
\ 'gorename',
\ '-to', l:to,
\ '-offset', gopher#buf#cursor(1)
\ ] + gopher#go#add_build_tags(get(g:, 'gopher_gorename_flags', [])))
catch
" Just so we don't leave the buffer in nomod state on errors, and it doesn't
" hurt to do twice.
call gopher#buf#doall('set modifiable')
let &autoread = l:autoread
endtry
endfun
fun! s:done(exit, out, autoread) abort
checktime
let &autoread = a:autoread
call gopher#buf#doall('set modifiable')
if a:exit > 0
return gopher#rename#_errors_(a:out)
endif
call gopher#info(a:out)
endfun
" Display an error.
fun! gopher#rename#_errors_(out) abort
if a:out =~# '": no identifier at this position'
return gopher#error('gorename: no identifier at this position')
endif
if a:out =~# ': renaming this.*\(would conflict\|conflicts with\)'
" Remove all instances of the file path in case of multi-line messages, which
" look like:
" /home/martin/go/src/a/a.go:7:6: renaming this func "v" to "b"
" /home/martin/go/src/a/a.go:8:6: conflicts with func in same block
let l:path = matchstr(a:out, '^.\{-}:')
let l:pat = gopher#str#escape(l:path) . '\d\+:\d\+: '
let l:out = a:out[:len(l:path)-1] . substitute(a:out[len(l:path):], l:pat, '', '')
let l:out = substitute(l:out, '\s\+', ' ', 'g')
return gopher#error(printf('gorename: %s', l:out))
endif
" Probably compile errors.
for l:err in split(a:out, "\n")
" Not a very useful line to add.
if l:err =~# "^gorename: couldn't load packages due to errors:"
continue
endif
call gopher#error('gorename: %s', l:err)
endfor
endfun
fun! gopher#rename#_auto_to_(w) abort
if a:w =~# '^\u\+$'
return a:w[0] . tolower(a:w[1:])
elseif a:w =~# '_'
return a:w =~# '^\u' ? s:export(a:w) : s:unexport(a:w)
else
return a:w =~# '^\u' ? s:unexport(a:w) : s:export(a:w)
endif
endfun
" Copied from tpope/vim-abolish.
fun! s:unexport(word) abort
let l:word = substitute(a:word, '-', '_', 'g')
if l:word !~# '_' && l:word =~# '\l'
return substitute(l:word, '^.', '\l&', '')
else
return substitute(l:word, '\C\(_\)\=\(.\)', '\=submatch(1)==""?tolower(submatch(2)) : toupper(submatch(2))','g')
endif
endfun
fun! s:export(word) abort
let l:word = s:unexport(a:word)
return toupper(l:word[0]) . l:word[1:]
endfun