diff options
author | Adam Stankiewicz <sheerun@sher.pl> | 2018-10-08 19:00:59 +0200 |
---|---|---|
committer | Adam Stankiewicz <sheerun@sher.pl> | 2018-10-08 19:00:59 +0200 |
commit | fd74d8b2b170b540680a9bbf6c64990f8ebafd08 (patch) | |
tree | b1fdef6203a78a21053d1b8e0666ab7a38c36df2 /autoload/rust.vim | |
parent | 055f7710b65dfa2df52fc0b5be2486ae36ac5751 (diff) | |
download | vim-polyglot-3.3.3.tar.gz vim-polyglot-3.3.3.zip |
Updatev3.3.3
Diffstat (limited to 'autoload/rust.vim')
-rw-r--r-- | autoload/rust.vim | 91 |
1 files changed, 76 insertions, 15 deletions
diff --git a/autoload/rust.vim b/autoload/rust.vim index 7e766da1..77c38b7c 100644 --- a/autoload/rust.vim +++ b/autoload/rust.vim @@ -86,7 +86,7 @@ function! s:Run(dict, rustc_args, args) let pwd = a:dict.istemp ? a:dict.tmpdir : '' let output = s:system(pwd, shellescape(rustc) . " " . join(map(rustc_args, 'shellescape(v:val)'))) - if output != '' + if output !=# '' echohl WarningMsg echo output echohl None @@ -153,7 +153,7 @@ function! s:Expand(dict, pretty, args) endfunction function! rust#CompleteExpand(lead, line, pos) - if a:line[: a:pos-1] =~ '^RustExpand!\s*\S*$' + if a:line[: a:pos-1] =~# '^RustExpand!\s*\S*$' " first argument and it has a ! let list = ["normal", "expanded", "typed", "expanded,identified", "flowgraph=", "everybody_loops"] if !empty(a:lead) @@ -182,7 +182,7 @@ function! s:Emit(dict, type, args) let args = [relpath, '--emit', a:type, '-o', output_path] + a:args let pwd = a:dict.istemp ? a:dict.tmpdir : '' let output = s:system(pwd, shellescape(rustc) . " " . join(map(args, 'shellescape(v:val)'))) - if output != '' + if output !=# '' echohl WarningMsg echo output echohl None @@ -192,10 +192,10 @@ function! s:Emit(dict, type, args) exe 'silent keepalt read' fnameescape(output_path) 1 d - if a:type == "llvm-ir" + if a:type ==# "llvm-ir" setl filetype=llvm let extension = 'll' - elseif a:type == "asm" + elseif a:type ==# "asm" setl filetype=asm let extension = 's' endif @@ -261,8 +261,8 @@ function! s:WithPath(func, ...) let dict.tmpdir_relpath = filename let dict.path = dict.tmpdir.'/'.filename - let saved.mod = &mod - set nomod + let saved.mod = &modified + set nomodified silent exe 'keepalt write! ' . fnameescape(dict.path) if pathisempty @@ -343,7 +343,7 @@ function! s:ShellTokenize(text) endif let l:state = 3 elseif l:state == 5 " single-quoted - if l:c == "'" + if l:c ==# "'" let l:state = 1 else let l:current .= l:c @@ -361,7 +361,7 @@ function! s:RmDir(path) if empty(a:path) echoerr 'Attempted to delete empty path' return 0 - elseif a:path == '/' || a:path == $HOME + elseif a:path ==# '/' || a:path ==# $HOME echoerr 'Attempted to delete protected path: ' . a:path return 0 endif @@ -414,22 +414,83 @@ function! rust#Play(count, line1, line2, ...) abort call setreg('"', save_regcont, save_regtype) endif - let body = l:rust_playpen_url."?code=".webapi#http#encodeURI(content) + let url = 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 + if strlen(url) > 5000 + echohl ErrorMsg | echomsg 'Buffer too large, max 5000 encoded characters ('.strlen(url).')' | echohl None return endif - let payload = "format=simple&url=".webapi#http#encodeURI(body) + let payload = "format=simple&url=".webapi#http#encodeURI(url) let res = webapi#http#post(l:rust_shortener_url.'create.php', payload, {}) - let url = res.content + if res.status[0] ==# '2' + let url = res.content + endif + let footer = '' if exists('g:rust_clip_command') call system(g:rust_clip_command, url) + if !v:shell_error + let footer = ' (copied to clipboard)' + endif endif + redraw | echomsg 'Done: '.url.footer +endfunction - redraw | echomsg 'Done: '.url +" Run a test under the cursor or all tests {{{1 + +" Finds a test function name under the cursor. Returns empty string when a +" test function is not found. +function! s:SearchTestFunctionNameUnderCursor() abort + let cursor_line = line('.') + + " Find #[test] attribute + if search('#\[test]', 'bcW') is 0 + return '' + endif + + " Move to an opening brace of the test function + let test_func_line = search('^\s*fn\s\+\h\w*\s*(.\+{$', 'eW') + if test_func_line is 0 + return '' + endif + + " Search the end of test function (closing brace) to ensure that the + " cursor position is within function definition + normal! % + if line('.') < cursor_line + return '' + endif + + return matchstr(getline(test_func_line), '^\s*fn\s\+\zs\h\w*') +endfunction + +function! rust#Test(all, options) abort + let pwd = expand('%:p:h') + if findfile('Cargo.toml', pwd . ';') ==# '' + return rust#Run(1, '--test ' . a:options) + endif + + let pwd = shellescape(pwd) + + if a:all + execute '!cd ' . pwd . ' && cargo test ' . a:options + return + endif + + let saved = getpos('.') + try + let func_name = s:SearchTestFunctionNameUnderCursor() + if func_name ==# '' + echohl ErrorMsg + echo 'No test function was found under the cursor. Please add ! to command if you want to run all tests' + echohl None + return + endif + execute '!cd ' . pwd . ' && cargo test ' . func_name . ' ' . a:options + finally + call setpos('.', saved) + endtry endfunction " }}}1 |