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>
This commit is contained in:
Ivan Donchevskii
2019-01-28 08:11:20 +01:00
parent 80fb0178fd
commit 1dee275f58
24 changed files with 185 additions and 95 deletions

View File

@@ -48,7 +48,8 @@ bool JavaIndenter::isElectricCharacter(const QChar &ch) const
void JavaIndenter::indentBlock(const QTextBlock &block, void JavaIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
int indent = indentFor(block, tabSettings); int indent = indentFor(block, tabSettings);
if (typedChar == QLatin1Char('}')) if (typedChar == QLatin1Char('}'))
@@ -56,7 +57,9 @@ void JavaIndenter::indentBlock(const QTextBlock &block,
tabSettings.indentLine(block, qMax(0, indent)); 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(); QTextBlock previous = block.previous();
if (!previous.isValid()) if (!previous.isValid())

View File

@@ -39,9 +39,12 @@ public:
void indentBlock(const QTextBlock &block, void indentBlock(const QTextBlock &block,
const QChar &typedChar, 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 Internal
} // namespace Android } // namespace Android

View File

@@ -274,15 +274,19 @@ ClangFormatBaseIndenter::ClangFormatBaseIndenter(QTextDocument *doc)
{} {}
TextEditor::IndentationForBlock ClangFormatBaseIndenter::indentationForBlocks( 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; TextEditor::IndentationForBlock ret;
for (QTextBlock block : blocks) for (QTextBlock block : blocks)
ret.insert(block.blockNumber(), indentFor(block)); ret.insert(block.blockNumber(), indentFor(block, cursorPositionInEditor));
return ret; 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()) { if (cursor.hasSelection()) {
// Calling currentBlock.next() might be unsafe because we change the document. // 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); const QTextBlock currentBlock = m_doc->findBlockByNumber(currentBlockNumber);
if (currentBlock.isValid()) { if (currentBlock.isValid()) {
const int blocksAmount = m_doc->blockCount(); const int blocksAmount = m_doc->blockCount();
indentBlock(currentBlock, typedChar); indentBlock(currentBlock, typedChar, cursorPositionInEditor);
QTC_CHECK(blocksAmount == m_doc->blockCount() QTC_CHECK(blocksAmount == m_doc->blockCount()
&& "ClangFormat plugin indentation changed the amount of blocks."); && "ClangFormat plugin indentation changed the amount of blocks.");
} }
} }
} else { } else {
indentBlock(cursor.block(), typedChar); indentBlock(cursor.block(), typedChar, cursorPositionInEditor);
} }
} }
void ClangFormatBaseIndenter::indent(const QTextCursor &cursor, void ClangFormatBaseIndenter::indent(const QTextCursor &cursor,
const QChar &typedChar, 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, 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 utf8Offset;
int utf8Length; int utf8Length;
@@ -345,12 +352,16 @@ TextEditor::Replacements ClangFormatBaseIndenter::format(const QTextCursor &curs
} }
TextEditor::Replacements ClangFormatBaseIndenter::format( 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); trimFirstNonEmptyBlock(block);
trimCurrentBlock(block); trimCurrentBlock(block);
@@ -363,12 +374,13 @@ void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block, const QChar &
void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block, void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar, 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); trimFirstNonEmptyBlock(block);
trimCurrentBlock(block); trimCurrentBlock(block);
@@ -388,9 +400,10 @@ int ClangFormatBaseIndenter::indentFor(const QTextBlock &block)
} }
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 bool ClangFormatBaseIndenter::isElectricCharacter(const QChar &ch) const
@@ -412,12 +425,12 @@ bool ClangFormatBaseIndenter::isElectricCharacter(const QChar &ch) const
void ClangFormatBaseIndenter::formatOrIndent(const QTextCursor &cursor, void ClangFormatBaseIndenter::formatOrIndent(const QTextCursor &cursor,
const TextEditor::TabSettings & /*tabSettings*/, const TextEditor::TabSettings & /*tabSettings*/,
int /*cursorPositionInEditor*/) int cursorPositionInEditor)
{ {
if (formatCodeInsteadOfIndent()) if (formatCodeInsteadOfIndent())
format(cursor); format(cursor, cursorPositionInEditor);
else else
indent(cursor, QChar::Null); indent(cursor, QChar::Null, cursorPositionInEditor);
} }
clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const clang::format::FormatStyle ClangFormatBaseIndenter::styleForFile() const

View File

@@ -36,26 +36,33 @@ class ClangFormatBaseIndenter : public TextEditor::Indenter
public: public:
ClangFormatBaseIndenter(QTextDocument *doc); ClangFormatBaseIndenter(QTextDocument *doc);
TextEditor::IndentationForBlock indentationForBlocks( TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const QVector<QTextBlock> &blocks, const TextEditor::TabSettings & /*tabSettings*/) override; const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void indent(const QTextCursor &cursor, void indent(const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings & /*tabSettings*/) override; const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void reindent(const QTextCursor &cursor, void reindent(const QTextCursor &cursor,
const TextEditor::TabSettings & /*tabSettings*/) override; const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void formatOrIndent(const QTextCursor &cursor, void formatOrIndent(const QTextCursor &cursor,
const TextEditor::TabSettings &tabSettings, const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override; int cursorPositionInEditor = -1) override;
TextEditor::Replacements format(const QTextCursor &cursor, TextEditor::Replacements format(const QTextCursor &cursor,
const TextEditor::TabSettings & /*tabSettings*/) override; const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void indentBlock(const QTextBlock &block, void indentBlock(const QTextBlock &block,
const QChar &typedChar, 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; bool isElectricCharacter(const QChar &ch) const override;
@@ -64,10 +71,10 @@ protected:
virtual bool formatCodeInsteadOfIndent() const { return false; } virtual bool formatCodeInsteadOfIndent() const { return false; }
private: private:
TextEditor::Replacements format(const QTextCursor &cursor); TextEditor::Replacements format(const QTextCursor &cursor, int cursorPositionInEditor);
void indent(const QTextCursor &cursor, const QChar &typedChar); void indent(const QTextCursor &cursor, const QChar &typedChar, int cursorPositionInEditor);
void indentBlock(const QTextBlock &block, const QChar &typedChar); void indentBlock(const QTextBlock &block, const QChar &typedChar, int cursorPositionInEditor);
int indentFor(const QTextBlock &block); int indentFor(const QTextBlock &block, int cursorPositionInEditor);
TextEditor::Replacements replacements(QByteArray buffer, TextEditor::Replacements replacements(QByteArray buffer,
int utf8Offset, int utf8Offset,
int utf8Length, int utf8Length,

View File

@@ -103,7 +103,8 @@ static int paranthesesLevel(const QString &line)
} }
int CMakeIndenter::indentFor(const QTextBlock &block, int CMakeIndenter::indentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings) const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
QTextBlock previousBlock = block.previous(); QTextBlock previousBlock = block.previous();
// find the next previous block that is non-empty (contains non-whitespace characters) // find the next previous block that is non-empty (contains non-whitespace characters)

View File

@@ -39,7 +39,8 @@ public:
bool isElectricCharacter(const QChar &ch) const override; bool isElectricCharacter(const QChar &ch) const override;
int indentFor(const QTextBlock &block, int indentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings) override; const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -93,7 +93,8 @@ static bool isElectricInLine(const QChar ch, const QString &text)
void CppQtStyleIndenter::indentBlock(const QTextBlock &block, void CppQtStyleIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings()); QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
@@ -121,7 +122,8 @@ void CppQtStyleIndenter::indentBlock(const QTextBlock &block,
void CppQtStyleIndenter::indent(const QTextCursor &cursor, void CppQtStyleIndenter::indent(const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
if (cursor.hasSelection()) { if (cursor.hasSelection()) {
QTextBlock block = m_doc->findBlock(cursor.selectionStart()); QTextBlock block = m_doc->findBlock(cursor.selectionStart());
@@ -160,7 +162,8 @@ void CppQtStyleIndenter::invalidateCache()
} }
int CppQtStyleIndenter::indentFor(const QTextBlock &block, int CppQtStyleIndenter::indentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings) const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings()); QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
@@ -180,7 +183,9 @@ CppCodeStyleSettings CppQtStyleIndenter::codeStyleSettings() const
} }
TextEditor::IndentationForBlock CppQtStyleIndenter::indentationForBlocks( 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()); QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());

View File

@@ -46,17 +46,22 @@ public:
bool isElectricCharacter(const QChar &ch) const override; bool isElectricCharacter(const QChar &ch) const override;
void indentBlock(const QTextBlock &block, void indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) override; const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void indent(const QTextCursor &cursor, void indent(const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) override; const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void setCodeStylePreferences(TextEditor::ICodeStylePreferences *preferences) override; void setCodeStylePreferences(TextEditor::ICodeStylePreferences *preferences) override;
void invalidateCache() override; void invalidateCache() override;
int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override; int indentFor(const QTextBlock &block,
TextEditor::IndentationForBlock indentationForBlocks( const TextEditor::TabSettings &tabSettings,
const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) override; int cursorPositionInEditor = -1) override;
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
private: private:
CppCodeStyleSettings codeStyleSettings() const; CppCodeStyleSettings codeStyleSettings() const;

View File

@@ -51,7 +51,8 @@ bool GlslIndenter::isElectricCharacter(const QChar &ch) const
void GlslIndenter::indentBlock(const QTextBlock &block, void GlslIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
// TODO: do something with it // TODO: do something with it
CppTools::QtStyleCodeFormatter CppTools::QtStyleCodeFormatter
@@ -78,7 +79,8 @@ void GlslIndenter::indentBlock(const QTextBlock &block,
void GlslIndenter::indent(const QTextCursor &cursor, void GlslIndenter::indent(const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
if (cursor.hasSelection()) { if (cursor.hasSelection()) {
QTextBlock block = m_doc->findBlock(cursor.selectionStart()); 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 CppTools::QtStyleCodeFormatter
codeFormatter(tabSettings, codeFormatter(tabSettings,
@@ -122,7 +126,9 @@ int GlslIndenter::indentFor(const QTextBlock &block, const TextEditor::TabSettin
} }
TextEditor::IndentationForBlock GlslIndenter::indentationForBlocks( TextEditor::IndentationForBlock GlslIndenter::indentationForBlocks(
const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
CppTools::QtStyleCodeFormatter CppTools::QtStyleCodeFormatter
codeFormatter(tabSettings, codeFormatter(tabSettings,

View File

@@ -39,15 +39,20 @@ public:
bool isElectricCharacter(const QChar &ch) const override; bool isElectricCharacter(const QChar &ch) const override;
void indentBlock(const QTextBlock &block, void indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) override; const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void indent(const QTextCursor &cursor, void indent(const QTextCursor &cursor,
const QChar &typedChar, 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,
TextEditor::IndentationForBlock indentationForBlocks( const TextEditor::TabSettings &tabSettings,
const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) override; int cursorPositionInEditor = -1) override;
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -48,9 +48,11 @@ bool NimIndenter::isElectricCharacter(const QChar &ch) const
void NimIndenter::indentBlock(const QTextBlock &block, void NimIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &settings) const TextEditor::TabSettings &settings,
int cursorPositionInEditor)
{ {
Q_UNUSED(typedChar); Q_UNUSED(typedChar);
Q_UNUSED(cursorPositionInEditor);
const QString currentLine = block.text(); const QString currentLine = block.text();

View File

@@ -44,7 +44,8 @@ public:
void indentBlock(const QTextBlock &block, void indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &settings) override; const TextEditor::TabSettings &settings,
int cursorPositionInEditor = -1) override;
private: private:
static const QSet<QChar> &electricCharacters(); static const QSet<QChar> &electricCharacters();

View File

@@ -64,7 +64,9 @@ bool PythonIndenter::isElectricCharacter(const QChar &ch) const
return ch == ':'; 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(); QTextBlock previousBlock = block.previous();
if (!previousBlock.isValid()) if (!previousBlock.isValid())

View File

@@ -35,7 +35,9 @@ public:
explicit PythonIndenter(QTextDocument *doc); explicit PythonIndenter(QTextDocument *doc);
private: private:
bool isElectricCharacter(const QChar &ch) const override; 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; bool isElectricLine(const QString &line) const;
int getIndentDiff(const QString &previousLine, int getIndentDiff(const QString &previousLine,

View File

@@ -53,7 +53,8 @@ bool Indenter::isElectricCharacter(const QChar &ch) const
void Indenter::indentBlock(const QTextBlock &block, void Indenter::indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
const int depth = indentFor(block, tabSettings); const int depth = indentFor(block, tabSettings);
if (depth == -1) if (depth == -1)
@@ -79,7 +80,9 @@ void Indenter::invalidateCache()
codeFormatter.invalidateCache(m_doc); 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); QmlJSTools::CreatorCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
@@ -87,7 +90,9 @@ int Indenter::indentFor(const QTextBlock &block, const TextEditor::TabSettings &
} }
TextEditor::IndentationForBlock Indenter::indentationForBlocks( 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); QmlJSTools::CreatorCodeFormatter codeFormatter(tabSettings);

View File

@@ -41,12 +41,16 @@ public:
bool isElectricCharacter(const QChar &ch) const override; bool isElectricCharacter(const QChar &ch) const override;
void indentBlock(const QTextBlock &block, void indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TextEditor::TabSettings &tabSettings) override; const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void invalidateCache() override; void invalidateCache() override;
int indentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings) override; int indentFor(const QTextBlock &block,
TextEditor::IndentationForBlock indentationForBlocks( const TextEditor::TabSettings &tabSettings,
const QVector<QTextBlock> &blocks, const TextEditor::TabSettings &tabSettings) override; int cursorPositionInEditor = -1) override;
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
}; };
} // Internal } // Internal

View File

@@ -76,47 +76,56 @@ public:
virtual void invalidateCache() {} 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; return -1;
} }
virtual void formatOrIndent(const QTextCursor &cursor, virtual void formatOrIndent(const QTextCursor &cursor,
const TabSettings &tabSettings, 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. // By default just calls indent with default settings.
virtual Replacements format(const QTextCursor &cursor, 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(); return Replacements();
} }
// Expects a list of blocks in order of occurrence in the document. // Expects a list of blocks in order of occurrence in the document.
virtual IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks, virtual IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TabSettings & /*tabSettings*/) const TabSettings &tabSettings,
int cursorPositionInEditor = -1)
= 0; = 0;
virtual Utils::optional<TabSettings> tabSettings() const = 0; virtual Utils::optional<TabSettings> tabSettings() const = 0;
// Indent a text block based on previous line. Default does nothing // Indent a text block based on previous line. Default does nothing
virtual void indentBlock(const QTextBlock &block, virtual void indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TabSettings &tabSettings) const TabSettings &tabSettings,
int cursorPositionInEditor = -1)
= 0; = 0;
// Indent at cursor. Calls indentBlock for selection or current line. // Indent at cursor. Calls indentBlock for selection or current line.
virtual void indent(const QTextCursor &cursor, virtual void indent(const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
const TabSettings &tabSettings) const TabSettings &tabSettings,
int cursorPositionInEditor = -1)
= 0; = 0;
// Reindent at cursor. Selection will be adjusted according to the indentation // Reindent at cursor. Selection will be adjusted according to the indentation
// change of the first block. // 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: protected:
QTextDocument *m_doc; QTextDocument *m_doc;

View File

@@ -54,9 +54,12 @@ NormalIndenter::NormalIndenter(QTextDocument *doc)
: TextIndenter(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(tabSettings);
Q_UNUSED(cursorPositionInEditor);
QTextBlock previous = block.previous(); QTextBlock previous = block.previous();
if (!previous.isValid()) if (!previous.isValid())

View File

@@ -35,7 +35,9 @@ public:
explicit NormalIndenter(QTextDocument *doc); explicit NormalIndenter(QTextDocument *doc);
~NormalIndenter() override = default; ~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 } // namespace TextEditor

View File

@@ -412,14 +412,14 @@ void TextDocument::setExtraEncodingSettings(const ExtraEncodingSettings &extraEn
d->m_extraEncodingSettings = extraEncodingSettings; 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) void TextDocument::autoFormatOrIndent(const QTextCursor &cursor)

View File

@@ -87,8 +87,10 @@ public:
void setIndenter(Indenter *indenter); void setIndenter(Indenter *indenter);
Indenter *indenter() const; Indenter *indenter() const;
void autoIndent(const QTextCursor &cursor, QChar typedChar = QChar::Null); void autoIndent(const QTextCursor &cursor,
void autoReindent(const QTextCursor &cursor); QChar typedChar = QChar::Null,
int currentCursorPosition = -1);
void autoReindent(const QTextCursor &cursor, int currentCursorPosition = -1);
void autoFormatOrIndent(const QTextCursor &cursor); void autoFormatOrIndent(const QTextCursor &cursor);
QTextCursor indent(const QTextCursor &cursor, bool blockSelection = false, int column = 0, QTextCursor indent(const QTextCursor &cursor, bool blockSelection = false, int column = 0,
int *offset = nullptr); int *offset = nullptr);

View File

@@ -2491,7 +2491,7 @@ void TextEditorWidget::keyPressEvent(QKeyEvent *e)
--extraBlocks; --extraBlocks;
ensureVisible.movePosition(QTextCursor::NextBlock); ensureVisible.movePosition(QTextCursor::NextBlock);
if (tps.m_autoIndent) if (tps.m_autoIndent)
d->m_document->autoIndent(ensureVisible); d->m_document->autoIndent(ensureVisible, QChar::Null, cursorPosition);
else if (!previousIndentationString.isEmpty()) else if (!previousIndentationString.isEmpty())
ensureVisible.insertText(previousIndentationString); ensureVisible.insertText(previousIndentationString);
if (d->m_animateAutoComplete || d->m_highlightAutoComplete) { if (d->m_animateAutoComplete || d->m_highlightAutoComplete) {
@@ -2773,7 +2773,7 @@ void TextEditorWidget::keyPressEvent(QKeyEvent *e)
cursor.setPosition(pos, QTextCursor::KeepAnchor); cursor.setPosition(pos, QTextCursor::KeepAnchor);
} }
if (!electricChar.isNull() && d->m_autoCompleter->contextAllowsElectricCharacters(cursor)) 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()) if (!autoText.isEmpty())
cursor.setPosition(autoText.length() == 1 ? cursor.position() : cursor.anchor()); cursor.setPosition(autoText.length() == 1 ? cursor.position() : cursor.anchor());

View File

@@ -37,7 +37,8 @@ TextIndenter::TextIndenter(QTextDocument *doc)
TextIndenter::~TextIndenter() = default; TextIndenter::~TextIndenter() = default;
IndentationForBlock TextIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks, IndentationForBlock TextIndenter::indentationForBlocks(const QVector<QTextBlock> &blocks,
const TabSettings &tabSettings) const TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
IndentationForBlock ret; IndentationForBlock ret;
for (QTextBlock block : blocks) for (QTextBlock block : blocks)
@@ -47,7 +48,8 @@ IndentationForBlock TextIndenter::indentationForBlocks(const QVector<QTextBlock>
void TextIndenter::indentBlock(const QTextBlock &block, void TextIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TabSettings &tabSettings) const TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
Q_UNUSED(typedChar); Q_UNUSED(typedChar);
const int indent = indentFor(block, tabSettings); const int indent = indentFor(block, tabSettings);
@@ -58,7 +60,8 @@ void TextIndenter::indentBlock(const QTextBlock &block,
void TextIndenter::indent(const QTextCursor &cursor, void TextIndenter::indent(const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
const TabSettings &tabSettings) const TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{ {
if (cursor.hasSelection()) { if (cursor.hasSelection()) {
QTextBlock block = m_doc->findBlock(cursor.selectionStart()); 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()) { if (cursor.hasSelection()) {
QTextBlock block = m_doc->findBlock(cursor.selectionStart()); QTextBlock block = m_doc->findBlock(cursor.selectionStart());

View File

@@ -45,17 +45,21 @@ public:
~TextIndenter() override; ~TextIndenter() override;
IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks, IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TabSettings &tabSettings) override; const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void indentBlock(const QTextBlock &block, void indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
const TabSettings &tabSettings) override; const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void indent(const QTextCursor &cursor, void indent(const QTextCursor &cursor,
const QChar &typedChar, const QChar &typedChar,
const TabSettings &tabSettings) override; const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void reindent(const QTextCursor &cursor,
void reindent(const QTextCursor &cursor, const TabSettings &tabSettings) override; const TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
Utils::optional<TabSettings> tabSettings() const override; Utils::optional<TabSettings> tabSettings() const override;
}; };