Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Version 1.0: Initial upload

  • Loading branch information...
commit a1d657d4321bac6bfc806c5a1d5c78400cee2930 0 parents
@phillipberndt phillipberndt authored committed
Showing with 144 additions and 0 deletions.
  1. +17 −0 README
  2. +127 −0 ftplugin/python.vim
17 README
@@ -0,0 +1,17 @@
+This is a mirror of http://www.vim.org/scripts/script.php?script_id=4007
+
+This plugin automates the insertion of import statements at the top of a python script. It contains a function capable of adding import statements at the top of the file. This function is mapped to <C-f>. So you can for example type "sys.exit<C-f>" and the plugin will append "import sys" above the other import statements at the top of the file, unless sys already was imported. The script tries to maintain the import statements in order.
+
+If you define
+ let g:PythonAutoAddImports = 1
+in your vimrc, this script will additionally hook into the period key, such that for common modules,
+imports are added as you type: Typing
+ if len(sys.argv) == 0:
+into an empty file will leave you with
+ import sys
+ if len(sys.argv) == 0:
+then.
+
+This plugin is in its early stages. Also, it is my first vim plugin, so do not expect everything to run smoothly yet. I maintain this script in my vim repo at github:
+
+http://github.com/phillipberndt/vimconfig/blob/master/bundle/vim-python-imports/ftplugin/python.vim
127 ftplugin/python.vim
@@ -0,0 +1,127 @@
+" Python import management
+" Vim plugin
+"
+" Version: 0.1
+" Licence: VIM licence
+"
+" Copyright (c) 2012, Phillip Berndt
+"
+" Maps a function to <C-f> in insert mode which automatically adds import
+" statements if needed. Simply position the cursor over a module name and the
+" import will be added. If there is no word under the cursor the user will be
+" asked to enter a module name.
+"
+" This script also exports a function called g:PythonAddImport
+" which adds an arbitrary line (should start with import or from) at the
+" imports section at the top of the file.
+"
+" If you want automatic imports for common modules, define
+" let g:PythonAutoAddImports = 1
+" to your vimrc.
+"
+" Ideas for improvement:
+" * Somehow handle from .. import .. statements
+" * Recognize strings at the top of the file and insert
+" imports below. Currently, only hash'ed comments are
+" recognized to cope with the shebang and encoding comment
+"
+
+if !exists("g:PythonAddImportDefined")
+let g:PythonAddImportDefined = 1
+
+function g:PythonAddImport(import)
+ let l:oldpos = getpos(".")
+
+ " First check if this already gets imported
+ call cursor(1, 1)
+ if search("^\[ \t\]*" . a:import . "\[ \t\]*$", "cn") != 0
+ call setpos(".", l:oldpos)
+ return
+ end
+
+ " Locate the first import line
+ call cursor(1, 1)
+ let l:firstImport = search("^\[ \t\]*\(from\|import\)\[ \t\]", "cn")
+ if l:firstImport == 0
+ " Search for the first non-comment line and use that one
+ call cursor(1, 1)
+ let l:firstImport = 1
+ while 1
+ let l:firstImport += 1
+ let l:found = search("^\[ \t\]*#", "", line("$"))
+ if l:found == 0 || l:found > l:firstImport
+ break
+ end
+ endwhile
+ end
+ if line("$") < l:firstImport
+ let l:firstImport = line("$")
+ end
+
+ " Insert the new import
+ call cursor(l:firstImport, 1)
+ exe "normal I\<CR>\<ESC>"
+ let l:oldpos[1] += 1
+ call setline(l:firstImport, a:import)
+
+ " Resort the imports
+ call cursor(l:firstImport, 1)
+ let l:lastImport = l:firstImport
+ while 1
+ let l:found = search("^\[ \t\]*\(from\|import\)\[ \t\]", "", line("$"))
+ if l:found == 0 || l:found > l:lastImport + 1
+ break
+ end
+ let l:lastImport += 1
+ endwhile
+ exe "normal :" . l:firstImport . "," . l:lastImport . "sort\<CR>"
+
+ " Position the cursor where it was before
+ let l:oldpos[2] += 1
+ call setpos(".", l:oldpos)
+
+ " Return an empty string to allow to use this in snippets
+ return ""
+endfunction
+
+function <SID>PythonInsert()
+ let l:import = substitute(expand("<cWORD>"), "\\.[^\.]*$", "", "")
+ if l:import == ""
+ let l:import = input("Module to import: ")
+ if l:import == ""
+ return
+ endif
+ end
+ call g:PythonAddImport("import " . l:import)
+endfunction
+
+" Register normal insert hook
+imap <C-f> <C-O>:call <SID>PythonInsert()<CR>
+
+if exists("g:PythonAutoAddImports") && g:PythonAutoAddImports == 1
+ " Get a list of available Python modules
+python <<END
+import sys
+import os
+import vim
+vim.command("let s:PythonModules = " +
+ repr([ y[:-3] for x in filter(os.path.isdir, sys.path) for y in os.listdir(x) if y[-3:] == ".py" ] +
+ [ "sys", "gtk" ]
+ ))
+END
+
+ " Hook for dynamic imports
+ function <SID>DynamicPythonInsert()
+ let l:import = substitute(expand("<cWORD>"), "\\.[^\.]*$", "", "")
+ if l:import == ""
+ return
+ endif
+ if index(s:PythonModules, l:import) == -1
+ return
+ end
+ call g:PythonAddImport("import " . l:import)
+ endfunction
+ imap . <C-V>.<C-O>:call <SID>DynamicPythonInsert()<CR>
+end
+
+endif
Please sign in to comment.
Something went wrong with that request. Please try again.