| 101 | } |
| 102 | |
| 103 | QString CustomScriptPlugin::formatSourceWithStyle(const SourceFormatterStyle& style, const QString& text, |
| 104 | const QUrl& url, const QMimeType& mime, const QString& leftContext, |
| 105 | const QString& rightContext) const |
| 106 | { |
| 107 | KProcess proc; |
| 108 | QTextStream ios(&proc); |
| 109 | |
| 110 | std::unique_ptr<QTemporaryFile> tmpFile; |
| 111 | |
| 112 | QString styleContent = style.content(); |
| 113 | if (styleContent.isEmpty()) { |
| 114 | styleContent = predefinedStyle(style.name()).content(); |
| 115 | if (styleContent.isEmpty()) { |
| 116 | qCWarning(CUSTOMSCRIPT) << "Empty contents for style" << style.name() << "for indent plugin"; |
| 117 | return text; |
| 118 | } |
| 119 | } |
| 120 | // NOTE: from now on, only one member function of @p style may be called: name(), because only the |
| 121 | // name of an incomplete style is guaranteed to match that of the corresponding predefined style. |
| 122 | |
| 123 | QString useText = text; |
| 124 | useText = leftContext + useText + rightContext; |
| 125 | |
| 126 | QMap<QString, QString> projectVariables; |
| 127 | const auto projects = ICore::self()->projectController()->projects(); |
| 128 | for (IProject* project : projects) { |
| 129 | projectVariables[project->name()] = project->path().toUrl().toLocalFile(); |
| 130 | } |
| 131 | |
| 132 | QString command = styleContent; |
| 133 | |
| 134 | // Replace ${<project name>} with the project path |
| 135 | command = replaceVariables(command, projectVariables); |
| 136 | command.replace(QLatin1String("$FILE"), url.toLocalFile()); |
| 137 | |
| 138 | if (command.contains(QLatin1String("$TMPFILE"))) { |
| 139 | tmpFile.reset(new QTemporaryFile(QDir::tempPath() + QLatin1String("/code"))); |
| 140 | if (tmpFile->open()) { |
| 141 | qCDebug(CUSTOMSCRIPT) << "using temporary file" << tmpFile->fileName(); |
| 142 | command.replace(QLatin1String("$TMPFILE"), tmpFile->fileName()); |
| 143 | QByteArray useTextArray = useText.toLocal8Bit(); |
| 144 | if (tmpFile->write(useTextArray) != useTextArray.size()) { |
| 145 | qCWarning(CUSTOMSCRIPT) << "failed to write text to temporary file"; |
| 146 | return text; |
| 147 | } |
| 148 | } else { |
| 149 | qCWarning(CUSTOMSCRIPT) << "Failed to create a temporary file"; |
| 150 | return text; |
| 151 | } |
| 152 | tmpFile->close(); |
| 153 | } |
| 154 | |
| 155 | qCDebug(CUSTOMSCRIPT) << "using shell command for indentation: " << command; |
| 156 | proc.setShellCommand(command); |
| 157 | proc.setOutputChannelMode(KProcess::OnlyStdoutChannel); |
| 158 | |
| 159 | proc.start(); |
| 160 | if (!proc.waitForStarted()) { |
nothing calls this directly
no test coverage detected