diff --git a/.clang-format b/.clang-format index 97f7f2b2347..1b9871712f1 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 4afe16e3795..ed17d7c073b 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -8,9 +8,11 @@ #include "llvmfilesystem.h" #include + #include #include #include + #include #include @@ -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 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(), - ¤tSettingsStyle); - QTC_ASSERT(error.value() == static_cast(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(), + ¤tSettingsStyle); + QTC_ASSERT(error.value() == static_cast(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 styleFromProjectFolder + = clang::format::getStyle("file", + m_fileName.toFSPathString().toStdString(), + "none", + "", + &llvmFileSystemAdapter); if (styleFromProjectFolder) { addQtcStatementMacros(*styleFromProjectFolder);