Commit a7e5f80d authored by Christian Kamm's avatar Christian Kamm

QmlJS indenter: Only auto-reindent if indent was unchanged.

This change in how electric characters are handled has gone into the
C++ indenter a while ago and works well there. It means Creator is less
likely to annoyingly change the indent on lines where the
indentation whas changed manually.
It is still possible to trigger a reindent manually.

Reviewed-by: Erik Verbruggen
parent 465f02a5
...@@ -471,6 +471,18 @@ int CodeFormatter::indentFor(const QTextBlock &block) ...@@ -471,6 +471,18 @@ int CodeFormatter::indentFor(const QTextBlock &block)
return m_indentDepth; 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) void CodeFormatter::updateStateUntil(const QTextBlock &endBlock)
{ {
QStack<State> previousState = initialState(); QStack<State> previousState = initialState();
......
...@@ -66,6 +66,7 @@ public: ...@@ -66,6 +66,7 @@ public:
void updateLineStateChange(const QTextBlock &block); void updateLineStateChange(const QTextBlock &block);
int indentFor(const QTextBlock &block); int indentFor(const QTextBlock &block);
int indentForNewLineAfter(const QTextBlock &block);
void setTabSize(int tabSize); void setTabSize(int tabSize);
......
...@@ -66,7 +66,6 @@ void Indenter::indentBlock(QTextDocument *doc, ...@@ -66,7 +66,6 @@ void Indenter::indentBlock(QTextDocument *doc,
TextEditor::BaseTextEditor *editor) TextEditor::BaseTextEditor *editor)
{ {
Q_UNUSED(doc) Q_UNUSED(doc)
Q_UNUSED(typedChar)
Q_UNUSED(editor) Q_UNUSED(editor)
const TextEditor::TabSettings &ts = editor->tabSettings(); const TextEditor::TabSettings &ts = editor->tabSettings();
...@@ -74,5 +73,14 @@ void Indenter::indentBlock(QTextDocument *doc, ...@@ -74,5 +73,14 @@ void Indenter::indentBlock(QTextDocument *doc,
codeFormatter.updateStateUntil(block); codeFormatter.updateStateUntil(block);
const int depth = codeFormatter.indentFor(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); ts.indentLine(block, depth);
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment