Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 1.0: Initial upload

  • Loading branch information...
commit 20c821e3cc45ad179b4acbe278f7582606e4a1a1 0 parents
zack shen authored committed
Showing with 313 additions and 0 deletions.
  1. +31 −0 README
  2. +282 −0 plugin/djangojump.vim
31 README
@@ -0,0 +1,31 @@
+This is a mirror of http://www.vim.org/scripts/script.php?script_id=4257
+
+\\\" vim-djangojump plugin is used for
+\\\" jumping between url, view function and
+\\\" template immediately.
+\\\"
+\\\" note: now open vim at django project\\&#039s
+\\\" root path where has settings.py file
+\\\"
+\\\" author: zack
+\\\"
+\\\" tip:
+\\\" 1. url => view function
+\\\" :DUrltoview /app1/overview/
+\\\"
+\\\" 2. view function ==> template file
+\\\" :DViewtotpl (at line which contains
+\\\" template\\&#039s name)
+\\\"
+\\\" example code:
+\\\" ....
+\\\" (*) return render_to_response(\\\"index.html\\\")
+\\\"
+\\\" cursor at this line(*) and call command
+\\\" :DViewtotpl
+\\\"
+\\\" 3. template file ==> view function
+\\\" :DTpltoview
+\\\"
+\\\" 4. reload cache
+\\\" :DReloadCache
282 plugin/djangojump.vim
@@ -0,0 +1,282 @@
+" *********************************
+" vim-djangojump plugin is used for
+" jumping between view function and
+" template immediately.
+"
+" note: now open vim at django project's
+" root path where has settings.py file
+"
+" author: zack
+"
+" tip:
+" 1. url => view function
+" :DUrltoview /app1/overview/
+"
+" 2. view function ==> template file
+" :DViewtotpl (at line which contains
+" template's name)
+"
+" example code:
+" ....
+" (*) return render_to_response("index.html")
+"
+" cursor at this line(*) and call command
+" :DViewtotpl
+"
+" 3. template file ==> view function
+" :DTpltoview
+"
+" 4. reload cache
+" :DReloadCache
+"
+" *********************************
+
+
+python << EOF
+
+import vim
+import os
+import imp
+import sys
+import re
+
+PROJECT = os.path.basename(os.path.abspath(os.curdir))
+PROJECT_PATH = os.path.abspath(os.curdir)
+CONFIG_PATH = os.path.expanduser('~/.djangojump/')
+SETTINGS = "settings"
+
+DJANGO_SETTINGS_MODULE = "%s.%s" %(PROJECT, SETTINGS)
+
+sys.path.append(os.path.abspath('.'))
+sys.path.append(os.path.join(os.path.abspath('.'), '..'))
+
+template_cache_dir_map = {}
+template_view_map_cache = {}
+pattern_cache = []
+
+try:
+ imp.find_module(SETTINGS)
+except Exception, e:
+ pass
+else:
+ os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE
+ import settings
+
+def load_tplname_to_cache():
+
+ import pickle
+
+ if not os.path.exists(CONFIG_PATH):
+ os.mkdir(CONFIG_PATH)
+
+ global template_view_map_cache
+ global pattern_cache
+ cache_file = os.path.join(CONFIG_PATH, 'template_view_map_cache')
+ pattern_cache_file = os.path.join(CONFIG_PATH, 'pattern_cache')
+ if os.path.exists(cache_file):
+ try:
+ template_view_map_cache = pickle.load(open(cache_file, 'r'))
+ except Exception,e:
+ print e
+
+ try:
+ pattern_cache = pickle.load(open(pattern_cache_file, 'r'))
+ except Exception,e:
+ print e
+
+ return
+
+ from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
+
+ try:
+ import settings
+ except:
+ return
+
+ imp_m = __import__(settings.ROOT_URLCONF)
+ try:
+ urlpatterns = imp_m.urls.urlpatterns
+ except:
+ urlpatterns = imp_m.urlpatterns
+
+ template_dirs = None
+ try:
+ template_dirs = settings.TEMPLATE_DIRS
+ except:
+ return
+
+
+ def load_tpl_view_to_cache(urlpatterns, prefix_pattern=None):
+ for url in urlpatterns:
+ if isinstance(url, RegexURLResolver):
+ load_tpl_view_to_cache(url.url_patterns, url._regex)
+ elif isinstance(url, RegexURLPattern):
+ _callback = None
+ try:
+ _callback = '%s.%s' %(url._callback.__module__, url._callback.__name__)
+ except:
+ _callback = url._callback_str
+
+ if _callback.find('django') == 0:
+ continue
+ else:
+ # rebuild pattern
+ url_regexp = ''
+ if prefix_pattern:
+ url_regexp = url._regex
+ if url_regexp.find('^') == 0:
+ url_regexp = url_regexp[1:]
+ url_regexp = '%s%s' %(prefix_pattern, url_regexp)
+ else:
+ url_regexp = '%s%s' %(prefix_pattern, url_regexp)
+ else:
+ url_regexp = url._regex
+
+ _callback_split = _callback.split('.')
+ sep = os.path.sep
+ module_path_part = sep.join(_callback_split[:len(_callback_split)-1]) + ".py"
+ module_abs_path = os.path.join(PROJECT_PATH, module_path_part)
+ method_name = _callback_split[-1]
+
+ pattern_cache.append({'module': module_abs_path, 'method': method_name, 'regexp': url_regexp})
+
+ with open(module_abs_path, 'r') as fp:
+ code_block = []
+ isAfterFunc = False
+ view_at_line_num = 0
+ for line in fp:
+ view_at_line_num += 1
+ match = re.search('^def\s+%s' %method_name, line)
+ if match:
+ code_block.append(line)
+ isAfterFunc = True
+ continue
+
+ if isAfterFunc:
+ code_block.append(line)
+ match2 = re.search('^def\s+.+', line)
+ if match2:
+ break
+
+ for cline in code_block:
+ match = re.search('[\'\"].+\.html[\'\"]', cline)
+ if match:
+ tpl_part_path = match.group()
+ tpl_part_path = tpl_part_path[1:len(tpl_part_path)-1]
+ for tmpdir in template_dirs:
+ tpl_abspath = os.path.join(tmpdir, tpl_part_path)
+ if os.path.exists(tpl_abspath):
+ view_at_line_num -= len(code_block)
+ template_view_map_cache.setdefault(tpl_abspath, []).append({'path': module_abs_path, 'linenum': view_at_line_num})
+
+ load_tpl_view_to_cache(urlpatterns)
+
+ try:
+ pickle.dump(template_view_map_cache, open(cache_file, 'w'))
+ pickle.dump(pattern_cache, open(pattern_cache_file, 'w'))
+ except Exception, e:
+ pass
+
+load_tplname_to_cache()
+
+
+def url_to_view(url):
+ for pattern in pattern_cache:
+ regexp = pattern.get('regexp', None)
+ if regexp:
+ if url.find('/') == 0:
+ url = url[1:]
+ match = re.match(regexp, url)
+ if match:
+ module_abs_path = pattern.get('module')
+ method_name = pattern.get('method')
+ vim.command(":set wrapscan")
+ vim.command(":e %s" %module_abs_path)
+ vim.command("/^def\_s%s" %method_name)
+ break
+
+
+def view_to_template():
+ template_dirs = None
+ try:
+ template_dirs = settings.TEMPLATE_DIRS
+ except:
+ return
+
+ line = vim.current.line
+ line = "".join(line)
+
+ match = re.search('[\'\"].+\.html[\'\"]', line)
+ if match:
+ html_name = match.group()
+ html_name = html_name[1:len(html_name)-1]
+ html_name_split = html_name.split('/')
+ html_name = html_name_split[len(html_name_split) - 1]
+ left_path = "/".join(html_name_split[0:len(html_name_split)-1])
+
+ def get_abspath_from_cache(tpl_name, path_left_part=None):
+ part_path = os.path.join(path_left_part, tpl_name)
+ for abspath in template_cache_dir_map.get(tpl_name, []):
+ if abspath.find(part_path) > 0:
+ return abspath
+
+ def get_tpl_abspath(tpl_name, path_left_part):
+ tmp_dir = get_abspath_from_cache(tpl_name, path_left_part)
+ if tmp_dir:
+ return tmp_dir
+
+ for tpldir in template_dirs:
+ for dirpath, dirnames, filenames in os.walk(tpldir):
+ for fname in filenames:
+ if fname == tpl_name:
+ abspath = os.path.abspath(os.path.join(dirpath, fname))
+ part_path = os.path.join(path_left_part, tpl_name)
+ if abspath.find(part_path) > 0:
+ return abspath
+
+ tpl_abspath = get_tpl_abspath(html_name, left_path)
+
+ def is_in_dir_cache(tpl_path):
+ tpl_key = os.path.basename(tpl_path)
+ for path in template_cache_dir_map.get(tpl_key, []):
+ if path == tpl_path:
+ return True
+
+ def add_to_tpl_cache(tpl_abspath):
+ if not is_in_dir_cache(tpl_abspath):
+ tpl_key = os.path.basename(tpl_abspath)
+ template_cache_dir_map.setdefault(tpl_key, []).append(tpl_abspath)
+
+ if tpl_abspath:
+ add_to_tpl_cache(tpl_abspath)
+ vim.command(":e %s" % tpl_abspath)
+
+def template_to_view():
+ tpl_abspath = vim.current.window.buffer.name
+ view_paths = template_view_map_cache.get(tpl_abspath, None)
+ if view_paths:
+ vp = view_paths[0]
+ path = vp.get('path')
+ linenum = vp.get('linenum')
+ if os.path.exists(path):
+ vim.command(':e %s' %path)
+ vim.command(':%d' %linenum)
+
+def reload_to_cache():
+ print 'start reload'
+ cache_file = os.path.join(CONFIG_PATH, 'template_view_map_cache')
+ pattern_cache_file = os.path.join(CONFIG_PATH, 'pattern_cache')
+ try:
+ os.remove(cache_file)
+ os.remove(pattern_cache_file)
+ except Exception,e:
+ print e
+ load_tplname_to_cache()
+ print 'finish reload'
+
+EOF
+
+com! -nargs=1 DUrltoview python url_to_view(<f-args>)
+com! DViewtotpl python view_to_template()
+com! DTpltoview python template_to_view()
+com! DReloadCache python reload_to_cache()
Please sign in to comment.
Something went wrong with that request. Please try again.