ClangFormat: Parse clang-format file when needed

Previously the .clang-format was parsed even if there is an override
active.

Fixed .clang-format to follow style-guide more closely.

Change-Id: I39c5e5793cfe9e8a996e084e4ba169231a9bcebb
Reviewed-by: Artem Sokolovskii <artem.sokolovskii@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2022-12-13 08:42:07 +01:00
committed by hjk
parent f81724f458
commit d63b2ca51f
2 changed files with 36 additions and 25 deletions

View File

@@ -90,7 +90,7 @@ NamespaceIndentation: None
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 150
PenaltyBreakAssignment: 88
PenaltyBreakBeforeFirstCallParameter: 300
PenaltyBreakComment: 500
PenaltyBreakFirstLessLess: 400

View File

@@ -8,9 +8,11 @@
#include "llvmfilesystem.h"
#include <coreplugin/icore.h>
#include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/project.h>
#include <projectexplorer/session.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/texteditorsettings.h>
@@ -737,39 +739,48 @@ void ClangFormatBaseIndenter::autoIndent(const QTextCursor &cursor,
}
}
clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const
clang::format::FormatStyle overrideStyle(const ProjectExplorer::Project *projectForFile)
{
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder = clang::format::getStyle(
"file", m_fileName.toFSPathString().toStdString(), "none", "", &llvmFileSystemAdapter);
const ProjectExplorer::Project *projectForFile
= ProjectExplorer::SessionManager::projectForFile(m_fileName);
const bool overrideStyleFile
= projectForFile ? projectForFile->namedSettings(Constants::OVERRIDE_FILE_ID).toBool()
: ClangFormatSettings::instance().overrideDefaultFile();
const TextEditor::ICodeStylePreferences *preferences
= projectForFile
? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
: TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences();
if (!styleFromProjectFolder || overrideStyleFile
|| *styleFromProjectFolder == clang::format::getNoStyle()) {
Utils::FilePath filePath = filePathToCurrentSettings(preferences);
Utils::FilePath filePath = filePathToCurrentSettings(preferences);
if (!filePath.exists())
return qtcStyle();
if (!filePath.exists())
return qtcStyle();
clang::format::FormatStyle currentSettingsStyle;
currentSettingsStyle.Language = clang::format::FormatStyle::LK_Cpp;
const std::error_code error = clang::format::parseConfiguration(filePath.fileContents()
.value_or(QByteArray())
.toStdString(),
&currentSettingsStyle);
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
return qtcStyle());
clang::format::FormatStyle currentSettingsStyle;
currentSettingsStyle.Language = clang::format::FormatStyle::LK_Cpp;
const std::error_code error = clang::format::parseConfiguration(filePath.fileContents()
.value_or(QByteArray())
.toStdString(),
&currentSettingsStyle);
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
return qtcStyle());
return currentSettingsStyle;
}
return currentSettingsStyle;
}
clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const
{
const ProjectExplorer::Project *projectForFile
= ProjectExplorer::SessionManager::projectForFile(m_fileName);
const bool overrideStyleFile
= projectForFile ? projectForFile->namedSettings(Constants::OVERRIDE_FILE_ID).toBool()
: ClangFormatSettings::instance().overrideDefaultFile();
if (overrideStyleFile)
return overrideStyle(projectForFile);
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
= clang::format::getStyle("file",
m_fileName.toFSPathString().toStdString(),
"none",
"",
&llvmFileSystemAdapter);
if (styleFromProjectFolder) {
addQtcStatementMacros(*styleFromProjectFolder);