From 20cb7283068b17fd04518dbc5ebef505e6fa45d3 Mon Sep 17 00:00:00 2001 From: Cyano Hao Date: Tue, 3 Sep 2024 19:53:15 +0800 Subject: [PATCH] fix astyle locale (v2) (#479) --- RedPandaIDE/editor.cpp | 24 +++++++-- RedPandaIDE/mainwindow.cpp | 15 ++++-- RedPandaIDE/settings.cpp | 3 +- .../settingsdialog/formattergeneralwidget.cpp | 21 ++++++-- RedPandaIDE/utils.cpp | 52 +++++++++---------- RedPandaIDE/utils.h | 12 +++-- 6 files changed, 85 insertions(+), 42 deletions(-) diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 2cdb0bec..0421bd9e 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -56,6 +56,7 @@ #include #include "project.h" #include +#include "utils/escape.h" QHash> Editor::mSharedParsers; @@ -5115,17 +5116,34 @@ void Editor::reformat(bool doReparse) { if (readOnly()) return; - if (!fileExists(pSettings->environment().AStylePath())) { + const QString &astyle = pSettings->environment().AStylePath(); + if (!fileExists(astyle)) { QMessageBox::critical(this, tr("astyle not found"), - tr("Can't find astyle in \"%1\".").arg(pSettings->environment().AStylePath())); + tr("Can't find astyle in \"%1\".").arg(astyle)); return; } //we must remove all breakpoints and syntax issues // onLinesDeleted(1,lineCount()); QByteArray content = text().toUtf8(); QStringList args = pSettings->codeFormatter().getArguments(); - QByteArray newContent = reformatContentUsingAstyle(content,args); + QString command = escapeCommandForPlatformShell(extractFileName(astyle), args); + pMainWindow->logToolsOutput(tr("Reformatting content using astyle...")); + pMainWindow->logToolsOutput("------------------"); + pMainWindow->logToolsOutput(tr("- Astyle: %1").arg(astyle)); + pMainWindow->logToolsOutput(tr("- Command: %1").arg(command)); + auto [newContent, astyleError, processError] = + runAndGetOutput(astyle, extractFileDir(astyle), args, content, true); + if (!astyleError.isEmpty()) { +#ifdef Q_OS_WIN + QString msg = QString::fromLocal8Bit(astyleError); +#else + QString msg = QString::fromUtf8(astyleError); +#endif + pMainWindow->logToolsOutput(msg); + } + if (!processError.isEmpty()) + pMainWindow->logToolsOutput(processError); if (newContent.isEmpty()) return; replaceContent(QString::fromUtf8(newContent), doReparse); diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index 3fbf6d7b..8c2353be 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -1218,6 +1218,7 @@ void MainWindow::executeTool(PToolItem item) Editor *e; QByteArray inputContent; QByteArray output; + QString errorMessage; clearToolsOutput(); switch(item->inputOrigin) { case ToolItemInputOrigin::None: @@ -1251,18 +1252,24 @@ void MainWindow::executeTool(PToolItem item) QString cmd="cmd"; QStringList args{"/C",file.fileName()}; command = escapeCommandForPlatformShell(cmd, args); - output = runAndGetOutput(cmd, workDir, args, inputContent); + auto [o, _, em] = runAndGetOutput(cmd, workDir, args, inputContent); + output = o; + errorMessage = em; } } else { #endif command = escapeCommandForPlatformShell(program, params); - output = runAndGetOutput(program, workDir, params, inputContent); + auto [o, _, em] = runAndGetOutput(program, workDir, params, inputContent); + output = o; + errorMessage = em; #ifdef Q_OS_WIN } #endif switch(item->outputTarget) { case ToolItemOutputTarget::RedirectToToolsOutputPanel: logToolsOutput(tr(" - Command: %1").arg(command)); + if (!errorMessage.isEmpty()) + logToolsOutput(errorMessage); logToolsOutput(""); logToolsOutput(byteArrayToString(output, item->isUTF8)); stretchMessagesPanel(true); @@ -1273,12 +1280,12 @@ void MainWindow::executeTool(PToolItem item) case ToolItemOutputTarget::RepalceWholeDocument: e=mEditorList->getEditor(); if (e) - e->replaceContent(byteArrayToString(output, item->isUTF8)); + e->replaceContent(errorMessage + byteArrayToString(output, item->isUTF8)); break; case ToolItemOutputTarget::ReplaceCurrentSelection: e=mEditorList->getEditor(); if (e) - e->setSelText(byteArrayToString(output, item->isUTF8)); + e->setSelText(errorMessage + byteArrayToString(output, item->isUTF8)); break; } } diff --git a/RedPandaIDE/settings.cpp b/RedPandaIDE/settings.cpp index 4c82a66f..36965a2e 100644 --- a/RedPandaIDE/settings.cpp +++ b/RedPandaIDE/settings.cpp @@ -2849,12 +2849,13 @@ QByteArray Settings::CompilerSet::getCompilerOutput(const QString &binDir, const env.insert("LANG","en"); QString path = binDir; env.insert("PATH",path); - QByteArray result = runAndGetOutput( + auto [result, _, errorMessage] = runAndGetOutput( includeTrailingPathDelimiter(binDir)+binFile, binDir, arguments, QByteArray(), false, + false, env); return result.trimmed(); } diff --git a/RedPandaIDE/settingsdialog/formattergeneralwidget.cpp b/RedPandaIDE/settingsdialog/formattergeneralwidget.cpp index 2ffc63e1..269b33fe 100644 --- a/RedPandaIDE/settingsdialog/formattergeneralwidget.cpp +++ b/RedPandaIDE/settingsdialog/formattergeneralwidget.cpp @@ -326,8 +326,9 @@ void FormatterGeneralWidget::on_chkBreakMaxCodeLength_stateChanged(int) void FormatterGeneralWidget::updateDemo() { - if (!fileExists(pSettings->environment().AStylePath())) { - ui->editDemo->document()->setText(Editor::tr("Can't find astyle in \"%1\".").arg(pSettings->environment().AStylePath())); + const QString &astyle = pSettings->environment().AStylePath(); + if (!fileExists(astyle)) { + ui->editDemo->document()->setText(Editor::tr("Can't find astyle in \"%1\".").arg(astyle)); return; } QFile file(":/codes/formatdemo.cpp"); @@ -337,8 +338,20 @@ void FormatterGeneralWidget::updateDemo() Settings::CodeFormatter formatter(nullptr); updateCodeFormatter(formatter); - QByteArray newContent = reformatContentUsingAstyle(content, formatter.getArguments()); - ui->editDemo->document()->setText(newContent); + auto [newContent, astyleError, processError] = + runAndGetOutput(astyle, extractFileDir(astyle), formatter.getArguments(), content, true); + QString display; + if (!processError.isEmpty()) + display += processError + '\n'; + if (!astyleError.isEmpty()) { +#ifdef Q_OS_WIN + display += QString::fromLocal8Bit(astyleError) + '\n'; +#else + display += QString::fromUtf8(astyleError) + '\n'; +#endif + } + display += newContent; + ui->editDemo->document()->setText(display); } void FormatterGeneralWidget::updateCodeFormatter(Settings::CodeFormatter &format) diff --git a/RedPandaIDE/utils.cpp b/RedPandaIDE/utils.cpp index 2e3d8675..9fe3dadd 100644 --- a/RedPandaIDE/utils.cpp +++ b/RedPandaIDE/utils.cpp @@ -369,12 +369,16 @@ bool isGreenEdition() } #endif -QByteArray runAndGetOutput(const QString &cmd, const QString& workingDir, const QStringList& arguments, - const QByteArray &inputContent, bool inheritEnvironment, +ProcessOutput runAndGetOutput(const QString &cmd, const QString& workingDir, const QStringList& arguments, + const QByteArray &inputContent, + bool separateStderr, + bool inheritEnvironment, const QProcessEnvironment& env) { QProcess process; - QByteArray result; + QByteArray standardOutput; + QByteArray standardError; + QString errorMessage; bool errorOccurred = false; if (env.isEmpty()) { if (inheritEnvironment) { @@ -385,13 +389,21 @@ QByteArray runAndGetOutput(const QString &cmd, const QString& workingDir, const } else { process.setProcessEnvironment(env); } - process.setProcessChannelMode(QProcess::MergedChannels); + if (separateStderr) + process.setProcessChannelMode(QProcess::SeparateChannels); + else + process.setProcessChannelMode(QProcess::MergedChannels); process.setReadChannel(QProcess::StandardOutput); process.setWorkingDirectory(workingDir); process.connect(&process,&QProcess::readyReadStandardOutput, [&](){ - result.append(process.readAllStandardOutput()); + standardOutput.append(process.readAllStandardOutput()); }); + if (separateStderr) + process.connect(&process, &QProcess::readyReadStandardError, + [&]() { + standardError.append(process.readAllStandardError()); + }); process.connect(&process, &QProcess::errorOccurred, [&](){ errorOccurred= true; @@ -405,28 +417,27 @@ QByteArray runAndGetOutput(const QString &cmd, const QString& workingDir, const if (errorOccurred) { switch(process.error()) { case QProcess::FailedToStart: - result += "Failed to start process!"; + errorMessage += "Failed to start process!"; break; case QProcess::Crashed: - result += "Process crashed!"; + errorMessage += "Process crashed!"; break; case QProcess::Timedout: - result += "Timeout!"; + errorMessage += "Timeout!"; break; case QProcess::ReadError: - result += "Read Error:"; + errorMessage += "Read Error:"; break; case QProcess::WriteError: - result += "Write Error:"; + errorMessage += "Write Error:"; break; case QProcess::UnknownError: - result += "Unknown Error:"; + errorMessage += "Unknown Error:"; break; } - - //result += process.errorString().toLocal8Bit(); + errorMessage += process.errorString(); } - return result; + return {standardOutput, standardError, errorMessage}; } void executeFile(const QString &fileName, const QStringList ¶ms, const QString &workingDir, const QString &tempFile) @@ -772,19 +783,6 @@ std::tuple wrapCommandFor return wrapCommandForTerminalEmulator(terminal, parseArguments(argsPattern, Settings::Environment::terminalArgsPatternMagicVariables(), false), payloadArgsWithArgv0); } -QByteArray reformatContentUsingAstyle(const QByteArray &content, const QStringList &arguments) -{ - QProcessEnvironment env; - env.insert("LANG","en"); - QByteArray newContent = runAndGetOutput(pSettings->environment().AStylePath(), - extractFileDir(pSettings->environment().AStylePath()), - arguments, - content, - false, - env); - return newContent; -} - ExternalResource::ExternalResource() { Q_INIT_RESOURCE(qsynedit_qmake_qmake_qm_files); Q_INIT_RESOURCE(redpanda_qt_utils_qmake_qmake_qm_files); diff --git a/RedPandaIDE/utils.h b/RedPandaIDE/utils.h index b4b5d039..54e9dd06 100644 --- a/RedPandaIDE/utils.h +++ b/RedPandaIDE/utils.h @@ -143,13 +143,19 @@ void resetCppParser(std::shared_ptr parser, int compilerSetIndex=-1); int getNewFileNumber(); -QByteArray runAndGetOutput(const QString& cmd, const QString& workingDir, const QStringList& arguments, +struct ProcessOutput +{ + QByteArray standardOutput; + QByteArray standardError; + QString errorMessage; +}; + +ProcessOutput runAndGetOutput(const QString& cmd, const QString& workingDir, const QStringList& arguments, const QByteArray& inputContent = QByteArray(), + bool separateStderr = false, bool inheritEnvironment = false, const QProcessEnvironment& env = QProcessEnvironment() ); -QByteArray reformatContentUsingAstyle(const QByteArray& content, const QStringList& arguments); - void openFileFolderInExplorer(const QString& path); void executeFile(const QString& fileName,