def closeTemplateTag(): """Close the last templatetag open""" template_tags = '|'.join(TEMPLATE_TAGS_CLOSE) pattern_tag_open = re.compile("(.)*{%%%(espaces)s(%(tags)s)%(espaces)s(.)*%(espaces)s%%}(.)*" % {'espaces': str_blank, 'tags': template_tags}) pattern_tag_close = re.compile("(.)*{%%%(espaces)send(%(tags)s)%(espaces)s%(espaces)s%%}(.)*" % {'espaces': str_blank, 'tags': template_tags}) tag_closes = {} currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() tag = '' while currentLine >= 0: text = unicode(currentDocument.line(currentLine)) match_open = pattern_tag_open.match(text) if match_open: tag_name = match_open.groups()[1] if tag_name in tag_closes: tag_closes[tag_name] -= 1 if tag_closes[tag_name] == 0: del tag_closes[tag_name] elif not tag_closes: tag = match_open.groups()[1] break else: match_close = pattern_tag_close.match(text) if match_close: tag_name = match_close.groups()[1] tag_closes[tag_name] = tag_closes.get(tag_name, 0) + 1 currentLine = currentLine - 1 insertText("{%% end%s %%}" % tag)
def importUrls(): """Insert the typical code of the urls.py file""" currentDocument = kate.activeDocument() path = unicode(currentDocument.url().directory()) path_split = path.split('/') application = path_split[len(path_split) - 1] or TEXT_TO_CHANGE insertText(TEXT_URLS % {'app': application, 'change': TEXT_TO_CHANGE})
def togglePrettyJsonFormat(): """Pretty format of a XML code""" currentDocument = kate.activeDocument() view = currentDocument.activeView() source = unicode(view.selectionText()).encode('utf-8', 'ignore') if not source: kate.gui.popup('Select a xml text', 2, icon='dialog-warning', minTextWidth=200) else: try: target = minidom.parseString(source) view.removeSelectionText() xml_pretty = target.toprettyxml() xml_pretty = '\n'.join([ line for line in xml_pretty.split("\n") if line.replace(' ', '').replace('\t', '') ]) text.insertText(xml_pretty) except ExpatError: kate.gui.popup('This text is not a valid xml text', 2, icon='dialog-warning', minTextWidth=200)
def closeTemplateTag(): """Close the last templatetag open""" django_utils_conf = kate.configuration.root.get('django_utils', {}) template_tags_close = django_utils_conf.get(_TEMPLATE_TAGS_CLOSE, DEFAULT_TEMPLATE_TAGS_CLOSE).split(",") template_tags_close = [tag.strip() for tag in template_tags_close] template_tags = '|'.join(template_tags_close) pattern_tag_open = re.compile("(.)*{%%%(espaces)s(%(tags)s)%(espaces)s(.)*%(espaces)s%%}(.)*" % {'espaces': str_blank, 'tags': template_tags}) pattern_tag_close = re.compile("(.)*{%%%(espaces)send(%(tags)s)%(espaces)s%(espaces)s%%}(.)*" % {'espaces': str_blank, 'tags': template_tags}) tag_closes = {} currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() tag = '' while currentLine >= 0: text = currentDocument.line(currentLine) match_open = pattern_tag_open.match(text) if match_open: tag_name = match_open.groups()[1] if tag_name in tag_closes: tag_closes[tag_name] -= 1 if tag_closes[tag_name] == 0: del tag_closes[tag_name] elif not tag_closes: tag = match_open.groups()[1] break else: match_close = pattern_tag_close.match(text) if match_close: tag_name = match_close.groups()[1] tag_closes[tag_name] = tag_closes.get(tag_name, 0) + 1 currentLine = currentLine - 1 insertText("{%% end%s %%}" % tag)
def importUrls(): """Insert the typical code of the urls.py file""" currentDocument = kate.activeDocument() path = currentDocument.url().directory() path_split = path.split('/') application = path_split[len(path_split) - 1] or TEXT_TO_CHANGE django_utils_conf = kate.configuration.root.get('django_utils', {}) text_urls = django_utils_conf.get(_TEMPLATE_TEXT_URLS, DEFAULT_TEXT_URLS) insertText(text_urls % {'app': application, 'change': TEXT_TO_CHANGE})
def createBlock(): """Insert the tag block/endblock. The name of the block will be the text selected""" currentDocument = kate.activeDocument() view = currentDocument.activeView() source = view.selectionText() try: block_type, block_source = source.split("#") except ValueError: block_type = 'block' block_source = source view.removeSelectionText() insertText("{%% %(block_type)s %(block_source)s %%}XXX{%% end%(block_type)s %%}" % {'block_type': block_type, 'block_source': block_source})
def insertInit(): """Insert the __init__ function (Into a class)""" class_name = TEXT_TO_CHANGE currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() while currentLine >= 0: text = unicode(currentDocument.line(currentLine)) match = pattern_class.match(text) if match: class_name = match.groups()[0] break currentLine = currentLine - 1 insertText(TEXT_INIT % class_name)
def insertSuper(): """Insert the call to the parent method (Into a method of a class)""" currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() indent, class_name, func_name, params = get_prototype_of_current_func() text = currentDocument.line(currentLine).strip() text_super_template = TEXT_SUPER if not text: currentDocument.removeLine(currentPosition.line()) else: indent = '' if currentLine == currentDocument.lines(): text_super_template = '\n%s' % text_super_template insertText(text_super_template % (indent, class_name, params[0], func_name, ', '.join(params[1:])))
def togglePrettyJsonFormat(): """A simple JSON pretty printer. JSON formatter which a good indents""" currentDocument = kate.activeDocument() view = currentDocument.activeView() source = view.selectionText() if not source: kate.gui.popup('Select a json text', 2, icon='dialog-warning', minTextWidth=200) else: try: target = simplejson.dumps(simplejson.loads(source), indent=2) view.removeSelectionText() text.insertText(target) except simplejson.JSONDecodeError: kate.gui.popup('This text is not a valid json text', 2, icon='dialog-warning', minTextWidth=200)
def insertSuper(): """Insert the call to the parent method (Into a method of a class)""" currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() indent, class_name, func_name, params = get_prototype_of_current_func() text = currentDocument.line(currentLine).strip() text_super_template = TEXT_SUPER if not text: currentDocument.removeLine(currentPosition.line()) else: indent = '' if currentLine == currentDocument.lines(): text_super_template = '\n%s' % text_super_template insertText( text_super_template % (indent, class_name, params[0], func_name, ', '.join(params[1:])))
def callRecursive(): """Insert the recursive call (Into a method of a class or into a function)""" currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() indent, class_name, func_name, params = get_prototype_of_current_func() text = currentDocument.line(currentLine).strip() if class_name != TEXT_TO_CHANGE: text_recursive_template = TEXT_RECURSIVE_CLASS % (indent, params[0], func_name, ', '.join(params[1:])) else: text_recursive_template = TEXT_RECURSIVE_NO_CLASS % (indent, func_name, ', '.join(params)) if not text: currentDocument.removeLine(currentPosition.line()) else: text_recursive_template = text_recursive_template.lstrip() if currentLine == currentDocument.lines(): text_recursive_template = '\n%s' % text_recursive_template insertText(text_recursive_template)
def togglePrettyJsonFormat(): """Pretty format of a XML code""" currentDocument = kate.activeDocument() view = currentDocument.activeView() source = unicode(view.selectionText()).encode('utf-8', 'ignore') if not source: kate.gui.popup('Select a xml text', 2, icon='dialog-warning', minTextWidth=200) else: try: target = minidom.parseString(source) view.removeSelectionText() xml_pretty = target.toprettyxml() xml_pretty = '\n'.join([line for line in xml_pretty.split("\n") if line.replace(' ', '').replace('\t', '')]) text.insertText(xml_pretty) except ExpatError: kate.gui.popup('This text is not a valid xml text', 2, icon='dialog-warning', minTextWidth=200)
def insertInit(): """Insert the __init__ function (Into a class)""" indent = '' class_name = TEXT_TO_CHANGE currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() while currentLine >= 0: text = currentDocument.line(currentLine) match = pattern_class.match(text) if match: indent = match.groups()[0] class_name = match.groups()[1] break currentLine = currentLine - 1 text_init = TEXT_INIT % class_name if indent: text_init = '\n'.join(['%s%s' % (indent, l) for l in text_init.split('\n')]) insertText('\n%s' % text_init)
def callRecursive(): """Insert the recursive call (Into a method of a class or into a function)""" currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() indent, class_name, func_name, params = get_prototype_of_current_func() text = currentDocument.line(currentLine).strip() if class_name != TEXT_TO_CHANGE: text_recursive_template = TEXT_RECURSIVE_CLASS % ( indent, params[0], func_name, ', '.join(params[1:])) else: text_recursive_template = TEXT_RECURSIVE_NO_CLASS % (indent, func_name, ', '.join(params)) if not text: currentDocument.removeLine(currentPosition.line()) else: text_recursive_template = text_recursive_template.lstrip() if currentLine == currentDocument.lines(): text_recursive_template = '\n%s' % text_recursive_template insertText(text_recursive_template)
def insertInit(): """Insert the __init__ function (Into a class)""" indent = '' class_name = TEXT_TO_CHANGE currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() while currentLine >= 0: text = currentDocument.line(currentLine) match = pattern_class.match(text) if match: indent = match.groups()[0] class_name = match.groups()[1] break currentLine = currentLine - 1 text_init = TEXT_INIT % class_name if indent: text_init = '\n'.join( ['%s%s' % (indent, l) for l in text_init.split('\n')]) insertText('\n%s' % text_init)
def insertIPDB(): """Insert the instructions to debug the python code""" python_utils_conf = kate.configuration.root.get('python_utils', {}) ipdb_snippet = python_utils_conf.get(_IPDB_SNIPPET, DEFAULT_IPDB_SNIPPET) insertText(ipdb_snippet)
def insert_jquery_ready_action(): """Snippet with the ready code of the jQuery""" text.insertText(SETTING_JQUERY_READY.lookup(), start_in_current_column=True)
def insertReady(): """Snippet with the ready code of the jQuery""" text.insertText(TEXT_JQUERY % text.TEXT_TO_CHANGE, start_in_current_column=True)
def insertIPDB(): """Insert the instructions to debug the python code""" insertText("import ipdb; ipdb.set_trace()")
def importViews(): """Insert the usual imports of the views.py file""" insertText(TEXT_VIEWS)
def importViews(): """Insert the usual imports of the views.py file""" django_utils_conf = kate.configuration.root.get('django_utils', {}) text_views = django_utils_conf.get(_TEMPLATE_TEXT_VIEWS, DEFAULT_TEXT_VIEWS) insertText(text_views)