2008-07-15 07:34:46 +00:00
|
|
|
#!BPY
|
|
|
|
"""
|
2008-08-10 16:07:14 +00:00
|
|
|
Name: 'Import Complete|Space'
|
2008-07-15 07:34:46 +00:00
|
|
|
Blender: 246
|
|
|
|
Group: 'TextPlugin'
|
|
|
|
Shortcut: 'Space'
|
|
|
|
Tooltip: 'Lists modules when import or from is typed'
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Only run if we have the required modules
|
|
|
|
try:
|
|
|
|
import bpy, sys
|
|
|
|
from BPyTextPlugin import *
|
2008-07-15 12:55:20 +00:00
|
|
|
except ImportError:
|
2008-07-26 20:02:10 +00:00
|
|
|
OK = False
|
|
|
|
else:
|
|
|
|
OK = True
|
2008-07-15 07:34:46 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
txt = bpy.data.texts.active
|
2008-07-16 12:56:23 +00:00
|
|
|
if not txt:
|
|
|
|
return
|
|
|
|
|
2008-07-15 07:34:46 +00:00
|
|
|
line, c = current_line(txt)
|
|
|
|
|
|
|
|
# Check we are in a normal context
|
2008-07-21 00:38:42 +00:00
|
|
|
if get_context(txt) != CTX_NORMAL:
|
2008-07-15 07:34:46 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
pos = line.rfind('from ', 0, c)
|
|
|
|
|
|
|
|
# No 'from' found
|
|
|
|
if pos == -1:
|
2008-08-10 16:07:14 +00:00
|
|
|
# Check instead for straight 'import xxxx'
|
2008-07-15 07:34:46 +00:00
|
|
|
pos2 = line.rfind('import ', 0, c)
|
2008-08-10 16:07:14 +00:00
|
|
|
if pos2 != -1:
|
|
|
|
pos2 += 7
|
|
|
|
for i in range(pos2, c):
|
|
|
|
if line[i]==',' or (line[i]==' ' and line[i-1]==','):
|
|
|
|
pos2 = i+1
|
|
|
|
elif not line[i].isalnum() and line[i] != '_':
|
|
|
|
return
|
2008-07-16 10:33:48 +00:00
|
|
|
items = [(m, 'm') for m in get_modules()]
|
2008-07-15 07:34:46 +00:00
|
|
|
items.sort(cmp = suggest_cmp)
|
2008-08-10 16:07:14 +00:00
|
|
|
txt.suggest(items, line[pos2:c].strip())
|
|
|
|
return
|
|
|
|
|
|
|
|
# Found 'from xxxxx' before cursor
|
|
|
|
immediate = True
|
|
|
|
pos += 5
|
|
|
|
for i in range(pos, c):
|
2008-08-12 15:17:08 +00:00
|
|
|
if not line[i].isalnum() and line[i] != '_' and line[i] != '.':
|
2008-08-10 16:07:14 +00:00
|
|
|
immediate = False
|
|
|
|
break
|
2008-07-15 07:34:46 +00:00
|
|
|
|
2008-08-10 16:07:14 +00:00
|
|
|
# Immediate 'from' followed by at most a module name
|
|
|
|
if immediate:
|
2008-07-16 10:33:48 +00:00
|
|
|
items = [(m, 'm') for m in get_modules()]
|
2008-07-15 07:34:46 +00:00
|
|
|
items.sort(cmp = suggest_cmp)
|
2008-08-12 15:17:08 +00:00
|
|
|
txt.suggest(items, line[pos:c])
|
2008-08-10 16:07:14 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Found 'from' earlier, suggest import if not already there
|
|
|
|
pos2 = line.rfind('import ', pos, c)
|
2008-07-15 07:34:46 +00:00
|
|
|
|
2008-08-10 16:07:14 +00:00
|
|
|
# No 'import' found after 'from' so suggest it
|
|
|
|
if pos2 == -1:
|
|
|
|
txt.suggest([('import', 'k')], '')
|
|
|
|
return
|
2008-07-15 07:34:46 +00:00
|
|
|
|
2008-08-10 16:07:14 +00:00
|
|
|
# Immediate 'import' before cursor and after 'from...'
|
|
|
|
for i in range(pos2+7, c):
|
|
|
|
if line[i]==',' or (line[i]==' ' and line[i-1]==','):
|
|
|
|
pass
|
|
|
|
elif not line[i].isalnum() and line[i] != '_':
|
|
|
|
return
|
|
|
|
between = line[pos:pos2-1].strip()
|
|
|
|
try:
|
|
|
|
mod = get_module(between)
|
|
|
|
except ImportError:
|
|
|
|
return
|
|
|
|
|
|
|
|
items = [('*', 'k')]
|
|
|
|
for (k,v) in mod.__dict__.items():
|
|
|
|
items.append((k, type_char(v)))
|
|
|
|
items.sort(cmp = suggest_cmp)
|
|
|
|
txt.suggest(items, '')
|
2008-07-15 07:34:46 +00:00
|
|
|
|
2008-07-16 10:33:48 +00:00
|
|
|
# Check we are running as a script and not imported as a module
|
|
|
|
if __name__ == "__main__" and OK:
|
2008-07-15 07:34:46 +00:00
|
|
|
main()
|