diff options
author | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-11-13 07:29:21 +0100 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-11-14 09:29:41 +0000 |
commit | e9d0083ccd80c1edd09fb1dd5e97d48585b5507f (patch) | |
tree | 0697412c83d17b189db9dd2b70aa376b238c10b7 /src/plugins/texteditor | |
parent | 9a85ce88a7254d9d54c42930a5514f0dd6308ce0 (diff) | |
download | qt-creator-e9d0083ccd80c1edd09fb1dd5e97d48585b5507f.tar.gz |
ClangFormat: Do not format text but indent only
Provide the separate infrastructure for the formatting
but use it only when QTC_FORMAT_INSTEAD_OF_INDENT is
provided in run environment.
Fixes: QTCREATORBUG-21447
Fixes: QTCREATORBUG-21459
Change-Id: I1ad6fe23f5de17016c0c7b18749c6977fc03a22b
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
Diffstat (limited to 'src/plugins/texteditor')
-rw-r--r-- | src/plugins/texteditor/indenter.cpp | 7 | ||||
-rw-r--r-- | src/plugins/texteditor/indenter.h | 5 | ||||
-rw-r--r-- | src/plugins/texteditor/textdocument.cpp | 5 | ||||
-rw-r--r-- | src/plugins/texteditor/textdocument.h | 1 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditor.cpp | 12 |
5 files changed, 29 insertions, 1 deletions
diff --git a/src/plugins/texteditor/indenter.cpp b/src/plugins/texteditor/indenter.cpp index e5d939d29e..4e47a15abd 100644 --- a/src/plugins/texteditor/indenter.cpp +++ b/src/plugins/texteditor/indenter.cpp @@ -104,6 +104,13 @@ void Indenter::reindent(QTextDocument *doc, const QTextCursor &cursor, const Tab } } +void Indenter::format(QTextDocument *doc, + const QTextCursor &cursor, + const TabSettings &tabSettings) +{ + indent(doc, cursor, QChar::Null, tabSettings); +} + void Indenter::setCodeStylePreferences(ICodeStylePreferences *) { diff --git a/src/plugins/texteditor/indenter.h b/src/plugins/texteditor/indenter.h index 5b5cdb6a55..05a65c7243 100644 --- a/src/plugins/texteditor/indenter.h +++ b/src/plugins/texteditor/indenter.h @@ -67,6 +67,11 @@ public: const TabSettings &tabSettings, bool autoTriggered = true); + // By default just calls indent with default settings. + virtual void format(QTextDocument *doc, + const QTextCursor &cursor, + const TabSettings &tabSettings); + // Reindent at cursor. Selection will be adjusted according to the indentation // change of the first block. virtual void reindent(QTextDocument *doc, const QTextCursor &cursor, const TabSettings &tabSettings); diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp index 6bc47e3f56..7363988c60 100644 --- a/src/plugins/texteditor/textdocument.cpp +++ b/src/plugins/texteditor/textdocument.cpp @@ -426,6 +426,11 @@ void TextDocument::autoReindent(const QTextCursor &cursor) d->m_indenter->reindent(&d->m_document, cursor, tabSettings()); } +void TextDocument::autoFormat(const QTextCursor &cursor) +{ + d->m_indenter->format(&d->m_document, cursor, tabSettings()); +} + QTextCursor TextDocument::indent(const QTextCursor &cursor, bool blockSelection, int column, int *offset) { diff --git a/src/plugins/texteditor/textdocument.h b/src/plugins/texteditor/textdocument.h index e05d202ead..67d4eae0f9 100644 --- a/src/plugins/texteditor/textdocument.h +++ b/src/plugins/texteditor/textdocument.h @@ -90,6 +90,7 @@ public: void autoIndent(const QTextCursor &cursor, QChar typedChar = QChar::Null, bool autoTriggered = true); void autoReindent(const QTextCursor &cursor); + void autoFormat(const QTextCursor &cursor); QTextCursor indent(const QTextCursor &cursor, bool blockSelection = false, int column = 0, int *offset = nullptr); QTextCursor unindent(const QTextCursor &cursor, bool blockSelection = false, int column = 0, diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 37a57412bf..3ae7a79751 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -7132,11 +7132,21 @@ void TextEditorWidget::setIfdefedOutBlocks(const QList<BlockRange> &blocks) documentLayout->requestUpdate(); } +static bool applyFormattingInsteadOfIndentation() +{ + constexpr const char option[] = "QTC_FORMAT_INSTEAD_OF_INDENT"; + return qEnvironmentVariableIsSet(option); +} + void TextEditorWidget::format() { + static bool formattingInsteadOfIndentation = applyFormattingInsteadOfIndentation(); QTextCursor cursor = textCursor(); cursor.beginEditBlock(); - d->m_document->autoIndent(cursor, QChar::Null, false); + if (formattingInsteadOfIndentation) + d->m_document->autoFormat(cursor); + else + d->m_document->autoIndent(cursor); cursor.endEditBlock(); } |