Skip to content

Commit a6657d0

Browse files
committed
[Refactor] Consistently prefix member variables with 'm_' in CodeEditor, LineNumberArea, MainWindow, and Syntax classes for improved readability
1 parent ed48a0c commit a6657d0

File tree

7 files changed

+67
-68
lines changed

7 files changed

+67
-68
lines changed

include/CodeEditor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ private slots:
2929
void updateLineNumberArea(const QRect &rect, int dy);
3030

3131
private:
32-
QWidget *lineNumberArea;
32+
QWidget *m_lineNumberArea;
3333
};

include/LineNumberArea.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
class LineNumberArea : public QWidget
1010
{
1111
public:
12-
LineNumberArea(CodeEditor *editor) : QWidget(editor), m_codeEditor(editor) {}
12+
LineNumberArea(CodeEditor *editor) : QWidget(editor), codeEditor(editor) {}
1313

1414
QSize sizeHint() const override
1515
{
16-
return QSize(m_codeEditor->lineNumberAreaWidth(), 0);
16+
return QSize(codeEditor->lineNumberAreaWidth(), 0);
1717
}
1818

1919
protected:
2020
void paintEvent(QPaintEvent *event) override
2121
{
22-
m_codeEditor->lineNumberAreaPaintEvent(event);
22+
codeEditor->lineNumberAreaPaintEvent(event);
2323
}
2424

2525
private:
26-
CodeEditor *m_codeEditor;
26+
CodeEditor *codeEditor;
2727
};

include/MainWindow.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ private slots:
3939
QAction *createAction(const QIcon &icon, const QString &text,
4040
const QKeySequence &shortcut, const QString &statusTip,
4141
void (MainWindow::*slot)());
42-
std::unique_ptr<CodeEditor> editor;
43-
QString currentFileName;
44-
std::unique_ptr<Syntax> syntax;
45-
std::unique_ptr<Tree> tree;
42+
std::unique_ptr<CodeEditor> m_editor;
43+
std::unique_ptr<Syntax> m_syntax;
44+
std::unique_ptr<Tree> m_tree;
45+
QString m_currentFileName;
4646
};

include/Syntax.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ class Syntax : public QSyntaxHighlighter
1818
private:
1919
struct SyntaxRule
2020
{
21-
QRegularExpression pattern;
22-
QTextCharFormat format;
21+
QRegularExpression m_pattern;
22+
QTextCharFormat m_format;
2323
};
24-
QVector<SyntaxRule> syntaxRules;
25-
26-
QTextCharFormat keywordFormat;
27-
QTextCharFormat singleLineCommentFormat;
28-
QTextCharFormat quotationMark;
29-
QTextCharFormat functionFormat;
30-
QTextCharFormat parenthesisFormat;
31-
QTextCharFormat charFormat;
32-
QTextCharFormat iterationFormat;
24+
QVector<SyntaxRule> m_syntaxRules;
25+
26+
QTextCharFormat m_keywordFormat;
27+
QTextCharFormat m_singleLineCommentFormat;
28+
QTextCharFormat m_quotationMark;
29+
QTextCharFormat m_functionFormat;
30+
QTextCharFormat m_parenthesisFormat;
31+
QTextCharFormat m_charFormat;
32+
QTextCharFormat m_iterationFormat;
3333

3434
void addPattern(const QString &pattern, const QTextCharFormat &format);
3535

src/CodeEditor.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
CodeEditor::CodeEditor(QWidget *parent)
99
: QPlainTextEdit(parent),
10-
lineNumberArea(new LineNumberArea(this))
10+
m_lineNumberArea(new LineNumberArea(this))
1111
{
1212
connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
1313
connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
@@ -82,11 +82,11 @@ void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
8282
{
8383
if (dy)
8484
{
85-
lineNumberArea->scroll(0, dy);
85+
m_lineNumberArea->scroll(0, dy);
8686
}
8787
else
8888
{
89-
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
89+
m_lineNumberArea->update(0, rect.y(), m_lineNumberArea->width(), rect.height());
9090
}
9191

9292
if (rect.contains(viewport()->rect()))
@@ -100,7 +100,7 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
100100
QPlainTextEdit::resizeEvent(e);
101101

102102
QRect cr = contentsRect();
103-
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
103+
m_lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
104104
}
105105

106106
void CodeEditor::highlightCurrentLine()
@@ -128,13 +128,13 @@ void CodeEditor::highlightCurrentLine()
128128

129129
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
130130
{
131-
QPainter painter(lineNumberArea);
131+
QPainter painter(m_lineNumberArea);
132132

133133
// Match the background color of the editor
134134
painter.fillRect(event->rect(), palette().color(QPalette::Base));
135135

136136
// Draw a separating line between the number area and the text editor
137-
int separatorX = lineNumberArea->width() - 4;
137+
int separatorX = m_lineNumberArea->width() - 4;
138138
painter.drawLine(separatorX, event->rect().top(), separatorX, event->rect().bottom());
139139

140140
QTextBlock block = firstVisibleBlock();
@@ -152,7 +152,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
152152
QString number = QString::number(blockNumber + 1);
153153
painter.setPen(Qt::darkGray);
154154

155-
painter.drawText(0, top + padding, lineNumberArea->width(), lineHeight,
155+
painter.drawText(0, top + padding, m_lineNumberArea->width(), lineHeight,
156156
Qt::AlignCenter, number);
157157
}
158158

src/MainWindow.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515

1616
MainWindow::MainWindow(QWidget *parent)
1717
: QMainWindow(parent),
18-
editor(std::make_unique<CodeEditor>(this)),
19-
syntax(std::make_unique<Syntax>(editor->document())),
20-
tree(nullptr)
18+
m_editor(std::make_unique<CodeEditor>(this)),
19+
m_syntax(std::make_unique<Syntax>(m_editor->document())),
20+
m_tree(nullptr)
2121
{
2222
setWindowTitle("CodeAstra ~ Code Editor");
2323

2424
// Set tab width to 4 spaces
25-
QFontMetrics metrics(editor->font());
25+
QFontMetrics metrics(m_editor->font());
2626
int spaceWidth = metrics.horizontalAdvance(" ");
27-
editor->setTabStopDistance(spaceWidth * 4);
28-
editor->setLineWrapMode(QPlainTextEdit::NoWrap);
27+
m_editor->setTabStopDistance(spaceWidth * 4);
28+
m_editor->setLineWrapMode(QPlainTextEdit::NoWrap);
2929

3030
initTree();
3131
createMenuBar();
@@ -39,9 +39,9 @@ void MainWindow::initTree()
3939
QSplitter *splitter = new QSplitter(Qt::Horizontal, this);
4040
setCentralWidget(splitter);
4141

42-
tree = std::make_unique<Tree>(splitter, this);
42+
m_tree = std::make_unique<Tree>(splitter, this);
4343

44-
splitter->addWidget(editor.get());
44+
splitter->addWidget(m_editor.get());
4545
splitter->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
4646
splitter->setHandleWidth(5);
4747
splitter->setSizes(QList<int>() << 150 << 800);
@@ -164,23 +164,23 @@ void MainWindow::openFile()
164164

165165
void MainWindow::saveFile()
166166
{
167-
if (currentFileName.isEmpty())
167+
if (m_currentFileName.isEmpty())
168168
{
169169
saveFileAs();
170170
return;
171171
}
172172

173-
QFile file(currentFileName);
173+
QFile file(m_currentFileName);
174174
if (!file.open(QFile::WriteOnly | QFile::Text))
175175
{
176176
QMessageBox::warning(this, "Error", "Cannot save file: " + file.errorString());
177177
return;
178178
}
179179

180180
QTextStream out(&file);
181-
if (editor)
181+
if (m_editor)
182182
{
183-
out << editor->toPlainText();
183+
out << m_editor->toPlainText();
184184
}
185185
else
186186
{
@@ -199,7 +199,7 @@ void MainWindow::saveFileAs()
199199

200200
if (!fileName.isEmpty())
201201
{
202-
currentFileName = fileName;
202+
m_currentFileName = fileName;
203203
saveFile();
204204
}
205205
}
@@ -214,9 +214,9 @@ void MainWindow::loadFileInEditor(const QString &filePath)
214214
}
215215

216216
QTextStream in(&file);
217-
if (editor)
217+
if (m_editor)
218218
{
219-
editor->setPlainText(in.readAll());
219+
m_editor->setPlainText(in.readAll());
220220
}
221221
else
222222
{
@@ -225,6 +225,6 @@ void MainWindow::loadFileInEditor(const QString &filePath)
225225
}
226226
file.close();
227227

228-
currentFileName = filePath;
228+
m_currentFileName = filePath;
229229
setWindowTitle("CodeAstra ~ " + QFileInfo(filePath).fileName());
230230
}

src/Syntax.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Syntax::Syntax(QTextDocument *parent)
1919
void Syntax::initKeywordRules()
2020
{
2121
// Keyword format
22-
keywordFormat.setForeground(Qt::blue);
23-
keywordFormat.setFontWeight(QFont::Bold);
22+
m_keywordFormat.setForeground(Qt::blue);
23+
m_keywordFormat.setFontWeight(QFont::Bold);
2424

2525
QStringList keywordPatterns = {
2626
"\\bchar\\b", "\\bclass\\b", "\\bconst\\b",
@@ -36,77 +36,76 @@ void Syntax::initKeywordRules()
3636

3737
for (const QString &pattern : keywordPatterns)
3838
{
39-
addPattern(pattern, keywordFormat);
39+
addPattern(pattern, m_keywordFormat);
4040
}
4141

42-
iterationFormat.setForeground(Qt::darkMagenta);
43-
iterationFormat.setFontWeight(QFont::Bold);
44-
QStringList iterationPatterns = {
45-
"\\bfor\\b", "\\bwhile\\b", "\\bdo\\b", "\\bif\\b", "\\belse\\b"};
42+
m_iterationFormat.setForeground(Qt::darkMagenta);
43+
m_iterationFormat.setFontWeight(QFont::Bold);
44+
QStringList iterationPatterns = {"\\bfor\\b", "\\bwhile\\b", "\\bdo\\b", "\\bif\\b", "\\belse\\b"};
4645
for (const QString &pattern : iterationPatterns)
4746
{
48-
addPattern(pattern, iterationFormat);
47+
addPattern(pattern, m_iterationFormat);
4948
}
5049
}
5150

5251
void Syntax::initCommentRules()
5352
{
5453
// Single line comment format expression
55-
singleLineCommentFormat.setForeground(Qt::darkGray);
56-
addPattern("//[^\n]*", singleLineCommentFormat);
54+
m_singleLineCommentFormat.setForeground(Qt::darkGray);
55+
addPattern("//[^\n]*", m_singleLineCommentFormat);
5756

5857
// TO-DO: Add multi-line comment support
5958
}
6059

6160
void Syntax::initQuotationRules()
6261
{
6362
// Double quotation mark for string
64-
quotationMark.setForeground(Qt::darkGreen);
65-
addPattern("\"(\\\\.|[^\"\\\\])*\"", quotationMark);
63+
m_quotationMark.setForeground(Qt::darkGreen);
64+
addPattern("\"(\\\\.|[^\"\\\\])*\"", m_quotationMark);
6665

6766
// TO-DO: Add single quotation mark for character
6867
}
6968

7069
void Syntax::initFunctionRules()
7170
{
7271
// Function format expression
73-
functionFormat.setFontItalic(true);
74-
functionFormat.setForeground(Qt::darkYellow);
75-
addPattern("\\b(?!for\\b|if\\b|else\\b|while\\b|do\\b)[a-zA-Z_][a-zA-Z0-9_]*(?=\\s*\\()", functionFormat);
72+
m_functionFormat.setFontItalic(true);
73+
m_functionFormat.setForeground(Qt::darkYellow);
74+
addPattern("\\b(?!for\\b|if\\b|else\\b|while\\b|do\\b)[a-zA-Z_][a-zA-Z0-9_]*(?=\\s*\\()", m_functionFormat);
7675
}
7776

7877
void Syntax::initParenthesisRules()
7978
{
8079
// Color pattern for parenthesis
8180
QColor parenthesisColor("#6495ED");
82-
parenthesisFormat.setForeground(parenthesisColor);
83-
addPattern("[()]", parenthesisFormat);
81+
m_parenthesisFormat.setForeground(parenthesisColor);
82+
addPattern("[()]", m_parenthesisFormat);
8483
}
8584

8685
// Regex for single character format 'a', '\n', etc.
8786
void Syntax::initCharRules()
8887
{
89-
charFormat.setForeground(Qt::darkCyan);
90-
addPattern("'(\\\\.|[^'])'", charFormat);
88+
m_charFormat.setForeground(Qt::darkCyan);
89+
addPattern("'(\\\\.|[^'])'", m_charFormat);
9190
}
9291

9392
void Syntax::addPattern(const QString &pattern, const QTextCharFormat &format)
9493
{
9594
SyntaxRule rule;
96-
rule.pattern = QRegularExpression(pattern);
97-
rule.format = format;
98-
syntaxRules.append(rule);
95+
rule.m_pattern = QRegularExpression(pattern);
96+
rule.m_format = format;
97+
m_syntaxRules.append(rule);
9998
}
10099

101100
void Syntax::highlightBlock(const QString &text)
102101
{
103-
for (const SyntaxRule &rule : syntaxRules)
102+
for (const SyntaxRule &rule : m_syntaxRules)
104103
{
105-
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
104+
QRegularExpressionMatchIterator matchIterator = rule.m_pattern.globalMatch(text);
106105
while (matchIterator.hasNext())
107106
{
108107
QRegularExpressionMatch match = matchIterator.next();
109-
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
108+
setFormat(match.capturedStart(), match.capturedLength(), rule.m_format);
110109
}
111110
}
112111
}

0 commit comments

Comments
 (0)