Exemplo n.º 1
0
def scan_command_file(state):
    bang = state.consume()

    if bang == EOF:
        return None, [TokenCommandFile(), TokenEof()]

    bang = bang == '!'
    if not bang:
        raise nvim.Error(nvim.E_TRAILING_CHARS)

    state.expect(EOF, on_error=lambda: nvim.Error(nvim.E_TRAILING_CHARS))

    return None, [TokenCommandFile(forced=bang == '!'), TokenEof()]
def scan_command_write_and_quit_all(state):
    params = {
        '++': '',
    }

    state.skip(' ')
    state.ignore()

    c = state.consume()

    if c == '+':
        state.expect('+')
        state.ignore()
        # TODO: expect_match should work with emit()
        # https://neovim.io/doc/user/editing.html#[++opt]
        m = state.expect_match(
            r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
            lambda: nvim.Error(nvim.E_INVALID_ARGUMENT))
        name = m.group(0)
        params['++'] = plus_plus_translations.get(name, name)
        state.ignore()

    state.expect(EOF)

    return None, [TokenCommandWriteAndQuitAll(params), TokenEof()]
Exemplo n.º 3
0
def scan_command_new(state):
    params = {
        '++': None,
        'cmd': None,
    }
    state.skip(' ')
    state.ignore()

    c = state.consume()

    if c == '+':
        state.expect('+')
        state.ignore()
        # TODO: expect_match should work with emit()
        # https://neovim.io/doc/user/editing.html#[++opt]
        m = state.expect_match(
            r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
            lambda: nvim.Error(nvim.E_INVALID_ARGUMENT))
        name = m.group(0)
        params['++'] = plus_plus_translations.get(name, name)
        state.ignore()
        raise NotImplementedError(':new not fully implemented')

    m = state.match(r'.+$')
    if m:
        params['cmd'] = m.group(0).strip()
        raise NotImplementedError(':new not fully implemented')

    return None, [TokenNew(params), TokenEof()]
Exemplo n.º 4
0
    def validate(self):
        """Raise an error for known conditions."""
        if not (self.command and self.line_range):
            return

        if not self.command.addressable and not self.line_range.is_empty:
            raise nvim.Error(nvim.E_NO_RANGE_ALLOWED)
Exemplo n.º 5
0
def scan_command_buffers(state):
    try:
        state.expect(EOF)
    except ValueError:
        raise nvim.Error(nvim.E_TRAILING_CHARS)

    return None, [TokenCommandBuffers(), TokenEof()]
Exemplo n.º 6
0
def scan_command(state):
    for (pattern, subscanner) in subscanners.patterns.items():
        if state.match(pattern):
            state.ignore()
            return subscanner(state)

    state.expect(EOF, lambda: nvim.Error(nvim.E_UNKNOWN_COMMAND))

    return None, [TokenEof()]
Exemplo n.º 7
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):
                nvim.exception_message(nvim.Error(nvim.E_OTHER_BUFFER_HAS_CHANGES))
                return

            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:
            nvim.console_message('unknown tab control command')
            nvim.status_message('unknown tab control command')
Exemplo n.º 8
0
def scan_command_let(state):
    params = {
        'name': None,
        'value': None,
    }

    # TODO(guillermooo): :let has many more options.

    m = state.expect_match(
        r'(?P<name>.+?)\s*=\s*(?P<value>.+?)\s*$',
        on_error=lambda: nvim.Error(nvim.E_UNDEFINED_VARIABLE))

    params.update(m.groupdict())

    return None, [TokenCommandLet(params), TokenEof()]
def scan_command_exit(state):
    params = {
        'file_name': '',
    }

    bang = state.consume()

    if bang == EOF:
        return None, [TokenCommandExit(params), TokenEof()]

    bang = bang == '!'
    if not bang:
        state.backup()

    state.skip(' ')
    state.ignore()

    while True:
        c = state.consume()

        if c == EOF:
            return None, [TokenCommandExit(params, forced=bang), TokenEof()]

        if c == '+':
            state.expect('+')
            state.ignore()
            # TODO: expect_match should work with emit()
            # https://neovim.io/doc/user/editing.html#[++opt]
            m = state.expect_match(
                r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
                lambda: nvim.Error(nvim.E_INVALID_ARGUMENT))
            name = m.group(0)
            params['++'] = plus_plus_translations.get(name, name)
            state.ignore()
            continue

        if c != ' ':
            state.match(r'.*')
            params['file_name'] = state.emit().strip()
            state.skip(' ')
            state.ignore()

    state.expect(EOF)

    return None, [TokenCommandExit(params, forced=bang == '!'), TokenEof()]
Exemplo n.º 10
0
def scan_command_read_shell_out(state):
    params = {
        'cmd': None,
        '++': [],
        'file_name': None,
    }

    state.skip(' ')
    state.ignore()

    c = state.consume()

    if c == '+':
        state.expect('+')
        state.ignore()
        # TODO: expect_match should work with emit()
        # https://neovim.io/doc/user/editing.html#[++opt]
        m = state.expect_match(
            r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
            lambda: nvim.Error(nvim.E_INVALID_ARGUMENT))
        name = m.group(0)
        params['++'] = plus_plus_translations.get(name, name)
        state.ignore()
        raise NotImplementedError('++opt not implemented')

    elif c == '!':
        m = state.match(r'(?P<cmd>.+)')
        params.update(m.groupdict())

    else:
        state.backup()
        m = state.match(r'(?P<file_name>.+)$')
        params.update(m.groupdict())

    state.expect(EOF)

    return None, [TokenReadShellOut(params), TokenEof()]
def scan_command_write_and_quit_command(state):
    params = {
        '++': None,
        'file': None,
    }

    c = state.consume()

    if c == EOF:
        return None, [TokenWriteAndQuitCommand(params), TokenEof()]

    bang = True if c == '!' else False
    if not bang:
        state.backup()

    c = state.consume()

    if c == '+':
        state.expect('+')
        state.ignore()
        # TODO: expect_match should work with emit()
        # https://neovim.io/doc/user/editing.html#[++opt]
        m = state.expect_match(
            r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
            lambda: nvim.Error(nvim.E_INVALID_ARGUMENT))
        name = m.group(0)
        params['++'] = plus_plus_translations.get(name, name)
        state.ignore()
        raise NotImplementedError('param not implemented')

    if c == EOF:
        return None, [TokenWriteAndQuitCommand(params), TokenEof()]

    m = state.expect_match(r'.+$')
    params['file'] = m.group(0).strip()

    return None, [TokenWriteAndQuitCommand(params), TokenEof()]
def scan_command_edit(state):
    params = {
        '++': None,
        'cmd': None,
        'file_name': None,
        'count': None,
    }

    c = state.consume()

    if c == EOF:
        return None, [TokenEdit(params), TokenEof()]

    bang = c == '!'
    if not bang:
        state.backup()

    while True:
        c = state.consume()

        if c == EOF:
            return None, [TokenEdit(params, forced=bang), TokenEof()]

        if c == '+':
            k = state.consume()

            if k == '+':
                state.ignore()
                # TODO: expect_match should work with emit()
                # https://neovim.io/doc/user/editing.html#[++opt]
                m = state.expect_match(
                    r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
                    lambda: nvim.Error(nvim.E_INVALID_ARGUMENT))
                name = m.group(0)
                params['++'] = plus_plus_translations.get(name, name)
                state.ignore()
                raise NotImplementedError('param not implemented')
                continue

            state.backup()
            state.ignore()
            state.expect_match(r'.+$')
            params['cmd'] = state.emit()
            raise NotImplementedError('param not implemented')
            continue

        if c != ' ':
            state.match(r'.*')
            params['file_name'] = state.emit().strip()

            state.skip(' ')
            state.ignore()
            continue

        if c == '#':
            state.ignore()
            m = state.expect_match(r'\d+')
            params['count'] = m.group(0)
            raise NotImplementedError('param not implemented')
            continue

    return None, [TokenEdit(params, forced=bang), TokenEof()]