eric7/QScintilla/Editor.py

branch
eric7-maintenance
changeset 9111
4ac66b6c33a4
parent 8881
54e42bc2437a
child 9192
a763d57e23bc
diff -r 2b9bd8f97576 -r 4ac66b6c33a4 eric7/QScintilla/Editor.py
--- a/eric7/QScintilla/Editor.py	Mon May 02 15:53:05 2022 +0200
+++ b/eric7/QScintilla/Editor.py	Wed Jun 01 13:48:49 2022 +0200
@@ -522,6 +522,9 @@
         self.autosaveEnabled = Preferences.getEditor("AutosaveInterval") > 0
         self.autosaveManuallyDisabled = False
         
+        # code coverage related attributes
+        self.__coverageFile = ""
+        
         self.__initContextMenu()
         self.__initContextMenuMargins()
         
@@ -5629,39 +5632,29 @@
         ):
             fn = self.project.getMainScript(True)
             if fn is not None:
-                tfn = Utilities.getTestFileName(fn)
-                basename = os.path.splitext(fn)[0]
-                tbasename = os.path.splitext(tfn)[0]
                 prEnable = (
-                    prEnable or
-                    os.path.isfile("{0}.profile".format(basename)) or
-                    os.path.isfile("{0}.profile".format(tbasename))
+                    self.project.isPy3Project() and
+                    bool(Utilities.getProfileFileNames(fn))
                 )
                 coEnable = (
-                    (coEnable or
-                     os.path.isfile("{0}.coverage".format(basename)) or
-                     os.path.isfile("{0}.coverage".format(tbasename))) and
-                    self.project.isPy3Project()
+                    self.project.isPy3Project() and
+                    bool(Utilities.getCoverageFileNames(fn))
                 )
         
         # now check ourselves
         fn = self.getFileName()
         if fn is not None:
-            tfn = Utilities.getTestFileName(fn)
-            basename = os.path.splitext(fn)[0]
-            tbasename = os.path.splitext(tfn)[0]
-            prEnable = (
-                prEnable or
-                os.path.isfile("{0}.profile".format(basename)) or
-                os.path.isfile("{0}.profile".format(tbasename))
+            prEnable |= (
+                self.project.isPy3Project() and
+                bool(Utilities.getProfileFileName(fn))
             )
-            coEnable = (
-                (coEnable or
-                 os.path.isfile("{0}.coverage".format(basename)) or
-                 os.path.isfile("{0}.coverage".format(tbasename))) and
-                self.isPyFile()
+            coEnable |= (
+                self.project.isPy3Project() and
+                bool(Utilities.getCoverageFileName(fn))
             )
         
+        coEnable |= bool(self.__coverageFile)
+        
         # now check for syntax errors
         if self.hasSyntaxErrors():
             coEnable = False
@@ -6048,9 +6041,14 @@
         Private method to get the file name of the file containing coverage
         info.
         
-        @return file name of the coverage file (string)
-        """
-        files = []
+        @return file name of the coverage file
+        @rtype str
+        """
+        files = set()
+        
+        if bool(self.__coverageFile):
+            # return the path of a previously used coverage file
+            return self.__coverageFile
         
         # first check if the file belongs to a project and there is
         # a project coverage file
@@ -6058,36 +6056,19 @@
             self.project.isOpen() and
             self.project.isProjectSource(self.fileName)
         ):
-            fn = self.project.getMainScript(True)
-            if fn is not None:
-                tfn = Utilities.getTestFileName(fn)
-                basename = os.path.splitext(fn)[0]
-                tbasename = os.path.splitext(tfn)[0]
-                
-                f = "{0}.coverage".format(basename)
-                tf = "{0}.coverage".format(tbasename)
-                if os.path.isfile(f):
-                    files.append(f)
-                if os.path.isfile(tf):
-                    files.append(tf)
+            pfn = self.project.getMainScript(True)
+            if pfn is not None:
+                files |= set(Utilities.getCoverageFileNames(pfn))
         
         # now check, if there are coverage files belonging to ourselves
         fn = self.getFileName()
         if fn is not None:
-            tfn = Utilities.getTestFileName(fn)
-            basename = os.path.splitext(fn)[0]
-            tbasename = os.path.splitext(tfn)[0]
-            
-            f = "{0}.coverage".format(basename)
-            tf = "{0}.coverage".format(tbasename)
-            if os.path.isfile(f) and f not in files:
-                files.append(f)
-            if os.path.isfile(tf) and tf not in files:
-                files.append(tf)
-        
+            files |= set(Utilities.getCoverageFileNames(fn))
+        
+        files = list(files)
         if files:
             if len(files) > 1:
-                fn, ok = QInputDialog.getItem(
+                cfn, ok = QInputDialog.getItem(
                     self,
                     self.tr("Code Coverage"),
                     self.tr("Please select a coverage file"),
@@ -6096,17 +6077,18 @@
                 if not ok:
                     return ""
             else:
-                fn = files[0]
-        else:
-            fn = None
-        
-        return fn
+                cfn = files[0]
+        else:
+            cfn = None
+        
+        return cfn
         
     def __showCodeCoverage(self):
         """
         Private method to handle the code coverage context menu action.
         """
         fn = self.__getCodeCoverageFile()
+        self.__coverageFile = fn
         if fn:
             from DataViews.PyCoverageDialog import PyCoverageDialog
             self.codecoverage = PyCoverageDialog()
@@ -6120,16 +6102,27 @@
         if self.showingNotcoveredMarkers:
             self.codeCoverageShowAnnotations(silent=True)
         
-    def codeCoverageShowAnnotations(self, silent=False):
+    def codeCoverageShowAnnotations(self, silent=False, coverageFile=None):
         """
         Public method to handle the show code coverage annotations context
         menu action.
         
-        @param silent flag indicating to not show any dialog (boolean)
+        @param silent flag indicating to not show any dialog (defaults to
+            False)
+        @type bool (optional)
+        @param coverageFile path of the file containing the code coverage data
+            (defaults to None)
+        @type str (optional)
         """
         self.__codeCoverageHideAnnotations()
         
-        fn = self.__getCodeCoverageFile()
+        fn = (
+            coverageFile
+            if bool(coverageFile) else
+            self.__getCodeCoverageFile()
+        )
+        self.__coverageFile = fn
+        
         if fn:
             from coverage import Coverage
             cover = Coverage(data_file=fn)
@@ -6230,7 +6223,7 @@
         """
         Private method to handle the show profile data context menu action.
         """
-        files = []
+        files = set()
         
         # first check if the file belongs to a project and there is
         # a project profile file
@@ -6240,31 +6233,14 @@
         ):
             fn = self.project.getMainScript(True)
             if fn is not None:
-                tfn = Utilities.getTestFileName(fn)
-                basename = os.path.splitext(fn)[0]
-                tbasename = os.path.splitext(tfn)[0]
-                
-                f = "{0}.profile".format(basename)
-                tf = "{0}.profile".format(tbasename)
-                if os.path.isfile(f):
-                    files.append(f)
-                if os.path.isfile(tf):
-                    files.append(tf)
+                files |= set(Utilities.getProfileFileNames(fn))
         
         # now check, if there are profile files belonging to ourselves
         fn = self.getFileName()
         if fn is not None:
-            tfn = Utilities.getTestFileName(fn)
-            basename = os.path.splitext(fn)[0]
-            tbasename = os.path.splitext(tfn)[0]
-            
-            f = "{0}.profile".format(basename)
-            tf = "{0}.profile".format(tbasename)
-            if os.path.isfile(f) and f not in files:
-                files.append(f)
-            if os.path.isfile(tf) and tf not in files:
-                files.append(tf)
-        
+            files |= set(Utilities.getProfileFileNames(fn))
+        
+        files = list(files)
         if files:
             if len(files) > 1:
                 fn, ok = QInputDialog.getItem(

eric ide

mercurial