diff options
Diffstat (limited to '')
-rw-r--r-- | autoload/cargo.vim | 116 | ||||
-rw-r--r-- | autoload/cargo/quickfix.vim | 30 |
2 files changed, 89 insertions, 57 deletions
diff --git a/autoload/cargo.vim b/autoload/cargo.vim index e92da22b..d1547fc9 100644 --- a/autoload/cargo.vim +++ b/autoload/cargo.vim @@ -1,89 +1,91 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'rust') == -1 +function! cargo#Load() + " Utility call to get this script loaded, for debugging +endfunction + function! cargo#cmd(args) - silent! clear - if !a:args - execute "!" . "cargo ". a:args + execute "! cargo" a:args +endfunction + +function! s:nearest_cargo(...) abort + " If the second argument is not specified, the first argument determines + " whether we will start from the current directory or the directory of the + " current buffer, otherwise, we start with the provided path on the + " second argument. + + let l:is_getcwd = get(a:, 1, 0) + if l:is_getcwd + let l:starting_path = get(a:, 2, getcwd()) else - echom "Missing arguments" + let l:starting_path = get(a:, 2, expand('%:p:h')) endif + + return findfile('Cargo.toml', l:starting_path . ';') endfunction -function! cargo#build(args) - silent! clear - if !a:args - execute "!" . "cargo build " . a:args - else - execute "!" . "cargo build" +function! cargo#nearestCargo(is_getcwd) abort + return s:nearest_cargo(a:is_getcwd) +endfunction + +function! cargo#nearestWorkspaceCargo(is_getcwd) abort + let l:nearest = s:nearest_cargo(a:is_getcwd) + while l:nearest !=# '' + for l:line in readfile(l:nearest, '', 0x100) + if l:line =~# '\V[workspace]' + return l:nearest + endif + endfor + let l:next = fnamemodify(l:nearest, ':p:h:h') + let l:nearest = s:nearest_cargo(0, l:next) + endwhile + return '' +endfunction + +function! cargo#nearestRootCargo(is_getcwd) abort + " Try to find a workspace Cargo.toml, and if not found, take the nearest + " regular Cargo.toml + let l:workspace_cargo = cargo#nearestWorkspaceCargo(a:is_getcwd) + if l:workspace_cargo !=# '' + return l:workspace_cargo endif - silent! clear - execute "!" . "cargo build" + return s:nearest_cargo(a:is_getcwd) +endfunction + + +function! cargo#build(args) + call cargo#cmd("build " . a:args) endfunction function! cargo#clean(args) - silent! clear - if !a:args - execute "!" . "cargo clean " . a:args - else - execute "!" . "cargo clean" - endif - silent! clear - execute "!" . "cargo clean" + call cargo#cmd("clean " . a:args) endfunction function! cargo#doc(args) - silent! clear - if !a:args - execute "!" . "cargo doc " . a:args - else - execute "!" . "cargo doc" - endif + call cargo#cmd("doc " . a:args) endfunction function! cargo#new(args) - silent! clear - if !a:args - execute "!cargo new " . a:args - :cd `=a:args` - else - echom "Missing arguments" - endif + call cargo#cmd("new " . a:args) + cd `=a:args` endfunction function! cargo#init(args) - silent! clear - if !a:args - execute "!" . "cargo init " . a:args - else - execute "!" . "cargo init" - endif + call cargo#cmd("init " . a:args) endfunction function! cargo#run(args) - silent! clear - if !a:args - execute "!" . "cargo run " . a:args - else - execute "!" . "cargo run" - endif + call cargo#cmd("run " . a:args) endfunction function! cargo#test(args) - silent! clear - if !a:args - execute "!" . "cargo test " . a:args - else - execute "!" . "cargo test" - endif + call cargo#cmd("test " . a:args) endfunction function! cargo#bench(args) - silent! clear - if !a:args - execute "!" . "cargo bench " . a:args - else - execute "!" . "cargo bench" - endif + call cargo#cmd("bench " . a:args) endfunction +" vim: set et sw=4 sts=4 ts=8: + endif diff --git a/autoload/cargo/quickfix.vim b/autoload/cargo/quickfix.vim new file mode 100644 index 00000000..13c3b465 --- /dev/null +++ b/autoload/cargo/quickfix.vim @@ -0,0 +1,30 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'rust') == -1 + +function! cargo#quickfix#CmdPre() abort + if &filetype ==# 'rust' && get(b:, 'current_compiler', '') ==# 'cargo' + " Preserve the current directory, and 'lcd' to the nearest Cargo file. + let b:rust_compiler_cargo_qf_has_lcd = haslocaldir() + let b:rust_compiler_cargo_qf_prev_cd = getcwd() + let b:rust_compiler_cargo_qf_prev_cd_saved = 1 + let l:nearest = fnamemodify(cargo#nearestRootCargo(0), ':h') + execute 'lchdir! '.l:nearest + else + let b:rust_compiler_cargo_qf_prev_cd_saved = 0 + endif +endfunction + +function! cargo#quickfix#CmdPost() abort + if b:rust_compiler_cargo_qf_prev_cd_saved + " Restore the current directory. + if b:rust_compiler_cargo_qf_has_lcd + execute 'lchdir! '.b:rust_compiler_cargo_qf_prev_cd + else + execute 'chdir! '.b:rust_compiler_cargo_qf_prev_cd + endif + let b:rust_compiler_cargo_qf_prev_cd_saved = 0 + endif +endfunction + +" vim: set et sw=4 sts=4 ts=8: + +endif |