Example #1
0
def wrap_with_abbreviation():

    profile_name, doc_type = get_profile_name(), get_doc_type()

    abbr = vim.eval('input("Wrap with abbreviation: ")')
    text = "\n".join(vim.current.range[:])
    cur_line = vim.current.range[0]
    cur_line_pad = re.match(r"^(\s+)", cur_line)
    cur_line_num = vim.current.range.start
    if cur_line_pad:
        cur_line_pad_str = cur_line_pad.group(1)
        cur_line_pad_len = len(cur_line_pad_str)
        text = text[cur_line_pad_len:].split("\n")
        for line_num in range(len(text)):
            if text[line_num][0:cur_line_pad_len] == cur_line_pad_str:
                text[line_num] = text[line_num][cur_line_pad_len:]
        text = "\n".join(text)

    insertion_point = insertion_point_maker(text)
    get_insertion_point = insertion_point.get_insertion_point
    zen_core.insertion_point = get_insertion_point
    zen_core.sub_insertion_point = get_insertion_point

    result = zen_core.wrap_with_abbreviation(abbr, text, doc_type, profile_name)
    if result:
        if cur_line_pad:
            result = zen_core.pad_string(result, cur_line_pad.group(1))
        result = (cur_line_pad.group(1) if cur_line_pad else "") + result
        vim.current.range[:] = result.replace(insertion_point.placeholder, "", 1).split("\n")
        for line in result.split("\n"):
            cur_line_num += 1
            pos = line.find(insertion_point.placeholder)
            if pos > -1:
                vim.current.window.cursor = (cur_line_num, pos - 1)
                break
Example #2
0
def wrap_with_abbreviation(editor, abbr, syntax=None, profile_name=None):
	"""
	Wraps content with abbreviation
	@param editor: Editor instance
	@type editor: ZenEditor
	@param syntax: Syntax type (html, css, etc.)
	@type syntax: str
	@param profile_name: Output profile name (html, xml, xhtml)
	@type profile_name: str
	"""
	if not abbr: return None
	if syntax is None: syntax = editor.get_syntax()
	if profile_name is None: profile_name = editor.get_profile_name()
	start_offset, end_offset = editor.get_selection_range()
	content = editor.get_content()
	if start_offset == end_offset:
		# no selection, find tag pair
		rng = html_matcher.match(content, start_offset, profile_name)
		if rng[0] is None: # nothing to wrap
			return None
		else:
			start_offset, end_offset = rng
	start_offset, end_offset = narrow_to_non_space(content, start_offset, end_offset)
	line_bounds = get_line_bounds(content, start_offset)
	padding = get_line_padding(content[line_bounds[0]:line_bounds[1]])
	new_content = content[start_offset:end_offset]
	result = zen_coding.wrap_with_abbreviation(abbr, unindent_text(new_content, padding), syntax, profile_name)
	if result:
		editor.replace_content(result, start_offset, end_offset)
		return True
	return False
Example #3
0
def wrap(abbr, content):
	return zen.wrap_with_abbreviation(abbr, content)
 def wrap(self, context, abbr, profile_name='xhtml'):
     # Set up the config variables
     zen_settings = settings_loader.load_settings()
     zen.update_settings(zen_settings)
     zen.newline = self.safe_str(tea.get_line_ending(context))
     zen_settings['variables']['indentation'] = self.safe_str(tea.get_indentation_string(context))
     
     # This allows us to use smart incrementing tab stops in zen snippets
     point_ix = [0]
     def place_ins_point(text):
         if not point_ix[0]:
             point_ix[0] += 1
             return '$0'
         else:
             return ''
     zen.insertion_point = place_ins_point
     
     text, rng = tea.selection_and_range(context)
     if not text:
         # no selection, find matching tag
         content = context.string()
         start, end = html_matcher.match(content, rng.location)
         if start is None:
             # nothing to wrap
             return False
         
         def is_space(char):
             return char.isspace() or char in r'\n\r'
         
         # narrow down selection until first non-space character
         while start < end:
             if not is_space(content[start]):
                 break
             start += 1
         
         while end > start:
             end -= 1
             if not is_space(content[end]):
                 end += 1
                 break
         
         rng = tea.new_range(start, end - start)
         text = tea.get_selection(context, rng)
         
     # Fetch the doctype based on file extension
     doc_type = tea.get_zen_doctype(context)
     
     text = self.unindent(context, text)
     
     # Damn Python's encodings! Have to convert string to ascii before wrapping 
     # and then back to utf-8
     result = zen.wrap_with_abbreviation(self.safe_str(abbr), self.safe_str(text), doc_type, profile_name)
     result = unicode(result, 'utf-8')
     
     result = tea.indent_snippet(context, result, rng)
     result = tea.clean_line_endings(context, result)
     
     cursor_loc = result.find('$0')
     if cursor_loc != -1:
         select_range = tea.new_range(cursor_loc + rng.location, 0)
         result = result.replace('$0', '')
         tea.insert_text_and_select(context, result, rng, select_range)
     else:
         tea.insert_text(context, result, rng)