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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
" Erlang refactor file
" Language: Erlang
" Maintainer: Pawel 'kTT' Salata <rockplayer.pl@gmail.com>
" URL: http://ktototaki.info
if exists("b:did_ftplugin_erlang")
finish
endif
" Don't load any other
let b:did_ftplugin_erlang=1
if !exists('g:erlangRefactoring') || g:erlangRefactoring == 0
finish
endif
if !exists('g:erlangWranglerPath')
let g:erlangWranglerPath = '/usr/share/wrangler/'
endif
if glob(g:erlangWranglerPath) == ""
call confirm("Wrong path to wrangler dir")
finish
endif
autocmd VimLeavePre * call StopWranglerServer()
let s:erlangServerName = "wrangler_vim"
" Starting background erlang session with wrangler on
function! StartWranglerServer()
let wranglerEbinDir = g:erlangWranglerPath . "/ebin"
let command = "erl_call -s -sname " . s:erlangServerName . " -x 'erl -pa " . wranglerEbinDir . "'"
call system(command)
call s:send_rpc('application', 'start', '[wrangler_app]')
endfunction
" Stopping erlang session
function! StopWranglerServer()
echo s:send_rpc('erlang', 'halt', '')
endfunction
" Sending rpc call to erlang session
function! s:send_rpc(module, fun, args)
let command = "erl_call -sname " . s:erlangServerName . " -a '" . a:module . " " . a:fun . " " . a:args . "'"
let result = system(command)
if match(result, 'erl_call: failed to connect to node .*') != -1
call StartWranglerServer()
return system(command)
endif
return result
endfunction
function! ErlangUndo()
echo s:send_rpc("wrangler_undo_server", "undo", "[]")
:e!
endfunction
function! s:trim(text)
return substitute(a:text, "^\\s\\+\\|\\s\\+$", "", "g")
endfunction
function! s:get_msg(result, tuple_start)
let msg_begin = '{' . a:tuple_start . ','
let matching_start = match(a:result, msg_begin)
if matching_start != -1
return s:trim(matchstr(a:result, '[^}]*', matching_start + strlen(msg_begin)))
endif
return ""
endfunction
" Check if there is an error in result
function! s:check_for_error(result)
let msg = s:get_msg(a:result, 'ok')
if msg != ""
return [0, msg]
endif
let msg = s:get_msg(a:result, 'warning')
if msg != ""
return [1, msg]
endif
let msg = s:get_msg(a:result, 'error')
if msg != ""
return [2, msg]
endif
return [-1, ""]
endfunction
" Sending apply changes to file
function! s:send_confirm()
let choice = confirm("What do you want?", "&Preview\n&Confirm\nCa&ncel", 0)
if choice == 1
echo "TODO: Display preview :)"
elseif choice == 2
let module = 'wrangler_preview_server'
let fun = 'commit'
let args = '[]'
return s:send_rpc(module, fun, args)
else
let module = 'wrangler_preview_server'
let fun = 'abort'
let args = '[]'
return s:send_rpc(module, fun, args)
echo "Canceled"
endif
endfunction
" Manually send confirm, for testing purpose only
function! SendConfirm()
echo s:send_confirm()
endfunction
" Format and send function extracton call
function! s:call_extract(start_line, start_col, end_line, end_col, name)
let file = expand("%:p")
let module = 'wrangler'
let fun = 'fun_extraction'
let args = '["' . file . '", {' . a:start_line . ', ' . a:start_col . '}, {' . a:end_line . ', ' . a:end_col . '}, "' . a:name . '", ' . &sw . ']'
let result = s:send_rpc(module, fun, args)
let [error_code, msg] = s:check_for_error(result)
if error_code != 0
call confirm(msg)
return 0
endif
echo "This files will be changed: " . matchstr(msg, "[^]]*", 1)
echo s:send_confirm()
return 1
endfunction
function! ErlangExtractFunction(mode) range
silent w!
let name = inputdialog("New function name: ")
if name != ""
if a:mode == "v"
let start_pos = getpos("'<")
let start_line = start_pos[1]
let start_col = start_pos[2]
let end_pos = getpos("'>")
let end_line = end_pos[1]
let end_col = end_pos[2]
elseif a:mode == "n"
let pos = getpos(".")
let start_line = pos[1]
let start_col = pos[2]
let end_line = pos[1]
let end_col = pos[2]
else
echo "Mode not supported."
return
endif
if s:call_extract(start_line, start_col, end_line, end_col, name)
let temp = &autoread
set autoread
:e
if temp == 0
set noautoread
endif
endif
else
echo "Empty function name. Ignoring."
endif
endfunction
nmap <A-r>e :call ErlangExtractFunction("n")<ENTER>
vmap <A-r>e :call ErlangExtractFunction("v")<ENTER>
function! s:call_rename(mode, line, col, name, search_path)
let file = expand("%:p")
let module = 'wrangler'
let fun = 'rename_' . a:mode
let args = '["' . file .'", '
if a:mode != "mod"
let args = args . a:line . ', ' . a:col . ', '
endif
let args = args . '"' . a:name . '", ["' . a:search_path . '"], ' . &sw . ']'
let result = s:send_rpc(module, fun, args)
let [error_code, msg] = s:check_for_error(result)
if error_code != 0
call confirm(msg)
return 0
endif
echo "This files will be changed: " . matchstr(msg, "[^]]*", 1)
echo s:send_confirm()
return 1
endfunction
function! ErlangRename(mode)
silent w!
if a:mode == "mod"
let name = inputdialog('Rename module to: ')
else
let name = inputdialog('Rename "' . expand("<cword>") . '" to: ')
endif
if name != ""
let search_path = expand("%:p:h")
"let search_path = inputdialog('Search path: ', expand("%:p:h"))
let pos = getpos(".")
let line = pos[1]
let col = pos[2]
let current_filename = expand("%")
let current_filepath = expand("%:p")
let new_filename = name . '.erl'
if s:call_rename(a:mode, line, col, name, search_path)
if a:mode == "mod"
execute ':bd ' . current_filename
execute ':e ' . new_filename
silent execute '!mv ' . current_filepath . ' ' . current_filepath . '.bak'
redraw!
else
let temp = &autoread
set autoread
:e
if temp == 0
set noautoread
endif
endif
endif
else
echo "Empty name. Ignoring."
endif
endfunction
function! ErlangRenameFunction()
call ErlangRename("fun")
endfunction
map <A-r>f :call ErlangRenameFunction()<ENTER>
function! ErlangRenameVariable()
call ErlangRename("var")
endfunction
map <A-r>v :call ErlangRenameVariable()<ENTER>
function! ErlangRenameModule()
call ErlangRename("mod")
endfunction
map <A-r>m :call ErlangRenameModule()<ENTER>
function! ErlangRenameProcess()
call ErlangRename("process")
endfunction
map <A-r>p :call ErlangRenameProcess()<ENTER>
function! s:call_tuple_fun_args(start_line, start_col, end_line, end_col, search_path)
let file = expand("%:p")
let module = 'wrangler'
let fun = 'tuple_funpar'
let args = '["' . file . '", {' . a:start_line . ', ' . a:start_col . '}, {' . a:end_line . ', ' . a:end_col . '}, ["' . a:search_path . '"], ' . &sw . ']'
let result = s:send_rpc(module, fun, args)
if s:check_for_error(result)
return 0
endif
call s:send_confirm()
return 1
endfunction
function! ErlangTupleFunArgs(mode)
silent w!
let search_path = expand("%:p:h")
"let search_path = inputdialog('Search path: ', expand("%:p:h"))
if a:mode == "v"
let start_pos = getpos("'<")
let start_line = start_pos[1]
let start_col = start_pos[2]
let end_pos = getpos("'>")
let end_line = end_pos[1]
let end_col = end_pos[2]
if s:call_tuple_fun_args(start_line, start_col, end_line, end_col, search_path)
let temp = &autoread
set autoread
:e
if temp == 0
set noautoread
endif
endif
elseif a:mode == "n"
let pos = getpos(".")
let line = pos[1]
let col = pos[2]
if s:call_tuple_fun_args(line, col, line, col, search_path)
let temp = &autoread
set autoread
:e
if temp == 0
set noautoread
endif
endif
else
echo "Mode not supported."
endif
endfunction
nmap <A-r>t :call ErlangTupleFunArgs("n")<ENTER>
vmap <A-r>t :call ErlangTupleFunArgs("v")<ENTER>
" vim: set foldmethod=marker:
|