Created
August 13, 2014 06:51
-
-
Save SebastianJarsve/8e4b396a1c8a1f3f80d0 to your computer and use it in GitHub Desktop.
To use with Pythonista
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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