purcell / emacs-reformatter

Define commands which run reformatters on the current Emacs buffer
266 stars 21 forks source link

Support formatters with different working directories #5

Open pmiddend opened 5 years ago

pmiddend commented 5 years ago

I, unfortunately, have a formatter that requires it to be run from the project's root directory. In my hand-written formatter, I pass projectile-project-root to the started subprocess. reformatter.el doesn't seem to support this. Any way to implement this easily?

wbolster commented 5 years ago

quote from the comments in the package:

The :args" and :program expressions will be evaluated at runtime

perhaps you can use this to create a :program that will execute something like this:

sh -c 'cd "..." && your-formatter'
purcell commented 5 years ago

Hi @pmiddend! Which formatter is that? It's unusual to have a formatter which works on STDIN/STDOUT but needs to be run from the root of a project.

Since reformatter currently only suggests STDIN/STDOUT reformatters, it would probably be quite easy for me to override the working directory as a result of, say, a new :workdir fn option.

perhaps you can use this to create a :program that will execute something like this:

Ah, this won't work, because "program" must be the actual executable, not a command string, since it's passed to call-process.

purcell commented 5 years ago

(Funnily enough, I contributed the code which allows flycheck to run checkers from different working directories, and that was horribly fiddly to get right.)

pmiddend commented 5 years ago

@purcell it's https://github.com/asottile/reorder_python_imports and I admit it's pretty strange

purcell commented 5 years ago

The source code of that script suggests that it doesn't use STDIN/STDOUT anyway, so presumably you're passing it arguments that rely on (buffer-file-name)? If so, that's already a bit of a hack. :-)

pmiddend commented 5 years ago

It actually does use STDIN/STDOUT, though I completely understand that you want to limit reformatter.el's scope and not support every hacky formatter out there ;)

wbolster commented 5 years ago

what about my earlier comment, but use /bin/sh as the executable, and do the "cd ... && ..." part in :args?

victorolinasc commented 5 years ago

@purcell first of all, thanks a lot for this code! It really helps :)

I'll build the case for this feature with a valid case of a non-awkward formatter.

Elixir Mix Format task

Using reformatter.el with mix is a breeze.

(reformatter-define elixir-format
    :program "mix"
    :args '("format" "-"))

It just works.

The problem is with a specific optionm import_deps where dependencies can contribute to the formatting rules applied in the project. To use this option, the mix format command must be run in the same directory as the mix.exs file that defines those dependencies. Now add to that that one of the most famous framework in the language add this option upon project generation (scaffolding)... then it is a bit of a pain...

This could be thought as a valid pattern in other languages too. I'd really appreciate if this could be built in reformatter.el. It has been a very though ride to get this right in emacs-elixir... (see here and here for instance...).

Thanks once again for your wonderful work on Emacs.