Files
qt-creator/src/plugins/clangformat/clangformatbaseindenter.h
Christian Kandeler a39119669c CppEditor: Format quickfix code only if formatting is enabled
... in the ClangFormat settings (rather than the default of just
indenting).
As opposed to files generated by the wizard, quickfixes are often touching
existing code, and since ClangFormat works on line granularity, users
will experience unexpected re-formattings when ClangFormat is in indent-
only mode.
Therefore, do the formatting only if the user has enabled it in the
ClangFormat settings.

Change-Id: Icb30f166f2b6fb94113a8f25c4a5f92ff8bca9b0
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
2023-07-03 08:47:42 +00:00

90 lines
3.7 KiB
C++

// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <texteditor/indenter.h>
#include <clang/Format/Format.h>
namespace ClangFormat {
enum class ReplacementsToKeep { OnlyIndent, IndentAndBefore, All };
class ClangFormatBaseIndenter : public TextEditor::Indenter
{
public:
ClangFormatBaseIndenter(QTextDocument *doc);
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void indent(const QTextCursor &cursor,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void reindent(const QTextCursor &cursor,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void autoIndent(const QTextCursor &cursor,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
Utils::Text::Replacements format(const TextEditor::RangesInLines &rangesInLines,
FormattingMode mode = FormattingMode::Forced) override;
void indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
int indentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
bool isElectricCharacter(const QChar &ch) const override;
std::optional<int> margin() const override;
const clang::format::FormatStyle &styleForFile() const;
protected:
virtual bool formatCodeInsteadOfIndent() const { return false; }
virtual bool formatWhileTyping() const { return false; }
virtual int lastSaveRevision() const { return 0; }
private:
void indent(const QTextCursor &cursor, const QChar &typedChar, int cursorPositionInEditor);
void indentBlocks(const QTextBlock &startBlock,
const QTextBlock &endBlock,
const QChar &typedChar,
int cursorPositionInEditor);
Utils::Text::Replacements indentsFor(QTextBlock startBlock,
const QTextBlock &endBlock,
const QChar &typedChar,
int cursorPositionInEditor,
bool trimTrailingWhitespace = true);
Utils::Text::Replacements replacements(QByteArray buffer,
const QTextBlock &startBlock,
const QTextBlock &endBlock,
int cursorPositionInEditor,
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