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

add indentation support for set tag #621

Merged
merged 6 commits into from
May 8, 2023
Merged

add indentation support for set tag #621

merged 6 commits into from
May 8, 2023

Conversation

ahmetelgun
Copy link
Contributor

Pull Request Check List

  • [] Added tests for changed code.
  • [] Updated documentation for changed code.

I have a code like this. This code is not formatted very well. I tried to add support for set tag. I don't know how i write a test for this and i don't find a document section for this.

{% set schema=[
{
"name": "id",
"type": "integer",
"primary": true
},
{
"name": "name",
"type": "string"
}
] %}

Now, code formatted like this:

{% set schema=[
    {
        "name": "id",
        "type": "integer",
        "primary": true
    },
    {
        "name": "name",
        "type": "string"
    }
] %}

@christopherpickering
Copy link
Contributor

Thanks for this, its a great start. I will see about adding tests.

@codecov
Copy link

codecov bot commented May 5, 2023

Codecov Report

Patch coverage: 97.67% and project coverage change: +0.08 🎉

Comparison is base (f335e3b) 95.54% compared to head (85aca4c) 95.63%.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #621      +/-   ##
==========================================
+ Coverage   95.54%   95.63%   +0.08%     
==========================================
  Files          16       16              
  Lines         921      962      +41     
  Branches      253      259       +6     
==========================================
+ Hits          880      920      +40     
  Misses         29       29              
- Partials       12       13       +1     
Impacted Files Coverage Δ
src/djlint/formatter/indent.py 97.56% <97.50%> (-0.03%) ⬇️
src/djlint/formatter/condense.py 96.82% <100.00%> (ø)
src/djlint/settings.py 92.19% <100.00%> (+0.03%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

if the contents are parsable as a python list or json5 they can be reformmated. Otherwise we will
fall back to the standard indent this pr added.

closes #287, closes #518, re #370
@christopherpickering
Copy link
Contributor

christopherpickering commented May 5, 2023

@ahmetelgun I expanded this a bit more - it will now also try to clean up the value of set. I added json5 library to parse javascript style json (single or no quotes on keys and values).

  • if it is json then the json will be formatted w/ python's json.dump. With this we can indent the json if the line becomes too long.

  • if the value is a list of tuples we can can use python eval to cleanup whitepsace and quotes... no line length formatting tho
    some fancy logic could be added for that one day

  • otherwise it will leave the formatting as-is... for example, if the json is invalid etc.

I added tests in the nunjucks > test_set folder.

Thanks a bunch for this!

closes #287
closes #518
fixes 1/2 of #370 (still no function formatting)

json keys that don't require quoting in javascript will not be quoted here. Does that work in your case as well?

@christopherpickering
Copy link
Contributor

looks like I accendently snuck in another change here that fixes some html tags getting formatted inside script tags.

@christopherpickering christopherpickering merged commit b6dc706 into djlint:dev May 8, 2023
@ahmetelgun
Copy link
Contributor Author

ahmetelgun commented May 8, 2023

Thanks for your help @christopherpickering . I can't wait to use it 😃

@christopherpickering
Copy link
Contributor

🎉 This PR is included in version 1.25.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants