Skip to content

Commit 4a6c17a

Browse files
committed
Merge branch 'main' of https://github.com/sandbox-science/CodeAstra into main
2 parents e7df727 + baed706 commit 4a6c17a

File tree

4 files changed

+77
-20
lines changed

4 files changed

+77
-20
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,27 @@ CodeAstra is a modern, extensible, and lightweight code editor built using C++ a
3434
3535
> [!NOTE]
3636
>
37-
> CodeAstra is **under active development**—stay tuned for updates or feel free to contribute and help shape its future!
37+
> CodeAstra is **under active development**. Stay tuned for updates or feel free to contribute and help shape its future!
3838
3939
> [!IMPORTANT]
4040
>
41-
> CodeAstra has been tested on macOS and Linux and works as expected on those platforms. On Windows, the project has not been tested natively, but it works when using WSL (Windows Subsystem for Linux). Native Windows support is not guaranteed.
41+
> CodeAstra has been tested on macOS and Linux and works as expected on those platforms. The project has not been tested natively on Windows, but it works when using **WSL** (Windows Subsystem for Linux). Native Windows support is not guaranteed.
4242
4343
## How to install the project
4444
Please, check the [wiki](https://github.com/sandbox-science/CodeAstra/wiki) for recommended installation instructions.
4545

4646
## Required Tools
4747
- CMake
4848
- Make
49-
- g++ compiler (with support of C++17 and beyond)
49+
- g++ compiler (with support of C++20 and beyond)
5050
- Qt6 Framework
5151

5252
## Roadmap
5353
- [x] Basic text editing
54+
- [x] Open a Project
5455
- [x] Open a file
5556
- [x] Save file
56-
- [ ] Create a new file ~ in progress
57+
- [x] Create a new file
5758
- [x] File tree navigation
5859
- [ ] Syntax highlighting ~ in progress
5960
- Supported Languages:

src/FileManager.cpp

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,63 @@ void FileManager::setCurrentFileName(const QString fileName)
3737

3838
void FileManager::newFile()
3939
{
40-
// Logic to create a new file
40+
QString currentFileName = getCurrentFileName();
41+
bool isFileSaved = !currentFileName.isEmpty();
42+
bool isTextEditorEmpty = this->m_editor->toPlainText().isEmpty();
43+
// File has not been saved and the text editor is not empty
44+
if (!isFileSaved && !isTextEditorEmpty)
45+
{
46+
// Create box to prompt user to save changes to file
47+
QMessageBox promptBox;
48+
promptBox.setWindowTitle("Save Current File");
49+
promptBox.setText("Would you like to save the file?");
50+
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
51+
promptBox.setDefaultButton(QMessageBox::Save);
52+
53+
int option = promptBox.exec();
54+
// return if the user hit Cancel button
55+
if (option == QMessageBox::Cancel)
56+
{
57+
return;
58+
}
59+
60+
saveFile();
61+
}
62+
// File has been previously saved
63+
else if (isFileSaved)
64+
{
65+
// Read from saved file and compare to current file
66+
QFile file(currentFileName);
67+
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
68+
return;
69+
QTextStream in(&file);
70+
QString savedFileContents = in.readAll();
71+
file.close();
72+
if (savedFileContents != this->m_editor->toPlainText().trimmed())
73+
{
74+
// Create box to prompt user to save changes to file
75+
QMessageBox promptBox;
76+
promptBox.setWindowTitle("Changes Detected");
77+
promptBox.setText("Would you like to save the current changes to the file?");
78+
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
79+
promptBox.setDefaultButton(QMessageBox::Save);
80+
int option = promptBox.exec();
81+
// return if the user hit Cancel button
82+
if (option == QMessageBox::Cancel)
83+
{
84+
return;
85+
}
86+
saveFile();
87+
}
88+
}
89+
90+
if (!m_currentFileName.isEmpty())
91+
{
92+
setCurrentFileName("");
93+
m_editor->clear();
94+
m_mainWindow->setWindowTitle("Code Astra ~ untitled");
95+
}
96+
4197
}
4298

4399
void FileManager::saveFile()
@@ -235,14 +291,13 @@ OperationResult FileManager::deletePath(const QFileInfo &pathInfo)
235291
}
236292

237293
std::filesystem::path pathToDelete = pathInfo.absoluteFilePath().toStdString();
238-
239294
// Validate the input path
240295
if (!isValidPath(pathToDelete))
241296
{
242297
return {false, "ERROR: invalid file path." + pathToDelete.filename().string()};
243298
}
244-
245-
if (!QFile::moveToTrash(pathToDelete))
299+
QString qPathToDelete = QString::fromStdString(pathToDelete.string());
300+
if (!QFile::moveToTrash(qPathToDelete))
246301
{
247302
return {false, "ERROR: failed to delete: " + pathToDelete.string()};
248303
}
@@ -349,4 +404,4 @@ OperationResult FileManager::duplicatePath(const QFileInfo &pathInfo)
349404
qDebug() << "Duplicated file to:" << QString::fromStdString(dupPath.string());
350405

351406
return {true, dupPath.filename().string() + " duplicated successfully."};
352-
}
407+
}

tests/test_mainwindow.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void TestMainWindow::cleanupTestCase()
3838

3939
void TestMainWindow::testWindowTitle()
4040
{
41-
QCOMPARE(mainWindow->windowTitle(), "CodeAstra ~ Code Editor");
41+
QCOMPARE_EQ(mainWindow->windowTitle(), "CodeAstra ~ Code Editor");
4242
}
4343

4444
void TestMainWindow::testEditorInitialization()
@@ -83,24 +83,25 @@ void TestMainWindow::testCreateAction()
8383
{
8484
// Mock parameters for createAction
8585
QIcon icon;
86-
QString text = "Test Action";
86+
QString text = "Test Action";
8787
QKeySequence shortcut = QKeySequence(Qt::CTRL | Qt::Key_T);
88-
QString statusTip = "This is a test action";
89-
bool slotCalled = false;
90-
91-
auto slot = [&slotCalled]() { slotCalled = true; };
92-
88+
QString statusTip = "This is a test action";
89+
bool slotCalled = false;
90+
91+
auto slot = [&slotCalled]()
92+
{ slotCalled = true; };
93+
9394
QAction *action = mainWindow->createAction(icon, text, shortcut, statusTip, slot);
94-
95+
9596
QVERIFY2(action != nullptr, "Action should be successfully created.");
9697
QCOMPARE_EQ(action->text(), text);
9798
QCOMPARE_EQ(action->shortcuts().first(), shortcut);
9899
QCOMPARE_EQ(action->statusTip(), statusTip);
99-
100+
100101
// Simulate triggering the action
101102
action->trigger();
102103
QCOMPARE_EQ(slotCalled, true);
103104
}
104105

105106
QTEST_MAIN(TestMainWindow)
106-
#include "test_mainwindow.moc"
107+
#include "test_mainwindow.moc"

tests/test_syntax.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ void TestSyntax::testLoadMissingKeywords()
102102
}
103103

104104
QTEST_MAIN(TestSyntax)
105-
#include "test_syntax.moc"
105+
#include "test_syntax.moc"

0 commit comments

Comments
 (0)