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

View File

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

View File

@@ -26,7 +26,9 @@
#pragma once
namespace ClangFormat {
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";
}
} // namespace Constants
} // namespace ClangFormat

View File

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

View File

@@ -106,9 +106,11 @@ static Utils::FileName globalPath()
return Utils::FileName::fromString(Core::ICore::userResourcePath());
}
static bool configForFileExists(Utils::FileName fileName) {
static bool configForFileExists(Utils::FileName fileName)
{
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())
return false;
}
@@ -137,7 +139,7 @@ static clang::format::FormatStyle constructStyle(bool isGlobal)
void createStyleFileIfNeeded(bool isGlobal)
{
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))
return;
@@ -159,7 +161,7 @@ clang::format::FormatStyle styleForFile(Utils::FileName fileName)
fileName = globalPath();
isGlobal = true;
}
fileName.appendPath(SAMPLE_FILE_NAME);
fileName.appendPath(Constants::SAMPLE_FILE_NAME);
createStyleFileIfNeeded(isGlobal);
}
@@ -178,12 +180,12 @@ clang::format::FormatStyle styleForFile(Utils::FileName fileName)
clang::format::FormatStyle currentProjectStyle()
{
return styleForFile(projectPath().appendPath(SAMPLE_FILE_NAME));
return styleForFile(projectPath().appendPath(Constants::SAMPLE_FILE_NAME));
}
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->toSettings(QLatin1String(CppTools::Constants::CPP_SETTINGS_ID), s);
}
m_widget->apply();
}
}

View File

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

View File

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

View File

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

View File

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

View File

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