artisticstyle.cpp 5.7 KB
Newer Older
Lorenz Haas's avatar
Lorenz Haas committed
1 2
/**************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 Lorenz Haas
** Contact: http://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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
Lorenz Haas's avatar
Lorenz Haas committed
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Lorenz Haas's avatar
Lorenz Haas committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
Lorenz Haas's avatar
Lorenz Haas committed
27 28 29 30
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

31 32
// Tested with version 2.01, 2.02, 2.02.1, 2.03 and 2.04

Lorenz Haas's avatar
Lorenz Haas committed
33 34 35 36 37 38 39 40
#include "artisticstyle.h"

#include "artisticstyleconstants.h"
#include "artisticstyleoptionspage.h"
#include "artisticstylesettings.h"

#include "../beautifierconstants.h"
#include "../beautifierplugin.h"
41
#include "../command.h"
Lorenz Haas's avatar
Lorenz Haas committed
42 43 44 45 46 47 48 49

#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>
50
#include <coreplugin/idocument.h>
Lorenz Haas's avatar
Lorenz Haas committed
51
#include <cppeditor/cppeditorconstants.h>
52
#include <projectexplorer/projecttree.h>
Lorenz Haas's avatar
Lorenz Haas committed
53 54
#include <projectexplorer/project.h>
#include <utils/fileutils.h>
55
#include <utils/hostosinfo.h>
Lorenz Haas's avatar
Lorenz Haas committed
56 57 58 59 60 61 62 63

#include <QAction>
#include <QMenu>

namespace Beautifier {
namespace Internal {
namespace ArtisticStyle {

64
ArtisticStyle::ArtisticStyle(BeautifierPlugin *parent) :
Lorenz Haas's avatar
Lorenz Haas committed
65
    BeautifierAbstractTool(parent),
66
    m_beautifierPlugin(parent),
Lorenz Haas's avatar
Lorenz Haas committed
67 68 69 70 71 72 73 74 75 76 77 78
    m_settings(new ArtisticStyleSettings)
{
}

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

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

81
    m_formatFile = new QAction(BeautifierPlugin::msgFormatCurrentFile(), this);
82 83
    menu->addAction(Core::ActionManager::registerAction(m_formatFile,
                                                        Constants::ArtisticStyle::ACTION_FORMATFILE));
Montel Laurent's avatar
Montel Laurent committed
84
    connect(m_formatFile, &QAction::triggered, this, &ArtisticStyle::formatFile);
Lorenz Haas's avatar
Lorenz Haas committed
85 86 87 88 89 90 91 92

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

    return true;
}

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

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

void ArtisticStyle::formatFile()
{
    QString cfgFileName;

    if (m_settings->useOtherFiles()) {
        if (const ProjectExplorer::Project *project
108
                = ProjectExplorer::ProjectTree::currentProject()) {
Lorenz Haas's avatar
Lorenz Haas committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
            const QStringList files = project->files(ProjectExplorer::Project::AllFiles);
            for (int i = 0, total = files.size(); i < total; ++i) {
                const QString &file = files.at(i);
                if (!file.endsWith(QLatin1String(".astylerc")))
                    continue;
                const QFileInfo fi(file);
                if (fi.isReadable()) {
                    cfgFileName = file;
                    break;
                }
            }
        }
    }

    if (cfgFileName.isEmpty() && m_settings->useHomeFile()) {
124 125
        const QDir homeDirectory = QDir::home();
        QString file = homeDirectory.filePath(QLatin1String(".astylerc"));
Lorenz Haas's avatar
Lorenz Haas committed
126 127 128
        if (QFile::exists(file)) {
            cfgFileName = file;
        } else {
129
            file = homeDirectory.filePath(QLatin1String("astylerc"));
Lorenz Haas's avatar
Lorenz Haas committed
130 131 132 133 134 135 136 137 138
            if (QFile::exists(file))
                cfgFileName = file;
        }
    }

    if (m_settings->useCustomStyle())
        cfgFileName = m_settings->styleFileName(m_settings->customStyle());

    if (cfgFileName.isEmpty()) {
139
        BeautifierPlugin::showError(BeautifierPlugin::msgCannotGetConfigurationFile(
140
                                        QLatin1String(Constants::ArtisticStyle::DISPLAY_NAME)));
Lorenz Haas's avatar
Lorenz Haas committed
141
    } else {
142 143 144 145 146 147 148 149
        Command command;
        command.setExecutable(m_settings->command());
        command.addOption(QLatin1String("-q"));
        command.addOption(QLatin1String("--options=") + cfgFileName);

        if (m_settings->version() > ArtisticStyleSettings::Version_2_03) {
            command.setProcessing(Command::PipeProcessing);
            command.setPipeAddsNewline(true);
150
            command.setReturnsCRLF(Utils::HostOsInfo::isWindowsHost());
151 152 153 154
        } else {
            command.addOption(QLatin1String("%file"));
        }

155
        m_beautifierPlugin->formatCurrentFile(command);
Lorenz Haas's avatar
Lorenz Haas committed
156 157 158 159 160 161
    }
}

} // namespace ArtisticStyle
} // namespace Internal
} // namespace Beautifier