ClangFormat: Fix the "Open Used .clang-format File" function

- FilePathify related functions
- Logic of related functions were updated to use new configuration
files location and new settings variables

Change-Id: I9f5504fc72f5c051a83dc80e1feb2e13272de146
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2023-01-16 12:40:41 +01:00
parent f0f0cf129a
commit a20c4c4490
3 changed files with 24 additions and 58 deletions

View File

@@ -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(),

View File

@@ -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 (parentDir.exists(Constants::SETTINGS_FILE_NAME))
return parentDir.filePath(Constants::SETTINGS_FILE_NAME);
return parentDir.filePath(Constants::SETTINGS_FILE_ALT_NAME);
}
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);
}
if (!checkForSettings || useGlobalOverriddenSettings()) {
overrideDir.setPath(globalPath().toString());
if (!overrideDir.isEmpty() && overrideDir.exists(Constants::SETTINGS_FILE_NAME))
return overrideDir.filePath(Constants::SETTINGS_FILE_NAME);
}
if (!getCurrentOverriddenSettings(fileName))
return findConfig(fileName);
}
QString configForFile(Utils::FilePath fileName)
{
return configForFile(fileName, true);
const ProjectExplorer::Project *projectForFile
= ProjectExplorer::SessionManager::projectForFile(fileName);
const TextEditor::ICodeStylePreferences *preferences
= projectForFile
? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
: TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences();
return filePathToCurrentSettings(preferences);
}
void addQtcStatementMacros(clang::format::FormatStyle &style)

View File

@@ -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);