Created
November 25, 2012 10:24
-
-
Save GuyCarver/4143003 to your computer and use it in GitHub Desktop.
Pythonista editor script to toggle comment of selected lines.
This file contains 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
#Comment/Uncomment selected lines. | |
import editor | |
text = editor.get_text() | |
selection = editor.get_line_selection() | |
selected_text = text[selection[0]:selection[1]] | |
is_comment = selected_text.strip().startswith('#') | |
replacement = '' | |
for line in selected_text.splitlines(): | |
if is_comment: | |
if line.strip().startswith('#'): | |
replacement += line[line.find('#') + 1:] + '\n' | |
else: | |
replacement += line + '\n' | |
else: | |
replacement += '#' + line + '\n' | |
editor.replace_text(selection[0], selection[1], replacement) | |
editor.set_selection(selection[0], selection[0] + len(replacement) - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment