Skip to content

Refactor Codebase for Modularity, Correctness, and Maintainability #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
0ba2f83
[Update] Refactor MainWindow initialization and enhance file dialog f…
chrisdedman Mar 16, 2025
a12b36d
[Update] Simplify CodeEditor constructor by initializing lineNumberAr…
chrisdedman Mar 16, 2025
e352206
[Update] Improve icon creation by adding error handling and using ant…
chrisdedman Mar 16, 2025
fecca4b
[Add] Declare initTree method in MainWindow class for tree initializa…
chrisdedman Mar 16, 2025
8286019
[Refactor] Refactor Tree constructor to initialize members in the ini…
chrisdedman Mar 16, 2025
bb89ad4
Update README to reflect progress on file creation and syntax highlig…
chrisdedman Mar 16, 2025
f564991
[Update] Bump application version to 0.1.0 in main.cpp
chrisdedman Mar 19, 2025
98c4307
[Add] Include LineNumberArea.h in the executable target
chrisdedman Mar 21, 2025
c5ece45
[Refactor] Replace include guards with #pragma once in CodeEditor and…
chrisdedman Mar 21, 2025
b498028
[Refactor] Replace include guards with #pragma once in MainWindow and…
chrisdedman Mar 21, 2025
113d206
[Refactor] Replace include guards with #pragma once and change QList …
chrisdedman Mar 21, 2025
b12e995
[Refactor] Improve memory management by using smart pointers for edit…
chrisdedman Mar 21, 2025
53aafe5
[Refactor] Organize syntax highlighting rules into separate initializ…
chrisdedman Mar 21, 2025
ed48a0c
[Refactor] Replace raw pointers with smart pointers for improved memo…
chrisdedman Mar 21, 2025
a6657d0
[Refactor] Consistently prefix member variables with 'm_' in CodeEdit…
chrisdedman Mar 21, 2025
a714767
[Refactor] Emit status messages on mode changes in CodeEditor for imp…
chrisdedman Mar 21, 2025
1be8461
[Refactor] Enhance status message display in MainWindow with timestam…
chrisdedman Mar 21, 2025
8fbb4f4
[Refactor] Emit status message on file save in MainWindow for improve…
chrisdedman Mar 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ add_executable(${TARGET_NAME}
include/CodeEditor.h
include/Syntax.h
include/Tree.h
include/LineNumberArea.h
)

qt_add_resources(APP_RESOURCES resources.qrc)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ Please, check the [wiki](https://github.com/sandbox-science/CodeAstra/wiki) for
- [x] Basic text editing
- [x] Open a file
- [x] Save file
- [ ] Create a new file
- [ ] File tree navigation
- [ ] Syntax highlighting
- [ ] Create a new file ~ in progress
- [x] File tree navigation
- [ ] Syntax highlighting ~ in progress
- [ ] Plugin system

## To-Do
Expand Down
12 changes: 6 additions & 6 deletions include/CodeEditor.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#pragma once

#include <QPlainTextEdit>
#include <QKeyEvent>
Expand All @@ -20,6 +19,9 @@ class CodeEditor : public QPlainTextEdit
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();

signals:
void statusMessageChanged(const QString &message);

protected:
void keyPressEvent(QKeyEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
Expand All @@ -30,7 +32,5 @@ private slots:
void updateLineNumberArea(const QRect &rect, int dy);

private:
QWidget *lineNumberArea;
};

#endif // CODEEDITOR_H
QWidget *m_lineNumberArea;
};
27 changes: 12 additions & 15 deletions include/LineNumberArea.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef LINENUMBER_H
#define LINENUMBER_H
#pragma once

#include "CodeEditor.h"

Expand All @@ -10,21 +9,19 @@
class LineNumberArea : public QWidget
{
public:
LineNumberArea(CodeEditor *editor) : QWidget(editor), codeEditor(editor) {}
LineNumberArea(CodeEditor *editor) : QWidget(editor), codeEditor(editor) {}

QSize sizeHint() const override
{
return QSize(codeEditor->lineNumberAreaWidth(), 0);
}
QSize sizeHint() const override
{
return QSize(codeEditor->lineNumberAreaWidth(), 0);
}

protected:
void paintEvent(QPaintEvent *event) override
{
codeEditor->lineNumberAreaPaintEvent(event);
}
void paintEvent(QPaintEvent *event) override
{
codeEditor->lineNumberAreaPaintEvent(event);
}

private:
CodeEditor *codeEditor;
};

#endif // LINENUMBER_H
CodeEditor *codeEditor;
};
91 changes: 46 additions & 45 deletions include/MainWindow.h
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "CodeEditor.h"
#include "Syntax.h"
#include "Tree.h"

#include <QMainWindow>
#include <QMenu>
#include <QAction>
#include <QIcon>
#include <QKeySequence>
#include <QDesktopServices>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = nullptr);
virtual ~MainWindow();
void loadFileInEditor(const QString &filePath);

private slots:
void newFile();
void openFile();
void saveFile();
void saveFileAs();
void showAbout();

private:
void createMenuBar();
void createFileActions(QMenu *fileMenu);
void createHelpActions(QMenu *helpMenu);
void createAppActions(QMenu *appMenu);
QAction *createAction(const QIcon &icon, const QString &text,
const QKeySequence &shortcut, const QString &statusTip,
void (MainWindow::*slot)());
CodeEditor *editor;
QString currentFileName;
Syntax *syntax;
Tree *tree;
};

#endif // MAINWINDOW_H
#pragma once

#include <QMainWindow>
#include <QMenu>
#include <QAction>
#include <QIcon>
#include <QKeySequence>
#include <memory>
#include <QFile>

class CodeEditor;
class Syntax;
class Tree;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = nullptr);
virtual ~MainWindow();
void loadFileInEditor(const QString &filePath);
// Initialize the file tree view and set it as the central widget
// of the main window, alongside the code editor
void initTree();

private slots:
void newFile();
void openFile();
void saveFile();
void saveFileAs();
void showAbout();

private:
void createMenuBar();
void createFileActions(QMenu *fileMenu);
void createHelpActions(QMenu *helpMenu);
void createAppActions(QMenu *appMenu);
QAction *createAction(const QIcon &icon, const QString &text,
const QKeySequence &shortcut, const QString &statusTip,
void (MainWindow::*slot)());
std::unique_ptr<CodeEditor> m_editor;
std::unique_ptr<Syntax> m_syntax;
std::unique_ptr<Tree> m_tree;
QString m_currentFileName;
};
32 changes: 19 additions & 13 deletions include/Syntax.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef SYNTAX_H
#define SYNTAX_H
#pragma once

#include <QTextCharFormat>
#include <QRegularExpression>
Expand All @@ -19,19 +18,26 @@ class Syntax : public QSyntaxHighlighter
private:
struct SyntaxRule
{
QRegularExpression pattern;
QTextCharFormat format;
QRegularExpression m_pattern;
QTextCharFormat m_format;
};
QList<SyntaxRule> syntaxRules;
QVector<SyntaxRule> m_syntaxRules;

QTextCharFormat keywordFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat quotationMark;
QTextCharFormat functionFormat;
QTextCharFormat parenthesisFormat;
QTextCharFormat charFormat;
QTextCharFormat m_keywordFormat;
QTextCharFormat m_singleLineCommentFormat;
QTextCharFormat m_quotationMark;
QTextCharFormat m_functionFormat;
QTextCharFormat m_parenthesisFormat;
QTextCharFormat m_charFormat;
QTextCharFormat m_iterationFormat;

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

#endif // SYNTAX_H
// Initialization functions for different syntax highlighting rules
void initKeywordRules();
void initCommentRules();
void initFunctionRules();
void initParenthesisRules();
void initCharRules();
void initQuotationRules();
};
26 changes: 14 additions & 12 deletions include/Tree.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
#ifndef TREE_H
#define TREE_H
#pragma once

#include <QSplitter>
#include <QTreeView>
#include <QFileSystemModel>
#include <QObject>
#include <memory>

class MainWindow; // Forward declaration
// Forward declarations
class MainWindow;
class QTreeView;
class QFileSystemModel;
class QFileIconProvider;

class Tree : public QObject
{
Q_OBJECT

public:
Tree(QSplitter *splitter, MainWindow *mainWindow);
explicit Tree(QSplitter *splitter, MainWindow *mainWindow);
~Tree();

private:
void showContextMenu(const QPoint &pos);
void setupModel();
void setupTree();
void openFile(const QModelIndex &index);
QString getDirectoryPath();
QString getDirectoryPath() const;

QFileSystemModel *model;
QTreeView *tree;
MainWindow *mainWindow;
};
std::unique_ptr<QFileIconProvider> m_iconProvider;
std::unique_ptr<QFileSystemModel> m_model;
std::unique_ptr<QTreeView> m_tree;

#endif // TREE_H
MainWindow *m_mainWindow;
};
36 changes: 23 additions & 13 deletions src/CodeEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

#include <QPainter>
#include <QTextBlock>
#include <QStatusBar>

CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
CodeEditor::CodeEditor(QWidget *parent)
: QPlainTextEdit(parent),
m_lineNumberArea(new LineNumberArea(this))
{
lineNumberArea = new LineNumberArea(this);

connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
connect(this, &CodeEditor::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine);
Expand All @@ -33,6 +34,7 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
{
case Qt::Key_I:
mode = INSERT;
emit statusMessageChanged("Insert mode activated");
break;
case Qt::Key_A:
moveCursor(QTextCursor::Left);
Expand All @@ -46,14 +48,22 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
case Qt::Key_W:
moveCursor(QTextCursor::Up);
break;
case Qt::Key_Escape:
mode = NORMAL;
default:
emit statusMessageChanged("Insert mode is not active. Press 'i' to enter insert mode.");
break;
}
}
else
else if (mode == INSERT)
{
QPlainTextEdit::keyPressEvent(event);
if (event->key() == Qt::Key_Escape)
{
mode = NORMAL;
emit statusMessageChanged("Normal mode activated. Press 'escape' to return to normal mode.");
}
else
{
QPlainTextEdit::keyPressEvent(event);
}
}
}

Expand Down Expand Up @@ -82,11 +92,11 @@ void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
{
if (dy)
{
lineNumberArea->scroll(0, dy);
m_lineNumberArea->scroll(0, dy);
}
else
{
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
m_lineNumberArea->update(0, rect.y(), m_lineNumberArea->width(), rect.height());
}

if (rect.contains(viewport()->rect()))
Expand All @@ -100,7 +110,7 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
QPlainTextEdit::resizeEvent(e);

QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
m_lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}

void CodeEditor::highlightCurrentLine()
Expand Down Expand Up @@ -128,13 +138,13 @@ void CodeEditor::highlightCurrentLine()

void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
QPainter painter(m_lineNumberArea);

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

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

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

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

Expand Down
Loading