beautifierplugin.cpp 19 KB
Newer Older
1
/****************************************************************************
Lorenz Haas's avatar
Lorenz Haas committed
2
**
3 4
** Copyright (C) 2016 Lorenz Haas
** Contact: https://www.qt.io/licensing/
Lorenz Haas's avatar
Lorenz Haas committed
5 6 7 8 9 10 11
**
** 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
12 13 14
** 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.
Lorenz Haas's avatar
Lorenz Haas committed
15
**
16 17 18 19 20 21 22
** 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.
Lorenz Haas's avatar
Lorenz Haas committed
23 24 25 26 27 28
**
****************************************************************************/

#include "beautifierplugin.h"

#include "beautifierconstants.h"
29 30
#include "generaloptionspage.h"
#include "generalsettings.h"
31

Lorenz Haas's avatar
Lorenz Haas committed
32 33 34 35 36 37 38 39
#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>
40
#include <coreplugin/editormanager/documentmodel.h>
Lorenz Haas's avatar
Lorenz Haas committed
41 42 43
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/messagemanager.h>
44
#include <cppeditor/cppeditorconstants.h>
Lorenz Haas's avatar
Lorenz Haas committed
45
#include <diffeditor/differ.h>
46 47
#include <projectexplorer/project.h>
#include <projectexplorer/projecttree.h>
48
#include <texteditor/convenience.h>
49 50 51
#include <texteditor/textdocument.h>
#include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditor.h>
52 53 54 55
#include <texteditor/texteditorconstants.h>
#include <utils/algorithm.h>
#include <utils/fileutils.h>
#include <utils/mimetypes/mimedatabase.h>
56
#include <utils/qtcassert.h>
Eike Ziller's avatar
Eike Ziller committed
57
#include <utils/runextensions.h>
58
#include <utils/synchronousprocess.h>
59
#include <utils/temporarydirectory.h>
Lorenz Haas's avatar
Lorenz Haas committed
60

hjk's avatar
hjk committed
61
#include <QDir>
Lorenz Haas's avatar
Lorenz Haas committed
62
#include <QFileInfo>
63
#include <QFutureWatcher>
64
#include <QMenu>
Lorenz Haas's avatar
Lorenz Haas committed
65 66 67 68 69
#include <QPlainTextEdit>
#include <QProcess>
#include <QScrollBar>
#include <QTextBlock>

70 71
using namespace TextEditor;

Lorenz Haas's avatar
Lorenz Haas committed
72 73 74
namespace Beautifier {
namespace Internal {

75
FormatTask format(FormatTask task)
Lorenz Haas's avatar
Lorenz Haas committed
76
{
77 78
    task.error.clear();
    task.formattedData.clear();
Lorenz Haas's avatar
Lorenz Haas committed
79

80
    const QString executable = task.command.executable();
81
    if (executable.isEmpty())
82
        return task;
Lorenz Haas's avatar
Lorenz Haas committed
83

84
    switch (task.command.processing()) {
85 86
    case Command::FileProcessing: {
        // Save text to temporary file
87
        const QFileInfo fi(task.filePath);
88 89
        Utils::TempFileSaver sourceFile(Utils::TemporaryDirectory::masterDirectoryPath()
                                        + "/qtc_beautifier_XXXXXXXX."
90 91
                                        + fi.suffix());
        sourceFile.setAutoRemove(true);
92
        sourceFile.write(task.sourceData.toUtf8());
93
        if (!sourceFile.finalize()) {
Takumi ASAKI's avatar
Takumi ASAKI committed
94
            task.error = BeautifierPlugin::tr("Cannot create temporary file \"%1\": %2.")
95 96
                    .arg(sourceFile.fileName()).arg(sourceFile.errorString());
            return task;
97 98 99
        }

        // Format temporary file
100
        QStringList options = task.command.options();
101 102 103
        options.replaceInStrings(QLatin1String("%file"), sourceFile.fileName());
        Utils::SynchronousProcess process;
        process.setTimeoutS(5);
104
        Utils::SynchronousProcessResponse response = process.runBlocking(executable, options);
105
        if (response.result != Utils::SynchronousProcessResponse::Finished) {
106
            task.error = BeautifierPlugin::tr("Failed to format: %1.").arg(response.exitMessage(executable, 5));
107
            return task;
108
        }
109
        const QString output = response.stdErr();
110
        if (!output.isEmpty())
111
            task.error = executable + QLatin1String(": ") + output;
112 113 114 115

        // Read text back
        Utils::FileReader reader;
        if (!reader.fetch(sourceFile.fileName(), QIODevice::Text)) {
Takumi ASAKI's avatar
Takumi ASAKI committed
116
            task.error = BeautifierPlugin::tr("Cannot read file \"%1\": %2.")
117 118
                    .arg(sourceFile.fileName()).arg(reader.errorString());
            return task;
119
        }
120
        task.formattedData = QString::fromUtf8(reader.data());
121
    }
122
    return task;
123 124 125

    case Command::PipeProcessing: {
        QProcess process;
126
        QStringList options = task.command.options();
127 128
        options.replaceInStrings("%filename", QFileInfo(task.filePath).fileName());
        options.replaceInStrings("%file", task.filePath);
129
        process.start(executable, options);
130
        if (!process.waitForStarted(3000)) {
Takumi ASAKI's avatar
Takumi ASAKI committed
131
            task.error = BeautifierPlugin::tr("Cannot call %1 or some other error occurred.")
132 133
                    .arg(executable);
            return task;
134
        }
135
        process.write(task.sourceData.toUtf8());
136
        process.closeWriteChannel();
137
        if (!process.waitForFinished(5000) && process.state() == QProcess::Running) {
138
            process.kill();
Takumi ASAKI's avatar
Takumi ASAKI committed
139
            task.error = BeautifierPlugin::tr("Cannot call %1 or some other error occurred. Timeout "
140 141 142
                                     "reached while formatting file %2.")
                    .arg(executable).arg(task.filePath);
            return task;
143
        }
144 145
        const QByteArray errorText = process.readAllStandardError();
        if (!errorText.isEmpty()) {
146 147 148
            task.error = QString::fromLatin1("%1: %2").arg(executable)
                    .arg(QString::fromUtf8(errorText));
            return task;
149 150
        }

151 152
        const bool addsNewline = task.command.pipeAddsNewline();
        const bool returnsCRLF = task.command.returnsCRLF();
153
        if (addsNewline || returnsCRLF) {
154
            task.formattedData = QString::fromUtf8(process.readAllStandardOutput());
Lorenz Haas's avatar
Lorenz Haas committed
155 156 157 158 159
            if (addsNewline && task.formattedData.endsWith('\n')) {
                task.formattedData.chop(1);
                if (task.formattedData.endsWith('\r'))
                    task.formattedData.chop(1);
            }
160
            if (returnsCRLF)
161
                task.formattedData.replace("\r\n", "\n");
162
            return task;
163
        }
164 165
        task.formattedData = QString::fromUtf8(process.readAllStandardOutput());
        return task;
Lorenz Haas's avatar
Lorenz Haas committed
166 167
    }
    }
168

169
    return task;
Lorenz Haas's avatar
Lorenz Haas committed
170 171
}

172
QString sourceData(TextEditorWidget *editor, int startPos, int endPos)
Lorenz Haas's avatar
Lorenz Haas committed
173
{
174 175 176 177
    return (startPos < 0)
            ? editor->toPlainText()
            : Convenience::textAt(editor->textCursor(), startPos, (endPos - startPos));
}
178

179 180
bool isAutoFormatApplicable(const Core::IDocument *document,
                            const QList<Utils::MimeType> &allowedMimeTypes)
181
{
182 183 184
    if (!document)
        return false;

185 186 187 188
    if (allowedMimeTypes.isEmpty())
        return true;

    const Utils::MimeDatabase mdb;
189 190 191 192
    const Utils::MimeType documentMimeType = mdb.mimeTypeForName(document->mimeType());
    return Utils::anyOf(allowedMimeTypes, [&documentMimeType](const Utils::MimeType &mime) {
        return documentMimeType.inherits(mime.name());
    });
193 194
}

195 196 197 198 199 200 201
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));
202
    menu->setOnAllDisabledBehavior(Core::ActionContainer::Show);
203
    Core::ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu);
204 205
    return true;
}
206

207 208
void BeautifierPlugin::extensionsInitialized()
{
209 210 211 212 213 214
    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());
215
    for (BeautifierAbstractTool *tool : m_tools) {
216
        toolIds << tool->id();
217 218
        tool->initialize();
        const QList<QObject *> autoReleasedObjects = tool->autoReleaseObjects();
219
        for (QObject *object : autoReleasedObjects)
220
            addAutoReleasedObject(object);
221
    }
222

223 224 225 226
    m_generalSettings = new GeneralSettings;
    auto settingsPage = new GeneralOptionsPage(m_generalSettings, toolIds, this);
    addAutoReleasedObject(settingsPage);

227
    updateActions();
228

229 230 231 232 233
    const Core::EditorManager *editorManager = Core::EditorManager::instance();
    connect(editorManager, &Core::EditorManager::currentEditorChanged,
            this, &BeautifierPlugin::updateActions);
    connect(editorManager, &Core::EditorManager::aboutToSave,
            this, &BeautifierPlugin::autoFormatOnSave);
234 235 236 237 238
}

ExtensionSystem::IPlugin::ShutdownFlag BeautifierPlugin::aboutToShutdown()
{
    return SynchronousShutdown;
239 240
}

241
void BeautifierPlugin::updateActions(Core::IEditor *editor)
242
{
243
    for (BeautifierAbstractTool *tool : m_tools)
244
        tool->updateActions(editor);
245 246
}

247 248 249 250 251
void BeautifierPlugin::autoFormatOnSave(Core::IDocument *document)
{
    if (!m_generalSettings->autoFormatOnSave())
        return;

252
    if (!isAutoFormatApplicable(document, m_generalSettings->autoFormatMime()))
253 254 255 256 257
        return;

    // Check if file is contained in the current project (if wished)
    if (m_generalSettings->autoFormatOnlyCurrentProject()) {
        const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject();
258 259
        if (!pro || !pro->files(ProjectExplorer::Project::SourceFiles).contains(
                    document->filePath().toString())) {
260
            return;
261
        }
262 263 264 265 266 267 268
    }

    // 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()) {
269 270
        if (!(*tool)->isApplicable(document))
            return;
271 272 273 274 275 276 277 278 279 280 281
        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);
    }
}

282
void BeautifierPlugin::formatCurrentFile(const Command &command, int startPos, int endPos)
283
{
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    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())
302
        return;
303 304 305
    checkAndApplyTask(format(FormatTask(editor, editor->textDocument()->filePath().toString(), sd,
                                        command, startPos, endPos)));
}
306

307 308 309 310 311 312 313 314 315 316
/**
 * 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())
317 318
        return;

319 320 321
    QFutureWatcher<FormatTask> *watcher = new QFutureWatcher<FormatTask>;
    const TextDocument *doc = editor->textDocument();
    connect(doc, &TextDocument::contentsChanged, watcher, &QFutureWatcher<FormatTask>::cancel);
Lorenz Haas's avatar
Lorenz Haas committed
322
    connect(watcher, &QFutureWatcherBase::finished, [this, watcher] {
323 324 325 326 327 328 329 330 331
        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)));
}
332

333 334 335 336 337 338 339 340
/**
 * 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);
341 342 343
        return;
    }

344 345 346 347 348
    if (task.formattedData.isEmpty()) {
        showError(tr("Could not format file %1.").arg(task.filePath));
        return;
    }

349 350 351 352 353 354
    QPlainTextEdit *textEditor = task.editor;
    if (!textEditor) {
        showError(tr("File %1 was closed.").arg(task.filePath));
        return;
    }

355 356
    const QString formattedData = (task.startPos < 0)
            ? task.formattedData
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
            : 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)
Lorenz Haas's avatar
Lorenz Haas committed
372 373
        return;

374 375 376
    // Calculate diff
    DiffEditor::Differ differ;
    const QList<DiffEditor::Diff> diff = differ.diff(editorText, text);
377

Lorenz Haas's avatar
Lorenz Haas committed
378 379 380
    // 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;
381
    QTextBlock block = editor->document()->firstBlock();
Lorenz Haas's avatar
Lorenz Haas committed
382
    while (block.isValid()) {
383
        if (const TextBlockUserData *userdata = static_cast<TextBlockUserData *>(block.userData())) {
Lorenz Haas's avatar
Lorenz Haas committed
384 385
            if (userdata->folded()) {
                foldedBlocks << block.blockNumber();
386
                TextDocumentLayout::doFoldOrUnfold(block, true);
Lorenz Haas's avatar
Lorenz Haas committed
387 388 389 390
            }
        }
        block = block.next();
    }
391
    editor->update();
Lorenz Haas's avatar
Lorenz Haas committed
392 393 394

    // Save the current viewport position of the cursor to ensure the same vertical position after
    // the formatted text has set to the editor.
395
    int absoluteVerticalCursorOffset = editor->cursorRect().y();
Lorenz Haas's avatar
Lorenz Haas committed
396 397

    // Update changed lines and keep track of the cursor position
398
    QTextCursor cursor = editor->textCursor();
Lorenz Haas's avatar
Lorenz Haas committed
399 400 401 402
    int charactersInfrontOfCursor = cursor.position();
    int newCursorPos = charactersInfrontOfCursor;
    cursor.beginEditBlock();
    cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
403
    for (const DiffEditor::Diff &d : diff) {
Lorenz Haas's avatar
Lorenz Haas committed
404 405 406 407 408 409 410 411 412 413
        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.
414 415
            if (d.text.contains('\n')) {
                const int newLineCount = d.text.count('\n');
Lorenz Haas's avatar
Lorenz Haas committed
416 417 418 419 420 421 422 423 424
                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;
425 426
        }

Lorenz Haas's avatar
Lorenz Haas committed
427 428 429 430 431 432 433 434 435 436 437 438
        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.
439 440
            if (d.text.contains('\n')) {
                const int newLineCount = d.text.count('\n');
Lorenz Haas's avatar
Lorenz Haas committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
                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;
456 457
        }

Lorenz Haas's avatar
Lorenz Haas committed
458 459 460 461 462 463 464 465 466
        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);
467
    editor->setTextCursor(cursor);
Lorenz Haas's avatar
Lorenz Haas committed
468 469

    // Adjust vertical scrollbar
470 471 472
    absoluteVerticalCursorOffset = editor->cursorRect().y() - absoluteVerticalCursorOffset;
    const double fontHeight = QFontMetrics(editor->document()->defaultFont()).height();
    editor->verticalScrollBar()->setValue(editor->verticalScrollBar()->value()
Lorenz Haas's avatar
Lorenz Haas committed
473 474
                                              + absoluteVerticalCursorOffset / fontHeight);
    // Restore folded blocks
475
    const QTextDocument *doc = editor->document();
476
    for (int blockId : foldedBlocks) {
477
        const QTextBlock block = doc->findBlockByNumber(qMax(0, blockId));
Lorenz Haas's avatar
Lorenz Haas committed
478
        if (block.isValid())
479
            TextDocumentLayout::doFoldOrUnfold(block, false);
Lorenz Haas's avatar
Lorenz Haas committed
480 481
    }

482
    editor->document()->setModified(true);
Lorenz Haas's avatar
Lorenz Haas committed
483 484 485 486
}

void BeautifierPlugin::showError(const QString &error)
{
487
    Core::MessageManager::write(tr("Error in Beautifier: %1").arg(error.trimmed()));
Lorenz Haas's avatar
Lorenz Haas committed
488 489
}

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
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);
}

Lorenz Haas's avatar
Lorenz Haas committed
513 514
} // namespace Internal
} // namespace Beautifier