Skip to content

Commit

Permalink
Add black formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ authored and nyalldawson committed Mar 17, 2023
1 parent 4065413 commit 63a4898
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 21 deletions.
34 changes: 27 additions & 7 deletions python/console/console_editor.py
Expand Up @@ -31,6 +31,7 @@
from pathlib import Path

import autopep8
import black

from qgis.core import Qgis, QgsApplication, QgsBlockingNetworkRequest, QgsFileUtils, QgsSettings
from qgis.gui import QgsCodeEditorPython, QgsMessageBar
Expand All @@ -54,7 +55,7 @@
QTreeWidgetItem,
QWidget,
)
from qgis.utils import OverrideCursor
from qgis.utils import OverrideCursor, iface


class Editor(QgsCodeEditorPython):
Expand Down Expand Up @@ -232,12 +233,31 @@ def findPrevious(self):
self.findText(False)

def formatCode(self):
""" Format the code using autopep8 """
options = {
"aggressive": self.settings.value("pythonConsole/autopep8Aggressiveness", 1, type=int),
"max_line_length": self.settings.value("pythonConsole/maxLineLength", 80, type=int),
}
new_text = autopep8.fix_code(self.text(), options=options)
""" Format the code using the selected formatter """

formatter = self.settings.value("pythonConsole/formatter", "autopep8", type=str)
max_line_length = self.settings.value("pythonConsole/maxLineLength", 80, type=int)
# autopep8
if formatter == "autopep8":
aggressive = self.settings.value("pythonConsole/autopep8Aggressiveness", 1, type=int)
options = {"aggressive": aggressive, "max_line_length": max_line_length}
new_text = autopep8.fix_code(self.text(), options=options)

# black
else:
if not self.syntaxCheck():
iface.messageBar().pushMessage(
QCoreApplication.translate("PythonConsole", "Code formatting failed"),
QCoreApplication.translate("PythonConsole", "The code contains syntax errors"),
level=Qgis.Warning,
duration=2,
)
return

normalize = self.settings.value("pythonConsole/blackNormalizeQuotes", True, type=bool)
options = {"string_normalization": normalize, "line_length": max_line_length}
new_text = black.format_str(self.text(), mode=black.Mode(**options))

if new_text == self.text():
return

Expand Down
17 changes: 17 additions & 0 deletions python/console/console_settings.py
Expand Up @@ -90,6 +90,8 @@ def __init__(self, parent):
self.compileAPIs.clicked.connect(self._prepareAPI)

self.generateToken.clicked.connect(self.generateGHToken)
self.formatter.currentTextChanged.connect(self.onFormatterChanged)
self.onFormatterChanged()

def generateGHToken(self):
description = self.tr("PyQGIS Console")
Expand Down Expand Up @@ -207,7 +209,9 @@ def saveSettings(self):
settings.setValue("pythonConsole/autoInsertImport", self.autoInsertImport.isChecked())

settings.setValue("pythonConsole/formatOnSave", self.formatOnSave.isChecked())
settings.setValue("pythonConsole/formatter", self.formatter.currentText())
settings.setValue("pythonConsole/autopep8Aggressiveness", self.autopep8Aggressiveness.value())
settings.setValue("pythonConsole/blackNormalizeQuotes", self.blackNormalizeQuotes.isChecked())
settings.setValue("pythonConsole/maxLineLength", self.maxLineLength.value())

def restoreSettings(self):
Expand Down Expand Up @@ -236,7 +240,9 @@ def restoreSettings(self):
self.autoInsertImport.setChecked(settings.value("pythonConsole/autoInsertImport", False, type=bool))

self.formatOnSave.setChecked(settings.value("pythonConsole/formatOnSave", False, type=bool))
self.formatter.setCurrentText(settings.value("pythonConsole/formatter", "autopep8", type=str))
self.autopep8Aggressiveness.setValue(settings.value("pythonConsole/autopep8Aggressiveness", 1, type=int))
self.blackNormalizeQuotes.setChecked(settings.value("pythonConsole/blackNormalizeQuotes", True, type=bool))
self.maxLineLength.setValue(settings.value("pythonConsole/maxLineLength", 80, type=int))

if settings.value("pythonConsole/autoCompleteSource") == 'fromDoc':
Expand All @@ -245,3 +251,14 @@ def restoreSettings(self):
self.autoCompFromAPI.setChecked(True)
elif settings.value("pythonConsole/autoCompleteSource") == 'fromDocAPI':
self.autoCompFromDocAPI.setChecked(True)

def onFormatterChanged(self):
""" Toggle formatter-specific options visibility when the formatter is changed """
if self.formatter.currentText() == 'autopep8':
self.autopep8Aggressiveness.setVisible(True)
self.autopep8AggressivenessLabel.setVisible(True)
self.blackNormalizeQuotes.setVisible(False)
else: # black
self.autopep8Aggressiveness.setVisible(False)
self.autopep8AggressivenessLabel.setVisible(False)
self.blackNormalizeQuotes.setVisible(True)
63 changes: 49 additions & 14 deletions python/console/console_settings.ui
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>1055</width>
<width>888</width>
<height>731</height>
</rect>
</property>
Expand Down Expand Up @@ -54,8 +54,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>1041</width>
<height>854</height>
<width>874</width>
<height>914</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
Expand Down Expand Up @@ -456,20 +456,34 @@
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="2" column="0">
<widget class="QLabel" name="label_8">
<widget class="QLabel" name="label">
<property name="text">
<string>Maximum line length</string>
<string>Formatter</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_7">
<item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="blackNormalizeQuotes">
<property name="text">
<string>Normalize quotes</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="formatOnSave">
<property name="text">
<string>Format on save</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="autopep8AggressivenessLabel">
<property name="text">
<string>Autopep8 agressiveness</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="3" column="1">
<widget class="QSpinBox" name="autopep8Aggressiveness">
<property name="maximum">
<number>3</number>
Expand All @@ -479,14 +493,34 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="formatOnSave">
<item row="2" column="1">
<widget class="QComboBox" name="formatter">
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<item>
<property name="text">
<string>autopep8</string>
</property>
</item>
<item>
<property name="text">
<string>black</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Format on save</string>
<string>Maximum line length</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="1" column="1">
<widget class="QSpinBox" name="maxLineLength">
<property name="minimum">
<number>60</number>
Expand All @@ -499,7 +533,7 @@
</property>
</widget>
</item>
<item row="1" column="2">
<item row="0" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand Down Expand Up @@ -540,8 +574,9 @@
<tabstop>autoSurround</tabstop>
<tabstop>autoInsertImport</tabstop>
<tabstop>formatOnSave</tabstop>
<tabstop>formatter</tabstop>
<tabstop>autopep8Aggressiveness</tabstop>
<tabstop>maxLineLength</tabstop>
<tabstop>blackNormalizeQuotes</tabstop>
<tabstop>enableObjectInspector</tabstop>
<tabstop>autoSaveScript</tabstop>
<tabstop>preloadAPI</tabstop>
Expand Down

0 comments on commit 63a4898

Please sign in to comment.