openfile(): Go back to opening the files in text mode. This undoes
[python/dscho.git] / Lib / idlelib / CallTips.py
blobe90da493a34773ec670949043e5f119e64c72a96
1 # CallTips.py - An IDLE extension that provides "Call Tips" - ie, a floating window that
2 # displays parameter information as you open parens.
4 import string
5 import types
7 class CallTips:
9 menudefs = [
12 def __init__(self, editwin):
13 self.editwin = editwin
14 self.text = editwin.text
15 self.calltip = None
16 if hasattr(self.text, "make_calltip_window"):
17 self._make_calltip_window = self.text.make_calltip_window
18 else:
19 self._make_calltip_window = self._make_tk_calltip_window
21 def close(self):
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):
27 import CallTipWindow
28 return CallTipWindow.CallTip(self.text)
30 def _remove_calltip_window(self):
31 if self.calltip:
32 self.calltip.hidetip()
33 self.calltip = None
35 def paren_open_event(self, event):
36 self._remove_calltip_window()
37 arg_text = get_arg_text(self.get_object_at_cursor())
38 if arg_text:
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):
51 if self.calltip:
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.
71 text = self.text
72 chars = text.get("insert linestart", "insert")
73 i = len(chars)
74 while i and chars[i-1] in wordchars:
75 i = i-1
76 word = chars[i:]
77 if word:
78 # How is this for a hack!
79 import sys, __main__
80 namespace = sys.modules.copy()
81 namespace.update(__main__.__dict__)
82 try:
83 return eval(word, namespace)
84 except:
85 pass
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.
91 try:
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
97 return None
99 def get_arg_text(ob):
100 # Get a string describing the arguments for the given object.
101 argText = ""
102 if ob is not None:
103 argOffset = 0
104 if type(ob)==types.ClassType:
105 # Look for the highest __init__ in the class chain.
106 fob = _find_constructor(ob)
107 if fob is None:
108 fob = lambda: None
109 else:
110 argOffset = 1
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.
114 fob = ob.im_func
115 argOffset = 1
116 else:
117 fob = ob
118 # Try and build one for Python defined functions
119 if type(fob) in [types.FunctionType, types.LambdaType]:
120 try:
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:
127 items.append("...")
128 if fob.func_code.co_flags & 0x8:
129 items.append("***")
130 argText = ", ".join(items)
131 argText = "(%s)" % argText
132 except:
133 pass
134 # See if we can use the docstring
135 doc = getattr(ob, "__doc__", "")
136 if doc:
137 while doc[:1] in " \t\n":
138 doc = doc[1:]
139 pos = doc.find("\n")
140 if pos < 0 or pos > 70:
141 pos = 70
142 if argText:
143 argText += "\n"
144 argText += doc[:pos]
146 return argText
148 #################################################
150 # Test code
152 if __name__=='__main__':
154 def t1(): "()"
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, ..., ***)"
161 class TC:
162 "(a=None, ...)"
163 def __init__(self, a=None, *b): "(a=None, ...)"
164 def t1(self): "()"
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, ..., ***)"
171 def test( tests ):
172 failed=[]
173 for t in tests:
174 expected = t.__doc__ + "\n" + t.__doc__
175 if get_arg_text(t) != expected:
176 failed.append(t)
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))
180 tc = TC()
181 tests = t1, t2, t3, t4, t5, t6, \
182 TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6
184 test(tests)