Skip to content

Instantly share code, notes, and snippets.

@qwIvan
Forked from kearnh/ultisnips_py_console
Created June 3, 2017 07:20
Show Gist options
  • Save qwIvan/5cce06be4cd3043876249d485f06a30a to your computer and use it in GitHub Desktop.
Save qwIvan/5cce06be4cd3043876249d485f06a30a to your computer and use it in GitHub Desktop.

Revisions

  1. @kearnh kearnh revised this gist Jun 9, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions ultisnips_py_console
    Original file line number Diff line number Diff line change
    @@ -98,8 +98,10 @@ if out:
    r = vim.eval('@a').strip()
    vim.command('let @a="{}"'.format(old_a))

    if re.match('.*at 0x[0-9A-F]{8}>', r) or r.startswith('<built-in '):
    r = ''
    try:
    r = re.sub('(?<= at )0x[0-9A-F]*(?=>$)', 'some address', r)
    except Exception as e:
    r = str(e)

    py.last_cmd = cmd

  2. @kearnh kearnh created this gist Feb 17, 2014.
    134 changes: 134 additions & 0 deletions ultisnips_py_console
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,134 @@
    global !p
    import string, vim
    import textwrap

    _snips_fns = {}


    def py(*args):
    import re
    old_a = vim.eval('@a')

    cmd = ' '.join(args)
    if cmd.startswith('print '):
    cmd = cmd[5:]
    splits = re.split('\s', cmd)

    if py.last_cmd != cmd:
    vim.command('let @a=""')
    vim.command('redir @a')

    vim.command("""py
    out = []
    try:
    gs = globals()
    out.append(str(eval(''' {0} '''.strip())))
    if out[-1].startswith('<built-in'):
    out.pop[0]
    except Exception as e:
    print e.message
    try:
    exec(''' {0} '''.strip())
    except:
    import keyword
    import __builtin__
    cmd = ''' {0} '''.strip()
    text = ''' {1} '''.strip()

    if text:
    if text.endswith('?'):
    try:
    exec('help(%s)' % text.rstrip('?'))
    except:
    pass
    elif '.' in text:

    def get_class_members(klass):
    ret = dir(klass)
    if hasattr(klass,'__bases__'):
    for base in klass.__bases__:
    ret = ret + get_class_members(base)
    return ret

    m = re.match(r'(\w+(\.\w+)*)\.(\w*)', text)
    if m:
    expr, attr = m.group(1, 3)
    thisobject = None
    try:
    thisobject = eval(expr, globals())
    except Exception:
    pass

    if thisobject is not None:
    # get the content of the object, except __builtins__
    words = dir(thisobject)
    if '__builtins__' in words:
    words.remove('__builtins__')

    if hasattr(thisobject, '__class__'):
    words.append('__class__')
    words.extend(get_class_members(thisobject.__class__))
    matches = []
    n = len(attr)
    for word in words:
    if word[:n] == attr and hasattr(thisobject, word):
    val = getattr(thisobject, word)
    word = '%s.%s' % (expr, word)
    out.append(word)
    else:
    n = len(text)
    for word in keyword.kwlist:
    if word[:n] == text:
    out.append(word)
    for nspace in [__builtin__.__dict__, globals()]:
    for word, val in nspace.items():
    if word[:n] == text and word != '__builtins__':
    out.append(word)

    if len(out) == 1:
    out = []

    if out:
    out = out + [''] * ((4 - (len(out) % 4)) % 4)
    _out = [out[i*4:i*4+4] for i in range(len(out)) if out[i*4:i*4+4]]
    longest = [max(map(lambda x: len(str(x)), o)) for o in zip(*_out)]
    print '\\n'.join(' '.join(('%%-%ds' % longest[i]) % str(w) for i, w in enumerate(ws)) for ws in _out)
    """.format(cmd, splits[-1].rstrip(')')))
    vim.command('redir END')
    r = vim.eval('@a').strip()
    vim.command('let @a="{}"'.format(old_a))

    if re.match('.*at 0x[0-9A-F]{8}>', r) or r.startswith('<built-in '):
    r = ''

    py.last_cmd = cmd

    if r:
    py.cache = r
    return r
    return ''
    else:
    py.last_cmd = cmd
    return py.cache
    py.last_cmd = None
    py.cache = None

    endglobal


    snippet p "p"
    >>> ${1: }
    `!p
    d = dir()
    snip.rv = ''
    if t[1] and t[1].strip() != 't':
    try:
    snip.rv = py(t[1])
    except:
    pass
    if not snip.rv:
    snip.rv = '...'
    `
    $2
    $0
    endsnippet