diff options
author | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2019-01-28 08:11:20 +0100 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2019-01-31 10:25:18 +0000 |
commit | 1dee275f58e9f9df2e2719325157cfdec42fe1cf (patch) | |
tree | b5d3480d9484a9d0347de6d3d8219f31d70b602e /src | |
parent | 80fb0178fdd9ee82acfb22aa512c7514868e89d1 (diff) |
ClangFormat: Add cursor position to the indenter interface
Sometimes it's imnportant where the cursor currently is
to properly format the code without affecting the current line.
Change-Id: I8b1fb11d2303adb5f960c7cb80a0ed2e6e45010f
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Diffstat (limited to 'src')
24 files changed, 186 insertions, 96 deletions
diff --git a/src/plugins/android/javaindenter.cpp b/src/plugins/android/javaindenter.cpp index 270e9cf988..8e31a7aea9 100644 --- a/src/plugins/android/javaindenter.cpp +++ b/src/plugins/android/javaindenter.cpp @@ -48,7 +48,8 @@ bool JavaIndenter::isElectricCharacter(const QChar &ch) const void JavaIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { int indent = indentFor(block, tabSettings); if (typedChar == QLatin1Char('}')) @@ -56,7 +57,9 @@ void JavaIndenter::indentBlock(const QTextBlock &block, tabSettings.indentLine(block, qMax(0, indent)); } -int JavaIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) +int JavaIndenter::indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { QTextBlock previous = block.previous(); if (!previous.isValid()) diff --git a/src/plugins/android/javaindenter.h b/src/plugins/android/javaindenter.h index 8a93058e83..af186f5dfc 100644 --- a/src/plugins/android/javaindenter.h +++ b/src/plugins/android/javaindenter.h @@ -39,9 +39,12 @@ public: void indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; - int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override; + int indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; }; } // namespace Internal } // namespace Android diff --git a/src/plugins/clangformat/clangformatbaseindenter.cpp b/src/plugins/clangformat/clangformatbaseindenter.cpp index d86f0909a8..d69498d047 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.cpp +++ b/src/plugins/clangformat/clangformatbaseindenter.cpp @@ -274,15 +274,19 @@ ClangFormatBaseIndenter::ClangFormatBaseIndenter(QTextDocument *doc) {} TextEditor::IndentationForBlock ClangFormatBaseIndenter::indentationForBlocks( - const QVector<QTextBlock> &blocks, const TextEditor::TabSettings & /*tabSettings*/) + const QVector<QTextBlock> &blocks, + const TextEditor::TabSettings & /*tabSettings*/, + int cursorPositionInEditor) { TextEditor::IndentationForBlock ret; for (QTextBlock block : blocks) - ret.insert(block.blockNumber(), indentFor(block)); + ret.insert(block.blockNumber(), indentFor(block, cursorPositionInEditor)); return ret; } -void ClangFormatBaseIndenter::indent(const QTextCursor &cursor, const QChar &typedChar) +void ClangFormatBaseIndenter::indent(const QTextCursor &cursor, + const QChar &typedChar, + int cursorPositionInEditor) { if (cursor.hasSelection()) { // Calling currentBlock.next() might be unsafe because we change the document. @@ -294,30 +298,33 @@ void ClangFormatBaseIndenter::indent(const QTextCursor &cursor, const QChar &typ const QTextBlock currentBlock = m_doc->findBlockByNumber(currentBlockNumber); if (currentBlock.isValid()) { const int blocksAmount = m_doc->blockCount(); - indentBlock(currentBlock, typedChar); + indentBlock(currentBlock, typedChar, cursorPositionInEditor); QTC_CHECK(blocksAmount == m_doc->blockCount() && "ClangFormat plugin indentation changed the amount of blocks."); } } } else { - indentBlock(cursor.block(), typedChar); + indentBlock(cursor.block(), typedChar, cursorPositionInEditor); } } void ClangFormatBaseIndenter::indent(const QTextCursor &cursor, const QChar &typedChar, - const TextEditor::TabSettings & /*tabSettings*/) + const TextEditor::TabSettings & /*tabSettings*/, + int cursorPositionInEditor) { - indent(cursor, typedChar); + indent(cursor, typedChar, cursorPositionInEditor); } void ClangFormatBaseIndenter::reindent(const QTextCursor &cursor, - const TextEditor::TabSettings & /*tabSettings*/) + const TextEditor::TabSettings & /*tabSettings*/, + int cursorPositionInEditor) { - indent(cursor, QChar::Null); + indent(cursor, QChar::Null, cursorPositionInEditor); } -TextEditor::Replacements ClangFormatBaseIndenter::format(const QTextCursor &cursor) +TextEditor::Replacements ClangFormatBaseIndenter::format(const QTextCursor &cursor, + int cursorPositionInEditor) { int utf8Offset; int utf8Length; @@ -345,12 +352,16 @@ TextEditor::Replacements ClangFormatBaseIndenter::format(const QTextCursor &curs } TextEditor::Replacements ClangFormatBaseIndenter::format( - const QTextCursor &cursor, const TextEditor::TabSettings & /*tabSettings*/) + const QTextCursor &cursor, + const TextEditor::TabSettings & /*tabSettings*/, + int cursorPositionInEditor) { - return format(cursor); + return format(cursor, cursorPositionInEditor); } -void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar) +void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block, + const QChar &typedChar, + int /*cursorPositionInEditor*/) { trimFirstNonEmptyBlock(block); trimCurrentBlock(block); @@ -363,12 +374,13 @@ void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block, const QChar & void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings & /*tabSettings*/) + const TextEditor::TabSettings & /*tabSettings*/, + int cursorPositionInEditor) { - indentBlock(block, typedChar); + indentBlock(block, typedChar, cursorPositionInEditor); } -int ClangFormatBaseIndenter::indentFor(const QTextBlock &block) +int ClangFormatBaseIndenter::indentFor(const QTextBlock &block, int /*cursorPositionInEditor*/) { trimFirstNonEmptyBlock(block); trimCurrentBlock(block); @@ -388,9 +400,10 @@ int ClangFormatBaseIndenter::indentFor(const QTextBlock &block) } int ClangFormatBaseIndenter::indentFor(const QTextBlock &block, - const TextEditor::TabSettings & /*tabSettings*/) + const TextEditor::TabSettings & /*tabSettings*/, + int cursorPositionInEditor) { - return indentFor(block); + return indentFor(block, cursorPositionInEditor); } bool ClangFormatBaseIndenter::isElectricCharacter(const QChar &ch) const @@ -412,12 +425,12 @@ bool ClangFormatBaseIndenter::isElectricCharacter(const QChar &ch) const void ClangFormatBaseIndenter::formatOrIndent(const QTextCursor &cursor, const TextEditor::TabSettings & /*tabSettings*/, - int /*cursorPositionInEditor*/) + int cursorPositionInEditor) { if (formatCodeInsteadOfIndent()) - format(cursor); + format(cursor, cursorPositionInEditor); else - indent(cursor, QChar::Null); + indent(cursor, QChar::Null, cursorPositionInEditor); } clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const diff --git a/src/plugins/clangformat/clangformatbaseindenter.h b/src/plugins/clangformat/clangformatbaseindenter.h index fa880f5ea4..4071422cfd 100644 --- a/src/plugins/clangformat/clangformatbaseindenter.h +++ b/src/plugins/clangformat/clangformatbaseindenter.h @@ -36,26 +36,33 @@ class ClangFormatBaseIndenter : public TextEditor::Indenter public: ClangFormatBaseIndenter(QTextDocument *doc); - TextEditor::IndentationForBlock indentationForBlocks( - const QVector<QTextBlock> &blocks, const TextEditor::TabSettings & /*tabSettings*/) override; + 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*/) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; void reindent(const QTextCursor &cursor, - const TextEditor::TabSettings & /*tabSettings*/) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; void formatOrIndent(const QTextCursor &cursor, const TextEditor::TabSettings &tabSettings, int cursorPositionInEditor = -1) override; TextEditor::Replacements format(const QTextCursor &cursor, - const TextEditor::TabSettings & /*tabSettings*/) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; void indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings & /*tabSettings*/) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; - int indentFor(const QTextBlock &block, const TextEditor::TabSettings & /*tabSettings*/) override; + int indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; bool isElectricCharacter(const QChar &ch) const override; @@ -64,10 +71,10 @@ protected: virtual bool formatCodeInsteadOfIndent() const { return false; } private: - TextEditor::Replacements format(const QTextCursor &cursor); - void indent(const QTextCursor &cursor, const QChar &typedChar); - void indentBlock(const QTextBlock &block, const QChar &typedChar); - int indentFor(const QTextBlock &block); + TextEditor::Replacements format(const QTextCursor &cursor, int cursorPositionInEditor); + void indent(const QTextCursor &cursor, const QChar &typedChar, int cursorPositionInEditor); + void indentBlock(const QTextBlock &block, const QChar &typedChar, int cursorPositionInEditor); + int indentFor(const QTextBlock &block, int cursorPositionInEditor); TextEditor::Replacements replacements(QByteArray buffer, int utf8Offset, int utf8Length, diff --git a/src/plugins/cmakeprojectmanager/cmakeindenter.cpp b/src/plugins/cmakeprojectmanager/cmakeindenter.cpp index 3ba47a634d..e281d532db 100644 --- a/src/plugins/cmakeprojectmanager/cmakeindenter.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeindenter.cpp @@ -103,7 +103,8 @@ static int paranthesesLevel(const QString &line) } int CMakeIndenter::indentFor(const QTextBlock &block, - const TextEditor::TabSettings &tabSettings) + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { QTextBlock previousBlock = block.previous(); // find the next previous block that is non-empty (contains non-whitespace characters) diff --git a/src/plugins/cmakeprojectmanager/cmakeindenter.h b/src/plugins/cmakeprojectmanager/cmakeindenter.h index f7d80b0e0b..bb4377f8b6 100644 --- a/src/plugins/cmakeprojectmanager/cmakeindenter.h +++ b/src/plugins/cmakeprojectmanager/cmakeindenter.h @@ -39,7 +39,8 @@ public: bool isElectricCharacter(const QChar &ch) const override; int indentFor(const QTextBlock &block, - const TextEditor::TabSettings &tabSettings) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; }; } // namespace Internal diff --git a/src/plugins/cpptools/cppqtstyleindenter.cpp b/src/plugins/cpptools/cppqtstyleindenter.cpp index 97ffd666e3..fe9b17828e 100644 --- a/src/plugins/cpptools/cppqtstyleindenter.cpp +++ b/src/plugins/cpptools/cppqtstyleindenter.cpp @@ -93,7 +93,8 @@ static bool isElectricInLine(const QChar ch, const QString &text) void CppQtStyleIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings()); @@ -121,7 +122,8 @@ void CppQtStyleIndenter::indentBlock(const QTextBlock &block, void CppQtStyleIndenter::indent(const QTextCursor &cursor, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { if (cursor.hasSelection()) { QTextBlock block = m_doc->findBlock(cursor.selectionStart()); @@ -160,7 +162,8 @@ void CppQtStyleIndenter::invalidateCache() } int CppQtStyleIndenter::indentFor(const QTextBlock &block, - const TextEditor::TabSettings &tabSettings) + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings()); @@ -180,7 +183,9 @@ CppCodeStyleSettings CppQtStyleIndenter::codeStyleSettings() const } TextEditor::IndentationForBlock CppQtStyleIndenter::indentationForBlocks( - const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) + const QVector<QTextBlock> &blocks, + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings()); diff --git a/src/plugins/cpptools/cppqtstyleindenter.h b/src/plugins/cpptools/cppqtstyleindenter.h index 3b8fb07168..c8335d9893 100644 --- a/src/plugins/cpptools/cppqtstyleindenter.h +++ b/src/plugins/cpptools/cppqtstyleindenter.h @@ -46,17 +46,22 @@ public: bool isElectricCharacter(const QChar &ch) const override; void indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; void indent(const QTextCursor &cursor, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; void setCodeStylePreferences(TextEditor::ICodeStylePreferences *preferences) override; void invalidateCache() override; - int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override; - TextEditor::IndentationForBlock indentationForBlocks( - const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) override; + int indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; + TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; private: CppCodeStyleSettings codeStyleSettings() const; diff --git a/src/plugins/glsleditor/glslindenter.cpp b/src/plugins/glsleditor/glslindenter.cpp index 87b12f98e9..91229d5a5a 100644 --- a/src/plugins/glsleditor/glslindenter.cpp +++ b/src/plugins/glsleditor/glslindenter.cpp @@ -51,7 +51,8 @@ bool GlslIndenter::isElectricCharacter(const QChar &ch) const void GlslIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { // TODO: do something with it CppTools::QtStyleCodeFormatter @@ -78,7 +79,8 @@ void GlslIndenter::indentBlock(const QTextBlock &block, void GlslIndenter::indent(const QTextCursor &cursor, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { if (cursor.hasSelection()) { QTextBlock block = m_doc->findBlock(cursor.selectionStart()); @@ -107,7 +109,9 @@ void GlslIndenter::indent(const QTextCursor &cursor, } } -int GlslIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) +int GlslIndenter::indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, @@ -122,7 +126,9 @@ int GlslIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettin } TextEditor::IndentationForBlock GlslIndenter::indentationForBlocks( - const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) + const QVector<QTextBlock> &blocks, + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, diff --git a/src/plugins/glsleditor/glslindenter.h b/src/plugins/glsleditor/glslindenter.h index beb6aaa849..271d2c3ce3 100644 --- a/src/plugins/glsleditor/glslindenter.h +++ b/src/plugins/glsleditor/glslindenter.h @@ -39,15 +39,20 @@ public: bool isElectricCharacter(const QChar &ch) const override; void indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; void indent(const QTextCursor &cursor, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) override; - - int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override; - TextEditor::IndentationForBlock indentationForBlocks( - const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; + + int indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; + TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; }; } // namespace Internal diff --git a/src/plugins/nim/editor/nimindenter.cpp b/src/plugins/nim/editor/nimindenter.cpp index 0b61a4536b..84dad703b1 100644 --- a/src/plugins/nim/editor/nimindenter.cpp +++ b/src/plugins/nim/editor/nimindenter.cpp @@ -48,9 +48,11 @@ bool NimIndenter::isElectricCharacter(const QChar &ch) const void NimIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings &settings) + const TextEditor::TabSettings &settings, + int cursorPositionInEditor) { Q_UNUSED(typedChar); + Q_UNUSED(cursorPositionInEditor); const QString currentLine = block.text(); diff --git a/src/plugins/nim/editor/nimindenter.h b/src/plugins/nim/editor/nimindenter.h index 0342eda753..6f4457a813 100644 --- a/src/plugins/nim/editor/nimindenter.h +++ b/src/plugins/nim/editor/nimindenter.h @@ -44,7 +44,8 @@ public: void indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings &settings) override; + const TextEditor::TabSettings &settings, + int cursorPositionInEditor = -1) override; private: static const QSet<QChar> &electricCharacters(); diff --git a/src/plugins/pythoneditor/pythonindenter.cpp b/src/plugins/pythoneditor/pythonindenter.cpp index ee7813f327..a6fb880dcd 100644 --- a/src/plugins/pythoneditor/pythonindenter.cpp +++ b/src/plugins/pythoneditor/pythonindenter.cpp @@ -64,7 +64,9 @@ bool PythonIndenter::isElectricCharacter(const QChar &ch) const return ch == ':'; } -int PythonIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) +int PythonIndenter::indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { QTextBlock previousBlock = block.previous(); if (!previousBlock.isValid()) diff --git a/src/plugins/pythoneditor/pythonindenter.h b/src/plugins/pythoneditor/pythonindenter.h index 8ce10ba4fe..ff9378e007 100644 --- a/src/plugins/pythoneditor/pythonindenter.h +++ b/src/plugins/pythoneditor/pythonindenter.h @@ -35,7 +35,9 @@ public: explicit PythonIndenter(QTextDocument *doc); private: bool isElectricCharacter(const QChar &ch) const override; - int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override; + int indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; bool isElectricLine(const QString &line) const; int getIndentDiff(const QString &previousLine, diff --git a/src/plugins/qmljstools/qmljsindenter.cpp b/src/plugins/qmljstools/qmljsindenter.cpp index 3e156d756e..ce6f18eca9 100644 --- a/src/plugins/qmljstools/qmljsindenter.cpp +++ b/src/plugins/qmljstools/qmljsindenter.cpp @@ -53,7 +53,8 @@ bool Indenter::isElectricCharacter(const QChar &ch) const void Indenter::indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { const int depth = indentFor(block, tabSettings); if (depth == -1) @@ -79,7 +80,9 @@ void Indenter::invalidateCache() codeFormatter.invalidateCache(m_doc); } -int Indenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) +int Indenter::indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { QmlJSTools::CreatorCodeFormatter codeFormatter(tabSettings); codeFormatter.updateStateUntil(block); @@ -87,7 +90,9 @@ int Indenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings & } TextEditor::IndentationForBlock Indenter::indentationForBlocks( - const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) + const QVector<QTextBlock> &blocks, + const TextEditor::TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { QmlJSTools::CreatorCodeFormatter codeFormatter(tabSettings); diff --git a/src/plugins/qmljstools/qmljsindenter.h b/src/plugins/qmljstools/qmljsindenter.h index 3a51790556..24d3b8736b 100644 --- a/src/plugins/qmljstools/qmljsindenter.h +++ b/src/plugins/qmljstools/qmljsindenter.h @@ -41,12 +41,16 @@ public: bool isElectricCharacter(const QChar &ch) const override; void indentBlock(const QTextBlock &block, const QChar &typedChar, - const TextEditor::TabSettings &tabSettings) override; + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; void invalidateCache() override; - int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override; - TextEditor::IndentationForBlock indentationForBlocks( - const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) override; + int indentFor(const QTextBlock &block, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; + TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks, + const TextEditor::TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; }; } // Internal diff --git a/src/plugins/texteditor/indenter.h b/src/plugins/texteditor/indenter.h index a9fb5de7cd..bb23c9d7ac 100644 --- a/src/plugins/texteditor/indenter.h +++ b/src/plugins/texteditor/indenter.h @@ -76,47 +76,56 @@ public: virtual void invalidateCache() {} - virtual int indentFor(const QTextBlock & /*block*/, const TabSettings & /*tabSettings*/) + virtual int indentFor(const QTextBlock & /*block*/, + const TabSettings & /*tabSettings*/, + int /*cursorPositionInEditor*/ = -1) { return -1; } virtual void formatOrIndent(const QTextCursor &cursor, const TabSettings &tabSettings, - int /*cursorPositionInEditor*/ = -1) + int cursorPositionInEditor = -1) { - indent(cursor, QChar::Null, tabSettings); + indent(cursor, QChar::Null, tabSettings, cursorPositionInEditor); } // By default just calls indent with default settings. virtual Replacements format(const QTextCursor &cursor, - const TabSettings &tabSettings) + const TabSettings &tabSettings, + int cursorPositionInEditor = -1) { - indent(cursor, QChar::Null, tabSettings); + indent(cursor, QChar::Null, tabSettings, cursorPositionInEditor); return Replacements(); } // Expects a list of blocks in order of occurrence in the document. virtual IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks, - const TabSettings & /*tabSettings*/) + const TabSettings &tabSettings, + int cursorPositionInEditor = -1) = 0; virtual Utils::optional<TabSettings> tabSettings() const = 0; // Indent a text block based on previous line. Default does nothing virtual void indentBlock(const QTextBlock &block, const QChar &typedChar, - const TabSettings &tabSettings) + const TabSettings &tabSettings, + int cursorPositionInEditor = -1) = 0; // Indent at cursor. Calls indentBlock for selection or current line. virtual void indent(const QTextCursor &cursor, const QChar &typedChar, - const TabSettings &tabSettings) + const TabSettings &tabSettings, + int cursorPositionInEditor = -1) = 0; // Reindent at cursor. Selection will be adjusted according to the indentation // change of the first block. - virtual void reindent(const QTextCursor &cursor, const TabSettings &tabSettings) = 0; + virtual void reindent(const QTextCursor &cursor, + const TabSettings &tabSettings, + int cursorPositionInEditor = -1) + = 0; protected: QTextDocument *m_doc; diff --git a/src/plugins/texteditor/normalindenter.cpp b/src/plugins/texteditor/normalindenter.cpp index 7faea84c48..c6cb0febd1 100644 --- a/src/plugins/texteditor/normalindenter.cpp +++ b/src/plugins/texteditor/normalindenter.cpp @@ -54,9 +54,12 @@ NormalIndenter::NormalIndenter(QTextDocument *doc) : TextIndenter(doc) {} -int NormalIndenter::indentFor(const QTextBlock &block, const TabSettings &tabSettings) +int NormalIndenter::indentFor(const QTextBlock &block, + const TabSettings &tabSettings, + int cursorPositionInEditor) { Q_UNUSED(tabSettings); + Q_UNUSED(cursorPositionInEditor); QTextBlock previous = block.previous(); if (!previous.isValid()) diff --git a/src/plugins/texteditor/normalindenter.h b/src/plugins/texteditor/normalindenter.h index ece41fa52d..5a53caa4a3 100644 --- a/src/plugins/texteditor/normalindenter.h +++ b/src/plugins/texteditor/normalindenter.h @@ -35,7 +35,9 @@ public: explicit NormalIndenter(QTextDocument *doc); ~NormalIndenter() override = default; - int indentFor(const QTextBlock &block, const TabSettings &tabSettings) override; + int indentFor(const QTextBlock &block, + const TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; }; } // namespace TextEditor diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp index d96b463cd4..98096daee9 100644 --- a/src/plugins/texteditor/textdocument.cpp +++ b/src/plugins/texteditor/textdocument.cpp @@ -412,14 +412,14 @@ void TextDocument::setExtraEncodingSettings(const ExtraEncodingSettings &extraEn d->m_extraEncodingSettings = extraEncodingSettings; } -void TextDocument::autoIndent(const QTextCursor &cursor, QChar typedChar) +void TextDocument::autoIndent(const QTextCursor &cursor, QChar typedChar, int currentCursorPosition) { - d->m_indenter->indent(cursor, typedChar, tabSettings()); + d->m_indenter->indent(cursor, typedChar, tabSettings(), currentCursorPosition); } -void TextDocument::autoReindent(const QTextCursor &cursor) +void TextDocument::autoReindent(const QTextCursor &cursor, int currentCursorPosition) { - d->m_indenter->reindent(cursor, tabSettings()); + d->m_indenter->reindent(cursor, tabSettings(), currentCursorPosition); } void TextDocument::autoFormatOrIndent(const QTextCursor &cursor) diff --git a/src/plugins/texteditor/textdocument.h b/src/plugins/texteditor/textdocument.h index f71922f2d2..b7c771e748 100644 --- a/src/plugins/texteditor/textdocument.h +++ b/src/plugins/texteditor/textdocument.h @@ -87,8 +87,10 @@ public: void setIndenter(Indenter *indenter); Indenter *indenter() const; - void autoIndent(const QTextCursor &cursor, QChar typedChar = QChar::Null); - void autoReindent(const QTextCursor &cursor); + void autoIndent(const QTextCursor &cursor, + QChar typedChar = QChar::Null, + int currentCursorPosition = -1); + void autoReindent(const QTextCursor &cursor, int currentCursorPosition = -1); void autoFormatOrIndent(const QTextCursor &cursor); QTextCursor indent(const QTextCursor &cursor, bool blockSelection = false, int column = 0, int *offset = nullptr); diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 7907259306..4e9de84f10 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -2491,7 +2491,7 @@ void TextEditorWidget::keyPressEvent(QKeyEvent *e) --extraBlocks; ensureVisible.movePosition(QTextCursor::NextBlock); if (tps.m_autoIndent) - d->m_document->autoIndent(ensureVisible); + d->m_document->autoIndent(ensureVisible, QChar::Null, cursorPosition); else if (!previousIndentationString.isEmpty()) ensureVisible.insertText(previousIndentationString); if (d->m_animateAutoComplete || d->m_highlightAutoComplete) { @@ -2773,7 +2773,7 @@ void TextEditorWidget::keyPressEvent(QKeyEvent *e) cursor.setPosition(pos, QTextCursor::KeepAnchor); } if (!electricChar.isNull() && d->m_autoCompleter->contextAllowsElectricCharacters(cursor)) - d->m_document->autoIndent(cursor, electricChar); + d->m_document->autoIndent(cursor, electricChar, cursor.position()); if (!autoText.isEmpty()) cursor.setPosition(autoText.length() == 1 ? cursor.position() : cursor.anchor()); diff --git a/src/plugins/texteditor/textindenter.cpp b/src/plugins/texteditor/textindenter.cpp index 53801fabfa..3dab3e2c48 100644 --- a/src/plugins/texteditor/textindenter.cpp +++ b/src/plugins/texteditor/textindenter.cpp @@ -37,7 +37,8 @@ TextIndenter::TextIndenter(QTextDocument *doc) TextIndenter::~TextIndenter() = default; IndentationForBlock TextIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks, - const TabSettings &tabSettings) + const TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { IndentationForBlock ret; for (QTextBlock block : blocks) @@ -47,7 +48,8 @@ IndentationForBlock TextIndenter::indentationForBlocks(const QVector<QTextBlock> void TextIndenter::indentBlock(const QTextBlock &block, const QChar &typedChar, - const TabSettings &tabSettings) + const TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { Q_UNUSED(typedChar); const int indent = indentFor(block, tabSettings); @@ -58,7 +60,8 @@ void TextIndenter::indentBlock(const QTextBlock &block, void TextIndenter::indent(const QTextCursor &cursor, const QChar &typedChar, - const TabSettings &tabSettings) + const TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { if (cursor.hasSelection()) { QTextBlock block = m_doc->findBlock(cursor.selectionStart()); @@ -72,7 +75,9 @@ void TextIndenter::indent(const QTextCursor &cursor, } } -void TextIndenter::reindent(const QTextCursor &cursor, const TabSettings &tabSettings) +void TextIndenter::reindent(const QTextCursor &cursor, + const TabSettings &tabSettings, + int /*cursorPositionInEditor*/) { if (cursor.hasSelection()) { QTextBlock block = m_doc->findBlock(cursor.selectionStart()); diff --git a/src/plugins/texteditor/textindenter.h b/src/plugins/texteditor/textindenter.h index 51be65f409..d361713074 100644 --- a/src/plugins/texteditor/textindenter.h +++ b/src/plugins/texteditor/textindenter.h @@ -45,17 +45,21 @@ public: ~TextIndenter() override; IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks, - const TabSettings &tabSettings) override; + const TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; void indentBlock(const QTextBlock &block, const QChar &typedChar, - const TabSettings &tabSettings) override; + const TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; void indent(const QTextCursor &cursor, const QChar &typedChar, - const TabSettings &tabSettings) override; + const TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; - - void reindent(const QTextCursor &cursor, const TabSettings &tabSettings) override; + void reindent(const QTextCursor &cursor, + const TabSettings &tabSettings, + int cursorPositionInEditor = -1) override; Utils::optional<TabSettings> tabSettings() const override; }; |