Skip to content

Commit 1c2aead

Browse files
committedMar 20, 2023
Remove duplicate code
1 parent 8e52635 commit 1c2aead

File tree

1 file changed

+1
-123
lines changed

1 file changed

+1
-123
lines changed
 

‎python/console/console_editor.py

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from operator import itemgetter
3131
from pathlib import Path
3232

33-
from qgis.core import Qgis, QgsApplication, QgsBlockingNetworkRequest, QgsStringUtils, QgsSettings
33+
from qgis.core import Qgis, QgsApplication, QgsBlockingNetworkRequest, QgsSettings
3434
from qgis.gui import QgsCodeEditorPython, QgsMessageBar
3535
from qgis.PyQt.Qsci import QsciScintilla
3636
from qgis.PyQt.QtCore import QByteArray, QCoreApplication, QDir, QEvent, QFileInfo, QJsonDocument, QSize, Qt, QUrl
@@ -55,48 +55,6 @@
5555
from qgis.utils import OverrideCursor
5656

5757

58-
def findMinimalDistanceIndex(source, target):
59-
""" Find the source substring index that most closely matches the target string"""
60-
index = min(len(source), len(target))
61-
62-
distance = QgsStringUtils.levenshteinDistance
63-
64-
d0 = distance(source[:index], target)
65-
if d0 == 0:
66-
return index
67-
68-
ref_dist_more = d0
69-
ref_index_more = index
70-
if index < len(source) - 1:
71-
while True:
72-
new_dist = distance(source[:ref_index_more + 1], target)
73-
if new_dist <= ref_dist_more:
74-
ref_dist_more = new_dist
75-
ref_index_more = ref_index_more + 1
76-
if ref_index_more == len(source) - 1:
77-
break
78-
else:
79-
break
80-
81-
ref_dist_less = d0
82-
ref_index_less = index
83-
if index > 0:
84-
while True:
85-
new_dist = distance(source[:ref_index_less - 1], target)
86-
if new_dist <= ref_dist_less:
87-
ref_dist_less = new_dist
88-
ref_index_less = ref_index_less - 1
89-
if ref_index_less == 0:
90-
break
91-
else:
92-
break
93-
94-
if ref_dist_more < ref_dist_less:
95-
return ref_index_more
96-
else:
97-
return ref_index_less
98-
99-
10058
class Editor(QgsCodeEditorPython):
10159

10260
def __init__(self, parent=None):
@@ -271,86 +229,6 @@ def findNext(self):
271229
def findPrevious(self):
272230
self.findText(False)
273231

274-
def textBeforeCursor(self):
275-
return self.text(0, self.positionFromLineIndex(*self.getCursorPosition()))
276-
277-
def reformatCode(self):
278-
""" Reformat the code using the selected formatter """
279-
280-
formatter = self.settings.value("pythonConsole/formatter", "autopep8", type=str)
281-
max_line_length = self.settings.value("pythonConsole/maxLineLength", 80, type=int)
282-
283-
new_text = self.text()
284-
285-
target = self.textBeforeCursor()
286-
287-
# isort
288-
if self.settings.value("pythonConsole/sortImports", True, type=bool):
289-
try:
290-
import isort
291-
options = {
292-
"line_length": max_line_length,
293-
"profile": "black" if formatter == "black" else "",
294-
"known_first_party": ["qgis", "console", "processing", "plugins"],
295-
}
296-
new_text = isort.code(new_text, **options)
297-
298-
except ImportError:
299-
self.parent.infoBar.pushWarning(
300-
QCoreApplication.translate("PythonConsole", "Python Console"),
301-
QCoreApplication.translate("PythonConsole", "Module {0} is missing").format("isort"),
302-
)
303-
304-
# autopep8
305-
if formatter == "autopep8":
306-
try:
307-
import autopep8
308-
except ImportError:
309-
self.parent.infoBar.pushWarning(
310-
QCoreApplication.translate("PythonConsole", "Python Console"),
311-
QCoreApplication.translate("PythonConsole", "Module {0} is missing").format("autopep8"),
312-
)
313-
return
314-
level = self.settings.value("pythonConsole/autopep8Level", 1, type=int)
315-
options = {"aggressive": level, "max_line_length": max_line_length}
316-
new_text = autopep8.fix_code(new_text, options=options)
317-
318-
# black
319-
else:
320-
try:
321-
import black
322-
except ImportError:
323-
self.parent.infoBar.pushWarning(
324-
QCoreApplication.translate("PythonConsole", "Python Console"),
325-
QCoreApplication.translate("PythonConsole", "Module {0} is missing").format("black"),
326-
)
327-
return
328-
if not self.syntaxCheck():
329-
self.parent.infoBar.pushWarning(
330-
QCoreApplication.translate("PythonConsole", "Code formatting failed"),
331-
QCoreApplication.translate("PythonConsole", "The code contains syntax errors"),
332-
)
333-
return
334-
335-
normalize = self.settings.value("pythonConsole/blackNormalizeQuotes", True, type=bool)
336-
options = {"string_normalization": normalize, "line_length": max_line_length}
337-
new_text = black.format_str(new_text, mode=black.Mode(**options))
338-
339-
if new_text == self.text():
340-
return
341-
342-
# Try to preserve the cursor position and scroll position
343-
old_scroll_value = self.verticalScrollBar().value()
344-
linearPosition = findMinimalDistanceIndex(new_text, target)
345-
346-
self.beginUndoAction()
347-
self.selectAll()
348-
self.removeSelectedText()
349-
self.insert(new_text)
350-
self.setCursorPosition(*self.lineIndexFromPosition(linearPosition))
351-
self.verticalScrollBar().setValue(old_scroll_value)
352-
self.endUndoAction()
353-
354232
def objectListEditor(self):
355233
listObj = self.pythonconsole.listClassMethod
356234
if listObj.isVisible():

0 commit comments

Comments
 (0)
Please sign in to comment.