Started implementing project type specific APIs. APIs

Wed, 30 Nov 2016 19:59:57 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 30 Nov 2016 19:59:57 +0100
branch
APIs
changeset 5349
bccda3b5920a
parent 5348
96197e13fdf3
child 5350
57e82ffafdfc

Started implementing project type specific APIs.

Preferences/ConfigurationPages/EditorAPIsPage.py file | annotate | diff | comparison | revisions
Preferences/ConfigurationPages/EditorAPIsPage.ui file | annotate | diff | comparison | revisions
Preferences/__init__.py file | annotate | diff | comparison | revisions
QScintilla/APIsManager.py file | annotate | diff | comparison | revisions
QScintilla/Editor.py file | annotate | diff | comparison | revisions
--- a/Preferences/ConfigurationPages/EditorAPIsPage.py	Wed Nov 30 18:54:04 2016 +0100
+++ b/Preferences/ConfigurationPages/EditorAPIsPage.py	Wed Nov 30 19:59:57 2016 +0100
@@ -23,6 +23,8 @@
 import Utilities
 
 
+# TODO: add combo box to select project type
+#       getEditorAPI und setEditorAPI
 class EditorAPIsPage(ConfigurationPageBase, Ui_EditorAPIsPage):
     """
     Class implementing the Editor APIs configuration page.
@@ -57,12 +59,37 @@
         for lang in apiLanguages:
             if lang != "Guessed":
                 self.apiLanguageComboBox.addItem(lang)
-        self.currentApiLanguage = ''
-        self.on_apiLanguageComboBox_activated(self.currentApiLanguage)
+        apiProjectTypes = sorted(
+            [("", "")] + [(trans, ptype) for ptype, trans in 
+                e5App().getObject("Project").getProjectTypes().items()]
+        )
+        for projectTypeStr, projectType in apiProjectTypes:
+            self.projectTypeComboBox.addItem(projectTypeStr, projectType)
+        self.__currentApiLanguage = ""
+        self.__currentApiProjectType = ""
+        self.on_apiLanguageComboBox_activated(self.__currentApiLanguage)
+##        self.on_projectTypeComboBox_activated(self.__currentApiProjectType)
+##        
+##        for lang in apiLanguages[1:]:
+##            self.apis[lang] = Preferences.getEditorAPI(lang)[:]
+    
+    def __apiKey(self, language, projectType):
+        """
+        Private method to generate a key for the apis dictionary.
         
-        for lang in apiLanguages[1:]:
-            self.apis[lang] = Preferences.getEditorAPI(lang)[:]
-        
+        @param language programming language of the API
+        @type str
+        @param projectType project type of the API
+        @type str
+        @return key to be used
+        @rtype str
+        """
+        if projectType:
+            key = (language, projectType)
+        else:
+            key = (language, "")
+        return key
+    
     def save(self):
         """
         Public slot to save the Editor APIs configuration.
@@ -72,10 +99,23 @@
             self.apiAutoPrepareCheckBox.isChecked())
         
         lang = self.apiLanguageComboBox.currentText()
-        self.apis[lang] = self.__editorGetApisFromApiList()
+        self.apis[self.__apiKey()] = self.__editorGetApisFromApiList()
         
-        for lang, apis in list(self.apis.items()):
-            Preferences.setEditorAPI(lang, apis)
+        for (language, projectType), apis in self.apis.items():
+            Preferences.setEditorAPI(lang, projectType, apis)
+    
+    # TODO: carry on from here
+    @pyqtSlot(str)
+    def on_projectTypeComboBox_activated(self, p0):
+        """
+        Slot documentation goes here.
+        
+        @param p0 DESCRIPTION
+        @type str
+        """
+        return
+        # TODO: not implemented yet
+        raise NotImplementedError
         
     @pyqtSlot(str)
     def on_apiLanguageComboBox_activated(self, language):
@@ -84,11 +124,11 @@
         
         @param language selected API language (string)
         """
-        if self.currentApiLanguage == language:
+        if self.__currentApiLanguage == language:
             return
             
-        self.apis[self.currentApiLanguage] = self.__editorGetApisFromApiList()
-        self.currentApiLanguage = language
+        self.apis[self.__currentApiLanguage] = self.__editorGetApisFromApiList()
+        self.__currentApiLanguage = language
         self.apiList.clear()
         
         if not language:
@@ -100,13 +140,14 @@
         self.addApiFileButton.setEnabled(False)
         self.apiFilePicker.clear()
         
-        for api in self.apis[self.currentApiLanguage]:
+        for api in self.apis[self.__currentApiLanguage]:
             if api:
                 self.apiList.addItem(api)
         self.prepareApiButton.setEnabled(self.apiList.count() > 0)
         
+        # TODO: add project type
         from QScintilla.APIsManager import APIsManager
-        self.__currentAPI = APIsManager().getAPIs(self.currentApiLanguage)
+        self.__currentAPI = APIsManager().getAPIs(self.__currentApiLanguage)
         if self.__currentAPI is not None:
             self.__currentAPI.apiPreparationFinished.connect(
                 self.__apiPreparationFinished)
@@ -121,7 +162,7 @@
         
         self.addPluginApiFileButton.setEnabled(
             len(self.pluginManager.getPluginApiFiles(
-                self.currentApiLanguage)) > 0)
+                self.__currentApiLanguage)) > 0)
         
     def __editorGetApisFromApiList(self):
         """
@@ -195,7 +236,7 @@
         by plugins for the selected lexer language.
         """
         pluginAPIFiles = self.pluginManager.getPluginApiFiles(
-            self.currentApiLanguage)
+            self.__currentApiLanguage)
         pluginAPIFilesDict = {}
         for apiFile in pluginAPIFiles:
             pluginAPIFilesDict[QFileInfo(apiFile).fileName()] = apiFile
--- a/Preferences/ConfigurationPages/EditorAPIsPage.ui	Wed Nov 30 18:54:04 2016 +0100
+++ b/Preferences/ConfigurationPages/EditorAPIsPage.ui	Wed Nov 30 19:59:57 2016 +0100
@@ -7,10 +7,10 @@
     <x>0</x>
     <y>0</y>
     <width>462</width>
-    <height>422</height>
+    <height>539</height>
    </rect>
   </property>
-  <layout class="QVBoxLayout">
+  <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QLabel" name="headerLabel">
      <property name="text">
@@ -42,8 +42,8 @@
     </widget>
    </item>
    <item>
-    <layout class="QHBoxLayout">
-     <item>
+    <layout class="QGridLayout" name="gridLayout_2">
+     <item row="0" column="0">
       <widget class="QLabel" name="TextLabel1_3_3">
        <property name="toolTip">
         <string/>
@@ -56,7 +56,7 @@
        </property>
       </widget>
      </item>
-     <item>
+     <item row="0" column="1">
       <widget class="QComboBox" name="apiLanguageComboBox">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@@ -69,6 +69,20 @@
        </property>
       </widget>
      </item>
+     <item row="1" column="0">
+      <widget class="QLabel" name="label">
+       <property name="text">
+        <string>Project Type:</string>
+       </property>
+      </widget>
+     </item>
+     <item row="1" column="1">
+      <widget class="QComboBox" name="projectTypeComboBox">
+       <property name="toolTip">
+        <string>Select the project type to be configured</string>
+       </property>
+      </widget>
+     </item>
     </layout>
    </item>
    <item>
@@ -189,6 +203,7 @@
  <tabstops>
   <tabstop>apiAutoPrepareCheckBox</tabstop>
   <tabstop>apiLanguageComboBox</tabstop>
+  <tabstop>projectTypeComboBox</tabstop>
   <tabstop>apiList</tabstop>
   <tabstop>deleteApiFileButton</tabstop>
   <tabstop>addApiFileButton</tabstop>
--- a/Preferences/__init__.py	Wed Nov 30 18:54:04 2016 +0100
+++ b/Preferences/__init__.py	Wed Nov 30 19:59:57 2016 +0100
@@ -2120,14 +2120,23 @@
     prefClass.settings.setValue("Editor/Other Fonts/" + key, font.toString())
     
 
-def getEditorAPI(key, prefClass=Prefs):
+def getEditorAPI(language, projectType="", prefClass=Prefs):
     """
-    Module function to retrieve the various lists of api files.
+    Module function to retrieve the various lists of API files.
     
-    @param key the key of the value to get
+    @param language language of the API list
+    @type str
+    @param projectType project type of the API list
+    @type str
     @param prefClass preferences class used as the storage area
-    @return the requested list of api files (list of strings)
+    @type Prefs
+    @return requested list of API files
+    @rtype list of str
     """
+    if projectType:
+        key = "{0}_{1}".format(language, projectType)
+    else:
+        key = language
     apis = prefClass.settings.value("Editor/APIs/" + key)
     if apis is not None:
         if len(apis) and apis[0] == "":
@@ -2135,17 +2144,30 @@
         else:
             return apis
     else:
+        if projectType:
+            # try again without project type
+            return getEditorAPI(language, prefClass=prefClass)
+        
         return []
     
 
-def setEditorAPI(key, apilist, prefClass=Prefs):
+def setEditorAPI(language, projectType, apilist, prefClass=Prefs):
     """
-    Module function to store the various lists of api files.
+    Module function to store the various lists of API files.
     
-    @param key the key of the api to be set
-    @param apilist the list of api files (list of strings)
+    @param language language of the API list
+    @type str
+    @param projectType project type of the API list
+    @type str
+    @param apilist list of API files
+    @type list of str
     @param prefClass preferences class used as the storage area
+    @type Prefs
     """
+    if projectType:
+        key = "{0}_{1}".format(language, projectType)
+    else:
+        key = language
     prefClass.settings.setValue("Editor/APIs/" + key, apilist)
     
 
--- a/QScintilla/APIsManager.py	Wed Nov 30 18:54:04 2016 +0100
+++ b/QScintilla/APIsManager.py	Wed Nov 30 19:59:57 2016 +0100
@@ -34,23 +34,34 @@
     apiPreparationCancelled = pyqtSignal()
     apiPreparationStarted = pyqtSignal()
     
-    def __init__(self, language, forPreparation=False, parent=None):
+    def __init__(self, language, projectType="", forPreparation=False,
+                 parent=None):
         """
         Constructor
         
-        @param language language of the APIs object (string)
+        @param language language of the APIs object
+        @type str
+        @param projectType type of the project
+        @type str
         @param forPreparation flag indicating this object is just needed
-            for a preparation process (boolean)
-        @param parent reference to the parent object (QObject)
+            for a preparation process
+        @type bool
+        @param parent reference to the parent object
+        @type QObject
         """
         super(APIs, self).__init__(parent)
-        self.setObjectName("APIs_{0}".format(language))
+        if projectType:
+            self.setObjectName("APIs_{0}_{1}".format(language, projectType))
+        else:
+            self.setObjectName("APIs_{0}".format(language))
         
         self.__inPreparation = False
         self.__language = language
+        self.__projectType = projectType
         self.__forPreparation = forPreparation
         self.__lexer = Lexers.getLexer(self.__language)
-        self.__apifiles = Preferences.getEditorAPI(self.__language)
+        self.__apifiles = Preferences.getEditorAPI(self.__language,
+                                                   self.__projectType)
         self.__apifiles.sort()
         if self.__lexer is None:
             self.__apis = None
@@ -73,7 +84,7 @@
             if not self.__forPreparation and \
                     Preferences.getEditor("AutoPrepareAPIs"):
                 self.prepareAPIs()
-            self.__apis.loadPrepared()
+            self.__apis.loadPrepared(self.__preparedName())
         else:
             # load the raw files and prepare the API file
             if not self.__forPreparation and \
@@ -112,7 +123,7 @@
         """
         Private method called to save an API, after it has been prepared.
         """
-        self.__apis.savePrepared()
+        self.__apis.savePrepared(self.__preparedName())
         self.__inPreparation = False
         self.apiPreparationFinished.emit()
     
@@ -146,15 +157,15 @@
             needsPreparation = True
         else:
             # check, if a new preparation is necessary
-            preparedAPIs = self.__defaultPreparedName()
+            preparedAPIs = self.__preparedName()
             if preparedAPIs:
                 preparedAPIsInfo = QFileInfo(preparedAPIs)
                 if not preparedAPIsInfo.exists():
                     needsPreparation = True
                 else:
                     preparedAPIsTime = preparedAPIsInfo.lastModified()
-                    apifiles = sorted(
-                        Preferences.getEditorAPI(self.__language))
+                    apifiles = sorted(Preferences.getEditorAPI(
+                        self.__language, self.__projectType))
                     if self.__apifiles != apifiles:
                         needsPreparation = True
                     for apifile in apifiles:
@@ -169,7 +180,8 @@
             if rawList:
                 apifiles = rawList
             else:
-                apifiles = Preferences.getEditorAPI(self.__language)
+                apifiles = Preferences.getEditorAPI(
+                    self.__language, self.__projectType)
             for apifile in apifiles:
                 self.__apis.load(apifile)
             self.__apis.prepare()
@@ -209,14 +221,20 @@
         else:
             return []
     
-    def __defaultPreparedName(self):
+    def __preparedName(self):
         """
         Private method returning the default name of a prepared API file.
         
         @return complete filename for the Prepared APIs file (string)
         """
+        apisDir = os.path.join(Globals.getConfigDir(), "APIs")
         if self.__apis is not None:
-            return self.__apis.defaultPreparedName()
+            if self.__projectType:
+                filename = "{0}_{1}.pap".format(self.__language,
+                                                self.__projectType)
+            else:
+                filename = "{0}.pap".format(self.__language)
+            return os.path.join(apisDir, filename)
         else:
             return ""
     
@@ -244,28 +262,35 @@
         for api in list(self.__apis.values()):
             api and api.reloadAPIs()
     
-    def getAPIs(self, language, forPreparation=False):
+    def getAPIs(self, language, projectType="", forPreparation=False):
         """
-        Public method to get an apis object for autocompletion/calltips.
+        Public method to get an APIs object for autocompletion/calltips.
         
         This method creates and loads an APIs object dynamically upon request.
         This saves memory for languages, that might not be needed at the
         moment.
         
-        @param language the language of the requested api object (string)
-        @param forPreparation flag indicating the requested api object is just
-            needed for a preparation process (boolean)
-        @return the apis object (APIs)
+        @param language language of the requested APIs object
+        @type str
+        @param projectType type of the project
+        @type str
+        @param forPreparation flag indicating the requested APIs object is just
+            needed for a preparation process
+        @type bool
+        @return reference to the APIs object
+        @rtype APIs
         """
         if forPreparation:
-            return APIs(language, forPreparation=forPreparation)
+            return APIs(language, projectType=projectType,
+                        forPreparation=forPreparation)
         else:
             try:
-                return self.__apis[language]
+                return self.__apis[(language, projectType)]
             except KeyError:
                 if language in Lexers.getSupportedLanguages():
                     # create the api object
-                    self.__apis[language] = APIs(language)
-                    return self.__apis[language]
+                    self.__apis[(language, projectType)] = \
+                        APIs(language, projectType=projectType)
+                    return self.__apis[(language, projectType)]
                 else:
                     return None
--- a/QScintilla/Editor.py	Wed Nov 30 18:54:04 2016 +0100
+++ b/QScintilla/Editor.py	Wed Nov 30 19:59:57 2016 +0100
@@ -1578,7 +1578,12 @@
         self.lexer_.initProperties()
         
         # initialize the lexer APIs settings
-        api = self.vm.getAPIsManager().getAPIs(self.apiLanguage)
+        if self.project.isOpen() and self.project.isProjectFile(filename):
+            projectType = self.project.getProjectType()
+        else:
+            projectType = ""
+        api = self.vm.getAPIsManager().getAPIs(self.apiLanguage,
+                                               projectType=projectType)
         if api is not None and not api.isEmpty():
             self.lexer_.setAPIs(api.getQsciAPIs())
             self.acAPI = True

eric ide

mercurial