• Login
Community
  • Login

Python auto-indent

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 3 Posters 3.9k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M
    Michael Vincent
    last edited by Michael Vincent Jan 31, 2020, 8:45 PM Jan 31, 2020, 8:44 PM

    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.

    A E 2 Replies Last reply Jan 31, 2020, 8:55 PM Reply Quote 0
    • A
      Alan Kilborn @Michael Vincent
      last edited by Jan 31, 2020, 8:55 PM

      @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. :-)

      1 Reply Last reply Reply Quote 2
      • E
        Ekopalypse @Michael Vincent
        last edited by Jan 31, 2020, 10:07 PM

        @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 …

        M A 2 Replies Last reply Jan 31, 2020, 11:57 PM Reply Quote 4
        • M
          Michael Vincent @Ekopalypse
          last edited by Jan 31, 2020, 11:57 PM

          @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.

          1 Reply Last reply Reply Quote 1
          • A
            Alan Kilborn @Ekopalypse
            last edited by Feb 1, 2020, 12:18 AM

            @Ekopalypse said in Python auto-indent:

            Let me know if you want to have a look.

            Sure I do! :)

            E 1 Reply Last reply Feb 3, 2020, 1:46 PM Reply Quote 0
            • E
              Ekopalypse @Alan Kilborn
              last edited by Feb 3, 2020, 1:46 PM

              @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)
              
              1 Reply Last reply Reply Quote 1
              2 out of 6
              • First post
                2/6
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors