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:
Artem Sokolovskii
2023-06-07 16:17:29 +02:00
parent 2edaf459bd
commit 6a20e588bf
6 changed files with 117 additions and 75 deletions

View File

@@ -7,6 +7,7 @@
#include <coreplugin/icore.h>
#include <cppeditor/cppcodestylepreferences.h>
#include <cppeditor/cppcodestylesettings.h>
#include <texteditor/icodestylepreferences.h>
@@ -179,6 +180,83 @@ clang::format::FormatStyle qtcStyle()
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)
{
if (!project)