AssistantEric/Assistant.py

changeset 119
263a95431e41
parent 118
67d952a9036e
child 121
edad4b3132fd
--- a/AssistantEric/Assistant.py	Sun Mar 22 17:37:55 2015 +0100
+++ b/AssistantEric/Assistant.py	Sun May 31 18:03:01 2015 +0200
@@ -124,9 +124,8 @@
         
         # preload the api to give the manager a chance to prepare the database
         language = editor.getLanguage()
-        if language == "":
-            return
-        self.__apisManager.getAPIs(language)
+        if language:
+            self.__apisManager.getAPIs(language)
     
     def __editorClosed(self, editor):
         """
@@ -138,10 +137,20 @@
             editor.editorSaved.disconnect(
                 self.__apisManager.getAPIs(ApisNameProject).editorSaved)
             self.__editors.remove(editor)
-            if editor.autoCompletionHook() == self.autocomplete:
-                self.__unsetAutoCompletionHook(editor)
-            if editor.callTipHook() == self.calltips:
-                self.__unsetCalltipsHook(editor)
+            try:
+                if editor.getCompletionListHook("Assistant"):
+                    self.__unsetAutoCompletionHook(editor)
+            except AttributeError:
+                # old interface (before 6.1.0)
+                if editor.autoCompletionHook() == self.autocomplete:
+                    self.__unsetAutoCompletionHook(editor)
+            try:
+                if editor.getCallTipHook("Assistant"):
+                    self.__unsetCalltipsHook(editor)
+            except AttributeError:
+                # old interface (before 6.1.0)
+                if editor.callTipHook() == self.calltips:
+                    self.__unsetCalltipsHook(editor)
     
     def __preferencesChanged(self):
         """
@@ -149,31 +158,8 @@
         """
         self.__apisManager.reloadAPIs()
     
-    def __getCharacter(self, pos, editor):
-        """
-        Private method to get the character to the left of the current position
-        in the current line.
-        
-        @param pos position to get character at (integer)
-        @param editor reference to the editor object to work with
-            (QScintilla.Editor)
-        @return requested character or "", if there are no more (string) and
-            the next position (i.e. pos - 1)
-        """
-        if pos <= 0:
-            return "", pos
-        
-        pos -= 1
-        ch = editor.charAt(pos)
-        
-        # Don't go past the end of the previous line
-        if ch == '\n' or ch == '\r':
-            return "", pos
-        
-        return ch, pos
-    
     #################################
-    ## autocompletion methods below
+    ## auto-completion methods below
     #################################
     
     def __completionListSelected(self, id, txt):
@@ -210,14 +196,36 @@
             editor.insert(txt)
             editor.setCursorPosition(line, col + len(txt))
     
+    def __recordSelectedContext(self, id, txt):
+        """
+        Private slot to handle the selection from the completion list to
+        record the selected completion context.
+        
+        @param id the ID of the user list (should be 1) (integer)
+        @param txt the selected text (string)
+        """
+        from QScintilla.Editor import EditorAutoCompletionListID
+        
+        if id == EditorAutoCompletionListID:
+            lst = txt.split()
+            if len(lst) > 1:
+                self.__lastFullContext = lst[1][1:].split(")")[0]
+            else:
+                self.__lastFullContext = None
+    
     def __setAutoCompletionHook(self, editor):
         """
         Private method to set the autocompletion hook.
         
         @param editor reference to the editor (QScintilla.Editor)
         """
-        editor.userListActivated.connect(self.__completionListSelected)
-        editor.setAutoCompletionHook(self.autocomplete)
+        try:
+            editor.userListActivated.connect(self.__recordSelectedContext)
+            editor.addCompletionListHook("Assistant", self.getCompletionsList)
+        except AttributeError:
+            # old interface (before 6.1.0)
+            editor.userListActivated.connect(self.__completionListSelected)
+            editor.setAutoCompletionHook(self.autocomplete)
     
     def __unsetAutoCompletionHook(self, editor):
         """
@@ -225,8 +233,13 @@
         
         @param editor reference to the editor (QScintilla.Editor)
         """
-        editor.unsetAutoCompletionHook()
-        editor.userListActivated.disconnect(self.__completionListSelected)
+        try:
+            editor.userListActivated.disconnect(self.__recordSelectedContext)
+            editor.removeCompletionListHook("Assistant")
+        except AttributeError:
+            # old interface (before 6.1.0)
+            editor.unsetAutoCompletionHook()
+            editor.userListActivated.disconnect(self.__completionListSelected)
     
     def autocomplete(self, editor, context):
         """
@@ -697,7 +710,11 @@
         
         @param editor reference to the editor (QScintilla.Editor)
         """
-        editor.setCallTipHook(self.calltips)
+        try:
+            editor.addCallTipHook("Assistant", self.calltips)
+        except AttributeError:
+            # old interface (before 6.1.0)
+            editor.setCallTipHook(self.calltips)
     
     def __unsetCalltipsHook(self, editor):
         """
@@ -705,7 +722,11 @@
         
         @param editor reference to the editor (QScintilla.Editor)
         """
-        editor.unsetCallTipHook()
+        try:
+            editor.removeCallTipHook("Assistant")
+        except AttributeError:
+            # old interface (before 6.1.0)
+            editor.unsetCallTipHook()
     
     def calltips(self, editor, pos, commas):
         """

eric ide

mercurial