summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Tillmanns <marcus.tillmanns@qt.io>2022-12-13 08:42:07 +0100
committerhjk <hjk@qt.io>2022-12-20 11:39:33 +0000
commitd63b2ca51f4a48358fc7c2ae635a88ac60beda38 (patch)
treec95a850332207c923f974908a0d78615ee31c8f2
parentf81724f45819ae6c08b8a9d0390b1eeadc9ce4df (diff)
downloadqt-creator-d63b2ca51f4a48358fc7c2ae635a88ac60beda38.tar.gz
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>
-rw-r--r--.clang-format2
-rw-r--r--src/plugins/clangformat/clangformatbaseindenter.cpp59
2 files changed, 36 insertions, 25 deletions
diff --git a/.clang-format b/.clang-format
index 97f7f2b234..1b9871712f 100644
--- a/.clang-format
+++ b/.clang-format
@@ -90,7 +90,7 @@ NamespaceIndentation: None
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
-PenaltyBreakAssignment: 150
+PenaltyBreakAssignment: 88
PenaltyBreakBeforeFirstCallParameter: 300
PenaltyBreakComment: 500
PenaltyBreakFirstLessLess: 400
diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp
index 4afe16e379..ed17d7c073 100644
--- a/src/plugins/clangformat/clangformatbaseindenter.cpp
+++ b/src/plugins/clangformat/clangformatbaseindenter.cpp
@@ -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);