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

Would auto-formatting / auto-indentation make sense for this project? #19

Open
simonw opened this issue Sep 7, 2020 · 7 comments
Open
Labels
enhancement New feature or request
Milestone

Comments

@simonw
Copy link

simonw commented Sep 7, 2020

I've been hoping to find a templating equivalent of Black for quite a while. Could automatic formatting fit this project?

@simonw simonw added the enhancement New feature or request label Sep 7, 2020
@simonw
Copy link
Author

simonw commented Sep 7, 2020

Whether or not this feature makes sense, this project is excellent - thank you so much! It just found a couple of bugs in Datasette. simonw/datasette@d0c752d

@thibaudcolas
Copy link
Owner

Hey @simonw, well I’m glad you’re finding it helpful! 🙂 99% of the credit for those two issues should go to https://github.com/motet-a/jinjalint, from which most of Curlylint’s parser comes from (I’ve only changed the parser for a few bug fixes so far).


For auto-formatting – this is something I’m personally very interested in as well, as a heavy user of both Prettier and black. I’ve been keen to work on this for a while, but need to be realistic with the time commitment something like this would take, so the short answer is "not for now", I think.

The long answer is that there is some interest in making this happen in the Prettier world, and to me it feels like a good ecosystem for this to be done within, since there is so much similarity between all the curly template languages. So far the most promising attempt there is https://github.com/trivago/prettier-plugin-twig-melody, and I’m hoping this will be something we can leverage for Jinja / Django templates.

For Curlylint, I can see a few ways for this project to help make auto-formatting happen:

  • First there’s the parser. There aren’t so many parsers out there that provide a useful AST for both HTML, and template language syntax. This would obviously be a good basis for auto-formatting – assuming the parser is good you’d "just" need a printer for the AST. That parser needs work, regardless of whether it’s for linting or auto-formatting.
  • Then there’s the parser’s unit tests! Currently there aren’t many. I’d really like to write a lot, in a format that would be helpful to the current parser, but that could also be used to a JS-implemented parser.
  • And smoke tests. https://github.com/thibaudcolas/curlylint/tree/main/tests contains code from many projects partly for me to have an easy set of templates to test all kinds of static analysis on curly templates.

If you or anyone else wants to help with this, I can see a few options:

  • Testing Curlylint more. I’m only using it on a handful of projects currently, and have an ok understanding of the parser, but it really does need to be more battle-tested before I’d let any tool rewrite code based on it. Any feedback on that front is appreciated.
  • Same as the above, but with unit tests :) That parser code really needs its 100% coverage as a minimum.
  • Finally to depart from the above I’d also be interested for someone to investigate whether the existing indent rule could be re-purposed for automated indentation. It’s not something I’m too keen on myself (I’d rather work on a full-on parser!), but if it’s not that much work to make use of that existing indent logic, might as well.

@thibaudcolas thibaudcolas added this to the Nice to have milestone Mar 13, 2021
@yaegassy
Copy link
Contributor

I'm also interested in the jinja2, django template formatter.

It seems that there is a package called "vsot", but unfortunately it is not maintained.

PRE-APLHA, mostly unmaintained

(Something, if it would be helpful.)

Of course, if there is a dedicated plugin for prettier, I'm sure that's fine.

@panosl
Copy link

panosl commented Apr 11, 2021

I'm also interested in the jinja2, django template formatter.

It seems that there is a package called "vsot", but unfortunately it is not maintained.

PRE-APLHA, mostly unmaintained

* https://pypi.org/project/vsot/

* https://github.com/benhowes/vsot

(Something, if it would be helpful.)

Of course, if there is a dedicated plugin for prettier, I'm sure that's fine.

Now that looks promising!

@thibaudcolas
Copy link
Owner

Ah that’s really interesting, thank you for sharing it here. There aren’t that many parsers out there for HTML + template syntax, particularly with enough information for pretty-printing, so it’s always interesting to compare options at the very least.

@yaegassy
Copy link
Contributor

yaegassy commented Jun 7, 2021

@thibaudcolas I recently discovered a tool called djhtml. It appears to be actively being developed now.

DjHTML is an indenter and not a formatter

Currently, the feature seems to be only for indent adjustment, but we can expect more in the future.

@thibaudcolas
Copy link
Owner

@yaegassy 🤘 that seems pretty promising, thank you for sharing. I’ll have to try that out myself.

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

No branches or pull requests

4 participants