Vim Tips Wiki
Register
(adjust Template:ScriptComments to remove id as no longer needed; minor tweaks)
Line 1: Line 1:
{{ScriptComments|3003|indent/python: yet another Python indentation script}}
+
{{ScriptComments|indent/python: yet another Python indentation script}}
   
 
==Python indenting script==
 
==Python indenting script==

Revision as of 07:49, 15 November 2011

Use this page to discuss script 3003 indent/python: yet another Python indentation script

  • Add constructive comments, bug reports, or discuss improvements (see the guideline).
  • Do not document the script here (the author should do that on vim.org).
  • This page may be out of date: check the script's vim.org page above, and its release notes.

Python indenting script

This is my attempt at indenting Python to conform to PEP8. Based on the 7.2 Python indentation, with bits of script#974 hacked in.

Sample data

This is the Python code I tested with:

import line2, line3, line4, line5

class B:
    def foo():
        line1 = (line3({[a, b]
                        : 3,
                       }
                      ),
                 (
                  ( "hello" ),
                  99,
                 ),
                 line4)
        line5("foo",
              ("bar",
               "bat",
               "baz"),
              "snaf")

def bar(b, c):
    a = (b
         + c) - (c
                 + b)

class Rectangle(Blob):
    def __init__(self, width, height,
                 color='black', emphasis=None, highlight=0):
        """
        fjlaskdjf
        """
        if width == 0 and height == 0 and \
           color == 'red' and emphasis == 'strong' or \
           highlight > 100:
            raise ValueError("sorry, you lose")
        if width == 0 and height == 0 and (color == 'red' or
                                           emphasis is None):
            raise ValueError("I don't think so -- values are %s, %s" %
                             (width, height))
            Blob.__init__(self, width, height,
                          color, emphasis, highlight)

Comments

This is largely a huge improvement, but the indentation inside braces, brackets, etc. is a little aggressive.

For example:

 some_really_long_thing = {
    'foo': 'bar',<ENTER>

Hitting <ENTER> will always indent the cursor to the level of the "{", ignoring the previous lines indentation level.


I could change it to say a brace or parentheses only marks a new indent if it has text after it. If it is by itself at the end of the line, then the indentation for the next line should be unchanged.

Would that be an improvement?

-- David Moore 2010-10-24



That would be perfect, thanks David.

-- Alec Thomas 2010-10-24