forked from fables-tales/rubyfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rubyfmt.vim
78 lines (62 loc) · 1.82 KB
/
rubyfmt.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
let s:cpo_save = &cpo
set cpo&vim
let g:rubyfmt_path = '/Users/penelope/dev/rubyfmt/target/release/rubyfmt-main'
function! rubyfmt#format() abort
let l:bin_args = [g:rubyfmt_path, '-i']
let l:curw = winsaveview()
let l:tmpname = "/tmp/bees.rb"
call writefile(rubyfmt#get_lines(), l:tmpname)
let current_col = col('.')
let [l:out, l:err] = rubyfmt#run(l:bin_args, l:tmpname, expand('%'))
let line_offset = len(readfile(l:tmpname)) - line('$')
let l:orig_line = getline('.')
if l:err == 0
call rubyfmt#update_file(l:tmpname, expand('%'))
else
endif
call delete(l:tmpname)
call winrestview(l:curw)
let l:lineno = ('.') + line_offset
call cursor(l:lineno, current_col + (len(getline(l:lineno)) - len(l:orig_line)))
syntax sync fromstart
endfunction
function! rubyfmt#update_file(source, target) abort
try | silent undojoin | catch | endtry
let old_fileformat = &fileformat
if exists("*getfperm")
let original_fperm = getfperm(a:target)
endif
call rename(a:source, a:target)
if exists("*setfperm") && original_fperm != ''
call setfperm(a:target, original_fperm)
endif
silent edit!
let &fileformat = old_fileformat
let &syntax = &syntax
endfunction
function! rubyfmt#run(bin_args, source, target) abort
if a:source == ''
return
endif
if a:target == ''
return
endif
let s:res = system(join(a:bin_args + [a:source], " "))
return [s:res, v:shell_error]
endfunction
function! rubyfmt#get_lines() abort
let buf = getline(1, '$')
return buf
endfunction
function! rubyfmt#exec(cmd, ...) abort
if len(a:cmd) == 0
endfunction
function! rubyfmt#show_errors(errors) abort
echom a:errors
endfunction
if !exists("s:rubyfmt_ac_set")
let s:rubyfmt_ac_set=1
autocmd FileType ruby autocmd! BufWritePre <buffer> call rubyfmt#format()
endif
let &cpo = s:cpo_save
unlet s:cpo_save