summaryrefslogtreecommitdiff
path: root/src/plugins/clangformat
diff options
context:
space:
mode:
authorIvan Donchevskii <ivan.donchevskii@qt.io>2019-03-05 17:14:42 +0100
committerIvan Donchevskii <ivan.donchevskii@qt.io>2019-03-06 08:44:05 +0000
commitdc9c9249fcfb822a02131f4121e2b71522a0066c (patch)
treeff68921fe6891627317942a8de03c50630470971 /src/plugins/clangformat
parent5bb8e678467ae6b1ee9d15ead673b06543740969 (diff)
downloadqt-creator-dc9c9249fcfb822a02131f4121e2b71522a0066c.tar.gz
ClangFormat: Try to use existing file to override settings
If there is a .clang-format file in the project use it as a base for the custom file that creator uses to override the project settings. Change-Id: I0786dbdd6077b87d4dd428981e24d503668f1031 Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Diffstat (limited to 'src/plugins/clangformat')
-rw-r--r--src/plugins/clangformat/clangformatutils.cpp36
1 files changed, 26 insertions, 10 deletions
diff --git a/src/plugins/clangformat/clangformatutils.cpp b/src/plugins/clangformat/clangformatutils.cpp
index 36bcf2f97d..bfe0e43cfd 100644
--- a/src/plugins/clangformat/clangformatutils.cpp
+++ b/src/plugins/clangformat/clangformatutils.cpp
@@ -131,6 +131,20 @@ static Utils::FileName projectPath()
return Utils::FileName();
}
+static QString findConfig(Utils::FileName 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::FileName fileName, bool checkForSettings)
{
QDir overrideDir;
@@ -146,16 +160,7 @@ static QString configForFile(Utils::FileName fileName, bool checkForSettings)
return overrideDir.filePath(Constants::SETTINGS_FILE_NAME);
}
- 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);
+ return findConfig(fileName);
}
static clang::format::FormatStyle constructStyle(bool isGlobal,
@@ -201,6 +206,17 @@ void createStyleFileIfNeeded(bool isGlobal)
return;
QDir().mkpath(path.parentDir().toString());
+ if (!isGlobal) {
+ const Project *project = SessionManager::startupProject();
+ Utils::FileName possibleProjectConfig = project->rootProjectDirectory().appendPath(
+ Constants::SETTINGS_FILE_NAME);
+ if (possibleProjectConfig.exists()) {
+ // Just copy th .clang-format if current project has one.
+ QFile::copy(possibleProjectConfig.toString(), configFile);
+ return;
+ }
+ }
+
std::fstream newStyleFile(configFile.toStdString(), std::fstream::out);
if (newStyleFile.is_open()) {
newStyleFile << clang::format::configurationAsText(constructStyle(isGlobal));