Skip to content

Commit 1273f94

Browse files
lilyballalexcrichton
authored andcommitted
Add commands :RustEmitIr and :RustEmitAsm
1 parent 918eda5 commit 1273f94

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/etc/vim/autoload/rust.vim

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,39 @@ function! rust#CompleteExpand(lead, line, pos)
130130
return glob(escape(a:lead, "*?[") . '*', 0, 1)
131131
endfunction
132132

133+
" Emit {{{1
134+
135+
function! rust#Emit(type, args)
136+
call s:WithPath(function("s:Emit"), a:type, a:args)
137+
endfunction
138+
139+
function! s:Emit(path, type, args)
140+
try
141+
let rustc = exists("g:rustc_path") ? g:rustc_path : "rustc"
142+
143+
let args = [a:path, '--emit', a:type, '-o', '-'] + a:args
144+
let output = system(shellescape(rustc) . " " . join(map(args, "shellescape(v:val)")))
145+
if v:shell_error
146+
echohl WarningMsg
147+
echo output
148+
echohl None
149+
else
150+
new
151+
silent put =output
152+
1
153+
d
154+
if a:type == "ir"
155+
setl filetype=llvm
156+
elseif a:type == "asm"
157+
setl filetype=asm
158+
endif
159+
setl buftype=nofile
160+
setl bufhidden=hide
161+
setl noswapfile
162+
endif
163+
endtry
164+
endfunction
165+
133166
" Utility functions {{{1
134167

135168
function! s:WithPath(func, ...)

src/etc/vim/doc/rust.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ COMMANDS *rust-commands*
107107
If |g:rustc_path| is defined, it is used as the path to rustc.
108108
Otherwise it is assumed rustc can be found in $PATH.
109109

110+
:RustEmitIr [args] *:RustEmitIr*
111+
Compiles the current file to LLVM IR and displays the results
112+
in a new split. If the current file has unsaved changes, it
113+
will be saved first using |:update|. If the current file is an
114+
unnamed buffer, it will be written to a temporary file first.
115+
116+
The arguments given to |:RustEmitIr| will be passed to rustc.
117+
118+
If |g:rustc_path| is defined, it is used as the path to rustc.
119+
Otherwise it is assumed rustc can be found in $PATH.
120+
121+
:RustEmitAsm [args] *:RustEmitAsm*
122+
Compiles the current file to assembly and displays the results
123+
in a new split. If the current file has unsaved changes, it
124+
will be saved first using |:update|. If the current file is an
125+
unnamed buffer, it will be written to a temporary file first.
126+
127+
The arguments given to |:RustEmitAsm| will be passed to rustc.
128+
129+
If |g:rustc_path| is defined, it is used as the path to rustc.
130+
Otherwise it is assumed rustc can be found in $PATH.
110131

111132
==============================================================================
112133
MAPPINGS *rust-mappings*

src/etc/vim/ftplugin/rust.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ command! -nargs=* -complete=file -bang -bar -buffer RustRun call rust#Run(<bang>
6666
" See |:RustExpand| for docs
6767
command! -nargs=* -complete=customlist,rust#CompleteExpand -bang -bar -buffer RustExpand call rust#Expand(<bang>0, [<f-args>])
6868

69+
" See |:RustEmitIr| for docs
70+
command! -nargs=* -bar -buffer RustEmitIr call rust#Emit("ir", [<f-args>])
71+
72+
" See |:RustEmitAsm| for docs
73+
command! -nargs=* -bar -buffer RustEmitAsm call rust#Emit("asm", [<f-args>])
74+
6975
" Mappings {{{1
7076

7177
" Bind ⌘R in MacVim to :RustRun
@@ -91,6 +97,8 @@ let b:undo_ftplugin = "
9197
\|unlet! b:rust_last_rustc_args b:rust_last_args
9298
\|delcommand RustRun
9399
\|delcommand RustExpand
100+
\|delcommand RustEmitIr
101+
\|delcommand RustEmitAsm
94102
\|nunmap <buffer> <D-r>
95103
\|nunmap <buffer> <D-R>
96104
\|nunmap <buffer> [[

0 commit comments

Comments
 (0)