src/eric7/CodeFormatting/BlackFormattingDialog.py

branch
eric7
changeset 9344
52990830b13f
parent 9292
a5c8a0213fe3
child 9348
f61d71d95cb1
equal deleted inserted replaced
9343:7180fb8677e5 9344:52990830b13f
72 """ 72 """
73 super().__init__(parent) 73 super().__init__(parent)
74 self.setupUi(self) 74 self.setupUi(self)
75 75
76 self.progressBar.setMaximum(len(filesList)) 76 self.progressBar.setMaximum(len(filesList))
77 self.progressBar.setValue(0)
78 77
79 self.resultsList.header().setSortIndicator(1, Qt.SortOrder.AscendingOrder) 78 self.resultsList.header().setSortIndicator(1, Qt.SortOrder.AscendingOrder)
80
81 self.statisticsGroup.setVisible(False)
82
83 self.__statistics = BlackStatistics()
84 79
85 self.__config = copy.deepcopy(configuration) 80 self.__config = copy.deepcopy(configuration)
86 self.__config["__action__"] = action # needed by the workers 81 self.__config["__action__"] = action # needed by the workers
87 self.__project = project 82 self.__project = project
88 self.__action = action 83
84 self.__filesList = filesList[:]
85
86 self.__diffDialog = None
87
88 self.__allFilter = self.tr("<all>")
89
90 self.__formatButton = self.buttonBox.addButton(
91 self.tr("Format Code"), QDialogButtonBox.ButtonRole.ActionRole
92 )
93 self.__formatButton.setVisible(False)
94
95 self.show()
96 QCoreApplication.processEvents()
97
98 self.__performAction()
99
100 def __performAction(self):
101 """
102 Private method to exceute the requested formatting action.
103 """
104 self.progressBar.setValue(0)
105
106 self.statisticsGroup.setVisible(False)
107 self.__statistics = BlackStatistics()
89 108
90 self.__cancelled = False 109 self.__cancelled = False
91 self.__diffDialog = None 110
92 111 self.statusFilterComboBox.clear()
93 self.__allFilter = self.tr("<all>") 112 self.resultsList.clear()
94 113
95 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(True) 114 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(True)
96 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(False) 115 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(False)
97 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setDefault(True) 116 self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setDefault(True)
98 117
99 self.show() 118 files = self.__filterFiles(self.__filesList)
100 QCoreApplication.processEvents()
101
102 files = self.__filterFiles(filesList)
103 if len(files) > 1: 119 if len(files) > 1:
104 self.__formatManyFiles(files) 120 self.__formatManyFiles(files)
105 elif len(files) == 1: 121 elif len(files) == 1:
106 self.__formatOneFile(files[0]) 122 self.__formatOneFile(files[0])
107 123
180 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(True) 196 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(True)
181 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault(True) 197 self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault(True)
182 198
183 self.progressBar.setVisible(False) 199 self.progressBar.setVisible(False)
184 200
201 self.__formatButton.setVisible(
202 self.__config["__action__"] is not BlackFormattingAction.Format
203 and self.__statistics.changeCount > 0
204 )
205
185 self.__updateStatistics() 206 self.__updateStatistics()
186 self.__populateStatusFilterCombo() 207 self.__populateStatusFilterCombo()
187 208
188 def __updateStatistics(self): 209 def __updateStatistics(self):
189 """ 210 """
190 Private method to update the statistics about the recent formatting run and 211 Private method to update the statistics about the recent formatting run and
191 make them visible. 212 make them visible.
192 """ 213 """
193 self.reformattedLabel.setText( 214 self.reformattedLabel.setText(
194 self.tr("Reformatted:") 215 self.tr("Reformatted:")
195 if self.__action is BlackFormattingAction.Format 216 if self.__config["__action__"] is BlackFormattingAction.Format
196 else self.tr("Would Reformat:") 217 else self.tr("Would Reformat:")
197 ) 218 )
198 219
199 total = self.progressBar.maximum() 220 total = self.progressBar.maximum()
200 221
221 """ 242 """
222 if button == self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel): 243 if button == self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel):
223 self.__cancelled = True 244 self.__cancelled = True
224 elif button == self.buttonBox.button(QDialogButtonBox.StandardButton.Close): 245 elif button == self.buttonBox.button(QDialogButtonBox.StandardButton.Close):
225 self.accept() 246 self.accept()
247 elif button is self.__formatButton:
248 self.__formatButtonClicked()
249
250 @pyqtSlot()
251 def __formatButtonClicked(self):
252 """
253 Private slot handling the selection of the 'Format Code' button.
254 """
255 self.__config["__action__"] = BlackFormattingAction.Format
256
257 self.__performAction()
226 258
227 @pyqtSlot(QTreeWidgetItem, int) 259 @pyqtSlot(QTreeWidgetItem, int)
228 def on_resultsList_itemDoubleClicked(self, item, column): 260 def on_resultsList_itemDoubleClicked(self, item, column):
229 """ 261 """
230 Private slot handling a double click of a result item. 262 Private slot handling a double click of a result item.
311 # Tell child processes to stop 343 # Tell child processes to stop
312 for _ in range(NumberOfProcesses): 344 for _ in range(NumberOfProcesses):
313 taskQueue.put("STOP") 345 taskQueue.put("STOP")
314 346
315 for worker in workers: 347 for worker in workers:
316 worker.join() 348 worker.join(2) # 2 seconds timeout just in case
349 # TODO: monitor this change
350 if worker.exitcode is None:
351 worker.terminate()
317 worker.close() 352 worker.close()
318 353
319 taskQueue.close() 354 taskQueue.close()
320 doneQueue.close() 355 ## doneQueue.close()
356 # TODO: monitor this change
321 357
322 self.__finish() 358 self.__finish()
323 359
324 @staticmethod 360 @staticmethod
325 def formattingWorkerTask(inputQueue, outputQueue, config): 361 def formattingWorkerTask(inputQueue, outputQueue, config):
405 line_length=int(self.__config["line-length"]), 441 line_length=int(self.__config["line-length"]),
406 string_normalization=not self.__config["skip-string-normalization"], 442 string_normalization=not self.__config["skip-string-normalization"],
407 magic_trailing_comma=not self.__config["skip-magic-trailing-comma"], 443 magic_trailing_comma=not self.__config["skip-magic-trailing-comma"],
408 ) 444 )
409 445
410 if self.__action is BlackFormattingAction.Diff: 446 if self.__config["__action__"] is BlackFormattingAction.Diff:
411 relSrc = self.__project.getRelativePath(str(file)) if self.__project else "" 447 relSrc = self.__project.getRelativePath(str(file)) if self.__project else ""
412 self.__diffFormatFile( 448 self.__diffFormatFile(
413 pathlib.Path(file), fast=False, mode=mode, report=report, relSrc=relSrc 449 pathlib.Path(file), fast=False, mode=mode, report=report, relSrc=relSrc
414 ) 450 )
415 else: 451 else:
499 isError = False 535 isError = False
500 536
501 if status == "changed": 537 if status == "changed":
502 statusMsg = ( 538 statusMsg = (
503 self.tr("would reformat") 539 self.tr("would reformat")
504 if self.__action 540 if self.__config["__action__"]
505 in (BlackFormattingAction.Check, BlackFormattingAction.Diff) 541 in (BlackFormattingAction.Check, BlackFormattingAction.Diff)
506 else self.tr("reformatted") 542 else self.tr("reformatted")
507 ) 543 )
508 self.__statistics.changeCount += 1 544 self.__statistics.changeCount += 1
509 545

eric ide

mercurial