forked from qt-creator/qt-creator
QmlJS indenter: Never touch indent of multi-line strings.
Task-number: QTCREATORBUG-6368 Change-Id: Iae68427e429e625214bc0729aaaf313dc3371570 Reviewed-by: Fawzi Mohamed <fawzi.mohamed@nokia.com>
This commit is contained in:
@@ -517,10 +517,10 @@ int CodeFormatter::indentForNewLineAfter(const QTextBlock &block)
|
||||
{
|
||||
restoreCurrentState(block);
|
||||
|
||||
int lexerState = loadLexerState(block);
|
||||
m_tokens.clear();
|
||||
m_currentLine.clear();
|
||||
adjustIndent(m_tokens, lexerState, &m_indentDepth);
|
||||
const int startLexerState = loadLexerState(block.previous());
|
||||
adjustIndent(m_tokens, startLexerState, &m_indentDepth);
|
||||
|
||||
return m_indentDepth;
|
||||
}
|
||||
@@ -665,10 +665,11 @@ void CodeFormatter::leave(bool statementDone)
|
||||
|
||||
void CodeFormatter::correctIndentation(const QTextBlock &block)
|
||||
{
|
||||
const int lexerState = tokenizeBlock(block);
|
||||
tokenizeBlock(block);
|
||||
Q_ASSERT(m_currentState.size() >= 1);
|
||||
|
||||
adjustIndent(m_tokens, lexerState, &m_indentDepth);
|
||||
const int startLexerState = loadLexerState(block.previous());
|
||||
adjustIndent(m_tokens, startLexerState, &m_indentDepth);
|
||||
}
|
||||
|
||||
bool CodeFormatter::tryInsideExpression(bool alsoExpression)
|
||||
|
||||
@@ -71,7 +71,7 @@ public:
|
||||
|
||||
protected:
|
||||
virtual void onEnter(int newState, int *indentDepth, int *savedIndentDepth) const = 0;
|
||||
virtual void adjustIndent(const QList<Token> &tokens, int lexerState, int *indentDepth) const = 0;
|
||||
virtual void adjustIndent(const QList<Token> &tokens, int startLexerState, int *indentDepth) const = 0;
|
||||
|
||||
struct State;
|
||||
class QMLJS_EXPORT BlockData
|
||||
|
||||
@@ -70,6 +70,8 @@ void Indenter::indentBlock(QTextDocument *doc,
|
||||
|
||||
codeFormatter.updateStateUntil(block);
|
||||
const int depth = codeFormatter.indentFor(block);
|
||||
if (depth == -1)
|
||||
return;
|
||||
|
||||
if (isElectricCharacter(typedChar)) {
|
||||
// only reindent the current line when typing electric characters if the
|
||||
|
||||
@@ -289,10 +289,8 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd
|
||||
}
|
||||
}
|
||||
|
||||
void QtStyleCodeFormatter::adjustIndent(const QList<Token> &tokens, int lexerState, int *indentDepth) const
|
||||
void QtStyleCodeFormatter::adjustIndent(const QList<Token> &tokens, int startLexerState, int *indentDepth) const
|
||||
{
|
||||
Q_UNUSED(lexerState)
|
||||
|
||||
State topState = state();
|
||||
State previousState = state(1);
|
||||
|
||||
@@ -304,6 +302,12 @@ void QtStyleCodeFormatter::adjustIndent(const QList<Token> &tokens, int lexerSta
|
||||
return;
|
||||
}
|
||||
}
|
||||
// don't touch multi-line strings at all
|
||||
if ((startLexerState & Scanner::MultiLineMask) == Scanner::MultiLineStringDQuote
|
||||
|| (startLexerState & Scanner::MultiLineMask) == Scanner::MultiLineStringSQuote) {
|
||||
*indentDepth = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
const int kind = extendedTokenKind(tokenAt(0));
|
||||
switch (kind) {
|
||||
|
||||
@@ -54,7 +54,7 @@ public:
|
||||
|
||||
protected:
|
||||
virtual void onEnter(int newState, int *indentDepth, int *savedIndentDepth) const;
|
||||
virtual void adjustIndent(const QList<QmlJS::Token> &tokens, int lexerState, int *indentDepth) const;
|
||||
virtual void adjustIndent(const QList<QmlJS::Token> &tokens, int startLexerState, int *indentDepth) const;
|
||||
|
||||
virtual void saveBlockData(QTextBlock *block, const BlockData &data) const;
|
||||
virtual bool loadBlockData(const QTextBlock &block, BlockData *data) const;
|
||||
|
||||
@@ -69,7 +69,9 @@ public:
|
||||
codeFormatter.updateStateUntil(block);
|
||||
|
||||
do {
|
||||
tabSettings.indentLine(block, codeFormatter.indentFor(block));
|
||||
const int depth = codeFormatter.indentFor(block);
|
||||
if (depth != -1)
|
||||
tabSettings.indentLine(block, depth);
|
||||
codeFormatter.updateLineStateChange(block);
|
||||
block = block.next();
|
||||
} while (block.isValid() && block != end);
|
||||
|
||||
@@ -95,8 +95,11 @@ private Q_SLOTS:
|
||||
void labelledStatements2();
|
||||
void labelledStatements3();
|
||||
void multilineTernaryInProperty();
|
||||
void multilineString();
|
||||
};
|
||||
|
||||
enum { DontCheck = -2, DontIndent = -1 };
|
||||
|
||||
struct Line {
|
||||
Line(QString l)
|
||||
: line(l)
|
||||
@@ -139,7 +142,7 @@ void checkIndent(QList<Line> data, int style = 0)
|
||||
int i = 0;
|
||||
foreach (const Line &l, data) {
|
||||
QTextBlock b = document.findBlockByLineNumber(i);
|
||||
if (l.expectedIndent != -1) {
|
||||
if (l.expectedIndent != DontCheck) {
|
||||
int actualIndent = formatter.indentFor(b);
|
||||
if (actualIndent != l.expectedIndent) {
|
||||
QFAIL(QString("Wrong indent in line %1 with text '%2', expected indent %3, got %4").arg(
|
||||
@@ -727,9 +730,9 @@ void tst_QMLCodeFormatter::strayElse()
|
||||
data << Line("Rectangle {")
|
||||
<< Line("onClicked: {", 4)
|
||||
<< Line(" while ( true ) {}")
|
||||
<< Line(" else", -1)
|
||||
<< Line(" else {", -1)
|
||||
<< Line(" }", -1)
|
||||
<< Line(" else", DontCheck)
|
||||
<< Line(" else {", DontCheck)
|
||||
<< Line(" }", DontCheck)
|
||||
<< Line("}");
|
||||
checkIndent(data);
|
||||
}
|
||||
@@ -1222,6 +1225,23 @@ void tst_QMLCodeFormatter::multilineTernaryInProperty()
|
||||
checkIndent(data);
|
||||
}
|
||||
|
||||
void tst_QMLCodeFormatter::multilineString()
|
||||
{
|
||||
QList<Line> data;
|
||||
data << Line("Item {")
|
||||
<< Line(" a: 'foo")
|
||||
<< Line(" bar", DontIndent)
|
||||
<< Line(" boo boo", DontIndent)
|
||||
<< Line(" end'", DontIndent)
|
||||
<< Line(" a: \"foo")
|
||||
<< Line(" bar", DontIndent)
|
||||
<< Line(" boo boo", DontIndent)
|
||||
<< Line(" end\"", DontIndent)
|
||||
<< Line("}")
|
||||
;
|
||||
checkIndent(data);
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_QMLCodeFormatter)
|
||||
#include "tst_qmlcodeformatter.moc"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user