ClangFormat: Fix applying global settings

The actual apply() method was never called for the widget.

Change-Id: Idff194a36591db437cbe5695377005ed5a0b25d4
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-01-22 14:16:30 +01:00
parent 020d1aab0a
commit 2e19352177
11 changed files with 47 additions and 27 deletions

View File

@@ -117,7 +117,7 @@ static QByteArray tableToYAML(QTableWidget *table)
ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project, ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project,
QWidget *parent) QWidget *parent)
: QWidget(parent) : CodeStyleEditorWidget(parent)
, m_project(project) , m_project(project)
, m_ui(std::make_unique<Ui::ClangFormatConfigWidget>()) , m_ui(std::make_unique<Ui::ClangFormatConfigWidget>())
{ {
@@ -136,7 +136,8 @@ void ClangFormatConfigWidget::initialize()
if (lastItem->spacerItem()) if (lastItem->spacerItem())
m_ui->verticalLayout->removeItem(lastItem); m_ui->verticalLayout->removeItem(lastItem);
if (m_project && !m_project->projectDirectory().appendPath(SETTINGS_FILE_NAME).exists()) { if (m_project
&& !m_project->projectDirectory().appendPath(Constants::SETTINGS_FILE_NAME).exists()) {
m_ui->projectHasClangFormat->setText(tr("No .clang-format file for the project.")); m_ui->projectHasClangFormat->setText(tr("No .clang-format file for the project."));
m_ui->clangFormatOptionsTable->hide(); m_ui->clangFormatOptionsTable->hide();
m_ui->applyButton->hide(); m_ui->applyButton->hide();
@@ -158,7 +159,9 @@ void ClangFormatConfigWidget::initialize()
} else { } else {
const Project *currentProject = SessionManager::startupProject(); const Project *currentProject = SessionManager::startupProject();
if (!currentProject if (!currentProject
|| !currentProject->projectDirectory().appendPath(SETTINGS_FILE_NAME).exists()) { || !currentProject->projectDirectory()
.appendPath(Constants::SETTINGS_FILE_NAME)
.exists()) {
m_ui->projectHasClangFormat->hide(); m_ui->projectHasClangFormat->hide();
} else { } else {
m_ui->projectHasClangFormat->setText( m_ui->projectHasClangFormat->setText(
@@ -189,9 +192,9 @@ void ClangFormatConfigWidget::apply()
const QByteArray text = tableToYAML(m_ui->clangFormatOptionsTable); const QByteArray text = tableToYAML(m_ui->clangFormatOptionsTable);
QString filePath; QString filePath;
if (m_project) if (m_project)
filePath = m_project->projectDirectory().appendPath(SETTINGS_FILE_NAME).toString(); filePath = m_project->projectDirectory().appendPath(Constants::SETTINGS_FILE_NAME).toString();
else else
filePath = Core::ICore::userResourcePath() + "/" + SETTINGS_FILE_NAME; filePath = Core::ICore::userResourcePath() + "/" + Constants::SETTINGS_FILE_NAME;
QFile file(filePath); QFile file(filePath);
if (!file.open(QFile::WriteOnly)) if (!file.open(QFile::WriteOnly))
return; return;

View File

@@ -25,7 +25,7 @@
#pragma once #pragma once
#include <QWidget> #include <texteditor/icodestylepreferencesfactory.h>
#include <memory> #include <memory>
@@ -37,7 +37,7 @@ namespace Ui {
class ClangFormatConfigWidget; class ClangFormatConfigWidget;
} }
class ClangFormatConfigWidget : public QWidget class ClangFormatConfigWidget : public TextEditor::CodeStyleEditorWidget
{ {
Q_OBJECT Q_OBJECT
@@ -45,7 +45,7 @@ public:
explicit ClangFormatConfigWidget(ProjectExplorer::Project *project = nullptr, explicit ClangFormatConfigWidget(ProjectExplorer::Project *project = nullptr,
QWidget *parent = nullptr); QWidget *parent = nullptr);
~ClangFormatConfigWidget() override; ~ClangFormatConfigWidget() override;
void apply(); void apply() override;
private: private:
void initialize(); void initialize();

View File

@@ -26,7 +26,9 @@
#pragma once #pragma once
namespace ClangFormat { namespace ClangFormat {
namespace Constants {
static const char SETTINGS_FILE_NAME[] = ".clang-format"; static const char SETTINGS_FILE_NAME[] = ".clang-format";
static const char SETTINGS_FILE_ALT_NAME[] = "_clang-format"; static const char SETTINGS_FILE_ALT_NAME[] = "_clang-format";
static const char SAMPLE_FILE_NAME[] = "test.cpp"; static const char SAMPLE_FILE_NAME[] = "test.cpp";
} } // namespace Constants
} // namespace ClangFormat

View File

@@ -46,6 +46,7 @@
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <projectexplorer/target.h> #include <projectexplorer/target.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
#include <clang/Format/Format.h> #include <clang/Format/Format.h>
@@ -71,8 +72,8 @@ ClangFormatPlugin::~ClangFormatPlugin() = default;
class ClangFormatStyleFactory : public CppTools::CppCodeStylePreferencesFactory class ClangFormatStyleFactory : public CppTools::CppCodeStylePreferencesFactory
{ {
public: public:
QWidget *createCodeStyleEditor(TextEditor::ICodeStylePreferences *, TextEditor::CodeStyleEditorWidget *createCodeStyleEditor(
QWidget *parent = nullptr) override TextEditor::ICodeStylePreferences *preferences, QWidget *parent = nullptr) override
{ {
if (!parent) if (!parent)
return new ClangFormatConfigWidget; return new ClangFormatConfigWidget;

View File

@@ -106,9 +106,11 @@ static Utils::FileName globalPath()
return Utils::FileName::fromString(Core::ICore::userResourcePath()); return Utils::FileName::fromString(Core::ICore::userResourcePath());
} }
static bool configForFileExists(Utils::FileName fileName) { static bool configForFileExists(Utils::FileName fileName)
{
QDir projectDir(fileName.parentDir().toString()); QDir projectDir(fileName.parentDir().toString());
while (!projectDir.exists(SETTINGS_FILE_NAME) && !projectDir.exists(SETTINGS_FILE_ALT_NAME)) { while (!projectDir.exists(Constants::SETTINGS_FILE_NAME)
&& !projectDir.exists(Constants::SETTINGS_FILE_ALT_NAME)) {
if (!projectDir.cdUp()) if (!projectDir.cdUp())
return false; return false;
} }
@@ -137,7 +139,7 @@ static clang::format::FormatStyle constructStyle(bool isGlobal)
void createStyleFileIfNeeded(bool isGlobal) void createStyleFileIfNeeded(bool isGlobal)
{ {
Utils::FileName path = isGlobal ? globalPath() : projectPath(); Utils::FileName path = isGlobal ? globalPath() : projectPath();
const QString configFile = path.appendPath(SETTINGS_FILE_NAME).toString(); const QString configFile = path.appendPath(Constants::SETTINGS_FILE_NAME).toString();
if (QFile::exists(configFile)) if (QFile::exists(configFile))
return; return;
@@ -159,7 +161,7 @@ clang::format::FormatStyle styleForFile(Utils::FileName fileName)
fileName = globalPath(); fileName = globalPath();
isGlobal = true; isGlobal = true;
} }
fileName.appendPath(SAMPLE_FILE_NAME); fileName.appendPath(Constants::SAMPLE_FILE_NAME);
createStyleFileIfNeeded(isGlobal); createStyleFileIfNeeded(isGlobal);
} }
@@ -178,12 +180,12 @@ clang::format::FormatStyle styleForFile(Utils::FileName fileName)
clang::format::FormatStyle currentProjectStyle() clang::format::FormatStyle currentProjectStyle()
{ {
return styleForFile(projectPath().appendPath(SAMPLE_FILE_NAME)); return styleForFile(projectPath().appendPath(Constants::SAMPLE_FILE_NAME));
} }
clang::format::FormatStyle currentGlobalStyle() clang::format::FormatStyle currentGlobalStyle()
{ {
return styleForFile(globalPath().appendPath(SAMPLE_FILE_NAME)); return styleForFile(globalPath().appendPath(Constants::SAMPLE_FILE_NAME));
} }
} }

View File

@@ -556,6 +556,8 @@ void CppCodeStyleSettingsPage::apply()
originalCppCodeStylePreferences->setCurrentDelegate(m_pageCppCodeStylePreferences->currentDelegate()); originalCppCodeStylePreferences->setCurrentDelegate(m_pageCppCodeStylePreferences->currentDelegate());
originalCppCodeStylePreferences->toSettings(QLatin1String(CppTools::Constants::CPP_SETTINGS_ID), s); originalCppCodeStylePreferences->toSettings(QLatin1String(CppTools::Constants::CPP_SETTINGS_ID), s);
} }
m_widget->apply();
} }
} }

View File

@@ -30,6 +30,7 @@
#include "cppcodeformatter.h" #include "cppcodeformatter.h"
#include <coreplugin/dialogs/ioptionspage.h> #include <coreplugin/dialogs/ioptionspage.h>
#include <texteditor/icodestylepreferencesfactory.h>
#include <QWidget> #include <QWidget>
#include <QPointer> #include <QPointer>
@@ -37,7 +38,6 @@
namespace TextEditor { namespace TextEditor {
class FontSettings; class FontSettings;
class TabSettings; class TabSettings;
class ICodeStylePreferences;
class SnippetEditorWidget; class SnippetEditorWidget;
class CodeStyleEditor; class CodeStyleEditor;
} }
@@ -91,7 +91,7 @@ public:
private: private:
CppCodeStylePreferences *m_pageCppCodeStylePreferences = nullptr; CppCodeStylePreferences *m_pageCppCodeStylePreferences = nullptr;
QPointer<QWidget> m_widget; QPointer<TextEditor::CodeStyleEditorWidget> m_widget;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -43,7 +43,7 @@ using namespace TextEditor;
CodeStyleEditor::CodeStyleEditor(ICodeStylePreferencesFactory *factory, CodeStyleEditor::CodeStyleEditor(ICodeStylePreferencesFactory *factory,
ICodeStylePreferences *codeStyle, QWidget *parent) ICodeStylePreferences *codeStyle, QWidget *parent)
: QWidget(parent), : CodeStyleEditorWidget(parent),
m_factory(factory), m_factory(factory),
m_codeStyle(codeStyle) m_codeStyle(codeStyle)
{ {

View File

@@ -27,7 +27,7 @@
#include "texteditor_global.h" #include "texteditor_global.h"
#include <QWidget> #include "icodestylepreferencesfactory.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QVBoxLayout; class QVBoxLayout;
@@ -39,7 +39,7 @@ class ICodeStylePreferencesFactory;
class ICodeStylePreferences; class ICodeStylePreferences;
class SnippetEditorWidget; class SnippetEditorWidget;
class TEXTEDITOR_EXPORT CodeStyleEditor : public QWidget class TEXTEDITOR_EXPORT CodeStyleEditor : public CodeStyleEditorWidget
{ {
Q_OBJECT Q_OBJECT
public: public:

View File

@@ -34,8 +34,8 @@ ICodeStylePreferencesFactory::ICodeStylePreferencesFactory(QObject *parent) :
{ {
} }
QWidget *ICodeStylePreferencesFactory::createCodeStyleEditor(ICodeStylePreferences *codeStyle, CodeStyleEditorWidget *ICodeStylePreferencesFactory::createCodeStyleEditor(
QWidget *parent) ICodeStylePreferences *codeStyle, QWidget *parent)
{ {
return new CodeStyleEditor(this, codeStyle, parent); return new CodeStyleEditor(this, codeStyle, parent);
} }

View File

@@ -29,7 +29,7 @@
#include "indenter.h" #include "indenter.h"
#include <QObject> #include <QWidget>
namespace Core { class Id; } namespace Core { class Id; }
namespace TextEditor { namespace TextEditor {
@@ -37,13 +37,23 @@ namespace TextEditor {
class ICodeStylePreferences; class ICodeStylePreferences;
class SnippetProvider; class SnippetProvider;
class TEXTEDITOR_EXPORT CodeStyleEditorWidget : public QWidget
{
Q_OBJECT
public:
CodeStyleEditorWidget(QWidget *parent = nullptr)
: QWidget(parent)
{}
virtual void apply() {}
};
class TEXTEDITOR_EXPORT ICodeStylePreferencesFactory : public QObject class TEXTEDITOR_EXPORT ICodeStylePreferencesFactory : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ICodeStylePreferencesFactory(QObject *parent = nullptr); explicit ICodeStylePreferencesFactory(QObject *parent = nullptr);
virtual QWidget *createCodeStyleEditor(ICodeStylePreferences *codeStyle, virtual CodeStyleEditorWidget *createCodeStyleEditor(ICodeStylePreferences *codeStyle,
QWidget *parent = nullptr); QWidget *parent = nullptr);
virtual Core::Id languageId() = 0; virtual Core::Id languageId() = 0;
virtual QString displayName() = 0; virtual QString displayName() = 0;