forked from qt-creator/qt-creator
ClangFormat: Move the functions from clangformatutils
- Moved the functions from clangformatutils to the places where it is belong Change-Id: Ia4108c5b096610170e0f9d16d15d40d5538ffbdc Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
#include "clangformatconstants.h"
|
||||
#include "clangformatindenter.h"
|
||||
#include "clangformatfile.h"
|
||||
#include "clangformatsettings.h"
|
||||
#include "clangformatutils.h"
|
||||
|
||||
// the file was generated by scripts/generateClangFormatChecksLayout.py
|
||||
@@ -150,6 +149,62 @@ void ClangFormatConfigWidget::connectChecks()
|
||||
}
|
||||
}
|
||||
|
||||
static clang::format::FormatStyle constructStyle(const QByteArray &baseStyle = QByteArray())
|
||||
{
|
||||
if (!baseStyle.isEmpty()) {
|
||||
// Try to get the style for this base style.
|
||||
llvm::Expected<clang::format::FormatStyle> style
|
||||
= clang::format::getStyle(baseStyle.toStdString(), "dummy.cpp", baseStyle.toStdString());
|
||||
if (style)
|
||||
return *style;
|
||||
|
||||
handleAllErrors(style.takeError(), [](const llvm::ErrorInfoBase &) {
|
||||
// do nothing
|
||||
});
|
||||
// Fallthrough to the default style.
|
||||
}
|
||||
return qtcStyle();
|
||||
}
|
||||
|
||||
Utils::FilePath ClangFormatConfigWidget::globalPath()
|
||||
{
|
||||
return Core::ICore::userResourcePath();
|
||||
}
|
||||
|
||||
Utils::FilePath ClangFormatConfigWidget::projectPath()
|
||||
{
|
||||
if (m_project)
|
||||
return globalPath().pathAppended("clang-format/" + projectUniqueId(m_project));
|
||||
|
||||
return Utils::FilePath();
|
||||
}
|
||||
|
||||
void ClangFormatConfigWidget::createStyleFileIfNeeded(bool isGlobal)
|
||||
{
|
||||
const FilePath path = isGlobal ? globalPath() : projectPath();
|
||||
const FilePath configFile = path / Constants::SETTINGS_FILE_NAME;
|
||||
|
||||
if (configFile.exists())
|
||||
return;
|
||||
|
||||
QDir().mkpath(path.toString());
|
||||
if (!isGlobal) {
|
||||
FilePath possibleProjectConfig = m_project->rootProjectDirectory()
|
||||
/ Constants::SETTINGS_FILE_NAME;
|
||||
if (possibleProjectConfig.exists()) {
|
||||
// Just copy th .clang-format if current project has one.
|
||||
possibleProjectConfig.copyFile(configFile);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::fstream newStyleFile(configFile.toString().toStdString(), std::fstream::out);
|
||||
if (newStyleFile.is_open()) {
|
||||
newStyleFile << clang::format::configurationAsText(constructStyle());
|
||||
newStyleFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
void ClangFormatConfigWidget::showOrHideWidgets()
|
||||
{
|
||||
auto verticalLayout = qobject_cast<QVBoxLayout *>(layout());
|
||||
@@ -244,6 +299,38 @@ static void fillComboBoxOrLineEdit(QObject *object, const std::string &text, siz
|
||||
lineEdit->setText(QString::fromStdString(value));
|
||||
}
|
||||
|
||||
std::string ClangFormatConfigWidget::readFile(const QString &path)
|
||||
{
|
||||
const std::string defaultStyle = clang::format::configurationAsText(qtcStyle());
|
||||
|
||||
QFile file(path);
|
||||
if (!file.open(QFile::ReadOnly))
|
||||
return defaultStyle;
|
||||
|
||||
const std::string content = file.readAll().toStdString();
|
||||
file.close();
|
||||
|
||||
clang::format::FormatStyle style;
|
||||
style.Language = clang::format::FormatStyle::LK_Cpp;
|
||||
const std::error_code error = clang::format::parseConfiguration(content, &style);
|
||||
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
|
||||
return defaultStyle);
|
||||
|
||||
addQtcStatementMacros(style);
|
||||
std::string settings = clang::format::configurationAsText(style);
|
||||
|
||||
// Needed workaround because parseConfiguration remove BasedOnStyle field
|
||||
// ToDo: standardize this behavior for future
|
||||
const size_t index = content.find("BasedOnStyle");
|
||||
if (index != std::string::npos) {
|
||||
const size_t size = content.find("\n", index) - index;
|
||||
const size_t insert_index = settings.find("\n");
|
||||
settings.insert(insert_index, "\n" + content.substr(index, size));
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
void ClangFormatConfigWidget::fillTable()
|
||||
{
|
||||
Utils::GuardLocker locker(m_ignoreChanges);
|
||||
|
@@ -48,11 +48,15 @@ public:
|
||||
private:
|
||||
bool eventFilter(QObject *object, QEvent *event) override;
|
||||
|
||||
Utils::FilePath globalPath();
|
||||
Utils::FilePath projectPath();
|
||||
void createStyleFileIfNeeded(bool isGlobal);
|
||||
void showOrHideWidgets();
|
||||
void initChecksAndPreview();
|
||||
void connectChecks();
|
||||
|
||||
void fillTable();
|
||||
std::string readFile(const QString &path);
|
||||
void saveChanges(QObject *sender);
|
||||
|
||||
void updatePreview();
|
||||
|
@@ -118,6 +118,18 @@ void ClangFormatFile::saveNewFormat(QByteArray style)
|
||||
m_filePath.writeFileContents(style);
|
||||
}
|
||||
|
||||
void ClangFormatFile::saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath)
|
||||
{
|
||||
std::string styleStr = clang::format::configurationAsText(style);
|
||||
|
||||
// workaround: configurationAsText() add comment "# " before BasedOnStyle line
|
||||
const int pos = styleStr.find("# BasedOnStyle");
|
||||
if (pos != int(std::string::npos))
|
||||
styleStr.erase(pos, 2);
|
||||
styleStr.append("\n");
|
||||
filePath.writeFileContents(QByteArray::fromStdString(styleStr));
|
||||
}
|
||||
|
||||
CppEditor::CppCodeStyleSettings ClangFormatFile::toCppCodeStyleSettings(
|
||||
ProjectExplorer::Project *project) const
|
||||
{
|
||||
|
@@ -38,6 +38,7 @@ public:
|
||||
private:
|
||||
void saveNewFormat();
|
||||
void saveNewFormat(QByteArray style);
|
||||
void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath);
|
||||
|
||||
private:
|
||||
Utils::FilePath m_filePath;
|
||||
|
@@ -6,12 +6,17 @@
|
||||
#include "clangformatconstants.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
|
||||
#include <cppeditor/cppcodestylesettings.h>
|
||||
|
||||
#include <texteditor/icodestylepreferences.h>
|
||||
#include <texteditor/tabsettings.h>
|
||||
#include <texteditor/texteditorsettings.h>
|
||||
|
||||
#include <projectexplorer/editorconfiguration.h>
|
||||
#include <projectexplorer/project.h>
|
||||
#include <projectexplorer/session.h>
|
||||
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
#include <QCryptographicHash>
|
||||
@@ -178,7 +183,11 @@ static bool useGlobalOverriddenSettings()
|
||||
|
||||
QString currentProjectUniqueId()
|
||||
{
|
||||
const Project *project = SessionManager::startupProject();
|
||||
return projectUniqueId(SessionManager::startupProject());
|
||||
}
|
||||
|
||||
QString projectUniqueId(ProjectExplorer::Project *project)
|
||||
{
|
||||
if (!project)
|
||||
return QString();
|
||||
|
||||
@@ -187,18 +196,6 @@ QString currentProjectUniqueId()
|
||||
.toHex(0));
|
||||
}
|
||||
|
||||
void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath)
|
||||
{
|
||||
std::string styleStr = clang::format::configurationAsText(style);
|
||||
|
||||
// workaround: configurationAsText() add comment "# " before BasedOnStyle line
|
||||
const int pos = styleStr.find("# BasedOnStyle");
|
||||
if (pos != int(std::string::npos))
|
||||
styleStr.erase(pos, 2);
|
||||
styleStr.append("\n");
|
||||
filePath.writeFileContents(QByteArray::fromStdString(styleStr));
|
||||
}
|
||||
|
||||
static bool useProjectOverriddenSettings()
|
||||
{
|
||||
const Project *project = SessionManager::startupProject();
|
||||
@@ -307,52 +304,6 @@ QString configForFile(Utils::FilePath fileName)
|
||||
return configForFile(fileName, true);
|
||||
}
|
||||
|
||||
static clang::format::FormatStyle constructStyle(const QByteArray &baseStyle = QByteArray())
|
||||
{
|
||||
if (!baseStyle.isEmpty()) {
|
||||
// Try to get the style for this base style.
|
||||
Expected<FormatStyle> style = getStyle(baseStyle.toStdString(),
|
||||
"dummy.cpp",
|
||||
baseStyle.toStdString());
|
||||
if (style)
|
||||
return *style;
|
||||
|
||||
handleAllErrors(style.takeError(), [](const ErrorInfoBase &) {
|
||||
// do nothing
|
||||
});
|
||||
// Fallthrough to the default style.
|
||||
}
|
||||
|
||||
return qtcStyle();
|
||||
}
|
||||
|
||||
void createStyleFileIfNeeded(bool isGlobal)
|
||||
{
|
||||
const FilePath path = isGlobal ? globalPath() : projectPath();
|
||||
const FilePath configFile = path / Constants::SETTINGS_FILE_NAME;
|
||||
|
||||
if (configFile.exists())
|
||||
return;
|
||||
|
||||
QDir().mkpath(path.toString());
|
||||
if (!isGlobal) {
|
||||
const Project *project = SessionManager::startupProject();
|
||||
FilePath possibleProjectConfig = project->rootProjectDirectory()
|
||||
/ Constants::SETTINGS_FILE_NAME;
|
||||
if (possibleProjectConfig.exists()) {
|
||||
// Just copy th .clang-format if current project has one.
|
||||
possibleProjectConfig.copyFile(configFile);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::fstream newStyleFile(configFile.toString().toStdString(), std::fstream::out);
|
||||
if (newStyleFile.is_open()) {
|
||||
newStyleFile << clang::format::configurationAsText(constructStyle());
|
||||
newStyleFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
void addQtcStatementMacros(clang::format::FormatStyle &style)
|
||||
{
|
||||
static const std::vector<std::string> macros = {"Q_OBJECT",
|
||||
@@ -371,35 +322,4 @@ Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreference
|
||||
/ Utils::FileUtils::fileSystemFriendlyName(codeStyle->displayName())
|
||||
/ QLatin1String(Constants::SETTINGS_FILE_NAME);
|
||||
}
|
||||
|
||||
std::string readFile(const QString &path)
|
||||
{
|
||||
const std::string defaultStyle = clang::format::configurationAsText(qtcStyle());
|
||||
|
||||
QFile file(path);
|
||||
if (!file.open(QFile::ReadOnly))
|
||||
return defaultStyle;
|
||||
|
||||
const std::string content = file.readAll().toStdString();
|
||||
file.close();
|
||||
|
||||
clang::format::FormatStyle style;
|
||||
style.Language = clang::format::FormatStyle::LK_Cpp;
|
||||
const std::error_code error = clang::format::parseConfiguration(content, &style);
|
||||
QTC_ASSERT(error.value() == static_cast<int>(ParseError::Success), return defaultStyle);
|
||||
|
||||
addQtcStatementMacros(style);
|
||||
std::string settings = clang::format::configurationAsText(style);
|
||||
|
||||
// Needed workaround because parseConfiguration remove BasedOnStyle field
|
||||
// ToDo: standardize this behavior for future
|
||||
const size_t index = content.find("BasedOnStyle");
|
||||
if (index != std::string::npos) {
|
||||
const size_t size = content.find("\n", index) - index;
|
||||
const size_t insert_index = settings.find("\n");
|
||||
settings.insert(insert_index, "\n" + content.substr(index, size));
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
} // namespace ClangFormat
|
||||
|
@@ -18,12 +18,7 @@ namespace TextEditor { class ICodeStylePreferences; }
|
||||
namespace ProjectExplorer { class Project; }
|
||||
namespace ClangFormat {
|
||||
|
||||
// Creates the style for the current project or the global style if needed.
|
||||
void createStyleFileIfNeeded(bool isGlobal);
|
||||
|
||||
QString currentProjectUniqueId();
|
||||
|
||||
std::string readFile(const QString &path);
|
||||
QString projectUniqueId(ProjectExplorer::Project *project);
|
||||
|
||||
bool getProjectUseGlobalSettings(const ProjectExplorer::Project *project);
|
||||
|
||||
@@ -36,7 +31,8 @@ ClangFormatSettings::Mode getCurrentIndentationOrFormattingSettings(const Utils:
|
||||
|
||||
// Is the style from the matching .clang-format file or global one if it's not found.
|
||||
QString configForFile(Utils::FilePath fileName);
|
||||
void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath);
|
||||
|
||||
bool getProjectOverriddenSettings(const ProjectExplorer::Project *project);
|
||||
|
||||
void addQtcStatementMacros(clang::format::FormatStyle &style);
|
||||
clang::format::FormatStyle qtcStyle();
|
||||
|
Reference in New Issue
Block a user