Menu

[r254]: / drplugins / CodeCompletion.py  Maximize  Restore  History

Download this file

381 lines (298 with data), 12.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# Programmer: Daniel Pozmanter
# E-mail: drpython@bluebottle.com
# Copyright 2004-2005
# Programmer: Tom De Muer
# E-mail: tomdemuer@users.sourceforge.net
#
# Copyright 2004 Tom De Muer
#
# Distributed under the terms of the GPL (GNU Public License)
#
# The DrPython License:
#
# DrPython is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Credits :
# This code is based on the original AutoComplete code from
# Daniel Pozmanter released under the GPL. You can find the original
# AutoComplete code on the project site of DrPython.
# Thanks to Daniel Pozmanter and Franz Steinhaeusler for their support.
#
# Changelog:
# 15/7/04 : 0.0.0 first public release
# 0.0.0a expanded choice for different keyboards
# 16/7/04 : 0.0.0b Reorganized the event binding of the plugin.
# Now the plugin binds to shortcuts.
# Thx to Daniel Pozmanter for the suggestions!
# 17/7/04 : 0.0.0c If no documentation is found the signature is shown
# 18/7/04 : 0.0.1 Local import aliases are recognized
# Local functions are recognized
# Fixed bug where ident[len(... doesn't get recognized as
# trigger for 'len', now searching for identifiers instead of
# non-whitespace
# 19/7/04 : 0.0.1a Local imports without alias are recognized
# 20/7/04 : 0.0.2 Bug fix in function signature rendering
# 21/7/04 : 0.0.2a Global variables eliminated to avoid pollution of namespace
# Added some documentation, no functional changes
#**************************************************
#As of 0.0.3, By Daniel Pozmanter.
#Versions 0.0.0 - 0.0.2 by Tom (Original CallTips Code by Tom) (Original Autocomplete Code by Dan).
#0.0.3 Changelog:
#Bug Report, Thanks Klaus O.
#Bugfix, thanks Franz Steinhausler
#I cleaned up help and about a bit.
#Plus you can now update th namespace from the pop up menu
#or the toolbar.
#0.1.0: Now works in the Active Styled Text Control (DrFrame.GetActiveSTC).
#Bug-Report/Feature Request, Thanks Rene Aguirre
#Updated with 3.9.x changes.
#Now uses manually set shortcuts for autocomplete and calltips,
#to make things simpler.
#Updated the documentation.
#0.2.0:
#Removed parsing code, now uses faster re method.
#Optional toggle codecompletion shortcut added.
#Removed manual update namespace, it is now automatic.
#Indentation Type is now tabs.
#Cleaned Up Imports, Documentation.
#Only does autocomplete for python files (bug-report, thanks Franz Steinhausler).
#0.2.1:
#Now shows the shortcut in the menu.
#Bug-Report with Fix from Franz
#Minor help typo fixed.
#0.2.2:
# update for wxPython 2.8 and DrPython 1.65.
#**************************************************
#The version 0.0.3 mentioned below was not released.
#(or if it was, it was integrated into 0.0.2).
#version 0.0.3 is 0.0.2 modified by Dan
#using the bugfix given by Franz in the forums.
# 06/8/04 : 0.0.3 Complete rewrite, functionality now also include AutoComplete
# The python parser module is used to extract information
# 10/8/04 : 0.0.3a Added shortcut for manual update of parse info
# Preferences
# 18/8/04 : 0.0.3b Fixed preferences bug
# Fixed bug in calltips on variables
# 26/8/04 : 0.0.3c Support for hiding the "__" methods
# 26/8/04 : 0.0.0 Renamed to CodeCompletion
# 06/9/04 : 0.0.1 Only active when document is identified as Python (thx Franz Steinhaeusler)
# Turned of the debug log by default
# 25/9/04 : 0.0.2 Possible to bind to other keys than "." and "(" by ignoring them (thx Dan Kruger)
# CodeCompletion now sees more functions (thx Dan Kruger)
#
# This plugin for DrPython can show calltips and autocompletion info
import wx
import sys, re, os.path, inspect
#Global Regular Expressions
reword = re.compile('(\w+\.)*\w+\Z', re.I | re.M)
reas = re.compile('[a-zA-Z.]*\s* as \s*[a-zA-Z.]*', re.M)
reassignments = re.compile('[a-zA-Z.]*\s*=\s*[a-zA-Z.]*', re.M)
def _isastring(text):
text = text.strip()
return (text[0] == '"') or (text[0] == "'")
def _isacomment(text):
text = text.strip()
return text[0] == '#'
def OnAbout(DrFrame):
AboutString = '''CodeCompletion:
Version: 0.2.2
By Daniel Pozmanter (As of Version 0.0.3)
By Tom De Muer (Versions 0.0.0 - 0.0.2)
Released under the GPL.
Credits (0.0.3 + ):
BugFix, Thanks Franz Steinhausler
Bug-Report, Thanks Franz Steinhausler
Bug-Report with Fix, Thanks Franz Steinhausler
Credits (0.0.0 - 0.0.2):Daniel Pozmanter
For useful suggestions and writing AutoComplete on which
the code for this plugin is based on.
Franz Steinhaeusler and Dan Kruger for bug fixes and improvements.'''
DrFrame.ShowMessage(AboutString, "About")
def GetAlias(stc, word):
#First, find interesting lines to look at:
text = stc.GetText()
p = word.find('.')
if p > -1:
wword = word[:p]
wappend = word[p:]
else:
wword = word
wappend = ''
asmatches = reas.findall(text)
assignmentmatches = reassignments.findall(text)
for m in asmatches:
if m.find(wword) > -1:
if not _isastring(m) and not _isacomment(m):
r = m.find('as')
if m[r+2:].strip() == wword:
return m[:r].strip() + wappend
for m in assignmentmatches:
if m.find(wword) > -1:
if not _isastring(m) and not _isacomment(m):
r = m.find('=')
if m[:r].strip() == wword:
return m[r+1:].strip() + wappend
return word
def GetDoc(word, filename=''):
try:
exec(compile("doc = inspect.getdoc(" + word + ")", filename, 'exec'))
return doc
except:
pass
return ''
def GetMembers(word, filename='', prepend=''):
try:
try:
exec(compile("import " + word, filename, 'exec'))
except:
pass
exec(compile("members = dir(" + word + ")", filename, 'exec'))
except:
return ''
if not prepend:
prepend = word
members.sort()
members.reverse()
results = ''
for m in members:
results = prepend + '.' + m + ' ' + results
return results.rstrip()
def GetWordOfInterest(stc, AutoComplete=False):
"""GetWordOfInterest(stc) -> string
Gives the identifier or module for which the calltip needs to be generated"""
#Get The Current Word
text = stc.GetText()
pos = stc.GetCurrentPos()
partext = text
eol = stc.GetEndOfLineCharacter()
#Get the left bit
i = text[0:pos].rfind(eol)
if i == -1:
i = 0
else:
i = i + len(eol)
#Get the right bit
result = reword.search(text[i:pos])
if result is None:
start = i
else:
start = i + result.start()
word = text[start:pos]
if AutoComplete:
return word, GetAlias(stc, word)
return GetAlias(stc, word)
def Plugin(DrFrame):
def GetAliases():
stc = DrFrame.GetActiveSTC()
return [], []
def OnHideCallTip(event):
event.Skip()
stc = DrFrame.GetActiveSTC()
stc.CallTipCancel()
def OnCallTip(event):
event.Skip()
if not DrFrame.CodeCompletion:
return
stc = DrFrame.GetActiveSTC()
if stc.filetype != 0:
return
#word,partext,origword = GetWordOfInterest(stc,True)
word = GetWordOfInterest( stc )
if (word.find("gtk") > -1) or (word.find("drpython") > -1):
#This crashes wxPython resp. drPython
return
result = GetDoc(word)
if result:
if stc.CallTipActive():
stc.CallTipCancel()
stc.CallTipSetBackground('#FFFFB8')
stc.CallTipShow(stc.GetCurrentPos(), result)
def OnAutoComplete(event):
event.Skip()
if not DrFrame.CodeCompletion:
return
stc = DrFrame.GetActiveSTC()
if stc.filetype != 0:
return
word, wordalias = GetWordOfInterest(stc, True)
if (wordalias.find("gtk") > -1) or (wordalias.find("drpython") > -1):
#This crashes wxPython resp. drPython
return
if wordalias != word:
results = GetMembers(wordalias, prepend=word)
else:
results = GetMembers(word)
if not results:
return
if stc.CallTipActive():
stc.CallTipCancel()
stc.AutoCompShow(len(word), results)
def OnToggleCodeCompletion(event):
DrFrame.CodeCompletion = not DrFrame.CodeCompletion
if DrFrame.CodeCompletion:
DrFrame.togglecodecompletion.SetText(DrFrame.GetPluginMenuLabel('CodeCompletion', 'Toggle Code Completion', 'Disable CodeCompletion'))
else:
DrFrame.togglecodecompletion.SetText(DrFrame.GetPluginMenuLabel('CodeCompletion', 'Toggle Code Completion', 'Enable CodeCompletion'))
def CheckKey(event):
if DrFrame.GetActiveSTC().AutoCompActive():
event.Skip()
return 1
return 0
DrFrame.CodeCompletion = True
menutext = 'Disable CodeCompletion'
#Prefs:
pfile = DrFrame.pluginspreferencesdirectory + "/CodeCompletion.preferences.dat"
if os.path.exists(pfile):
f = file(pfile, 'rb')
t = f.read().strip()
f.close()
try:
DrFrame.CodeCompletion = int(t)
if not DrFrame.CodeCompletion:
menutext = 'Enable CodeCompletion'
except:
pass
#Override the way enter is handled, if in the prompt:
DrFrame.AddKeyEvent(CheckKey, wx.WXK_UP)
DrFrame.AddKeyEvent(CheckKey, wx.WXK_DOWN)
DrFrame.AddKeyEvent(CheckKey, wx.WXK_RETURN)
DrFrame.AddKeyEvent(OnAutoComplete, ord('.'))
DrFrame.AddKeyEvent(OnCallTip, ord('8'), Shift=1)
DrFrame.AddKeyEvent(OnHideCallTip, ord('9'), Shift=1)
DrFrame.AddPluginFunction("CodeCompletion", "Toggle Code Completion", OnToggleCodeCompletion)
DrFrame.LoadPluginShortcuts('CodeCompletion')
menutext = DrFrame.GetPluginMenuLabel('CodeCompletion', 'Toggle Code Completion', menutext)
ID_TOGGLE_CODE_COMPLETION = DrFrame.GetNewId()
DrFrame.togglecodecompletion = wx.MenuItem(DrFrame.optionsmenu, ID_TOGGLE_CODE_COMPLETION, menutext)
DrFrame.optionsmenu.AppendSeparator()
DrFrame.optionsmenu.AppendItem(DrFrame.togglecodecompletion)
DrFrame.Bind(wx.EVT_MENU, OnToggleCodeCompletion, id=ID_TOGGLE_CODE_COMPLETION)
def OnHelp(DrFrame):
helpText = '''CodeCompletion Version 0.2.1
This plugin supports autocomplete (bound to the '.') and
calltips (bound to the '(').
The calltip issued is the documentation of the function you are calling. If no
documentation is found, the signature of the function is used as a call tip.
You can toggle code completion (both autocomplete and calltips) on and
off via the options menu (or by assigning a shortcut, pop up menu entry, or toolbar button).
You can also set whether or not codecompletion is enabled by default at startup via plugin preferences.
Bug reports/suggestions are welcome at the DrPython forums.'''
DrFrame.ShowMessage(helpText, "CodeCompletion Help")
def OnPreferences(DrFrame):
answer = DrFrame.Ask('Would you like CodeCompletion Enabled by Default?', 'CodeCompletion')
pfile = DrFrame.pluginspreferencesdirectory + "/CodeCompletion.preferences.dat"
f = file(pfile, 'wb')
f.write(str(int(answer)))
f.close()