1 # CallTips.py - An IDLE extension that provides "Call Tips" - ie, a floating window that
2 # displays parameter information as you open parens.
12 def __init__(self
, editwin
):
13 self
.editwin
= editwin
14 self
.text
= editwin
.text
16 if hasattr(self
.text
, "make_calltip_window"):
17 self
._make
_calltip
_window
= self
.text
.make_calltip_window
19 self
._make
_calltip
_window
= self
._make
_tk
_calltip
_window
22 self
._make
_calltip
_window
= None
24 # Makes a Tk based calltip window. Used by IDLE, but not Pythonwin.
25 # See __init__ above for how this is used.
26 def _make_tk_calltip_window(self
):
28 return CallTipWindow
.CallTip(self
.text
)
30 def _remove_calltip_window(self
):
32 self
.calltip
.hidetip()
35 def paren_open_event(self
, event
):
36 self
._remove
_calltip
_window
()
37 arg_text
= get_arg_text(self
.get_object_at_cursor())
39 self
.calltip_start
= self
.text
.index("insert")
40 self
.calltip
= self
._make
_calltip
_window
()
41 self
.calltip
.showtip(arg_text
)
42 return "" #so the event is handled normally.
44 def paren_close_event(self
, event
):
45 # Now just hides, but later we should check if other
46 # paren'd expressions remain open.
47 self
._remove
_calltip
_window
()
48 return "" #so the event is handled normally.
50 def check_calltip_cancel_event(self
, event
):
52 # If we have moved before the start of the calltip,
53 # or off the calltip line, then cancel the tip.
54 # (Later need to be smarter about multi-line, etc)
55 if self
.text
.compare("insert", "<=", self
.calltip_start
) or \
56 self
.text
.compare("insert", ">", self
.calltip_start
+ " lineend"):
57 self
._remove
_calltip
_window
()
58 return "" #so the event is handled normally.
60 def calltip_cancel_event(self
, event
):
61 self
._remove
_calltip
_window
()
62 return "" #so the event is handled normally.
64 def get_object_at_cursor(self
,
65 wordchars
="._" + string
.ascii_letters
+ string
.digits
):
66 # Usage of ascii_letters is necessary to avoid UnicodeErrors
67 # if chars contains non-ASCII.
69 # XXX - This needs to be moved to a better place
70 # so the "." attribute lookup code can also use it.
72 chars
= text
.get("insert linestart", "insert")
74 while i
and chars
[i
-1] in wordchars
:
78 # How is this for a hack!
80 namespace
= sys
.modules
.copy()
81 namespace
.update(__main__
.__dict
__)
83 return eval(word
, namespace
)
86 return None # Can't find an object.
88 def _find_constructor(class_ob
):
89 # Given a class object, return a function object used for the
90 # constructor (ie, __init__() ) or None if we can't find one.
92 return class_ob
.__init
__.im_func
93 except AttributeError:
94 for base
in class_ob
.__bases
__:
95 rc
= _find_constructor(base
)
96 if rc
is not None: return rc
100 # Get a string describing the arguments for the given object.
104 if type(ob
)==types
.ClassType
:
105 # Look for the highest __init__ in the class chain.
106 fob
= _find_constructor(ob
)
111 elif type(ob
)==types
.MethodType
:
112 # bit of a hack for methods - turn it into a function
113 # but we drop the "self" param.
118 # Try and build one for Python defined functions
119 if type(fob
) in [types
.FunctionType
, types
.LambdaType
]:
121 realArgs
= fob
.func_code
.co_varnames
[argOffset
:fob
.func_code
.co_argcount
]
122 defaults
= fob
.func_defaults
or []
123 defaults
= list(map(lambda name
: "=%s" % name
, defaults
))
124 defaults
= [""] * (len(realArgs
)-len(defaults
)) + defaults
125 items
= map(lambda arg
, dflt
: arg
+dflt
, realArgs
, defaults
)
126 if fob
.func_code
.co_flags
& 0x4:
128 if fob
.func_code
.co_flags
& 0x8:
130 argText
= ", ".join(items
)
131 argText
= "(%s)" % argText
134 # See if we can use the docstring
135 doc
= getattr(ob
, "__doc__", "")
137 while doc
[:1] in " \t\n":
140 if pos
< 0 or pos
> 70:
148 #################################################
152 if __name__
=='__main__':
155 def t2(a
, b
=None): "(a, b=None)"
156 def t3(a
, *args
): "(a, ...)"
157 def t4(*args
): "(...)"
158 def t5(a
, *args
): "(a, ...)"
159 def t6(a
, b
=None, *args
, **kw
): "(a, b=None, ..., ***)"
163 def __init__(self
, a
=None, *b
): "(a=None, ...)"
165 def t2(self
, a
, b
=None): "(a, b=None)"
166 def t3(self
, a
, *args
): "(a, ...)"
167 def t4(self
, *args
): "(...)"
168 def t5(self
, a
, *args
): "(a, ...)"
169 def t6(self
, a
, b
=None, *args
, **kw
): "(a, b=None, ..., ***)"
174 expected
= t
.__doc
__ + "\n" + t
.__doc
__
175 if get_arg_text(t
) != expected
:
177 print "%s - expected %s, but got %s" % (t
, `expected`
, `
get_arg_text(t
)`
)
178 print "%d of %d tests failed" % (len(failed
), len(tests
))
181 tests
= t1
, t2
, t3
, t4
, t5
, t6
, \
182 TC
, tc
.t1
, tc
.t2
, tc
.t3
, tc
.t4
, tc
.t5
, tc
.t6