From bd3b381bb0097c7013afce1472711bebc62ac5a3 Mon Sep 17 00:00:00 2001 From: Remisa Yousefvand Date: Mon, 7 Oct 2024 17:13:16 +0330 Subject: [PATCH 1/2] github action --- .github/workflows/cmake-multi-platform.yml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 2847656..3ec68f0 100755 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -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 From 4a5336b9bacce1cacc5a499273c45624f2a2a9b7 Mon Sep 17 00:00:00 2001 From: Remisa Yousefvand Date: Mon, 7 Oct 2024 21:34:35 +0330 Subject: [PATCH 2/2] syntax highlighting based on file extension fixed --- CMakeLists.txt.user | 18 +++---- src/document.cpp | 87 +++++++++++++++++-------------- src/languages/languagemanager.cpp | 8 +-- 3 files changed, 57 insertions(+), 56 deletions(-) diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user index d450ba8..9b1d7e2 100755 --- a/CMakeLists.txt.user +++ b/CMakeLists.txt.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -102,12 +102,12 @@ 2 false - -DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} --DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} + -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 /data/Code/Qt/Notepad-- @@ -160,12 +160,12 @@ 2 false - -DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} --DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} + -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 /data/Code/Qt/Notepad-- diff --git a/src/document.cpp b/src/document.cpp index 7bef2f2..661359c 100755 --- a/src/document.cpp +++ b/src/document.cpp @@ -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; } @@ -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); @@ -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) { @@ -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; } @@ -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() { @@ -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(); } @@ -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(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; } } @@ -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); } diff --git a/src/languages/languagemanager.cpp b/src/languages/languagemanager.cpp index e7f3197..ed6fa92 100755 --- a/src/languages/languagemanager.cpp +++ b/src/languages/languagemanager.cpp @@ -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; }