CodeStyleCheckerDialog: added configuration entry for commented code whitelist patterns.

Wed, 13 Jan 2021 20:13:26 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 13 Jan 2021 20:13:26 +0100
changeset 7978
1e391f977124
parent 7977
f0d18add14ea
child 7979
54b73174ab61

CodeStyleCheckerDialog: added configuration entry for commented code whitelist patterns.

eric6/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.py file | annotate | diff | comparison | revisions
eric6/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.ui file | annotate | diff | comparison | revisions
eric6/Plugins/CheckerPlugins/CodeStyleChecker/eradicate.py file | annotate | diff | comparison | revisions
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.py	Wed Jan 13 19:12:35 2021 +0100
+++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.py	Wed Jan 13 20:13:26 2021 +0100
@@ -14,7 +14,7 @@
 from PyQt5.QtGui import QIcon
 from PyQt5.QtWidgets import (
     QDialog, QTreeWidgetItem, QAbstractButton, QDialogButtonBox, QApplication,
-    QHeaderView, QListWidgetItem
+    QHeaderView, QListWidgetItem, QInputDialog, QLineEdit
 )
 
 from E5Gui.E5Application import e5App
@@ -463,10 +463,29 @@
                 "str": ["unicode", ],
                 "chr": ["unichr", ],
             }
+        
         if "CommentedCodeChecker" not in self.__data:
             self.__data["CommentedCodeChecker"] = {
                 "Aggressive": False,
             }
+        if "WhiteList" not in self.__data["CommentedCodeChecker"]:
+            self.__data["CommentedCodeChecker"]["WhiteList"] = [
+                r'pylint',
+                r'pyright',
+                r'noqa',
+                r'type:\s*ignore',
+                r'fmt:\s*(on|off)',
+                r'TODO',
+                r'FIXME',
+                r'WARNING',
+                r'NOTE',
+                r'TEST',
+                r'DOCU',
+                r'XXX',
+                r'~ ',
+                r'- ',
+            ]
+        
         if "AnnotationsChecker" not in self.__data:
             self.__data["AnnotationsChecker"] = {
                 "MinimumCoverage": 75,
@@ -526,6 +545,8 @@
         self.__initBuiltinsIgnoreList(self.__data["BuiltinsChecker"])
         self.aggressiveCheckBox.setChecked(
             self.__data["CommentedCodeChecker"]["Aggressive"])
+        self.__initCommentedCodeCheckerWhiteList(
+            self.__data["CommentedCodeChecker"]["WhiteList"])
         self.minAnnotationsCoverageSpinBox.setValue(
             self.__data["AnnotationsChecker"]["MinimumCoverage"])
         self.maxAnnotationsComplexitySpinBox.setValue(
@@ -677,6 +698,7 @@
                 "BuiltinsChecker": self.__getBuiltinsIgnoreList(),
                 "CommentedCodeChecker": {
                     "Aggressive": self.aggressiveCheckBox.isChecked(),
+                    "WhiteList": self.__getCommentedCodeCheckerWhiteList(),
                 }
             }
             annotationArgs = {
@@ -1082,6 +1104,7 @@
                 "BuiltinsChecker": self.__getBuiltinsIgnoreList(),
                 "CommentedCodeChecker": {
                     "Aggressive": self.aggressiveCheckBox.isChecked(),
+                    "WhiteList": self.__getCommentedCodeCheckerWhiteList(),
                 },
                 "AnnotationsChecker": {
                     "MinimumCoverage":
@@ -1882,3 +1905,63 @@
         
         self.excludeMessagesEdit.setText(",".join(excludedMessages))
         self.__initCategoriesList(",".join(enabledCheckers))
+    
+    def __initCommentedCodeCheckerWhiteList(self, whitelist):
+        """
+        Private method to populate the list of commented code whitelist
+        patterns.
+        
+        @param whitelist list of commented code whitelist patterns
+        @type list of str
+        """
+        self.whitelistWidget.clear()
+        
+        for pattern in whitelist:
+            QListWidgetItem(pattern, self.whitelistWidget)
+        
+        self.on_whitelistWidget_itemSelectionChanged()
+    
+    def __getCommentedCodeCheckerWhiteList(self):
+        """
+        Private method to get the list of commented code whitelist patterns.
+        
+        @return list of commented code whitelist patterns
+        @rtype list of str
+        """
+        whitelist = []
+        
+        for row in range(self.whitelistWidget.count()):
+            whitelist.append(self.whitelistWidget.item(row).text())
+        
+        return whitelist
+    
+    @pyqtSlot()
+    def on_whitelistWidget_itemSelectionChanged(self):
+        """
+        Private slot to react upon changes of the selected whitelist patterns.
+        """
+        self.deleteWhitelistButton.setEnabled(
+            len(self.whitelistWidget.selectedItems()) > 0)
+    
+    @pyqtSlot()
+    def on_addWhitelistButton_clicked(self):
+        """
+        Private slot to add a commented code whitelist pattern.
+        """
+        pattern, ok = QInputDialog.getText(
+            self,
+            self.tr("Commented Code Whitelist Pattern"),
+            self.tr("Enter a Commented Code Whitelist Pattern"),
+            QLineEdit.Normal)
+        if ok and pattern:
+            QListWidgetItem(pattern, self.whitelistWidget)
+    
+    @pyqtSlot()
+    def on_deleteWhitelistButton_clicked(self):
+        """
+        Private slot to delete the selected items from the list.
+        """
+        for itm in self.whitelistWidget.selectedItems():
+            index = self.whitelistWidget.indexOfTopLevelItem(itm)
+            self.whitelistWidget.takeTopLevelItem(index)
+            del itm
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.ui	Wed Jan 13 19:12:35 2021 +0100
+++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.ui	Wed Jan 13 20:13:26 2021 +0100
@@ -233,18 +233,6 @@
            <string>Specific Options</string>
           </attribute>
           <layout class="QVBoxLayout" name="verticalLayout_4">
-           <property name="leftMargin">
-            <number>1</number>
-           </property>
-           <property name="topMargin">
-            <number>1</number>
-           </property>
-           <property name="rightMargin">
-            <number>1</number>
-           </property>
-           <property name="bottomMargin">
-            <number>1</number>
-           </property>
            <item>
             <widget class="QScrollArea" name="scrollArea">
              <property name="frameShape">
@@ -263,12 +251,12 @@
               <property name="geometry">
                <rect>
                 <x>0</x>
-                <y>0</y>
-                <width>637</width>
-                <height>884</height>
+                <y>-628</y>
+                <width>611</width>
+                <height>1178</height>
                </rect>
               </property>
-              <layout class="QVBoxLayout" name="verticalLayout_6">
+              <layout class="QVBoxLayout" name="verticalLayout_3">
                <item>
                 <widget class="QGroupBox" name="groupBox">
                  <property name="title">
@@ -652,8 +640,8 @@
                  <property name="title">
                   <string>Commented Code</string>
                  </property>
-                 <layout class="QVBoxLayout" name="verticalLayout_3">
-                  <item>
+                 <layout class="QGridLayout" name="gridLayout_8">
+                  <item row="0" column="0" colspan="3">
                    <widget class="QCheckBox" name="aggressiveCheckBox">
                     <property name="toolTip">
                      <string>Select to search for commented code more aggressively. This may increase the number of false positives.</string>
@@ -663,6 +651,56 @@
                     </property>
                    </widget>
                   </item>
+                  <item row="1" column="0">
+                   <widget class="QLabel" name="label_30">
+                    <property name="text">
+                     <string>Whitelist:</string>
+                    </property>
+                    <property name="alignment">
+                     <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
+                    </property>
+                   </widget>
+                  </item>
+                  <item row="1" column="1">
+                   <widget class="QListWidget" name="whitelistWidget"/>
+                  </item>
+                  <item row="1" column="2">
+                   <layout class="QVBoxLayout" name="verticalLayout_6">
+                    <item alignment="Qt::AlignHCenter">
+                     <widget class="QToolButton" name="addWhitelistButton">
+                      <property name="toolTip">
+                       <string>Press to add a commented code whitelist pattern</string>
+                      </property>
+                      <property name="text">
+                       <string notr="true">+</string>
+                      </property>
+                     </widget>
+                    </item>
+                    <item alignment="Qt::AlignHCenter">
+                     <widget class="QToolButton" name="deleteWhitelistButton">
+                      <property name="toolTip">
+                       <string>Press to delete the selected entries</string>
+                      </property>
+                      <property name="text">
+                       <string notr="true">-</string>
+                      </property>
+                     </widget>
+                    </item>
+                    <item>
+                     <spacer name="verticalSpacer_6">
+                      <property name="orientation">
+                       <enum>Qt::Vertical</enum>
+                      </property>
+                      <property name="sizeHint" stdset="0">
+                       <size>
+                        <width>20</width>
+                        <height>40</height>
+                       </size>
+                      </property>
+                     </spacer>
+                    </item>
+                   </layout>
+                  </item>
                  </layout>
                 </widget>
                </item>
@@ -1430,6 +1468,9 @@
   <tabstop>addBuiltinButton</tabstop>
   <tabstop>deleteBuiltinButton</tabstop>
   <tabstop>aggressiveCheckBox</tabstop>
+  <tabstop>whitelistWidget</tabstop>
+  <tabstop>addWhitelistButton</tabstop>
+  <tabstop>deleteWhitelistButton</tabstop>
   <tabstop>complexitySpinBox</tabstop>
   <tabstop>lineComplexitySpinBox</tabstop>
   <tabstop>lineComplexityScoreSpinBox</tabstop>
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/eradicate.py	Wed Jan 13 19:12:35 2021 +0100
+++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/eradicate.py	Wed Jan 13 20:13:26 2021 +0100
@@ -60,8 +60,6 @@
         r'TODO',
         r'FIXME',
         r'XXX',
-        r'~ ',
-        r'- ',
     )
     WHITELIST_REGEX = re.compile(r'|'.join(DEFAULT_WHITELIST), flags=re.IGNORECASE)
 

eric ide

mercurial