forked from qt-creator/qt-creator
ClangFormat: Add caching of current style
Previously, ClangFormat was getting the style for a file every time styleForFile was called, which could cause unnecessary delays and impact performance. This commit adds caching of the current style with a timeout of 1s, so that ClangFormat can quickly access the cached style instead of recalculating it each time. Change-Id: I33c114d51d6ce1acd0b6d9d2a28e2b6712e149dd Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -774,10 +774,28 @@ clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName)
|
||||
return currentSettingsStyle;
|
||||
}
|
||||
|
||||
clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const
|
||||
static std::chrono::milliseconds getCacheTimeout()
|
||||
{
|
||||
if (getCurrentOverriddenSettings(m_fileName))
|
||||
return overrideStyle(m_fileName);
|
||||
using namespace std::chrono_literals;
|
||||
bool ok = false;
|
||||
const int envCacheTimeout = qEnvironmentVariableIntValue("CLANG_FORMAT_CACHE_TIMEOUT", &ok);
|
||||
return ok ? std::chrono::milliseconds(envCacheTimeout) : 1s;
|
||||
}
|
||||
|
||||
const clang::format::FormatStyle &ClangFormatBaseIndenter::styleForFile() const
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
static const std::chrono::milliseconds cacheTimeout = getCacheTimeout();
|
||||
|
||||
QDateTime time = QDateTime::currentDateTime();
|
||||
if (m_cachedStyle.expirationTime > time && !(m_cachedStyle.style == clang::format::getNoStyle()))
|
||||
return m_cachedStyle.style;
|
||||
|
||||
if (getCurrentOverriddenSettings(m_fileName)) {
|
||||
clang::format::FormatStyle style = overrideStyle(m_fileName);
|
||||
m_cachedStyle.setCache(style, cacheTimeout);
|
||||
return m_cachedStyle.style;
|
||||
}
|
||||
|
||||
llvm::Expected<clang::format::FormatStyle> styleFromProjectFolder
|
||||
= clang::format::getStyle("file",
|
||||
@@ -788,14 +806,17 @@ clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const
|
||||
|
||||
if (styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle())) {
|
||||
addQtcStatementMacros(*styleFromProjectFolder);
|
||||
return *styleFromProjectFolder;
|
||||
m_cachedStyle.setCache(*styleFromProjectFolder, cacheTimeout);
|
||||
return m_cachedStyle.style;
|
||||
}
|
||||
|
||||
handleAllErrors(styleFromProjectFolder.takeError(), [](const llvm::ErrorInfoBase &) {
|
||||
// do nothing
|
||||
});
|
||||
|
||||
return qtcStyle();
|
||||
|
||||
m_cachedStyle.setCache(qtcStyle(), 0ms);
|
||||
return m_cachedStyle.style;
|
||||
}
|
||||
|
||||
} // namespace ClangFormat
|
||||
|
||||
@@ -46,7 +46,7 @@ public:
|
||||
|
||||
std::optional<int> margin() const override;
|
||||
|
||||
clang::format::FormatStyle styleForFile() const;
|
||||
const clang::format::FormatStyle &styleForFile() const;
|
||||
|
||||
protected:
|
||||
virtual bool formatCodeInsteadOfIndent() const { return false; }
|
||||
@@ -71,6 +71,18 @@ private:
|
||||
ReplacementsToKeep replacementsToKeep,
|
||||
const QChar &typedChar = QChar::Null,
|
||||
bool secondTry = false) const;
|
||||
|
||||
struct CachedStyle {
|
||||
clang::format::FormatStyle style = clang::format::getNoStyle();
|
||||
QDateTime expirationTime;
|
||||
void setCache(clang::format::FormatStyle newStyle, std::chrono::milliseconds timeout)
|
||||
{
|
||||
style = newStyle;
|
||||
expirationTime = QDateTime::currentDateTime() + timeout;
|
||||
}
|
||||
};
|
||||
|
||||
mutable CachedStyle m_cachedStyle;
|
||||
};
|
||||
|
||||
} // namespace ClangFormat
|
||||
|
||||
@@ -31,7 +31,7 @@ using namespace Utils;
|
||||
|
||||
namespace ClangFormat {
|
||||
|
||||
clang::format::FormatStyle qtcStyle()
|
||||
clang::format::FormatStyle calculateQtcStyle()
|
||||
{
|
||||
clang::format::FormatStyle style = getLLVMStyle();
|
||||
style.Language = FormatStyle::LK_Cpp;
|
||||
@@ -179,6 +179,12 @@ clang::format::FormatStyle qtcStyle()
|
||||
return style;
|
||||
}
|
||||
|
||||
clang::format::FormatStyle qtcStyle()
|
||||
{
|
||||
static clang::format::FormatStyle style = calculateQtcStyle();
|
||||
return style;
|
||||
}
|
||||
|
||||
QString projectUniqueId(ProjectExplorer::Project *project)
|
||||
{
|
||||
if (!project)
|
||||
|
||||
Reference in New Issue
Block a user