summaryrefslogtreecommitdiff
path: root/src/plugins/beautifier/beautifierplugin.cpp
diff options
context:
space:
mode:
authorLorenz Haas <lykurg@gmail.com>2014-05-11 19:59:46 +0200
committerDavid Schulz <david.schulz@digia.com>2014-05-28 13:34:07 +0200
commitca433d9b2f20ad565ceecbae5357b4c81e580d9e (patch)
treead5e7828784c45a7c08e66a21f2391fa626e72ce /src/plugins/beautifier/beautifierplugin.cpp
parentaf5b3246cf080afdc0fa75fe7808d569fb5d6302 (diff)
downloadqt-creator-ca433d9b2f20ad565ceecbae5357b4c81e580d9e.tar.gz
Beautifier: Add new class Command and option to format text via piping
The tool's formatting command is now enclosed in Command. For the formatting itself it can be defined whether to use a temporary file or the pipe. Additionally, settings can return the current tool's version - if needed. Change-Id: I0e242c3e8016ed77cad92cc97a19fe3384dda858 Reviewed-by: David Schulz <david.schulz@digia.com>
Diffstat (limited to 'src/plugins/beautifier/beautifierplugin.cpp')
-rw-r--r--src/plugins/beautifier/beautifierplugin.cpp104
1 files changed, 71 insertions, 33 deletions
diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp
index 211623ad40..c588305028 100644
--- a/src/plugins/beautifier/beautifierplugin.cpp
+++ b/src/plugins/beautifier/beautifierplugin.cpp
@@ -30,6 +30,8 @@
#include "beautifierplugin.h"
#include "beautifierconstants.h"
+#include "command.h"
+
#include "artisticstyle/artisticstyle.h"
#include "clangformat/clangformat.h"
#include "uncrustify/uncrustify.h"
@@ -116,47 +118,83 @@ void BeautifierPlugin::updateActions(Core::IEditor *editor)
m_tools.at(i)->updateActions(editor);
}
-QString BeautifierPlugin::format(const QString &text, QStringList command, const QString &fileName)
+QString BeautifierPlugin::format(const QString &text, const Command &command, const QString &fileName)
{
- if (command.isEmpty())
+ const QString executable = command.executable();
+ if (executable.isEmpty())
return QString();
- // Save text to temporary file
- QFileInfo fi(fileName);
- Utils::TempFileSaver sourceFile(fi.absolutePath() + QLatin1String("/qtc_beautifier_XXXXXXXX.")
- + fi.suffix());
- sourceFile.setAutoRemove(true);
- sourceFile.write(text.toUtf8());
- if (!sourceFile.finalize()) {
- showError(tr("Cannot create temporary file \"%1\": %2.")
- .arg(sourceFile.fileName()).arg(sourceFile.errorString()));
- return QString();
- }
+ switch (command.processing()) {
+ case Command::FileProcessing: {
+ // Save text to temporary file
+ QFileInfo fi(fileName);
+ Utils::TempFileSaver sourceFile(QDir::tempPath() + QLatin1String("/qtc_beautifier_XXXXXXXX.")
+ + fi.suffix());
+ sourceFile.setAutoRemove(true);
+ sourceFile.write(text.toUtf8());
+ if (!sourceFile.finalize()) {
+ showError(tr("Cannot create temporary file \"%1\": %2.")
+ .arg(sourceFile.fileName()).arg(sourceFile.errorString()));
+ return QString();
+ }
- // Format temporary file
- QProcess process;
- command.replaceInStrings(QLatin1String("%file"), sourceFile.fileName());
- const QString processProgram = command.takeFirst();
- process.start(processProgram, command);
- if (!process.waitForFinished()) {
- showError(tr("Cannot call %1 or some other error occurred.").arg(processProgram));
- return QString();
+ // Format temporary file
+ QProcess process;
+ QStringList options = command.options();
+ options.replaceInStrings(QLatin1String("%file"), sourceFile.fileName());
+ process.start(executable, options);
+ if (!process.waitForFinished()) {
+ showError(tr("Cannot call %1 or some other error occurred.").arg(executable));
+ return QString();
+ }
+ const QByteArray output = process.readAllStandardError();
+ if (!output.isEmpty())
+ showError(executable + QLatin1String(": ") + QString::fromUtf8(output));
+
+ // Read text back
+ Utils::FileReader reader;
+ if (!reader.fetch(sourceFile.fileName(), QIODevice::Text)) {
+ showError(tr("Cannot read file \"%1\": %2.")
+ .arg(sourceFile.fileName()).arg(reader.errorString()));
+ return QString();
+ }
+ return QString::fromUtf8(reader.data());
+ } break;
+
+ case Command::PipeProcessing: {
+ QProcess process;
+ QStringList options = command.options();
+ options.replaceInStrings(QLatin1String("%file"), fileName);
+ process.start(executable, options);
+ if (!process.waitForStarted()) {
+ showError(tr("Cannot call %1 or some other error occurred.").arg(executable));
+ return QString();
+ }
+ process.write(text.toUtf8());
+ process.closeWriteChannel();
+ if (!process.waitForFinished()) {
+ showError(tr("Cannot call %1 or some other error occurred.").arg(executable));
+ return QString();
+ }
+ const QByteArray error = process.readAllStandardError();
+ if (!error.isEmpty()) {
+ showError(executable + QLatin1String(": ") + QString::fromUtf8(error));
+ return QString();
+ }
+
+ if (command.pipeAddsNewline()) {
+ QString formatted = QString::fromUtf8(process.readAllStandardOutput());
+ formatted.remove(QRegExp(QLatin1String("(\\r\\n|\\n)$")));
+ return formatted;
+ }
+ return QString::fromUtf8(process.readAllStandardOutput());
}
- const QByteArray output = process.readAllStandardError();
- if (!output.isEmpty())
- showError(processProgram + QLatin1String(": ") + QString::fromLocal8Bit(output));
-
- // Read text back
- Utils::FileReader reader;
- if (!reader.fetch(sourceFile.fileName(), QIODevice::Text)) {
- showError(tr("Cannot read file \"%1\": %2.")
- .arg(sourceFile.fileName()).arg(reader.errorString()));
- return QString();
}
- return QString::fromUtf8(reader.data());
+
+ return QString();
}
-void BeautifierPlugin::formatCurrentFile(QStringList command)
+void BeautifierPlugin::formatCurrentFile(const Command &command)
{
QPlainTextEdit *textEditor = 0;
if (TextEditor::BaseTextEditor *editor