annotate runtime/autoload/rustfmt.vim @ 18486:9d887cad7315

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