AssistantEric/Assistant.py

changeset 131
7d868e8e1cfb
parent 122
d746710bfe2e
child 132
eb12cd27384f
diff -r 08c7aece376c -r 7d868e8e1cfb AssistantEric/Assistant.py
--- a/AssistantEric/Assistant.py	Sat Nov 26 14:43:28 2016 +0100
+++ b/AssistantEric/Assistant.py	Sat Dec 03 18:08:44 2016 +0100
@@ -123,9 +123,14 @@
         self.__editors.append(editor)
         
         # preload the api to give the manager a chance to prepare the database
-        language = editor.getLanguage()
+        try:
+            language = editor.getApiLanguage()
+        except AttributeError:
+            # backward compatibility
+            language = editor.apiLanguage
         if language:
-            self.__apisManager.getAPIs(language)
+            projectType = self.__getProjectType(editor)
+            self.__apisManager.getAPIs(language, projectType=projectType)
     
     def __editorClosed(self, editor):
         """
@@ -158,6 +163,24 @@
         """
         self.__apisManager.reloadAPIs()
     
+    def __getProjectType(self, editor):
+        """
+        Private method to determine the project type to be used.
+        
+        @param editor reference to the editor to check
+        @type Editor
+        @return project type
+        @rtype str
+        """
+        filename = editor.getFileName()
+        if self.__project.isOpen() and filename and \
+                self.__project.isProjectFile(filename):
+            projectType = self.__project.getProjectType()
+        else:
+            projectType = ""
+        
+        return projectType
+    
     #################################
     ## auto-completion methods below
     #################################
@@ -269,9 +292,21 @@
         @param context flag indicating to autocomplete a context (boolean)
         @return list of possible completions (list of strings)
         """
-        language = editor.getLanguage()
-        if language == "":
-            return []
+        try:
+            language = editor.getApiLanguage()
+        except AttributeError:
+            # backward compatibility
+            language = editor.apiLanguage
+        
+        completeFromDocumentOnly = False
+        if language in ["", "Guessed"] or language.startswith("Pygments|"):
+            if self.__plugin.getPreferences("AutoCompletionSource") & \
+                    AcsDocument:
+                completeFromDocumentOnly = True
+            else:
+                return []
+        
+        projectType = self.__getProjectType(editor)
         
         line, col = editor.getCursorPosition()
         self.__completingContext = context
@@ -374,19 +409,19 @@
         
         if word or importCompletion:
             completionsList = self.__getCompletions(
-                word, context, prefix, language, mod, editor,
-                importCompletion, sep)
+                word, context, prefix, language, projectType, mod, editor,
+                importCompletion, completeFromDocumentOnly, sep)
             if len(completionsList) == 0 and prefix:
                 # searching with prefix didn't return anything, try without
                 completionsList = self.__getCompletions(
-                    word, context, "", language, mod, editor, importCompletion,
-                    sep)
+                    word, context, "", language, projectType, mod, editor,
+                    importCompletion, completeFromDocumentOnly, sep)
             return completionsList
         
         return []
-
-    def __getCompletions(self, word, context, prefix, language, module, editor,
-                         importCompletion, sep):
+    
+    def __getCompletions(self, word, context, prefix, language, projectType,
+                         module, editor, importCompletion, documentOnly, sep):
         """
         Private method to get the list of possible completions.
         
@@ -394,9 +429,12 @@
         @param context flag indicating to autocomplete a context (boolean)
         @param prefix prefix of the word to be completed (string)
         @param language programming language of the source (string)
+        @param projectType type of the project (string)
         @param module reference to the scanned module info (Module)
         @param editor reference to the editor object (QScintilla.Editor.Editor)
         @param importCompletion flag indicating an import completion (boolean)
+        @param documentOnly flag indicating to complete from the document only
+            (boolean)
         @param sep separator string (string)
         @return list of possible completions (list of strings)
         """
@@ -404,15 +442,18 @@
         docCompletionsList = []
         projectCompletionList = []
         
-        if self.__plugin.getPreferences("AutoCompletionSource") & AcsAPIs:
-            api = self.__apisManager.getAPIs(language)
-            apiCompletionsList = self.__getApiCompletions(
-                api, word, context, prefix, module, editor)
-        
-        if self.__plugin.getPreferences("AutoCompletionSource") & AcsProject:
-            api = self.__apisManager.getAPIs(ApisNameProject)
-            projectCompletionList = self.__getApiCompletions(
-                api, word, context, prefix, module, editor)
+        if not documentOnly:
+            if self.__plugin.getPreferences("AutoCompletionSource") & AcsAPIs:
+                api = self.__apisManager.getAPIs(
+                    language, projectType=projectType)
+                apiCompletionsList = self.__getApiCompletions(
+                    api, word, context, prefix, module, editor)
+            
+            if self.__plugin.getPreferences("AutoCompletionSource") & \
+                    AcsProject:
+                api = self.__apisManager.getAPIs(ApisNameProject)
+                projectCompletionList = self.__getApiCompletions(
+                    api, word, context, prefix, module, editor)
         
         if self.__plugin.getPreferences("AutoCompletionSource") & AcsDocument \
                 and not importCompletion:
@@ -738,9 +779,21 @@
             (integer)
         @return list of possible calltips (list of strings)
         """
-        language = editor.getLanguage()
-        if language == "":
-            return
+        try:
+            language = editor.getApiLanguage()
+        except AttributeError:
+            # backward compatibility
+            language = editor.apiLanguage
+        
+        completeFromDocumentOnly = False
+        if language in ["", "Guessed"] or language.startswith("Pygments|"):
+            if self.__plugin.getPreferences("AutoCompletionSource") & \
+                    AcsDocument:
+                completeFromDocumentOnly = True
+            else:
+                return []
+        
+        projectType = self.__getProjectType(editor)
         
         line, col = editor.lineIndexFromPosition(pos)
         wc = re.sub("\w", "", editor.wordCharacters())
@@ -778,17 +831,20 @@
         projectCalltips = []
         documentCalltips = []
         
-        if self.__plugin.getPreferences("AutoCompletionSource") & AcsAPIs:
-            api = self.__apisManager.getAPIs(language)
-            if api is not None:
-                apiCalltips = self.__getApiCalltips(
+        if not completeFromDocumentOnly:
+            if self.__plugin.getPreferences("AutoCompletionSource") & AcsAPIs:
+                api = self.__apisManager.getAPIs(
+                    language, projectType=projectType)
+                if api is not None:
+                    apiCalltips = self.__getApiCalltips(
+                        api, word, commas, prefix, mod, editor)
+            
+            if self.__plugin.getPreferences("AutoCompletionSource") & \
+                    AcsProject:
+                api = self.__apisManager.getAPIs(ApisNameProject)
+                projectCalltips = self.__getApiCalltips(
                     api, word, commas, prefix, mod, editor)
         
-        if self.__plugin.getPreferences("AutoCompletionSource") & AcsProject:
-            api = self.__apisManager.getAPIs(ApisNameProject)
-            projectCalltips = self.__getApiCalltips(
-                api, word, commas, prefix, mod, editor)
-        
         if self.__plugin.getPreferences("AutoCompletionSource") & AcsDocument:
             documentCalltips = self.__getDocumentCalltips(
                 word, prefix, mod, editor)

eric ide

mercurial