Commits

ZyX_I committed b9868e5

@%aurum/powerline: Made aurum.powerline utilize more aurum functions

Comments (0)

Files changed (2)

autoload/aurum/powerline.vim

 "▶1 
 scriptencoding utf-8
 execute frawor#Setup('0.0', {'@aurum/cache': '2.3',
-            \                '@/python': '1.0',})
+            \                '@%aurum/repo': '5.0',
+            \            '@%aurum/cmdutils': '4.0',
+            \                    '@/python': '1.0',})
 python import aurum.powerline
 "▶1 plwipe
 function s:F.plwipe(key)
 aurum.powerline.sid     = str(int(vim.eval('s:_sid')))
 aurum.powerline.started = True
 EOF
+"▶1 _unload
+function s:._unload()
+    python reload(aurum.powerline)
+endfunction
 "▶1
 " vim: ft=vim ts=4 sts=4 et fmr=▶,▲

python/aurum/powerline.py

 from importlib import import_module
 from collections import namedtuple
+from time import time
 import aurum.rcdriverfuncs as rcdfuncs
 import aurum.repeatedcmd as rc
 import vim
             break
         yield path
 
-def guess(path):
-    for directory in generate_directories(path):
-        for vcs, vcs_dir in (('git', '.git'),
-                             ('mercurial', '.hg'),
-                             ('bazaar', '.bzr'),
-                             ('subversion', '.svn')):
-            if isdir(join(directory, vcs_dir)):
+funcs_cache = {}
+fname_cache = {}
+
+if hasattr(vim, 'bindeval'):
+    self_dict = vim.bindeval('{}')
+
+    def get_repo(path):
+        global get_repo
+        vim_get_repo = vim.bindeval('<SNR>'+sid+'_Eval("s:_r.repo.get")')
+        get_repo = lambda path: vim_get_repo(path, self=self_dict)
+        return get_repo(path)
+
+    def get_interval(key):
+        global get_interval
+        vim_get_interval = vim.bindeval('<SNR>'+sid+'_Eval("s:_r.cache.getinterval")')
+        get_interval = lambda key: vim_get_interval(key, self=self_dict)
+        return get_interval(key)
+
+    rf_dict = vim.bindeval('{"repo": ":"}')
+
+    def get_rrf():
+        global get_rrf
+        vim_get_rrf = vim.bindeval('<SNR>'+sid+'_Eval("s:_r.cmdutils.getrrf")')
+        get_rrf = lambda : list(vim_get_rrf(rf_dict, 0, 'getsilent', self=self_dict))[1:]
+        return get_rrf()
+
+else:
+    import json
+    def get_repo(path):
+        return vim.eval('<SNR>'+sid+'_Eval(printf("s:_r.repo.get(%s)", string('+json.dumps(path)+')))')
+
+    def get_interval(key):
+        return float(vim.eval('<SNR>'+sid+'_Eval("s:_r.cache.getinterval(\''+key+'\')")'))
+
+    def get_rrf():
+        return vim.eval('<SNR>'+sid+'_Eval("s:_r.cmdutils.getrrf({\'repo\': \':\'}, 0, \'getsilent\')")[1:]')
+
+def get_rf():
+    global funcs_cache
+    global fname_cache
+
+    bufnr = vim.current.buffer.number
+
+    if bufnr in fname_cache and (time()-fname_cache[bufnr][1]) < get_repo_interval():
+        return funcs_cache[bufnr][0], fname_cache[bufnr][0]
+
+    vim_repo, rev, fname = get_rrf()
+    funcs = process_repo(vim_repo)
+
+    if not fname or fname == '0':
+        fname = None
+
+    set_bufnr_cache(funcs_cache, bufnr, funcs)
+    set_bufnr_cache(fname_cache, bufnr, fname)
+
+    return funcs, fname
+
+repo_interval = None
+
+def get_repo_interval():
+    global repo_interval
+    global get_repo_interval
+    repo_interval = get_interval('repo')
+    get_repo_interval = lambda : repo_interval
+    return repo_interval
+
+def set_bufnr_cache(cache_dict, bufnr, value):
+    funcs_cache[bufnr] = (value, time())
+    return value
+
+def process_repo(vim_repo):
+    try:
+        path = vim_repo['path']
+    except TypeError:
+        return None
+
+    vcs = vim_repo['type'].lower()
+    try:
+        if vcs not in vcss:
+            try:
+                module = import_module('aurum.au'+vcs)
+            except:
+                module = None
+
+            try:
+                status_func = func_generators['status'](module.g_repo, getattr(module, '_get_status'))
+            except AttributeError:
                 try:
-                    if vcs not in vcss:
-                        try:
-                            module = import_module('aurum.au'+vcs)
-                        except:
-                            module = None
+                    status_func = getattr(rcdfuncs, vcs+'_status')
+                except AttributeError:
+                    status_func = dummy_func
 
-                        try:
-                            status_func = func_generators['status'](module.g_repo, getattr(module, '_get_status'))
-                        except AttributeError:
-                            try:
-                                status_func = getattr(rcdfuncs, vcs+'_status')
-                            except AttributeError:
-                                status_func = dummy_func
+            try:
+                branch_func = func_generators['branch'](module.g_repo,
+                                                        getattr(module, 'repo_props')['branch'])
+            except AttributeError, KeyError:
+                try:
+                    branch_func = getattr(rcdfuncs, vcs+'_branch')
+                except AttributeError:
+                    branch_func = dummy_func
 
-                        try:
-                            branch_func = func_generators['branch'](module.g_repo,
-                                                                    getattr(module, 'repo_props')['branch'])
-                        except AttributeError, KeyError:
-                            try:
-                                branch_func = getattr(rcdfuncs, vcs+'_branch')
-                            except AttributeError:
-                                branch_func = dummy_func
+            vcss[vcs] = Funcs(status=status_func, branch=branch_func)
+        return specialize(vcss[vcs], path)
+    except:
+        pass
 
-                        vcss[vcs] = Funcs(status=status_func, branch=branch_func)
-                    return specialize(vcss[vcs], directory)
-                except:
-                    pass
     return None
 
+def guess():
+    global funcs_cache
+
+    bufnr = vim.current.buffer.number
+
+    if bufnr in funcs_cache and (time()-funcs_cache[bufnr][1]) < get_repo_interval():
+        return funcs_cache[bufnr][0]
+
+    vim_repo = get_repo(':')
+
+    return set_bufnr_cache(funcs_cache, bufnr, process_repo(vim_repo))
+
 buffers = {}
 started = False
 
     if bufnr in buffers and key in buffers[bufnr]:
         rc.get(buffers[bufnr][key], now=True)
 
-def setup_buffer(bufnr, func_name, *args):
+def setup_buffer(bufnr, func_name, funcs=None, *args):
     if not started:
         vim.eval('FraworLoad(@%aurum/powerline)')
 
         bdict = {}
         buffers[bufnr] = bdict
 
-        funcs = guess(vim.buffers[bufnr-1].name)
+        if not funcs:
+            funcs = guess()
 
         bdict['funcs'] = funcs
     else:
         bdict = buffers[bufnr]
-        funcs = bdict['funcs']
+        if not funcs:
+            funcs = bdict['funcs']
 
     if not funcs:
         return None
 
     if func_name not in bdict:
-        bdict[func_name] = rc.new(float(vim.eval('<SNR>'+sid+'_Eval("s:_r.cache.getinterval(\''+func_name+'\')")')),
-                                  getattr(funcs, func_name), *args)
+        bdict[func_name] = rc.new(get_interval(func_name), getattr(funcs, func_name), *args)
         if func_name == 'status':
             vim.command('autocmd! AuRemovePowerlineRC BufWritePost <buffer='+str(bufnr)+'> '+
                     ':python aurum.powerline.wipe_buffer_key('+str(bufnr)+', '+repr(func_name)+')')
     return bdict[func_name]
 
 def branch():
-    return rc.get(setup_buffer(vim.current.buffer.number, 'branch'))
+    bsetup = setup_buffer(vim.current.buffer.number, 'branch')
+    if bsetup is None:
+        return None
+    return rc.get(bsetup)
 
 def file_vcs_status():
     if not vim.current.buffer.name or vim.eval('&buftype'):
         return None
-    bsetup = setup_buffer(vim.current.buffer.number, 'status', vim.current.buffer.name)
+
+    funcs, fname = get_rf()
+    if not fname or not funcs:
+        return None
+
+    bsetup = setup_buffer(vim.current.buffer.number, 'status', funcs, fname)
     if bsetup is None:
         return None