Skip to content

Derive apheleia formatters from project configuration #66

@berquist

Description

@berquist

It is annoying to have apheleia formatters (and to some extent flycheck checkers) run for files whose projects don't have a configuration. For example, I'm not sure I want yamlfmt to run if .yamlfmt.yaml isn't a dominating file and/or it isn't used in a dominating .pre-commit-config.yaml. This is an issue when working on a YAML file for a project that doesn't have yamlfmt configured and enforced, then when I edit that file, my default configuration for apheleia runs yamlfmt and messes up the entire buffer.

blacken https://github.com/pythonic-emacs/blacken/blob/a43695f9cb412df93ac8d38b55ab1515e86e217e/blacken.el#L133-L138 can do this non-generally: it has to look for [tool.black] in pyproject.toml. Using the modern alternative of ruff, we would need to do one of:

  • look for a dominating ruff.toml file
  • look through all dominating pyproject.toml files for [tool.ruff(?P<subdict>\.\w+)?] (or something like that)
  • look through a dominating .pre-commit-config.yaml file
  • look for a ruff call somewhere in .github/workflows/..., .gitlab-ci.yml, etc.

The result would then set apheleia-formatter for each buffer depending on the mode. On the other hand, this may be overengineering compared to just doing the above in a .dir-locals.el by hand. The problem then turns into managing the .dir-locals.el file outside of version control.


Ideally, apheleia would run the formatter on just the changed region(s). Apheleia doesn't do this, it only runs on whole buffers. The RCS patch code is for placing the point properly after (re)formatting, not for taking the diff against the last save and applying the formatter to the region. Perhaps it could be reused to only send a changed region to a formatter. python-black does this, but it's using https://github.com/wbolster/black-macchiato, so the implementation is external, not a part of Emacs, meaning it isn't general and would require something similar for every single formatter to be supported. This is basically impossible, whereas detecting whether a formatter should be run at all is a pain but enumerable. The problem is essentially tracked in radian-software/apheleia#43.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions