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:
Artem Sokolovskii
2023-05-12 11:57:26 +02:00
parent 375db16ac4
commit 74f77f6407
3 changed files with 46 additions and 7 deletions

View File

@@ -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