QScintilla/Editor.py

changeset 5888
f23f3d2b7516
parent 5887
9a6ce5faed7a
child 5890
22ec89341f5e
diff -r 9a6ce5faed7a -r f23f3d2b7516 QScintilla/Editor.py
--- a/QScintilla/Editor.py	Tue Oct 03 16:21:33 2017 +0200
+++ b/QScintilla/Editor.py	Tue Oct 03 19:37:44 2017 +0200
@@ -27,6 +27,7 @@
 
 from E5Gui.E5Application import e5App
 from E5Gui import E5FileDialog, E5MessageBox
+from E5Utilities.E5Cache import E5Cache
 
 from .QsciScintillaCompat import QsciScintillaCompat, QSCINTILLA_VERSION
 from .EditorMarkerMap import EditorMarkerMap
@@ -384,6 +385,8 @@
         self.__acContext = True
         self.__acText = ""
         self.__acCompletions = set()
+        self.__acCache = E5Cache(
+            size=Preferences.getEditor("AutoCompletionCacheSize"))
         self.__acTimer = QTimer(self)
         self.__acTimer.setSingleShot(True)
         self.__acTimer.setInterval(
@@ -831,6 +834,9 @@
         self.menuActs["acDynamic"] = menu.addAction(
             self.tr('Complete'), self.autoComplete)
         menu.addSeparator()
+        self.menuActs["acClearCache"] = menu.addAction(
+            self.tr("Clear Completions Cache"), self.__clearCompletionsCache)
+        menu.addSeparator()
         menu.addAction(
             self.tr('Complete from Document'), self.autoCompleteFromDocument)
         self.menuActs["acAPI"] = menu.addAction(
@@ -4043,8 +4049,15 @@
         # set margin 0 and 2 configuration
         self.__setMarginsDisplay()
         
-        # set the autocompletion and calltips function
+        # set the auto-completion function
+        self.__acCache.setSize(
+            Preferences.getEditor("AutoCompletionCacheSize"))
+        acTimeout = Preferences.getEditor("AutoCompletionTimeout")
+        if acTimeout != self.__acTimer.interval:
+            self.__acTimer.setInterval(acTimeout)
         self.__setAutoCompletion()
+        
+        # set the calltips function
         self.__setCallTips()
         
         # set the autosave flags
@@ -4634,22 +4647,37 @@
         @keyparam context flag indicating to complete a context
         @type bool or None
         """
-        # TODO: add a cache for recent completions
-        if context is None:
-            context = self.__acContext
-        
         line, col = self.getCursorPosition()
-        self.__acText = self.getWordLeft(line, col)
+        text = self.text(line)
+        if self.__isStartChar(text[col - 1]):
+            self.__acText = self.getWordLeft(line, col - 1) + text[col - 1]
+        else:
+            self.__acText = self.getWordLeft(line, col)
         self.__acCompletions.clear()
         
-        for key in self.__completionListAsyncHookFunctions:
-            self.__completionListAsyncHookFunctions[key](
-                self, context, self.__acText)
-        
-        for key in self.__completionListHookFunctions:
-            completions = self.__completionListHookFunctions[key](
-                self, context)
-            self.completionsListReady(completions, self.__acText)
+        completions = self.__acCache.get(self.__acText)
+        if completions is not None:
+            # show list with cached entries
+            if self.isListActive():
+                self.cancelList()
+            
+            self.showUserList(
+                EditorAutoCompletionListID,
+                sorted(list(completions),
+                       reverse=Preferences.getEditor(
+                    "AutoCompletionReversedList")))
+        else:
+            if context is None:
+                context = self.__acContext
+            
+            for key in self.__completionListAsyncHookFunctions:
+                self.__completionListAsyncHookFunctions[key](
+                    self, context, self.__acText)
+            
+            for key in self.__completionListHookFunctions:
+                completions = self.__completionListHookFunctions[key](
+                    self, context)
+                self.completionsListReady(completions, self.__acText)
     
     def completionsListReady(self, completions, acText):
         """
@@ -4657,23 +4685,31 @@
         provider.
         
         @param completions list of possible completions
-        @type list of str
+        @type list of str or set of str
         @param acText text to be completed
         @type str
         """
-        if acText == self.__acText:
-            # process the list only, if not already obsolete
+        if acText == self.__acText and bool(completions):
+            # process the list only, if not already obsolete or completions
+            # are not empty
             if self.isListActive():
                 self.cancelList()
             
             self.__acCompletions.update(set(completions))
             if self.__acCompletions:
+                self.__acCache.add(acText, set(self.__acCompletions))
                 self.showUserList(
                     EditorAutoCompletionListID,
                     sorted(list(self.__acCompletions),
                            reverse=Preferences.getEditor(
                         "AutoCompletionReversedList")))
     
+    def __clearCompletionsCache(self):
+        """
+        Private method to clear the auto-completions cache.
+        """
+        self.__acCache.clear()
+    
     def __completionListSelected(self, listId, txt):
         """
         Private slot to handle the selection from the completion list.
@@ -5033,6 +5069,8 @@
         """
         self.menuActs["acDynamic"].setEnabled(
             self.canProvideDynamicAutoCompletion())
+        self.menuActs["acClearCache"].setEnabled(
+            self.canProvideDynamicAutoCompletion())
         self.menuActs["acAPI"].setEnabled(self.acAPI)
         self.menuActs["acAPIDocument"].setEnabled(self.acAPI)
         self.menuActs["calltip"].setEnabled(self.canProvideCallTipps())

eric ide

mercurial