Skip to content

Commit 566fa06

Browse files
committed
Merge pull request rust-lang#16 from richo/playpen-support
Add rudimentary support for the playpen
2 parents fa1bccc + 0b75330 commit 566fa06

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

autoload/rust.vim

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,56 @@ function! s:system(pwd, cmd)
359359
return system(cmd)
360360
endfunction
361361

362+
" Playpen Support {{{1
363+
" Parts of gist.vim by Yasuhiro Matsumoto <[email protected]> reused
364+
" gist.vim available under the BSD license, available at
365+
" http://github.com/mattn/gist-vim
366+
function! s:has_webapi()
367+
if !exists("*webapi#http#post")
368+
try
369+
call webapi#http#post()
370+
catch
371+
endtry
372+
endif
373+
return exists("*webapi#http#post")
374+
endfunction
375+
376+
function! rust#Play(count, line1, line2, ...) abort
377+
redraw
378+
379+
let l:rust_playpen_url = get(g:, 'rust_playpen_url', 'https://play.rust-lang.org/')
380+
let l:rust_shortener_url = get(g:, 'rust_shortener_url', 'https://is.gd/')
381+
382+
if !s:has_webapi()
383+
echohl ErrorMsg | echomsg ':RustPlay depends on webapi.vim (https://github.com/mattn/webapi-vim)' | echohl None
384+
return
385+
endif
386+
387+
let bufname = bufname('%')
388+
if a:count < 1
389+
let content = join(getline(a:line1, a:line2), "\n")
390+
else
391+
let save_regcont = @"
392+
let save_regtype = getregtype('"')
393+
silent! normal! gvy
394+
let content = @"
395+
call setreg('"', save_regcont, save_regtype)
396+
endif
397+
398+
let body = l:rust_playpen_url."?code=".webapi#http#encodeURI(content)
399+
400+
if strlen(body) > 5000
401+
echohl ErrorMsg | echomsg 'Buffer too large, max 5000 encoded characters ('.strlen(body).')' | echohl None
402+
return
403+
endif
404+
405+
let payload = "format=simple&url=".webapi#http#encodeURI(body)
406+
let res = webapi#http#post(l:rust_shortener_url.'create.php', payload, {})
407+
let url = res.content
408+
409+
redraw | echomsg 'Done: '.url
410+
endfunction
411+
362412
" }}}1
363413

364414
" vim: set noet sw=4 ts=4:

doc/rust.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ g:cargo_manifest_name~
9595
let g:cargo_manifest_name = 'Cargo.toml'
9696
<
9797

98+
*g:rust_playpen_url*
99+
g:rust_playpen_url~
100+
Set this option to override the url for the playpen to use: >
101+
let g:rust_playpen_url = 'https://play.rust-lang.org/'
102+
<
103+
104+
*g:rust_shortener_url*
105+
g:rust_shortener_url~
106+
Set this option to override the url for the url shortener: >
107+
let g:rust_shortener_url = 'https://is.gd/'
108+
<
109+
110+
98111
==============================================================================
99112
COMMANDS *rust-commands*
100113

@@ -157,6 +170,20 @@ COMMANDS *rust-commands*
157170
If |g:rustc_path| is defined, it is used as the path to rustc.
158171
Otherwise it is assumed rustc can be found in $PATH.
159172

173+
:RustPlay *:RustPlay*
174+
This command will only work if you have web-api.vim installed
175+
(available at https://github.com/mattn/webapi-vim). It sends the
176+
current selection, or if nothing is selected, the entirety of the
177+
current buffer to the Rust playpen, and emits a message with the
178+
shortened URL to the playpen.
179+
180+
|g:rust_playpen_url| is the base URL to the playpen, by default
181+
"https://play.rust-lang.org/".
182+
183+
|g:rust_shortener_url| is the base url for the shorterner, by
184+
default "https://is.gd/"
185+
186+
160187
==============================================================================
161188
MAPPINGS *rust-mappings*
162189

ftplugin/rust.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ command! -nargs=* -buffer RustEmitIr call rust#Emit("llvm-ir", <q-args>)
106106
" See |:RustEmitAsm| for docs
107107
command! -nargs=* -buffer RustEmitAsm call rust#Emit("asm", <q-args>)
108108

109+
" See |:RustPlay| for docs
110+
command! -range=% RustPlay :call rust#Play(<count>, <line1>, <line2>, <f-args>)
111+
109112
" Mappings {{{1
110113

111114
" Bind ⌘R in MacVim to :RustRun
@@ -142,6 +145,7 @@ let b:undo_ftplugin = "
142145
\|delcommand RustExpand
143146
\|delcommand RustEmitIr
144147
\|delcommand RustEmitAsm
148+
\|delcommand RustPlay
145149
\|nunmap <buffer> <D-r>
146150
\|nunmap <buffer> <D-R>
147151
\|nunmap <buffer> [[

0 commit comments

Comments
 (0)