Skip to content

Commit 4b6cb67

Browse files
committed
If the buffer has a path, pass it to buildifier as '-path PATH'.
Fixes google#100.
1 parent 78f6465 commit 4b6cb67

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

autoload/codefmt/buildifier.vim

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ function! codefmt#buildifier#GetFormatter() abort
3939
" @flag(buildifier)
4040
" @throws ShellError
4141
function l:formatter.Format() abort
42-
let l:cmd = s:plugin.Flag('buildifier_executable')
42+
let l:cmd = [ s:plugin.Flag('buildifier_executable') ]
43+
let l:fname = expand('%:p')
44+
if !empty(l:fname)
45+
let l:cmd += ['-path', l:fname]
46+
endif
47+
4348
let l:input = join(getline(1, line('$')), "\n")
4449
try
4550
let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call()

vroom/buildifier.vroom

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
The built-in buildifier formatter knows how to format Bazel BUILD files. If you
2-
aren't familiar with basic codefmt usage yet, see main.vroom first.
1+
The built-in buildifier formatter knows how to format Bazel BUILD and .bzl
2+
files. If you aren't familiar with basic codefmt usage yet, see main.vroom
3+
first.
34

45
We'll set up codefmt and configure the vroom environment, then jump into some
56
examples.
@@ -38,14 +39,17 @@ buildifier_executable flag if the default of "buildifier" doesn't work.
3839
$ )
3940
:Glaive codefmt buildifier_executable='buildifier'
4041

41-
The bzl filetype will use the buildifier formatter by default.
42+
The bzl filetype will use the buildifier formatter by default. The path to the
43+
file being edited is passed to buildifier so that it can adjust to whether the
44+
file is a BUILD or .bzl file.
4245

4346
@clear
4447
% foo_library(name = "foo", srcs = ["bar.js"],)
4548

49+
:silent file /foo/bar/BUILD
4650
:set filetype=bzl
4751
:FormatCode
48-
! buildifier .*
52+
! buildifier -path /foo/bar/BUILD .*
4953
$ foo_library(
5054
$ name = "foo",
5155
$ srcs = ["bar.js"],

0 commit comments

Comments
 (0)