Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mix format does not respect local config #236

Open
bitboxer opened this issue Apr 19, 2018 · 10 comments
Open

mix format does not respect local config #236

bitboxer opened this issue Apr 19, 2018 · 10 comments

Comments

@bitboxer
Copy link

Sadly the mix format - does not respect my local config for the formatter. It uses the default mix format settings. Is this a known problem? Do I have it misconfigured locally?

bitboxer pushed a commit to bitboxer/dotfiles that referenced this issue Apr 19, 2018
The autoformat plugin does not respect the current mix configs and uses the default settings...which breaks a lot of things sadly.

Opened a ticket here: vim-autoformat/vim-autoformat#236
@chtenb
Copy link
Member

chtenb commented Apr 19, 2018

Is this a known problem?

My guess is that the contributor who added this support didn't care for that use case.
If you can figure out what additional parameters are needed to make mix format detect local config, you could provide a pull request.

@bitboxer
Copy link
Author

Will try to figure this out.

@bitboxer
Copy link
Author

Apparently this was a bug and fixed in Elixir 1.6.4. Will try it out on the weekend and report back.

@bitboxer
Copy link
Author

Sadly I can't confirm that 1.6.4 fixed this. I am waiting for a response by the elixir team and will keep you updated here.

@bitboxer
Copy link
Author

Okay, the problem is that my formatter config has some relative path in it. And elixir will not work with piping in the content and having the formatter have relative paths 😭 . There is no good way to fix this with piping the code into it. The only way forward is to safe the file, run the formatter on the saved file and reload it.

@chtenb
Copy link
Member

chtenb commented Apr 23, 2018

The only way forward is to safe the file, run the formatter on the saved file and reload it.

Conceptually that is not true. There are many formatters that accept an additional filepath argument which is treated as if the stdin would come from that file. Elixir should do that too to solve this. It's the cleanest and most common solution for these kind of problems.

@michaeljones
Copy link

michaeljones commented Jan 28, 2019

I've attempted to solve this issue locally with:

" Set autoformat command for mix so that we 'cd' into the file's directory
" and then 'cat' the file into 'mix format -' so we get the formatted output
" on std-out but we also get the correct mix formatters context. Import for
" not introducing loads of parens into Phoenix routes, etc.
let g:formatdef_mix_format = "'cd '.expand('%:h').'; cat '.expand('%:t').' | mix format -'"

It seems to work though I haven't tested it vigorously. I'm also not sure how portable it is but it seems fine on my Ubuntu/neovim set up. I've been experiencing the issue with Elixir/mix 1.8.

@bitboxer
Copy link
Author

@michaeljones thanks for this. Works on my setup like a charm 👍

bitboxer added a commit to bitboxer/dotfiles that referenced this issue Jan 30, 2019
@michaeljones
Copy link

I'm afraid I've not had time to test this in anger and I'm worried that it is going to use the contents of the file on disk rather than the contents of the buffer. I don't see a way to get mix format to understand the context of the file whilst also reading from stdinput. I'm going to do some more research but it might require writing a (hopefully) small script that uses the Elixir Code & Format modules to provide the functionality needed.

@michaeljones
Copy link

My fears were confirmed. If you delete a line and then save the file, the line comes back because the formatter runs before the save and uses the file on disk. This is confirmed by :help BufWrite saying that it runs before the write. Perhaps using BufWritePost would solve this but that might lead to a double write, or something. Instead my current strategy is to use findfile to try to find the closest .formatter.exs file and pass that to mix format using the --dot-formatter flag. I think that this is the correct approach and provides adequate context for the formatting but I don't know for sure that mix format doesn't do more and I haven't used this much so far. From light testing it works better than the previous suggestion.

The code I now have is:

" Try to find the closest '.formatter.exs' to the file currently being edited.
" If we find one, then pass that as context to the `mix format` command.
" Withouth this the `mix format -` has no context for the formatting so and
" cannot apply local project rules.
function! g:BuildMixFormatLocalCmd()
	let l:format_file = findfile('.formatter.exs', '.;')
	if format_file != '' 
		return 'mix format --dot-formatter ' . format_file . ' -'
	else
		return 'mix format -'
	endif
endfunction
let g:formatdef_mix_format = "g:BuildMixFormatLocalCmd()"

The mix format 'find file' behaviour is potentially associated with this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants