1 # CallTips.py - An IDLE extension that provides "Call Tips" - ie, a floating window that
2 # displays parameter information as you open parens.
14 '<<paren-open>>': ['<Key-parenleft>'],
15 '<<paren-close>>': ['<Key-parenright>'],
16 '<<check-calltip-cancel>>': ['<KeyRelease>'],
17 '<<calltip-cancel>>': ['<ButtonPress>', '<Key-Escape>'],
26 def __init__(self
, editwin
):
27 self
.editwin
= editwin
28 self
.text
= editwin
.text
30 if hasattr(self
.text
, "make_calltip_window"):
31 self
._make
_calltip
_window
= self
.text
.make_calltip_window
33 self
._make
_calltip
_window
= self
._make
_tk
_calltip
_window
36 self
._make
_calltip
_window
= None
38 # Makes a Tk based calltip window. Used by IDLE, but not Pythonwin.
39 # See __init__ above for how this is used.
40 def _make_tk_calltip_window(self
):
42 return CallTipWindow
.CallTip(self
.text
)
44 def _remove_calltip_window(self
):
46 self
.calltip
.hidetip()
49 def paren_open_event(self
, event
):
50 self
._remove
_calltip
_window
()
51 arg_text
= get_arg_text(self
.get_object_at_cursor())
53 self
.calltip_start
= self
.text
.index("insert")
54 self
.calltip
= self
._make
_calltip
_window
()
55 self
.calltip
.showtip(arg_text
)
56 return "" #so the event is handled normally.
58 def paren_close_event(self
, event
):
59 # Now just hides, but later we should check if other
60 # paren'd expressions remain open.
61 self
._remove
_calltip
_window
()
62 return "" #so the event is handled normally.
64 def check_calltip_cancel_event(self
, event
):
66 # If we have moved before the start of the calltip,
67 # or off the calltip line, then cancel the tip.
68 # (Later need to be smarter about multi-line, etc)
69 if self
.text
.compare("insert", "<=", self
.calltip_start
) or \
70 self
.text
.compare("insert", ">", self
.calltip_start
+ " lineend"):
71 self
._remove
_calltip
_window
()
72 return "" #so the event is handled normally.
74 def calltip_cancel_event(self
, event
):
75 self
._remove
_calltip
_window
()
76 return "" #so the event is handled normally.
78 def get_object_at_cursor(self
,
79 wordchars
="._" + string
.uppercase
+ string
.lowercase
+ string
.digits
):
80 # XXX - This needs to be moved to a better place
81 # so the "." attribute lookup code can also use it.
83 chars
= text
.get("insert linestart", "insert")
85 while i
and chars
[i
-1] in wordchars
:
89 # How is this for a hack!
91 namespace
= sys
.modules
.copy()
92 namespace
.update(__main__
.__dict
__)
94 return eval(word
, namespace
)
97 return None # Can't find an object.
99 def _find_constructor(class_ob
):
100 # Given a class object, return a function object used for the
101 # constructor (ie, __init__() ) or None if we can't find one.
103 return class_ob
.__init
__.im_func
104 except AttributeError:
105 for base
in class_ob
.__bases
__:
106 rc
= _find_constructor(base
)
107 if rc
is not None: return rc
110 def get_arg_text(ob
):
111 # Get a string describing the arguments for the given object.
115 if type(ob
)==types
.ClassType
:
116 # Look for the highest __init__ in the class chain.
117 fob
= _find_constructor(ob
)
122 elif type(ob
)==types
.MethodType
:
123 # bit of a hack for methods - turn it into a function
124 # but we drop the "self" param.
129 # Try and build one for Python defined functions
130 if type(fob
) in [types
.FunctionType
, types
.LambdaType
]:
132 realArgs
= fob
.func_code
.co_varnames
[argOffset
:fob
.func_code
.co_argcount
]
133 defaults
= fob
.func_defaults
or []
134 defaults
= list(map(lambda name
: "=%s" % name
, defaults
))
135 defaults
= [""] * (len(realArgs
)-len(defaults
)) + defaults
136 items
= map(lambda arg
, dflt
: arg
+dflt
, realArgs
, defaults
)
137 if fob
.func_code
.co_flags
& 0x4:
139 if fob
.func_code
.co_flags
& 0x8:
141 argText
= string
.join(items
, ", ")
142 argText
= "(%s)" % argText
145 # See if we can use the docstring
146 if hasattr(ob
, "__doc__") and ob
.__doc
__:
147 pos
= string
.find(ob
.__doc
__, "\n")
148 if pos
<0 or pos
>70: pos
=70
149 if argText
: argText
= argText
+ "\n"
150 argText
= argText
+ ob
.__doc
__[:pos
]
154 #################################################
158 if __name__
=='__main__':
161 def t2(a
, b
=None): "(a, b=None)"
162 def t3(a
, *args
): "(a, ...)"
163 def t4(*args
): "(...)"
164 def t5(a
, *args
): "(a, ...)"
165 def t6(a
, b
=None, *args
, **kw
): "(a, b=None, ..., ***)"
169 def __init__(self
, a
=None, *b
): "(a=None, ...)"
171 def t2(self
, a
, b
=None): "(a, b=None)"
172 def t3(self
, a
, *args
): "(a, ...)"
173 def t4(self
, *args
): "(...)"
174 def t5(self
, a
, *args
): "(a, ...)"
175 def t6(self
, a
, b
=None, *args
, **kw
): "(a, b=None, ..., ***)"
180 expected
= t
.__doc
__ + "\n" + t
.__doc
__
181 if get_arg_text(t
) != expected
:
183 print "%s - expected %s, but got %s" % (t
, `expected`
, `
get_arg_text(t
)`
)
184 print "%d of %d tests failed" % (len(failed
), len(tests
))
187 tests
= t1
, t2
, t3
, t4
, t5
, t6
, \
188 TC
, tc
.t1
, tc
.t2
, tc
.t3
, tc
.t4
, tc
.t5
, tc
.t6