forked from qt-creator/qt-creator
ClangFormat: Add use global settings check box for a project
Change-Id: Ie2872de3574aaad058a9fca17247db00e9559ba7 Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -2,8 +2,6 @@
|
|||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||||
|
|
||||||
#include "clangformatbaseindenter.h"
|
#include "clangformatbaseindenter.h"
|
||||||
#include "clangformatconstants.h"
|
|
||||||
#include "clangformatsettings.h"
|
|
||||||
#include "clangformatutils.h"
|
#include "clangformatutils.h"
|
||||||
#include "llvmfilesystem.h"
|
#include "llvmfilesystem.h"
|
||||||
|
|
||||||
@@ -739,8 +737,11 @@ void ClangFormatBaseIndenter::autoIndent(const QTextCursor &cursor,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clang::format::FormatStyle overrideStyle(const ProjectExplorer::Project *projectForFile)
|
clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName)
|
||||||
{
|
{
|
||||||
|
const ProjectExplorer::Project *projectForFile
|
||||||
|
= ProjectExplorer::SessionManager::projectForFile(fileName);
|
||||||
|
|
||||||
const TextEditor::ICodeStylePreferences *preferences
|
const TextEditor::ICodeStylePreferences *preferences
|
||||||
= projectForFile
|
= projectForFile
|
||||||
? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
|
? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
|
||||||
@@ -765,15 +766,8 @@ clang::format::FormatStyle overrideStyle(const ProjectExplorer::Project *project
|
|||||||
|
|
||||||
clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const
|
clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const
|
||||||
{
|
{
|
||||||
const ProjectExplorer::Project *projectForFile
|
if (getCurrentOverriddenSettings(m_fileName))
|
||||||
= ProjectExplorer::SessionManager::projectForFile(m_fileName);
|
return overrideStyle(m_fileName);
|
||||||
|
|
||||||
const bool overrideStyleFile
|
|
||||||
= projectForFile ? projectForFile->namedSettings(Constants::OVERRIDE_FILE_ID).toBool()
|
|
||||||
: ClangFormatSettings::instance().overrideDefaultFile();
|
|
||||||
|
|
||||||
if (overrideStyleFile)
|
|
||||||
return overrideStyle(projectForFile);
|
|
||||||
|
|
||||||
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
|
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
|
||||||
= clang::format::getStyle("file",
|
= clang::format::getStyle("file",
|
||||||
|
@@ -13,6 +13,7 @@ static const char OVERRIDE_FILE_ID[] = "ClangFormat.OverrideFile";
|
|||||||
static const char FORMAT_CODE_ON_SAVE_ID[] = "ClangFormat.FormatCodeOnSave";
|
static const char FORMAT_CODE_ON_SAVE_ID[] = "ClangFormat.FormatCodeOnSave";
|
||||||
static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping";
|
static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping";
|
||||||
static const char MODE_ID[] = "ClangFormat.Mode";
|
static const char MODE_ID[] = "ClangFormat.Mode";
|
||||||
|
static const char USE_GLOBAL_SETTINGS[] = "ClangFormat.UseGlobalSettings";
|
||||||
static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig";
|
static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig";
|
||||||
} // namespace Constants
|
} // namespace Constants
|
||||||
} // namespace ClangFormat
|
} // namespace ClangFormat
|
||||||
|
@@ -36,12 +36,15 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Pr
|
|||||||
m_formatWhileTyping = new QCheckBox(tr("Format while typing"));
|
m_formatWhileTyping = new QCheckBox(tr("Format while typing"));
|
||||||
m_formatOnSave = new QCheckBox(tr("Format edited code on file save"));
|
m_formatOnSave = new QCheckBox(tr("Format edited code on file save"));
|
||||||
m_overrideDefault = new QCheckBox(tr("Override Clang Format configuration file"));
|
m_overrideDefault = new QCheckBox(tr("Override Clang Format configuration file"));
|
||||||
|
m_useGlobalSettings = new QCheckBox(tr("Use global settings"));
|
||||||
|
m_useGlobalSettings->hide();
|
||||||
|
|
||||||
using namespace Layouting;
|
using namespace Layouting;
|
||||||
|
|
||||||
Group globalSettingsGroupBox {
|
Group globalSettingsGroupBox {
|
||||||
title(tr("ClangFormat settings:")),
|
title(tr("ClangFormat settings:")),
|
||||||
Column {
|
Column {
|
||||||
|
m_useGlobalSettings,
|
||||||
Row { m_formattingModeLabel, m_indentingOrFormatting, st },
|
Row { m_formattingModeLabel, m_indentingOrFormatting, st },
|
||||||
m_formatWhileTyping,
|
m_formatWhileTyping,
|
||||||
m_formatOnSave,
|
m_formatOnSave,
|
||||||
@@ -57,11 +60,14 @@ ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Pr
|
|||||||
initCheckBoxes();
|
initCheckBoxes();
|
||||||
initIndentationOrFormattingCombobox();
|
initIndentationOrFormattingCombobox();
|
||||||
initOverrideCheckBox();
|
initOverrideCheckBox();
|
||||||
|
initUseGlobalSettingsCheckBox();
|
||||||
|
|
||||||
if (project) {
|
if (project) {
|
||||||
m_formattingModeLabel->hide();
|
m_formattingModeLabel->hide();
|
||||||
m_formatOnSave->hide();
|
m_formatOnSave->hide();
|
||||||
m_formatWhileTyping->hide();
|
m_formatWhileTyping->hide();
|
||||||
|
|
||||||
|
m_useGlobalSettings->show();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
globalSettingsGroupBox.widget->show();
|
globalSettingsGroupBox.widget->show();
|
||||||
@@ -94,13 +100,8 @@ void ClangFormatGlobalConfigWidget::initIndentationOrFormattingCombobox()
|
|||||||
m_indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Disable),
|
m_indentingOrFormatting->insertItem(static_cast<int>(ClangFormatSettings::Mode::Disable),
|
||||||
tr("Disable"));
|
tr("Disable"));
|
||||||
|
|
||||||
if (m_project) {
|
|
||||||
m_indentingOrFormatting->setCurrentIndex(
|
m_indentingOrFormatting->setCurrentIndex(
|
||||||
m_project->namedSettings(Constants::MODE_ID).toInt());
|
static_cast<int>(getProjectIndentationOrFormattingSettings(m_project)));
|
||||||
} else {
|
|
||||||
m_indentingOrFormatting->setCurrentIndex(
|
|
||||||
static_cast<int>(ClangFormatSettings::instance().mode()));
|
|
||||||
}
|
|
||||||
|
|
||||||
connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged, this, [this](int index) {
|
connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged, this, [this](int index) {
|
||||||
if (m_project)
|
if (m_project)
|
||||||
@@ -108,6 +109,29 @@ void ClangFormatGlobalConfigWidget::initIndentationOrFormattingCombobox()
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ClangFormatGlobalConfigWidget::initUseGlobalSettingsCheckBox()
|
||||||
|
{
|
||||||
|
if (!m_project)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const auto enableProjectSettings = [this] {
|
||||||
|
const bool isDisabled = m_project && m_useGlobalSettings->isChecked();
|
||||||
|
m_indentingOrFormatting->setDisabled(isDisabled);
|
||||||
|
m_overrideDefault->setDisabled(isDisabled
|
||||||
|
|| (m_indentingOrFormatting->currentIndex()
|
||||||
|
== static_cast<int>(ClangFormatSettings::Mode::Disable)));
|
||||||
|
};
|
||||||
|
|
||||||
|
m_useGlobalSettings->setChecked(getProjectUseGlobalSettings(m_project));
|
||||||
|
enableProjectSettings();
|
||||||
|
|
||||||
|
connect(m_useGlobalSettings, &QCheckBox::toggled,
|
||||||
|
this, [this, enableProjectSettings] (bool checked) {
|
||||||
|
m_project->setNamedSettings(Constants::USE_GLOBAL_SETTINGS, checked);
|
||||||
|
enableProjectSettings();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists()
|
bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists()
|
||||||
{
|
{
|
||||||
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
|
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
|
||||||
@@ -128,7 +152,7 @@ void ClangFormatGlobalConfigWidget::initOverrideCheckBox()
|
|||||||
|
|
||||||
auto setEnableOverrideCheckBox = [this](int index) {
|
auto setEnableOverrideCheckBox = [this](int index) {
|
||||||
bool isDisable = index == static_cast<int>(ClangFormatSettings::Mode::Disable);
|
bool isDisable = index == static_cast<int>(ClangFormatSettings::Mode::Disable);
|
||||||
m_overrideDefault->setEnabled(!isDisable);
|
m_overrideDefault->setDisabled(isDisable);
|
||||||
};
|
};
|
||||||
|
|
||||||
setEnableOverrideCheckBox(m_indentingOrFormatting->currentIndex());
|
setEnableOverrideCheckBox(m_indentingOrFormatting->currentIndex());
|
||||||
@@ -138,11 +162,7 @@ void ClangFormatGlobalConfigWidget::initOverrideCheckBox()
|
|||||||
m_overrideDefault->setToolTip(
|
m_overrideDefault->setToolTip(
|
||||||
tr("Override Clang Format configuration file with the chosen configuration."));
|
tr("Override Clang Format configuration file with the chosen configuration."));
|
||||||
|
|
||||||
if (m_project)
|
m_overrideDefault->setChecked(getProjectOverriddenSettings(m_project));
|
||||||
m_overrideDefault->setChecked(
|
|
||||||
m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool());
|
|
||||||
else
|
|
||||||
m_overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile());
|
|
||||||
|
|
||||||
connect(m_overrideDefault, &QCheckBox::toggled, this, [this](bool checked) {
|
connect(m_overrideDefault, &QCheckBox::toggled, this, [this](bool checked) {
|
||||||
if (m_project)
|
if (m_project)
|
||||||
|
@@ -31,6 +31,7 @@ private:
|
|||||||
void initCheckBoxes();
|
void initCheckBoxes();
|
||||||
void initIndentationOrFormattingCombobox();
|
void initIndentationOrFormattingCombobox();
|
||||||
void initOverrideCheckBox();
|
void initOverrideCheckBox();
|
||||||
|
void initUseGlobalSettingsCheckBox();
|
||||||
|
|
||||||
bool projectClangFormatFileExists();
|
bool projectClangFormatFileExists();
|
||||||
|
|
||||||
@@ -42,6 +43,7 @@ private:
|
|||||||
QCheckBox *m_formatWhileTyping;
|
QCheckBox *m_formatWhileTyping;
|
||||||
QCheckBox *m_formatOnSave;
|
QCheckBox *m_formatOnSave;
|
||||||
QCheckBox *m_overrideDefault;
|
QCheckBox *m_overrideDefault;
|
||||||
|
QCheckBox *m_useGlobalSettings;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ClangFormat
|
} // namespace ClangFormat
|
||||||
|
@@ -2,7 +2,6 @@
|
|||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||||
|
|
||||||
#include "clangformatindenter.h"
|
#include "clangformatindenter.h"
|
||||||
#include "clangformatconstants.h"
|
|
||||||
#include "clangformatsettings.h"
|
#include "clangformatsettings.h"
|
||||||
#include "clangformatutils.h"
|
#include "clangformatutils.h"
|
||||||
|
|
||||||
@@ -60,7 +59,8 @@ ClangFormatIndenter::ClangFormatIndenter(QTextDocument *doc)
|
|||||||
|
|
||||||
bool ClangFormatIndenter::formatCodeInsteadOfIndent() const
|
bool ClangFormatIndenter::formatCodeInsteadOfIndent() const
|
||||||
{
|
{
|
||||||
return ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting;
|
return getCurrentIndentationOrFormattingSettings(m_fileName)
|
||||||
|
== ClangFormatSettings::Mode::Formatting;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<TabSettings> ClangFormatIndenter::tabSettings() const
|
std::optional<TabSettings> ClangFormatIndenter::tabSettings() const
|
||||||
@@ -128,12 +128,7 @@ void ClangFormatForwardingIndenter::setFileName(const Utils::FilePath &fileName)
|
|||||||
|
|
||||||
TextEditor::Indenter *ClangFormatForwardingIndenter::currentIndenter() const
|
TextEditor::Indenter *ClangFormatForwardingIndenter::currentIndenter() const
|
||||||
{
|
{
|
||||||
const ProjectExplorer::Project *projectForFile
|
ClangFormatSettings::Mode mode = getCurrentIndentationOrFormattingSettings(m_fileName);
|
||||||
= ProjectExplorer::SessionManager::projectForFile(m_fileName);
|
|
||||||
|
|
||||||
ClangFormatSettings::Mode mode = projectForFile ? static_cast<ClangFormatSettings::Mode>(
|
|
||||||
projectForFile->namedSettings(Constants::MODE_ID).toInt())
|
|
||||||
: ClangFormatSettings::instance().mode();
|
|
||||||
|
|
||||||
if (mode == ClangFormatSettings::Disable)
|
if (mode == ClangFormatSettings::Disable)
|
||||||
return m_cppIndenter.get();
|
return m_cppIndenter.get();
|
||||||
|
@@ -4,7 +4,6 @@
|
|||||||
#include "clangformatutils.h"
|
#include "clangformatutils.h"
|
||||||
|
|
||||||
#include "clangformatconstants.h"
|
#include "clangformatconstants.h"
|
||||||
#include "clangformatsettings.h"
|
|
||||||
|
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
#include <cppeditor/cppcodestylesettings.h>
|
#include <cppeditor/cppcodestylesettings.h>
|
||||||
@@ -203,7 +202,58 @@ void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath)
|
|||||||
static bool useProjectOverriddenSettings()
|
static bool useProjectOverriddenSettings()
|
||||||
{
|
{
|
||||||
const Project *project = SessionManager::startupProject();
|
const Project *project = SessionManager::startupProject();
|
||||||
return project ? project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool() : false;
|
return getProjectOverriddenSettings(project);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project)
|
||||||
|
{
|
||||||
|
const QVariant projectUseGlobalSettings = project ? project->namedSettings(
|
||||||
|
Constants::USE_GLOBAL_SETTINGS)
|
||||||
|
: QVariant();
|
||||||
|
|
||||||
|
return projectUseGlobalSettings.isValid() ? projectUseGlobalSettings.toBool() : true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getProjectOverriddenSettings(const ProjectExplorer::Project *project)
|
||||||
|
{
|
||||||
|
const QVariant projectOverride = project ? project->namedSettings(Constants::OVERRIDE_FILE_ID)
|
||||||
|
: QVariant();
|
||||||
|
|
||||||
|
return projectOverride.isValid()
|
||||||
|
? projectOverride.toBool()
|
||||||
|
: ClangFormatSettings::instance().overrideDefaultFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool getCurrentOverriddenSettings(const Utils::FilePath &filePath)
|
||||||
|
{
|
||||||
|
const ProjectExplorer::Project *project = ProjectExplorer::SessionManager::projectForFile(
|
||||||
|
filePath);
|
||||||
|
|
||||||
|
return getProjectUseGlobalSettings(project)
|
||||||
|
? ClangFormatSettings::instance().overrideDefaultFile()
|
||||||
|
: getProjectOverriddenSettings(project);
|
||||||
|
}
|
||||||
|
|
||||||
|
ClangFormatSettings::Mode getProjectIndentationOrFormattingSettings(
|
||||||
|
const ProjectExplorer::Project *project)
|
||||||
|
{
|
||||||
|
const QVariant projectIndentationOrFormatting = project
|
||||||
|
? project->namedSettings(Constants::MODE_ID)
|
||||||
|
: QVariant();
|
||||||
|
|
||||||
|
return projectIndentationOrFormatting.isValid()
|
||||||
|
? static_cast<ClangFormatSettings::Mode>(projectIndentationOrFormatting.toInt())
|
||||||
|
: ClangFormatSettings::instance().mode();
|
||||||
|
}
|
||||||
|
|
||||||
|
ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils::FilePath &filePath)
|
||||||
|
{
|
||||||
|
const ProjectExplorer::Project *project = ProjectExplorer::SessionManager::projectForFile(
|
||||||
|
filePath);
|
||||||
|
|
||||||
|
return getProjectUseGlobalSettings(project)
|
||||||
|
? ClangFormatSettings::instance().mode()
|
||||||
|
: getProjectIndentationOrFormattingSettings(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Utils::FilePath globalPath()
|
static Utils::FilePath globalPath()
|
||||||
|
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "clangformatsettings.h"
|
||||||
|
|
||||||
#include <utils/fileutils.h>
|
#include <utils/fileutils.h>
|
||||||
#include <utils/id.h>
|
#include <utils/id.h>
|
||||||
|
|
||||||
@@ -13,6 +15,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
namespace TextEditor { class ICodeStylePreferences; }
|
namespace TextEditor { class ICodeStylePreferences; }
|
||||||
|
namespace ProjectExplorer { class Project; }
|
||||||
namespace ClangFormat {
|
namespace ClangFormat {
|
||||||
|
|
||||||
// Creates the style for the current project or the global style if needed.
|
// Creates the style for the current project or the global style if needed.
|
||||||
@@ -27,6 +30,15 @@ clang::format::FormatStyle currentProjectStyle();
|
|||||||
clang::format::FormatStyle currentGlobalStyle();
|
clang::format::FormatStyle currentGlobalStyle();
|
||||||
std::string readFile(const QString &path);
|
std::string readFile(const QString &path);
|
||||||
|
|
||||||
|
bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project);
|
||||||
|
|
||||||
|
bool getProjectOverriddenSettings(const ProjectExplorer::Project *project);
|
||||||
|
bool getCurrentOverriddenSettings(const Utils::FilePath &filePath);
|
||||||
|
|
||||||
|
ClangFormatSettings::Mode getProjectIndentationOrFormattingSettings(
|
||||||
|
const ProjectExplorer::Project *project);
|
||||||
|
ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils::FilePath &filePath);
|
||||||
|
|
||||||
// Is the style from the matching .clang-format file or global one if it's not found.
|
// Is the style from the matching .clang-format file or global one if it's not found.
|
||||||
QString configForFile(Utils::FilePath fileName);
|
QString configForFile(Utils::FilePath fileName);
|
||||||
clang::format::FormatStyle styleForFile(Utils::FilePath fileName);
|
clang::format::FormatStyle styleForFile(Utils::FilePath fileName);
|
||||||
|
Reference in New Issue
Block a user