Advertisement
Guest User

simple_completer.py

a guest
Aug 4th, 2014
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.70 KB | None | 0 0
  1. from PyQt4.QtGui import QCompleter, QTextCursor
  2. from PyQt4.QtCore import Qt, SIGNAL
  3. from PyQt4.Qt import QObject
  4. from plugins_api import Plugin
  5. from functools import wraps
  6. import re
  7.  
  8. # original source(from http://ninja-ide.org/plugins/5/) modified by SHARP66
  9.  
  10. class SimpleAutocompleter(Plugin):
  11.     """Simple auto-complete plugin for kht-editor
  12.        that would suggest possible word completions
  13.        based on the text in the current document.
  14.        shortcut key: Ctrl + Space
  15.    """
  16.  
  17.     capabilities = ['afterKeyPressEvent']
  18.     __version__ = '0.1'
  19.  
  20.     # end of word
  21.     eow = "~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="
  22.  
  23.     def __init__(self):
  24.         self.editor = None
  25.         self.prefix_lenght = 3
  26.  
  27.     def do_afterKeyPressEvent(self, editor, event):
  28.         """Check for completer activation."""
  29.         is_shortcut = (event.modifiers() == Qt.ControlModifier and
  30.                        event.key() == Qt.Key_Space)
  31.         if not self.editor:
  32.             self.editor = editor
  33.             self.initialize()
  34.         completionPrefix = self.text_under_cursor()
  35.         if completionPrefix is None:
  36.             return
  37.         should_hide = not event.text() or len(completionPrefix) < self.prefix_lenght
  38.         if not is_shortcut and should_hide:
  39.             self.completer.popup().hide()
  40.             return
  41.         else:
  42.             self.update_model()
  43.  
  44.         if (completionPrefix != self.completer.completionPrefix()):
  45.             self.completer.setCompletionPrefix(completionPrefix)
  46.             popup = self.completer.popup()
  47.             popup.setCurrentIndex(
  48.                 self.completer.completionModel().index(0, 0))
  49.         cr = self.editor.cursorRect()
  50.         print cr, dir(cr)
  51.         cr.setWidth(self.completer.popup().sizeHintForColumn(0)
  52.             + self.completer.popup().verticalScrollBar().sizeHint().width())
  53.         self.completer.complete(cr)
  54.  
  55.     def initialize(self):
  56.         """plugin initializer."""
  57.         self.completer = DocumentCompleter()
  58.         self.completer.setCompletionMode(QCompleter.PopupCompletion)
  59.         self.completer.setCaseSensitivity(Qt.CaseSensitive)
  60.         self._add_completer(self.editor)
  61.         self.completer.setWidget(self.editor)
  62.  
  63.         QObject.connect(self.completer,
  64.             SIGNAL("activated(const QString&)"), self.insert_completion)
  65.  
  66.     def _add_completer(self, editor):
  67.         """Set up completer for the editor."""
  68.  
  69.         # HACK: decorator to avoid editor keypress when activating completion
  70.         def check_completer_activation(completer, function):
  71.             @wraps(function)
  72.             def _inner(event):
  73.                 if completer and completer.popup().isVisible():
  74.                     if event.key() in (
  75.                             Qt.Key_Enter,
  76.                             Qt.Key_Return,
  77.                             Qt.Key_Escape,
  78.                             Qt.Key_Tab,
  79.                             Qt.Key_Backtab):
  80.                         event.ignore()
  81.                         return
  82.                 return function(event)
  83.             return _inner
  84.  
  85.         self.editor.keyPressEvent = check_completer_activation(self.completer,
  86.                                                           self.editor.keyPressEvent)
  87.                                                          
  88.  
  89.     def update_model(self):
  90.         """Update StringList alternatives for the completer."""
  91.         data = self.editor.get_text("sof", "eof")
  92.         current = self.text_under_cursor()
  93.         words = set(re.split('\W+', data))
  94.         if current in words:
  95.             words.remove(current)
  96.         self.completer.model().setStringList(sorted(words))
  97.  
  98.     def insert_completion(self, completion):
  99.         """Insert chosen completion."""
  100.         tc = self.editor.textCursor()
  101.         extra = len(self.completer.completionPrefix())
  102.         tc.movePosition(QTextCursor.Left)
  103.         tc.movePosition(QTextCursor.EndOfWord)
  104.         tc.insertText(completion[extra:])
  105.         self.editor.setTextCursor(tc)
  106.  
  107.     def text_under_cursor(self):
  108.         """Return the word under the cursor for possible completion search."""
  109.         if self.editor is not None:
  110.             tc = self.editor.textCursor()
  111.             tc.select(QTextCursor.WordUnderCursor)
  112.             prefix = tc.selectedText()
  113.             if prefix and prefix[0] in self.eow:
  114.                 tc.movePosition(QTextCursor.WordLeft, n=2)
  115.                 tc.select(QTextCursor.WordUnderCursor)
  116.                 prefix = tc.selectedText()
  117.             return prefix
  118.         return None
  119.  
  120.  
  121. class DocumentCompleter(QCompleter):
  122.     """StringList Simple Completer."""
  123.  
  124.     def __init__(self, parent=None):
  125.         words = []
  126.         QCompleter.__init__(self, words, parent)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement