Skip to content

Instantly share code, notes, and snippets.

@SebastianJarsve
Created August 13, 2014 06:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SebastianJarsve/8e4b396a1c8a1f3f80d0 to your computer and use it in GitHub Desktop.
Save SebastianJarsve/8e4b396a1c8a1f3f80d0 to your computer and use it in GitHub Desktop.
To use with Pythonista
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment