Permalink
Browse files

Version 0.3.1

Keymappings to work with related files when in a app.
Extensions to work with NERDTree and VirtualEnv
Custom activation of projects
Misc bug fixes
  • Loading branch information...
1 parent 64800eb commit 71b0ae9df54e105ddc002b7014e74a96698b8e72 @cwood cwood committed with Jul 22, 2012
View
@@ -1,7 +1,11 @@
-Django Vim
+Vim-Django
===============================================================================
-Django vim wraps the manage.py and will allow vim to work with your project.
+Vim-Django is a vim script that helps manage a django app. It has wrapping's
+for the ``django-admin.py``, your projects ``manage.py`` and some helper
+commands. It also has some quick ways to find templates, edit related files
+in a app, create new apps, and help manage django while using Vim.
+
Commands
-------------------------------------------------------------------------------
@@ -26,14 +30,43 @@ apps, then run the ``django-admin.py`` to create the app there.
Mappings
-------------------------------------------------------------------------------
-TODO
+There are mappings for easily editing related django files while working on a
+app. They all can be changed if you dont like how they are currently mapped.
+There are mappins for (v|views), (u|urls), (a|admin),
+and (t|tests). If you want to remap these you can call the function
+``django#apps#edit('name_of_file.py', 'split')`` and it will open up
+the file it fineds in a split.
Configuration
-------------------------------------------------------------------------------
``g:django_projects`` is where your django projects are located.
+``g:django_activate_virtualenv`` when true will activate a virtualenv that
+starts with the same name. An example being. colinbits, is the site, and the
+virtuale env being colinbits.com.
+
+
+Custom Activation of Projects
+-------------------------------------------------------------------------------
+
+Since not everyone uses the default settings file vim-django will allow you to
+add a custom function to activate any project. In your vimrc you can define a
+function called ``ActivateProject`` it needs to accept one argument. From here
+you can have it activate the project.
+
+Working with other Scripts
+-------------------------------------------------------------------------------
+
+NERDTree:
+ ``g:django_activate_nerdtree`` when set to 1 will launch nerdtree when
+ activating the django project.
+
+VirtualEnv:
+ ``g:django_activate_virtualenv`` when set to 1 will activate the virtual
+ env that starts with the same name.
+
Installing
-------------------------------------------------------------------------------
View
@@ -0,0 +1,4 @@
+" django.vim - A bangin' interface for ViM and Django
+" Maintainer: Colin Wood <cwood06@gmail.com>
+" Version: 0.2.0a
+" License: Same as ViM. see http://www.gnu.org/licenses/vim-license.txt
View
@@ -0,0 +1,36 @@
+function! django#apps#create_app(app_name, ...)
+ " Should create a app if a projects_app is specified or
+ " should take a python path and create it there
+ let pypath = split(a:app_name , '\.')
+ let new_app = pypath[-1]
+
+ exec 'chdir '.g:project_directory
+ for app in pypath
+
+ if app == new_app
+ silent exec '! django-admin.py startapp '.new_app
+ echo "Created new app at ".join(pypath, '.')
+ break
+ endif
+
+ if !isdirectory(app)
+ call mkdir(app)
+ silent exec '!touch '.app.'/__init__.py'
+ endif
+
+ exec 'chdir '.app
+ endfor
+endfunction
+
+
+function! django#apps#edit(file_prefix, open_as)
+ " BUG: This function will have issues if the file is a model such as custom
+ " models. Since this can be a __init__ file inside of the models.py.
+ " Should check both for a file_prefix.py and a folder called file_prefix.
+ let current_app_directory = fnamemodify(expand('%:p'), ':h')
+ let python_files = split(globpath(current_app_directory, '**/'.a:file_prefix))
+ if len(python_files) == 1
+ let only_file = python_files[0]
+ call django#utils#vim_open(only_file, a:open_as)
+ endif
+endfunction
@@ -0,0 +1,9 @@
+function! django#commands#manage(command, ...)
+ let file_regex = '**/manage.py'
+ let manage = split(globpath(g:project_directory, file_regex))[0]
+ execute '!python '.manage.' '.a:command
+endfunction
+
+function! django#commands#admin(comamnd, ...)
+ execute '!django-admin.py '.a:command
+endfunction
@@ -0,0 +1,135 @@
+function! django#completions#pypath(prefix, ...)
+python << EOB
+import os
+import vim
+import sys
+
+class NoMatchFound(Exception):
+ pass
+
+def create_prefix_paths(prefix):
+ "Should return a list of items to compare aginst"
+
+ if not prefix:
+ paths = []
+ else:
+ try:
+ paths = prefix.split('.')
+ except AttributeError:
+ paths = [prefix]
+ return paths
+
+def create_possible_paths(current_root, current_prefix):
+ "Should take the prefix list and return a list of possible paths"
+
+ if not isinstance(current_prefix, list):
+ raise Exception('Prefix is not a list. What we gunna do!')
+
+ full_path = os.path.join(current_root, *current_prefix)
+
+ if not os.path.exists(full_path):
+ if full_path == current_root:
+ directories = _get_directories(current_root)
+ else:
+ base, head = os.path.split(full_path)
+ directories = _get_directories(base)
+ else:
+ directories = _get_directories(full_path)
+
+ paths = []
+
+ if not current_prefix:
+ begining_path = ''
+ else:
+ begining_path = '.'.join(current_prefix[:-1])
+ matching = current_prefix[-1]
+
+ if begining_path.startswith('.'):
+ begining_path = begining_path[1:]
+
+ for directory in directories:
+
+ if begining_path and matching:
+ if directory.startswith(matching):
+ possible_path = begining_path+'.'+directory+'.'
+ paths.append(possible_path)
+
+ elif begining_path and not matching:
+ possible_path = begining_path+'.'+directory+'.'
+ paths.append(possible_path)
+ else:
+ possible_path = directory+'.'
+ paths.append(possible_path)
+
+
+ return paths
+
+def _get_directories(path):
+
+ directories = []
+ for item in os.listdir(path):
+ if os.path.isdir(os.path.join(path, item)) and not item.startswith('.'):
+ directories.append(item)
+
+ return directories
+
+prefix = vim.eval('a:prefix')
+project_root = vim.eval('g:project_directory')
+
+prefix_path = create_prefix_paths(prefix)
+possible_paths = create_possible_paths(project_root, prefix_path)
+
+vim.command('return '+str(possible_paths))
+EOB
+endfunction
+
+
+function! django#completions#managmentcommands(prefix, ...)
+python << EOB
+from django.core.management import get_commands
+
+prefix = vim.eval('a:prefix')
+commands = list(get_commands())
+
+if prefix:
+ commands = [command for command in commands if command.startswith(prefix)]
+
+vim.command('return '+str(commands))
+EOB
+endfunction
+
+function! django#completions#projectscomplete(prefix, ...)
+ " TODO: Make me faster!
+ let file_regex = '**/settings.py'
+ let arg_regex = 'v:val =~ "'.a:prefix.'"'
+
+ let all_settings_files = split(globpath(g:django_projects, file_regex))
+
+ let all_projects = []
+
+ for setting_file in all_settings_files
+ let project = fnamemodify(setting_file, ':h:t')
+ call add(all_projects, project)
+ endfor
+
+ if a:prefix == ''
+ return all_projects
+ endif
+
+ return filter(copy(all_projects), arg_regex)
+endfunction
+
+function! django#completions#installed_apps(prefix, ...)
+python << EOB
+import vim
+from django.conf import settings
+prefix = vim.eval('a:prefix')
+
+if prefix:
+ apps = [app for app in settings.INSTALLED_APPS if app.startswith(prefix)]
+else:
+ apps = settings.INSTALLED_APPS
+
+vim.command('return'+str(apps))
+EOB
+endfunction
@@ -0,0 +1,42 @@
+if !exists('*ActivateProject')
+function! ActivateProject(project)
+python << EOF
+import vim
+import sys
+import os
+
+os.environ['DJANGO_SETTINGS_MODULE'] = vim.eval('a:project')+'.settings'
+sys.path.append(vim.eval('g:project_directory'))
+EOF
+endfunction
+endif
+
+function! django#project#activate(project)
+
+ let file_regex = '**/'.a:project.'/settings.py'
+ let file = split(globpath(g:django_projects, file_regex))[0]
+ let g:project_directory = fnamemodify(file, ':h:h')
+ let g:project_name = a:project
+
+ if exists('g:django_activate_virtualenv')
+ if exists('g:virtualenv_loaded') && g:django_activate_virtualenv == 1
+ for env in virtualenv#names(a:project)
+ call virtualenv#activate(env)
+ endfor
+ else
+ echoerr 'VirtualEnv not installed. Not activating.'
+ endif
+ endif
+
+ if exists('g:django_activate_nerdtree')
+ if exists('g:loaded_nerd_tree') && g:django_activate_nerdtree == 1
+ exec ':NERDTree '.g:project_directory
+ else
+ echoerr "NERDTree not installed. Can not open."
+ endif
+
+ exec 'set path+='.expand(g:project_directory)
+
+ call ActivateProject(a:project)
+
+endfunction
@@ -0,0 +1,5 @@
+function! django#templates#find(name)
+ let file_regex = '**/templates/'.a:name
+ let possible_paths = split(globpath(g:project_directory, file_regex))
+ return possible_paths
+endfunction
@@ -0,0 +1,9 @@
+function! django#utils#vim_open(file, open_as)
+ if a:open_as is 'tab'
+ exec 'tabnew '.a:file
+ elseif a:open_as is 'vsplit'
+ exec 'vsplit '.a:file
+ elseif a:open_as is 'split'
+ exec 'split'.a:file
+ endif
+endfunction
Oops, something went wrong.

0 comments on commit 71b0ae9

Please sign in to comment.