blender/release/scripts/textplugin_imports.py
Ian Thompson 6352cd509e BPyTextPlugin now has descriptors for variables, functions and classes (and their variables/functions). Each descriptor also holds the line number of the definition allowing a simple outliner to be written.
Text.setCursorPos(row, col) now pops the text into view if it is in the active window space. The outliner uses this to jump to any definition in a script; it is invoked with Ctrl+T.
2008-07-21 00:38:42 +00:00

73 lines
1.6 KiB
Python

#!BPY
"""
Name: 'Import Complete'
Blender: 246
Group: 'TextPlugin'
Shortcut: 'Space'
Tooltip: 'Lists modules when import or from is typed'
"""
# Only run if we have the required modules
OK = False
try:
import bpy, sys
from BPyTextPlugin import *
OK = True
except ImportError:
pass
def main():
txt = bpy.data.texts.active
if not txt:
return
line, c = current_line(txt)
# Check we are in a normal context
if get_context(txt) != CTX_NORMAL:
return
pos = line.rfind('from ', 0, c)
# No 'from' found
if pos == -1:
# Check instead for straight 'import'
pos2 = line.rfind('import ', 0, c)
if pos2 != -1 and (pos2 == c-7 or (pos2 < c-7 and line[c-2]==',')):
items = [(m, 'm') for m in get_modules()]
items.sort(cmp = suggest_cmp)
txt.suggest(items, '')
# Immediate 'from' before cursor
elif pos == c-5:
items = [(m, 'm') for m in get_modules()]
items.sort(cmp = suggest_cmp)
txt.suggest(items, '')
# Found 'from' earlier
else:
pos2 = line.rfind('import ', pos+5, c)
# No 'import' found after 'from' so suggest it
if pos2 == -1:
txt.suggest([('import', 'k')], '')
# Immediate 'import' before cursor and after 'from...'
elif pos2 == c-7 or line[c-2] == ',':
between = line[pos+5:pos2-1].strip()
try:
mod = get_module(between)
except ImportError:
print 'Module not found:', between
return
items = [('*', 'k')]
for (k,v) in mod.__dict__.items():
items.append((k, type_char(v)))
items.sort(cmp = suggest_cmp)
txt.suggest(items, '')
# Check we are running as a script and not imported as a module
if __name__ == "__main__" and OK:
main()