Skip to content

Commit

Permalink
Integrate changes from downstream vim
Browse files Browse the repository at this point in the history
  • Loading branch information
Original User committed May 3, 2017
1 parent 732b5fc commit b5d4cc2
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 160 deletions.
309 changes: 285 additions & 24 deletions after/syntax/rust.vim

Large diffs are not rendered by default.

83 changes: 42 additions & 41 deletions autoload/rust.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
" Author: Kevin Ballard
" Description: Helper functions for Rust commands/mappings
" Last Modified: May 27, 2014
" For bugs, patches and license go to https://github.com/rust-lang/rust.vim

" Jump {{{1

Expand Down Expand Up @@ -364,51 +365,51 @@ endfunction
" gist.vim available under the BSD license, available at
" http://github.com/mattn/gist-vim
function! s:has_webapi()
if !exists("*webapi#http#post")
try
call webapi#http#post()
catch
endtry
endif
return exists("*webapi#http#post")
if !exists("*webapi#http#post")
try
call webapi#http#post()
catch
endtry
endif
return exists("*webapi#http#post")
endfunction

function! rust#Play(count, line1, line2, ...) abort
redraw

let l:rust_playpen_url = get(g:, 'rust_playpen_url', 'https://play.rust-lang.org/')
let l:rust_shortener_url = get(g:, 'rust_shortener_url', 'https://is.gd/')

if !s:has_webapi()
echohl ErrorMsg | echomsg ':RustPlay depends on webapi.vim (https://github.com/mattn/webapi-vim)' | echohl None
return
endif

let bufname = bufname('%')
if a:count < 1
let content = join(getline(a:line1, a:line2), "\n")
else
let save_regcont = @"
let save_regtype = getregtype('"')
silent! normal! gvy
let content = @"
call setreg('"', save_regcont, save_regtype)
endif

let body = l:rust_playpen_url."?code=".webapi#http#encodeURI(content)

if strlen(body) > 5000
echohl ErrorMsg | echomsg 'Buffer too large, max 5000 encoded characters ('.strlen(body).')' | echohl None
return
endif

let payload = "format=simple&url=".webapi#http#encodeURI(body)
let res = webapi#http#post(l:rust_shortener_url.'create.php', payload, {})
let url = res.content

redraw | echomsg 'Done: '.url
redraw

let l:rust_playpen_url = get(g:, 'rust_playpen_url', 'https://play.rust-lang.org/')
let l:rust_shortener_url = get(g:, 'rust_shortener_url', 'https://is.gd/')

if !s:has_webapi()
echohl ErrorMsg | echomsg ':RustPlay depends on webapi.vim (https://github.com/mattn/webapi-vim)' | echohl None
return
endif

let bufname = bufname('%')
if a:count < 1
let content = join(getline(a:line1, a:line2), "\n")
else
let save_regcont = @"
let save_regtype = getregtype('"')
silent! normal! gvy
let content = @"
call setreg('"', save_regcont, save_regtype)
endif

let body = l:rust_playpen_url."?code=".webapi#http#encodeURI(content)

if strlen(body) > 5000
echohl ErrorMsg | echomsg 'Buffer too large, max 5000 encoded characters ('.strlen(body).')' | echohl None
return
endif

let payload = "format=simple&url=".webapi#http#encodeURI(body)
let res = webapi#http#post(l:rust_shortener_url.'create.php', payload, {})
let url = res.content

redraw | echomsg 'Done: '.url
endfunction

" }}}1

" vim: set noet sw=4 ts=4:
" vim: set noet sw=8 ts=8:
141 changes: 71 additions & 70 deletions autoload/rustfmt.vim
Original file line number Diff line number Diff line change
@@ -1,106 +1,107 @@
" Author: Stephen Sugden <stephen@stephensugden.com>
"
" Adapted from https://github.com/fatih/vim-go
" For bugs, patches and license go to https://github.com/rust-lang/rust.vim

if !exists("g:rustfmt_autosave")
let g:rustfmt_autosave = 0
let g:rustfmt_autosave = 0
endif

if !exists("g:rustfmt_command")
let g:rustfmt_command = "rustfmt"
let g:rustfmt_command = "rustfmt"
endif

if !exists("g:rustfmt_options")
let g:rustfmt_options = ""
let g:rustfmt_options = ""
endif

if !exists("g:rustfmt_fail_silently")
let g:rustfmt_fail_silently = 0
let g:rustfmt_fail_silently = 0
endif

let s:got_fmt_error = 0

function! s:RustfmtCommandRange(filename, line1, line2)
let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]}
return printf("%s %s --write-mode=overwrite --file-lines '[%s]'", g:rustfmt_command, g:rustfmt_options, json_encode(l:arg))
let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]}
return printf("%s %s --write-mode=overwrite --file-lines '[%s]'", g:rustfmt_command, g:rustfmt_options, json_encode(l:arg))
endfunction

function! s:RustfmtCommand(filename)
return g:rustfmt_command . " --write-mode=overwrite " . g:rustfmt_options . " " . shellescape(a:filename)
return g:rustfmt_command . " --write-mode=overwrite " . g:rustfmt_options . " " . shellescape(a:filename)
endfunction

function! s:RunRustfmt(command, curw, tmpname)
if exists("*systemlist")
let out = systemlist(a:command)
else
let out = split(system(a:command), '\r\?\n')
endif

if v:shell_error == 0 || v:shell_error == 3
" remove undo point caused via BufWritePre
try | silent undojoin | catch | endtry

" Replace current file with temp file, then reload buffer
call rename(a:tmpname, expand('%'))
silent edit!
let &syntax = &syntax

" only clear location list if it was previously filled to prevent
" clobbering other additions
if s:got_fmt_error
let s:got_fmt_error = 0
call setloclist(0, [])
lwindow
endif
elseif g:rustfmt_fail_silently == 0
" otherwise get the errors and put them in the location list
let errors = []

for line in out
" src/lib.rs:13:5: 13:10 error: expected `,`, or `}`, found `value`
let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\):\s*\(\d\+:\d\+\s*\)\?\s*error: \(.*\)')
if !empty(tokens)
call add(errors, {"filename": @%,
\"lnum": tokens[2],
\"col": tokens[3],
\"text": tokens[5]})
endif
endfor

if empty(errors)
% | " Couldn't detect rustfmt error format, output errors
endif

if !empty(errors)
call setloclist(0, errors, 'r')
echohl Error | echomsg "rustfmt returned error" | echohl None
endif

let s:got_fmt_error = 1
lwindow
" We didn't use the temp file, so clean up
call delete(a:tmpname)
endif

call winrestview(a:curw)
if exists("*systemlist")
let out = systemlist(a:command)
else
let out = split(system(a:command), '\r\?\n')
endif

if v:shell_error == 0 || v:shell_error == 3
" remove undo point caused via BufWritePre
try | silent undojoin | catch | endtry

" Replace current file with temp file, then reload buffer
call rename(a:tmpname, expand('%'))
silent edit!
let &syntax = &syntax

" only clear location list if it was previously filled to prevent
" clobbering other additions
if s:got_fmt_error
let s:got_fmt_error = 0
call setloclist(0, [])
lwindow
endif
elseif g:rustfmt_fail_silently == 0
" otherwise get the errors and put them in the location list
let errors = []

for line in out
" src/lib.rs:13:5: 13:10 error: expected `,`, or `}`, found `value`
let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\):\s*\(\d\+:\d\+\s*\)\?\s*error: \(.*\)')
if !empty(tokens)
call add(errors, {"filename": @%,
\"lnum": tokens[2],
\"col": tokens[3],
\"text": tokens[5]})
endif
endfor

if empty(errors)
% | " Couldn't detect rustfmt error format, output errors
endif

if !empty(errors)
call setloclist(0, errors, 'r')
echohl Error | echomsg "rustfmt returned error" | echohl None
endif

let s:got_fmt_error = 1
lwindow
" We didn't use the temp file, so clean up
call delete(a:tmpname)
endif

call winrestview(a:curw)
endfunction

function! rustfmt#FormatRange(line1, line2)
let l:curw = winsaveview()
let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
call writefile(getline(1, '$'), l:tmpname)
let l:curw = winsaveview()
let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
call writefile(getline(1, '$'), l:tmpname)

let command = s:RustfmtCommandRange(l:tmpname, a:line1, a:line2)
let command = s:RustfmtCommandRange(l:tmpname, a:line1, a:line2)

call s:RunRustfmt(command, l:curw, l:tmpname)
call s:RunRustfmt(command, l:curw, l:tmpname)
endfunction

function! rustfmt#Format()
let l:curw = winsaveview()
let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
call writefile(getline(1, '$'), l:tmpname)
let l:curw = winsaveview()
let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
call writefile(getline(1, '$'), l:tmpname)

let command = s:RustfmtCommand(l:tmpname)
let command = s:RustfmtCommand(l:tmpname)

call s:RunRustfmt(command, l:curw, l:tmpname)
call s:RunRustfmt(command, l:curw, l:tmpname)
endfunction
15 changes: 11 additions & 4 deletions compiler/cargo.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
" Compiler: Cargo Compiler
" Maintainer: Damien Radtke <damienradtke@gmail.com>
" Latest Revision: 2014 Sep 24
" For bugs, patches and license go to https://github.com/rust-lang/rust.vim

if exists('current_compiler')
finish
finish
endif
runtime compiler/rustc.vim
let current_compiler = "cargo"

let s:save_cpo = &cpo
set cpo&vim

if exists(':CompilerSet') != 2
command -nargs=* CompilerSet setlocal <args>
command -nargs=* CompilerSet setlocal <args>
endif

if exists('g:cargo_makeprg_params')
execute 'CompilerSet makeprg=cargo\ '.escape(g:cargo_makeprg_params, ' \|"').'\ $*'
execute 'CompilerSet makeprg=cargo\ '.escape(g:cargo_makeprg_params, ' \|"').'\ $*'
else
CompilerSet makeprg=cargo\ $*
CompilerSet makeprg=cargo\ $*
endif

" Ignore general cargo progress messages
Expand All @@ -26,3 +30,6 @@ CompilerSet errorformat+=
\%-G%\\s%#Finished%.%#,
\%-G%\\s%#error:\ Could\ not\ compile\ %.%#,
\%-G%\\s%#To\ learn\ more\\,%.%#

let &cpo = s:save_cpo
unlet s:save_cpo
3 changes: 2 additions & 1 deletion compiler/rustc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
" Compiler: Rust Compiler
" Maintainer: Chris Morgan <me@chrismorgan.info>
" Latest Revision: 2013 Jul 12
" For bugs, patches and license go to https://github.com/rust-lang/rust.vim

if exists("current_compiler")
finish
finish
endif
let current_compiler = "rustc"

Expand Down
4 changes: 2 additions & 2 deletions doc/rust.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*rust.txt* Filetype plugin for Rust
*ft_rust.txt* Filetype plugin for Rust

==============================================================================
CONTENTS *rust* *ft-rust*
CONTENTS *rust*

1. Introduction |rust-intro|
2. Settings |rust-settings|
Expand Down
16 changes: 3 additions & 13 deletions ftplugin/rust.vim
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
" Language: Rust
" Description: Vim syntax file for Rust
" Description: Vim ftplugin for Rust
" Maintainer: Chris Morgan <me@chrismorgan.info>
" Maintainer: Kevin Ballard <kevin@sb.org>
" Last Change: June 08, 2016
" For bugs, patches and license go to https://github.com/rust-lang/rust.vim

if exists("b:did_ftplugin")
finish
Expand Down Expand Up @@ -179,7 +180,6 @@ let b:undo_ftplugin = "
\|ounmap <buffer> [[
\|ounmap <buffer> ]]
\|set matchpairs-=<:>
\|unlet b:match_skip
\"

" }}}1
Expand All @@ -191,17 +191,7 @@ endif

augroup END

" %-matching. <:> is handy for generics.
set matchpairs+=<:>
" There are two minor issues with it; (a) comparison operators in expressions,
" where a less-than may match a greater-than later on—this is deemed a trivial
" issue—and (b) `Fn() -> X` syntax. This latter issue is irremediable from the
" highlighting perspective (built into Vim), but the actual % functionality
" can be fixed by this use of matchit.vim.
let b:match_skip = 's:comment\|string\|rustArrow'
source $VIMRUNTIME/macros/matchit.vim

let &cpo = s:save_cpo
unlet s:save_cpo

" vim: set noet sw=4 ts=4:
" vim: set noet sw=8 ts=8:
Loading

0 comments on commit b5d4cc2

Please sign in to comment.