2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2019 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2019-01-16 09:37:54 +01:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <texteditor/indenter.h>
|
|
|
|
|
|
|
|
|
|
#include <clang/Format/Format.h>
|
|
|
|
|
|
|
|
|
|
namespace ClangFormat {
|
|
|
|
|
|
2019-02-08 12:31:53 +01:00
|
|
|
enum class ReplacementsToKeep { OnlyIndent, IndentAndBefore, All };
|
2019-01-28 08:13:33 +01:00
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
class ClangFormatBaseIndenter : public TextEditor::Indenter
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ClangFormatBaseIndenter(QTextDocument *doc);
|
|
|
|
|
|
2019-01-28 08:11:20 +01:00
|
|
|
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
|
|
|
|
|
const TextEditor::TabSettings &tabSettings,
|
|
|
|
|
int cursorPositionInEditor = -1) override;
|
2019-01-16 09:37:54 +01:00
|
|
|
void indent(const QTextCursor &cursor,
|
|
|
|
|
const QChar &typedChar,
|
2019-01-28 08:11:20 +01:00
|
|
|
const TextEditor::TabSettings &tabSettings,
|
|
|
|
|
int cursorPositionInEditor = -1) override;
|
2019-01-16 09:37:54 +01:00
|
|
|
|
|
|
|
|
void reindent(const QTextCursor &cursor,
|
2019-01-28 08:11:20 +01:00
|
|
|
const TextEditor::TabSettings &tabSettings,
|
|
|
|
|
int cursorPositionInEditor = -1) override;
|
2019-01-16 09:37:54 +01:00
|
|
|
|
2019-11-12 12:43:16 +01:00
|
|
|
void autoIndent(const QTextCursor &cursor,
|
|
|
|
|
const TextEditor::TabSettings &tabSettings,
|
|
|
|
|
int cursorPositionInEditor = -1) override;
|
2023-07-04 10:05:33 +02:00
|
|
|
Utils::EditOperations format(const TextEditor::RangesInLines &rangesInLines,
|
|
|
|
|
FormattingMode mode = FormattingMode::Forced) override;
|
2019-01-16 09:37:54 +01:00
|
|
|
|
|
|
|
|
void indentBlock(const QTextBlock &block,
|
|
|
|
|
const QChar &typedChar,
|
2019-01-28 08:11:20 +01:00
|
|
|
const TextEditor::TabSettings &tabSettings,
|
|
|
|
|
int cursorPositionInEditor = -1) override;
|
2019-01-16 09:37:54 +01:00
|
|
|
|
2019-01-28 08:11:20 +01:00
|
|
|
int indentFor(const QTextBlock &block,
|
|
|
|
|
const TextEditor::TabSettings &tabSettings,
|
|
|
|
|
int cursorPositionInEditor = -1) override;
|
2019-01-16 09:37:54 +01:00
|
|
|
|
|
|
|
|
bool isElectricCharacter(const QChar &ch) const override;
|
|
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
std::optional<int> margin() const override;
|
2021-01-24 17:11:02 +01:00
|
|
|
|
2023-05-12 11:57:26 +02:00
|
|
|
const clang::format::FormatStyle &styleForFile() const;
|
2022-04-08 15:12:30 +02:00
|
|
|
|
2023-12-18 17:20:30 +01:00
|
|
|
void setOverriddenPreferences(TextEditor::ICodeStylePreferences *preferences);
|
|
|
|
|
|
2019-01-16 09:37:54 +01:00
|
|
|
protected:
|
2019-01-28 07:54:05 +01:00
|
|
|
virtual bool formatCodeInsteadOfIndent() const { return false; }
|
2022-06-28 16:17:02 +02:00
|
|
|
virtual bool formatWhileTyping() const { return false; }
|
2019-01-28 08:13:33 +01:00
|
|
|
virtual int lastSaveRevision() const { return 0; }
|
2019-01-16 09:37:54 +01:00
|
|
|
|
|
|
|
|
private:
|
2019-01-28 08:11:20 +01:00
|
|
|
void indent(const QTextCursor &cursor, const QChar &typedChar, int cursorPositionInEditor);
|
2019-02-19 14:30:52 +01:00
|
|
|
void indentBlocks(const QTextBlock &startBlock,
|
2019-02-18 15:56:50 +01:00
|
|
|
const QTextBlock &endBlock,
|
|
|
|
|
const QChar &typedChar,
|
|
|
|
|
int cursorPositionInEditor);
|
2023-07-04 10:05:33 +02:00
|
|
|
Utils::ChangeSet indentsFor(QTextBlock startBlock,
|
|
|
|
|
const QTextBlock &endBlock,
|
|
|
|
|
const QChar &typedChar,
|
|
|
|
|
int cursorPositionInEditor,
|
|
|
|
|
bool trimTrailingWhitespace = true);
|
|
|
|
|
Utils::ChangeSet replacements(QByteArray buffer,
|
|
|
|
|
const QTextBlock &startBlock,
|
|
|
|
|
const QTextBlock &endBlock,
|
|
|
|
|
int cursorPositionInEditor,
|
|
|
|
|
ReplacementsToKeep replacementsToKeep,
|
|
|
|
|
const QChar &typedChar = QChar::Null,
|
|
|
|
|
bool secondTry = false) const;
|
2023-05-12 11:57:26 +02:00
|
|
|
|
|
|
|
|
struct CachedStyle {
|
|
|
|
|
clang::format::FormatStyle style = clang::format::getNoStyle();
|
|
|
|
|
QDateTime expirationTime;
|
|
|
|
|
void setCache(clang::format::FormatStyle newStyle, std::chrono::milliseconds timeout)
|
|
|
|
|
{
|
|
|
|
|
style = newStyle;
|
2023-09-05 09:20:52 +02:00
|
|
|
expirationTime = QDateTime::currentDateTime().addMSecs(timeout.count());
|
2023-05-12 11:57:26 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mutable CachedStyle m_cachedStyle;
|
2023-12-18 17:20:30 +01:00
|
|
|
|
|
|
|
|
clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName) const;
|
|
|
|
|
TextEditor::ICodeStylePreferences *m_overriddenPreferences = nullptr;
|
2019-01-16 09:37:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace ClangFormat
|