Python auto-indent



  • A search turns up a lot on this but I’m looking for a definitive answer. I didn’t code in Python much at all and do now so I don’t remember if this ever “worked” or if it broke or I broke it with some configuration.

    if test==true: # press enter now
    next line goes here
    

    I would “expect”:

    if test==true: # press enter now
        next line goes here
    

    Notice the indent, based on my indent settings, which for Python are replace tabs with spaces equal to “4”. It’s the “auto indent” that doesn’t seem to be working in Python, which does work in Perl, C, C++, etc … (when adding the curly braces { } ).

    I see there is a Python Indent plugin. Is that what Python programmers use in N++, or should this be handled “natively”?

    Cheers.



  • @Michael-Vincent

    Well, not an answer, but merely my 2c: I do a LOT of Python. With Notepad++. And I don’t use the Python Indent plugin. And to be brutally honest: I had to go and actually check what my Notepad++ does in that situation. And it doesn’t auto-indent for me. And apparently that has never bothered me. I just hit Tab and move on with my life. :-)



  • @Michael-Vincent

    I can just confirm what @Alan-Kilborn said, it doesn’t work for python.
    I’m using a modify callback to make it happen, something like
    if the previous line end with a colon but is not a comment, indent += 4 based on previous line.
    Not sure it works 100% but good enough for me.
    Let me know if you want to have a look.
    I can’t post it right now, as I would have to refactor it
    as I use it in a bigger IDE-like_features_class with linting etc …



  • @Ekopalypse
    Thanks. I tried the python indent plugin and it works fine, produces the expected results. I can live with that plugin working, source code is simple enough.

    Cheers.



  • @Ekopalypse said in Python auto-indent:

    Let me know if you want to have a look.

    Sure I do! :)



  • @Alan-Kilborn

    Note, I do have autoindent disabled in order to make this work.
    Because I know you are doing python, I haven’t refactored anything,
    but I guess it’s mostly getting rid of self to make this work in a non-class way.
    Concerning the todo, no, I haven’t looked at it since then.

    def __init__(self):
        self.excluded_styles= [1, 3, 4, 6, 7, 12, 16, 17, 18, 19]
    
    def on_modified(self, args):
        # TODO: text == \r even if \r\n is assumed, but length is 2, is this a bug?? Or am I doing something wrong?
        if args['modificationType'] & 0x100000 == 0x100000 and args['text'] in ['\r','\n']:
            text = '\r\n' if args['length'] == 2 else args['text']
            self._indent(args['position'], text)
    
    def _indent(self, position, text):
        if self.is_python and self.auto_indent:
            indent = editor.getLineIndentation(editor.lineFromPosition(position))
            if (editor.getCharAt(position-1) == 58 and  # 58 == :
                editor.getStyleAt(position-1) not in self.excluded_styles):
                tabwidth = editor.getTabWidth()
                text += ' '*(indent//tabwidth+1)*tabwidth
            else:
                text += ' '*indent
            editor.changeInsertion(text)

Log in to reply