All: Reformat according to new standard

This commit is contained in:
pre-commit-ci[bot]
2025-11-11 13:49:01 +01:00
committed by Kacper Donat
parent eafd18dac0
commit 25c3ba7338
2390 changed files with 154630 additions and 115818 deletions

View File

@@ -21,12 +21,12 @@
***************************************************************************/
# include <QContextMenuEvent>
# include <QMenu>
# include <QPainter>
# include <QRegularExpression>
# include <QShortcut>
# include <QTextCursor>
#include <QContextMenuEvent>
#include <QMenu>
#include <QPainter>
#include <QRegularExpression>
#include <QShortcut>
#include <QTextCursor>
#include <Base/Parameter.h>
@@ -41,23 +41,24 @@
using namespace Gui;
namespace Gui {
namespace Gui
{
struct PythonEditorP
{
int debugLine{-1};
int debugLine {-1};
QRect debugRect;
QPixmap breakpoint;
QPixmap debugMarker;
QString filename;
PythonDebugger* debugger;
PythonEditorP()
: breakpoint(BitmapFactory().iconFromTheme("breakpoint").pixmap(16,16)),
debugMarker(BitmapFactory().iconFromTheme("debug-marker").pixmap(16,16))
: breakpoint(BitmapFactory().iconFromTheme("breakpoint").pixmap(16, 16))
, debugMarker(BitmapFactory().iconFromTheme("debug-marker").pixmap(16, 16))
{
debugger = Application::Instance->macroManager()->debugger();
}
};
} // namespace Gui
} // namespace Gui
/* TRANSLATOR Gui::PythonEditor */
@@ -92,13 +93,12 @@ PythonEditor::~PythonEditor()
delete d;
}
void PythonEditor::OnChange(Base::Subject<const char*> &rCaller, const char* sReason)
void PythonEditor::OnChange(Base::Subject<const char*>& rCaller, const char* sReason)
{
const auto & rGrp = static_cast<ParameterGrp &>(rCaller);
const auto& rGrp = static_cast<ParameterGrp&>(rCaller);
if (strcmp(sReason, "EnableBlockCursor") == 0 ||
strcmp(sReason, "FontSize") == 0 ||
strcmp(sReason, "Font") == 0) {
if (strcmp(sReason, "EnableBlockCursor") == 0 || strcmp(sReason, "FontSize") == 0
|| strcmp(sReason, "Font") == 0) {
bool block = rGrp.GetBool("EnableBlockCursor", false);
if (block) {
setCursorWidth(QFontMetrics(font()).averageCharWidth());
@@ -140,12 +140,14 @@ void PythonEditor::showDebugMarker(int line)
cursor.movePosition(QTextCursor::StartOfBlock);
int cur = cursor.blockNumber() + 1;
if (cur > line) {
for (int i=line; i<cur; i++)
for (int i = line; i < cur; i++) {
cursor.movePosition(QTextCursor::Up);
}
}
else if (cur < line) {
for (int i=cur; i<line; i++)
for (int i = cur; i < line; i++) {
cursor.movePosition(QTextCursor::Down);
}
}
setTextCursor(cursor);
}
@@ -163,22 +165,22 @@ void PythonEditor::drawMarker(int line, int x, int y, QPainter* p)
p->drawPixmap(x, y, d->breakpoint);
}
if (d->debugLine == line) {
p->drawPixmap(x, y+2, d->debugMarker);
d->debugRect = QRect(x, y+2, d->debugMarker.width(), d->debugMarker.height());
p->drawPixmap(x, y + 2, d->debugMarker);
d->debugRect = QRect(x, y + 2, d->debugMarker.width(), d->debugMarker.height());
}
}
void PythonEditor::contextMenuEvent ( QContextMenuEvent * e )
void PythonEditor::contextMenuEvent(QContextMenuEvent* e)
{
QMenu* menu = createStandardContextMenu();
if (!isReadOnly()) {
menu->addSeparator();
QAction* comment = menu->addAction( tr("Comment"), this, &PythonEditor::onComment);
QAction* comment = menu->addAction(tr("Comment"), this, &PythonEditor::onComment);
comment->setShortcut(QKeySequence(QStringLiteral("ALT+C")));
QAction* uncomment = menu->addAction( tr("Uncomment"), this, &PythonEditor::onUncomment);
QAction* uncomment = menu->addAction(tr("Uncomment"), this, &PythonEditor::onUncomment);
uncomment->setShortcut(QKeySequence(QStringLiteral("ALT+U")));
QAction* execInConsole = menu->addAction( tr("Execute in Console"),
this, &PythonEditor::onExecuteInConsole);
QAction* execInConsole
= menu->addAction(tr("Execute in Console"), this, &PythonEditor::onExecuteInConsole);
execInConsole->setShortcut(QKeySequence(QStringLiteral("ALT+Shift+P")));
}
@@ -196,43 +198,44 @@ void PythonEditor::keyPressEvent(QKeyEvent* e)
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
bool shiftPressed = e->modifiers() & Qt::ShiftModifier;
ParameterGrp::handle hPrefGrp = getWindowParameter();
int indent = hPrefGrp->GetInt( "IndentSize", 4 );
bool space = hPrefGrp->GetBool( "Spaces", true );
QString ch = space ? QStringLiteral(" ")
: QStringLiteral("\t");
int indent = hPrefGrp->GetInt("IndentSize", 4);
bool space = hPrefGrp->GetBool("Spaces", true);
QString ch = space ? QStringLiteral(" ") : QStringLiteral("\t");
QTextCursor cursor = textCursor();
QString currentLineText = cursor.block().text();
bool endsWithColon = currentLineText.endsWith(QLatin1Char(':'));
int currentIndentation = 0;
//count spaces/tabs at start of current line
// count spaces/tabs at start of current line
for (auto c : currentLineText) {
if (c == ch) {
currentIndentation++;
} else {
}
else {
break;
}
}
cursor.insertBlock(); //new line
cursor.movePosition(QTextCursor::StartOfBlock); //carriage return
//Shift+Enter means dedent, but ensure we are not at column 0
if (shiftPressed && currentIndentation >= indent){
cursor.insertBlock(); // new line
cursor.movePosition(QTextCursor::StartOfBlock); // carriage return
// Shift+Enter means dedent, but ensure we are not at column 0
if (shiftPressed && currentIndentation >= indent) {
currentIndentation -= indent;
}
//insert appropriate number of spaces/tabs to match current indentation
// insert appropriate number of spaces/tabs to match current indentation
cursor.insertText(QString(currentIndentation, ch[0]));
//if the line ended in a colon, then we need to add another tab or multiple spaces
// if the line ended in a colon, then we need to add another tab or multiple spaces
if (endsWithColon) {
if (space){
cursor.insertText(QString(indent, ch[0])); //4 more spaces by default
} else {
cursor.insertText(ch); //1 more tab
if (space) {
cursor.insertText(QString(indent, ch[0])); // 4 more spaces by default
}
else {
cursor.insertText(ch); // 1 more tab
}
}
setTextCursor(cursor);
return; //skip default handler
return; // skip default handler
}
PythonTextEditor::keyPressEvent(e); //wasn't enter key, so let base class handle it
PythonTextEditor::keyPressEvent(e); // wasn't enter key, so let base class handle it
}
void PythonEditor::onComment()
@@ -299,10 +302,15 @@ void PythonEditor::onExecuteInConsole()
if (!dedentedCode.isEmpty()) {
try {
Gui::Command::doCommand(Gui::Command::Doc, dedentedCode.toStdString().c_str());
} catch (const Base::Exception& e) {
}
catch (const Base::Exception& e) {
QString errorMessage = QString::fromStdString(e.what());
Base::Console().error("Error executing Python code:\n%s\n", errorMessage.toUtf8().constData());
} catch (...) {
Base::Console().error(
"Error executing Python code:\n%s\n",
errorMessage.toUtf8().constData()
);
}
catch (...) {
Base::Console().error("An unknown error occurred while executing Python code.\n");
}
}
@@ -311,35 +319,30 @@ void PythonEditor::onExecuteInConsole()
// ------------------------------------------------------------------------
namespace Gui {
namespace Gui
{
class PythonSyntaxHighlighterP
{
public:
PythonSyntaxHighlighterP()
{
keywords << QLatin1String("and") << QLatin1String("as")
<< QLatin1String("assert") << QLatin1String("break")
<< QLatin1String("class") << QLatin1String("continue")
<< QLatin1String("def") << QLatin1String("del")
<< QLatin1String("elif") << QLatin1String("else")
<< QLatin1String("except") << QLatin1String("exec")
<< QLatin1String("False") << QLatin1String("finally")
<< QLatin1String("for") << QLatin1String("from")
<< QLatin1String("global") << QLatin1String("if")
<< QLatin1String("import") << QLatin1String("in")
<< QLatin1String("is") << QLatin1String("lambda")
<< QLatin1String("None") << QLatin1String("nonlocal")
<< QLatin1String("not") << QLatin1String("or")
<< QLatin1String("pass") << QLatin1String("print")
<< QLatin1String("raise") << QLatin1String("return")
<< QLatin1String("True") << QLatin1String("try")
<< QLatin1String("while") << QLatin1String("with")
<< QLatin1String("yield");
keywords << QLatin1String("and") << QLatin1String("as") << QLatin1String("assert")
<< QLatin1String("break") << QLatin1String("class") << QLatin1String("continue")
<< QLatin1String("def") << QLatin1String("del") << QLatin1String("elif")
<< QLatin1String("else") << QLatin1String("except") << QLatin1String("exec")
<< QLatin1String("False") << QLatin1String("finally") << QLatin1String("for")
<< QLatin1String("from") << QLatin1String("global") << QLatin1String("if")
<< QLatin1String("import") << QLatin1String("in") << QLatin1String("is")
<< QLatin1String("lambda") << QLatin1String("None") << QLatin1String("nonlocal")
<< QLatin1String("not") << QLatin1String("or") << QLatin1String("pass")
<< QLatin1String("print") << QLatin1String("raise") << QLatin1String("return")
<< QLatin1String("True") << QLatin1String("try") << QLatin1String("while")
<< QLatin1String("with") << QLatin1String("yield");
}
QStringList keywords;
};
} // namespace Gui
} // namespace Gui
/**
* Constructs a Python syntax highlighter.
@@ -359,216 +362,209 @@ PythonSyntaxHighlighter::~PythonSyntaxHighlighter()
/**
* Detects all kinds of text to highlight them in the correct color.
*/
void PythonSyntaxHighlighter::highlightBlock (const QString & text)
void PythonSyntaxHighlighter::highlightBlock(const QString& text)
{
int i = 0;
QChar prev, ch;
int i = 0;
QChar prev, ch;
const int Standard = 0; // Standard text
const int Digit = 1; // Digits
const int Comment = 2; // Comment begins with #
const int Literal1 = 3; // String literal beginning with "
const int Literal2 = 4; // Other string literal beginning with '
const int Blockcomment1 = 5; // Block comments beginning and ending with """
const int Blockcomment2 = 6; // Other block comments beginning and ending with '''
const int ClassName = 7; // Text after the keyword class
const int DefineName = 8; // Text after the keyword def
const int Standard = 0; // Standard text
const int Digit = 1; // Digits
const int Comment = 2; // Comment begins with #
const int Literal1 = 3; // String literal beginning with "
const int Literal2 = 4; // Other string literal beginning with '
const int Blockcomment1 = 5; // Block comments beginning and ending with """
const int Blockcomment2 = 6; // Other block comments beginning and ending with '''
const int ClassName = 7; // Text after the keyword class
const int DefineName = 8; // Text after the keyword def
int endStateOfLastPara = previousBlockState();
if (endStateOfLastPara < 0 || endStateOfLastPara > maximumUserState())
endStateOfLastPara = Standard;
while ( i < text.length() )
{
ch = text.at( i );
switch ( endStateOfLastPara )
{
case Standard:
{
switch ( ch.unicode() )
{
case '#':
{
// begin a comment
setFormat( i, 1, this->colorByType(SyntaxHighlighter::Comment));
endStateOfLastPara=Comment;
} break;
case '"':
{
// Begin either string literal or block comment
if ((i>=2) && text.at(i-1) == QLatin1Char('"') &&
text.at(i-2) == QLatin1Char('"'))
{
setFormat( i-2, 3, this->colorByType(SyntaxHighlighter::BlockComment));
endStateOfLastPara=Blockcomment1;
}
else
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::String));
endStateOfLastPara=Literal1;
}
} break;
case '\'':
{
// Begin either string literal or block comment
if ((i>=2) && text.at(i-1) == QLatin1Char('\'') &&
text.at(i-2) == QLatin1Char('\''))
{
setFormat( i-2, 3, this->colorByType(SyntaxHighlighter::BlockComment));
endStateOfLastPara=Blockcomment2;
}
else
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::String));
endStateOfLastPara=Literal2;
}
} break;
case ' ':
case '\t':
{
// ignore whitespaces
} break;
case '(': case ')': case '[': case ']':
case '+': case '-': case '*': case '/':
case ':': case '%': case '^': case '~':
case '!': case '=': case '<': case '>': // possibly two characters
{
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Operator));
endStateOfLastPara=Standard;
} break;
default:
{
// Check for normal text
if ( ch.isLetter() || ch == QLatin1Char('_') )
{
QString buffer;
int j=i;
while ( ch.isLetterOrNumber() || ch == QLatin1Char('_') ) {
buffer += ch;
++j;
if (j >= text.length())
break; // end of text
ch = text.at(j);
}
if ( d->keywords.contains( buffer ) != 0 ) {
if ( buffer == QLatin1String("def"))
endStateOfLastPara = DefineName;
else if ( buffer == QLatin1String("class"))
endStateOfLastPara = ClassName;
QTextCharFormat keywordFormat;
keywordFormat.setForeground(this->colorByType(SyntaxHighlighter::Keyword));
keywordFormat.setFontWeight(QFont::Bold);
setFormat( i, buffer.length(), keywordFormat);
}
else {
setFormat( i, buffer.length(),this->colorByType(SyntaxHighlighter::Text));
}
// increment i
if ( !buffer.isEmpty() )
i = j-1;
}
// this is the beginning of a number
else if ( ch.isDigit() )
{
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Number));
endStateOfLastPara=Digit;
}
// probably an operator
else if ( ch.isSymbol() || ch.isPunct() )
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::Operator));
}
}
}
} break;
case Comment:
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::Comment));
} break;
case Literal1:
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::String));
if ( ch == QLatin1Char('"') )
endStateOfLastPara = Standard;
} break;
case Literal2:
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::String));
if ( ch == QLatin1Char('\'') )
endStateOfLastPara = Standard;
} break;
case Blockcomment1:
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::BlockComment));
if ( i>=2 && ch == QLatin1Char('"') &&
text.at(i-1) == QLatin1Char('"') &&
text.at(i-2) == QLatin1Char('"'))
endStateOfLastPara = Standard;
} break;
case Blockcomment2:
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::BlockComment));
if ( i>=2 && ch == QLatin1Char('\'') &&
text.at(i-1) == QLatin1Char('\'') &&
text.at(i-2) == QLatin1Char('\''))
endStateOfLastPara = Standard;
} break;
case DefineName:
{
if ( ch.isLetterOrNumber() || ch == QLatin1Char(' ') || ch == QLatin1Char('_') )
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::Defname));
}
else
{
if ( ch.isSymbol() || ch.isPunct() )
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Operator));
endStateOfLastPara = Standard;
}
} break;
case ClassName:
{
if ( ch.isLetterOrNumber() || ch == QLatin1Char(' ') || ch == QLatin1Char('_') )
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::Classname));
}
else
{
if (ch.isSymbol() || ch.isPunct() )
setFormat( i, 1, this->colorByType(SyntaxHighlighter::Operator));
endStateOfLastPara = Standard;
}
} break;
case Digit:
{
if (ch.isDigit() || ch == QLatin1Char('.'))
{
setFormat( i, 1, this->colorByType(SyntaxHighlighter::Number));
}
else
{
if ( ch.isSymbol() || ch.isPunct() )
setFormat( i, 1, this->colorByType(SyntaxHighlighter::Operator));
endStateOfLastPara = Standard;
}
}break;
int endStateOfLastPara = previousBlockState();
if (endStateOfLastPara < 0 || endStateOfLastPara > maximumUserState()) {
endStateOfLastPara = Standard;
}
prev = ch;
i++;
}
while (i < text.length()) {
ch = text.at(i);
// only block comments can have several lines
if ( endStateOfLastPara != Blockcomment1 && endStateOfLastPara != Blockcomment2 )
{
endStateOfLastPara = Standard ;
}
switch (endStateOfLastPara) {
case Standard: {
switch (ch.unicode()) {
case '#': {
// begin a comment
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Comment));
endStateOfLastPara = Comment;
} break;
case '"': {
// Begin either string literal or block comment
if ((i >= 2) && text.at(i - 1) == QLatin1Char('"')
&& text.at(i - 2) == QLatin1Char('"')) {
setFormat(i - 2, 3, this->colorByType(SyntaxHighlighter::BlockComment));
endStateOfLastPara = Blockcomment1;
}
else {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::String));
endStateOfLastPara = Literal1;
}
} break;
case '\'': {
// Begin either string literal or block comment
if ((i >= 2) && text.at(i - 1) == QLatin1Char('\'')
&& text.at(i - 2) == QLatin1Char('\'')) {
setFormat(i - 2, 3, this->colorByType(SyntaxHighlighter::BlockComment));
endStateOfLastPara = Blockcomment2;
}
else {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::String));
endStateOfLastPara = Literal2;
}
} break;
case ' ':
case '\t': {
// ignore whitespaces
} break;
case '(':
case ')':
case '[':
case ']':
case '+':
case '-':
case '*':
case '/':
case ':':
case '%':
case '^':
case '~':
case '!':
case '=':
case '<':
case '>': // possibly two characters
{
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Operator));
endStateOfLastPara = Standard;
} break;
default: {
// Check for normal text
if (ch.isLetter() || ch == QLatin1Char('_')) {
QString buffer;
int j = i;
while (ch.isLetterOrNumber() || ch == QLatin1Char('_')) {
buffer += ch;
++j;
if (j >= text.length()) {
break; // end of text
}
ch = text.at(j);
}
setCurrentBlockState(endStateOfLastPara);
if (d->keywords.contains(buffer) != 0) {
if (buffer == QLatin1String("def")) {
endStateOfLastPara = DefineName;
}
else if (buffer == QLatin1String("class")) {
endStateOfLastPara = ClassName;
}
QTextCharFormat keywordFormat;
keywordFormat.setForeground(
this->colorByType(SyntaxHighlighter::Keyword)
);
keywordFormat.setFontWeight(QFont::Bold);
setFormat(i, buffer.length(), keywordFormat);
}
else {
setFormat(i, buffer.length(), this->colorByType(SyntaxHighlighter::Text));
}
// increment i
if (!buffer.isEmpty()) {
i = j - 1;
}
}
// this is the beginning of a number
else if (ch.isDigit()) {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Number));
endStateOfLastPara = Digit;
}
// probably an operator
else if (ch.isSymbol() || ch.isPunct()) {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Operator));
}
}
}
} break;
case Comment: {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Comment));
} break;
case Literal1: {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::String));
if (ch == QLatin1Char('"')) {
endStateOfLastPara = Standard;
}
} break;
case Literal2: {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::String));
if (ch == QLatin1Char('\'')) {
endStateOfLastPara = Standard;
}
} break;
case Blockcomment1: {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::BlockComment));
if (i >= 2 && ch == QLatin1Char('"') && text.at(i - 1) == QLatin1Char('"')
&& text.at(i - 2) == QLatin1Char('"')) {
endStateOfLastPara = Standard;
}
} break;
case Blockcomment2: {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::BlockComment));
if (i >= 2 && ch == QLatin1Char('\'') && text.at(i - 1) == QLatin1Char('\'')
&& text.at(i - 2) == QLatin1Char('\'')) {
endStateOfLastPara = Standard;
}
} break;
case DefineName: {
if (ch.isLetterOrNumber() || ch == QLatin1Char(' ') || ch == QLatin1Char('_')) {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Defname));
}
else {
if (ch.isSymbol() || ch.isPunct()) {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Operator));
}
endStateOfLastPara = Standard;
}
} break;
case ClassName: {
if (ch.isLetterOrNumber() || ch == QLatin1Char(' ') || ch == QLatin1Char('_')) {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Classname));
}
else {
if (ch.isSymbol() || ch.isPunct()) {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Operator));
}
endStateOfLastPara = Standard;
}
} break;
case Digit: {
if (ch.isDigit() || ch == QLatin1Char('.')) {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Number));
}
else {
if (ch.isSymbol() || ch.isPunct()) {
setFormat(i, 1, this->colorByType(SyntaxHighlighter::Operator));
}
endStateOfLastPara = Standard;
}
} break;
}
prev = ch;
i++;
}
// only block comments can have several lines
if (endStateOfLastPara != Blockcomment1 && endStateOfLastPara != Blockcomment2) {
endStateOfLastPara = Standard;
}
setCurrentBlockState(endStateOfLastPara);
}
#include "moc_PythonEditor.cpp"