0

when python code block gets larger, it's hard to find the right indentation level

def foo():
    for a in l:  # l1
    # l1-1
        for b in k: # l2
            print(a)
        # (2)
    print(b)
    # (1)

suppose I have my cursor at the first for (l1 line) , then I would insert a new line at # (1) and move cursor there similarly from # l2 to # (2)

I found similar solution https://superuser.com/q/1017094/309229 but it navigates only, it doesn't let me create a new line.

eugene
  • 481
  • 1
  • 5
  • 11
  • Does this answer your question: https://emacs.stackexchange.com/questions/30363/mapping-ret-to-use-the-same-indentation-level-as-the-previous-line?rq=1? – Manuel Uberti Aug 04 '23 at 13:57
  • @ManuelUberti thanks, but it seems it goes from #l1 to #l1-1 not # (1) – eugene Aug 04 '23 at 14:29

1 Answers1

0

It may be not accepted by usual Python standards. See the picture below and note the error [E115]. (the `:' introduce an indented block)

enter image description here

This is automatically corrected if you add the apheleia package to your init.el and add the Python module black to your installation, which is a code format utility. See the picture below.

enter image description here

As for indentation guides, the package highlight-indent-guides may help you - the character ?\x2502 draw a tiny vertical line, as in posted pictures.

(use-package highlight-indent-guides
  :defer t
  :hook (prog-mode-hook . highlight-indent-guides-mode)
  :init
  (setq highlight-indent-guides-method 'character
        highlight-indent-guides-character ?\x2502)
  (highlight-indent-guides-mode 1))
Ian
  • 1,321
  • 10
  • 12