diff --git a/src/libs/qmljs/qmljscodeformatter.cpp b/src/libs/qmljs/qmljscodeformatter.cpp index 5fb4a9ebcf1f2ecf27c2b38b2693741270a85c76..6914bb10e7416d5b8cabf51370654dc7c981b166 100644 --- a/src/libs/qmljs/qmljscodeformatter.cpp +++ b/src/libs/qmljs/qmljscodeformatter.cpp @@ -471,6 +471,18 @@ int CodeFormatter::indentFor(const QTextBlock &block) return m_indentDepth; } +int CodeFormatter::indentForNewLineAfter(const QTextBlock &block) +{ + restoreCurrentState(block); + + int lexerState = loadLexerState(block); + m_tokens.clear(); + m_currentLine.clear(); + adjustIndent(m_tokens, lexerState, &m_indentDepth); + + return m_indentDepth; +} + void CodeFormatter::updateStateUntil(const QTextBlock &endBlock) { QStack<State> previousState = initialState(); diff --git a/src/libs/qmljs/qmljscodeformatter.h b/src/libs/qmljs/qmljscodeformatter.h index 30e1bae6f6c696ff223f3689921c4172d9783cf9..52a4f6ed885102550bda083420aa435bb6ba97ee 100644 --- a/src/libs/qmljs/qmljscodeformatter.h +++ b/src/libs/qmljs/qmljscodeformatter.h @@ -66,6 +66,7 @@ public: void updateLineStateChange(const QTextBlock &block); int indentFor(const QTextBlock &block); + int indentForNewLineAfter(const QTextBlock &block); void setTabSize(int tabSize); diff --git a/src/plugins/qmljseditor/qmljsindenter.cpp b/src/plugins/qmljseditor/qmljsindenter.cpp index 99bd68a32e1ded504f02a48659f66971722599a4..8dddb7747f553df758ee22bca551e5dfd55f1f04 100644 --- a/src/plugins/qmljseditor/qmljsindenter.cpp +++ b/src/plugins/qmljseditor/qmljsindenter.cpp @@ -66,7 +66,6 @@ void Indenter::indentBlock(QTextDocument *doc, TextEditor::BaseTextEditor *editor) { Q_UNUSED(doc) - Q_UNUSED(typedChar) Q_UNUSED(editor) const TextEditor::TabSettings &ts = editor->tabSettings(); @@ -74,5 +73,14 @@ void Indenter::indentBlock(QTextDocument *doc, codeFormatter.updateStateUntil(block); const int depth = codeFormatter.indentFor(block); + + if (isElectricCharacter(typedChar)) { + // only reindent the current line when typing electric characters if the + // indent is the same it would be if the line were empty + const int newlineIndent = codeFormatter.indentForNewLineAfter(block.previous()); + if (ts.indentationColumn(block.text()) != newlineIndent) + return; + } + ts.indentLine(block, depth); }