forked from qt-creator/qt-creator
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:
@@ -113,7 +113,7 @@ bool ClangFormatPlugin::initialize(const QStringList &arguments, QString *errorS
|
|||||||
[openClangFormatConfigAction] {
|
[openClangFormatConfigAction] {
|
||||||
const FilePath fileName = FilePath::fromVariant(openClangFormatConfigAction->data());
|
const FilePath fileName = FilePath::fromVariant(openClangFormatConfigAction->data());
|
||||||
if (!fileName.isEmpty())
|
if (!fileName.isEmpty())
|
||||||
EditorManager::openEditor(FilePath::fromString(configForFile(fileName)));
|
EditorManager::openEditor(configForFile(fileName));
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(EditorManager::instance(),
|
connect(EditorManager::instance(),
|
||||||
|
@@ -176,16 +176,6 @@ clang::format::FormatStyle qtcStyle()
|
|||||||
return style;
|
return style;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool useGlobalOverriddenSettings()
|
|
||||||
{
|
|
||||||
return ClangFormatSettings::instance().overrideDefaultFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
QString currentProjectUniqueId()
|
|
||||||
{
|
|
||||||
return projectUniqueId(SessionManager::startupProject());
|
|
||||||
}
|
|
||||||
|
|
||||||
QString projectUniqueId(ProjectExplorer::Project *project)
|
QString projectUniqueId(ProjectExplorer::Project *project)
|
||||||
{
|
{
|
||||||
if (!project)
|
if (!project)
|
||||||
@@ -196,12 +186,6 @@ QString projectUniqueId(ProjectExplorer::Project *project)
|
|||||||
.toHex(0));
|
.toHex(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool useProjectOverriddenSettings()
|
|
||||||
{
|
|
||||||
const Project *project = SessionManager::startupProject();
|
|
||||||
return getProjectOverriddenSettings(project);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project)
|
bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project)
|
||||||
{
|
{
|
||||||
const QVariant projectUseGlobalSettings = project ? project->namedSettings(
|
const QVariant projectUseGlobalSettings = project ? project->namedSettings(
|
||||||
@@ -253,55 +237,37 @@ ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils:
|
|||||||
: getProjectIndentationOrFormattingSettings(project);
|
: 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()
|
Utils::FilePath settingsAltFilePath = parentDirectory / Constants::SETTINGS_FILE_ALT_NAME;
|
||||||
{
|
if (settingsAltFilePath.exists())
|
||||||
const Project *project = SessionManager::startupProject();
|
return settingsAltFilePath;
|
||||||
if (project)
|
|
||||||
return globalPath().pathAppended("clang-format/" + currentProjectUniqueId());
|
|
||||||
|
|
||||||
|
parentDirectory = parentDirectory.parentDir();
|
||||||
|
}
|
||||||
return Utils::FilePath();
|
return Utils::FilePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
static QString findConfig(Utils::FilePath fileName)
|
Utils::FilePath configForFile(const Utils::FilePath &fileName)
|
||||||
{
|
{
|
||||||
QDir parentDir(fileName.parentDir().toString());
|
if (!getCurrentOverriddenSettings(fileName))
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return findConfig(fileName);
|
return findConfig(fileName);
|
||||||
}
|
|
||||||
|
|
||||||
QString configForFile(Utils::FilePath fileName)
|
const ProjectExplorer::Project *projectForFile
|
||||||
{
|
= ProjectExplorer::SessionManager::projectForFile(fileName);
|
||||||
return configForFile(fileName, true);
|
|
||||||
|
const TextEditor::ICodeStylePreferences *preferences
|
||||||
|
= projectForFile
|
||||||
|
? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
|
||||||
|
: TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences();
|
||||||
|
|
||||||
|
return filePathToCurrentSettings(preferences);
|
||||||
}
|
}
|
||||||
|
|
||||||
void addQtcStatementMacros(clang::format::FormatStyle &style)
|
void addQtcStatementMacros(clang::format::FormatStyle &style)
|
||||||
|
@@ -29,8 +29,8 @@ ClangFormatSettings::Mode getProjectIndentationOrFormattingSettings(
|
|||||||
const ProjectExplorer::Project *project);
|
const ProjectExplorer::Project *project);
|
||||||
ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils::FilePath &filePath);
|
ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils::FilePath &filePath);
|
||||||
|
|
||||||
// Is the style from the matching .clang-format file or global one if it's not found.
|
Utils::FilePath configForFile(const Utils::FilePath &fileName);
|
||||||
QString configForFile(Utils::FilePath fileName);
|
Utils::FilePath findConfig(const Utils::FilePath &fileName);
|
||||||
|
|
||||||
bool getProjectOverriddenSettings(const ProjectExplorer::Project *project);
|
bool getProjectOverriddenSettings(const ProjectExplorer::Project *project);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user