Skip to content

Instantly share code, notes, and snippets.

@SebastianJarsve
Created August 13, 2014 06:51
Show Gist options
  • Select an option

  • Save SebastianJarsve/8e4b396a1c8a1f3f80d0 to your computer and use it in GitHub Desktop.

Select an option

Save SebastianJarsve/8e4b396a1c8a1f3f80d0 to your computer and use it in GitHub Desktop.

Revisions

  1. SebastianJarsve created this gist Aug 13, 2014.
    23 changes: 23 additions & 0 deletions indentation.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import editor, console

    script = editor.get_text()
    selection = editor.get_selection()
    selected_text = script[selection[0]:selection[1]].splitlines()
    indentation = int(console.input_alert('Indent'))
    replacement = []

    for line in selected_text:
    if indentation > 0:
    replacement.append(('\t'*indentation)+line+('\n' if line != selected_text[-1] else ''))
    elif indentation == 0:
    replacement.append(line+('\n' if line != selected_text[-1] else ''))
    elif indentation < 0:
    if len(line) == 0:
    replacement.append('\n')
    elif line[0] == '\t':
    indent = min(abs(indentation), line.count('\t'))
    replacement.append(line.replace('\t', '', indent)+('\n' if line != selected_text[-1] else ''))
    else:
    replacement.append(line+('\n' if line != selected_text[-1] else ''))

    editor.replace_text(selection[0], selection[1], ''.join(replacement))