def get_line_after_and_range(context, range = None):
    '''Get the full line immediately after the current (or supplied) range'''
    line_ending = tea.get_line_ending(context)
    len_line_ending = len(line_ending)
    if range is None: range = tea.get_range(context)
    content = context.string()
    
    start = range.location + range.length
    
    if not is_line_ending(content, start - len_line_ending, line_ending):
        start = content.find(line_ending, start)
        if start == -1:
            return None
        else:
            start += len_line_ending
    
    end = content.find(line_ending, start)
    if end == -1:
        end = len(content)
    else:
        end += len_line_ending
    
    start = max(0, start)
    end = min(end, len(content))
    line_range = tea.new_range(start, end - start)
    
    return tea.get_selection(context, line_range), line_range
def get_line_after_and_range(context, range=None):
    '''Get the full line immediately after the current (or supplied) range'''
    line_ending = tea.get_line_ending(context)
    len_line_ending = len(line_ending)
    if range is None: range = tea.get_range(context)
    content = context.string()

    start = range.location + range.length

    if not is_line_ending(content, start - len_line_ending, line_ending):
        start = content.find(line_ending, start)
        if start == -1:
            return None
        else:
            start += len_line_ending

    end = content.find(line_ending, start)
    if end == -1:
        end = len(content)
    else:
        end += len_line_ending

    start = max(0, start)
    end = min(end, len(content))
    line_range = tea.new_range(start, end - start)

    return tea.get_selection(context, line_range), line_range
	def get_selection_range(self):
		"""
		Returns character indexes of selected text
		@return: list of start and end indexes
		@example
		start, end = zen_editor.get_selection_range();
		print('%s, %s' % (start, end))
		"""
		rng = tea.get_range(self._context)
		return rng.location, rng.location + rng.length
Exemple #4
0
	def get_selection_range(self):
		"""
		Returns character indexes of selected text
		@return: list of start and end indexes
		@example
		start, end = zen_editor.get_selection_range();
		print('%s, %s' % (start, end))
		"""
		rng = tea.get_range(self._context)
		return rng.location, rng.location + rng.length
Exemple #5
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)
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
def lines_and_range(context, range=None):
    '''Get the range of the full lines containing the current (or supplied) range'''
    line_ending = tea.get_line_ending(context)
    len_line_ending = len(line_ending)
    if range is None: range = tea.get_range(context)
    content = context.string()

    start, end = range.location, range.location + range.length

    if not is_line_ending(content, start - len_line_ending, line_ending):
        start = content.rfind(line_ending, 0, start)
        if start == -1:
            start = 0
        else:
            start += len_line_ending

    # select to the end of the line (if it's not already selected)
    if not is_line_ending(content, end, line_ending):
        # edge case: cursor is at start of line and more than one line selected:
        if not is_line_ending(content, end - len_line_ending,
                              line_ending) or len(
                                  content[start:end].split(line_ending)) <= 1:
            end = content.find(line_ending, end)
            if end == -1:
                end = len(content)
            else:
                end += len_line_ending
    # edge case: empty line, not selected
    elif is_line_ending(content, end - len_line_ending, line_ending):
        if len(content[start:end].split(line_ending)) <= 1:
            end = content.find(line_ending, end)
            if end == -1:
                end = len(content)
            else:
                end += len_line_ending
    else:
        end += len_line_ending

    start = max(0, start)
    end = min(end, len(content))

    line_range = tea.new_range(start, end - start)

    return tea.get_selection(context, line_range), line_range
Exemple #8
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
def lines_and_range(context, range = None):
    '''Get the range of the full lines containing the current (or supplied) range'''
    line_ending = tea.get_line_ending(context)
    len_line_ending = len(line_ending)
    if range is None: range = tea.get_range(context)
    content = context.string()
    
    start, end = range.location, range.location + range.length
    
    if not is_line_ending(content, start - len_line_ending, line_ending):
        start = content.rfind(line_ending, 0, start)
        if start == -1:
            start = 0
        else:
            start += len_line_ending
    
    # select to the end of the line (if it's not already selected)
    if not is_line_ending(content, end, line_ending):
        # edge case: cursor is at start of line and more than one line selected:
        if not is_line_ending(content, end - len_line_ending, line_ending) or len(content[start:end].split(line_ending)) <= 1:
            end = content.find(line_ending, end)
            if end == -1:
                end = len(content)
            else:
                end += len_line_ending
    # edge case: empty line, not selected
    elif is_line_ending(content, end - len_line_ending, line_ending):
        if len(content[start:end].split(line_ending)) <= 1:
            end = content.find(line_ending, end)
            if end == -1:
                end = len(content)
            else:
                end += len_line_ending
    else:
        end += len_line_ending
    
    start = max(0, start)
    end = min(end, len(content))
    
    line_range = tea.new_range(start, end - start)
    
    return tea.get_selection(context, line_range), line_range
def find_image(context):
    """
    Find image tag under caret
    @return Image tag and its indexes inside editor source
    """
    rng = tea.get_range(context)
    caret_pos = rng.location
    text = context.string()
    start_ix = -1
    end_ix = -1
    
    # find the beginning of the tag
    while caret_pos >= 0:
        if text[caret_pos] == '<':
            if text[caret_pos:caret_pos + 4].lower() == '<img':
                # found the beginning of the image tag
                start_ix = caret_pos
                break
            else:
                # found some other tag
                return None
        caret_pos -= 1
            
    # find the end of the tag 
    caret_pos = rng.location
    ln = len(text)
    while caret_pos <= ln:
        if text[caret_pos] == '>':
            end_ix = caret_pos + 1
            break
        caret_pos += 1
    
    
    if start_ix != -1 and end_ix != -1:
        return {
            'start': start_ix,
            'end': end_ix,
            'tag': text[start_ix:end_ix]
        }
    
    return None
def get_line_before_and_range(context, range=None):
    '''Get the full line immediately before the current (or supplied) range'''
    line_ending = tea.get_line_ending(context)
    if range is None: range = tea.get_range(context)
    content = context.string()

    end = content.rfind(line_ending, 0, range.location)
    if end == -1:
        return None
    else:
        end = end + len(line_ending)

    start = content.rfind(line_ending, 0, end - len(line_ending))
    if start == -1:
        start = 0
    else:
        start += len(line_ending)

    start = max(0, start)
    end = min(end, len(content))
    line_range = tea.new_range(start, end - start)

    return tea.get_selection(context, line_range), line_range
def get_line_before_and_range(context, range = None):
    '''Get the full line immediately before the current (or supplied) range'''
    line_ending = tea.get_line_ending(context)
    if range is None: range = tea.get_range(context)
    content = context.string()

    end = content.rfind(line_ending, 0, range.location)
    if end == -1:
        return None
    else:
        end = end + len(line_ending)
    
    start = content.rfind(line_ending, 0, end - len(line_ending))
    if start == -1:
        start = 0
    else:
        start += len(line_ending)
    
    start = max(0, start)
    end = min(end, len(content))
    line_range = tea.new_range(start, end - start)
    
    return tea.get_selection(context, line_range), line_range
Exemple #13
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)