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
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
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>
|
2016-05-19 21:30:27 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2013-12-11 21:55:45 +01:00
|
|
|
#include <coreplugin/editormanager/ieditor.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>
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2016-01-19 20:26:59 +01:00
|
|
|
#include <utils/algorithm.h>
|
2016-05-19 21:30:27 +02:00
|
|
|
#include <utils/fileutils.h>
|
2013-12-11 21:55:45 +01:00
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QMenu>
|
2017-09-04 15:18:42 +02:00
|
|
|
#include <QTextBlock>
|
2013-12-11 21:55:45 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-17 21:27:25 +02:00
|
|
|
QString ClangFormat::id() const
|
|
|
|
|
{
|
|
|
|
|
return QLatin1String(Constants::ClangFormat::DISPLAY_NAME);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
bool ClangFormat::initialize()
|
|
|
|
|
{
|
|
|
|
|
Core::ActionContainer *menu = Core::ActionManager::createMenu(Constants::ClangFormat::MENU_ID);
|
2017-07-09 10:12:01 +03:00
|
|
|
menu->menu()->setTitle(tr("&ClangFormat"));
|
2013-12-11 21:55:45 +01:00
|
|
|
|
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,
|
2015-02-19 11:35:47 +01:00
|
|
|
Constants::ClangFormat::ACTION_FORMATFILE);
|
2013-12-11 21:55:45 +01:00
|
|
|
menu->addAction(cmd);
|
2015-01-29 14:32:36 +01:00
|
|
|
connect(m_formatFile, &QAction::triggered, this, &ClangFormat::formatFile);
|
2013-12-11 21:55:45 +01:00
|
|
|
|
2017-09-05 09:59:25 +02:00
|
|
|
m_formatRange = new QAction(BeautifierPlugin::msgFormatAtCursor(), this);
|
2013-12-11 21:55:45 +01:00
|
|
|
cmd = Core::ActionManager::registerAction(m_formatRange,
|
2017-09-05 09:59:25 +02:00
|
|
|
Constants::ClangFormat::ACTION_FORMATATCURSOR);
|
2013-12-11 21:55:45 +01:00
|
|
|
menu->addAction(cmd);
|
2017-09-05 09:59:25 +02:00
|
|
|
connect(m_formatRange, &QAction::triggered, this, &ClangFormat::formatAtCursor);
|
2013-12-11 21:55:45 +01:00
|
|
|
|
2017-09-04 15:18:42 +02:00
|
|
|
m_disableFormattingSelectedText
|
|
|
|
|
= new QAction(BeautifierPlugin::msgDisableFormattingSelectedText(), this);
|
|
|
|
|
cmd = Core::ActionManager::registerAction(
|
|
|
|
|
m_disableFormattingSelectedText, Constants::ClangFormat::ACTION_DISABLEFORMATTINGSELECTED);
|
|
|
|
|
menu->addAction(cmd);
|
|
|
|
|
connect(m_disableFormattingSelectedText, &QAction::triggered,
|
|
|
|
|
this, &ClangFormat::disableFormattingSelectedText);
|
|
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
Core::ActionManager::actionContainer(Constants::MENU_ID)->addMenu(menu);
|
|
|
|
|
|
2016-05-19 21:30:27 +02:00
|
|
|
connect(m_settings, &ClangFormatSettings::supportedMimeTypesChanged,
|
2016-06-01 18:12:51 +02:00
|
|
|
[this] { updateActions(Core::EditorManager::currentEditor()); });
|
2016-05-19 21:30:27 +02:00
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormat::updateActions(Core::IEditor *editor)
|
|
|
|
|
{
|
2016-06-01 18:12:51 +02:00
|
|
|
const bool enabled = editor && m_settings->isApplicable(editor->document());
|
2013-12-11 21:55:45 +01:00
|
|
|
m_formatFile->setEnabled(enabled);
|
|
|
|
|
m_formatRange->setEnabled(enabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<QObject *> ClangFormat::autoReleaseObjects()
|
|
|
|
|
{
|
2016-04-21 21:08:57 +02:00
|
|
|
return {new ClangFormatOptionsPage(m_settings, this)};
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormat::formatFile()
|
|
|
|
|
{
|
2014-06-26 00:00:08 +02:00
|
|
|
m_beautifierPlugin->formatCurrentFile(command());
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
|
2017-09-05 09:59:25 +02:00
|
|
|
void ClangFormat::formatAtCursor()
|
2013-12-11 21:55:45 +01:00
|
|
|
{
|
2015-06-21 19:12:02 +02:00
|
|
|
const TextEditor::TextEditorWidget *widget
|
|
|
|
|
= TextEditor::TextEditorWidget::currentTextEditorWidget();
|
2014-09-30 17:35:35 +02:00
|
|
|
if (!widget)
|
2013-12-11 21:55:45 +01:00
|
|
|
return;
|
|
|
|
|
|
2015-06-21 19:12:02 +02:00
|
|
|
const QTextCursor tc = widget->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));
|
2017-09-05 09:59:25 +02:00
|
|
|
} else {
|
|
|
|
|
// Pretend that the current line was selected.
|
|
|
|
|
// Note that clang-format will extend the range to the next bigger
|
|
|
|
|
// syntactic construct if needed.
|
|
|
|
|
const QTextBlock block = tc.block();
|
|
|
|
|
const int offset = block.position();
|
|
|
|
|
const int length = block.length();
|
|
|
|
|
m_beautifierPlugin->formatCurrentFile(command(offset, length));
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 15:18:42 +02:00
|
|
|
void ClangFormat::disableFormattingSelectedText()
|
|
|
|
|
{
|
|
|
|
|
TextEditor::TextEditorWidget *widget = TextEditor::TextEditorWidget::currentTextEditorWidget();
|
|
|
|
|
if (!widget)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QTextCursor tc = widget->textCursor();
|
|
|
|
|
if (!tc.hasSelection())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Insert start marker
|
|
|
|
|
const QTextBlock selectionStartBlock = tc.document()->findBlock(tc.selectionStart());
|
|
|
|
|
QTextCursor insertCursor(tc.document());
|
|
|
|
|
insertCursor.beginEditBlock();
|
|
|
|
|
insertCursor.setPosition(selectionStartBlock.position());
|
|
|
|
|
insertCursor.insertText("// clang-format off\n");
|
|
|
|
|
const int positionToRestore = tc.position();
|
|
|
|
|
|
|
|
|
|
// Insert end marker
|
|
|
|
|
QTextBlock selectionEndBlock = tc.document()->findBlock(tc.selectionEnd());
|
|
|
|
|
insertCursor.setPosition(selectionEndBlock.position() + selectionEndBlock.length() - 1);
|
|
|
|
|
insertCursor.insertText("\n// clang-format on");
|
|
|
|
|
insertCursor.endEditBlock();
|
|
|
|
|
|
|
|
|
|
// Reset the cursor position in order to clear the selection.
|
|
|
|
|
QTextCursor restoreCursor(tc.document());
|
|
|
|
|
restoreCursor.setPosition(positionToRestore);
|
|
|
|
|
widget->setTextCursor(restoreCursor);
|
|
|
|
|
|
|
|
|
|
// The indentation of these markers might be undesired, so reformat.
|
|
|
|
|
// This is not optimal because two undo steps will be needed to remove the markers.
|
|
|
|
|
const int reformatTextLength = insertCursor.position() - selectionStartBlock.position();
|
|
|
|
|
m_beautifierPlugin->formatCurrentFile(command(selectionStartBlock.position(),
|
|
|
|
|
reformatTextLength));
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-17 21:27:25 +02:00
|
|
|
Command ClangFormat::command() 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()) {
|
2017-03-01 20:40:27 +01:00
|
|
|
const QString predefinedStyle = m_settings->predefinedStyle();
|
|
|
|
|
command.addOption("-style=" + predefinedStyle);
|
|
|
|
|
if (predefinedStyle == "File") {
|
|
|
|
|
const QString fallbackStyle = m_settings->fallbackStyle();
|
|
|
|
|
if (fallbackStyle != "Default")
|
|
|
|
|
command.addOption("-fallback-style=" + fallbackStyle);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-11 13:28:35 +02:00
|
|
|
command.addOption("-assume-filename=%file");
|
2013-12-11 21:55:45 +01:00
|
|
|
} else {
|
2016-04-21 21:08:57 +02:00
|
|
|
command.addOption("-style=file");
|
2016-03-16 21:17:10 +01:00
|
|
|
const QString path =
|
|
|
|
|
QFileInfo(m_settings->styleFileName(m_settings->customStyle())).absolutePath();
|
2016-04-21 21:08:57 +02:00
|
|
|
command.addOption("-assume-filename=" + path + QDir::separator() + "%filename");
|
2013-12-11 21:55:45 +01:00
|
|
|
}
|
2014-05-11 19:59:46 +02:00
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
return command;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 21:30:27 +02:00
|
|
|
bool ClangFormat::isApplicable(const Core::IDocument *document) const
|
|
|
|
|
{
|
|
|
|
|
return m_settings->isApplicable(document);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-17 21:27:25 +02:00
|
|
|
Command ClangFormat::command(int offset, int length) const
|
|
|
|
|
{
|
|
|
|
|
Command c = command();
|
|
|
|
|
c.addOption("-offset=" + QString::number(offset));
|
|
|
|
|
c.addOption("-length=" + QString::number(length));
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-11 21:55:45 +01:00
|
|
|
} // namespace ClangFormat
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Beautifier
|