[wingide-users] Autocomplete python more liberally

Wingware Support support at wingware.com
Tue Nov 25 20:12:28 MST 2008


Peter Bengtsson wrote:
> I'm realizing more and more how great Wing is but I'm also realizing
> how much I miss the autocomplete.
> How about a custom plugin script that I can bind to a keyboard command
> that takes as a parameter the word the cursor is at and the whole
> buffer as a string?
> I'm happy to get my hands dirty if I can get some assistance.

A primitive word list driven completer is possible using the scripting
API but it's not possible to integrate that well with the regular
auto-completer.

I've appended below a sample script that uses the argument entry
facility at bottom of the editor window to provide simple word-list
driven completion based on the current file contents.

If you put this into the 'scripts' folder in your user settings
directory (which varies in location and is listed 5th in Wing's
About box) and do Reload All Scripts from the Edit menu, then
you can bind a key binding to the command word-list-completion.
It shows a completer once you start typing.

This is just a toy, really, but I thought it may be useful and/or
interesting.

-- 

Stephan Deibel
Wingware | Python IDE
Advancing Software Development

www.wingware.com

====================================

# Example of using arginfo to provide word list driven autocompletion
# via a script

import string
import wingapi

def word_list_completion(word):
   ed = wingapi.gApplication.GetActiveEditor()
   start, end = ed.GetSelection()
   doc = wingapi.gApplication.GetActiveDocument()
   doc.DeleteChars(start, end-1)
   doc.InsertChars(start, word)
   ed.SetSelection(start + len(word), start + len(word))

def _ArgInfo_word_list_completion():
   from wingutils import datatype
   from guiutils import formbuilder
   from command import commandmgr

   doc = wingapi.gApplication.GetActiveDocument()
   txt = doc.GetText()

   class CWordMap:
     def __init__(self, chars):
       self.chars = set(chars)
     def __getitem__(self, char):
       if unichr(char) in self.chars:
         return char
       else:
         return u' '

   trans = CWordMap(unicode(string.letters + string.digits + '_'))
   txt = txt.translate(trans)

   words = txt.split()
   choices = set()
   for word in words:
     choices.add(word)

   arginfo = {
     'word': commandmgr.CArgInfo(
       "Word", datatype.CType(''),
       formbuilder.CSmallTextGui(choices=list(choices)),
       "Word:", 'internal'
     )
   }
   return arginfo

word_list_completion.arginfo = _ArgInfo_word_list_completion



More information about the wingide-users mailing list