Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align key-value pair in PyCharm

Tags:

python

pycharm

Is it possible to set Align key-value pair in Pycharm Code Style? I'm looking for something that will reformat:

nickname = models.CharField(unique=True, max_length=24)
biography = models.TextField(default=BIO_DEFAULT_STRING)
avatar = models.ImageField(upload_to='members/avatars/')

to:

nickname  = models.CharField(unique=True, max_length=24)
biography = models.TextField(default=BIO_DEFAULT_STRING)
avatar    = models.ImageField(upload_to='members/avatars/')

I got it working in PhpStorm, can't find it in PyCharm.

like image 512
intelis Avatar asked Jun 08 '15 18:06

intelis


2 Answers

You need a function from plugin String Manipulation:

MenuCodeAlign to Columns


Configure hot-keys

  • SettingsKeymap
  • Search "Align to Columns" and select
  • Add Keyboard Shortcut(Enter)
  • Set your shortcuts (my Ctrl+K)
  • Press Apply and Ok
like image 180
Vyaches Avatar answered Oct 27 '22 10:10

Vyaches


It is really strange, but PyCharm 2016 has this stuff, and i was to search the way to turn it OFF. I found this question, while searching "How to turn align by value".

File - Settings - Editor - Code Style - Python

There you need Other tab, where you'll find Dict Aligment settings. There is 3 types:

  • Do not align
  • Align on value
  • Align on colon

Here is a screenshot

Have no idea why they added that stuff, maybe because of platform unification, but it is exist and was turned to 'Align on value' by default for me after i've updeted PyCharm to 2016.2.

UPDATE:

A litle bit more explanations:

It was asked how to make aligns in PyCharm code formating, for example in dicts.:

Here how does dict should looks like via PEP:

i_am_a_dict = {
    key:value,
    another_key:another_value,
    a_very_long_key:one_more_value
}

That's how does it looks for me in PyCharm after platform update (and such format DO NOT match PEP, but it can be done by PyCharm settings:

 i_am_a_dict = {
        key:            value,
        another_key:    another_value,
        a_very_long_key:one_more_value
    }

It was aligned by values. And the question was about "How to align key:values in PyCharm"

like image 29
bonusrk Avatar answered Oct 27 '22 11:10

bonusrk