Skip to content
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

Dev #10

Merged
merged 2 commits into from
Oct 7, 2024
Merged

Dev #10

Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 7 additions & 5 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ jobs:
# Set up MSVC on Windows
- name: Set up MSVC (Windows)
if: matrix.os == 'windows-latest'
uses: microsoft/setup-msbuild@v1 # Ensure MSVC is set up
- name: Set up VC Environment (Windows)
uses: actions/setup-msbuild@v1

# Set up Visual Studio using vswhere
- name: Set up Visual Studio (Windows)
if: matrix.os == 'windows-latest'
uses: microsoft/setup-vc@v1
with:
arch: x64
run: |
vswhere -products * -latest -property installationPath
export PATH="$PATH:/c/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/MSBuild/Current/Bin"

# Set reusable strings
- name: Set reusable strings
Expand Down
18 changes: 9 additions & 9 deletions CMakeLists.txt.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 14.0.1, 2024-10-07T04:55:20. -->
<!-- Written by QtCreator 14.0.1, 2024-10-07T21:31:36. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -102,12 +102,12 @@
<value type="int" key="CMake.Configure.BaseEnvironment">2</value>
<value type="bool" key="CMake.Configure.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="CMake.Configure.UserEnvironmentChanges"/>
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_BUILD_TYPE:STRING=Debug
-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}
-DCMAKE_GENERATOR:STRING=Ninja
-DCMAKE_BUILD_TYPE:STRING=Debug
-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
-DCMAKE_GENERATOR:STRING=Ninja
-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake</value>
<value type="QString" key="CMake.Source.Directory">/data/Code/Qt/Notepad--</value>
Expand Down Expand Up @@ -160,12 +160,12 @@
<value type="int" key="CMake.Configure.BaseEnvironment">2</value>
<value type="bool" key="CMake.Configure.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="CMake.Configure.UserEnvironmentChanges"/>
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
<value type="QString" key="CMake.Initial.Parameters">-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG}
-DCMAKE_GENERATOR:STRING=Ninja
-DCMAKE_BUILD_TYPE:STRING=Release
-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
-DCMAKE_GENERATOR:STRING=Ninja
-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake</value>
<value type="QString" key="CMake.Source.Directory">/data/Code/Qt/Notepad--</value>
Expand Down
87 changes: 47 additions & 40 deletions src/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,24 @@ Document::Document(const QString &filePath, QWidget *parent)
connect(m_fileLoaderWorker, &FileLoaderWorker::savingFinished, this, &Document::onSavingFinished, Qt::QueuedConnection);
connect(m_fileLoaderWorker, &FileLoaderWorker::loadingProgress, this, &Document::onLoadingProgress, Qt::QueuedConnection);
connect(m_fileLoaderWorker, &FileLoaderWorker::loadingFinished, this, &Document::onLoadingFinished, Qt::QueuedConnection);
connect(this, &Document::savingProgress, m_progressBar, &QProgressBar::setValue, Qt::QueuedConnection);
connect(m_fileLoaderWorker, &FileLoaderWorker::savingFinished, this, &Document::onSavingFinished, Qt::QueuedConnection);
connect(this, &Document::savingProgress, m_progressBar, &QProgressBar::setValue, Qt::QueuedConnection);
connect(this, &Document::uiReady, m_fileLoaderWorker, &FileLoaderWorker::startLoading);
connect(this, &Document::hideProgressBar, this, [this]() {
m_progressBar->setVisible(false); // Hide progress bar when loading/saving is done
m_statusLabel->setText("");
});

// Start worker thread
m_workerThread->start();
qDebug() << "Worker thread running:" << m_workerThread->isRunning();

// Connect uiReady signal to start loading in the worker thread
connect(this, &Document::uiReady, m_fileLoaderWorker, &FileLoaderWorker::startLoading);

// Use QTimer::singleShot to emit uiReady signal after a delay
QTimer::singleShot(100, this, [this]() {
qDebug() << "Emitting uiReady signal after ensuring UI is ready...";
emit uiReady();
});

// Set up status label and progress bar
m_statusLabel->setText("Loading File...");
m_progressBar->setVisible(true);
// Connect signals for progress updates
connect(this, &Document::hideProgressBar, this, [this]() {
m_progressBar->setVisible(false); // Hide progress bar when loading/saving is done
m_statusLabel->setText("");
});

qDebug() << "Document and thread initialized for file:" << m_filePath;
}

Expand Down Expand Up @@ -113,6 +106,7 @@ void Document::onLoadingStarted() {

void Document::onSavingStarted() {
m_isSaving = true;
m_statusLabel->setVisible(true);
m_statusLabel->setText("Saving File...");
m_progressBar->setVisible(true);
m_progressBar->setValue(0);
Expand Down Expand Up @@ -141,21 +135,24 @@ void Document::onSavingProgress(int progress) {

void Document::onLoadingFinished() {
m_isLoading = false;
emit loadingProgress(100); // Ensure progress is 100%
emit hideProgressBar(); // Hide the progress bar when loading finishes
emit loadingProgress(100);
emit hideProgressBar();

// Move the cursor to the first line after file is loaded
editor->moveCursor(QTextCursor::Start);
editor->ensureCursorVisible(); // Ensure the cursor is visible
qDebug() << "Cursor moved to the first line after file loaded";
// Detect file extension and apply syntax highlighter
QString fileExtension = QFileInfo(m_filePath).suffix();
QString language = LanguageManager::getLanguageFromExtension(fileExtension);
qDebug() << "Detected file extension: " << fileExtension << " Language: " << language;

qDebug() << "Loading finished, m_isLoading = " << m_isLoading;
// Apply syntax highlighter based on the language detected
applySyntaxHighlighter(language);

qDebug() << "File loading finished, syntax highlighter applied.";
}

void Document::onSavingFinished() {
m_isSaving = false;
m_statusLabel->setText(""); // Clear the label after saving
m_progressBar->setVisible(false); // Hide the progress bar when saving is finished
m_statusLabel->setText("");
m_progressBar->setVisible(false);
}

void Document::onLoadingError(const QString &error) {
Expand All @@ -165,6 +162,7 @@ void Document::onLoadingError(const QString &error) {

void Document::setFilePath(const QString &path) {
m_filePath = path;
m_fileExtension = QFileInfo(m_filePath).suffix();
qDebug() << "File path set to:" << m_filePath;
}

Expand All @@ -173,14 +171,24 @@ QString Document::filePath() const {
}

void Document::openFile(const QString &filePath) {
m_filePath = filePath;
qDebug() << "Opening file: " << m_filePath;
QFile file(filePath);
m_fileExtension = QFileInfo(filePath).suffix();
qDebug() << "file extension is: " << m_filePath;
qDebug() << "File extension extracted: " << m_fileExtension;

if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
editor->setPlainText(in.readAll()); // Set the content to the editor
file.close();

qDebug() << "About to emit uiReady signal from openFile...";
// Emit the signal to start loading the file in the worker thread
emit uiReady();
QString language = LanguageManager::getLanguageFromExtension(m_fileExtension);
qDebug() << "Detected file extension:" << m_fileExtension << "Detected language:" << language;

qDebug() << "Calling applySyntaxHighlighter for file:" << m_filePath;
// Apply the correct syntax highlighter based on the file extension
applySyntaxHighlighter(language);
} else {
qDebug() << "Error: Could not open file:" << filePath;
}
}

void Document::saveFile() {
Expand Down Expand Up @@ -213,8 +221,6 @@ void Document::saveFileAs(const QString &newFilePath) {

// Set the new file path
m_filePath = newFilePath;

// Now call saveFile to save the content to the new file path
saveFile();
}

Expand Down Expand Up @@ -295,20 +301,20 @@ void Document::goToLineNumberInText(QWidget* parent) {
}

void Document::applySyntaxHighlighter(const QString &language) {
qDebug() << "Applying syntax highlighter for language:" << language;
qDebug() << "Applying syntax highlighter for language: " << language;

// Automatically replaces and deletes the old syntax highlighter if it exists
syntaxHighlighter = nullptr;
if (syntaxHighlighter) {
qDebug() << "Deleting existing syntax highlighter";
syntaxHighlighter.reset(); // Safely reset unique_ptr
}

// Assuming LanguageManager handles creating syntax highlighters for specific languages
// Create highlighter based on the language
syntaxHighlighter = std::unique_ptr<QSyntaxHighlighter>(LanguageManager::createHighlighterForExtension(language, editor->document()));

if (syntaxHighlighter) {
qDebug() << "Rehighlighting document...";
syntaxHighlighter->rehighlight();
qDebug() << "Syntax highlighter applied for language:" << language;
qDebug() << "Syntax highlighter created for language: " << language;
syntaxHighlighter->rehighlight(); // Rehighlight to apply
} else {
qDebug() << "No syntax highlighter found for language:" << language;
qDebug() << "Failed to create syntax highlighter for language: " << language;
}
}

Expand Down Expand Up @@ -336,13 +342,14 @@ bool Document::compareText(const QString &text1, const QString &text2) {

void Document::saveDocument() {
QString filePath = m_filePath; // Get the file path
QString fileContent = editor->toPlainText(); // Get the current content from the editor

if (filePath.isEmpty()) {
qDebug() << "File path is empty. Save operation aborted.";
return;
}

QString fileContent = editor->toPlainText(); // Get the current content from the editor

// Call the worker to save the file content
m_fileLoaderWorker->saveFile(filePath, fileContent);
}
Expand Down
8 changes: 1 addition & 7 deletions src/languages/languagemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@

QSyntaxHighlighter* LanguageManager::createHighlighterForExtension(const QString &identifier, QTextDocument *document) {
qDebug() << "Creating highlighter for language:" << identifier << "with document:" << document;
if (document == nullptr) {
qDebug() << "Error: document is nullptr";
return nullptr;
}

// Create highlighter based on language identifier
if (identifier == "C++") {
auto* highlighter = new CppSyntaxHighlighter(document);
qDebug() << "Created C++ syntax highlighter at:" << highlighter;
return highlighter;
}

// Return nullptr if no matching highlighter is found
qDebug() << "Error: No matching highlighter found for language:" << identifier;
qDebug() << "No highlighter found for language: " << identifier;
return nullptr;
}

Expand Down
Loading