forked from qt-creator/qt-creator
ClangFormat: Use pre-existing code style settings
Instead using of default code style settings, this commit modifies the ClangFormat plugin to use the code style settings that were set before applying ClangFormat. By doing so, we preserve the previously defined code style. Fixes: QTCREATORBUG-29185 Change-Id: I347d44d5ed49a2c50e130a983b62c844c8964391 Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -7,15 +7,16 @@
|
|||||||
|
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
|
|
||||||
|
#include <cppeditor/cppcodestylepreferences.h>
|
||||||
|
|
||||||
#include <projectexplorer/editorconfiguration.h>
|
#include <projectexplorer/editorconfiguration.h>
|
||||||
#include <projectexplorer/project.h>
|
#include <projectexplorer/project.h>
|
||||||
#include <projectexplorer/projectmanager.h>
|
#include <projectexplorer/projectmanager.h>
|
||||||
|
|
||||||
#include <texteditor/icodestylepreferences.h>
|
#include <texteditor/icodestylepreferences.h>
|
||||||
|
#include <texteditor/tabsettings.h>
|
||||||
#include <texteditor/texteditorsettings.h>
|
#include <texteditor/texteditorsettings.h>
|
||||||
|
|
||||||
#include <clang/Tooling/Core/Replacement.h>
|
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
#include <utils/fileutils.h>
|
#include <utils/fileutils.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
@@ -24,6 +25,8 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QTextDocument>
|
#include <QTextDocument>
|
||||||
|
|
||||||
|
#include <clang/Tooling/Core/Replacement.h>
|
||||||
|
|
||||||
namespace ClangFormat {
|
namespace ClangFormat {
|
||||||
|
|
||||||
Internal::LlvmFileSystemAdapter llvmFileSystemAdapter = {};
|
Internal::LlvmFileSystemAdapter llvmFileSystemAdapter = {};
|
||||||
@@ -760,7 +763,7 @@ clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName)
|
|||||||
Utils::FilePath filePath = filePathToCurrentSettings(preferences);
|
Utils::FilePath filePath = filePathToCurrentSettings(preferences);
|
||||||
|
|
||||||
if (!filePath.exists())
|
if (!filePath.exists())
|
||||||
return qtcStyle();
|
return currentQtStyle(preferences);
|
||||||
|
|
||||||
clang::format::FormatStyle currentSettingsStyle;
|
clang::format::FormatStyle currentSettingsStyle;
|
||||||
currentSettingsStyle.Language = clang::format::FormatStyle::LK_Cpp;
|
currentSettingsStyle.Language = clang::format::FormatStyle::LK_Cpp;
|
||||||
@@ -769,7 +772,7 @@ clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName)
|
|||||||
.toStdString(),
|
.toStdString(),
|
||||||
¤tSettingsStyle);
|
¤tSettingsStyle);
|
||||||
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
|
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
|
||||||
return qtcStyle());
|
return currentQtStyle(preferences));
|
||||||
|
|
||||||
return currentSettingsStyle;
|
return currentSettingsStyle;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
|
|||||||
: CppCodeStyleWidget(parent), d(new Private)
|
: CppCodeStyleWidget(parent), d(new Private)
|
||||||
{
|
{
|
||||||
d->project = project;
|
d->project = project;
|
||||||
d->config = std::make_unique<ClangFormatFile>(filePathToCurrentSettings(codeStyle->currentPreferences()));
|
d->config = std::make_unique<ClangFormatFile>(codeStyle->currentPreferences());
|
||||||
|
|
||||||
d->fallbackConfig = new QLabel(Tr::tr("Clang-Format Style"));
|
d->fallbackConfig = new QLabel(Tr::tr("Clang-Format Style"));
|
||||||
d->checksScrollArea = new QScrollArea();
|
d->checksScrollArea = new QScrollArea();
|
||||||
@@ -136,7 +136,7 @@ void ClangFormatConfigWidget::slotCodeStyleChanged(
|
|||||||
{
|
{
|
||||||
if (!codeStyle)
|
if (!codeStyle)
|
||||||
return;
|
return;
|
||||||
d->config.reset(new ClangFormatFile(filePathToCurrentSettings(codeStyle)));
|
d->config.reset(new ClangFormatFile(codeStyle));
|
||||||
d->config->setIsReadOnly(codeStyle->isReadOnly());
|
d->config->setIsReadOnly(codeStyle->isReadOnly());
|
||||||
d->style = d->config->style();
|
d->style = d->config->style();
|
||||||
|
|
||||||
|
|||||||
@@ -4,17 +4,23 @@
|
|||||||
#include "clangformatfile.h"
|
#include "clangformatfile.h"
|
||||||
#include "clangformatsettings.h"
|
#include "clangformatsettings.h"
|
||||||
#include "clangformatutils.h"
|
#include "clangformatutils.h"
|
||||||
|
|
||||||
|
#include <cppeditor/cppcodestylepreferences.h>
|
||||||
#include <cppeditor/cppcodestylesettings.h>
|
#include <cppeditor/cppcodestylesettings.h>
|
||||||
|
|
||||||
#include <projectexplorer/project.h>
|
#include <projectexplorer/project.h>
|
||||||
|
|
||||||
|
#include <texteditor/icodestylepreferences.h>
|
||||||
#include <texteditor/tabsettings.h>
|
#include <texteditor/tabsettings.h>
|
||||||
|
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
using namespace ClangFormat;
|
using namespace ClangFormat;
|
||||||
|
|
||||||
ClangFormatFile::ClangFormatFile(Utils::FilePath filePath)
|
ClangFormatFile::ClangFormatFile(const TextEditor::ICodeStylePreferences *preferences)
|
||||||
: m_filePath(filePath)
|
: m_filePath(filePathToCurrentSettings(preferences))
|
||||||
{
|
{
|
||||||
if (!m_filePath.exists()) {
|
if (!m_filePath.exists()) {
|
||||||
// create file and folder
|
// create file and folder
|
||||||
@@ -23,7 +29,7 @@ ClangFormatFile::ClangFormatFile(Utils::FilePath filePath)
|
|||||||
if (newStyleFile.is_open()) {
|
if (newStyleFile.is_open()) {
|
||||||
newStyleFile.close();
|
newStyleFile.close();
|
||||||
}
|
}
|
||||||
resetStyleToQtC();
|
resetStyleToQtC(preferences);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +39,7 @@ ClangFormatFile::ClangFormatFile(Utils::FilePath filePath)
|
|||||||
.toStdString(),
|
.toStdString(),
|
||||||
&m_style);
|
&m_style);
|
||||||
if (error.value() != static_cast<int>(clang::format::ParseError::Success)) {
|
if (error.value() != static_cast<int>(clang::format::ParseError::Success)) {
|
||||||
resetStyleToQtC();
|
resetStyleToQtC(preferences);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,9 +68,9 @@ void ClangFormatFile::setIsReadOnly(bool isReadOnly)
|
|||||||
m_isReadOnly = isReadOnly;
|
m_isReadOnly = isReadOnly;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClangFormatFile::resetStyleToQtC()
|
void ClangFormatFile::resetStyleToQtC(const TextEditor::ICodeStylePreferences *preferences)
|
||||||
{
|
{
|
||||||
m_style = qtcStyle();
|
m_style = currentQtStyle(preferences);
|
||||||
saveStyleToFile(m_style, m_filePath);
|
saveStyleToFile(m_style, m_filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,48 +184,7 @@ CppEditor::CppCodeStyleSettings ClangFormatFile::toCppCodeStyleSettings(
|
|||||||
|
|
||||||
void ClangFormatFile::fromCppCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings)
|
void ClangFormatFile::fromCppCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings)
|
||||||
{
|
{
|
||||||
using namespace clang::format;
|
::fromCppCodeStyleSettings(m_style, settings);
|
||||||
if (settings.indentAccessSpecifiers)
|
|
||||||
m_style.AccessModifierOffset = 0;
|
|
||||||
else
|
|
||||||
m_style.AccessModifierOffset = -1 * m_style.IndentWidth;
|
|
||||||
|
|
||||||
if (settings.indentNamespaceBody || settings.indentNamespaceBraces)
|
|
||||||
m_style.NamespaceIndentation = FormatStyle::NamespaceIndentationKind::NI_All;
|
|
||||||
else
|
|
||||||
m_style.NamespaceIndentation = FormatStyle::NamespaceIndentationKind::NI_None;
|
|
||||||
|
|
||||||
if (settings.indentClassBraces || settings.indentEnumBraces || settings.indentBlockBraces
|
|
||||||
|| settings.indentFunctionBraces)
|
|
||||||
m_style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
|
|
||||||
else
|
|
||||||
m_style.BreakBeforeBraces = FormatStyle::BS_Custom;
|
|
||||||
|
|
||||||
|
|
||||||
m_style.IndentCaseLabels = settings.indentSwitchLabels;
|
|
||||||
#if LLVM_VERSION_MAJOR >= 11
|
|
||||||
m_style.IndentCaseBlocks = settings.indentBlocksRelativeToSwitchLabels;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (settings.extraPaddingForConditionsIfConfusingAlign)
|
|
||||||
m_style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
|
|
||||||
else if (settings.alignAssignments)
|
|
||||||
m_style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
|
|
||||||
else
|
|
||||||
m_style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
|
|
||||||
|
|
||||||
m_style.DerivePointerAlignment = settings.bindStarToIdentifier || settings.bindStarToTypeName
|
|
||||||
|| settings.bindStarToLeftSpecifier
|
|
||||||
|| settings.bindStarToRightSpecifier;
|
|
||||||
|
|
||||||
if ((settings.bindStarToIdentifier || settings.bindStarToRightSpecifier)
|
|
||||||
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting)
|
|
||||||
m_style.PointerAlignment = FormatStyle::PAS_Right;
|
|
||||||
|
|
||||||
if ((settings.bindStarToTypeName || settings.bindStarToLeftSpecifier)
|
|
||||||
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting)
|
|
||||||
m_style.PointerAlignment = FormatStyle::PAS_Left;
|
|
||||||
|
|
||||||
saveNewFormat();
|
saveNewFormat();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,22 +223,6 @@ TextEditor::TabSettings ClangFormatFile::toTabSettings(ProjectExplorer::Project
|
|||||||
|
|
||||||
void ClangFormatFile::fromTabSettings(const TextEditor::TabSettings &settings)
|
void ClangFormatFile::fromTabSettings(const TextEditor::TabSettings &settings)
|
||||||
{
|
{
|
||||||
using namespace clang::format;
|
::fromTabSettings(m_style, settings);
|
||||||
|
|
||||||
m_style.IndentWidth = settings.m_indentSize;
|
|
||||||
m_style.TabWidth = settings.m_tabSize;
|
|
||||||
|
|
||||||
switch (settings.m_tabPolicy) {
|
|
||||||
case TextEditor::TabSettings::TabPolicy::MixedTabPolicy:
|
|
||||||
m_style.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
|
|
||||||
break;
|
|
||||||
case TextEditor::TabSettings::TabPolicy::SpacesOnlyTabPolicy:
|
|
||||||
m_style.UseTab = FormatStyle::UT_Never;
|
|
||||||
break;
|
|
||||||
case TextEditor::TabSettings::TabPolicy::TabsOnlyTabPolicy:
|
|
||||||
m_style.UseTab = FormatStyle::UT_Always;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
saveNewFormat();
|
saveNewFormat();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,18 +9,21 @@
|
|||||||
|
|
||||||
namespace CppEditor { class CppCodeStyleSettings; }
|
namespace CppEditor { class CppCodeStyleSettings; }
|
||||||
namespace ProjectExplorer { class Project; }
|
namespace ProjectExplorer { class Project; }
|
||||||
namespace TextEditor { class TabSettings; }
|
namespace TextEditor {
|
||||||
|
class ICodeStylePreferences;
|
||||||
|
class TabSettings;
|
||||||
|
}
|
||||||
|
|
||||||
namespace ClangFormat {
|
namespace ClangFormat {
|
||||||
|
|
||||||
class ClangFormatFile
|
class ClangFormatFile
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit ClangFormatFile(Utils::FilePath file);
|
explicit ClangFormatFile(const TextEditor::ICodeStylePreferences *preferences);
|
||||||
clang::format::FormatStyle style();
|
clang::format::FormatStyle style();
|
||||||
|
|
||||||
Utils::FilePath filePath();
|
Utils::FilePath filePath();
|
||||||
void resetStyleToQtC();
|
void resetStyleToQtC(const TextEditor::ICodeStylePreferences *codeStyle);
|
||||||
void setBasedOnStyle(QString styleName);
|
void setBasedOnStyle(QString styleName);
|
||||||
void setStyle(clang::format::FormatStyle style);
|
void setStyle(clang::format::FormatStyle style);
|
||||||
QString setStyle(QString style);
|
QString setStyle(QString style);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
|
|
||||||
|
#include <cppeditor/cppcodestylepreferences.h>
|
||||||
#include <cppeditor/cppcodestylesettings.h>
|
#include <cppeditor/cppcodestylesettings.h>
|
||||||
|
|
||||||
#include <texteditor/icodestylepreferences.h>
|
#include <texteditor/icodestylepreferences.h>
|
||||||
@@ -179,6 +180,83 @@ clang::format::FormatStyle qtcStyle()
|
|||||||
return style;
|
return style;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreferences *preferences)
|
||||||
|
{
|
||||||
|
clang::format::FormatStyle style = qtcStyle();
|
||||||
|
if (!preferences)
|
||||||
|
return style;
|
||||||
|
|
||||||
|
fromTabSettings(style, preferences->tabSettings());
|
||||||
|
if (auto ccpPreferences = dynamic_cast<const CppEditor::CppCodeStylePreferences *>(preferences))
|
||||||
|
fromCppCodeStyleSettings(style, ccpPreferences->codeStyleSettings());
|
||||||
|
return style;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fromCppCodeStyleSettings(clang::format::FormatStyle &style,
|
||||||
|
const CppEditor::CppCodeStyleSettings &settings)
|
||||||
|
{
|
||||||
|
using namespace clang::format;
|
||||||
|
if (settings.indentAccessSpecifiers)
|
||||||
|
style.AccessModifierOffset = 0;
|
||||||
|
else
|
||||||
|
style.AccessModifierOffset = -1 * style.IndentWidth;
|
||||||
|
|
||||||
|
if (settings.indentNamespaceBody || settings.indentNamespaceBraces)
|
||||||
|
style.NamespaceIndentation = FormatStyle::NamespaceIndentationKind::NI_All;
|
||||||
|
else
|
||||||
|
style.NamespaceIndentation = FormatStyle::NamespaceIndentationKind::NI_None;
|
||||||
|
|
||||||
|
if (settings.indentClassBraces || settings.indentEnumBraces || settings.indentBlockBraces
|
||||||
|
|| settings.indentFunctionBraces)
|
||||||
|
style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
|
||||||
|
else
|
||||||
|
style.BreakBeforeBraces = FormatStyle::BS_Custom;
|
||||||
|
|
||||||
|
style.IndentCaseLabels = settings.indentSwitchLabels;
|
||||||
|
#if LLVM_VERSION_MAJOR >= 11
|
||||||
|
style.IndentCaseBlocks = settings.indentBlocksRelativeToSwitchLabels;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (settings.extraPaddingForConditionsIfConfusingAlign)
|
||||||
|
style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
|
||||||
|
else if (settings.alignAssignments)
|
||||||
|
style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
|
||||||
|
else
|
||||||
|
style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
|
||||||
|
|
||||||
|
style.DerivePointerAlignment = settings.bindStarToIdentifier || settings.bindStarToTypeName
|
||||||
|
|| settings.bindStarToLeftSpecifier
|
||||||
|
|| settings.bindStarToRightSpecifier;
|
||||||
|
|
||||||
|
if ((settings.bindStarToIdentifier || settings.bindStarToRightSpecifier)
|
||||||
|
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting)
|
||||||
|
style.PointerAlignment = FormatStyle::PAS_Right;
|
||||||
|
|
||||||
|
if ((settings.bindStarToTypeName || settings.bindStarToLeftSpecifier)
|
||||||
|
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting)
|
||||||
|
style.PointerAlignment = FormatStyle::PAS_Left;
|
||||||
|
}
|
||||||
|
|
||||||
|
void fromTabSettings(clang::format::FormatStyle &style, const TextEditor::TabSettings &settings)
|
||||||
|
{
|
||||||
|
using namespace clang::format;
|
||||||
|
|
||||||
|
style.IndentWidth = settings.m_indentSize;
|
||||||
|
style.TabWidth = settings.m_tabSize;
|
||||||
|
|
||||||
|
switch (settings.m_tabPolicy) {
|
||||||
|
case TextEditor::TabSettings::TabPolicy::MixedTabPolicy:
|
||||||
|
style.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
|
||||||
|
break;
|
||||||
|
case TextEditor::TabSettings::TabPolicy::SpacesOnlyTabPolicy:
|
||||||
|
style.UseTab = FormatStyle::UT_Never;
|
||||||
|
break;
|
||||||
|
case TextEditor::TabSettings::TabPolicy::TabsOnlyTabPolicy:
|
||||||
|
style.UseTab = FormatStyle::UT_Always;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QString projectUniqueId(ProjectExplorer::Project *project)
|
QString projectUniqueId(ProjectExplorer::Project *project)
|
||||||
{
|
{
|
||||||
if (!project)
|
if (!project)
|
||||||
|
|||||||
@@ -14,8 +14,12 @@
|
|||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
namespace TextEditor { class ICodeStylePreferences; }
|
namespace TextEditor {
|
||||||
|
class ICodeStylePreferences;
|
||||||
|
class TabSettings;
|
||||||
|
}
|
||||||
namespace ProjectExplorer { class Project; }
|
namespace ProjectExplorer { class Project; }
|
||||||
|
namespace CppEditor { class CppCodeStyleSettings; }
|
||||||
namespace ClangFormat {
|
namespace ClangFormat {
|
||||||
|
|
||||||
QString projectUniqueId(ProjectExplorer::Project *project);
|
QString projectUniqueId(ProjectExplorer::Project *project);
|
||||||
@@ -32,10 +36,15 @@ ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils:
|
|||||||
Utils::FilePath configForFile(const Utils::FilePath &fileName);
|
Utils::FilePath configForFile(const Utils::FilePath &fileName);
|
||||||
Utils::FilePath findConfig(const Utils::FilePath &fileName);
|
Utils::FilePath findConfig(const Utils::FilePath &fileName);
|
||||||
|
|
||||||
|
void fromTabSettings(clang::format::FormatStyle &style, const TextEditor::TabSettings &settings);
|
||||||
|
void fromCppCodeStyleSettings(clang::format::FormatStyle &style,
|
||||||
|
const CppEditor::CppCodeStyleSettings &settings);
|
||||||
|
|
||||||
bool getProjectOverriddenSettings(const ProjectExplorer::Project *project);
|
bool getProjectOverriddenSettings(const ProjectExplorer::Project *project);
|
||||||
|
|
||||||
void addQtcStatementMacros(clang::format::FormatStyle &style);
|
void addQtcStatementMacros(clang::format::FormatStyle &style);
|
||||||
clang::format::FormatStyle qtcStyle();
|
clang::format::FormatStyle qtcStyle();
|
||||||
|
clang::format::FormatStyle currentQtStyle(const TextEditor::ICodeStylePreferences *codeStyle);
|
||||||
|
|
||||||
Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle);
|
Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user