diff options
-rw-r--r-- | src/plugins/clangformat/clangformat.pro | 2 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformat.qbs | 2 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatbaseindenter.cpp | 19 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatbaseindenter.h | 5 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatconfigwidget.cpp | 10 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatconfigwidget.ui | 7 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatconstants.h | 2 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatindenter.cpp | 6 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatindenter.h | 1 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatsettings.cpp | 67 | ||||
-rw-r--r-- | src/plugins/clangformat/clangformatsettings.h | 44 | ||||
-rw-r--r-- | src/plugins/texteditor/indenter.h | 18 | ||||
-rw-r--r-- | src/plugins/texteditor/textdocument.cpp | 4 | ||||
-rw-r--r-- | src/plugins/texteditor/textdocument.h | 2 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditor.cpp | 12 | ||||
-rw-r--r-- | src/plugins/texteditor/textindenter.cpp | 6 | ||||
-rw-r--r-- | src/plugins/texteditor/textindenter.h | 1 |
17 files changed, 182 insertions, 26 deletions
diff --git a/src/plugins/clangformat/clangformat.pro b/src/plugins/clangformat/clangformat.pro index 2e168fdbc5..653dacdaba 100644 --- a/src/plugins/clangformat/clangformat.pro +++ b/src/plugins/clangformat/clangformat.pro @@ -24,12 +24,14 @@ SOURCES += \ clangformatconfigwidget.cpp \ clangformatindenter.cpp \ clangformatplugin.cpp \ + clangformatsettings.cpp \ clangformatutils.cpp HEADERS += \ clangformatconfigwidget.h \ clangformatindenter.h \ clangformatplugin.h \ + clangformatsettings.h \ clangformatutils.h FORMS += \ diff --git a/src/plugins/clangformat/clangformat.qbs b/src/plugins/clangformat/clangformat.qbs index cbfaffe657..c7b9182373 100644 --- a/src/plugins/clangformat/clangformat.qbs +++ b/src/plugins/clangformat/clangformat.qbs @@ -40,6 +40,8 @@ QtcPlugin { "clangformatindenter.h", "clangformatplugin.cpp", "clangformatplugin.h", + "clangformatsettings.cpp", + "clangformatsettings.h", "clangformatutils.h", "clangformatutils.cpp", ] diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index be515e1d3c..d86f0909a8 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -317,8 +317,7 @@ void ClangFormatBaseIndenter::reindent(const QTextCursor &cursor, indent(cursor, QChar::Null); } -TextEditor::Replacements ClangFormatBaseIndenter::format( - const QTextCursor &cursor, const TextEditor::TabSettings & /*tabSettings*/) +TextEditor::Replacements ClangFormatBaseIndenter::format(const QTextCursor &cursor) { int utf8Offset; int utf8Length; @@ -345,6 +344,12 @@ TextEditor::Replacements ClangFormatBaseIndenter::format( return toReplace; } +TextEditor::Replacements ClangFormatBaseIndenter::format( + const QTextCursor &cursor, const TextEditor::TabSettings & /*tabSettings*/) +{ + return format(cursor); +} + void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar) { trimFirstNonEmptyBlock(block); @@ -405,6 +410,16 @@ bool ClangFormatBaseIndenter::isElectricCharacter(const QChar &ch) const return false; } +void ClangFormatBaseIndenter::formatOrIndent(const QTextCursor &cursor, + const TextEditor::TabSettings & /*tabSettings*/, + int /*cursorPositionInEditor*/) +{ + if (formatCodeInsteadOfIndent()) + format(cursor); + else + indent(cursor, QChar::Null); +} + clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const { llvm::Expected<clang::format::FormatStyle> style diff --git a/src/plugins/clangformat/clangformatbaseindenter.h b/src/plugins/clangformat/clangformatbaseindenter.h index 47137420d7..fa880f5ea4 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.h +++ b/src/plugins/clangformat/clangformatbaseindenter.h @@ -45,6 +45,9 @@ public: void reindent(const QTextCursor &cursor, const TextEditor::TabSettings & /*tabSettings*/) override; + void formatOrIndent(const QTextCursor &cursor, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; TextEditor::Replacements format(const QTextCursor &cursor, const TextEditor::TabSettings & /*tabSettings*/) override; @@ -58,8 +61,10 @@ public: protected: virtual clang::format::FormatStyle styleForFile() const; + virtual bool formatCodeInsteadOfIndent() const { return false; } private: + TextEditor::Replacements format(const QTextCursor &cursor); void indent(const QTextCursor &cursor, const QChar &typedChar); void indentBlock(const QTextBlock &block, const QChar &typedChar); int indentFor(const QTextBlock &block); diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index 349f92905d..3d105f5580 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -27,6 +27,7 @@ #include "clangformatconfigwidget.h" #include "clangformatconstants.h" +#include "clangformatsettings.h" #include "clangformatutils.h" #include "ui_clangformatconfigwidget.h" @@ -131,6 +132,7 @@ void ClangFormatConfigWidget::initialize() m_ui->projectHasClangFormat->show(); m_ui->clangFormatOptionsTable->show(); m_ui->applyButton->show(); + m_ui->formatAlways->hide(); QLayoutItem *lastItem = m_ui->verticalLayout->itemAt(m_ui->verticalLayout->count() - 1); if (lastItem->spacerItem()) @@ -169,6 +171,8 @@ void ClangFormatConfigWidget::initialize() "and can be configured in Projects > Code Style > C++.")); } createStyleFileIfNeeded(true); + m_ui->formatAlways->setChecked(ClangFormatSettings::instance().formatCodeInsteadOfIndent()); + m_ui->formatAlways->show(); m_ui->applyButton->hide(); } @@ -189,6 +193,12 @@ ClangFormatConfigWidget::~ClangFormatConfigWidget() = default; void ClangFormatConfigWidget::apply() { + if (!m_project) { + ClangFormatSettings &settings = ClangFormatSettings::instance(); + settings.setFormatCodeInsteadOfIndent(m_ui->formatAlways->isChecked()); + settings.write(); + } + const QByteArray text = tableToYAML(m_ui->clangFormatOptionsTable); QString filePath; if (m_project) diff --git a/src/plugins/clangformat/clangformatconfigwidget.ui b/src/plugins/clangformat/clangformatconfigwidget.ui index 569f486912..8fcc87235d 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.ui +++ b/src/plugins/clangformat/clangformatconfigwidget.ui @@ -27,6 +27,13 @@ <number>8</number> </property> <item> + <widget class="QCheckBox" name="formatAlways"> + <property name="text"> + <string>Format instead of indenting</string> + </property> + </widget> + </item> + <item> <widget class="QLabel" name="projectHasClangFormat"> <property name="text"> <string/> diff --git a/src/plugins/clangformat/clangformatconstants.h b/src/plugins/clangformat/clangformatconstants.h index f2ff6a470e..aa2389bbfe 100644 --- a/src/plugins/clangformat/clangformatconstants.h +++ b/src/plugins/clangformat/clangformatconstants.h @@ -30,5 +30,7 @@ namespace Constants { static const char SETTINGS_FILE_NAME[] = ".clang-format"; static const char SETTINGS_FILE_ALT_NAME[] = "_clang-format"; static const char SAMPLE_FILE_NAME[] = "test.cpp"; +static const char SETTINGS_ID[] = "ClangFormat"; +static const char FORMAT_CODE_INSTEAD_OF_INDENT_ID[] = "ClangFormat.FormatCodeInsteadOfIndent"; } // namespace Constants } // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatindenter.cpp b/src/plugins/clangformat/clangformatindenter.cpp index b52b4e9ce6..1d0400d323 100644 --- a/src/plugins/clangformat/clangformatindenter.cpp +++ b/src/plugins/clangformat/clangformatindenter.cpp @@ -24,6 +24,7 @@ ****************************************************************************/ #include "clangformatindenter.h" +#include "clangformatsettings.h" #include "clangformatutils.h" #include <texteditor/tabsettings.h> @@ -43,6 +44,11 @@ FormatStyle ClangFormatIndenter::styleForFile() const return ClangFormat::styleForFile(m_fileName); } +bool ClangFormatIndenter::formatCodeInsteadOfIndent() const +{ + return ClangFormatSettings::instance().formatCodeInsteadOfIndent(); +} + Utils::optional<TabSettings> ClangFormatIndenter::tabSettings() const { FormatStyle style = currentProjectStyle(); diff --git a/src/plugins/clangformat/clangformatindenter.h b/src/plugins/clangformat/clangformatindenter.h index 5631e32db1..507ea68f41 100644 --- a/src/plugins/clangformat/clangformatindenter.h +++ b/src/plugins/clangformat/clangformatindenter.h @@ -38,6 +38,7 @@ public: Utils::optional<TextEditor::TabSettings> tabSettings() const override; private: + bool formatCodeInsteadOfIndent() const override; clang::format::FormatStyle styleForFile() const override; }; diff --git a/src/plugins/clangformat/clangformatsettings.cpp b/src/plugins/clangformat/clangformatsettings.cpp new file mode 100644 index 0000000000..0547bde960 --- /dev/null +++ b/src/plugins/clangformat/clangformatsettings.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** 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 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. +** +** 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. +** +****************************************************************************/ + +#include "clangformatconstants.h" +#include "clangformatsettings.h" + +#include <coreplugin/icore.h> + +namespace ClangFormat { + +ClangFormatSettings &ClangFormatSettings::instance() +{ + static ClangFormatSettings settings; + return settings; +} + +ClangFormatSettings::ClangFormatSettings() +{ + QSettings *settings = Core::ICore::settings(); + settings->beginGroup(QLatin1String(Constants::SETTINGS_ID)); + m_formatCodeInsteadOfIndent + = settings->value(QLatin1String(Constants::FORMAT_CODE_INSTEAD_OF_INDENT_ID), false).toBool(); + settings->endGroup(); +} + +void ClangFormatSettings::write() const +{ + QSettings *settings = Core::ICore::settings(); + settings->beginGroup(QLatin1String(Constants::SETTINGS_ID)); + settings->setValue(QLatin1String(Constants::FORMAT_CODE_INSTEAD_OF_INDENT_ID), + m_formatCodeInsteadOfIndent); + settings->endGroup(); +} + +void ClangFormatSettings::setFormatCodeInsteadOfIndent(bool enable) +{ + m_formatCodeInsteadOfIndent = enable; +} + +bool ClangFormatSettings::formatCodeInsteadOfIndent() const +{ + return m_formatCodeInsteadOfIndent; +} + +} // namespace ClangFormat diff --git a/src/plugins/clangformat/clangformatsettings.h b/src/plugins/clangformat/clangformatsettings.h new file mode 100644 index 0000000000..0620593228 --- /dev/null +++ b/src/plugins/clangformat/clangformatsettings.h @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** 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 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. +** +** 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. +** +****************************************************************************/ + +#pragma once + +namespace ClangFormat { + +class ClangFormatSettings +{ +public: + static ClangFormatSettings &instance(); + + ClangFormatSettings(); + void write() const; + + void setFormatCodeInsteadOfIndent(bool enable); + bool formatCodeInsteadOfIndent() const; +private: + bool m_formatCodeInsteadOfIndent = false; +}; + +} // namespace ClangFormat diff --git a/src/plugins/texteditor/indenter.h b/src/plugins/texteditor/indenter.h index 5b4b97fbfa..a9fb5de7cd 100644 --- a/src/plugins/texteditor/indenter.h +++ b/src/plugins/texteditor/indenter.h @@ -81,6 +81,21 @@ public: return -1; } + virtual void formatOrIndent(const QTextCursor &cursor, + const TabSettings &tabSettings, + int /*cursorPositionInEditor*/ = -1) + { + indent(cursor, QChar::Null, tabSettings); + } + + // By default just calls indent with default settings. + virtual Replacements format(const QTextCursor &cursor, + const TabSettings &tabSettings) + { + indent(cursor, QChar::Null, tabSettings); + return Replacements(); + } + // Expects a list of blocks in order of occurrence in the document. virtual IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks, const TabSettings & /*tabSettings*/) @@ -99,9 +114,6 @@ public: const TabSettings &tabSettings) = 0; - // By default just calls indent with default settings. - virtual Replacements format(const QTextCursor &cursor, const TabSettings &tabSettings) = 0; - // Reindent at cursor. Selection will be adjusted according to the indentation // change of the first block. virtual void reindent(const QTextCursor &cursor, const TabSettings &tabSettings) = 0; diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp index 352c609143..d96b463cd4 100644 --- a/src/plugins/texteditor/textdocument.cpp +++ b/src/plugins/texteditor/textdocument.cpp @@ -422,9 +422,9 @@ void TextDocument::autoReindent(const QTextCursor &cursor) d->m_indenter->reindent(cursor, tabSettings()); } -void TextDocument::autoFormat(const QTextCursor &cursor) +void TextDocument::autoFormatOrIndent(const QTextCursor &cursor) { - d->m_indenter->format(cursor, tabSettings()); + d->m_indenter->formatOrIndent(cursor, tabSettings()); } QTextCursor TextDocument::indent(const QTextCursor &cursor, bool blockSelection, int column, diff --git a/src/plugins/texteditor/textdocument.h b/src/plugins/texteditor/textdocument.h index eaa102ec74..f71922f2d2 100644 --- a/src/plugins/texteditor/textdocument.h +++ b/src/plugins/texteditor/textdocument.h @@ -89,7 +89,7 @@ public: Indenter *indenter() const; void autoIndent(const QTextCursor &cursor, QChar typedChar = QChar::Null); void autoReindent(const QTextCursor &cursor); - void autoFormat(const QTextCursor &cursor); + void autoFormatOrIndent(const QTextCursor &cursor); QTextCursor indent(const QTextCursor &cursor, bool blockSelection = false, int column = 0, int *offset = nullptr); QTextCursor unindent(const QTextCursor &cursor, bool blockSelection = false, int column = 0, diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 872c139b69..7907259306 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -7135,21 +7135,11 @@ void TextEditorWidget::setIfdefedOutBlocks(const QList<BlockRange> &blocks) documentLayout->requestUpdate(); } -static bool applyFormattingInsteadOfIndentation() -{ - constexpr const char option[] = "QTC_FORMAT_INSTEAD_OF_INDENT"; - return qEnvironmentVariableIsSet(option); -} - void TextEditorWidget::format() { - static bool formattingInsteadOfIndentation = applyFormattingInsteadOfIndentation(); QTextCursor cursor = textCursor(); cursor.beginEditBlock(); - if (formattingInsteadOfIndentation) - d->m_document->autoFormat(cursor); - else - d->m_document->autoIndent(cursor); + d->m_document->autoFormatOrIndent(cursor); cursor.endEditBlock(); } diff --git a/src/plugins/texteditor/textindenter.cpp b/src/plugins/texteditor/textindenter.cpp index bca7b1ae18..53801fabfa 100644 --- a/src/plugins/texteditor/textindenter.cpp +++ b/src/plugins/texteditor/textindenter.cpp @@ -102,12 +102,6 @@ void TextIndenter::reindent(const QTextCursor &cursor, const TabSettings &tabSet } } -Replacements TextIndenter::format(const QTextCursor &cursor, const TabSettings &tabSettings) -{ - indent(cursor, QChar::Null, tabSettings); - return Replacements(); -} - Utils::optional<TabSettings> TextIndenter::tabSettings() const { return Utils::optional<TabSettings>(); diff --git a/src/plugins/texteditor/textindenter.h b/src/plugins/texteditor/textindenter.h index 8a87b9214d..51be65f409 100644 --- a/src/plugins/texteditor/textindenter.h +++ b/src/plugins/texteditor/textindenter.h @@ -54,7 +54,6 @@ public: const QChar &typedChar, const TabSettings &tabSettings) override; - Replacements format(const QTextCursor &cursor, const TabSettings &tabSettings) override; void reindent(const QTextCursor &cursor, const TabSettings &tabSettings) override; Utils::optional<TabSettings> tabSettings() const override; |