python Auto complete for Vim

python。。恩,有点意思。。。。
介于vim中的python补全现在主要是利用字典,所以,一时兴起,就是自己写了一个python成员列表提醒(动态的哦)
应该算很简陋,但是基本功能已经实现了。
 
———代码开始,放在vimrc中即可——–
python << EOF
import vim
import re
import string
def Py_GetAttributesByName(ModulePath): #{{{2
    if len(ModulePath) == 0 :
        return ""
    # split the ModulePath to ModuleName and MemeberPath , ex : os.path
    # ==> ‘os’ & ‘.path’
    findResult = re.match(‘(w+)(.*)’,ModulePath)
    ModuleName = findResult.group(1)
    MemberPath = findResult.group(2)
    # import Module as objModule
    objModule = __import__(ModuleName)
    # Construct the reObj as Search pattern
    reObj = re.compile(‘<.*'(.*)’>’)
    # Make the result
    strMemeberList = eval(‘dir(objModule’ + MemberPath + ‘)’)
    MemberList = []
    for MemberName in strMemeberList:
        TypeStr = eval( ‘type(objModule ‘ + MemberPath + ‘.’ + MemberName + ‘)’ )
        MemberList.append( MemberName + ‘!’ + reObj.match(str(TypeStr)).group(1) )
    return string.join(MemberList,’,’)
#}}}2
EOF
 
function! Py_CompleteAttributes() "{{{2
 let ModulePath = matchstr(getline("."),’m[0-9A-Za-z.]*ze.w*$’)
 redir => AttrNameStr | silent exec "py print Py_GetAttributesByName(‘". ModulePath ."’)" | redir END
 let AttrNameStr = AttrNameStr[1:]
 let AttrNameList = split(AttrNameStr,’,’)
 if len(AttrNameList) == 0
  return ""
 endif
 let AttrNameListDict = []
 for item in AttrNameList
  let AttrName = matchstr( item , ‘mzs.*ze!.*’ )
  let AttrType = matchstr( item , ‘m.*!zs.*ze’ )
  call add( AttrNameListDict , {’word’: AttrName , ‘menu’:AttrType })
 endfor
 call complete(col(‘.’),AttrNameListDict)
 return ""
endfunction "}}}2
if !exists("autoload_python") "{{{2
 let autoload_python = 1
    augroup PYTHON
        au!
        autocmd FileType PYTHON set expandtab shiftwidth=4
        autocmd QuickFixCmdPre * copen
        autocmd FileType PYTHON set makeprg=python %
        autocmd FileType PYTHON inoremap <C-ENTER> <C-R>=Py_CompleteAttributes()<CR>
    augroup END
endif "}}}2
 ———————-over—————-
 
使用方法:
1.我自己编译的vim中加入了python2.5的支持,没有这个代码没运行的哦~!
2.当然了,你的机器也要安装python25环境
3.设置文件set ft=python
4.利用CTRL+ENTER,实现Module成员函数的提示
目前缺陷:
由于python是type unsensitive的,所以,没有办法知道变量的type(虽然,理论上通过语法分析应该能得到,但我没这么大本事)
所以,这段代码,只能对于路径上没有变量的进行成员列表的显示(我实在是不知道,有变量怎么处理)
 
举个例子:
os.path.XXX
在输入os.path. 之后,按ctrl+enter就可列出os.path中的所有成员(funciton ,str之类的东西,还有类型提醒哦。。hoho)
 
恩,就这样了,等有时间再改善啦~~
算是学习完Python语法部分的小总结吧,同时复习了一下好久没这么用的vim函数(们)。。。。
 
PS:不感兴趣的好朋友们略过这篇文章好了 =_=
 
Advertisements

3 thoughts on “python Auto complete for Vim

  1. Hi,Do you need digital signage, digital sign, ad players and ad displays? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

    amberdigital Contact Us
    E-mail:sstar@netvigator.com
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[bbidfihgieec

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s