diff --git a/src/plugins/clangformat/clangformatplugin.cpp b/src/plugins/clangformat/clangformatplugin.cpp index e3a6ae8e2a5..d8428e35f0b 100644 --- a/src/plugins/clangformat/clangformatplugin.cpp +++ b/src/plugins/clangformat/clangformatplugin.cpp @@ -113,7 +113,7 @@ bool ClangFormatPlugin::initialize(const QStringList &arguments, QString *errorS [openClangFormatConfigAction] { const FilePath fileName = FilePath::fromVariant(openClangFormatConfigAction->data()); if (!fileName.isEmpty()) - EditorManager::openEditor(FilePath::fromString(configForFile(fileName))); + EditorManager::openEditor(configForFile(fileName)); }); connect(EditorManager::instance(), diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp index 72464ac88dc..c7eb900fed5 100644 --- a/src/plugins/clangformat/clangformatutils.cpp +++ b/src/plugins/clangformat/clangformatutils.cpp @@ -176,16 +176,6 @@ clang::format::FormatStyle qtcStyle() return style; } -static bool useGlobalOverriddenSettings() -{ - return ClangFormatSettings::instance().overrideDefaultFile(); -} - -QString currentProjectUniqueId() -{ - return projectUniqueId(SessionManager::startupProject()); -} - QString projectUniqueId(ProjectExplorer::Project *project) { if (!project) @@ -196,12 +186,6 @@ QString projectUniqueId(ProjectExplorer::Project *project) .toHex(0)); } -static bool useProjectOverriddenSettings() -{ - const Project *project = SessionManager::startupProject(); - return getProjectOverriddenSettings(project); -} - bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project) { const QVariant projectUseGlobalSettings = project ? project->namedSettings( @@ -253,55 +237,37 @@ ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils: : getProjectIndentationOrFormattingSettings(project); } -static Utils::FilePath globalPath() +Utils::FilePath findConfig(const Utils::FilePath &fileName) { - return Core::ICore::userResourcePath(); -} + Utils::FilePath parentDirectory = fileName.parentDir(); + while (parentDirectory.exists()) { + Utils::FilePath settingsFilePath = parentDirectory / Constants::SETTINGS_FILE_NAME; + if (settingsFilePath.exists()) + return settingsFilePath; -static Utils::FilePath projectPath() -{ - const Project *project = SessionManager::startupProject(); - if (project) - return globalPath().pathAppended("clang-format/" + currentProjectUniqueId()); + Utils::FilePath settingsAltFilePath = parentDirectory / Constants::SETTINGS_FILE_ALT_NAME; + if (settingsAltFilePath.exists()) + return settingsAltFilePath; + parentDirectory = parentDirectory.parentDir(); + } return Utils::FilePath(); } -static QString findConfig(Utils::FilePath fileName) +Utils::FilePath configForFile(const Utils::FilePath &fileName) { - QDir parentDir(fileName.parentDir().toString()); - while (!parentDir.exists(Constants::SETTINGS_FILE_NAME) - && !parentDir.exists(Constants::SETTINGS_FILE_ALT_NAME)) { - if (!parentDir.cdUp()) - return QString(); - } + if (!getCurrentOverriddenSettings(fileName)) + return findConfig(fileName); - if (parentDir.exists(Constants::SETTINGS_FILE_NAME)) - return parentDir.filePath(Constants::SETTINGS_FILE_NAME); - return parentDir.filePath(Constants::SETTINGS_FILE_ALT_NAME); -} + const ProjectExplorer::Project *projectForFile + = ProjectExplorer::SessionManager::projectForFile(fileName); -static QString configForFile(Utils::FilePath fileName, bool checkForSettings) -{ - QDir overrideDir; - if (!checkForSettings || useProjectOverriddenSettings()) { - overrideDir.setPath(projectPath().toString()); - if (!overrideDir.isEmpty() && overrideDir.exists(Constants::SETTINGS_FILE_NAME)) - return overrideDir.filePath(Constants::SETTINGS_FILE_NAME); - } + const TextEditor::ICodeStylePreferences *preferences + = projectForFile + ? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences() + : TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences(); - if (!checkForSettings || useGlobalOverriddenSettings()) { - overrideDir.setPath(globalPath().toString()); - if (!overrideDir.isEmpty() && overrideDir.exists(Constants::SETTINGS_FILE_NAME)) - return overrideDir.filePath(Constants::SETTINGS_FILE_NAME); - } - - return findConfig(fileName); -} - -QString configForFile(Utils::FilePath fileName) -{ - return configForFile(fileName, true); + return filePathToCurrentSettings(preferences); } void addQtcStatementMacros(clang::format::FormatStyle &style) diff --git a/src/plugins/clangformat/clangformatutils.h b/src/plugins/clangformat/clangformatutils.h index de796c75dd0..f7d67e7dc83 100644 --- a/src/plugins/clangformat/clangformatutils.h +++ b/src/plugins/clangformat/clangformatutils.h @@ -29,8 +29,8 @@ ClangFormatSettings::Mode getProjectIndentationOrFormattingSettings( const ProjectExplorer::Project *project); ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils::FilePath &filePath); -// Is the style from the matching .clang-format file or global one if it's not found. -QString configForFile(Utils::FilePath fileName); +Utils::FilePath configForFile(const Utils::FilePath &fileName); +Utils::FilePath findConfig(const Utils::FilePath &fileName); bool getProjectOverriddenSettings(const ProjectExplorer::Project *project);