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)
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 replace_content(self, value, start=None, end=None, no_indent=False): """ 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) if not no_indent: value = zencoding.utils.pad_string(value, get_line_padding(self.get_current_line())) sel_start, sel_end, value = self.preprocess_text(value) if sel_start is not None: select_range = tea.new_range(sel_start + rng.location, sel_end - sel_start) tea.insert_text_and_select(self._context, value, rng, select_range) else: tea.insert_text(self._context, value, rng)
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)
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)