Author Topic: Special paste macro for Python to use previous indent  (Read 1820 times)

rowbearto

  • Senior Community Member
  • Posts: 2335
  • Hero Points: 132
Special paste macro for Python to use previous indent
« on: July 19, 2017, 03:23:28 PM »
Hi:

As Python does not have braces, it is hard for SE to know the desired level of indenting when copy/pasting code. However as the programmer, I know the desired level of indenting. I'd like to have a macro (or macro(s)) in SE so that I can tell SE which level of indenting to use.

For example, lets say I have this code:

Code: [Select]
while (argv_idx < argvLen):
    if sys.argv[argv_idx] == "-d":
        argv_idx = argv_idx + 1
        description = sys.argv[argv_idx]
        description = description.replace(" ","-")
    if sys.argv[argv_idx] == "-u":
        compressed = False

and then I want to paste the following at the end:

Code: [Select]
    if sys.argv[argv_idx] == "-r":
        compressed = False
        argv_idx = argv_idx + 1
        reviewnum = sys.argv[argv_idx]

When I do this, I get it with undesired indenting, because SE thinks it should continue with current indenting:

Code: [Select]
while (argv_idx < argvLen):
    if sys.argv[argv_idx] == "-d":
        argv_idx = argv_idx + 1
        description = sys.argv[argv_idx]
        description = description.replace(" ","-")
    if sys.argv[argv_idx] == "-u":
        compressed = False
        if sys.argv[argv_idx] == "-r":   # I really wanted this 4 spaces over to the left, not where it was pasted.
            compressed = False
            argv_idx = argv_idx + 1
            reviewnum = sys.argv[argv_idx]

But what I really desire is this:

Code: [Select]
while (argv_idx < argvLen):
    if sys.argv[argv_idx] == "-d":
        argv_idx = argv_idx + 1
        description = sys.argv[argv_idx]
        description = description.replace(" ","-")
    if sys.argv[argv_idx] == "-u":
        compressed = False
    if sys.argv[argv_idx] == "-r":   # Now this line has the desired indent
        compressed = False
        argv_idx = argv_idx + 1
        reviewnum = sys.argv[argv_idx]

To fix it, I need to do column select and then "Shift-Tab" to move it to the correct indenting. It is doable, but tedious.

Since I as the programmer know the desired level of indenting, if SE could provide me with a macro (that I can bind to a key) to tell SE to paste this code with the previous level of indenting, it would make me more productive.

So if 2 macros are provided - paste_python_with_current_indent and paste_python_with_previous_indent, then I could bind both to a key and know exactly which level of indent I am pasting to.
« Last Edit: July 19, 2017, 03:57:07 PM by rowbearto »

JeffB

  • Senior Community Member
  • Posts: 326
  • Hero Points: 14
Re: Special paste macro for Python to use previous indent
« Reply #1 on: July 19, 2017, 03:31:25 PM »
I think it would also be helpful to make the "deselect after paste" option language specific, so after pasting something in Python (and only Python), I just have to "shift-tab".

Jeff