2013-12-11 21:55:45 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2014 Lorenz Haas
|
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
|
**
|
|
|
|
|
** 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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-05-11 19:59:46 +02:00
|
|
|
// Tested with version 3.3, 3.4 and 3.4.1
|
|
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
#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>
|
2014-03-05 15:58:12 +01:00
|
|
|
#include <coreplugin/idocument.h>
|
2013-12-11 21:55:45 +01:00
|
|
|
#include <cppeditor/cppeditorconstants.h>
|
|
|
|
|
#include <texteditor/basetexteditor.h>
|
|
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QMenu>
|
|
|
|
|
|
|
|
|
|
namespace Beautifier {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
namespace ClangFormat {
|
|
|
|
|
|
2014-06-26 00:00:08 +02:00
|
|
|
ClangFormat::ClangFormat(BeautifierPlugin *parent) :
|
2013-12-11 21:55:45 +01:00
|
|
|
BeautifierAbstractTool(parent),
|
2014-06-26 00:00:08 +02:00
|
|
|
m_beautifierPlugin(parent),
|
2013-12-11 21:55:45 +01:00
|
|
|
m_settings(new ClangFormatSettings)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClangFormat::~ClangFormat()
|
|
|
|
|
{
|
|
|
|
|
delete m_settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ClangFormat::initialize()
|
|
|
|
|
{
|
|
|
|
|
Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::ClangFormat::MENU_ID);
|
|
|
|
|
menu->menu()->setTitle(QLatin1String("ClangFormat"));
|
|
|
|
|
|
2014-03-05 14:02:01 +01:00
|
|
|
m_formatFile = new QAction(BeautifierPlugin::msgFormatCurrentFile(), this);
|
2013-12-11 21:55:45 +01:00
|
|
|
Core::Command *cmd
|
|
|
|
|
= Core::ActionManager::registerAction(m_formatFile,
|
|
|
|
|
Constants::ClangFormat::ACTION_FORMATFILE,
|
|
|
|
|
Core::Context(Core::Constants::C_GLOBAL));
|
|
|
|
|
menu->addAction(cmd);
|
|
|
|
|
connect(m_formatFile, SIGNAL(triggered()), this, SLOT(formatFile()));
|
|
|
|
|
|
2014-03-05 14:02:01 +01:00
|
|
|
m_formatRange = new QAction(BeautifierPlugin::msgFormatSelectedText(), this);
|
2013-12-11 21:55:45 +01:00
|
|
|
cmd = Core::ActionManager::registerAction(m_formatRange,
|
|
|
|
|
Constants::ClangFormat::ACTION_FORMATSELECTED,
|
|
|
|
|
Core::Context(Core::Constants::C_GLOBAL));
|
|
|
|
|
menu->addAction(cmd);
|
|
|
|
|
connect(m_formatRange, SIGNAL(triggered()), this, SLOT(formatSelectedText()));
|
|
|
|
|
|
|
|
|
|
Core::ActionManager::actionContainer(Constants::MENU_ID)->addMenu(menu);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormat::updateActions(Core::IEditor *editor)
|
|
|
|
|
{
|
2014-03-05 15:58:12 +01:00
|
|
|
const bool enabled = (editor && editor->document()->id() == CppEditor::Constants::CPPEDITOR_ID);
|
2013-12-11 21:55:45 +01:00
|
|
|
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()
|
|
|
|
|
{
|
2014-06-26 00:00:08 +02:00
|
|
|
m_beautifierPlugin->formatCurrentFile(command());
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormat::formatSelectedText()
|
|
|
|
|
{
|
2014-08-27 11:57:32 +02:00
|
|
|
TextEditor::BaseTextEditor *editor = TextEditor::BaseTextEditor::currentTextEditor();
|
2013-12-11 21:55:45 +01:00
|
|
|
if (!editor)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-08-27 11:57:32 +02:00
|
|
|
QTextCursor tc = editor->textCursor();
|
2013-12-11 21:55:45 +01:00
|
|
|
if (tc.hasSelection()) {
|
|
|
|
|
const int offset = tc.selectionStart();
|
|
|
|
|
const int length = tc.selectionEnd() - offset;
|
2014-06-26 00:00:08 +02:00
|
|
|
m_beautifierPlugin->formatCurrentFile(command(offset, length));
|
2013-12-11 21:55:45 +01:00
|
|
|
} else if (m_settings->formatEntireFileFallback()) {
|
|
|
|
|
formatFile();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-11 19:59:46 +02:00
|
|
|
Command ClangFormat::command(int offset, int length) const
|
2013-12-11 21:55:45 +01:00
|
|
|
{
|
2014-05-11 19:59:46 +02:00
|
|
|
Command command;
|
|
|
|
|
command.setExecutable(m_settings->command());
|
|
|
|
|
command.setProcessing(Command::PipeProcessing);
|
|
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
if (m_settings->usePredefinedStyle()) {
|
2014-05-11 19:59:46 +02:00
|
|
|
command.addOption(QLatin1String("-style=") + m_settings->predefinedStyle());
|
2013-12-11 21:55:45 +01:00
|
|
|
} else {
|
2014-05-11 19:59:46 +02:00
|
|
|
command.addOption(QLatin1String("-style={")
|
|
|
|
|
+ m_settings->style(m_settings->customStyle()).remove(QLatin1Char('\n'))
|
2014-08-29 14:00:18 +02:00
|
|
|
+ QLatin1Char('}'));
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
2014-05-11 19:59:46 +02:00
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
if (offset != -1) {
|
2014-05-11 19:59:46 +02:00
|
|
|
command.addOption(QLatin1String("-offset=") + QString::number(offset));
|
|
|
|
|
command.addOption(QLatin1String("-length=") + QString::number(length));
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
2014-05-12 22:53:44 +02:00
|
|
|
command.addOption(QLatin1String("-assume-filename=%file"));
|
2013-12-11 21:55:45 +01:00
|
|
|
|
|
|
|
|
return command;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangFormat
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Beautifier
|