Ejemplo n.º 1
0
    def launch(self, text_before, visual_content, parent, start, end):
        indent = self._INDENT.match(text_before).group(0)
        lines = (self._v + "\n").splitlines()
        ind_util = IndentUtil()

        # Replace leading tabs in the snippet definition via proper indenting
        v = []
        for line_num, line in enumerate(lines):
            if "t" in self._opts:
                tabs = 0
            else:
                tabs = len(self._TABS.match(line).group(0))

            line_ind = ind_util.ntabs_to_proper_indent(tabs)

            if line_num != 0:
                line_ind = indent + line_ind

            v.append(line_ind + line[tabs:])
        v = '\n'.join(v)

        si = SnippetInstance(self,
                             parent,
                             indent,
                             v,
                             start,
                             end,
                             visual_content,
                             last_re=self._last_re,
                             globals=self._globals)

        return si
Ejemplo n.º 2
0
    def launch(self, text_before, visual_content, parent, start, end):
        indent = self._INDENT.match(text_before).group(0)
        lines = (self._v + "\n").splitlines()
        ind_util = IndentUtil()

        # Replace leading tabs in the snippet definition via proper indenting
        v = []
        for line_num, line in enumerate(lines):
            if "t" in self._opts:
                tabs = 0
            else:
                tabs = len(self._TABS.match(line).group(0))

            line_ind = ind_util.ntabs_to_proper_indent(tabs)

            if line_num != 0:
                line_ind = indent + line_ind

            v.append(line_ind + line[tabs:])
        v = '\n'.join(v)

        si = SnippetInstance(self, parent, indent, v, start, end, visual_content,
                last_re = self._last_re, globals = self._globals)

        return si
Ejemplo n.º 3
0
    def __init__(self, initial_indent, vmode, vtext):
        self._ind = IndentUtil()
        self._visual = _VisualContent(vmode, vtext)

        self._initial_indent = self._ind.indent_to_spaces(initial_indent)

        self._reset("")
Ejemplo n.º 4
0
    def __init__(self, initial_indent, vmode, vtext):
        self._ind = IndentUtil()
        self._visual = _VisualContent(vmode, vtext)

        self._initial_indent = self._ind.indent_to_spaces(initial_indent)

        self._reset("")
Ejemplo n.º 5
0
    def _update(self, done, not_done):
        if self._mode != "v":
            # Keep the indent for Line/Block Selection
            text_before = _vim.buf[self.start.line][:self.start.col]
            indent = self.__REPLACE_NON_WS.sub(" ", text_before)
            iu = IndentUtil()
            indent = iu.indent_to_spaces(indent)
            indent = iu.spaces_to_indent(indent)
            text = ""
            for idx, line in enumerate(self._text.splitlines(True)):
                if idx != 0:
                    text += indent
                text += line
            text = text[:-1] # Strip final '\n'
        else:
            text = self._text

        self.overwrite(text)
        self._parent._del_child(self)
        return True
Ejemplo n.º 6
0
    def _update(self, done, not_done):
        if self._mode != "v":
            # Keep the indent for Line/Block Selection
            text_before = _vim.buf[self.start.line][:self.start.col]
            indent = self.__REPLACE_NON_WS.sub(" ", text_before)
            iu = IndentUtil()
            indent = iu.indent_to_spaces(indent)
            indent = iu.spaces_to_indent(indent)
            text = ""
            for idx, line in enumerate(self._text.splitlines(True)):
                if idx != 0:
                    text += indent
                text += line
            text = text[:-1]  # Strip final '\n'
        else:
            text = self._text

        text = self._transform(text)
        self.overwrite(text)
        self._parent._del_child(self)

        return True
Ejemplo n.º 7
0
class SnippetUtil(object):
    """ Provides easy access to indentation, etc.
    """

    def __init__(self, initial_indent, vmode, vtext):
        self._ind = IndentUtil()
        self._visual = _VisualContent(vmode, vtext)

        self._initial_indent = self._ind.indent_to_spaces(initial_indent)
        self._inflector = Inflector()

        self._reset("")

    def _reset(self, cur):
        """ Gets the snippet ready for another update.

        :cur: the new value for c.
        """
        self._ind.reset()
        self._c = cur
        self._rv = ""
        self._changed = False
        self.reset_indent()

    def shift(self, amount=1):
        """ Shifts the indentation level.
        Note that this uses the shiftwidth because thats what code
        formatters use.

        :amount: the amount by which to shift.
        """
        self.indent += " " * self._ind.sw * amount

    def unshift(self, amount=1):
        """ Unshift the indentation level.
        Note that this uses the shiftwidth because thats what code
        formatters use.

        :amount: the amount by which to unshift.
        """
        by = -self._ind.sw * amount
        try:
            self.indent = self.indent[:by]
        except IndexError:
            indent = ""

    def mkline(self, line="", indent=None):
        """ Creates a properly set up line.

        :line: the text to add
        :indent: the indentation to have at the beginning
                 if None, it uses the default amount
        """
        if indent == None:
            indent = self.indent
            # this deals with the fact that the first line is
            # already properly indented
            if '\n' not in self._rv:
                try:
                    indent = indent[len(self._initial_indent):]
                except IndexError:
                    indent = ""
            indent = self._ind.spaces_to_indent(indent)

        return indent + line

    def reset_indent(self):
        """ Clears the indentation. """
        self.indent = self._initial_indent

    # Utility methods
    @property
    def fn(self):
        """ The filename. """
        return _vim.eval('expand("%:t")') or ""

    @property
    def basename(self):
        """ The filename without extension. """
        return _vim.eval('expand("%:t:r")') or ""

    @property
    def ft(self):
        """ The filetype. """
        return self.opt("&filetype", "")

    # Necessary stuff
    def rv():
        """ The return value.
        This is a list of lines to insert at the
        location of the placeholder.

        Deprecates res.
        """
        def fget(self):
            return self._rv
        def fset(self, value):
            self._changed = True
            self._rv = value
        return locals()
    rv = property(**rv())

    @property
    def _rv_changed(self):
        """ True if rv has changed. """
        return self._changed

    @property
    def c(self):
        """ The current text of the placeholder.

        Deprecates cur.
        """
        return self._c

    @property
    def v(self):
        """Content of visual expansions"""
        return self._visual

    @property
    def i(self):
      return self._inflector

    def opt(self, option, default=None):
        """ Gets a Vim variable. """
        if _vim.eval("exists('%s')" % option) == "1":
            try:
                return _vim.eval(option)
            except _vim.error:
                pass
        return default

    # Syntatic sugar
    def __add__(self, value):
        """ Appends the given line to rv using mkline. """
        self.rv += '\n' # handles the first line properly
        self.rv += self.mkline(value)
        return self

    def __lshift__(self, other):
        """ Same as unshift. """
        self.unshift(other)

    def __rshift__(self, other):
        """ Same as shift. """
        self.shift(other)
Ejemplo n.º 8
0
class SnippetUtil(object):
    """ Provides easy access to indentation, etc.
    """
    def __init__(self, initial_indent, vmode, vtext):
        self._ind = IndentUtil()
        self._visual = _VisualContent(vmode, vtext)

        self._initial_indent = self._ind.indent_to_spaces(initial_indent)

        self._reset("")

    def _reset(self, cur):
        """ Gets the snippet ready for another update.

        :cur: the new value for c.
        """
        self._ind.reset()
        self._c = cur
        self._rv = ""
        self._changed = False
        self.reset_indent()

    def shift(self, amount=1):
        """ Shifts the indentation level.
        Note that this uses the shiftwidth because thats what code
        formatters use.

        :amount: the amount by which to shift.
        """
        self.indent += " " * self._ind.sw * amount

    def unshift(self, amount=1):
        """ Unshift the indentation level.
        Note that this uses the shiftwidth because thats what code
        formatters use.

        :amount: the amount by which to unshift.
        """
        by = -self._ind.sw * amount
        try:
            self.indent = self.indent[:by]
        except IndexError:
            self.indent = ""

    def mkline(self, line="", indent=None):
        """ Creates a properly set up line.

        :line: the text to add
        :indent: the indentation to have at the beginning
                 if None, it uses the default amount
        """
        if indent is None:
            indent = self.indent
            # this deals with the fact that the first line is
            # already properly indented
            if '\n' not in self._rv:
                try:
                    indent = indent[len(self._initial_indent):]
                except IndexError:
                    indent = ""
            indent = self._ind.spaces_to_indent(indent)

        if vim.eval(
                "g:UltiSnips.empty_lines_add_indent") != "1" and line == "":
            indent = ""

        return indent + line

    def reset_indent(self):
        """ Clears the indentation. """
        self.indent = self._initial_indent

    # Utility methods
    @property
    def fn(self):
        """ The filename. """
        return _vim.eval('expand("%:t")') or ""

    @property
    def basename(self):
        """ The filename without extension. """
        return _vim.eval('expand("%:t:r")') or ""

    @property
    def ft(self):
        """ The filetype. """
        return self.opt("&filetype", "")

    # Necessary stuff
    def rv():
        """ The return value.
        This is a list of lines to insert at the
        location of the placeholder.

        Deprecates res.
        """
        def fget(self):
            return self._rv

        def fset(self, value):
            self._changed = True
            self._rv = value

        return locals()

    rv = property(**rv())

    @property
    def _rv_changed(self):
        """ True if rv has changed. """
        return self._changed

    @property
    def c(self):
        """ The current text of the placeholder.

        Deprecates cur.
        """
        return self._c

    @property
    def v(self):
        """Content of visual expansions"""
        return self._visual

    def opt(self, option, default=None):
        """ Gets a Vim variable. """
        if _vim.eval("exists('%s')" % option) == "1":
            try:
                return _vim.eval(option)
            except _vim.error:
                pass
        return default

    # Syntatic sugar
    def __add__(self, value):
        """ Appends the given line to rv using mkline. """
        self.rv += '\n'  # handles the first line properly
        self.rv += self.mkline(value)
        return self

    def __lshift__(self, other):
        """ Same as unshift. """
        self.unshift(other)

    def __rshift__(self, other):
        """ Same as shift. """
        self.shift(other)
Ejemplo n.º 9
0
    def __init__(self, initial_indent, cur=""):
        self._ind = IndentUtil()

        self._initial_indent = self._ind.indent_to_spaces(initial_indent)

        self._reset(cur)