clangformat.cpp 5.2 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
// Tested with version 3.3, 3.4 and 3.4.1

Lorenz Haas's avatar
Lorenz Haas committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "clangformat.h"

#include "clangformatconstants.h"
#include "clangformatoptionspage.h"
#include "clangformatsettings.h"

#include "../beautifierconstants.h"
#include "../beautifierplugin.h"

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
44
#include <coreplugin/idocument.h>
Lorenz Haas's avatar
Lorenz Haas committed
45
#include <cppeditor/cppeditorconstants.h>
46
#include <texteditor/texteditor.h>
47
#include <utils/algorithm.h>
Lorenz Haas's avatar
Lorenz Haas committed
48 49 50

#include <QAction>
#include <QMenu>
51
#include <QStringList>
Lorenz Haas's avatar
Lorenz Haas committed
52 53 54 55 56

namespace Beautifier {
namespace Internal {
namespace ClangFormat {

57
ClangFormat::ClangFormat(BeautifierPlugin *parent) :
Lorenz Haas's avatar
Lorenz Haas committed
58
    BeautifierAbstractTool(parent),
59
    m_beautifierPlugin(parent),
Lorenz Haas's avatar
Lorenz Haas committed
60 61 62 63 64 65 66 67 68 69 70 71
    m_settings(new ClangFormatSettings)
{
}

ClangFormat::~ClangFormat()
{
    delete m_settings;
}

bool ClangFormat::initialize()
{
    Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::ClangFormat::MENU_ID);
72
    menu->menu()->setTitle(QLatin1String(Constants::ClangFormat::DISPLAY_NAME));
Lorenz Haas's avatar
Lorenz Haas committed
73

74
    m_formatFile = new QAction(BeautifierPlugin::msgFormatCurrentFile(), this);
Lorenz Haas's avatar
Lorenz Haas committed
75 76
    Core::Command *cmd
            = Core::ActionManager::registerAction(m_formatFile,
77
                                                  Constants::ClangFormat::ACTION_FORMATFILE);
Lorenz Haas's avatar
Lorenz Haas committed
78
    menu->addAction(cmd);
Montel Laurent's avatar
Montel Laurent committed
79
    connect(m_formatFile, &QAction::triggered, this, &ClangFormat::formatFile);
Lorenz Haas's avatar
Lorenz Haas committed
80

81
    m_formatRange = new QAction(BeautifierPlugin::msgFormatSelectedText(), this);
Lorenz Haas's avatar
Lorenz Haas committed
82
    cmd = Core::ActionManager::registerAction(m_formatRange,
83
                                              Constants::ClangFormat::ACTION_FORMATSELECTED);
Lorenz Haas's avatar
Lorenz Haas committed
84
    menu->addAction(cmd);
Montel Laurent's avatar
Montel Laurent committed
85
    connect(m_formatRange, &QAction::triggered, this, &ClangFormat::formatSelectedText);
Lorenz Haas's avatar
Lorenz Haas committed
86 87 88 89 90 91 92 93

    Core::ActionManager::actionContainer(Constants::MENU_ID)->addMenu(menu);

    return true;
}

void ClangFormat::updateActions(Core::IEditor *editor)
{
94
    const bool enabled = (editor && editor->document()->id() == CppEditor::Constants::CPPEDITOR_ID);
Lorenz Haas's avatar
Lorenz Haas committed
95 96 97 98 99 100 101 102 103 104 105 106
    m_formatFile->setEnabled(enabled);
    m_formatRange->setEnabled(enabled);
}

QList<QObject *> ClangFormat::autoReleaseObjects()
{
    ClangFormatOptionsPage *optionsPage = new ClangFormatOptionsPage(m_settings, this);
    return QList<QObject *>() << optionsPage;
}

void ClangFormat::formatFile()
{
107
    m_beautifierPlugin->formatCurrentFile(command());
Lorenz Haas's avatar
Lorenz Haas committed
108 109 110 111
}

void ClangFormat::formatSelectedText()
{
112 113
    const TextEditor::TextEditorWidget *widget
            = TextEditor::TextEditorWidget::currentTextEditorWidget();
114
    if (!widget)
Lorenz Haas's avatar
Lorenz Haas committed
115 116
        return;

117
    const QTextCursor tc = widget->textCursor();
Lorenz Haas's avatar
Lorenz Haas committed
118 119 120
    if (tc.hasSelection()) {
        const int offset = tc.selectionStart();
        const int length = tc.selectionEnd() - offset;
121
        m_beautifierPlugin->formatCurrentFile(command(offset, length));
Lorenz Haas's avatar
Lorenz Haas committed
122 123 124 125 126
    } else if (m_settings->formatEntireFileFallback()) {
        formatFile();
    }
}

127
Command ClangFormat::command(int offset, int length) const
Lorenz Haas's avatar
Lorenz Haas committed
128
{
129 130 131 132
    Command command;
    command.setExecutable(m_settings->command());
    command.setProcessing(Command::PipeProcessing);

Lorenz Haas's avatar
Lorenz Haas committed
133
    if (m_settings->usePredefinedStyle()) {
134
        command.addOption(QLatin1String("-style=") + m_settings->predefinedStyle());
135
        command.addOption(QLatin1String("-assume-filename=%file"));
Lorenz Haas's avatar
Lorenz Haas committed
136
    } else {
137 138 139 140 141
        command.addOption(QLatin1String("-style=file"));
        const QString path =
                QFileInfo(m_settings->styleFileName(m_settings->customStyle())).absolutePath();
        command.addOption(QLatin1String("-assume-filename=") + path + QDir::separator()
                          + QLatin1String("%filename"));
Lorenz Haas's avatar
Lorenz Haas committed
142
    }
143

Lorenz Haas's avatar
Lorenz Haas committed
144
    if (offset != -1) {
145 146
        command.addOption(QLatin1String("-offset=") + QString::number(offset));
        command.addOption(QLatin1String("-length=") + QString::number(length));
Lorenz Haas's avatar
Lorenz Haas committed
147 148 149 150 151 152 153 154
    }

    return command;
}

} // namespace ClangFormat
} // namespace Internal
} // namespace Beautifier