示例#1
0
def _ex_route_wq(state):
    command = TokenCommand('wq')
    # TODO [review] None of the prams looks used
    params = {
        '++': None,
        'file': None,
    }

    c = state.consume()
    if c == state.EOF:
        command.params = params

        return None, [command, 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://vimhelp.appspot.com/editing.txt.html#[++opt]
        m = state.expect_match(
            r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
            lambda: Exception("E474: Invalid argument"))

        name = m.group(0)

        plus_plus_translations = {
            'ff': 'fileformat',
            'bin': 'binary',
            'enc': 'fileencoding',
            'nobin': 'nobinary',
        }

        params['++'] = plus_plus_translations.get(name, name)

        state.ignore()
        raise NotImplementedError('param not implemented')

    if c == state.EOF:
        command.params = params
        command.forced = bang

        return None, [command, TokenEof()]

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

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
示例#2
0
def _literal_route(state, name, forcable=False, **kwargs):
    command = TokenCommand(name, **kwargs)

    if forcable and state.match('!'):
        command.forced = True

    return command
示例#3
0
def _ex_route_edit(state):
    command = TokenCommand('edit')

    params = {'file_name': None}

    c = state.consume()
    if c == state.EOF:
        command.params = params

        return command

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

    while True:
        c = state.consume()
        if c == state.EOF:
            command.params = params
            command.forced = bang

            return command

        if c in ('+', '#'):
            raise NotImplementedError('parameter not implemented')

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

            state.skip(' ')
            state.ignore()
示例#4
0
def _ex_route_tabprevious(state):
    command = TokenCommand('tabprevious')
    c = state.consume()
    if c == state.EOF:
        return None, [command, TokenEof()]

    command.forced = c == '!'

    return None, [command, TokenEof()]
示例#5
0
def _ex_route_quit(state):
    command = TokenCommand('quit')
    bang = state.consume() == '!'

    state.expect_eof()

    command.forced = bang

    return None, [command, TokenEof()]
示例#6
0
def _ex_route_help(state):
    command = TokenCommand('help')
    match = state.expect_match(r'(?P<bang>!)?\s*(?P<subject>.+)?$').groupdict()
    params = {'subject': match['subject']}
    bang = bool(match['bang'])

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
示例#7
0
def _create_route(state,
                  name: str,
                  forcable: bool = False,
                  **kwargs) -> TokenCommand:
    command = TokenCommand(name, **kwargs)

    if forcable and state.match('!'):
        command.forced = True

    return command
示例#8
0
def _ex_route_cd(state):
    command = TokenCommand('cd')

    # TODO [refactor] Should params should used keys compatible with **kwargs? (review other commands too) # noqa: E501
    params = {'path': None, '-': None}
    bang = False

    c = state.consume()
    if c == state.EOF:
        command.params = params
        command.forced = bang

        return None, [command, TokenEof()]

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

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

    c = state.consume()
    if c == state.EOF:
        command.params = params
        command.forced = bang

        return None, [command, TokenEof()]

    if c == '-':
        raise NotImplementedError('parameter not implemented')

    state.backup()
    m = state.match(r'(?P<path>.+?)\s*$')
    params.update(m.groupdict())

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
示例#9
0
def _ex_route_file(state):
    command = TokenCommand('file')
    bang = state.consume()
    if bang == state.EOF:
        return None, [command, TokenEof()]

    bang = bang == '!'
    if not bang:
        raise Exception("E488: Trailing characters")

    state.expect_eof(on_error=lambda: Exception("E488: Trailing characters"))

    command.forced = bang

    return None, [command, TokenEof()]
示例#10
0
def _ex_route_only(state):
    command = TokenCommand('only')

    bang = state.consume()
    if bang == '!':
        state.ignore()
        state.expect_eof()

        command.forced = True

        return None, [command, TokenEof()]

    # TODO [refactor] and remove assertion
    assert bang == state.EOF, 'trailing characters'

    return None, [command, TokenEof()]
示例#11
0
def _ex_route_cdd(state):
    command = TokenCommand('cdd')

    c = state.consume()
    if c == state.EOF:
        return None, [command, TokenEof()]

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

    state.expect_eof()

    command.forced = bang

    return None, [command, TokenEof()]
示例#12
0
def _ex_route_global(state):
    command = TokenCommand('global')
    command.addressable = True
    params = {'pattern': None, 'cmd': None}

    c = state.consume()

    bang = c == '!'
    sep = c if not bang else state.consume()

    # TODO: we're probably missing legal separators.
    # TODO [refactor] and remove assertion
    assert c in '!:?/\\&$', 'bad separator'

    state.ignore()

    while True:
        c = state.consume()

        if c == state.EOF:
            raise ValueError('unexpected EOF in: ' + state.source)

        if c == sep:
            state.backup()

            params['pattern'] = state.emit()

            state.consume()
            state.ignore()
            break

    cmd = state.match(r'.*$').group(0).strip()
    if cmd:
        params['cmd'] = cmd

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
示例#13
0
def _ex_route_exit(state):
    command = TokenCommand('exit')
    command.addressable = True

    # TODO [review] file_name param looks unused by the ex_exit
    params = {'file_name': ''}

    bang = state.consume()

    if bang == state.EOF:
        command.params = params

        return None, [command, TokenEof()]

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

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

    plus_plus_translations = {
        'ff': 'fileformat',
        'bin': 'binary',
        'enc': 'fileencoding',
        'nobin': 'nobinary'
    }

    while True:
        c = state.consume()

        if c == state.EOF:
            command.params = params
            command.forced = bang

            return None, [command, TokenEof()]

        if c == '+':
            state.expect('+')
            state.ignore()

            # TODO: expect_match should work with emit()
            # https://vimhelp.appspot.com/editing.txt.html#[++opt]
            m = state.expect_match(
                r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
                lambda: Exception("E474: 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()

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
示例#14
0
def _ex_route_edit(state):
    command = TokenCommand('edit')
    # TODO [refactor] Should params should used keys compatible with **kwargs? (review other commands too) # noqa: E501
    params = {
        '++': None,
        'cmd': None,
        'file_name': None,
        'count': None,
    }

    c = state.consume()
    if c == state.EOF:
        command.params = params

        return None, [command, TokenEof()]

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

    plus_plus_translations = {
        'ff': 'fileformat',
        'bin': 'binary',
        'enc': 'fileencoding',
        'nobin': 'nobinary'
    }

    while True:
        c = state.consume()

        if c == state.EOF:
            command.params = params
            command.forced = bang

            return None, [command, TokenEof()]

        if c == '+':
            k = state.consume()
            if k == '+':
                state.ignore()
                # TODO: expect_match should work with emit()
                # https://vimhelp.appspot.com/editing.txt.html#[++opt]
                m = state.expect_match(
                    r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
                    lambda: Exception("E474: 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

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]
示例#15
0
def _ex_route_write(state):
    command = TokenCommand('write')
    command.addressable = True

    # TODO [refactor] params should used keys compatible with ex command function keyword arguments. Review other routes too.  # noqa: E501

    params = {
        '++': '',
        'file_name': '',
        '>>': False,
        'cmd': '',
    }

    bang = state.consume()
    if bang == state.EOF:
        command.params = params

        return None, [command, TokenEof()]

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

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

    plus_plus_translations = {
        'ff': 'fileformat',
        'bin': 'binary',
        'enc': 'fileencoding',
        'nobin': 'nobinary'
    }

    while True:
        c = state.consume()
        if c == state.EOF:
            # TODO: forced?
            command.params = params
            command.forced = bang

            return None, [command, TokenEof()]

        if c == '+':
            state.expect('+')
            state.ignore()
            # TODO: expect_match should work with emit()
            # https://vimhelp.appspot.com/editing.txt.html#[++opt]
            m = state.expect_match(
                r'(?:f(?:ile)?f(?:ormat)?|(?:file)?enc(?:oding)?|(?:no)?bin(?:ary)?|bad|edit)(?=\s|$)',
                lambda: Exception("E474: Invalid argument"))

            name = m.group(0)
            params['++'] = plus_plus_translations.get(name, name)
            state.ignore()
            continue

        if c == '>':
            state.expect('>')
            state.ignore()
            params['>>'] = True
            state.match(r'.*$')
            params['file_name'] = state.emit().strip()
            continue

        if c == '!':
            state.ignore()
            state.match(r'.*$')
            params['cmd'] = state.emit()
            continue

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

    state.expect_eof()

    command.params = params
    command.forced = bang

    return None, [command, TokenEof()]