1" Author: Stephen Sugden <stephen@stephensugden.com>
2"
3" Adapted from https://github.com/fatih/vim-go
4" For bugs, patches and license go to https://github.com/rust-lang/rust.vim
5
6if !exists("g:rustfmt_autosave")
7	let g:rustfmt_autosave = 0
8endif
9
10if !exists("g:rustfmt_command")
11	let g:rustfmt_command = "rustfmt"
12endif
13
14if !exists("g:rustfmt_options")
15	let g:rustfmt_options = ""
16endif
17
18if !exists("g:rustfmt_fail_silently")
19	let g:rustfmt_fail_silently = 0
20endif
21
22let s:got_fmt_error = 0
23
24function! s:RustfmtCommandRange(filename, line1, line2)
25	let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]}
26	return printf("%s %s --write-mode=overwrite --file-lines '[%s]'", g:rustfmt_command, g:rustfmt_options, json_encode(l:arg))
27endfunction
28
29function! s:RustfmtCommand(filename)
30	return g:rustfmt_command . " --write-mode=overwrite " . g:rustfmt_options . " " . shellescape(a:filename)
31endfunction
32
33function! s:RunRustfmt(command, curw, tmpname)
34	if exists("*systemlist")
35		let out = systemlist(a:command)
36	else
37		let out = split(system(a:command), '\r\?\n')
38	endif
39
40	if v:shell_error == 0 || v:shell_error == 3
41		" remove undo point caused via BufWritePre
42		try | silent undojoin | catch | endtry
43
44		" Replace current file with temp file, then reload buffer
45		call rename(a:tmpname, expand('%'))
46		silent edit!
47		let &syntax = &syntax
48
49		" only clear location list if it was previously filled to prevent
50		" clobbering other additions
51		if s:got_fmt_error
52			let s:got_fmt_error = 0
53			call setloclist(0, [])
54			lwindow
55		endif
56	elseif g:rustfmt_fail_silently == 0
57		" otherwise get the errors and put them in the location list
58		let errors = []
59
60		for line in out
61			" src/lib.rs:13:5: 13:10 error: expected `,`, or `}`, found `value`
62			let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\):\s*\(\d\+:\d\+\s*\)\?\s*error: \(.*\)')
63			if !empty(tokens)
64				call add(errors, {"filename": @%,
65						 \"lnum":     tokens[2],
66						 \"col":      tokens[3],
67						 \"text":     tokens[5]})
68			endif
69		endfor
70
71		if empty(errors)
72			% | " Couldn't detect rustfmt error format, output errors
73		endif
74
75		if !empty(errors)
76			call setloclist(0, errors, 'r')
77			echohl Error | echomsg "rustfmt returned error" | echohl None
78		endif
79
80		let s:got_fmt_error = 1
81		lwindow
82		" We didn't use the temp file, so clean up
83		call delete(a:tmpname)
84	endif
85
86	call winrestview(a:curw)
87endfunction
88
89function! rustfmt#FormatRange(line1, line2)
90	let l:curw = winsaveview()
91	let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
92	call writefile(getline(1, '$'), l:tmpname)
93
94	let command = s:RustfmtCommandRange(l:tmpname, a:line1, a:line2)
95
96	call s:RunRustfmt(command, l:curw, l:tmpname)
97endfunction
98
99function! rustfmt#Format()
100	let l:curw = winsaveview()
101	let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
102	call writefile(getline(1, '$'), l:tmpname)
103
104	let command = s:RustfmtCommand(l:tmpname)
105
106	call s:RunRustfmt(command, l:curw, l:tmpname)
107endfunction
108