def importUrls(): 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 closeTemplateTag(): 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 closeTemplateTag(): 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 createBlock(): 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(): 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 createBlock(): 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 insertSuper(): currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() espaces, class_name, func_name, params = get_prototype_of_current_func() text = unicode(currentDocument.line(currentLine)).strip() text_super_template = TEXT_SUPER if not text: currentDocument.removeLine(currentPosition.line()) else: espaces = "" if currentLine == currentDocument.lines(): text_super_template = "\n%s" % text_super_template insertText(text_super_template % (espaces, class_name, params[0], func_name, ", ".join(params[1:])))
def togglePrettyJsonFormat(): 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) try: target = simplejson.dumps(simplejson.loads(source), indent=2) view.removeSelectionText() insertText(target) except simplejson.JSONDecodeError: kate.gui.popup('This text is not a valid json text', 2, icon='dialog-warning', minTextWidth=200)
def insertSuper(): currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() espaces, class_name, func_name, params = get_prototype_of_current_func() text = unicode(currentDocument.line(currentLine)).strip() text_super_template = TEXT_SUPER if not text: currentDocument.removeLine(currentPosition.line()) else: espaces = '' if currentLine == currentDocument.lines(): text_super_template = '\n%s' % text_super_template insertText( text_super_template % (espaces, class_name, params[0], func_name, ', '.join(params[1:])))
def closeTemplateTag(): template_tags = "|".join(TEMPLATE_TAGS_CLOSE) pattern_close = re.compile( "(.)*{%%%(espaces)s(%(tags)s)%(espaces)s(.)*%(espaces)s%%}(.)*" % {"espaces": str_blank, "tags": template_tags} ) currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() while currentLine >= 0: text = unicode(currentDocument.line(currentLine)) match = pattern_close.match(text) if match: tag = match.groups()[1] break currentLine = currentLine - 1 insertText("{%% end%s %%}" % tag)
def togglePrettyJsonFormat(): 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) 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', '')]) insertText(xml_pretty) except ExpatError: kate.gui.popup('This text is not a valid xml text', 2, icon='dialog-warning', minTextWidth=200)
def togglePrettyJsonFormat(): currentDocument = kate.activeDocument() view = currentDocument.activeView() source = str(view.selectionText()).encode('utf-8', 'ignore') if not source: kate.gui.popup('Select a xml text', 2, icon='dialog-warning', minTextWidth=200) 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', '')]) insertText(xml_pretty) except ExpatError: kate.gui.popup('This text is not a valid xml text', 2, icon='dialog-warning', minTextWidth=200)
def togglePrettyJsonFormat(): currentDocument = kate.activeDocument() view = currentDocument.activeView() encoding = unicode(currentDocument.encoding()) source = unicode(view.selectionText(), encoding).encode(encoding) if not source: kate.gui.popup('Select a json text', 2, icon='dialog-warning', minTextWidth=200) pp = pprint.PrettyPrinter(indent=1) try: target = pp.pformat(simplejson.loads(source)) view.removeSelectionText() insertText(target.replace("'", '"')) except simplejson.JSONDecodeError: kate.gui.popup('This text is not a valid json text', 2, icon='dialog-warning', minTextWidth=200)
def callRecursive(): currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() espaces, class_name, func_name, params = get_prototype_of_current_func() text = unicode(currentDocument.line(currentLine)).strip() if class_name != TEXT_TO_CHANGE: text_recursive_template = TEXT_RECURSIVE_CLASS % (espaces, params[0], func_name, ", ".join(params[1:])) else: text_recursive_template = TEXT_RECURSIVE_NO_CLASS % (espaces, 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 callRecursive(): currentDocument = kate.activeDocument() view = currentDocument.activeView() currentPosition = view.cursorPosition() currentLine = currentPosition.line() espaces, class_name, func_name, params = get_prototype_of_current_func() text = unicode(currentDocument.line(currentLine)).strip() if class_name != TEXT_TO_CHANGE: text_recursive_template = TEXT_RECURSIVE_CLASS % ( espaces, params[0], func_name, ', '.join(params[1:])) else: text_recursive_template = TEXT_RECURSIVE_NO_CLASS % ( espaces, 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 importViews(): insertText(TEXT_VIEWS)
def insertIPDB(): insertText("import ipdb; ipdb.set_trace()")
def insertReady(): insertText(TEXT_JQUERY % TEXT_TO_CHANGE, start_in_current_column=True)