vim-bazel
vim-bazel copied to clipboard
Mechanism to automatically infer current file for build / test
It would be nice if the bazel plugin could infer "the target for the current file" automatically so you could just invoke :Bazel build
or :Bazel test
as a shortcut for "build this file's build target" and "run the test defined in this file".
Also inferring "current test method" would be good, but we can ignore that for v1 since there's some complexity getting that right and exposing it cleanly as a feature.
@dbarnett I don't know vimscript nearly well enough to make a pull request (also, i use Vundle which this plugin doesn't seem to support), but I made these functions for doing just that in my .vimrc.
function! BazelGetCurrentBufTarget()
let bazel_file_label=system("bazel query " . bufname("%") . " --color no --curses no --noshow_progress | tr -d '[:space:]'")
let bazel_file_package=split(bazel_file_label, ":")[0]
let g:current_bazel_target=system("bazel query \"attr('srcs', " . bazel_file_label . ", " . bazel_file_package . ":*)\" --color no --curses no --noshow_progress | tr -d '[:space:]'")
endfunction
function! BazelBuildHere()
:call BazelGetCurrentBufTarget()
:execute '!bazel build ' . g:current_bazel_target
endfunction
function! BazelTestHere()
:call BazelGetCurrentBufTarget()
:execute '!bazel test ' . g:current_bazel_target
endfunction