2016-01-15 14:57:40 +01:00
|
|
|
/****************************************************************************
|
2013-12-11 21:55:45 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 Lorenz Haas
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-12-11 21:55:45 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2013-12-11 21:55:45 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2013-12-11 21:55:45 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "beautifierplugin.h"
|
|
|
|
|
|
|
|
|
|
#include "beautifierconstants.h"
|
2015-09-17 21:27:25 +02:00
|
|
|
#include "generaloptionspage.h"
|
|
|
|
|
#include "generalsettings.h"
|
2014-05-11 19:59:46 +02:00
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
#include "artisticstyle/artisticstyle.h"
|
|
|
|
|
#include "clangformat/clangformat.h"
|
|
|
|
|
#include "uncrustify/uncrustify.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
#include <coreplugin/actionmanager/command.h>
|
|
|
|
|
#include <coreplugin/coreconstants.h>
|
2015-09-17 21:27:25 +02:00
|
|
|
#include <coreplugin/editormanager/documentmodel.h>
|
2013-12-11 21:55:45 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
|
|
|
|
#include <coreplugin/messagemanager.h>
|
2015-09-17 21:27:25 +02:00
|
|
|
#include <cppeditor/cppeditorconstants.h>
|
2013-12-11 21:55:45 +01:00
|
|
|
#include <diffeditor/differ.h>
|
2015-09-17 21:27:25 +02:00
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/projecttree.h>
|
2015-06-04 17:17:10 +02:00
|
|
|
#include <texteditor/convenience.h>
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/textdocument.h>
|
|
|
|
|
#include <texteditor/textdocumentlayout.h>
|
|
|
|
|
#include <texteditor/texteditor.h>
|
2015-09-17 21:27:25 +02:00
|
|
|
#include <texteditor/texteditorconstants.h>
|
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
#include <utils/mimetypes/mimedatabase.h>
|
2015-06-04 17:17:10 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2016-02-11 15:53:17 +01:00
|
|
|
#include <utils/runextensions.h>
|
2016-04-29 16:52:58 +02:00
|
|
|
#include <utils/synchronousprocess.h>
|
2013-12-11 21:55:45 +01:00
|
|
|
|
2014-05-28 14:05:58 +02:00
|
|
|
#include <QDir>
|
2013-12-11 21:55:45 +01:00
|
|
|
#include <QFileInfo>
|
2014-06-26 00:00:08 +02:00
|
|
|
#include <QFutureWatcher>
|
2015-02-26 13:38:54 +01:00
|
|
|
#include <QMenu>
|
2013-12-11 21:55:45 +01:00
|
|
|
#include <QPlainTextEdit>
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QScrollBar>
|
|
|
|
|
#include <QTextBlock>
|
|
|
|
|
|
2014-08-27 11:57:32 +02:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
namespace Beautifier {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
FormatTask format(FormatTask task)
|
2013-12-11 21:55:45 +01:00
|
|
|
{
|
2015-09-16 19:17:01 +02:00
|
|
|
task.error.clear();
|
|
|
|
|
task.formattedData.clear();
|
2013-12-11 21:55:45 +01:00
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
const QString executable = task.command.executable();
|
2014-05-11 19:59:46 +02:00
|
|
|
if (executable.isEmpty())
|
2015-09-16 19:17:01 +02:00
|
|
|
return task;
|
2013-12-11 21:55:45 +01:00
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
switch (task.command.processing()) {
|
2014-05-11 19:59:46 +02:00
|
|
|
case Command::FileProcessing: {
|
|
|
|
|
// Save text to temporary file
|
2015-09-16 19:17:01 +02:00
|
|
|
const QFileInfo fi(task.filePath);
|
2016-04-21 21:08:57 +02:00
|
|
|
Utils::TempFileSaver sourceFile(QDir::tempPath() + "/qtc_beautifier_XXXXXXXX."
|
2014-05-11 19:59:46 +02:00
|
|
|
+ fi.suffix());
|
|
|
|
|
sourceFile.setAutoRemove(true);
|
2015-09-16 19:17:01 +02:00
|
|
|
sourceFile.write(task.sourceData.toUtf8());
|
2014-05-11 19:59:46 +02:00
|
|
|
if (!sourceFile.finalize()) {
|
2016-04-26 15:30:23 +09:00
|
|
|
task.error = BeautifierPlugin::tr("Cannot create temporary file \"%1\": %2.")
|
2015-09-16 19:17:01 +02:00
|
|
|
.arg(sourceFile.fileName()).arg(sourceFile.errorString());
|
|
|
|
|
return task;
|
2014-05-11 19:59:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Format temporary file
|
2015-09-16 19:17:01 +02:00
|
|
|
QStringList options = task.command.options();
|
2016-04-29 16:52:58 +02:00
|
|
|
options.replaceInStrings(QLatin1String("%file"), sourceFile.fileName());
|
|
|
|
|
Utils::SynchronousProcess process;
|
|
|
|
|
process.setTimeoutS(5);
|
2016-05-26 12:12:01 +02:00
|
|
|
Utils::SynchronousProcessResponse response = process.runBlocking(executable, options);
|
2016-04-29 16:52:58 +02:00
|
|
|
if (response.result != Utils::SynchronousProcessResponse::Finished) {
|
2016-06-01 16:42:05 +09:00
|
|
|
task.error = BeautifierPlugin::tr("Failed to format: %1.").arg(response.exitMessage(executable, 5));
|
2015-09-16 19:17:01 +02:00
|
|
|
return task;
|
2014-05-11 19:59:46 +02:00
|
|
|
}
|
2016-04-29 16:52:58 +02:00
|
|
|
const QString output = response.stdErr;
|
2014-05-11 19:59:46 +02:00
|
|
|
if (!output.isEmpty())
|
2016-04-29 16:52:58 +02:00
|
|
|
task.error = executable + QLatin1String(": ") + output;
|
2014-05-11 19:59:46 +02:00
|
|
|
|
|
|
|
|
// Read text back
|
|
|
|
|
Utils::FileReader reader;
|
|
|
|
|
if (!reader.fetch(sourceFile.fileName(), QIODevice::Text)) {
|
2016-04-26 15:30:23 +09:00
|
|
|
task.error = BeautifierPlugin::tr("Cannot read file \"%1\": %2.")
|
2015-09-16 19:17:01 +02:00
|
|
|
.arg(sourceFile.fileName()).arg(reader.errorString());
|
|
|
|
|
return task;
|
2014-05-11 19:59:46 +02:00
|
|
|
}
|
2015-09-16 19:17:01 +02:00
|
|
|
task.formattedData = QString::fromUtf8(reader.data());
|
2016-04-21 21:08:57 +02:00
|
|
|
}
|
2016-04-29 16:52:58 +02:00
|
|
|
return task;
|
2014-05-11 19:59:46 +02:00
|
|
|
|
|
|
|
|
case Command::PipeProcessing: {
|
|
|
|
|
QProcess process;
|
2015-09-16 19:17:01 +02:00
|
|
|
QStringList options = task.command.options();
|
2016-04-21 21:08:57 +02:00
|
|
|
options.replaceInStrings("%filename", QFileInfo(task.filePath).fileName());
|
|
|
|
|
options.replaceInStrings("%file", task.filePath);
|
2014-05-11 19:59:46 +02:00
|
|
|
process.start(executable, options);
|
2014-06-26 00:00:08 +02:00
|
|
|
if (!process.waitForStarted(3000)) {
|
2016-04-26 15:30:23 +09:00
|
|
|
task.error = BeautifierPlugin::tr("Cannot call %1 or some other error occurred.")
|
2015-09-16 19:17:01 +02:00
|
|
|
.arg(executable);
|
|
|
|
|
return task;
|
2014-05-11 19:59:46 +02:00
|
|
|
}
|
2015-09-16 19:17:01 +02:00
|
|
|
process.write(task.sourceData.toUtf8());
|
2014-05-11 19:59:46 +02:00
|
|
|
process.closeWriteChannel();
|
2016-04-29 16:52:58 +02:00
|
|
|
if (!process.waitForFinished(5000) && process.state() == QProcess::Running) {
|
2014-06-26 00:00:08 +02:00
|
|
|
process.kill();
|
2016-04-26 15:30:23 +09:00
|
|
|
task.error = BeautifierPlugin::tr("Cannot call %1 or some other error occurred. Timeout "
|
2015-09-16 19:17:01 +02:00
|
|
|
"reached while formatting file %2.")
|
|
|
|
|
.arg(executable).arg(task.filePath);
|
|
|
|
|
return task;
|
2014-05-11 19:59:46 +02:00
|
|
|
}
|
2014-06-26 00:00:08 +02:00
|
|
|
const QByteArray errorText = process.readAllStandardError();
|
|
|
|
|
if (!errorText.isEmpty()) {
|
2015-09-16 19:17:01 +02:00
|
|
|
task.error = QString::fromLatin1("%1: %2").arg(executable)
|
|
|
|
|
.arg(QString::fromUtf8(errorText));
|
|
|
|
|
return task;
|
2014-05-11 19:59:46 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
const bool addsNewline = task.command.pipeAddsNewline();
|
|
|
|
|
const bool returnsCRLF = task.command.returnsCRLF();
|
2014-07-09 08:59:38 +02:00
|
|
|
if (addsNewline || returnsCRLF) {
|
2015-09-16 19:17:01 +02:00
|
|
|
task.formattedData = QString::fromUtf8(process.readAllStandardOutput());
|
2014-07-09 08:59:38 +02:00
|
|
|
if (addsNewline)
|
2016-04-21 21:08:57 +02:00
|
|
|
task.formattedData.remove(QRegExp("(\\r\\n|\\n)$"));
|
2014-07-09 08:59:38 +02:00
|
|
|
if (returnsCRLF)
|
2016-04-21 21:08:57 +02:00
|
|
|
task.formattedData.replace("\r\n", "\n");
|
2015-09-16 19:17:01 +02:00
|
|
|
return task;
|
2014-05-11 19:59:46 +02:00
|
|
|
}
|
2015-09-16 19:17:01 +02:00
|
|
|
task.formattedData = QString::fromUtf8(process.readAllStandardOutput());
|
|
|
|
|
return task;
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-05-11 19:59:46 +02:00
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
return task;
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
QString sourceData(TextEditorWidget *editor, int startPos, int endPos)
|
2013-12-11 21:55:45 +01:00
|
|
|
{
|
2015-09-16 19:17:01 +02:00
|
|
|
return (startPos < 0)
|
|
|
|
|
? editor->toPlainText()
|
|
|
|
|
: Convenience::textAt(editor->textCursor(), startPos, (endPos - startPos));
|
|
|
|
|
}
|
2015-06-04 17:17:10 +02:00
|
|
|
|
2016-05-19 21:30:27 +02:00
|
|
|
bool isAutoFormatApplicable(const Core::IDocument *document,
|
|
|
|
|
const QList<Utils::MimeType> &allowedMimeTypes)
|
2015-09-17 21:27:25 +02:00
|
|
|
{
|
2016-05-19 21:30:27 +02:00
|
|
|
if (!document)
|
|
|
|
|
return false;
|
|
|
|
|
|
2015-09-17 21:27:25 +02:00
|
|
|
if (allowedMimeTypes.isEmpty())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
const Utils::MimeDatabase mdb;
|
2016-05-19 21:30:27 +02:00
|
|
|
const Utils::MimeType documentMimeType = mdb.mimeTypeForName(document->mimeType());
|
|
|
|
|
return Utils::anyOf(allowedMimeTypes, [&documentMimeType](const Utils::MimeType &mime) {
|
|
|
|
|
return documentMimeType.inherits(mime.name());
|
|
|
|
|
});
|
2015-09-17 21:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
bool BeautifierPlugin::initialize(const QStringList &arguments, QString *errorString)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(arguments)
|
|
|
|
|
Q_UNUSED(errorString)
|
|
|
|
|
|
|
|
|
|
Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::MENU_ID);
|
|
|
|
|
menu->menu()->setTitle(QCoreApplication::translate("Beautifier", Constants::OPTION_TR_CATEGORY));
|
2016-03-26 16:28:08 +01:00
|
|
|
menu->setOnAllDisabledBehavior(Core::ActionContainer::Show);
|
2015-09-16 19:17:01 +02:00
|
|
|
Core::ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu);
|
|
|
|
|
|
2015-09-17 21:27:25 +02:00
|
|
|
m_tools << new ArtisticStyle::ArtisticStyle(this);
|
|
|
|
|
m_tools << new ClangFormat::ClangFormat(this);
|
|
|
|
|
m_tools << new Uncrustify::Uncrustify(this);
|
|
|
|
|
|
|
|
|
|
QStringList toolIds;
|
|
|
|
|
toolIds.reserve(m_tools.count());
|
2016-04-21 21:08:57 +02:00
|
|
|
for (BeautifierAbstractTool *tool : m_tools) {
|
2015-09-17 21:27:25 +02:00
|
|
|
toolIds << tool->id();
|
2015-09-16 19:17:01 +02:00
|
|
|
tool->initialize();
|
|
|
|
|
const QList<QObject *> autoReleasedObjects = tool->autoReleaseObjects();
|
2016-04-21 21:08:57 +02:00
|
|
|
for (QObject *object : autoReleasedObjects)
|
2015-09-16 19:17:01 +02:00
|
|
|
addAutoReleasedObject(object);
|
2015-06-04 17:17:10 +02:00
|
|
|
}
|
2015-09-16 19:17:01 +02:00
|
|
|
|
2015-09-17 21:27:25 +02:00
|
|
|
m_generalSettings = new GeneralSettings;
|
|
|
|
|
auto settingsPage = new GeneralOptionsPage(m_generalSettings, toolIds, this);
|
|
|
|
|
addAutoReleasedObject(settingsPage);
|
|
|
|
|
|
2016-03-26 16:28:08 +01:00
|
|
|
updateActions();
|
2015-09-16 19:17:01 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BeautifierPlugin::extensionsInitialized()
|
|
|
|
|
{
|
2015-09-17 21:27:25 +02:00
|
|
|
const Core::EditorManager *editorManager = Core::EditorManager::instance();
|
|
|
|
|
connect(editorManager, &Core::EditorManager::currentEditorChanged,
|
|
|
|
|
this, &BeautifierPlugin::updateActions);
|
|
|
|
|
connect(editorManager, &Core::EditorManager::aboutToSave,
|
|
|
|
|
this, &BeautifierPlugin::autoFormatOnSave);
|
2015-09-16 19:17:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExtensionSystem::IPlugin::ShutdownFlag BeautifierPlugin::aboutToShutdown()
|
|
|
|
|
{
|
|
|
|
|
return SynchronousShutdown;
|
2014-06-26 00:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
void BeautifierPlugin::updateActions(Core::IEditor *editor)
|
2014-06-26 00:00:08 +02:00
|
|
|
{
|
2016-04-21 21:08:57 +02:00
|
|
|
for (BeautifierAbstractTool *tool : m_tools)
|
2015-09-16 19:17:01 +02:00
|
|
|
tool->updateActions(editor);
|
2014-06-26 00:00:08 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-17 21:27:25 +02:00
|
|
|
void BeautifierPlugin::autoFormatOnSave(Core::IDocument *document)
|
|
|
|
|
{
|
|
|
|
|
if (!m_generalSettings->autoFormatOnSave())
|
|
|
|
|
return;
|
|
|
|
|
|
2016-05-19 21:30:27 +02:00
|
|
|
if (!isAutoFormatApplicable(document, m_generalSettings->autoFormatMime()))
|
2015-09-17 21:27:25 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Check if file is contained in the current project (if wished)
|
|
|
|
|
if (m_generalSettings->autoFormatOnlyCurrentProject()) {
|
|
|
|
|
const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject();
|
2016-05-19 21:30:27 +02:00
|
|
|
if (!pro || !pro->files(ProjectExplorer::Project::SourceFiles).contains(
|
|
|
|
|
document->filePath().toString())) {
|
2015-09-17 21:27:25 +02:00
|
|
|
return;
|
2016-05-19 21:30:27 +02:00
|
|
|
}
|
2015-09-17 21:27:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find tool to use by id and format file!
|
|
|
|
|
const QString id = m_generalSettings->autoFormatTool();
|
|
|
|
|
auto tool = std::find_if(m_tools.constBegin(), m_tools.constEnd(),
|
|
|
|
|
[&id](const BeautifierAbstractTool *t){return t->id() == id;});
|
|
|
|
|
if (tool != m_tools.constEnd()) {
|
2016-05-19 21:30:27 +02:00
|
|
|
if (!(*tool)->isApplicable(document))
|
|
|
|
|
return;
|
2015-09-17 21:27:25 +02:00
|
|
|
const Command command = (*tool)->command();
|
|
|
|
|
if (!command.isValid())
|
|
|
|
|
return;
|
|
|
|
|
const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForDocument(document);
|
|
|
|
|
if (editors.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
if (TextEditorWidget* widget = qobject_cast<TextEditorWidget *>(editors.first()->widget()))
|
|
|
|
|
formatEditor(widget, command);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
void BeautifierPlugin::formatCurrentFile(const Command &command, int startPos, int endPos)
|
2014-06-26 00:00:08 +02:00
|
|
|
{
|
2015-09-16 19:17:01 +02:00
|
|
|
if (TextEditorWidget *editor = TextEditorWidget::currentTextEditorWidget())
|
|
|
|
|
formatEditorAsync(editor, command, startPos, endPos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Formats the text of @a editor using @a command. @a startPos and @a endPos specifies the range of
|
|
|
|
|
* the editor's text that will be formatted. If @a startPos is negative the editor's entire text is
|
|
|
|
|
* formatted.
|
|
|
|
|
*
|
|
|
|
|
* @pre @a endPos must be greater than or equal to @a startPos
|
|
|
|
|
*/
|
|
|
|
|
void BeautifierPlugin::formatEditor(TextEditorWidget *editor, const Command &command, int startPos,
|
|
|
|
|
int endPos)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(startPos <= endPos, return);
|
|
|
|
|
|
|
|
|
|
const QString sd = sourceData(editor, startPos, endPos);
|
|
|
|
|
if (sd.isEmpty())
|
2014-06-26 00:00:08 +02:00
|
|
|
return;
|
2015-09-16 19:17:01 +02:00
|
|
|
checkAndApplyTask(format(FormatTask(editor, editor->textDocument()->filePath().toString(), sd,
|
|
|
|
|
command, startPos, endPos)));
|
|
|
|
|
}
|
2014-06-26 00:00:08 +02:00
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
/**
|
|
|
|
|
* Behaves like formatEditor except that the formatting is done asynchronously.
|
|
|
|
|
*/
|
|
|
|
|
void BeautifierPlugin::formatEditorAsync(TextEditorWidget *editor, const Command &command,
|
|
|
|
|
int startPos, int endPos)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(startPos <= endPos, return);
|
|
|
|
|
|
|
|
|
|
const QString sd = sourceData(editor, startPos, endPos);
|
|
|
|
|
if (sd.isEmpty())
|
2014-06-26 00:00:08 +02:00
|
|
|
return;
|
|
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
QFutureWatcher<FormatTask> *watcher = new QFutureWatcher<FormatTask>;
|
|
|
|
|
const TextDocument *doc = editor->textDocument();
|
|
|
|
|
connect(doc, &TextDocument::contentsChanged, watcher, &QFutureWatcher<FormatTask>::cancel);
|
2016-06-01 18:12:51 +02:00
|
|
|
connect(watcher, &QFutureWatcherBase::finished, [this, watcher] {
|
2015-09-16 19:17:01 +02:00
|
|
|
if (watcher->isCanceled())
|
|
|
|
|
showError(tr("File was modified."));
|
|
|
|
|
else
|
|
|
|
|
checkAndApplyTask(watcher->result());
|
|
|
|
|
watcher->deleteLater();
|
|
|
|
|
});
|
|
|
|
|
watcher->setFuture(Utils::runAsync(&format, FormatTask(editor, doc->filePath().toString(), sd,
|
|
|
|
|
command, startPos, endPos)));
|
|
|
|
|
}
|
2014-06-26 00:00:08 +02:00
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
/**
|
|
|
|
|
* Checks the state of @a task and if the formatting was successful calls updateEditorText() with
|
|
|
|
|
* the respective members of @a task.
|
|
|
|
|
*/
|
|
|
|
|
void BeautifierPlugin::checkAndApplyTask(const FormatTask &task)
|
|
|
|
|
{
|
|
|
|
|
if (!task.error.isEmpty()) {
|
|
|
|
|
showError(task.error);
|
2014-06-26 00:00:08 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:17:10 +02:00
|
|
|
if (task.formattedData.isEmpty()) {
|
|
|
|
|
showError(tr("Could not format file %1.").arg(task.filePath));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-26 00:00:08 +02:00
|
|
|
QPlainTextEdit *textEditor = task.editor;
|
|
|
|
|
if (!textEditor) {
|
|
|
|
|
showError(tr("File %1 was closed.").arg(task.filePath));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-04 17:17:10 +02:00
|
|
|
const QString formattedData = (task.startPos < 0)
|
|
|
|
|
? task.formattedData
|
2015-09-16 19:17:01 +02:00
|
|
|
: QString(textEditor->toPlainText()).replace(
|
|
|
|
|
task.startPos, (task.endPos - task.startPos), task.formattedData);
|
|
|
|
|
|
|
|
|
|
updateEditorText(textEditor, formattedData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the text of @a editor to @a text. Instead of replacing the entire text, however, only the
|
|
|
|
|
* actually changed parts are updated while preserving the cursor position, the folded
|
|
|
|
|
* blocks, and the scroll bar position.
|
|
|
|
|
*/
|
|
|
|
|
void BeautifierPlugin::updateEditorText(QPlainTextEdit *editor, const QString &text)
|
|
|
|
|
{
|
|
|
|
|
const QString editorText = editor->toPlainText();
|
|
|
|
|
if (editorText == text)
|
2013-12-11 21:55:45 +01:00
|
|
|
return;
|
|
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
// Calculate diff
|
|
|
|
|
DiffEditor::Differ differ;
|
|
|
|
|
const QList<DiffEditor::Diff> diff = differ.diff(editorText, text);
|
2015-06-04 17:17:10 +02:00
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
// Since QTextCursor does not work properly with folded blocks, all blocks must be unfolded.
|
|
|
|
|
// To restore the current state at the end, keep track of which block is folded.
|
|
|
|
|
QList<int> foldedBlocks;
|
2015-09-16 19:17:01 +02:00
|
|
|
QTextBlock block = editor->document()->firstBlock();
|
2013-12-11 21:55:45 +01:00
|
|
|
while (block.isValid()) {
|
2014-08-27 11:57:32 +02:00
|
|
|
if (const TextBlockUserData *userdata = static_cast<TextBlockUserData *>(block.userData())) {
|
2013-12-11 21:55:45 +01:00
|
|
|
if (userdata->folded()) {
|
|
|
|
|
foldedBlocks << block.blockNumber();
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::doFoldOrUnfold(block, true);
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
block = block.next();
|
|
|
|
|
}
|
2015-09-16 19:17:01 +02:00
|
|
|
editor->update();
|
2013-12-11 21:55:45 +01:00
|
|
|
|
|
|
|
|
// Save the current viewport position of the cursor to ensure the same vertical position after
|
|
|
|
|
// the formatted text has set to the editor.
|
2015-09-16 19:17:01 +02:00
|
|
|
int absoluteVerticalCursorOffset = editor->cursorRect().y();
|
2013-12-11 21:55:45 +01:00
|
|
|
|
|
|
|
|
// Update changed lines and keep track of the cursor position
|
2015-09-16 19:17:01 +02:00
|
|
|
QTextCursor cursor = editor->textCursor();
|
2013-12-11 21:55:45 +01:00
|
|
|
int charactersInfrontOfCursor = cursor.position();
|
|
|
|
|
int newCursorPos = charactersInfrontOfCursor;
|
|
|
|
|
cursor.beginEditBlock();
|
|
|
|
|
cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
|
2016-04-21 21:08:57 +02:00
|
|
|
for (const DiffEditor::Diff &d : diff) {
|
2013-12-11 21:55:45 +01:00
|
|
|
switch (d.command) {
|
|
|
|
|
case DiffEditor::Diff::Insert:
|
|
|
|
|
{
|
|
|
|
|
// Adjust cursor position if we do work in front of the cursor.
|
|
|
|
|
if (charactersInfrontOfCursor > 0) {
|
|
|
|
|
const int size = d.text.size();
|
|
|
|
|
charactersInfrontOfCursor += size;
|
|
|
|
|
newCursorPos += size;
|
|
|
|
|
}
|
|
|
|
|
// Adjust folded blocks, if a new block is added.
|
2016-04-21 21:08:57 +02:00
|
|
|
if (d.text.contains('\n')) {
|
|
|
|
|
const int newLineCount = d.text.count('\n');
|
2013-12-11 21:55:45 +01:00
|
|
|
const int number = cursor.blockNumber();
|
|
|
|
|
const int total = foldedBlocks.size();
|
|
|
|
|
for (int i = 0; i < total; ++i) {
|
|
|
|
|
if (foldedBlocks.at(i) > number)
|
|
|
|
|
foldedBlocks[i] += newLineCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cursor.insertText(d.text);
|
|
|
|
|
break;
|
2016-04-21 21:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
case DiffEditor::Diff::Delete:
|
|
|
|
|
{
|
|
|
|
|
// Adjust cursor position if we do work in front of the cursor.
|
|
|
|
|
if (charactersInfrontOfCursor > 0) {
|
|
|
|
|
const int size = d.text.size();
|
|
|
|
|
charactersInfrontOfCursor -= size;
|
|
|
|
|
newCursorPos -= size;
|
|
|
|
|
// Cursor was inside the deleted text, so adjust the new cursor position
|
|
|
|
|
if (charactersInfrontOfCursor < 0)
|
|
|
|
|
newCursorPos -= charactersInfrontOfCursor;
|
|
|
|
|
}
|
|
|
|
|
// Adjust folded blocks, if at least one block is being deleted.
|
2016-04-21 21:08:57 +02:00
|
|
|
if (d.text.contains('\n')) {
|
|
|
|
|
const int newLineCount = d.text.count('\n');
|
2013-12-11 21:55:45 +01:00
|
|
|
const int number = cursor.blockNumber();
|
|
|
|
|
for (int i = 0, total = foldedBlocks.size(); i < total; ++i) {
|
|
|
|
|
if (foldedBlocks.at(i) > number) {
|
|
|
|
|
foldedBlocks[i] -= newLineCount;
|
|
|
|
|
if (foldedBlocks[i] < number) {
|
|
|
|
|
foldedBlocks.removeAt(i);
|
|
|
|
|
--i;
|
|
|
|
|
--total;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, d.text.size());
|
|
|
|
|
cursor.removeSelectedText();
|
|
|
|
|
break;
|
2016-04-21 21:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
case DiffEditor::Diff::Equal:
|
|
|
|
|
// Adjust cursor position
|
|
|
|
|
charactersInfrontOfCursor -= d.text.size();
|
|
|
|
|
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, d.text.size());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cursor.endEditBlock();
|
|
|
|
|
cursor.setPosition(newCursorPos);
|
2015-09-16 19:17:01 +02:00
|
|
|
editor->setTextCursor(cursor);
|
2013-12-11 21:55:45 +01:00
|
|
|
|
|
|
|
|
// Adjust vertical scrollbar
|
2015-09-16 19:17:01 +02:00
|
|
|
absoluteVerticalCursorOffset = editor->cursorRect().y() - absoluteVerticalCursorOffset;
|
|
|
|
|
const double fontHeight = QFontMetrics(editor->document()->defaultFont()).height();
|
|
|
|
|
editor->verticalScrollBar()->setValue(editor->verticalScrollBar()->value()
|
2013-12-11 21:55:45 +01:00
|
|
|
+ absoluteVerticalCursorOffset / fontHeight);
|
|
|
|
|
// Restore folded blocks
|
2015-09-16 19:17:01 +02:00
|
|
|
const QTextDocument *doc = editor->document();
|
2016-04-21 21:08:57 +02:00
|
|
|
for (int blockId : foldedBlocks) {
|
2015-06-21 19:12:02 +02:00
|
|
|
const QTextBlock block = doc->findBlockByNumber(qMax(0, blockId));
|
2013-12-11 21:55:45 +01:00
|
|
|
if (block.isValid())
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout::doFoldOrUnfold(block, false);
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
2015-09-16 19:17:01 +02:00
|
|
|
editor->document()->setModified(true);
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BeautifierPlugin::showError(const QString &error)
|
|
|
|
|
{
|
2014-03-06 14:41:33 +01:00
|
|
|
Core::MessageManager::write(tr("Error in Beautifier: %1").arg(error.trimmed()));
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
2014-03-05 14:02:01 +01:00
|
|
|
QString BeautifierPlugin::msgCannotGetConfigurationFile(const QString &command)
|
|
|
|
|
{
|
|
|
|
|
return tr("Cannot get configuration file for %1.").arg(command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BeautifierPlugin::msgFormatCurrentFile()
|
|
|
|
|
{
|
|
|
|
|
//: Menu entry
|
|
|
|
|
return tr("Format Current File");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BeautifierPlugin::msgFormatSelectedText()
|
|
|
|
|
{
|
|
|
|
|
//: Menu entry
|
|
|
|
|
return tr("Format Selected Text");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BeautifierPlugin::msgCommandPromptDialogTitle(const QString &command)
|
|
|
|
|
{
|
|
|
|
|
//: File dialog title for path chooser when choosing binary
|
|
|
|
|
return tr("%1 Command").arg(command);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Beautifier
|