Vim Nesting Levels with Parentheses and Brackets

When I click the file again with gg=G , I notice that the indentation of the closing parenthesis or parenthesis does not match the opening line. For example (with leading tabs shown with> ...)

 if settings.DEBUG: >...urlpatterns += patterns('', >...>...url(r'^media/(?P<path>.*)$', 'django.views.static.serve', { >...>...>...'document_root': settings.MEDIA_ROOT, >...>...}), >...) 

I wanted to fix the indentation in the file that contains this. For python, my softtabstop set to 4, shiftwidth set to 4 and expandtab . When I did gg=G , this led to the following:

 if settings.DEBUG: urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT, }), ) 

There are two problems there. Firstly, the closing brackets do not coincide with the space level of their opening brackets, they recede at the same level as the code inside the brackets. This can be seen both with ( in the second line, and with its correspondence ) in the last line, as well as with { in the third line and its coincidence } in the fifth line. I would like the closing brackets to match at the same level as their opening brackets.

My second problem is that the indentation after opening the parenthesis doubles the indentation for the new block or even after opening { . I think this may be part of the file type fingerprint for Python, but I wonder how I can stop this, so all indentation is 4 inches wide.

+4
source share
1 answer

Use this indent script in vim to snooze your python files. It does what is recommended in PEP-0008 . The code you posted indented with the script gives me the following:

 if settings.DEBUG: urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT, }), ) 

Recommendation for your second problem:

Continuation lines should align wrapped items either vertically using an implicit Python line joining in parentheses, brackets, and curly braces, or using a padding indent. When using hanging padding, the following considerations should apply; there should be no arguments in the first line, and you must use extra indentation to clearly distinguish yourself as a continuation line.

So the indentation script is doing the right thing.

Also, the type of indentation you want is recommended if you have no arguments in the first line. Therefore, reordering the code and using the indentation script gives:

 if settings.DEBUG: urlpatterns += patterns( '', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', { 'document_root': settings.MEDIA_ROOT, }), ) 
+6
source

Source: https://habr.com/ru/post/1412123/


All Articles