Пример #1
0
def _parse_line(line):
    try:
        line = line.rstrip()
        if line:
            match = _PARSE_LINE_PATTERN.match(line)
            if match:
                cmdline = match.group('cmdline')
                # Ensure there is leading colon, because the parser pattern omits it.
                if cmdline:
                    cmdline = ':' + cmdline

                # By default, mapping the character '|' (bar) should be escaped
                # with a slash or '<Bar>' used instead. Neovintageous currently
                # doesn't support '<Bar>' and internally doesn't require the bar
                # character to be escaped, but in order not to break backwards
                # compatibility in the future, this piece of code checks that
                # the mapping escapes the bar character correctly. This piece of
                # code can be removed when this is fixed in the core. See :help
                # map_bar for more details.
                if '|' in cmdline:
                    if '|' in cmdline.replace('\\|', ''):
                        raise Exception('E488: Trailing characters: {}'.format(line.rstrip()))

                    cmdline = cmdline.replace('\\|', '|')

                return cmdline
    except Exception:
        message('error detected while processing vintageousrc at line "{}"'.format(line.rstrip()))

    return None
Пример #2
0
    def run(self):
        view = self.window.active_view()
        pt = view.sel()[0]
        # scope_name() needs to striped due to a bug in ST:
        # See https://github.com/SublimeTextIssues/Core/issues/657.
        scope = view.scope_name(pt.b).rstrip()

        # TODO Fix jumptags scopes (rename them to less generic scopes)
        jumptag_scopes = [
            'text.neovintageous.help string.neovintageous',
            'text.neovintageous.help support.constant.neovintageous'
        ]

        if scope not in jumptag_scopes:
            return

        subject = view.substr(view.extract_scope(pt.b))

        if len(subject) < 3:
            return message('E149: Sorry, no help for %s' % subject)

        match = re.match('^\'[a-z_]+\'|\\|[^\\s\\|]+\\|$', subject)
        if match:
            subject = subject.strip('|')
            # TODO Refactor ex_help code into a reusable middle layer so that
            # this command doesn't have to call the ex command.
            self.window.run_command('ex_help',
                                    {'command_line': 'help ' + subject})
        else:
            return message('E149: Sorry, no help for %s' % subject)
Пример #3
0
def do_modeline(view):
    # A feature similar to vim modeline.
    #
    # A number of lines at the beginning and end of the file are checked for
    # modelines.
    #
    # See :help auto-setting for more information.
    #
    # Args:
    #   view (sublime.View)
    #
    # Example:
    #     # sublime: gutter false
    #     # sublime: translate_tab_to_spaces true
    #     # sublime: rulers [80, 120]
    #     # sublime: tab_size 4
    for setter, name, value in _gen_modeline_options(view):
        if name == 'x_syntax':
            view.set_syntax_file(value)
        else:
            try:
                setter(name, _to_json_type(value))
            except ValueError:
                message(
                    'Error detected while processing modelines: option = {}'.
                    format(name))
Пример #4
0
def _parse_line(line):
    try:
        line = line.rstrip()
        if line:
            match = _parse_line_pattern.match(line)
            if match:
                cmd_line = match.group('command_line')
                cmd = match.group('cmd')

                if cmd in _recursive_mapping_alts:
                    raise Exception('Recursive mapping commands not allowed, use the "{}" command instead'
                                    .format(_recursive_mapping_alts[cmd]))

                # By default, mapping the character '|' (bar) should be escaped
                # with a slash or '<Bar>' used instead. Neovintageous currently
                # doesn't support '<Bar>' and internally doesn't require the bar
                # character to be escaped, but in order not to break backwards
                # compatibility in the future, this piece of code checks that
                # the mapping escapes the bar character correctly. This piece of
                # code can be removed when this is fixed in the core. See :help
                # map_bar for more details.
                if '|' in cmd_line:
                    if '|' in cmd_line.replace('\\|', ''):
                        raise Exception('E488: Trailing characters: {}'.format(line.rstrip()))
                    cmd_line = cmd_line.replace('\\|', '|')

                return ('ex_' + cmd, {'command_line': cmd_line})
    except Exception:
        msg = 'error detected while processing \'{}\' at line \'{}\''.format(file_name(), line.rstrip())
        message(msg)
        _log.exception(msg)

    return None, None
Пример #5
0
def _parse_line(line):
    try:
        line = line.rstrip()
        if line:
            match = _PARSE_LINE_PATTERN.match(line)
            if match:
                cmdline = match.group('cmdline')
                # Ensure there is leading colon, because the parser pattern omits it.
                if cmdline:
                    cmdline = ':' + cmdline

                # Since the '|' character is used to separate a map command from
                # the next command, you will have to do something special to
                # include a '|' in {rhs}. You can use '<bar>' or escape with a
                # slash '\|'. See :h map-bar. TODO Refactor logic for
                # translating escaped bar to <bar> into mapping internals.
                cmdline = cmdline.replace('\\|', '<bar>')

                # To map a backslash, or use a backslash literally in the {rhs},
                # the special sequence "<bslash>" can be used. This avoids the
                # need to double backslashes when using nested mappings. See :h
                # map-backslash. TODO Refactor logic for translating escaped
                # backslash to <bslash> into mapping internals.
                cmdline = cmdline.replace('\\', '<bslash>')

                if '|' in cmdline:
                    # Using '|' to separate map commands is currently not supported.
                    raise Exception('E488: Trailing characters: {}'.format(line.rstrip()))

                return cmdline
    except Exception:
        message('error detected while processing {} at line "{}"'.format(_file_name(), line.rstrip()))

    return None
Пример #6
0
def _parse_line(line: str):
    try:
        line = line.rstrip()
        if line:
            match = _PARSE_LINE_PATTERN.match(line)
            if match:
                cmdline = match.group('cmdline')
                # Ensure there is leading colon, because the parser pattern omits it.
                if cmdline:
                    cmdline = ':' + cmdline

                # The '|' character is used to chain commands. Users should
                # escape it with a slash or use '<bar>'. See :h map-bar. It's
                # translated to <bar> internally (implementation detail).
                # See https://github.com/NeoVintageous/NeoVintageous/issues/615.
                cmdline = cmdline.replace('\\|', '<bar>')

                if '|' in cmdline:
                    # Using '|' to separate map commands is currently not supported.
                    raise Exception('E488: Trailing characters: {}'.format(
                        line.rstrip()))

                return cmdline
    except Exception as e:
        message('error detected while processing {} at line "{}":\n{}'.format(
            _file_name(), line.rstrip(), str(e)))

    return None
Пример #7
0
    def run(self):
        view = self.window.active_view()
        pt = view.sel()[0]
        # scope_name() needs to striped due to a bug in ST:
        # See https://github.com/SublimeTextIssues/Core/issues/657.
        scope = view.scope_name(pt.b).rstrip()

        # TODO Fix jumptags scopes (rename them to less generic scopes)
        jumptag_scopes = [
            'text.neovintageous.help string.neovintageous',
            'text.neovintageous.help support.constant.neovintageous'
        ]

        if scope not in jumptag_scopes:
            return

        subject = view.substr(view.extract_scope(pt.b))

        if len(subject) < 3:
            return message('E149: Sorry, no help for %s' % subject)

        match = re.match('^\'[a-z_]+\'|\\|[^\\s\\|]+\\|$', subject)
        if match:
            do_ex_command(self.window, 'help', {'subject': subject.strip('|')})
        else:
            return message('E149: Sorry, no help for %s' % subject)
Пример #8
0
    def run(self, command, file_name=None, forced=False, index=None):
        view_count = len(self.window.views_in_group(
            self.window.active_group()))
        (group_index, view_index) = self.window.get_view_index(self._view)

        if command == 'open':
            if not file_name:  # TODO: file completion
                self.window.run_command('show_overlay', {
                    'overlay': 'goto',
                    'show_files': True,
                })
            else:
                cur_dir = os.path.dirname(self._view.file_name())
                self.window.open_file(os.path.join(cur_dir, file_name))

        elif command == 'next':
            self.window.run_command('select_by_index',
                                    {'index': (view_index + 1) % view_count})

        elif command == 'prev':
            self.window.run_command(
                'select_by_index',
                {'index': (view_index + view_count - 1) % view_count})

        elif command == "last":
            self.window.run_command('select_by_index',
                                    {'index': view_count - 1})

        elif command == "first":
            self.window.run_command('select_by_index', {'index': 0})

        elif command == 'goto':
            self.window.run_command('select_by_index', {'index': index - 1})

        elif command == 'only':
            quit_command_line = 'quit' + '' if not forced else '!'

            group = self.window.views_in_group(group_index)
            if any(view.is_dirty() for view in group):
                return message("E445: Other window contains changes")

            for view in group:
                if view.id() == self._view.id():
                    continue
                self.window.focus_view(view)
                self.window.run_command('ex_quit',
                                        {'command_line': quit_command_line})

            self.window.focus_view(self._view)

        else:
            return message('unknown tab control command')
Пример #9
0
    def on_done(self, cmd_line):
        if len(cmd_line) <= 1:
            return

        if cmd_line[0] != ':':
            return

        if ViColonInput.interactive_call:
            history_update(cmd_line)

        _nv_cmdline_handle_key.reset_last_history_index()

        try:
            parsed_new = parse_command_line(cmd_line[1:])
            if not parsed_new.command:
                parsed_new.command = TokenCommandGoto()

            self.window.run_command(parsed_new.command.target_command,
                                    {'command_line': cmd_line[1:]})
        except Exception as e:
            message(str(e) + ' ' + "(%s)" % cmd_line)
Пример #10
0
    def run(self):
        view = self.window.active_view()
        if not view:
            raise ValueError('view is required')

        if not view.sel():
            raise ValueError('selection is required')

        sel = view.sel()[0]

        score = view.score_selector(sel.b, 'text.neovintageous jumptag')
        # TODO ENHANCEMENT Allow goto to help for any word in a help file. See :h bar Anyway, you can use CTRL-] on any word, also when it is not within |, and Vim will try to find help for it.  Especially for options in single quotes, e.g. 'compatible'.  # noqa: E501
        if score == 0:
            return  # noop

        subject = view.substr(view.extract_scope(sel.b))
        if not subject:
            return  # noop

        if len(subject) > 35:
            return message('E149: Sorry, no help found')

        do_ex_command(self.window, 'help', {'subject': subject})
Пример #11
0
def do_modeline(view) -> None:
    # A feature similar to vim modeline. A number of lines at the beginning and
    # end of the file are checked for modelines. The number of lines checked is
    # controlled by the 'modelines' option, the default is 5.
    #
    # Examples:
    #   vim: number
    #   vim: nonumber
    #   vim: tabstop=4
    #   vim: ts=4 noet
    window = view.window()

    # If the view is "transient" (for example when opened in in preview via the
    # CTRL-p overlay) then the view won't have a window object. Some ST events
    # like on_load() may open transient views.
    if not window:
        window = active_window()

    if window:
        modelines = get_option(view, 'modelines')
        line_count = view.rowcol(view.size())[0] + 1
        head_lines = range(0, min(modelines, line_count))
        tail_lines = range(max(0, line_count - modelines), line_count)
        lines = list(set(list(head_lines) + list(tail_lines)))

        for i in lines:
            line = view.line(view.text_point(i, 0))
            if line.size() > 0:
                options = _parse_line(view.substr(line))
                if options:
                    for option in options:
                        if option.strip().startswith('shell'):
                            message('Error detected while processing modelines:')
                            message('line %s:', str(i + 1))
                            message('E520: Not allowed in a modeline: %s', option)
                        else:
                            do_ex_cmdline(window, ':setlocal ' + option)