def act(controller, bundle, options):
    context = tea.get_context(controller)
    
    snippet = tea.get_option(options, 'snippet', '')
    maintain_selection = tea.get_option(options, 'maintain_selection', False)
    
    text, range = tea.selection_and_range(context)
    
    snippet = tea.indent_snippet(context, snippet, range)
    snippet = tea.clean_line_endings(context, snippet)
    
    # Set up target selection
    sel_loc = snippet.find('$SELECTED_TEXT')
    cursor_loc = snippet.find('$0')
    if maintain_selection:
        select_range = tea.new_range(sel_loc + range.location, range.length)
    elif cursor_loc != -1:
        select_range = tea.new_range(snippet.find('$0') + range.location, 0)
    else:
        select_range = None
    
    snippet = snippet.replace('$SELECTED_TEXT', text)
    snippet = snippet.replace('$0', '')
    if select_range is not None:
        tea.insert_text_and_select(context, snippet, range, select_range)
    else:
        tea.insert_text(context, snippet, range)
示例#2
0
def act(controller, bundle, options):
    '''
    Required action method
    
    input dictates what should be trimmed:
    - None (default): falls back to alternate
    - selection: ignores lines if they exist, just trims selection
    - selected_lines: each line in the selection
    
    alternate dictates what to fall back on
    - None (default): will do nothing if input is blank
    - line: will trim the line the caret is on
    - all_lines: all lines in the document
    
    trim dictates what part of the text should be trimmed:
    - both (default)
    - start
    - end
    
    If respect_indent is True, indent characters (as defined in preferences)
    at the beginning of the line will be left untouched.
    '''
    context = tea.get_context(controller)
    
    input = tea.get_option(options, 'input')
    alternate = tea.get_option(options, 'alternate')
    trim = tea.get_option(options, 'trim', 'both')
    respect_indent = tea.get_option(options, 'respect_indent', False)
    discard_empty = tea.get_option(options, 'discard_empty', False)
    
    # Since input is always a selection of some kind, check if we have one
    range = tea.get_range(context)
    
    if (range.length == 0) or input is None:
        if alternate.lower() == 'line':
            text, range = tea.get_line(context)
            text = tea.trim(context, text, False, trim, respect_indent, True, discard_empty)
        elif alternate.lower() == 'all_lines':
            range = tea.new_range(0, context.string().length())
            text = tea.get_selection(context, range)
            text = tea.trim(context, text, True, trim, respect_indent, True, discard_empty)
    else:
        if input.lower() == 'selected_lines':
            parse_lines = True
        else:
            parse_lines = False
        text = tea.get_selection(context, range)
        text = tea.trim(context, text, parse_lines, trim, respect_indent, True, discard_empty)
    tea.insert_text(context, text, range)
    new_range = tea.new_range(range.location, len(text))
    tea.set_selected_range(context, new_range)
示例#3
0
def act(controller, bundle, options):
	context = tea.get_context(controller)
	action_name = tea.get_option(options, 'action', '')
	editor = ZenEditor(context, bundle)
	
	try:
		return zencoding.run_action(action_name, editor)
	except zencoding.utils.ZenError:
		tea.say(context, 'Error while performing Zen Coding action', sys.exc_info()[1].value)
	except:
		msg_writer = SimpleWriter()
		msg = traceback.print_exc(file=msg_writer)
		tea.say(context, 'Runtime error', msg_writer.get())
		
示例#4
0
def act(controller, bundle, options):
    context = tea.get_context(controller)
    action_name = tea.get_option(options, 'action', '')
    editor = ZenEditor(context, bundle)

    try:
        return zencoding.run_action(action_name, editor)
    except zencoding.utils.ZenError:
        tea.say(context, 'Error while performing Zen Coding action',
                sys.exc_info()[1].value)
    except:
        msg_writer = SimpleWriter()
        msg = traceback.print_exc(file=msg_writer)
        tea.say(context, 'Runtime error', msg_writer.get())
def act(controller, bundle, options):
    # Grab the context
    context = tea.get_context(controller)
    
    image = find_image(context)
    if image:
        size = get_image_size(context, image['tag'])
        if size:
            new_tag = replace_or_append(image['tag'], 'width', size['width'])
            new_tag = replace_or_append(new_tag, 'height', size['height'])
            
            rng = tea.get_range(context)
            tea.insert_text(context, new_tag, tea.new_range(image['start'], image['end'] - image['start']))
            tea.set_selected_range(context, rng)
            return True
    
    return False
示例#6
0
def act(controller, bundle, options):
    # Grab the context
    context = tea.get_context(controller)
    
    # Setup the options
    fallback = tea.get_option(options, 'fallback', '')
    snippet = tea.get_option(options, 'snippet', '$URL')
    
    # Get the clipboard contents, parse for a URL
    process = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
    clipboard, error = process.communicate(None)
    # Construct the default link
    url = format_hyperlink(clipboard, fallback)
    
    # Grab the selected text and range
    text, range = tea.selection_and_range(context)
    
    # Parse the snippet for $SELECTED_TEXT placeholder
    sel_loc = snippet.find('$SELECTED_TEXT')
    if sel_loc != -1:
        replace_text = True
        prefix = snippet[0:sel_loc]
        suffix = snippet[sel_loc+14:]
    else:
        replace_text = False
        prefix = snippet
        suffix = ''
    
    prefix = prefix.replace('$URL', url)
    suffix = suffix.replace('$URL', url)
    
    if replace_text:
        replacement = prefix + text + suffix
    else:
        replacement = prefix
    
    url_loc = replacement.find(url)
    if url_loc == -1:
        url_loc = len(replacement)
        url_len = 0
    else:
        url_len = len(url)
    newrange = tea.new_range(url_loc + range.location, url_len)
    
    tea.insert_text_and_select(context, replacement, range, newrange)
示例#7
0
def act(controller, bundle, options):
    context = tea.get_context(controller)
    direction = tea.get_option(options, 'direction', 'out')
    
    # Since input is always a selection of some kind, check if we have one
    
    rng = tea.get_range(context)
    cursor = rng.location + rng.length
    range_start, range_end = rng.location, rng.location + rng.length
    content = context.string()
    
    old_open_tag = html_matcher.last_match['opening_tag']
    old_close_tag = html_matcher.last_match['closing_tag']
    
    if direction.lower() == 'in' and old_open_tag and range_start != range_end:
        # user has previously selected tag and wants to move inward
        if not old_close_tag:
            # unary tag was selected, can't move inward
            return False
        elif old_open_tag.start == range_start:
            if content[old_open_tag.end] == '<':
                # test if the first inward tag matches the entire parent tag's content
                _start, _end = html_matcher.find(content, old_open_tag.end + 1)
                if _start == old_open_tag.end and _end == old_close_tag.start:
                    start, end = html_matcher.match(content, old_open_tag.end + 1)
                else:
                    start, end = old_open_tag.end, old_close_tag.start
            else:
                start, end = old_open_tag.end, old_close_tag.start
        else:
            new_cursor = content.find('<', old_open_tag.end, old_close_tag.start)
            search_pos = new_cursor != -1 and new_cursor + 1 or old_open_tag.end
            start, end = html_matcher.match(content, search_pos)
            
    else:
        start, end = html_matcher.match(content, cursor)
    
    if start is not None:
        new_range = tea.new_range(start, end - start)
        tea.set_selected_range(context, new_range)
        return True
    else:
        return False
示例#8
0
def act(controller, bundle, options):
    context = tea.get_context(controller)
    action_name = tea.get_option(options, "action", "")
    editor = ZenEditor(context)

    return zen_coding.run_action(action_name, editor)
def act(controller, bundle, options):
    context = tea.get_context(controller)
    
    # Get the options
    alpha_numeric = tea.get_option(options, 'alpha_numeric', True)
    extra_characters = tea.get_option(options, 'extra_characters', '_-')
    bidirectional = tea.get_option(options, 'bidirectional', True)
    snippet = tea.get_option(options, 'snippet', '<$SELECTED_TEXT>$0</$WORD>')
    mode = tea.get_option(options, 'mode', '')
    
    # Fetch the word
    range = context.selectedRange()
    word, new_range = tea.get_word_or_selection(context, range, alpha_numeric,
                                                extra_characters, bidirectional)
    if word == '':
        # No word, so nothing further to do
        return False
    # If we're using $WORD, make sure the word is just a word
    if snippet.find('$WORD') >= 0:
        fullword = word
        word = tea.parse_word(word)
        if word is None:
            word = ''
    else:
        fullword = word
    
    # Process that sucker!
    if mode == 'zen' and fullword.find(' ') < 0:
        # Explicitly load zen settings
        zen_settings = settings_loader.load_settings()
        zen_core.update_settings(zen_settings)
        
        # Set up the config variables
        zen_core.newline = tea.get_line_ending(context)
        zen_settings['variables']['indentation'] = 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_core.insertion_point = place_ins_point
        
        # Determine doctype as best we can based on file extension
        doc_type = tea.get_zen_doctype(context)
        
        # Prepare the snippet
        snippet = zen_core.expand_abbreviation(fullword, doc_type, 'xhtml')
    elif mode == 'zen' and tea.is_selfclosing(word):
        # Self-closing, so construct the snippet from scratch
        snippet = '<' + fullword
        if fullword == word and not fullword in ['br', 'hr']:
            snippet += ' $0 />'
        else:
            snippet += ' />$0'
    # Indent the snippet
    snippet = tea.indent_snippet(context, snippet, new_range)
    snippet = tea.clean_line_endings(context, snippet)
    # Special replacement in case we're using $WORD
    snippet = snippet.replace('$WORD', word)
    snippet = snippet.replace('$SELECTED_TEXT', fullword)
    cursor_loc = snippet.find('$0')
    if cursor_loc != -1:
        select_range = tea.new_range(cursor_loc + new_range.location, 0)
        snippet = snippet.replace('$0', '')
        tea.insert_text_and_select(context, snippet, new_range, select_range)
    else:
        tea.insert_text(context, snippet, new_range)
示例#10
0
 def initController_forBundle_(self, controller, bundle):
     # Store the controller and context for later reference
     self.controller = controller
     self.context = tea.get_context(controller, self)
示例#11
0
def act(controller, bundle, options):
    '''
    Required action method
    
    target dictates what we're looking for:
    - text
    - if unspecified, simply selects the source
    
    source dictates how to gather the string to search for:
    - word (word under the caret)
    - line (line under the caret)
    - if unspecified, defaults to selection
    
    Setting trim=True will cause the source to be trimmed
    
    Setting discard_indent=True will cause leading whitespace
    to be trimmed (unnecessary unless trim=True)
    
    search_string will set the string to search for if target is text or zone
    - $SELECTED_TEXT will be replaced with the source text
    
    Setting regex=True will cause search_string to be evaluated as regex
    '''
    context = tea.get_context(controller)
    
    target = tea.get_option(options, 'target')
    source = tea.get_option(options, 'source')
    trim = tea.get_option(options, 'trim', False)
    discard_indent = tea.get_option(options, 'discard_indent', False)
    search_string = tea.get_option(options, 'search_string')
    regex = tea.get_option(options, 'regex', False)
    
    range = tea.get_range(context)
    if source == 'word':
        text, range = tea.get_word(context, range)
    elif source == 'line':
        text, range = tea.get_line(context)
    elif range.length > 0:
        text = tea.get_selection(context, range)
    
    # Trim the source
    if trim:
        if discard_indent:
            trimmed = tea.trim(context, text, False, preserve_linebreaks=False)
        else:
            trimmed = tea.trim(context, text, False, 'end',
                               preserve_linebreaks=False)
        
        start = text.find(trimmed)
        if start != -1:
            start = range.location + start
        length = len(trimmed)
        if source == 'line' and trimmed[-1:] in ['\r\n', '\r', '\n']:
            # We don't want the linebreak if we're trimming
            length = length - 1
        range = tea.new_range(start, length)
        text = trimmed
    
    if target is not None and text:
        if search_string is not None:
            search = search_string.replace('$SELECTED_TEXT', text)
        else:
            search = text
        # Find the start and end points of the substring
        start = end = None
        if regex:
            match = re.search(r'(' + search + r')', context.string())
            if match:
                # Get the start and end points
                start, end = match.span(1)
        else:
            start = context.string().find(search)
            if start != -1:
                end = start + len(search)
            else:
                start = None
        # Construct the new target range
        if start is not None and end is not None:
            range = tea.new_range(start, end - start)
    
    # Set the new range
    tea.set_selected_range(context, range)