3 Name: 'Import Complete'
7 Tooltip: 'Lists modules when import or from is typed'
10 # Only run if we have the required modules
14 from BPyTextPlugin import *
20 txt = bpy.data.texts.active
21 line, c = current_line(txt)
23 # Check we are in a normal context
24 if get_context(txt) != 0:
27 pos = line.rfind('from ', 0, c)
31 # Check instead for straight 'import'
32 pos2 = line.rfind('import ', 0, c)
33 if pos2 != -1 and (pos2 == c-7 or (pos2 < c-7 and line[c-2]==',')):
34 items = [(m, 'm') for m in sys.builtin_module_names]
35 items.sort(cmp = suggest_cmp)
36 txt.suggest(items, '')
38 # Immediate 'from' before cursor
40 items = [(m, 'm') for m in sys.builtin_module_names]
41 items.sort(cmp = suggest_cmp)
42 txt.suggest(items, '')
44 # Found 'from' earlier
46 pos2 = line.rfind('import ', pos+5, c)
48 # No 'import' found after 'from' so suggest it
50 txt.suggest([('import', 'k')], '')
52 # Immediate 'import' before cursor and after 'from...'
53 elif pos2 == c-7 or line[c-2] == ',':
54 between = line[pos+5:pos2-1].strip()
56 mod = get_module(between)
58 print 'Module not found:', between
62 for (k,v) in mod.__dict__.items():
63 if is_module(v): t = 'm'
64 elif callable(v): t = 'f'
67 items.sort(cmp = suggest_cmp)
68 txt.suggest(items, '')