annotate runtime/autoload/python3complete.vim @ 17547:1b8463f97365 v8.1.1771

patch 8.1.1771: options test fails on MS-Windows commit https://github.com/vim/vim/commit/d4404b4391a0cd3ab197e09bee7828c88882f6b2 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jul 28 18:38:09 2019 +0200 patch 8.1.1771: options test fails on MS-Windows Problem: Options test fails on MS-Windows. Solution: Add correct and incorrect values for 'completeslash'.
author Bram Moolenaar <Bram@vim.org>
date Sun, 28 Jul 2019 18:45:04 +0200
parents c590de398af9
children 17c4178f26ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2329
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1 "python3complete.vim - Omni Completion for python
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
2 " Maintainer: Aaron Griffin <aaronmgriffin@gmail.com>
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
3 " Version: 0.9
7147
c590de398af9 commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents: 2329
diff changeset
4 " Last Updated: 18 Jun 2009 (small fix 2015 Sep 14 from Debian)
2329
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
5 "
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
6 " Roland Puntaier: this file contains adaptations for python3 and is parallel to pythoncomplete.vim
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
7 "
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
8 " Changes
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
9 " TODO:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
10 " 'info' item output can use some formatting work
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
11 " Add an "unsafe eval" mode, to allow for return type evaluation
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
12 " Complete basic syntax along with import statements
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
13 " i.e. "import url<c-x,c-o>"
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
14 " Continue parsing on invalid line??
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
15 "
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
16 " v 0.9
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
17 " * Fixed docstring parsing for classes and functions
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
18 " * Fixed parsing of *args and **kwargs type arguments
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
19 " * Better function param parsing to handle things like tuples and
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
20 " lambda defaults args
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
21 "
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
22 " v 0.8
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
23 " * Fixed an issue where the FIRST assignment was always used instead of
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
24 " using a subsequent assignment for a variable
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
25 " * Fixed a scoping issue when working inside a parameterless function
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
26 "
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
27 "
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
28 " v 0.7
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
29 " * Fixed function list sorting (_ and __ at the bottom)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
30 " * Removed newline removal from docs. It appears vim handles these better in
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
31 " recent patches
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
32 "
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
33 " v 0.6:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
34 " * Fixed argument completion
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
35 " * Removed the 'kind' completions, as they are better indicated
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
36 " with real syntax
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
37 " * Added tuple assignment parsing (whoops, that was forgotten)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
38 " * Fixed import handling when flattening scope
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
39 "
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
40 " v 0.5:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
41 " Yeah, I skipped a version number - 0.4 was never public.
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
42 " It was a bugfix version on top of 0.3. This is a complete
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
43 " rewrite.
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
44 "
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
45
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
46 if !has('python3')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
47 echo "Error: Required vim compiled with +python3"
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
48 finish
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
49 endif
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
50
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
51 function! python3complete#Complete(findstart, base)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
52 "findstart = 1 when we need to get the text length
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
53 if a:findstart == 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
54 let line = getline('.')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
55 let idx = col('.')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
56 while idx > 0
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
57 let idx -= 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
58 let c = line[idx]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
59 if c =~ '\w'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
60 continue
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
61 elseif ! c =~ '\.'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
62 let idx = -1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
63 break
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
64 else
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
65 break
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
66 endif
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
67 endwhile
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
68
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
69 return idx
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
70 "findstart = 0 when we need to return the list of completions
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
71 else
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
72 "vim no longer moves the cursor upon completion... fix that
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
73 let line = getline('.')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
74 let idx = col('.')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
75 let cword = ''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
76 while idx > 0
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
77 let idx -= 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
78 let c = line[idx]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
79 if c =~ '\w' || c =~ '\.'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
80 let cword = c . cword
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
81 continue
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
82 elseif strlen(cword) > 0 || idx == 0
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
83 break
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
84 endif
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
85 endwhile
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
86 execute "py3 vimpy3complete('" . cword . "', '" . a:base . "')"
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
87 return g:python3complete_completions
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
88 endif
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
89 endfunction
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
90
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
91 function! s:DefPython()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
92 py3 << PYTHONEOF
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
93 import sys, tokenize, io, types
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
94 from token import NAME, DEDENT, NEWLINE, STRING
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
95
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
96 debugstmts=[]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
97 def dbg(s): debugstmts.append(s)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
98 def showdbg():
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
99 for d in debugstmts: print("DBG: %s " % d)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
100
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
101 def vimpy3complete(context,match):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
102 global debugstmts
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
103 debugstmts = []
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
104 try:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
105 import vim
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
106 cmpl = Completer()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
107 cmpl.evalsource('\n'.join(vim.current.buffer),vim.eval("line('.')"))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
108 all = cmpl.get_completions(context,match)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
109 all.sort(key=lambda x:x['abbr'].replace('_','z'))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
110 dictstr = '['
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
111 # have to do this for double quoting
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
112 for cmpl in all:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
113 dictstr += '{'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
114 for x in cmpl: dictstr += '"%s":"%s",' % (x,cmpl[x])
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
115 dictstr += '"icase":0},'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
116 if dictstr[-1] == ',': dictstr = dictstr[:-1]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
117 dictstr += ']'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
118 #dbg("dict: %s" % dictstr)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
119 vim.command("silent let g:python3complete_completions = %s" % dictstr)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
120 #dbg("Completion dict:\n%s" % all)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
121 except vim.error:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
122 dbg("VIM Error: %s" % vim.error)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
123
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
124 class Completer(object):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
125 def __init__(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
126 self.compldict = {}
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
127 self.parser = PyParser()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
128
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
129 def evalsource(self,text,line=0):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
130 sc = self.parser.parse(text,line)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
131 src = sc.get_code()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
132 dbg("source: %s" % src)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
133 try: exec(src,self.compldict)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
134 except: dbg("parser: %s, %s" % (sys.exc_info()[0],sys.exc_info()[1]))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
135 for l in sc.locals:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
136 try: exec(l,self.compldict)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
137 except: dbg("locals: %s, %s [%s]" % (sys.exc_info()[0],sys.exc_info()[1],l))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
138
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
139 def _cleanstr(self,doc):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
140 return doc.replace('"',' ').replace("'",' ')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
141
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
142 def get_arguments(self,func_obj):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
143 def _ctor(class_ob):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
144 try: return class_ob.__init__
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
145 except AttributeError:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
146 for base in class_ob.__bases__:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
147 rc = _ctor(base)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
148 if rc is not None: return rc
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
149 return None
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
150
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
151 arg_offset = 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
152 if type(func_obj) == type: func_obj = _ctor(func_obj)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
153 elif type(func_obj) == types.MethodType: arg_offset = 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
154 else: arg_offset = 0
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
155
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
156 arg_text=''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
157 if type(func_obj) in [types.FunctionType, types.LambdaType,types.MethodType]:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
158 try:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
159 cd = func_obj.__code__
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
160 real_args = cd.co_varnames[arg_offset:cd.co_argcount]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
161 defaults = func_obj.__defaults__ or []
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
162 defaults = ["=%s" % name for name in defaults]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
163 defaults = [""] * (len(real_args)-len(defaults)) + defaults
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
164 items = [a+d for a,d in zip(real_args,defaults)]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
165 if func_obj.__code__.co_flags & 0x4:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
166 items.append("...")
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
167 if func_obj.__code__.co_flags & 0x8:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
168 items.append("***")
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
169 arg_text = (','.join(items)) + ')'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
170 except:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
171 dbg("arg completion: %s: %s" % (sys.exc_info()[0],sys.exc_info()[1]))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
172 pass
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
173 if len(arg_text) == 0:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
174 # The doc string sometimes contains the function signature
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
175 # this works for alot of C modules that are part of the
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
176 # standard library
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
177 doc = func_obj.__doc__
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
178 if doc:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
179 doc = doc.lstrip()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
180 pos = doc.find('\n')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
181 if pos > 0:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
182 sigline = doc[:pos]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
183 lidx = sigline.find('(')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
184 ridx = sigline.find(')')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
185 if lidx > 0 and ridx > 0:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
186 arg_text = sigline[lidx+1:ridx] + ')'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
187 if len(arg_text) == 0: arg_text = ')'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
188 return arg_text
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
189
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
190 def get_completions(self,context,match):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
191 #dbg("get_completions('%s','%s')" % (context,match))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
192 stmt = ''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
193 if context: stmt += str(context)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
194 if match: stmt += str(match)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
195 try:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
196 result = None
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
197 all = {}
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
198 ridx = stmt.rfind('.')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
199 if len(stmt) > 0 and stmt[-1] == '(':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
200 result = eval(_sanitize(stmt[:-1]), self.compldict)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
201 doc = result.__doc__
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
202 if doc is None: doc = ''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
203 args = self.get_arguments(result)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
204 return [{'word':self._cleanstr(args),'info':self._cleanstr(doc)}]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
205 elif ridx == -1:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
206 match = stmt
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
207 all = self.compldict
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
208 else:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
209 match = stmt[ridx+1:]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
210 stmt = _sanitize(stmt[:ridx])
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
211 result = eval(stmt, self.compldict)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
212 all = dir(result)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
213
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
214 dbg("completing: stmt:%s" % stmt)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
215 completions = []
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
216
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
217 try: maindoc = result.__doc__
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
218 except: maindoc = ' '
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
219 if maindoc is None: maindoc = ' '
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
220 for m in all:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
221 if m == "_PyCmplNoType": continue #this is internal
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
222 try:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
223 dbg('possible completion: %s' % m)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
224 if m.find(match) == 0:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
225 if result is None: inst = all[m]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
226 else: inst = getattr(result,m)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
227 try: doc = inst.__doc__
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
228 except: doc = maindoc
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
229 typestr = str(inst)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
230 if doc is None or doc == '': doc = maindoc
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
231
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
232 wrd = m[len(match):]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
233 c = {'word':wrd, 'abbr':m, 'info':self._cleanstr(doc)}
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
234 if "function" in typestr:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
235 c['word'] += '('
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
236 c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
237 elif "method" in typestr:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
238 c['word'] += '('
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
239 c['abbr'] += '(' + self._cleanstr(self.get_arguments(inst))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
240 elif "module" in typestr:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
241 c['word'] += '.'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
242 elif "type" in typestr:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
243 c['word'] += '('
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
244 c['abbr'] += '('
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
245 completions.append(c)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
246 except:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
247 i = sys.exc_info()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
248 dbg("inner completion: %s,%s [stmt='%s']" % (i[0],i[1],stmt))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
249 return completions
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
250 except:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
251 i = sys.exc_info()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
252 dbg("completion: %s,%s [stmt='%s']" % (i[0],i[1],stmt))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
253 return []
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
254
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
255 class Scope(object):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
256 def __init__(self,name,indent,docstr=''):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
257 self.subscopes = []
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
258 self.docstr = docstr
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
259 self.locals = []
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
260 self.parent = None
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
261 self.name = name
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
262 self.indent = indent
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
263
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
264 def add(self,sub):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
265 #print('push scope: [%s@%s]' % (sub.name,sub.indent))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
266 sub.parent = self
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
267 self.subscopes.append(sub)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
268 return sub
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
269
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
270 def doc(self,str):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
271 """ Clean up a docstring """
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
272 d = str.replace('\n',' ')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
273 d = d.replace('\t',' ')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
274 while d.find(' ') > -1: d = d.replace(' ',' ')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
275 while d[0] in '"\'\t ': d = d[1:]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
276 while d[-1] in '"\'\t ': d = d[:-1]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
277 dbg("Scope(%s)::docstr = %s" % (self,d))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
278 self.docstr = d
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
279
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
280 def local(self,loc):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
281 self._checkexisting(loc)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
282 self.locals.append(loc)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
283
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
284 def copy_decl(self,indent=0):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
285 """ Copy a scope's declaration only, at the specified indent level - not local variables """
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
286 return Scope(self.name,indent,self.docstr)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
287
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
288 def _checkexisting(self,test):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
289 "Convienance function... keep out duplicates"
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
290 if test.find('=') > -1:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
291 var = test.split('=')[0].strip()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
292 for l in self.locals:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
293 if l.find('=') > -1 and var == l.split('=')[0].strip():
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
294 self.locals.remove(l)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
295
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
296 def get_code(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
297 str = ""
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
298 if len(self.docstr) > 0: str += '"""'+self.docstr+'"""\n'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
299 for l in self.locals:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
300 if l.startswith('import'): str += l+'\n'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
301 str += 'class _PyCmplNoType:\n def __getattr__(self,name):\n return None\n'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
302 for sub in self.subscopes:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
303 str += sub.get_code()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
304 for l in self.locals:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
305 if not l.startswith('import'): str += l+'\n'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
306
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
307 return str
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
308
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
309 def pop(self,indent):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
310 #print('pop scope: [%s] to [%s]' % (self.indent,indent))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
311 outer = self
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
312 while outer.parent != None and outer.indent >= indent:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
313 outer = outer.parent
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
314 return outer
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
315
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
316 def currentindent(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
317 #print('parse current indent: %s' % self.indent)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
318 return ' '*self.indent
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
319
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
320 def childindent(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
321 #print('parse child indent: [%s]' % (self.indent+1))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
322 return ' '*(self.indent+1)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
323
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
324 class Class(Scope):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
325 def __init__(self, name, supers, indent, docstr=''):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
326 Scope.__init__(self,name,indent, docstr)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
327 self.supers = supers
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
328 def copy_decl(self,indent=0):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
329 c = Class(self.name,self.supers,indent, self.docstr)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
330 for s in self.subscopes:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
331 c.add(s.copy_decl(indent+1))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
332 return c
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
333 def get_code(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
334 str = '%sclass %s' % (self.currentindent(),self.name)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
335 if len(self.supers) > 0: str += '(%s)' % ','.join(self.supers)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
336 str += ':\n'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
337 if len(self.docstr) > 0: str += self.childindent()+'"""'+self.docstr+'"""\n'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
338 if len(self.subscopes) > 0:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
339 for s in self.subscopes: str += s.get_code()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
340 else:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
341 str += '%spass\n' % self.childindent()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
342 return str
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
343
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
344
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
345 class Function(Scope):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
346 def __init__(self, name, params, indent, docstr=''):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
347 Scope.__init__(self,name,indent, docstr)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
348 self.params = params
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
349 def copy_decl(self,indent=0):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
350 return Function(self.name,self.params,indent, self.docstr)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
351 def get_code(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
352 str = "%sdef %s(%s):\n" % \
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
353 (self.currentindent(),self.name,','.join(self.params))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
354 if len(self.docstr) > 0: str += self.childindent()+'"""'+self.docstr+'"""\n'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
355 str += "%spass\n" % self.childindent()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
356 return str
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
357
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
358 class PyParser:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
359 def __init__(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
360 self.top = Scope('global',0)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
361 self.scope = self.top
7147
c590de398af9 commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents: 2329
diff changeset
362 self.parserline = 0
2329
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
363
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
364 def _parsedotname(self,pre=None):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
365 #returns (dottedname, nexttoken)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
366 name = []
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
367 if pre is None:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
368 tokentype, token, indent = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
369 if tokentype != NAME and token != '*':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
370 return ('', token)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
371 else: token = pre
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
372 name.append(token)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
373 while True:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
374 tokentype, token, indent = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
375 if token != '.': break
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
376 tokentype, token, indent = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
377 if tokentype != NAME: break
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
378 name.append(token)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
379 return (".".join(name), token)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
380
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
381 def _parseimportlist(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
382 imports = []
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
383 while True:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
384 name, token = self._parsedotname()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
385 if not name: break
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
386 name2 = ''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
387 if token == 'as': name2, token = self._parsedotname()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
388 imports.append((name, name2))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
389 while token != "," and "\n" not in token:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
390 tokentype, token, indent = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
391 if token != ",": break
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
392 return imports
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
393
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
394 def _parenparse(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
395 name = ''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
396 names = []
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
397 level = 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
398 while True:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
399 tokentype, token, indent = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
400 if token in (')', ',') and level == 1:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
401 if '=' not in name: name = name.replace(' ', '')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
402 names.append(name.strip())
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
403 name = ''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
404 if token == '(':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
405 level += 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
406 name += "("
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
407 elif token == ')':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
408 level -= 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
409 if level == 0: break
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
410 else: name += ")"
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
411 elif token == ',' and level == 1:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
412 pass
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
413 else:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
414 name += "%s " % str(token)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
415 return names
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
416
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
417 def _parsefunction(self,indent):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
418 self.scope=self.scope.pop(indent)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
419 tokentype, fname, ind = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
420 if tokentype != NAME: return None
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
421
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
422 tokentype, open, ind = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
423 if open != '(': return None
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
424 params=self._parenparse()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
425
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
426 tokentype, colon, ind = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
427 if colon != ':': return None
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
428
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
429 return Function(fname,params,indent)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
430
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
431 def _parseclass(self,indent):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
432 self.scope=self.scope.pop(indent)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
433 tokentype, cname, ind = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
434 if tokentype != NAME: return None
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
435
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
436 super = []
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
437 tokentype, thenext, ind = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
438 if thenext == '(':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
439 super=self._parenparse()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
440 elif thenext != ':': return None
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
441
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
442 return Class(cname,super,indent)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
443
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
444 def _parseassignment(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
445 assign=''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
446 tokentype, token, indent = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
447 if tokentype == tokenize.STRING or token == 'str':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
448 return '""'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
449 elif token == '(' or token == 'tuple':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
450 return '()'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
451 elif token == '[' or token == 'list':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
452 return '[]'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
453 elif token == '{' or token == 'dict':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
454 return '{}'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
455 elif tokentype == tokenize.NUMBER:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
456 return '0'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
457 elif token == 'open' or token == 'file':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
458 return 'file'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
459 elif token == 'None':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
460 return '_PyCmplNoType()'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
461 elif token == 'type':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
462 return 'type(_PyCmplNoType)' #only for method resolution
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
463 else:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
464 assign += token
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
465 level = 0
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
466 while True:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
467 tokentype, token, indent = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
468 if token in ('(','{','['):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
469 level += 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
470 elif token in (']','}',')'):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
471 level -= 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
472 if level == 0: break
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
473 elif level == 0:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
474 if token in (';','\n'): break
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
475 assign += token
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
476 return "%s" % assign
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
477
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
478 def donext(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
479 type, token, (lineno, indent), end, self.parserline = next(self.gen)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
480 if lineno == self.curline:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
481 #print('line found [%s] scope=%s' % (line.replace('\n',''),self.scope.name))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
482 self.currentscope = self.scope
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
483 return (type, token, indent)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
484
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
485 def _adjustvisibility(self):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
486 newscope = Scope('result',0)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
487 scp = self.currentscope
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
488 while scp != None:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
489 if type(scp) == Function:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
490 slice = 0
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
491 #Handle 'self' params
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
492 if scp.parent != None and type(scp.parent) == Class:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
493 slice = 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
494 newscope.local('%s = %s' % (scp.params[0],scp.parent.name))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
495 for p in scp.params[slice:]:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
496 i = p.find('=')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
497 if len(p) == 0: continue
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
498 pvar = ''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
499 ptype = ''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
500 if i == -1:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
501 pvar = p
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
502 ptype = '_PyCmplNoType()'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
503 else:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
504 pvar = p[:i]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
505 ptype = _sanitize(p[i+1:])
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
506 if pvar.startswith('**'):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
507 pvar = pvar[2:]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
508 ptype = '{}'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
509 elif pvar.startswith('*'):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
510 pvar = pvar[1:]
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
511 ptype = '[]'
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
512
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
513 newscope.local('%s = %s' % (pvar,ptype))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
514
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
515 for s in scp.subscopes:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
516 ns = s.copy_decl(0)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
517 newscope.add(ns)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
518 for l in scp.locals: newscope.local(l)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
519 scp = scp.parent
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
520
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
521 self.currentscope = newscope
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
522 return self.currentscope
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
523
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
524 #p.parse(vim.current.buffer[:],vim.eval("line('.')"))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
525 def parse(self,text,curline=0):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
526 self.curline = int(curline)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
527 buf = io.StringIO(''.join(text) + '\n')
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
528 self.gen = tokenize.generate_tokens(buf.readline)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
529 self.currentscope = self.scope
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
530
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
531 try:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
532 freshscope=True
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
533 while True:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
534 tokentype, token, indent = self.donext()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
535 #dbg( 'main: token=[%s] indent=[%s]' % (token,indent))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
536
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
537 if tokentype == DEDENT or token == "pass":
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
538 self.scope = self.scope.pop(indent)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
539 elif token == 'def':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
540 func = self._parsefunction(indent)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
541 if func is None:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
542 print("function: syntax error...")
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
543 continue
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
544 dbg("new scope: function")
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
545 freshscope = True
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
546 self.scope = self.scope.add(func)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
547 elif token == 'class':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
548 cls = self._parseclass(indent)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
549 if cls is None:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
550 print("class: syntax error...")
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
551 continue
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
552 freshscope = True
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
553 dbg("new scope: class")
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
554 self.scope = self.scope.add(cls)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
555
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
556 elif token == 'import':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
557 imports = self._parseimportlist()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
558 for mod, alias in imports:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
559 loc = "import %s" % mod
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
560 if len(alias) > 0: loc += " as %s" % alias
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
561 self.scope.local(loc)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
562 freshscope = False
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
563 elif token == 'from':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
564 mod, token = self._parsedotname()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
565 if not mod or token != "import":
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
566 print("from: syntax error...")
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
567 continue
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
568 names = self._parseimportlist()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
569 for name, alias in names:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
570 loc = "from %s import %s" % (mod,name)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
571 if len(alias) > 0: loc += " as %s" % alias
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
572 self.scope.local(loc)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
573 freshscope = False
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
574 elif tokentype == STRING:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
575 if freshscope: self.scope.doc(token)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
576 elif tokentype == NAME:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
577 name,token = self._parsedotname(token)
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
578 if token == '=':
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
579 stmt = self._parseassignment()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
580 dbg("parseassignment: %s = %s" % (name, stmt))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
581 if stmt != None:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
582 self.scope.local("%s = %s" % (name,stmt))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
583 freshscope = False
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
584 except StopIteration: #thrown on EOF
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
585 pass
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
586 except:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
587 dbg("parse error: %s, %s @ %s" %
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
588 (sys.exc_info()[0], sys.exc_info()[1], self.parserline))
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
589 return self._adjustvisibility()
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
590
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
591 def _sanitize(str):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
592 val = ''
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
593 level = 0
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
594 for c in str:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
595 if c in ('(','{','['):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
596 level += 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
597 elif c in (']','}',')'):
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
598 level -= 1
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
599 elif level == 0:
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
600 val += c
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
601 return val
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
602
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
603 sys.path.extend(['.','..'])
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
604 PYTHONEOF
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
605 endfunction
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
606
ad2889f48843 Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
607 call s:DefPython()