forked from qt-creator/qt-creator
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:
@@ -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())
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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,
|
||||
|
@@ -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)
|
||||
|
@@ -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
|
||||
|
@@ -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());
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -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,
|
||||
|
@@ -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;
|
||||
const TextEditor::TabSettings &tabSettings,
|
||||
int cursorPositionInEditor = -1) 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;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
@@ -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();
|
||||
|
||||
|
@@ -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();
|
||||
|
@@ -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())
|
||||
|
@@ -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,
|
||||
|
@@ -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);
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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;
|
||||
|
@@ -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())
|
||||
|
@@ -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
|
||||
|
@@ -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)
|
||||
|
@@ -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);
|
||||
|
@@ -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());
|
||||
|
||||
|
@@ -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());
|
||||
|
@@ -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;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user