@@ -42,19 +42,21 @@ function! codefmt#mixformat#GetFormatter() abort
4242 " "
4343 " Reformat the current buffer using ktfmt, only targeting {ranges}.
4444 function l: formatter .FormatRange (startline, endline) abort
45- let l: filename = expand (' %:t ' )
45+ let l: filename = expand (' %:p ' )
4646 if empty (l: filename )
4747 let l: filename = ' stdin.exs'
4848 endif
4949 " mix format docs: https://hexdocs.pm/mix/main/Mix.Tasks.Format.html
5050 let l: cmd = codefmt#formatterhelpers#ResolveFlagToArray (' mix_executable' )
5151 " Specify stdin as the file
5252 let l: cmd = l: cmd + [' format' , ' --stdin-filename=' . l: filename , ' -' ]
53+ let l: dir = l: filename == ' stdin.exs' ? getcwd () : s: findMixDir (l: filename )
54+ let l: syscall = maktaba#syscall#Create (l: cmd ).WithCwd (l: dir )
5355 try
5456 " mix format doesn't have a line-range option, but does a reasonable job
5557 " (except for leading indent) when given a full valid expression
5658 call codefmt#formatterhelpers#AttemptFakeRangeFormatting (
57- \ a: startline , a: endline , l: cmd )
59+ \ a: startline , a: endline , l: syscall )
5860 catch /ERROR(ShellError):/
5961 " Parse all the errors and stick them in the quickfix list.
6062 let l: errors = []
@@ -87,3 +89,20 @@ function! codefmt#mixformat#GetFormatter() abort
8789
8890 return l: formatter
8991endfunction
92+
93+ " Finds the directory to run mix from. Looks for a mix.exs file first; if that
94+ " is not found looks for a .formatter.exs file, falling back to the parent of
95+ " filepath.
96+ function ! s: findMixDir (filepath) abort
97+ let l: path = empty (a: filepath ) ? getcwd () : fnamemodify (a: filepath , ' :h' )
98+ let l: root = findfile (' mix.exs' , l: path . ' ;' )
99+ if empty (l: root )
100+ let l: root = findfile (' .formatter.exs' , l: path . ' ;' )
101+ endif
102+ if empty (l: root )
103+ let l: root = l: path
104+ else
105+ let l: root = fnamemodify (l: root , ' :h' )
106+ endif
107+ return l: root
108+ endfunction
0 commit comments