def execlines(self): """execute selected lines in console. """ import editor import textwrap a=editor.get_text()[editor.get_line_selection()[0]:editor.get_line_selection()[1]] exec(textwrap.dedent(a))
def main(): import editor selection_range = editor.get_selection() if not selection_range: # No file opened in the editor return text = editor.get_text() selected_lines_range = editor.get_line_selection() selected_lines_text = text[selected_lines_range[0]:selected_lines_range[1]] selected_lines = selected_lines_text.splitlines(True) last_line_deleted = False if len(selected_lines) > 1: # Ignore the last line selection if there's just cursor at the beginning of # this line and nothing is selected last_line = selected_lines[-1] if selected_lines_range[1] - len(last_line) == selection_range[1]: last_line_deleted = True del selected_lines[-1] selected_lines_range = (selected_lines_range[0], selected_lines_range[1] - len(last_line) - 1) replacement = ''.join(_toggle_lines(selected_lines)) if last_line_deleted: replacement = replacement[:-1] editor.replace_text(selected_lines_range[0], selected_lines_range[1], replacement) editor.set_selection(selected_lines_range[0], selected_lines_range[0] + len(replacement))
def execlines(): """execute selected lines in console. """ import editor import textwrap a = editor.get_text()[editor.get_line_selection()[0]:editor. get_line_selection()[1]] exec(textwrap.dedent(a))
def comment_action(sender): """" comment out selected lines""" import re COMMENT='#' i=editor.get_line_selection() t=editor.get_text() # replace every occurance of newline with ewline plus COMMENT, except last newline editor.replace_text(i[0],i[1]-1,COMMENT+re.sub(r'\n',r'\n'+COMMENT,t[i[0]:i[1]-1])) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def uncomment_action(self): """" uncomment selected lines""" import re COMMENT='#' i=editor.get_line_selection() t=editor.get_text() # replace every occurance of newline # with newline, except last newline if all( [x.startswith('#') for x in t[i[0]:i[1]-1].split(r'\n')]): editor.replace_text(i[0],i[1]-1,re.sub(r'^'+COMMENT,r'',t[i[0]:i[1]-1],flags=re.MULTILINE)) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def comment(self): """" comment out selected lines""" import editor import re COMMENT='#' i=editor.get_line_selection() t=editor.get_text() # replace every occurance of newline with ewline plus COMMENT, except last newline editor.replace_text(i[0],i[1]-1,COMMENT+re.sub(r'\n',r'\n'+COMMENT,t[i[0]:i[1]-1])) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def unindent(self): """unindent selected lines all the way""" import editor import textwrap i=editor.get_line_selection() t=editor.get_text() editor.replace_text(i[0],i[1], textwrap.dedent(t[i[0]:i[1]])) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def indent(self): """indent selected lines by one tab""" import editor import re i=editor.get_line_selection() t=editor.get_text() # replace every occurance of newline with newline plus indent, except last newline editor.replace_text(i[0],i[1]-1,INDENTSTR+re.sub(r'\n',r'\n'+INDENTSTR,t[i[0]:i[1]-1])) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def unindent(self): """unindent selected lines all the way""" import editor import textwrap i = editor.get_line_selection() t = editor.get_text() editor.replace_text(i[0], i[1], textwrap.dedent(t[i[0]:i[1]])) editor.set_selection(i[0], i[1] - len(t) + len(editor.get_text()))
def did_load(self): text = editor.get_text() selection = editor.get_line_selection() selected_text = text[selection[0]:selection[1]] is_comment = selected_text.strip().startswith('#') if is_comment: self.__bC.title = 'Uncomment' self.__tvP.text = selected_text.strip()[1:11] + '...' self.__tvP.text_color = 'black' else: self.__bC.title = 'Comment' self.__tvP.text = '#' + selected_text.strip()[0:10] + '...' self.__tvP.text_color = 'green'
def indent(): """indent selected lines by one tab""" import editor import re INDENTSTR = '\t' #two spaces i = editor.get_line_selection() t = editor.get_text() # replace every occurance of newline with ewline plus indent, except last newline editor.replace_text( i[0], i[1] - 1, INDENTSTR + re.sub(r'\n', r'\n' + INDENTSTR, t[i[0]:i[1] - 1])) editor.set_selection(i[0], i[1] - len(t) + len(editor.get_text()))
def uncomment(self): """" uncomment selected lines""" import editor import re COMMENT='#' i=editor.get_line_selection() t=editor.get_text() # replace every occurance of newline # with newline, except last newline if all( [x.startswith('#') for x in t[i[0]:i[1]-1].split(r'\n')]): editor.replace_text(i[0],i[1]-1,re.sub(r'^'+COMMENT,r'',t[i[0]:i[1]-1],flags=re.MULTILINE)) editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text()))
def uncomment(): """" uncomment selected lines""" import editor import re COMMENT = '#' i = editor.get_line_selection() t = editor.get_text() # replace every occurance of newline # with newline, except last newline # todo.. probably should verify every line has comment... # num lines= re.findall if all([x.startswith('#') for x in t[i[0]:i[1] - 1].split(r'\n')]): editor.replace_text( i[0], i[1] - 1, re.sub(r'^' + COMMENT, r'', t[i[0]:i[1] - 1], flags=re.MULTILINE)) editor.set_selection(i[0], i[1] - len(t) + len(editor.get_text()))
def _get_block(separator): text = editor.get_text() separator_start = text.find(separator) + len(separator) cursor_position = editor.get_line_selection()[0] if cursor_position <= separator_start: return '' block_start = text.rfind('\n\n', separator_start, cursor_position + 1) if block_start == -1: block_start = separator_start block_end = text.find('\n\n', cursor_position - 1) if block_end == -1: block_end = len(text) return text[block_start:block_end].strip()
def toggle_comments(): selection_range = editor.get_selection() if not selection_range: # No file opened in the editor return text = editor.get_text() selected_lines_range = editor.get_line_selection() selected_lines_text = text[selected_lines_range[0]:selected_lines_range[1]] selected_lines = selected_lines_text.splitlines() if len(selected_lines) > 1: # Ignore the last line selection if there's just cursor at the beginning of # this line and nothing is selected last_line_length = len(selected_lines[-1]) if selection_range[1] == selected_lines_range[1] - last_line_length: del selected_lines[-1] selected_lines_range = (selected_lines_range[0], selected_lines_range[1] - last_line_length - 1) is_commented = selected_lines_text.strip().startswith('#') replacement = '' for line in selected_lines: if is_commented: if line.strip().startswith('#'): replacement += line[line.find('#') + 1:] + '\n' else: replacement += line + '\n' else: replacement += '#' + line + '\n' # Remove trailing \n to avoid empty new lines replacement = replacement[:-1] editor.replace_text(selected_lines_range[0], selected_lines_range[1], replacement) editor.set_selection(selected_lines_range[0], selected_lines_range[0] + len(replacement))
def __bCA(self, sender): # from: pythonista/docs/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) # end ----------------------------------------- self.close()
# coding: utf-8 # Put this script into the editor action (wrench) menu. # Whenever you want to import an Objective-C class, # you jutst need to type the classname, select it and invoke this script via the action menu. # This script will check if a classname with the name you selected exists # and add ` = ObjcClass(_classname_)` to the end of the line import editor, console, sys from objc_util import ObjCClass selection = editor.get_selection() line = editor.get_line_selection() text = editor.get_text() classname = text[int(selection[0]):int(selection[1])] try: ObjCClass(classname) editor.set_selection(selection[1]) editor.replace_text(selection[1], selection[1], ' = ObjCClass(\'{}\')'.format(classname)) except: console.hud_alert(sys.exc_info()[1].message, 'error')
# https://gist.github.com/GuyCarver/4143014 # unindent selection import editor text = editor.get_text() selection = editor.get_line_selection() selected_text = text[selection[0]:selection[1]] replacement = '' for line in selected_text.splitlines(): replacement += line[line.find('\t') + 1:] + '\n' editor.replace_text(selection[0], selection[1], replacement) editor.set_selection(selection[0], selection[0] + len(replacement) - 1)