Commits

Skirmantas Jurgaitis committed cbd8e7e

Improved gf for python modules

  • Participants
  • Parent commits 6fda1c6

Comments (0)

Files changed (1)

.vim/ftplugin/python/go_module.vim

+if exists("g:loaded_go_module")
+    finish
+else
+    let g:go_module = 1
+endif
+
+if !has('python')
+    echoerr "Error: the go_module.vim plugin requires Vim to be compiled with +python"
+    finish
+endif
+
+python << EOF
+import vim
+import sys
+import re
+import glob
+import os
+import subprocess
+
+
+def get_path():
+    p = subprocess.Popen(
+            ['python', '-c', r'import sys; print "\n".join(sys.path)'],
+            stdout=subprocess.PIPE
+        )
+    out, err = p.communicate()
+    return filter(bool, [l.strip() for l in out.splitlines()])
+
+
+def load(path):
+    for path, func in [(path, None),
+                       (os.path.dirname(path), os.path.basename(path))]:
+        if os.path.isdir(path):
+            path = os.path.join(path, '__init__')
+        path += '.py'
+        if os.path.isfile(path):
+            return path, func
+    return None, None
+
+
+def python_goto_file():
+    cw = vim.eval('expand("<cfile>")')
+    module = re.sub('\.', '/', cw)
+    for p in get_path():
+        path, func = load(os.path.join(p, module))
+        if path:
+            vim.command('e %s' % path)
+            if func:
+                searchfor = [(r'\(%s\)' % s) for s in [r'^class %s' % func,
+                                                       r'^def %s' % func,
+                                                       r'^%s =' % func]]
+                vim.command(r'/%s' % r'\|'.join(searchfor))
+            return
+    print >> sys.stderr, 'E447: Can\'t find file "%s" in python\'s sys.path' % cw
+EOF
+
+nnoremap <buffer> gm :python python_goto_file()<cr>