2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2021 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2021-09-28 10:26:34 +02:00
|
|
|
|
|
|
|
|
#include "clangformatfile.h"
|
2021-11-01 16:20:08 +01:00
|
|
|
#include "clangformatsettings.h"
|
2022-04-04 14:53:05 +02:00
|
|
|
#include "clangformatutils.h"
|
2023-06-07 16:17:29 +02:00
|
|
|
|
|
|
|
|
#include <cppeditor/cppcodestylepreferences.h>
|
2021-11-01 16:20:08 +01:00
|
|
|
#include <cppeditor/cppcodestylesettings.h>
|
2023-06-07 16:17:29 +02:00
|
|
|
|
2021-11-01 16:20:08 +01:00
|
|
|
#include <projectexplorer/project.h>
|
2023-06-07 16:17:29 +02:00
|
|
|
|
|
|
|
|
#include <texteditor/icodestylepreferences.h>
|
2021-11-01 16:20:08 +01:00
|
|
|
#include <texteditor/tabsettings.h>
|
2023-06-07 16:17:29 +02:00
|
|
|
|
2021-11-01 16:20:08 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2021-09-28 10:26:34 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
using namespace ClangFormat;
|
|
|
|
|
|
2023-06-07 16:17:29 +02:00
|
|
|
ClangFormatFile::ClangFormatFile(const TextEditor::ICodeStylePreferences *preferences)
|
|
|
|
|
: m_filePath(filePathToCurrentSettings(preferences))
|
2021-09-28 10:26:34 +02:00
|
|
|
{
|
|
|
|
|
if (!m_filePath.exists()) {
|
2022-04-04 14:53:05 +02:00
|
|
|
// create file and folder
|
|
|
|
|
m_filePath.parentDir().createDir();
|
|
|
|
|
std::fstream newStyleFile(m_filePath.path().toStdString(), std::fstream::out);
|
|
|
|
|
if (newStyleFile.is_open()) {
|
|
|
|
|
newStyleFile.close();
|
|
|
|
|
}
|
2023-06-07 16:17:29 +02:00
|
|
|
resetStyleToQtC(preferences);
|
2021-09-28 10:26:34 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 12:57:20 +02:00
|
|
|
m_style.Language = clang::format::FormatStyle::LK_Cpp;
|
2022-09-09 13:48:08 +02:00
|
|
|
const std::error_code error = clang::format::parseConfiguration(m_filePath.fileContents()
|
|
|
|
|
.value_or(QByteArray())
|
|
|
|
|
.toStdString(),
|
|
|
|
|
&m_style);
|
2021-09-28 10:26:34 +02:00
|
|
|
if (error.value() != static_cast<int>(clang::format::ParseError::Success)) {
|
2023-06-07 16:17:29 +02:00
|
|
|
resetStyleToQtC(preferences);
|
2021-09-28 10:26:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-01 17:28:42 +02:00
|
|
|
clang::format::FormatStyle ClangFormatFile::style() {
|
2021-09-28 10:26:34 +02:00
|
|
|
return m_style;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils::FilePath ClangFormatFile::filePath()
|
|
|
|
|
{
|
|
|
|
|
return m_filePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatFile::setStyle(clang::format::FormatStyle style)
|
|
|
|
|
{
|
|
|
|
|
m_style = style;
|
|
|
|
|
saveNewFormat();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-04 14:53:05 +02:00
|
|
|
bool ClangFormatFile::isReadOnly() const
|
2021-09-28 10:26:34 +02:00
|
|
|
{
|
2022-04-04 14:53:05 +02:00
|
|
|
return m_isReadOnly;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-01 17:28:42 +02:00
|
|
|
void ClangFormatFile::setIsReadOnly(bool isReadOnly)
|
|
|
|
|
{
|
|
|
|
|
m_isReadOnly = isReadOnly;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 16:17:29 +02:00
|
|
|
void ClangFormatFile::resetStyleToQtC(const TextEditor::ICodeStylePreferences *preferences)
|
2022-04-04 14:53:05 +02:00
|
|
|
{
|
2023-06-07 16:17:29 +02:00
|
|
|
m_style = currentQtStyle(preferences);
|
2022-04-04 14:53:05 +02:00
|
|
|
saveStyleToFile(m_style, m_filePath);
|
2021-09-28 10:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatFile::setBasedOnStyle(QString styleName)
|
|
|
|
|
{
|
|
|
|
|
changeField({"BasedOnStyle", styleName});
|
|
|
|
|
saveNewFormat();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ClangFormatFile::setStyle(QString style)
|
|
|
|
|
{
|
|
|
|
|
const std::error_code error = clang::format::parseConfiguration(style.toStdString(), &m_style);
|
|
|
|
|
if (error.value() != static_cast<int>(clang::format::ParseError::Success)) {
|
|
|
|
|
return QString::fromStdString(error.message());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveNewFormat(style.toUtf8());
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ClangFormatFile::changeField(Field field)
|
|
|
|
|
{
|
|
|
|
|
return changeFields({field});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ClangFormatFile::changeFields(QList<Field> fields)
|
|
|
|
|
{
|
|
|
|
|
std::stringstream content;
|
|
|
|
|
content << "---" << "\n";
|
|
|
|
|
|
|
|
|
|
for (const auto &field : fields) {
|
|
|
|
|
content << field.first.toStdString() << ": " << field.second.toStdString() << "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return setStyle(QString::fromStdString(content.str()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatFile::saveNewFormat()
|
|
|
|
|
{
|
2022-04-04 14:53:05 +02:00
|
|
|
if (m_isReadOnly)
|
|
|
|
|
return;
|
2021-09-28 10:26:34 +02:00
|
|
|
|
2022-04-04 14:53:05 +02:00
|
|
|
saveStyleToFile(m_style, m_filePath);
|
2021-09-28 10:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatFile::saveNewFormat(QByteArray style)
|
|
|
|
|
{
|
2022-04-04 14:53:05 +02:00
|
|
|
if (m_isReadOnly)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-09-28 10:26:34 +02:00
|
|
|
m_filePath.writeFileContents(style);
|
|
|
|
|
}
|
2021-11-01 16:20:08 +01:00
|
|
|
|
2023-01-16 12:33:43 +01:00
|
|
|
void ClangFormatFile::saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath)
|
|
|
|
|
{
|
|
|
|
|
std::string styleStr = clang::format::configurationAsText(style);
|
|
|
|
|
|
|
|
|
|
// workaround: configurationAsText() add comment "# " before BasedOnStyle line
|
|
|
|
|
const int pos = styleStr.find("# BasedOnStyle");
|
|
|
|
|
if (pos != int(std::string::npos))
|
|
|
|
|
styleStr.erase(pos, 2);
|
|
|
|
|
styleStr.append("\n");
|
|
|
|
|
filePath.writeFileContents(QByteArray::fromStdString(styleStr));
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-01 16:20:08 +01:00
|
|
|
CppEditor::CppCodeStyleSettings ClangFormatFile::toCppCodeStyleSettings(
|
|
|
|
|
ProjectExplorer::Project *project) const
|
|
|
|
|
{
|
|
|
|
|
using namespace clang::format;
|
|
|
|
|
auto settings = CppEditor::CppCodeStyleSettings::getProjectCodeStyle(project);
|
|
|
|
|
|
|
|
|
|
FormatStyle style;
|
|
|
|
|
style.Language = clang::format::FormatStyle::LK_Cpp;
|
2022-09-09 13:48:08 +02:00
|
|
|
const std::error_code error
|
|
|
|
|
= parseConfiguration(m_filePath.fileContents().value_or(QByteArray()).toStdString(), &style);
|
2021-11-01 16:20:08 +01:00
|
|
|
QTC_ASSERT(error.value() == static_cast<int>(ParseError::Success), return settings);
|
|
|
|
|
|
|
|
|
|
// Modifier offset should be opposite to indent width in order indentAccessSpecifiers
|
|
|
|
|
// to be false
|
2021-12-01 19:19:09 +01:00
|
|
|
settings.indentAccessSpecifiers = (style.AccessModifierOffset != -1 * int(style.IndentWidth));
|
2021-11-01 16:20:08 +01:00
|
|
|
|
|
|
|
|
settings.indentNamespaceBody = style.NamespaceIndentation
|
|
|
|
|
== FormatStyle::NamespaceIndentationKind::NI_All;
|
|
|
|
|
settings.indentNamespaceBraces = settings.indentNamespaceBody;
|
|
|
|
|
|
|
|
|
|
settings.indentClassBraces = style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
|
|
|
|
|
settings.indentEnumBraces = settings.indentClassBraces;
|
|
|
|
|
settings.indentBlockBraces = settings.indentClassBraces;
|
|
|
|
|
settings.indentFunctionBraces = settings.indentClassBraces;
|
|
|
|
|
|
|
|
|
|
settings.indentSwitchLabels = style.IndentCaseLabels;
|
2021-11-25 06:59:02 +01:00
|
|
|
#if LLVM_VERSION_MAJOR >= 11
|
2021-11-01 16:20:08 +01:00
|
|
|
settings.indentBlocksRelativeToSwitchLabels = style.IndentCaseBlocks;
|
2021-11-25 06:59:02 +01:00
|
|
|
#endif
|
2021-11-01 16:20:08 +01:00
|
|
|
if (style.DerivePointerAlignment
|
2022-05-12 12:51:51 +02:00
|
|
|
&& ClangFormatSettings::instance().mode() == ClangFormatSettings::Mode::Formatting) {
|
2021-11-01 16:20:08 +01:00
|
|
|
settings.bindStarToIdentifier = style.PointerAlignment == FormatStyle::PAS_Right;
|
|
|
|
|
settings.bindStarToTypeName = style.PointerAlignment == FormatStyle::PAS_Left;
|
|
|
|
|
settings.bindStarToLeftSpecifier = style.PointerAlignment == FormatStyle::PAS_Left;
|
|
|
|
|
settings.bindStarToRightSpecifier = style.PointerAlignment == FormatStyle::PAS_Right;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 11:39:33 +02:00
|
|
|
settings.extraPaddingForConditionsIfConfusingAlign = style.BreakBeforeBinaryOperators
|
|
|
|
|
== FormatStyle::BOS_All;
|
|
|
|
|
settings.alignAssignments = style.BreakBeforeBinaryOperators == FormatStyle::BOS_All
|
|
|
|
|
|| style.BreakBeforeBinaryOperators
|
|
|
|
|
== FormatStyle::BOS_NonAssignment;
|
|
|
|
|
|
2021-11-01 16:20:08 +01:00
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatFile::fromCppCodeStyleSettings(const CppEditor::CppCodeStyleSettings &settings)
|
|
|
|
|
{
|
2023-06-07 16:17:29 +02:00
|
|
|
::fromCppCodeStyleSettings(m_style, settings);
|
2021-11-01 16:20:08 +01:00
|
|
|
saveNewFormat();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::TabSettings ClangFormatFile::toTabSettings(ProjectExplorer::Project *project) const
|
|
|
|
|
{
|
|
|
|
|
using namespace clang::format;
|
|
|
|
|
auto settings = CppEditor::CppCodeStyleSettings::getProjectTabSettings(project);
|
|
|
|
|
|
|
|
|
|
FormatStyle style;
|
|
|
|
|
style.Language = clang::format::FormatStyle::LK_Cpp;
|
2022-09-09 13:48:08 +02:00
|
|
|
const std::error_code error
|
|
|
|
|
= parseConfiguration(m_filePath.fileContents().value_or(QByteArray()).toStdString(), &style);
|
2021-11-01 16:20:08 +01:00
|
|
|
QTC_ASSERT(error.value() == static_cast<int>(ParseError::Success), return settings);
|
|
|
|
|
|
|
|
|
|
settings.m_indentSize = style.IndentWidth;
|
|
|
|
|
settings.m_tabSize = style.TabWidth;
|
|
|
|
|
|
|
|
|
|
switch (style.UseTab) {
|
2021-11-25 06:59:02 +01:00
|
|
|
#if LLVM_VERSION_MAJOR >= 11
|
2021-11-01 16:20:08 +01:00
|
|
|
case FormatStyle::UT_AlignWithSpaces:
|
2021-11-25 06:59:02 +01:00
|
|
|
#endif
|
2021-11-01 16:20:08 +01:00
|
|
|
case FormatStyle::UT_ForIndentation:
|
|
|
|
|
case FormatStyle::UT_ForContinuationAndIndentation:
|
|
|
|
|
settings.m_tabPolicy = TextEditor::TabSettings::TabPolicy::MixedTabPolicy;
|
|
|
|
|
break;
|
|
|
|
|
case FormatStyle::UT_Never:
|
|
|
|
|
settings.m_tabPolicy = TextEditor::TabSettings::TabPolicy::SpacesOnlyTabPolicy;
|
|
|
|
|
break;
|
|
|
|
|
case FormatStyle::UT_Always:
|
|
|
|
|
settings.m_tabPolicy = TextEditor::TabSettings::TabPolicy::TabsOnlyTabPolicy;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangFormatFile::fromTabSettings(const TextEditor::TabSettings &settings)
|
|
|
|
|
{
|
2023-06-07 16:17:29 +02:00
|
|
|
::fromTabSettings(m_style, settings);
|
2021-11-01 16:20:08 +01:00
|
|
|
saveNewFormat();
|
|
|
|
|
}
|