| 92 | } |
| 93 | |
| 94 | QString CodeFormatter::runProcess(const QStringList &args) const |
| 95 | { |
| 96 | QProcess formatProcess; |
| 97 | formatProcess.start(getSetting("Program").toString(), args); |
| 98 | LOG_INFO(INFO_OF(formatProcess.program()) << INFO_OF(formatProcess.arguments().join(' '))); |
| 99 | |
| 100 | bool finished = formatProcess.waitForFinished(2000); // blocks main Thread |
| 101 | if (!finished) |
| 102 | { |
| 103 | formatProcess.kill(); |
| 104 | log->error(tr("Formatter"), |
| 105 | tr("The format process didn't finish in 2 seconds. This is probably because the %1 program is not " |
| 106 | "found by CP Editor. You can set the path to the program at %2.") |
| 107 | .arg(settingKey()) |
| 108 | .arg(SettingsManager::getPathText(settingKey() + "/Program")), |
| 109 | false); |
| 110 | return QString(); |
| 111 | } |
| 112 | |
| 113 | auto exitCode = formatProcess.exitCode(); |
| 114 | |
| 115 | if (exitCode != 0) |
| 116 | { |
| 117 | LOG_WARN(INFO_OF(exitCode)); |
| 118 | |
| 119 | log->warn(tr("Formatter"), tr("The format command [%1 %2] finished with exit code %3.") |
| 120 | .arg(formatProcess.program()) |
| 121 | .arg(formatProcess.arguments().join(' ')) |
| 122 | .arg(exitCode)); |
| 123 | auto stdOut = formatProcess.readAllStandardOutput(); |
| 124 | if (!stdOut.isEmpty()) |
| 125 | log->warn(tr("Formatter[stdout]"), stdOut); |
| 126 | auto stdError = formatProcess.readAllStandardError(); |
| 127 | if (!stdError.isEmpty()) |
| 128 | log->error(tr("Formatter[stderr]"), stdError); |
| 129 | return QString(); |
| 130 | } |
| 131 | |
| 132 | auto out = formatProcess.readAllStandardOutput(); |
| 133 | |
| 134 | if (out.isEmpty()) |
| 135 | { |
| 136 | LOG_WARN("Output is empty"); |
| 137 | log->warn(tr("Formatter"), tr("The output of the format process is empty. Please ensure there is no in-place " |
| 138 | "modification option in the formatting arguments.")); |
| 139 | } |
| 140 | |
| 141 | return out; |
| 142 | } |
| 143 | |
| 144 | bool CodeFormatter::formatSelectionOnly() const |
| 145 | { |