ClangFormat: Start to use new file management

- Indenter uses .clang-format file from a dir of
editing file or from a parent dir. If there is no
such file then indenter starts to use file from a dir
with the name current CodeStyle settings.
- Test fixed

ToDo: Add file absentness processing in case of import

Change-Id: If09ef0c598899856b948c214e524bcfd5dad76e2
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-04-08 15:12:30 +02:00
parent 3f9c1d5d2b
commit 72dd90e512
8 changed files with 37 additions and 14 deletions

View File

@@ -24,7 +24,11 @@
****************************************************************************/ ****************************************************************************/
#include "clangformatbaseindenter.h" #include "clangformatbaseindenter.h"
#include "clangformatconstants.h"
#include "clangformatutils.h" #include "clangformatutils.h"
#include <coreplugin/icore.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/icodestylepreferences.h>
#include <clang/Tooling/Core/Replacement.h> #include <clang/Tooling/Core/Replacement.h>
@@ -728,8 +732,22 @@ void ClangFormatBaseIndenter::autoIndent(const QTextCursor &cursor,
clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const
{ {
llvm::Expected<clang::format::FormatStyle> style llvm::Expected<clang::format::FormatStyle> style
= clang::format::getStyle("file", m_fileName.toString().toStdString(), "none"); = clang::format::getStyle("file", m_fileName.path().toStdString(), "none");
if (style) { if (style) {
if (*style == clang::format::getNoStyle()) {
Utils::FilePath filePath = filePathToCurrentSettings(
TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences());
clang::format::FormatStyle style;
style.Language = clang::format::FormatStyle::LK_Cpp;
const std::error_code error
= clang::format::parseConfiguration(filePath.fileContents().toStdString(), &style);
QTC_ASSERT(error.value() == static_cast<int>(clang::format::ParseError::Success),
return qtcStyle());
return style;
}
addQtcStatementMacros(*style); addQtcStatementMacros(*style);
return *style; return *style;
} }

View File

@@ -68,8 +68,9 @@ public:
Utils::optional<int> margin() const override; Utils::optional<int> margin() const override;
clang::format::FormatStyle styleForFile() const;
protected: protected:
virtual clang::format::FormatStyle styleForFile() const;
virtual bool formatCodeInsteadOfIndent() const { return false; } virtual bool formatCodeInsteadOfIndent() const { return false; }
virtual bool formatWhileTyping() const { return false; } virtual bool formatWhileTyping() const { return false; }
virtual int lastSaveRevision() const { return 0; } virtual int lastSaveRevision() const { return 0; }

View File

@@ -113,11 +113,8 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
m_ui->setupUi(this); m_ui->setupUi(this);
setEnabled(!codeStyle->isReadOnly()); setEnabled(!codeStyle->isReadOnly());
Utils::FilePath filePath = Core::ICore::userResourcePath(); m_config = std::make_unique<ClangFormatFile>(filePathToCurrentSettings(codeStyle),
filePath = filePath / "clang-format/" codeStyle->isReadOnly());
/ Utils::FileUtils::fileSystemFriendlyName(codeStyle->displayName())
/ QLatin1String(Constants::SETTINGS_FILE_NAME);
m_config = std::make_unique<ClangFormatFile>(filePath, codeStyle->isReadOnly());
initChecksAndPreview(); initChecksAndPreview();
showCombobox(); showCombobox();

View File

@@ -40,11 +40,6 @@ ClangFormatIndenter::ClangFormatIndenter(QTextDocument *doc)
: ClangFormatBaseIndenter(doc) : ClangFormatBaseIndenter(doc)
{} {}
FormatStyle ClangFormatIndenter::styleForFile() const
{
return ClangFormat::styleForFile(m_fileName);
}
bool ClangFormatIndenter::formatCodeInsteadOfIndent() const bool ClangFormatIndenter::formatCodeInsteadOfIndent() const
{ {
return ClangFormatSettings::instance().formatCodeInsteadOfIndent(); return ClangFormatSettings::instance().formatCodeInsteadOfIndent();

View File

@@ -41,7 +41,6 @@ public:
private: private:
bool formatCodeInsteadOfIndent() const override; bool formatCodeInsteadOfIndent() const override;
bool formatWhileTyping() const override; bool formatWhileTyping() const override;
clang::format::FormatStyle styleForFile() const override;
int lastSaveRevision() const override; int lastSaveRevision() const override;
}; };

View File

@@ -30,7 +30,9 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <cppeditor/cppcodestylesettings.h> #include <cppeditor/cppcodestylesettings.h>
#include <texteditor/icodestylepreferences.h>
#include <texteditor/tabsettings.h> #include <texteditor/tabsettings.h>
#include <texteditor/texteditorsettings.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
@@ -208,6 +210,7 @@ void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath)
const int pos = styleStr.find("# BasedOnStyle"); const int pos = styleStr.find("# BasedOnStyle");
if (pos != int(std::string::npos)) if (pos != int(std::string::npos))
styleStr.erase(pos, 2); styleStr.erase(pos, 2);
styleStr.append("\n");
filePath.writeFileContents(QByteArray::fromStdString(styleStr)); filePath.writeFileContents(QByteArray::fromStdString(styleStr));
} }
@@ -382,6 +385,13 @@ void addQtcStatementMacros(clang::format::FormatStyle &style)
} }
} }
Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle)
{
return Core::ICore::userResourcePath() / "clang-format/"
/ Utils::FileUtils::fileSystemFriendlyName(codeStyle->displayName())
/ QLatin1String(Constants::SETTINGS_FILE_NAME);
}
std::string readFile(const QString &path) std::string readFile(const QString &path)
{ {
const std::string defaultStyle = clang::format::configurationAsText(qtcStyle()); const std::string defaultStyle = clang::format::configurationAsText(qtcStyle());

View File

@@ -34,6 +34,7 @@
#include <fstream> #include <fstream>
namespace TextEditor { class ICodeStylePreferences; }
namespace ClangFormat { namespace ClangFormat {
// Creates the style for the current project or the global style if needed. // Creates the style for the current project or the global style if needed.
@@ -55,4 +56,6 @@ void saveStyleToFile(clang::format::FormatStyle style, Utils::FilePath filePath)
void addQtcStatementMacros(clang::format::FormatStyle &style); void addQtcStatementMacros(clang::format::FormatStyle &style);
clang::format::FormatStyle qtcStyle(); clang::format::FormatStyle qtcStyle();
Utils::FilePath filePathToCurrentSettings(const TextEditor::ICodeStylePreferences *codeStyle);
} }

View File

@@ -52,7 +52,7 @@ public:
ClangFormatExtendedTestIndenter(QTextDocument *doc) : ClangFormatTestIndenter(doc) {} ClangFormatExtendedTestIndenter(QTextDocument *doc) : ClangFormatTestIndenter(doc) {}
private: private:
bool formatWhileTyping() const override { return true; } bool formatCodeInsteadOfIndent() const override { return true; }
}; };
ClangFormatTest::ClangFormatTest() ClangFormatTest::ClangFormatTest()