Exemplo n.º 1
0
def process_snippet(item, profile, level):
    """
	Processes element with <code>snippet</code> type
	@type item: ZenNode
	@type profile: dict
	@type level: int
	"""
    data = item.source.value

    if not data:
        # snippet wasn't found, process it as tag
        return process_tag(item, profile, level)

    tokens = data.split(child_token)
    if len(tokens) < 2:
        start = tokens[0]
        end = ''
    else:
        start, end = tokens

    padding = item.parent and item.parent.padding or ''

    item.start = _replace(item.start, zen_coding.pad_string(start, padding))
    item.end = _replace(item.end, zen_coding.pad_string(end, padding))

    return item
Exemplo n.º 2
0
def process_snippet(item, profile, level):
	"""
	Processes element with <code>snippet</code> type
	@type item: ZenNode
	@type profile: dict
	@type level: int
	"""
	data = item.source.value;
		
	if not data:
		# snippet wasn't found, process it as tag
		return process_tag(item, profile, level)
		
	tokens = data.split(child_token)
	if len(tokens) < 2:
		start = tokens[0]
		end = ''
	else:
		start, end = tokens
		
	padding = item.parent and item.parent.padding or ''
		
	item.start = _replace(item.start, zen_coding.pad_string(start, padding))
	item.end = _replace(item.end, zen_coding.pad_string(end, padding))
	
	return item
Exemplo n.º 3
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
Exemplo n.º 4
0
def expand_abbreviation(*l_arg, **d_arg):

    profile_name, doc_type = get_profile_name(), get_doc_type()

    cur_line = vim.current.line
    cur_index = vim.current.window.cursor[1]
    cur_line_num = vim.current.window.cursor[0] - 1

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

    abbr, start_index = (None, None)
    if "prompt" in d_arg and d_arg["prompt"]:
        if cur_index + 1 == len(cur_line):
            cur_index += 1
        abbr = vim.eval('input("Expand abbreviation: ")')
        start_index = cur_index
    else:
        if cur_index + 1 == len(cur_line):
            cur_index += 1
            abbr, start_index = zen_core.find_abbr_in_line(cur_line, cur_index)
            if not abbr:
                cur_index -= 1
        if not abbr:
            abbr, start_index = zen_core.find_abbr_in_line(cur_line, cur_index)

    if abbr:
        result = cur_line[0:start_index] + zen_core.expand_abbreviation(abbr, doc_type, profile_name)
        if result:
            cur_line_pad = re.match(r"^(\s+)", cur_line)
            if cur_line_pad:
                result = zen_core.pad_string(result, cur_line_pad.group(1))
            vim.current.buffer[cur_line_num : cur_line_num + 1] = (
                result.replace(insertion_point.placeholder, "", 1) + cur_line[cur_index:]
            ).split("\n")
            if "set_return" in d_arg and d_arg["set_return"]:
                vim.command("let l:can_replace = 1")
            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)
                    break
Exemplo n.º 5
0
def process(tree, profile, level=0):
	"""
	Processes simplified tree, making it suitable for output as HTML structure
	@type item: ZenNode
	@type profile: dict
	@param level: Depth level
	@type level: int
	"""
	
	for item in tree.children:
		if item.type == 'tag':
			item = process_tag(item, profile, level)
		else:
			item = process_snippet(item, profile, level)
		
		if item.content:
			item.content = zen_coding.pad_string(item.content, item.padding)
			
		process(item, profile, level + 1)
	
	return tree
Exemplo n.º 6
0
def process(tree, profile, level=0):
    """
	Processes simplified tree, making it suitable for output as HTML structure
	@type item: ZenNode
	@type profile: dict
	@param level: Depth level
	@type level: int
	"""

    for item in tree.children:
        if item.type == 'tag':
            item = process_tag(item, profile, level)
        else:
            item = process_snippet(item, profile, level)

        if item.content:
            item.content = zen_coding.pad_string(item.content, item.padding)

        process(item, profile, level + 1)

    return tree
Exemplo n.º 7
0
	def replace_content(self, value, start=None, end=None):
		"""
		Replace editor's content or it's part (from <code>start</code> to
		<code>end</code> index). If <code>value</code> contains
		<code>caret_placeholder</code>, the editor will put caret into
		this position. If you skip <code>start</code> and <code>end</code>
		arguments, the whole target's content will be replaced with
		<code>value</code>.

		If you pass <code>start</code> argument only,
		the <code>value</code> will be placed at <code>start</code> string
		index of current content.

		If you pass <code>start</code> and <code>end</code> arguments,
		the corresponding substring of current target's content will be
		replaced with <code>value</code>
		@param value: Content you want to paste
		@type value: str
		@param start: Start index of editor's content
		@type start: int
		@param end: End index of editor's content
		@type end: int
		"""
		if start is None: start = 0
		if end is None: end = len(self.get_content())
		rng = tea.new_range(start, end - start)
		value = self.add_placeholders(value)
		
		value = zen_coding.pad_string(value, get_line_padding(self.get_current_line()))
		
		cursor_loc = value.find('$0')
		if cursor_loc != -1:
			select_range = tea.new_range(cursor_loc + rng.location, 0)
			value = value.replace('$0', '')
			tea.insert_text_and_select(self._context, value, rng, select_range)
		else:
			tea.insert_text(self._context, value, rng)
from zencoding import zen_core
from zen_editor import ZenEditor

editor = ZenEditor()
"""
In order to make "Expand Abbreviation" more natural to
TextMate's bundle system we have to forget about predefined Zen Coding actions
and write our own
"""

abbr = os.getenv('TM_SELECTED_TEXT', '')
if abbr:
    result = zen_core.expand_abbreviation(abbr, editor.get_syntax(),
                                          editor.get_profile_name())
    if result:
        sys.stdout.write(editor.add_placeholders(result))
else:
    cur_line = os.getenv('TM_CURRENT_LINE', '')
    cur_index = int(os.getenv('TM_LINE_INDEX', 0))
    line = cur_line[0:cur_index]
    abbr = zen_core.extract_abbreviation(line)

    if abbr:
        result = line[0:-len(abbr)] + zen_core.expand_abbreviation(
            abbr, editor.get_syntax(), editor.get_profile_name())
        cur_line_pad = re.match(r'^(\s+)', cur_line)
        if cur_line_pad:
            result = zen_core.pad_string(result, cur_line_pad.group(1))

        sys.stdout.write(
            editor.add_placeholders(result) + cur_line[cur_index:])
Exemplo n.º 9
0
try:
	if 'xsl' in scope:
		doc_type = 'xsl'
	else:
		doc_type = re.findall(r'\bhtml|css|xml\b', scope)[-1]
except:
	doc_type = 'html'

# doc_type = re.search(r'\b(html|css|xml|xsl)\b', scope)
if not doc_type:
	doc_type = 'html'

profile_name = 'xhtml'
"Output profile name"

abbr = os.getenv('TM_SELECTED_TEXT', '')
if abbr:
	result = zen_core.expand_abbreviation(abbr, doc_type, profile_name)
	if result:
		sys.stdout.write(result)
else:
	abbr, start_index = zen_core.find_abbr_in_line(cur_line, cur_index)

	if abbr:
		result = cur_line[0:start_index] + zen_core.expand_abbreviation(abbr, doc_type, profile_name)
		cur_line_pad = re.match(r'^(\s+)', cur_line)
		if cur_line_pad:
			result = zen_core.pad_string(result, cur_line_pad.group(1))

		sys.stdout.write(result + cur_line[cur_index:])