def insert_formatted_newline(editor, mode='html'): """ Inserts newline character with proper indentation @param editor: Editor instance @type editor: ZenEditor @param mode: Syntax mode (only 'html' is implemented) @type mode: str """ caret_pos = editor.get_caret_pos() nl = zen_coding.get_newline() pad = zen_coding.get_variable('indentation') if mode == 'html': # let's see if we're breaking newly created tag pair = html_matcher.get_tags(editor.get_content(), editor.get_caret_pos(), editor.get_profile_name()) if pair[0] and pair[1] and pair[0]['type'] == 'tag' and pair[0][ 'end'] == caret_pos and pair[1]['start'] == caret_pos: editor.replace_content( nl + pad + zen_coding.get_caret_placeholder() + nl, caret_pos) else: editor.replace_content(nl, caret_pos) else: editor.replace_content(nl, caret_pos) return True
def __init__(self, context=None): self._content = '' "Editor's content" self.apple_script = os.path.join(os.getenv('TM_BUNDLE_SUPPORT'), 'pasteboard.scpt') zen.set_newline(os.getenv('TM_LINE_ENDING', zen.get_newline())) self.set_context(context)
def set_caret_pos(self, pos): """ Set new caret position @type pos: int """ # figure out line and column vars head = zen.split_by_lines(self.get_content()[0:pos]) line = max(len(head), 1) column = pos - len(zen.get_newline().join(head[0:-1])) subprocess.Popen(['open', 'txmt://open/?line=%d&column=%d' % (line, column)]).communicate()
def split_join_tag(editor, profile_name=None): """ Splits or joins tag, e.g. transforms it into a short notation and vice versa: <div></div> → <div /> : join <div /> → <div></div> : split @param editor: Editor instance @type editor: ZenEditor @param profile_name: Profile name @type profile_name: str """ caret_pos = editor.get_caret_pos() profile = zen_coding.get_profile(profile_name or editor.get_profile_name()) caret = zen_coding.get_caret_placeholder() # find tag at current position pair = html_matcher.get_tags(editor.get_content(), caret_pos, profile_name or editor.get_profile_name()) if pair and pair[0]: new_content = pair[0].full_tag if pair[1]: # join tag closing_slash = '' if profile['self_closing_tag'] is True: closing_slash = '/' elif profile['self_closing_tag'] == 'xhtml': closing_slash = ' /' new_content = re.sub(r'\s*>$', closing_slash + '>', new_content) # add caret placeholder if len(new_content) + pair[0].start < caret_pos: new_content += caret else: d = caret_pos - pair[0].start new_content = new_content[0:d] + caret + new_content[d:] editor.replace_content(new_content, pair[0].start, pair[1].end) else: # split tag nl = zen_coding.get_newline() pad = zen_coding.get_variable('indentation') # define tag content depending on profile tag_content = profile[ 'tag_nl'] is True and nl + pad + caret + nl or caret new_content = '%s%s</%s>' % (re.sub( r'\s*\/>$', '>', new_content), tag_content, pair[0].name) editor.replace_content(new_content, pair[0].start, pair[0].end) return True else: return False
def unindent_text(text, pad): """ Removes padding at the beginning of each text's line @type text: str @type pad: str """ lines = zen_coding.split_by_lines(text) for i,line in enumerate(lines): if line.startswith(pad): lines[i] = line[len(pad):] return zen_coding.get_newline().join(lines)
def unindent_text(text, pad): """ Removes padding at the beginning of each text's line @type text: str @type pad: str """ lines = zen_coding.split_by_lines(text) for i, line in enumerate(lines): if line.startswith(pad): lines[i] = line[len(pad):] return zen_coding.get_newline().join(lines)
def unindent(self, context, text): """ Unindent content, thus preparing text for tag wrapping @param text: str @return str """ pad = self.get_current_line_padding(context) lines = zen.split_by_lines(text) for i,line in enumerate(lines): if line.find(pad) == 0: lines[i] = line[len(pad):] return zen.get_newline().join(lines)
def split_join_tag(editor, profile_name=None): """ Splits or joins tag, e.g. transforms it into a short notation and vice versa: <div></div> → <div /> : join <div /> → <div></div> : split @param editor: Editor instance @type editor: ZenEditor @param profile_name: Profile name @type profile_name: str """ caret_pos = editor.get_caret_pos() profile = zen_coding.get_profile(profile_name or editor.get_profile_name()) caret = zen_coding.get_caret_placeholder() # find tag at current position pair = html_matcher.get_tags(editor.get_content(), caret_pos, profile_name or editor.get_profile_name()) if pair and pair[0]: new_content = pair[0].full_tag if pair[1]: # join tag closing_slash = '' if profile['self_closing_tag'] is True: closing_slash = '/' elif profile['self_closing_tag'] == 'xhtml': closing_slash = ' /' new_content = re.sub(r'\s*>$', closing_slash + '>', new_content) # add caret placeholder if len(new_content) + pair[0].start < caret_pos: new_content += caret else: d = caret_pos - pair[0].start new_content = new_content[0:d] + caret + new_content[d:] editor.replace_content(new_content, pair[0].start, pair[1].end) else: # split tag nl = zen_coding.get_newline() pad = zen_coding.get_variable('indentation') # define tag content depending on profile tag_content = profile['tag_nl'] is True and nl + pad + caret + nl or caret new_content = '%s%s</%s>' % (re.sub(r'\s*\/>$', '>', new_content), tag_content, pair[0].name) editor.replace_content(new_content, pair[0].start, pair[0].end) return True else: return False
def add_comments(node, i): """ Add comments to tag @type node: ZenNode @type i: int """ id_attr = node.get_attribute('id') class_attr = node.get_attribute('class') nl = zen_coding.get_newline() if id_attr or class_attr: comment_str = '' padding = node.parent and node.parent.padding or '' if id_attr: comment_str += '#' + id_attr if class_attr: comment_str += '.' + class_attr node.start = node.start.replace('<', '<!-- ' + comment_str + ' -->' + nl + padding + '<', 1) node.end = node.end.replace('>', '>' + nl + padding + '<!-- /' + comment_str + ' -->', 1) # replace counters node.start = zen_coding.replace_counter(node.start, i + 1) node.end = zen_coding.replace_counter(node.end, i + 1)
def insert_formatted_newline(editor, mode='html'): """ Inserts newline character with proper indentation @param editor: Editor instance @type editor: ZenEditor @param mode: Syntax mode (only 'html' is implemented) @type mode: str """ caret_pos = editor.get_caret_pos() nl = zen_coding.get_newline() pad = zen_coding.get_variable('indentation') if mode == 'html': # let's see if we're breaking newly created tag pair = html_matcher.get_tags(editor.get_content(), editor.get_caret_pos(), editor.get_profile_name()) if pair[0] and pair[1] and pair[0]['type'] == 'tag' and pair[0]['end'] == caret_pos and pair[1]['start'] == caret_pos: editor.replace_content(nl + pad + zen_coding.get_caret_placeholder() + nl, caret_pos) else: editor.replace_content(nl, caret_pos) else: editor.replace_content(nl, caret_pos) return True
def add_comments(node, i): """ Add comments to tag @type node: ZenNode @type i: int """ id_attr = node.get_attribute('id') class_attr = node.get_attribute('class') nl = zen_coding.get_newline() if id_attr or class_attr: comment_str = '' padding = node.parent and node.parent.padding or '' if id_attr: comment_str += '#' + id_attr if class_attr: comment_str += '.' + class_attr node.start = node.start.replace( '<', '<!-- ' + comment_str + ' -->' + nl + padding + '<', 1) node.end = node.end.replace( '>', '>' + nl + padding + '<!-- /' + comment_str + ' -->', 1) # replace counters node.start = zen_coding.replace_counter(node.start, i + 1) node.end = zen_coding.replace_counter(node.end, i + 1)
def get_newline(): return zen_coding.get_newline()